Search in sources :

Example 1 with Bridge

use of co.rsk.peg.Bridge in project rskj by rsksmart.

the class Program method callToPrecompiledAddress.

public void callToPrecompiledAddress(MessageCall msg, PrecompiledContract contract) {
    if (getCallDeep() == MAX_DEPTH) {
        stackPushZero();
        this.refundGas(msg.getGas().longValue(), " call deep limit reach");
        return;
    }
    Repository track = getStorage().startTracking();
    RskAddress senderAddress = new RskAddress(getOwnerAddress());
    RskAddress codeAddress = new RskAddress(msg.getCodeAddress());
    RskAddress contextAddress = msg.getType().isStateless() ? senderAddress : codeAddress;
    Coin endowment = new Coin(msg.getEndowment().getData());
    Coin senderBalance = track.getBalance(senderAddress);
    if (senderBalance.compareTo(endowment) < 0) {
        stackPushZero();
        this.refundGas(msg.getGas().longValue(), "refund gas from message call");
        return;
    }
    byte[] data = this.memoryChunk(msg.getInDataOffs().intValue(), msg.getInDataSize().intValue());
    // Charge for endowment - is not reversible by rollback
    track.transfer(senderAddress, contextAddress, new Coin(msg.getEndowment().getData()));
    if (byTestingSuite()) {
        // This keeps track of the calls created for a test
        this.getResult().addCallCreate(data, codeAddress.getBytes(), msg.getGas().longValueSafe(), msg.getEndowment().getNoLeadZeroesData());
        stackPushOne();
        return;
    }
    long requiredGas = contract.getGasForData(data);
    if (requiredGas > msg.getGas().longValue()) {
        // matches cpp logic
        this.refundGas(0, "call pre-compiled");
        this.stackPushZero();
        track.rollback();
    } else {
        this.refundGas(msg.getGas().longValue() - requiredGas, "call pre-compiled");
        if (contract instanceof Bridge || contract instanceof RemascContract) {
            // CREATE CALL INTERNAL TRANSACTION
            InternalTransaction internalTx = addInternalTx(null, getGasLimit(), senderAddress, contextAddress, endowment, EMPTY_BYTE_ARRAY, "call");
            Block executionBlock = new Block(getPrevHash().getData(), EMPTY_BYTE_ARRAY, getCoinbase().getData(), EMPTY_BYTE_ARRAY, getDifficulty().getData(), getNumber().longValue(), getGasLimit().getData(), 0, getTimestamp().longValue(), EMPTY_BYTE_ARRAY, EMPTY_BYTE_ARRAY, EMPTY_BYTE_ARRAY, new ArrayList<>(), new ArrayList<>(), null);
            contract.init(internalTx, executionBlock, track, this.invoke.getBlockStore(), null, null);
        }
        byte[] out = contract.execute(data);
        this.memorySave(msg.getOutDataOffs().intValue(), out);
        this.stackPushOne();
        track.commit();
    }
}
Also used : Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) RemascContract(co.rsk.remasc.RemascContract) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block) Bridge(co.rsk.peg.Bridge)

Example 2 with Bridge

use of co.rsk.peg.Bridge in project rskj by rsksmart.

the class BridgePerformanceTestCase method execute.

private ExecutionTracker execute(ABIEncoder abiEncoder, BridgeStorageProviderInitializer storageInitializer, TxBuilder txBuilder, HeightProvider heightProvider, int executionIndex) {
    ExecutionTracker executionInfo = new ExecutionTracker(thread);
    RepositoryImpl repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
    storageInitializer.initialize(storageProvider, track, executionIndex);
    try {
        storageProvider.save();
    } catch (Exception e) {
        throw new RuntimeException("Error trying to save the storage after initialization", e);
    }
    track.commit();
    Transaction tx = txBuilder.build(executionIndex);
    List<LogInfo> logs = new ArrayList<>();
    RepositoryTrackWithBenchmarking benchmarkerTrack = new RepositoryTrackWithBenchmarking(config, repository);
    Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
    Blockchain blockchain = BlockChainBuilder.ofSizeWithNoTransactionPoolCleaner(heightProvider.getHeight(executionIndex));
    bridge.init(tx, blockchain.getBestBlock(), benchmarkerTrack, blockchain.getBlockStore(), null, logs);
    // Execute a random method so that bridge support initialization
    // does its initial writes to the repo for e.g. genesis block,
    // federation, etc, etc. and we don't get
    // those recorded in the actual execution.
    bridge.execute(Bridge.GET_FEDERATION_SIZE.encode());
    benchmarkerTrack.getStatistics().clear();
    executionInfo.startTimer();
    bridge.execute(abiEncoder.encode(executionIndex));
    executionInfo.endTimer();
    benchmarkerTrack.commit();
    executionInfo.setRepositoryStatistics(benchmarkerTrack.getStatistics());
    return executionInfo;
}
Also used : LogInfo(org.ethereum.vm.LogInfo) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) Blockchain(org.ethereum.core.Blockchain) ArrayList(java.util.ArrayList) RepositoryTrackWithBenchmarking(co.rsk.db.RepositoryTrackWithBenchmarking) Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) RepositoryImpl(co.rsk.db.RepositoryImpl) Bridge(co.rsk.peg.Bridge)

Aggregations

Bridge (co.rsk.peg.Bridge)2 Repository (org.ethereum.core.Repository)2 Coin (co.rsk.core.Coin)1 RskAddress (co.rsk.core.RskAddress)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 RepositoryTrackWithBenchmarking (co.rsk.db.RepositoryTrackWithBenchmarking)1 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)1 RemascContract (co.rsk.remasc.RemascContract)1 ArrayList (java.util.ArrayList)1 Block (org.ethereum.core.Block)1 Blockchain (org.ethereum.core.Blockchain)1 Transaction (org.ethereum.core.Transaction)1 LogInfo (org.ethereum.vm.LogInfo)1