Search in sources :

Example 1 with EthereumListener

use of org.ethereum.listener.EthereumListener in project rskj by rsksmart.

the class TestRunner method runTestCase.

public List<String> runTestCase(BlockTestCase testCase) {
    /* 1 */
    // Create genesis + init pre state
    Block genesis = BlockBuilder.build(testCase.getGenesisBlockHeader(), null, null);
    Repository repository = RepositoryBuilder.build(testCase.getPre());
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    EthereumListener listener = new CompositeEthereumListener();
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, null, null, new DummyBlockValidator());
    // BlockchainImpl blockchain = new BlockchainImpl(blockStore, repository, wallet, adminInfo, listener,
    // new CommonConfig().parentHeaderValidator(), receiptStore);
    blockchain.setNoValidation(true);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, null, receiptStore, null, listener, 10, 100);
    blockchain.setBestBlock(genesis);
    blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
    blockchain.setTransactionPool(transactionPool);
    /* 2 */
    // Create block traffic list
    List<Block> blockTraffic = new ArrayList<>();
    for (BlockTck blockTck : testCase.getBlocks()) {
        Block block = BlockBuilder.build(blockTck.getBlockHeader(), blockTck.getTransactions(), blockTck.getUncleHeaders());
        setNewStateRoot = !((blockTck.getTransactions() == null) && (blockTck.getUncleHeaders() == null) && (blockTck.getBlockHeader() == null));
        Block tBlock = null;
        try {
            byte[] rlp = parseData(blockTck.getRlp());
            tBlock = new Block(rlp);
            ArrayList<String> outputSummary = BlockHeaderValidator.valid(tBlock.getHeader(), block.getHeader());
            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.getShortHash(), shortHash(block.getParentHash().getBytes()), block.getCumulativeDifficulty(), importResult.toString());
    }
    // Check state root matches last valid block
    List<String> results = new ArrayList<>();
    String currRoot = Hex.toHexString(repository.getRoot());
    byte[] bestHash = Hex.decode(testCase.getLastblockhash());
    String finalRoot = Hex.toHexString(blockStore.getBlockByHash(bestHash).getStateRoot());
    /*
        if (!blockchain.byTest) // If this comes from ETH, it won't match
        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, false);
    results.addAll(repoResults);
    return results;
}
Also used : CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) EthereumListener(org.ethereum.listener.EthereumListener) ImportResult(org.ethereum.core.ImportResult) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) IOException(java.io.IOException) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) Repository(org.ethereum.core.Repository) BlockTck(org.ethereum.jsontestsuite.model.BlockTck) CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) Block(org.ethereum.core.Block) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource)

Example 2 with EthereumListener

use of org.ethereum.listener.EthereumListener in project rskj by rsksmart.

the class BlockchainLoaderTest method testLoadBlockchainEmptyBlockchain.

@Test
public void testLoadBlockchainEmptyBlockchain() throws IOException {
    String jsonFile = "blockchain_loader_genesis.json";
    RskSystemProperties systemProperties = Mockito.mock(RskSystemProperties.class);
    Constants constants = Mockito.mock(Constants.class);
    Mockito.when(constants.getInitialNonce()).thenReturn(BigInteger.ZERO);
    BlockchainNetConfig blockchainNetConfig = Mockito.mock(BlockchainNetConfig.class);
    Mockito.when(blockchainNetConfig.getCommonConstants()).thenReturn(constants);
    Mockito.when(systemProperties.databaseDir()).thenReturn(new RskSystemProperties().databaseDir());
    Mockito.when(systemProperties.getBlockchainConfig()).thenReturn(blockchainNetConfig);
    Mockito.when(systemProperties.genesisInfo()).thenReturn(jsonFile);
    BlockStore blockStore = Mockito.mock(BlockStore.class);
    Mockito.when(blockStore.getBestBlock()).thenReturn(null);
    EthereumListener ethereumListener = Mockito.mock(EthereumListener.class);
    Repository repository = new RepositoryImpl(systemProperties, new TrieStoreImpl(new HashMapDB().setClearOnClose(false)));
    ;
    BlockChainLoader blockChainLoader = new BlockChainLoader(systemProperties, repository, blockStore, null, null, ethereumListener, null, null);
    blockChainLoader.loadBlockchain();
    Assert.assertEquals(5, repository.getAccountsKeys().size());
    Assert.assertEquals(Coin.valueOf(2000), repository.getBalance(new RskAddress("dabadabadabadabadabadabadabadabadaba0001")));
    Assert.assertEquals(BigInteger.valueOf(24), repository.getNonce(new RskAddress("dabadabadabadabadabadabadabadabadaba0001")));
    Assert.assertEquals(Coin.valueOf(1000), repository.getBalance(new RskAddress("dabadabadabadabadabadabadabadabadaba0002")));
    Assert.assertEquals(BigInteger.ZERO, repository.getNonce(new RskAddress("dabadabadabadabadabadabadabadabadaba0002")));
    Assert.assertEquals(Coin.valueOf(10), repository.getBalance(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")));
    Assert.assertEquals(BigInteger.valueOf(25), repository.getNonce(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")));
    Assert.assertEquals(DataWord.ONE, repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).get(DataWord.ZERO));
    Assert.assertEquals(new DataWord(3), repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).get(DataWord.ONE));
    Assert.assertEquals(274, repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).getCode().length);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) EthereumListener(org.ethereum.listener.EthereumListener) BlockStore(org.ethereum.db.BlockStore) Constants(org.ethereum.config.Constants) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 3 with EthereumListener

use of org.ethereum.listener.EthereumListener in project rskj by rsksmart.

the class BlockChainImplTest method createBlockChain.

private static BlockChainImpl createBlockChain(Repository repository, IndexedBlockStore blockStore, BlockValidatorImpl blockValidator) {
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    AdminInfo adminInfo = new SimpleAdminInfo();
    EthereumListener listener = new BlockExecutorTest.SimpleEthereumListener();
    BlockChainImpl blockChain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, listener, adminInfo, blockValidator);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, blockStore, receiptStore, null, listener, 10, 100);
    blockChain.setTransactionPool(transactionPool);
    return blockChain;
}
Also used : ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) EthereumListener(org.ethereum.listener.EthereumListener) AdminInfo(org.ethereum.manager.AdminInfo) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) ReceiptStore(org.ethereum.db.ReceiptStore)

Example 4 with EthereumListener

use of org.ethereum.listener.EthereumListener in project rskj by rsksmart.

the class BlockChainBuilder method build.

public BlockChainImpl build(boolean withoutCleaner) {
    if (repository == null)
        repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB().setClearOnClose(false)));
    if (blockStore == null) {
        blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    }
    if (receiptStore == null) {
        KeyValueDataSource ds = new HashMapDB();
        ds.init();
        receiptStore = new ReceiptStoreImpl(ds);
    }
    if (txinfos != null && !txinfos.isEmpty())
        for (TransactionInfo txinfo : txinfos) receiptStore.add(txinfo.getBlockHash(), txinfo.getIndex(), txinfo.getReceipt());
    EthereumListener listener = new BlockExecutorTest.SimpleEthereumListener();
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(repository).blockStore(blockStore);
    BlockValidator blockValidator = validatorBuilder.build();
    if (this.adminInfo == null)
        this.adminInfo = new AdminInfo();
    BlockChainImpl blockChain = new BlockChainImpl(config, this.repository, this.blockStore, receiptStore, null, listener, this.adminInfo, blockValidator);
    if (this.testing) {
        blockChain.setBlockValidator(new DummyBlockValidator());
        blockChain.setNoValidation(true);
    }
    TransactionPoolImpl transactionPool;
    if (withoutCleaner) {
        transactionPool = new TransactionPoolImplNoCleaner(config, blockChain.getRepository(), blockChain.getBlockStore(), receiptStore, new ProgramInvokeFactoryImpl(), new BlockExecutorTest.SimpleEthereumListener(), 10, 100);
    } else {
        transactionPool = new TransactionPoolImpl(config, blockChain.getRepository(), blockChain.getBlockStore(), receiptStore, new ProgramInvokeFactoryImpl(), new BlockExecutorTest.SimpleEthereumListener(), 10, 100);
    }
    blockChain.setTransactionPool(transactionPool);
    if (this.genesis != null) {
        for (RskAddress addr : this.genesis.getPremine().keySet()) {
            this.repository.createAccount(addr);
            this.repository.addBalance(addr, this.genesis.getPremine().get(addr).getAccountState().getBalance());
        }
        Repository track = this.repository.startTracking();
        new RepositoryBlockStore(config, track, PrecompiledContracts.BRIDGE_ADDR);
        track.commit();
        this.genesis.setStateRoot(this.repository.getRoot());
        this.genesis.flushRLP();
        blockChain.setBestBlock(this.genesis);
        blockChain.setTotalDifficulty(this.genesis.getCumulativeDifficulty());
    }
    if (this.blocks != null) {
        BlockExecutor blockExecutor = new BlockExecutor(config, repository, receiptStore, blockStore, listener);
        for (Block b : this.blocks) {
            blockExecutor.executeAndFillAll(b, blockChain.getBestBlock());
            blockChain.tryToConnect(b);
        }
    }
    return blockChain;
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) EthereumListener(org.ethereum.listener.EthereumListener) HashMap(java.util.HashMap) AdminInfo(org.ethereum.manager.AdminInfo) HashMapDB(org.ethereum.datasource.HashMapDB) BlockValidator(co.rsk.validators.BlockValidator) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) RepositoryImpl(co.rsk.db.RepositoryImpl) RepositoryBlockStore(co.rsk.peg.RepositoryBlockStore) RskAddress(co.rsk.core.RskAddress) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource)

Aggregations

HashMapDB (org.ethereum.datasource.HashMapDB)4 EthereumListener (org.ethereum.listener.EthereumListener)4 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)3 RskAddress (co.rsk.core.RskAddress)2 RepositoryImpl (co.rsk.db.RepositoryImpl)2 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)2 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)2 Repository (org.ethereum.core.Repository)2 AdminInfo (org.ethereum.manager.AdminInfo)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)1 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)1 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)1 RepositoryBlockStore (co.rsk.peg.RepositoryBlockStore)1 BlockValidator (co.rsk.validators.BlockValidator)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 BlockchainNetConfig (org.ethereum.config.BlockchainNetConfig)1 Constants (org.ethereum.config.Constants)1 Block (org.ethereum.core.Block)1