Search in sources :

Example 6 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class BootstrapImporter method fillUpTrieQueue.

private static void fillUpTrieQueue(Queue<Trie> trieQueue, Queue<byte[]> nodeDataQueue, Queue<byte[]> nodeValueQueue, HashMapDB hashMapDB) {
    TrieStoreImpl fakeStore = new TrieStoreImpl(hashMapDB);
    for (byte[] nodeData = nodeDataQueue.poll(); nodeData != null; nodeData = nodeDataQueue.poll()) {
        Trie trie = Trie.fromMessage(nodeData, fakeStore);
        hashMapDB.put(trie.getHash().getBytes(), nodeData);
        trieQueue.add(trie);
    }
    for (byte[] nodeValue = nodeValueQueue.poll(); nodeValue != null; nodeValue = nodeValueQueue.poll()) {
        hashMapDB.put(Keccak256Helper.keccak256(nodeValue), nodeValue);
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Trie(co.rsk.trie.Trie)

Example 7 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class BridgePerformanceTestCase method executeAndAverage.

protected ExecutionStats executeAndAverage(String name, int times, ABIEncoder abiEncoder, BridgeStorageProviderInitializer storageInitializer, TxBuilder txBuilder, HeightProvider heightProvider, ExecutionStats stats, ResultCallback resultCallback, PostInitCallback postInitCallback) throws VMException {
    EnvironmentBuilder environmentBuilder = new EnvironmentBuilder() {

        private Bridge bridge;

        private RepositoryTrackWithBenchmarking benchmarkerTrack;

        private BridgeStorageProvider storageProvider;

        private TrieStore createTrieStore() {
            return new TrieStoreImpl(new HashMapDB());
        }

        @Override
        public Environment build(int executionIndex, TxBuilder txBuilder, int height) throws VMException {
            TrieStore trieStore = createTrieStore();
            Trie trie = new Trie(trieStore);
            benchmarkerTrack = new RepositoryTrackWithBenchmarking(trieStore, trie);
            Repository repository = benchmarkerTrack.startTracking();
            storageProvider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationConfig.forBlock((long) executionIndex));
            BtcBlockStore btcBlockStore = btcBlockStoreFactory.newInstance(repository, bridgeConstants, storageProvider, activationConfig.forBlock((long) executionIndex));
            storageInitializer.initialize(storageProvider, repository, executionIndex, btcBlockStore);
            repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, co.rsk.core.Coin.fromBitcoin(Coin.COIN.multiply(21_000_000L)));
            try {
                storageProvider.save();
            } catch (Exception e) {
                throw new RuntimeException("Error trying to save the storage after initialization", e);
            }
            repository.commit();
            benchmarkerTrack.commit();
            benchmarkerTrack = new RepositoryTrackWithBenchmarking(trieStore, benchmarkerTrack.getTrie());
            List<LogInfo> logs = new ArrayList<>();
            // TODO: This was commented to make registerBtcCoinbaseTransactionTest & getBtcTransactionConfirmationTest work.
            // Cache is not being populated.
            // Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(
            // constants.getBridgeConstants().getBtcParams());
            BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(btcBlockStoreFactory, constants.getBridgeConstants(), activationConfig);
            bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
            BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
            Blockchain blockchain = blockChainBuilder.ofSize(height);
            Transaction tx = txBuilder.build(executionIndex);
            bridge.init(tx, blockchain.getBestBlock(), benchmarkerTrack, blockChainBuilder.getBlockStore(), null, logs);
            // Execute a random method so that bridge support initialization
            // does its initial writes to the repo for e.g. genesis block,
            // federation, etc, etc. and we don't get
            // those recorded in the actual execution.
            boolean oldLocalCall = tx.isLocalCallTransaction();
            tx.setLocalCallTransaction(true);
            bridge.execute(Bridge.GET_FEDERATION_SIZE.encode());
            tx.setLocalCallTransaction(oldLocalCall);
            benchmarkerTrack.getStatistics().clear();
            Environment environment = new Environment() {

                @Override
                public PrecompiledContracts.PrecompiledContract getContract() {
                    return bridge;
                }

                @Override
                public BenchmarkedRepository getBenchmarkedRepository() {
                    return benchmarkerTrack;
                }

                @Override
                public void finalise() {
                    benchmarkerTrack.commit();
                }
            };
            if (postInitCallback != null) {
                postInitCallback.afterInit(environment);
            }
            return environment;
        }
    };
    return super.executeAndAverage(name, times, environmentBuilder, abiEncoder, txBuilder, heightProvider, stats, resultCallback);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) LogInfo(org.ethereum.vm.LogInfo) Blockchain(org.ethereum.core.Blockchain) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) RepositoryTrackWithBenchmarking(co.rsk.db.RepositoryTrackWithBenchmarking) VMException(org.ethereum.vm.exception.VMException) BenchmarkedRepository(co.rsk.db.BenchmarkedRepository) Repository(org.ethereum.core.Repository) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) Trie(co.rsk.trie.Trie)

Example 8 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class BlockChainBuilder method build.

public BlockChainImpl build() {
    BlocksIndex blocksIndex = new HashMapBlocksIndex();
    if (config == null) {
        config = new TestSystemProperties();
    }
    if (trieStore == null) {
        trieStore = new TrieStoreImpl(new HashMapDB().setClearOnClose(false));
    }
    if (repository == null) {
        repository = new MutableRepository(trieStore, new Trie(trieStore));
    }
    if (stateRootHandler == null) {
        stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
    }
    if (genesis == null) {
        genesis = new BlockGenerator().getGenesisBlock();
    }
    GenesisLoaderImpl.loadGenesisInitalState(repository, genesis);
    repository.commit();
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
    if (blockStore == null) {
        blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), blocksIndex);
    }
    if (receiptStore == null) {
        KeyValueDataSource ds = new HashMapDB();
        ds.init();
        receiptStore = new ReceiptStoreImpl(ds);
    }
    if (listener == null) {
        listener = new BlockExecutorTest.SimpleEthereumListener();
    }
    if (bridgeSupportFactory == null) {
        bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
    }
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(trieStore).blockStore(blockStore);
    BlockValidator blockValidator = validatorBuilder.build();
    ReceivedTxSignatureCache receivedTxSignatureCache = new ReceivedTxSignatureCache();
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(receivedTxSignatureCache);
    TransactionExecutorFactory transactionExecutorFactory = new TransactionExecutorFactory(config, blockStore, receiptStore, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), blockTxSignatureCache);
    repositoryLocator = new RepositoryLocator(trieStore, stateRootHandler);
    transactionPool = new TransactionPoolImpl(config, repositoryLocator, this.blockStore, blockFactory, new TestCompositeEthereumListener(), transactionExecutorFactory, new ReceivedTxSignatureCache(), 10, 100);
    BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), repositoryLocator, transactionExecutorFactory);
    BlockChainImpl blockChain = new BlockChainLoader(blockStore, receiptStore, transactionPool, listener, blockValidator, blockExecutor, genesis, stateRootHandler, repositoryLocator).loadBlockchain();
    if (this.testing) {
        blockChain.setBlockValidator(new DummyBlockValidator());
        blockChain.setNoValidation(true);
    }
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    if (this.blocks != null) {
        for (Block b : this.blocks) {
            blockExecutor.executeAndFillAll(b, blockChain.getBestBlock().getHeader());
            blockChain.tryToConnect(b);
        }
    }
    return blockChain;
}
Also used : BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockValidator(co.rsk.validators.BlockValidator) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) TestCompositeEthereumListener(org.ethereum.listener.TestCompositeEthereumListener) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) Trie(co.rsk.trie.Trie) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) BlockChainLoader(org.ethereum.core.genesis.BlockChainLoader) HashMapDB(org.ethereum.datasource.HashMapDB) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) TestSystemProperties(co.rsk.config.TestSystemProperties)

Example 9 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class StateTestRunner method runImpl.

public List<String> runImpl() {
    vStats = new ValidationStats();
    logger.info("");
    trieStore = new TrieStoreImpl(new HashMapDB());
    repository = RepositoryBuilder.build(trieStore, stateTestCase.getPre());
    logger.info("loaded repository");
    transaction = TransactionBuilder.build(stateTestCase.getTransaction());
    logger.info("transaction: {}", transaction.toString());
    BlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
    blockchain = new BlockChainImpl(blockStore, null, null, null, null, new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(trieStore, stateRootHandler), new TransactionExecutorFactory(config, blockStore, null, blockFactory, new ProgramInvokeFactoryImpl(), precompiledContracts, new BlockTxSignatureCache(new ReceivedTxSignatureCache()))), stateRootHandler);
    env = EnvBuilder.build(stateTestCase.getEnv());
    invokeFactory = new TestProgramInvokeFactory(env);
    block = build(env);
    block.setStateRoot(repository.getRoot());
    block.flushRLP();
    blockchain.setStatus(block, block.getCumulativeDifficulty());
    // blockchain.setProgramInvokeFactory(invokeFactory);
    // blockchain.startTracking();
    ProgramResult programResult = executeTransaction(transaction);
    trieStore.flush();
    List<LogInfo> origLogs = programResult.getLogInfoList();
    List<LogInfo> postLogs = LogBuilder.build(stateTestCase.getLogs());
    List<String> logsResult = LogsValidator.valid(origLogs, postLogs, vStats);
    Repository postRepository = RepositoryBuilder.build(stateTestCase.getPost());
    // Balances cannot be validated because has consumption for CALLs differ.
    List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false, false, vStats);
    logger.info("--------- POST Validation---------");
    List<String> outputResults = OutputValidator.valid(ByteUtil.toHexString(programResult.getHReturn()), stateTestCase.getOut(), vStats);
    List<String> results = new ArrayList<>();
    results.addAll(repoResults);
    results.addAll(logsResult);
    results.addAll(outputResults);
    for (String result : results) {
        logger.error(result);
    }
    if ((vStats.storageChecks == 0) && (vStats.logChecks == 0) && (vStats.balancetChecks == 0) && (vStats.outputChecks == 0) && (vStats.blockChecks == 0)) {
        // This generally mean that the test didn't check anything
        // AccountChecks are considered not indicative of the result of the test
        logger.info("IRRELEVANT\n");
    }
    logger.info("\n\n");
    return results;
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) LogInfo(org.ethereum.vm.LogInfo) StateRootsStoreImpl(co.rsk.db.StateRootsStoreImpl) BlockExecutor(co.rsk.core.bc.BlockExecutor) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ProgramResult(org.ethereum.vm.program.ProgramResult) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) StateRootHandler(co.rsk.db.StateRootHandler) RepositoryLocator(co.rsk.db.RepositoryLocator) TestProgramInvokeFactory(org.ethereum.jsontestsuite.TestProgramInvokeFactory) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) ValidationStats(org.ethereum.jsontestsuite.validators.ValidationStats)

Example 10 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class TestRunner method runTestCase.

public List<String> runTestCase(BlockTestCase testCase) {
    /* 1 */
    // Create genesis + init pre state
    ValidationStats vStats = new ValidationStats();
    Block genesis = build(testCase.getGenesisBlockHeader(), null, null);
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    Repository repository = RepositoryBuilder.build(trieStore, testCase.getPre());
    IndexedBlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    CompositeEthereumListener listener = new TestCompositeEthereumListener();
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
    TransactionExecutorFactory transactionExecutorFactory = new TransactionExecutorFactory(config, blockStore, receiptStore, blockFactory, new ProgramInvokeFactoryImpl(), null, blockTxSignatureCache);
    StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
    RepositoryLocator repositoryLocator = new RepositoryLocator(trieStore, stateRootHandler);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repositoryLocator, null, blockFactory, listener, transactionExecutorFactory, new ReceivedTxSignatureCache(), 10, 100);
    BlockChainImpl blockchain = new BlockChainImpl(blockStore, receiptStore, transactionPool, null, new DummyBlockValidator(), new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(trieStore, stateRootHandler), transactionExecutorFactory), stateRootHandler);
    blockchain.setNoValidation(true);
    blockchain.setStatus(genesis, genesis.getCumulativeDifficulty());
    /* 2 */
    // Create block traffic list
    List<Block> blockTraffic = new ArrayList<>();
    for (BlockTck blockTck : testCase.getBlocks()) {
        Block block = build(blockTck.getBlockHeader(), blockTck.getTransactions(), blockTck.getUncleHeaders());
        Block tBlock = null;
        try {
            byte[] rlp = parseData(blockTck.getRlp());
            tBlock = blockFactory.decodeBlock(rlp);
            ArrayList<String> outputSummary = BlockHeaderValidator.valid(tBlock.getHeader(), block.getHeader(), null);
            if (!outputSummary.isEmpty()) {
                for (String output : outputSummary) logger.error("at block {}: {}", Integer.toString(blockTraffic.size()), output);
            }
            blockTraffic.add(tBlock);
        } catch (Exception e) {
            System.out.println("*** Exception");
        }
    }
    // Inject blocks to the blockchain execution
    for (Block block : blockTraffic) {
        ImportResult importResult = blockchain.tryToConnect(block);
        logger.debug("{} ~ {} difficulty: {} ::: {}", block.getPrintableHash(), toPrintableHash(block.getParentHash().getBytes()), block.getCumulativeDifficulty(), importResult.toString());
    }
    // Check state root matches last valid block
    List<String> results = new ArrayList<>();
    String currRoot = ByteUtil.toHexString(repository.getRoot());
    byte[] bestHash = Hex.decode(testCase.getLastblockhash());
    String finalRoot = ByteUtil.toHexString(blockStore.getBlockByHash(bestHash).getStateRoot());
    if (validateStateRoots) {
        if (!finalRoot.equals(currRoot)) {
            String formattedString = String.format("Root hash doesn't match best: expected: %s current: %s", finalRoot, currRoot);
            results.add(formattedString);
        }
    }
    Repository postRepository = RepositoryBuilder.build(testCase.getPostState());
    List<String> repoResults = RepositoryValidator.valid(repository, postRepository, validateStateRoots, validateBalances, null);
    results.addAll(repoResults);
    return results;
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) TrieStore(co.rsk.trie.TrieStore) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) BlockTck(org.ethereum.jsontestsuite.model.BlockTck) TestCompositeEthereumListener(org.ethereum.listener.TestCompositeEthereumListener) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) BlockExecutor(co.rsk.core.bc.BlockExecutor) HashMapDB(org.ethereum.datasource.HashMapDB) IOException(java.io.IOException) CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) TestCompositeEthereumListener(org.ethereum.listener.TestCompositeEthereumListener) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) ValidationStats(org.ethereum.jsontestsuite.validators.ValidationStats)

Aggregations

TrieStoreImpl (co.rsk.trie.TrieStoreImpl)38 HashMapDB (org.ethereum.datasource.HashMapDB)34 TrieStore (co.rsk.trie.TrieStore)25 Trie (co.rsk.trie.Trie)21 Test (org.junit.Test)20 MutableRepository (org.ethereum.db.MutableRepository)12 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)8 RskAddress (co.rsk.core.RskAddress)8 DataWord (org.ethereum.vm.DataWord)8 TrieImpl (co.rsk.trie.TrieImpl)7 Repository (org.ethereum.core.Repository)7 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)7 RepositoryImpl (co.rsk.db.RepositoryImpl)6 ArrayList (java.util.ArrayList)6 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)6 MutableTrieImpl (co.rsk.db.MutableTrieImpl)5 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)5 RskSystemProperties (co.rsk.config.RskSystemProperties)4 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)4 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)4