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);
}
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"));
}
}
Aggregations