Search in sources :

Example 1 with ImportResult

use of org.ethereum.core.ImportResult 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 ImportResult

use of org.ethereum.core.ImportResult in project rskj by rsksmart.

the class BlockchainTest method checkItDoesntAddAnInvalidBlock.

@Test
public void checkItDoesntAddAnInvalidBlock() {
    Blockchain blockchain = createBlockchain();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block1 = blockGenerator.createChildBlock(blockchain.getBestBlock());
    ImportResult importResult1 = blockchain.tryToConnect(block1);
    assertTrue(importResult1.isSuccessful());
    Block block2 = blockGenerator.createChildBlock(blockchain.getBestBlock());
    Block block2b = blockGenerator.createBlock(10, 5);
    Block block3 = Block.fromValidData(block2.getHeader(), block2b.getTransactionsList(), block2b.getUncleList());
    ImportResult importResult2 = blockchain.tryToConnect(block3);
    Assert.assertFalse(importResult2.isSuccessful());
}
Also used : ImportResult(org.ethereum.core.ImportResult) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 3 with ImportResult

use of org.ethereum.core.ImportResult in project rskj by rsksmart.

the class BlockSyncService method processBlock.

public BlockProcessResult processBlock(@Nonnull Block block, MessageChannel sender, boolean ignoreMissingHashes) {
    Instant start = Instant.now();
    long bestBlockNumber = this.getBestBlockNumber();
    long blockNumber = block.getNumber();
    final Keccak256 blockHash = block.getHash();
    int syncMaxDistance = syncConfiguration.getChunkSize() * syncConfiguration.getMaxSkeletonChunks();
    tryReleaseStore(bestBlockNumber);
    store.removeHeader(block.getHeader());
    unknownBlockHashes.remove(blockHash);
    if (blockNumber > bestBlockNumber + syncMaxDistance) {
        logger.trace("Block too advanced {} {} from {} ", blockNumber, block.getShortHash(), sender != null ? sender.getPeerNodeID().toString() : "N/A");
        return new BlockProcessResult(false, null, block.getShortHash(), Duration.between(start, Instant.now()));
    }
    if (sender != null) {
        nodeInformation.addBlockToNode(blockHash, sender.getPeerNodeID());
    }
    // already in a blockchain
    if (BlockUtils.blockInSomeBlockChain(block, blockchain)) {
        logger.trace("Block already in a chain {} {}", blockNumber, block.getShortHash());
        return new BlockProcessResult(false, null, block.getShortHash(), Duration.between(start, Instant.now()));
    }
    trySaveStore(block);
    Set<Keccak256> unknownHashes = BlockUtils.unknownDirectAncestorsHashes(block, blockchain, store);
    // We can't add the block if there are missing ancestors or uncles. Request the missing blocks to the sender.
    if (!unknownHashes.isEmpty()) {
        if (!ignoreMissingHashes) {
            logger.trace("Missing hashes for block in process {} {}", blockNumber, block.getShortHash());
            requestMissingHashes(sender, unknownHashes);
        }
        return new BlockProcessResult(false, null, block.getShortHash(), Duration.between(start, Instant.now()));
    }
    logger.trace("Trying to add to blockchain");
    Map<Keccak256, ImportResult> connectResult = connectBlocksAndDescendants(sender, BlockUtils.sortBlocksByNumber(this.getParentsNotInBlockchain(block)), ignoreMissingHashes);
    return new BlockProcessResult(true, connectResult, block.getShortHash(), Duration.between(start, Instant.now()));
}
Also used : ImportResult(org.ethereum.core.ImportResult) Instant(java.time.Instant) Keccak256(co.rsk.crypto.Keccak256)

Aggregations

ImportResult (org.ethereum.core.ImportResult)3 Block (org.ethereum.core.Block)2 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)1 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)1 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)1 Keccak256 (co.rsk.crypto.Keccak256)1 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 Blockchain (org.ethereum.core.Blockchain)1 Repository (org.ethereum.core.Repository)1 HashMapDB (org.ethereum.datasource.HashMapDB)1 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)1 BlockTck (org.ethereum.jsontestsuite.model.BlockTck)1 CompositeEthereumListener (org.ethereum.listener.CompositeEthereumListener)1 EthereumListener (org.ethereum.listener.EthereumListener)1 Test (org.junit.Test)1