Search in sources :

Example 6 with EVMAssembler

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

the class AssemblerTest method assemblerTest1.

@Test
public void assemblerTest1() throws IOException, InterruptedException {
    String asm = "0x01 label1: JUMPDEST @label1 JUMPI";
    EVMAssembler assembler = new EVMAssembler();
    byte[] code = assembler.assemble(asm);
    byte[] compilerCode = { // PUSH1
    96, // 0x01
    1, // JUMPDEST
    91, // PUSH4
    99, 0, 0, 0, // Offset label
    2, // JUMPI
    87 };
    Assert.assertArrayEquals(code, compilerCode);
}
Also used : EVMAssembler(co.rsk.asm.EVMAssembler) Test(org.junit.Test)

Example 7 with EVMAssembler

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

the class BridgeTestPowerMock method testCallFromContract_afterOrchid.

@Test
public void testCallFromContract_afterOrchid() {
    doReturn(false).when(activationConfig).isActive(eq(RSKIP87), anyLong());
    doReturn(true).when(activationConfig).isActive(eq(RSKIP88), anyLong());
    blockFactory = new BlockFactory(activationConfig);
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(constants.getBridgeConstants().getBtcParams()), constants.getBridgeConstants(), activationConfig);
    PrecompiledContracts precompiledContracts = new PrecompiledContracts(config, bridgeSupportFactory);
    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"));
    // Run the program on the VM
    Program program = new Program(config.getVmConfig(), precompiledContracts, blockFactory, activationConfig.forBlock(0), code, invoke, tx, new HashSet<>());
    try {
        for (int i = 0; i < numOps; i++) {
            vm.step(program);
        }
        Assert.fail();
    } catch (RuntimeException e) {
        Assert.assertTrue(e.getMessage().contains("Non-local-call"));
    }
}
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)

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