Search in sources :

Example 1 with VM

use of org.ethereum.vm.VM 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 2 with VM

use of org.ethereum.vm.VM in project rskj by rsksmart.

the class VMPerformanceTest method testVMPerformance1.

private void testVMPerformance1(ResultLogger resultLogger) {
    maxLLSize = 1000 * 1000 * 10;
    createLarseSetOfMemoryObjects();
    thread = ManagementFactory.getThreadMXBean();
    if (!thread.isThreadCpuTimeSupported())
        return;
    Boolean old = thread.isThreadCpuTimeEnabled();
    thread.setThreadCpuTimeEnabled(true);
    vm = new VM(config.getVmConfig(), new PrecompiledContracts(config, null));
    if (useProfiler)
        waitForProfiler();
    System.out.println("Configuration: shortArg =  " + shortArg.toString());
    // Program
    // push "A0", pop it
    measureProgram("PUSH/POP", Hex.decode("60A0" + "50"), 2, 2, 0, 100, null);
    // measureOpcode(OpCode.NOT, false, 0); // For reference, to see general overhead
    // measureOpcode(OpCode.NOT, false,0); // Re-measure to see if JIT does something
    measure11Opcodes(resultLogger);
    measure21Opcodes(resultLogger);
    measure31Opcodes(resultLogger);
    thread.setThreadCpuTimeEnabled(old);
}
Also used : PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) VM(org.ethereum.vm.VM)

Example 3 with VM

use of org.ethereum.vm.VM in project rskj by rsksmart.

the class VMPerformanceTest method testFibonacciLongTime.

// 
@Ignore
@Test
public void testFibonacciLongTime() {
    /**
     ******************************************************************************************
     *  This is the Solidity contract that was compiled:
     *
     *         contract Fibonacci50Times {
     *         function()  {
     *             uint256 a;
     *             uint256 b;
     *             uint256 c;
     *             for (uint k = 0; k < 1000; k++) {  // The maximum value of k was varied
     *                 a=0;
     *                 b=1;
     *                 for (uint i = 1; i < 50; i++) {
     *                     c = a+b;
     *                     a = b;
     *                     b = c;
     *                 }
     *             }
     *             assembly {
     *                 mstore(0x0, b)
     *                 return(0x0, 32)
     *             }
     *         }
     *         } // contract
     *******************************************************************************************
     */
    vm = new VM(config.getVmConfig(), new PrecompiledContracts(config, null));
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    // To increase precesion of the measurement, the maximum k value was increased
    // until the contract took more than 30 seconds
    // max k=100
    // byte[] codePlusPrefix = Hex.decode("6060604052607e8060106000396000f360606040523615600d57600d565b607c5b60006000600060006000600091505b6064821015606b57600094508450600193508350600190505b6032811015605e5783850192508250839450845082935083505b80806001019150506038565b5b8180600101925050601f565b8360005260206000f35b5050505050565b00");
    // max k=1000
    // byte[] codePlusPrefix = Hex.decode("6060604052607f8060106000396000f360606040523615600d57600d565b607d5b60006000600060006000600091505b6103e8821015606c57600094508450600193508350600190505b6032811015605f5783850192508250839450845082935083505b80806001019150506039565b5b8180600101925050601f565b8360005260206000f35b5050505050565b00");
    // max k=10K
    // byte[] codePlusPrefix = Hex.decode("6060604052607f8060106000396000f360606040523615600d57600d565b607d5b60006000600060006000600091505b612710821015606c57600094508450600193508350600190505b6032811015605f5783850192508250839450845082935083505b80806001019150506039565b5b8180600101925050601f565b8360005260206000f35b5050505050565b00");
    // max k=100K
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    byte[] codePlusPrefix = Hex.decode("606060405260808060106000396000f360606040523615600d57600d565b607e5b60006000600060006000600091505b620186a0821015606d57600094508450600193508350600190505b603281101560605783850192508250839450845082935083505b8080600101915050603a565b5b8180600101925050601f565b8360005260206000f35b5050505050565b00");
    byte[] code = Arrays.copyOfRange(codePlusPrefix, 16, codePlusPrefix.length);
    // vm.computeGas = false;
    // 50
    String s_expected = "00000000000000000000000000000000000000000000000000000002EE333961";
    System.out.println("Creating a large set of linked memory objects to force GC...");
    // 10 million linked objects.
    maxLLSize = 10 * 1000 * 1000;
    createLarseSetOfMemoryObjects();
    System.out.println("done.");
    testRunTime(code, s_expected);
}
Also used : PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) VM(org.ethereum.vm.VM) Ignore(org.junit.Ignore) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 4 with VM

use of org.ethereum.vm.VM in project rskj by rsksmart.

the class VMExecutionTest method executeCode.

private Program executeCode(byte[] code, int nsteps) {
    VM vm = new VM(vmConfig, precompiledContracts);
    Program program = new Program(vmConfig, precompiledContracts, mock(BlockchainConfig.class), code, invoke, null);
    for (int k = 0; k < nsteps; k++) vm.step(program);
    return program;
}
Also used : BlockchainConfig(org.ethereum.config.BlockchainConfig) Program(org.ethereum.vm.program.Program) VM(org.ethereum.vm.VM)

Example 5 with VM

use of org.ethereum.vm.VM in project rskj by rsksmart.

the class AltBN128Test method executeCode.

private Program executeCode(String code) {
    VmConfig vmConfig = config.getVmConfig();
    BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
    ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl();
    BytecodeCompiler compiler = new BytecodeCompiler();
    byte[] compiledCode = compiler.compile(code);
    VM vm = new VM(vmConfig, precompiledContracts);
    Program program = new Program(vmConfig, precompiledContracts, blockFactory, activations, compiledCode, invoke, null, new HashSet<>());
    vm.play(program);
    return program;
}
Also used : ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Program(org.ethereum.vm.program.Program) BlockFactory(org.ethereum.core.BlockFactory) VM(org.ethereum.vm.VM) BytecodeCompiler(co.rsk.vm.BytecodeCompiler) VmConfig(co.rsk.config.VmConfig)

Aggregations

VM (org.ethereum.vm.VM)11 Program (org.ethereum.vm.program.Program)9 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)5 Test (org.junit.Test)5 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)4 RskAddress (co.rsk.core.RskAddress)3 ProgramInvoke (org.ethereum.vm.program.invoke.ProgramInvoke)3 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)3 EVMAssembler (co.rsk.asm.EVMAssembler)2 Keccak256 (co.rsk.crypto.Keccak256)2 SimpleBtcTransaction (co.rsk.peg.bitcoin.SimpleBtcTransaction)2 DataWord (org.ethereum.vm.DataWord)2 Ignore (org.junit.Ignore)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 VmConfig (co.rsk.config.VmConfig)1 Coin (co.rsk.core.Coin)1 BytecodeCompiler (co.rsk.vm.BytecodeCompiler)1 IOException (java.io.IOException)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 BigInteger (java.math.BigInteger)1