Search in sources :

Example 21 with PrecompiledContracts

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

the class RemascStorageProviderTest method paysOnlyBlocksWithEnoughBalanceAccumulatedAfterRFS.

@Test
public void paysOnlyBlocksWithEnoughBalanceAccumulatedAfterRFS() throws IOException {
    Constants constants = spy(Constants.testnet(null));
    // we need to pass chain id check, and make believe that testnet config has same chain id as cow account
    when(constants.getChainId()).thenReturn(Constants.REGTEST_CHAIN_ID);
    when(constants.getMinimumPayableGas()).thenReturn(BigInteger.valueOf(21000L));
    RskSystemProperties config = spy(new TestSystemProperties());
    when(config.getNetworkConstants()).thenReturn(constants);
    long txValue = 10000;
    long gasLimit = 100000L;
    long gasPrice = 10L;
    long lowGasPrice = 1L;
    long minerFee = 21000;
    RskAddress coinbase = randomAddress();
    BlockChainBuilder builder = new BlockChainBuilder().setTesting(true).setGenesis(genesisBlock).setConfig(config);
    RemascTestRunner testRunner = new RemascTestRunner(builder, this.genesisBlock).txValue(txValue).minerFee(minerFee).initialHeight(13).siblingElements(new ArrayList<>()).txSigningKey(this.cowKey).gasPrice(gasPrice);
    testRunner.setFixedCoinbase(coinbase);
    testRunner.start();
    Blockchain blockchain = testRunner.getBlockChain();
    RepositoryLocator repositoryLocator = builder.getRepositoryLocator();
    List<Block> blocks = new ArrayList<>();
    blocks.add(RemascTestRunner.createBlock(genesisBlock, blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, gasPrice, 14, txValue, cowKey, null));
    blocks.add(RemascTestRunner.createBlock(genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, lowGasPrice, 15, txValue, cowKey, null));
    blocks.add(RemascTestRunner.createBlock(genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, gasPrice, 16, txValue, cowKey, null));
    blocks.add(RemascTestRunner.createBlock(genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, lowGasPrice, 17, txValue, cowKey, null));
    blocks.addAll(createSimpleBlocks(blocks.get(blocks.size() - 1), 10, coinbase));
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
    BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), repositoryLocator, new TransactionExecutorFactory(config, builder.getBlockStore(), null, new BlockFactory(config.getActivationConfig()), new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
    for (Block b : blocks) {
        blockExecutor.executeAndFillAll(b, blockchain.getBestBlock().getHeader());
        Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(b));
        RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
        long blockNumber = blockchain.getBestBlock().getNumber();
        if (blockNumber == 24) {
            // before first special block
            assertEquals(Coin.valueOf(1663200L), RemascTestRunner.getAccountBalance(repository, coinbase));
        } else if (blockNumber == 25 || blockNumber == 26) {
            // after first and second special block
            assertEquals(Coin.valueOf(1829520L), RemascTestRunner.getAccountBalance(repository, coinbase));
        } else if (blockNumber == 27 || blockNumber == 28) {
            // after third and fourth special block
            assertEquals(Coin.valueOf(1999167L), RemascTestRunner.getAccountBalance(repository, coinbase));
        }
    }
}
Also used : BlockExecutor(co.rsk.core.bc.BlockExecutor) Constants(org.ethereum.config.Constants) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) RskAddress(co.rsk.core.RskAddress) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) RskSystemProperties(co.rsk.config.RskSystemProperties) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 22 with PrecompiledContracts

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

the class MinerHelper method processBlock.

public void processBlock(Block block, Block parent) {
    latestStateRootHash = null;
    totalGasUsed = 0;
    totalPaidFees = Coin.ZERO;
    txReceipts = new ArrayList<>();
    Repository track = repositoryLocator.startTrackingAt(parent.getHeader());
    // this variable is set before iterating transactions in case list is empty
    latestStateRootHash = track.getRoot();
    // RSK test, remove
    String stateHash1 = ByteUtil.toHexString(blockchain.getBestBlock().getStateRoot());
    String stateHash2 = ByteUtil.toHexString(repository.getRoot());
    if (stateHash1.compareTo(stateHash2) != 0) {
        logger.error("Strange state in block {} {}", block.getNumber(), block.getHash());
        panicProcessor.panic("minerserver", String.format("Strange state in block %d %s", block.getNumber(), block.getHash()));
    }
    int txindex = 0;
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
    for (Transaction tx : block.getTransactionsList()) {
        TransactionExecutorFactory transactionExecutorFactory = new TransactionExecutorFactory(config, null, null, blockFactory, null, new PrecompiledContracts(config, bridgeSupportFactory), blockTxSignatureCache);
        TransactionExecutor executor = transactionExecutorFactory.newInstance(tx, txindex++, block.getCoinbase(), track, block, totalGasUsed);
        executor.executeTransaction();
        long gasUsed = executor.getGasUsed();
        Coin paidFees = executor.getPaidFees();
        totalGasUsed += gasUsed;
        totalPaidFees = totalPaidFees.add(paidFees);
        track.commit();
        TransactionReceipt receipt = new TransactionReceipt();
        receipt.setGasUsed(gasUsed);
        receipt.setCumulativeGas(totalGasUsed);
        latestStateRootHash = track.getRoot();
        receipt.setPostTxState(latestStateRootHash);
        receipt.setTxStatus(executor.getReceipt().isSuccessful());
        receipt.setStatus(executor.getReceipt().getStatus());
        receipt.setTransaction(tx);
        receipt.setLogInfoList(executor.getVMLogs());
        txReceipts.add(receipt);
    }
}
Also used : TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) Coin(co.rsk.core.Coin) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory)

Example 23 with PrecompiledContracts

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

the class VMPerformanceTest method testLongOperation.

// 
@Ignore
@Test
public void testLongOperation() {
    /* bad example because requires ABI parsing
        contract Fibonacci {
            function fib() returns (uint r) {
                uint256 a;
                uint256 b;
                uint256 c;

                a=0;
                b=1;
                for (uint i = 1; i < 55; i++) {
                    c = a+b;
                    a = b;
                    b = c;
                }
                r = b;
            }
        } // contract

     // Good example
    contract Fibonacci {
    function()  {
        uint256 a;
        uint256 b;
        uint256 c;

        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));
    // Strip the first 16 bytes which are added by Solidity to store the contract.
    byte[] codePlusPrefix = Hex.decode(// ---------------------------------------------------------------------------------------------------------------------nn
    "606060405260618060106000396000f360606040523615600d57600d565b605f5b6000600060006000600093508350600192508250600190505b60" + // "32"+ // 55
    "FE" + // 254
    "811015604f5782840191508150829350835081925082505b80806001019150506029565b8260005260206000f35b50505050565b00");
    // "606060405260618060106000396000f360606040523615600d57600d565b605f5b6000600060006000600093508350600192508250600190505b600f811015604f5782840191508150829350835081925082505b80806001019150506029565b8260005260206000f35b50505050565b00"
    /* Prefix code
        Instr.#    addrs.      mnemonic        operand                                                            xrefs                          description

        ------------------------------------------------------------------------------------------------------------------------------------------------------
        [       0] [0x00000000] PUSH1           0x60 ('`')                                                                                        # Place 1 byte item on stack.
        [       1] [0x00000002] PUSH1           0x40 ('@')                                                                                        # Place 1 byte item on stack.
        [       2] [0x00000004] MSTORE                                                                                                            # Save word to memory.
        [       3] [0x00000005] PUSH1           0x61 ('a')   This is the real contract length                                                                                     # Place 1 byte item on stack.
        [       4] [0x00000007] DUP1                                                                                                              # Duplicate 1st stack item.
        [       5] [0x00000008] PUSH1           0x10                                                                                              # Place 1 byte item on stack.
        [       6] [0x0000000a] PUSH1           0x00                                                                                              # Place 1 byte item on stack.
        [       7] [0x0000000c] CODECOPY                                                                                                          # Copy code running in current environment to memory.
        [       8] [0x0000000d] PUSH1           0x00                                                                                              # Place 1 byte item on stack.
        [       9] [0x0000000f] RETURN
        ------------------------------------------------------------------------------------------------------------------------------------------------------*/
    byte[] code = Arrays.copyOfRange(codePlusPrefix, 16, codePlusPrefix.length);
    program = new Program(vmConfig, precompiledContracts, blockFactory, activations, code, invoke, null, new HashSet<>());
    // String s_expected_1 = "000000000000000000000000000000000000000000000000000000033FFC1244"; // 55
    // String s_expected_1 = "00000000000000000000000000000000000000000000000000000002EE333961";// 50
    // 254
    String s_expected_1 = "0000000000000000000090A7ED63052BFF49E105B6B7BC90D0B352C89BA1AD59";
    startMeasure();
    vm.steps(program, Long.MAX_VALUE);
    endMeasure();
    byte[] actualHReturn = null;
    if (program.getResult().getHReturn() != null) {
        actualHReturn = program.getResult().getHReturn();
    }
    // if (!Arrays.equals(expectedHReturn, actualHReturn)) {
    // DataWord item1 = program.stackPop();
    assertEquals(s_expected_1, ByteUtil.toHexString(actualHReturn).toUpperCase());
}
Also used : PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) Program(org.ethereum.vm.program.Program) VM(org.ethereum.vm.VM) HashSet(java.util.HashSet) Ignore(org.junit.Ignore) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Aggregations

PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)23 TransactionExecutorFactory (co.rsk.core.TransactionExecutorFactory)11 BridgeSupportFactory (co.rsk.peg.BridgeSupportFactory)11 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)11 Test (org.junit.Test)9 TestSystemProperties (co.rsk.config.TestSystemProperties)8 RskAddress (co.rsk.core.RskAddress)6 RepositoryBtcBlockStoreWithCache (co.rsk.peg.RepositoryBtcBlockStoreWithCache)6 BlockExecutor (co.rsk.core.bc.BlockExecutor)5 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)5 VM (org.ethereum.vm.VM)5 HashMapDB (org.ethereum.datasource.HashMapDB)4 Keccak256 (co.rsk.crypto.Keccak256)3 RepositoryLocator (co.rsk.db.RepositoryLocator)3 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)3 EVMAssembler (co.rsk.asm.EVMAssembler)2 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 Coin (co.rsk.core.Coin)2 StateRootHandler (co.rsk.db.StateRootHandler)2 StateRootsStoreImpl (co.rsk.db.StateRootsStoreImpl)2