Search in sources :

Example 1 with TransactionExecutor

use of org.ethereum.core.TransactionExecutor in project rskj by rsksmart.

the class CodeReplaceTest method executeTransaction.

public TransactionExecutor executeTransaction(BlockChainImpl blockchain, Transaction tx) {
    Repository track = blockchain.getRepository().startTracking();
    TransactionExecutor executor = new TransactionExecutor(config, tx, 0, RskAddress.nullAddress(), blockchain.getRepository(), blockchain.getBlockStore(), null, new ProgramInvokeFactoryImpl(), blockchain.getBestBlock());
    executor.init();
    executor.execute();
    executor.go();
    executor.finalization();
    track.commit();
    return executor;
}
Also used : Repository(org.ethereum.core.Repository) TransactionExecutor(org.ethereum.core.TransactionExecutor) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)

Example 2 with TransactionExecutor

use of org.ethereum.core.TransactionExecutor in project rskj by rsksmart.

the class ReversibleTransactionExecutor method executeTransaction.

public ProgramResult executeTransaction(Block executionBlock, RskAddress coinbase, byte[] gasPrice, byte[] gasLimit, byte[] toAddress, byte[] value, byte[] data, byte[] fromAddress) {
    Repository repository = track.getSnapshotTo(executionBlock.getStateRoot()).startTracking();
    byte[] nonce = repository.getNonce(new RskAddress(fromAddress)).toByteArray();
    UnsignedTransaction tx = new UnsignedTransaction(nonce, gasPrice, gasLimit, toAddress, value, data, fromAddress);
    TransactionExecutor executor = new TransactionExecutor(config, tx, 0, coinbase, repository, blockStore, receiptStore, programInvokeFactory, executionBlock).setLocalCall(true);
    executor.init();
    executor.execute();
    executor.go();
    executor.finalization();
    return executor.getResult();
}
Also used : Repository(org.ethereum.core.Repository) TransactionExecutor(org.ethereum.core.TransactionExecutor)

Example 3 with TransactionExecutor

use of org.ethereum.core.TransactionExecutor in project rskj by rsksmart.

the class ReversibleTransactionExecutor method reversibleExecution.

private TransactionExecutor reversibleExecution(RepositorySnapshot snapshot, Block executionBlock, RskAddress coinbase, byte[] gasPrice, byte[] gasLimit, byte[] toAddress, byte[] value, byte[] data, RskAddress fromAddress) {
    Repository track = snapshot.startTracking();
    byte[] nonce = track.getNonce(fromAddress).toByteArray();
    UnsignedTransaction tx = new UnsignedTransaction(nonce, gasPrice, gasLimit, toAddress, value, data, fromAddress);
    TransactionExecutor executor = transactionExecutorFactory.newInstance(tx, 0, coinbase, track, executionBlock, 0).setLocalCall(true);
    executor.executeTransaction();
    return executor;
}
Also used : Repository(org.ethereum.core.Repository) TransactionExecutor(org.ethereum.core.TransactionExecutor)

Example 4 with TransactionExecutor

use of org.ethereum.core.TransactionExecutor in project rskj by rsksmart.

the class PendingState method executeTransaction.

private void executeTransaction(Repository currentRepository, Transaction tx) {
    LOGGER.trace("Apply pending state tx: {} {}", toBI(tx.getNonce()), tx.getHash());
    TransactionExecutor executor = transactionExecutorFactory.newInstance(currentRepository, tx);
    executor.executeTransaction();
}
Also used : TransactionExecutor(org.ethereum.core.TransactionExecutor)

Example 5 with TransactionExecutor

use of org.ethereum.core.TransactionExecutor in project rskj by rsksmart.

the class CodeReplaceTest method replaceCodeTest1.

@Test
public void replaceCodeTest1() throws IOException, InterruptedException {
    BigInteger nonce = config.getBlockchainConfig().getCommonConstants().getInitialNonce();
    BlockChainImpl blockchain = org.ethereum.core.ImportLightTest.createBlockchain(GenesisLoader.loadGenesis(config, nonce, getClass().getResourceAsStream("/genesis/genesis-light.json"), false));
    ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
    System.out.println("address: " + Hex.toHexString(sender.getAddress()));
    String asm = // (7b) Extract real code into address 0, skip first 12 bytes, copy 20 bytes
    "0x14 0x0C 0x00 CODECOPY " + // (5b) offset 0, size 0x14, now return the first code
    "0x14 0x00  RETURN " + // (4b) header script v1
    "HEADER !0x01 !0x01 !0x00 " + // (3b) store at offset 0, read data from offset 0. Transfer 32 bytes
    "0x00 CALLDATALOAD " + // (3b) store the data at address 0
    "0x00 MSTORE " + // We replace TWO TIMES to make sure that only the last replacement takes place
    "0x01 0x00 CODEREPLACE " + // This is the good one.
    "0x10 0x00 CODEREPLACE";
    // (5b) set new code: offset 0, size 16 bytes.
    EVMAssembler assembler = new EVMAssembler();
    byte[] code = assembler.assemble(asm);
    // Creates a contract
    Transaction tx1 = createTx(blockchain, sender, new byte[0], code);
    executeTransaction(blockchain, tx1);
    // Now we can directly check the store and see the new code.
    RskAddress createdContract = tx1.getContractAddress();
    byte[] expectedCode = Arrays.copyOfRange(code, 12, 12 + 20);
    byte[] installedCode = blockchain.getRepository().getContractDetails(createdContract).getCode();
    // assert the contract has been created
    Assert.assertTrue(Arrays.equals(expectedCode, installedCode));
    // Note that this code does not have a header, then its version == 0
    String asm2 = // (5b) Store at address 0x00, the value 0xFF
    "0xFF 0x00 MSTORE " + // (5b) And return such value 0xFF, at offset 0x1F
    "0x01 0x1F RETURN " + // fill with nops to make it 16 bytes in length
    "STOP STOP STOP STOP STOP STOP";
    // 16
    byte[] code2 = assembler.assemble(asm2);
    // The second transaction changes the contract code
    Transaction tx2 = createTx(blockchain, sender, tx1.getContractAddress().getBytes(), code2);
    TransactionExecutor executor2 = executeTransaction(blockchain, tx2);
    byte[] installedCode2 = blockchain.getRepository().getContractDetails(createdContract).getCode();
    // assert the contract code has been created
    Assert.assertTrue(Arrays.equals(installedCode2, code2));
    // there is one code change
    Assert.assertEquals(1, executor2.getResult().getCodeChanges().size());
    // We could add a third tx to execute the new code
    Transaction tx3 = createTx(blockchain, sender, tx1.getContractAddress().getBytes(), new byte[0]);
    TransactionExecutor executor3 = executeTransaction(blockchain, tx3);
    // check return code from contract call
    Assert.assertArrayEquals(Hex.decode("FF"), executor3.getResult().getHReturn());
}
Also used : Transaction(org.ethereum.core.Transaction) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) BigInteger(java.math.BigInteger) ECKey(org.ethereum.crypto.ECKey) TransactionExecutor(org.ethereum.core.TransactionExecutor) EVMAssembler(co.rsk.asm.EVMAssembler) Test(org.junit.Test)

Aggregations

TransactionExecutor (org.ethereum.core.TransactionExecutor)6 Repository (org.ethereum.core.Repository)3 EVMAssembler (co.rsk.asm.EVMAssembler)2 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)2 BigInteger (java.math.BigInteger)2 Transaction (org.ethereum.core.Transaction)2 ECKey (org.ethereum.crypto.ECKey)2 Test (org.junit.Test)2 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)1