Search in sources :

Example 1 with EVMAssembler

use of co.rsk.asm.EVMAssembler in project rskj by rsksmart.

the class VMTest method testCodereplace_1.

@Test
public void testCodereplace_1() {
    // CODEREPLACE is invalid if scriptVersion is zero
    VM vm = getSubject();
    String asm = // opHEADER, exevesion scriptversion extheaderlenth
    "0xFC 0x00 0x01 0x00 " + // address
    "0x00 " + // value
    "0x01 " + // stores 0x1 on address 0x00
    "MSTORE " + // adress
    "0x00 " + // length" +
    "0x01 " + "CODEREPLACE";
    EVMAssembler assembler = new EVMAssembler();
    byte[] code = assembler.assemble(asm);
    program = getProgram(code);
    // push
    vm.step(program);
    // push
    vm.step(program);
    // MSTORE
    vm.step(program);
    // push
    vm.step(program);
    // push
    vm.step(program);
    // CODEREPLACE
    vm.step(program);
}
Also used : ByteUtil.oneByteToHexString(org.ethereum.util.ByteUtil.oneByteToHexString) EVMAssembler(co.rsk.asm.EVMAssembler)

Example 2 with EVMAssembler

use of co.rsk.asm.EVMAssembler in project rskj by rsksmart.

the class VMTest method testCodereplace_0.

// ///////////////////////////////////////////////////////////////////////////////////////////
// Tests related to CODEREPLACE
@Test(expected = Program.IllegalOperationException.class)
public void testCodereplace_0() {
    // CODEREPLACE is invalid if scriptVersion is zero
    VM vm = getSubject();
    String asm = "256 0x00FF CODEREPLACE";
    EVMAssembler assembler = new EVMAssembler();
    byte[] code = assembler.assemble(asm);
    program = getProgram(code);
    // push
    vm.step(program);
    // push
    vm.step(program);
    // CODEREPLACE
    vm.step(program);
}
Also used : ByteUtil.oneByteToHexString(org.ethereum.util.ByteUtil.oneByteToHexString) EVMAssembler(co.rsk.asm.EVMAssembler)

Example 3 with EVMAssembler

use of co.rsk.asm.EVMAssembler in project rskj by rsksmart.

the class BridgeTestPowerMock method testCallFromContract_beforeOrchid.

@Test
public void testCallFromContract_beforeOrchid() {
    // GenesisConfig mockedConfig = spy(new GenesisConfig());
    // when(mockedConfig.isRskip88()).thenReturn(false);
    // config.setBlockchainConfig(mockedConfig);
    blockFactory = new BlockFactory(config.getActivationConfig());
    PrecompiledContracts precompiledContracts = new PrecompiledContracts(config, null);
    EVMAssembler assembler = new EVMAssembler();
    ProgramInvoke invoke = new ProgramInvokeMockImpl();
    // Save code on the sender's address so that the bridge
    // thinks its being called by a contract
    byte[] callerCode = assembler.assemble("0xaabb 0xccdd 0xeeff");
    invoke.getRepository().saveCode(new RskAddress(invoke.getOwnerAddress().getLast20Bytes()), callerCode);
    VM vm = new VM(config.getVmConfig(), precompiledContracts);
    // Encode a call to the bridge's getMinimumLockTxValue function
    // That means first pushing the corresponding encoded ABI storage to memory (MSTORE)
    // and then doing a DELEGATECALL to the corresponding address with the correct parameters
    String bridgeFunctionHex = ByteUtil.toHexString(Bridge.GET_MINIMUM_LOCK_TX_VALUE.encode());
    bridgeFunctionHex = String.format("0x%s%s", bridgeFunctionHex, String.join("", Collections.nCopies(32 * 2 - bridgeFunctionHex.length(), "0")));
    String asm = String.format("%s 0x00 MSTORE 0x20 0x30 0x20 0x00 0x0000000000000000000000000000000001000006 0x6000 DELEGATECALL", bridgeFunctionHex);
    int numOps = asm.split(" ").length;
    byte[] code = assembler.assemble(asm);
    // Mock a transaction, all we really need is a hash
    Transaction tx = mock(Transaction.class);
    when(tx.getHash()).thenReturn(new Keccak256("001122334455667788990011223344556677889900112233445566778899aabb"));
    try {
        // Run the program on the VM
        Program program = new Program(config.getVmConfig(), precompiledContracts, blockFactory, mock(ActivationConfig.ForBlock.class), code, invoke, tx, new HashSet<>());
        for (int i = 0; i < numOps; i++) {
            vm.step(program);
        }
        Assert.fail();
    } catch (NullPointerException e) {
        Assert.assertNull(e.getMessage());
    }
}
Also used : Program(org.ethereum.vm.program.Program) Keccak256(co.rsk.crypto.Keccak256) EVMAssembler(co.rsk.asm.EVMAssembler) ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) VM(org.ethereum.vm.VM) RskAddress(co.rsk.core.RskAddress) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with EVMAssembler

use of co.rsk.asm.EVMAssembler 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)

Example 5 with EVMAssembler

use of co.rsk.asm.EVMAssembler in project rskj by rsksmart.

the class CodeReplaceTest method replaceCodeTest2.

@Test
public void replaceCodeTest2() throws IOException, InterruptedException {
    // We test code replacement during initialization: this is forbitten.
    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 = // (4b) header script v1
    "HEADER !0x01 !0x01 !0x00 " + // (5b) we attempt to replace the code
    "0x01 0x00 CODEREPLACE " + // (7b) Extract real code into address 0, skip first 12 bytes, copy 1 bytes
    "0x01 0x15 0x00 CODECOPY " + // (5b) offset 0, size 0x01, now return the first code
    "0x01 0x00  RETURN " + // (1b) REAL code to install
    "STOP ";
    EVMAssembler assembler = new EVMAssembler();
    byte[] code = assembler.assemble(asm);
    // Creates a contract
    Transaction tx1 = createTx(blockchain, sender, new byte[0], code);
    TransactionExecutor executor1 = executeTransaction(blockchain, tx1);
    // Now we can directly check the store and see the new code.
    Assert.assertTrue(executor1.getResult().getException() != null);
}
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

EVMAssembler (co.rsk.asm.EVMAssembler)7 Test (org.junit.Test)5 RskAddress (co.rsk.core.RskAddress)2 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)2 Keccak256 (co.rsk.crypto.Keccak256)2 SimpleBtcTransaction (co.rsk.peg.bitcoin.SimpleBtcTransaction)2 BigInteger (java.math.BigInteger)2 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)2 Transaction (org.ethereum.core.Transaction)2 TransactionExecutor (org.ethereum.core.TransactionExecutor)2 ECKey (org.ethereum.crypto.ECKey)2 ByteUtil.oneByteToHexString (org.ethereum.util.ByteUtil.oneByteToHexString)2 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)2 VM (org.ethereum.vm.VM)2 Program (org.ethereum.vm.program.Program)2 ProgramInvoke (org.ethereum.vm.program.invoke.ProgramInvoke)2 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2