Search in sources :

Example 11 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplTest method createWeb3.

private Web3Impl createWeb3(Ethereum eth, World world, ReceiptStore receiptStore) {
    BlockChainImpl blockChain = world.getBlockChain();
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), null, null, null, 10, 100);
    return createWeb3(eth, world, transactionPool, receiptStore);
}
Also used : TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) BlockChainImpl(co.rsk.core.bc.BlockChainImpl)

Example 12 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplTest method createWeb3.

private Web3Impl createWeb3(World world, BlockProcessor blockProcessor, ReceiptStore receiptStore) {
    BlockChainImpl blockChain = world.getBlockChain();
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), null, null, null, 10, 100);
    return createWeb3(Web3Mocks.getMockEthereum(), blockChain, transactionPool, blockChain.getBlockStore(), blockProcessor, new SimpleConfigCapabilities(), receiptStore);
}
Also used : TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) BlockChainImpl(co.rsk.core.bc.BlockChainImpl)

Example 13 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class BlockChainBuilderTest method createBlockChain.

@Test
public void createBlockChain() {
    BlockChainBuilder builder = new BlockChainBuilder();
    BlockChainImpl blockChain = builder.build();
    Assert.assertNotNull(blockChain);
    Assert.assertNotNull(blockChain.getRepository());
    Assert.assertNotNull(blockChain.getBlockStore());
    Assert.assertNotNull(blockChain.getListener());
    Assert.assertNotNull(blockChain.getBlockValidator());
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) Test(org.junit.Test)

Example 14 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class BlockChainLoader method loadBlockchain.

public BlockChainImpl loadBlockchain() {
    BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, transactionPool, listener, adminInfo, blockValidator);
    if (!config.databaseReset()) {
        blockStore.load();
    }
    Block bestBlock = blockStore.getBestBlock();
    if (bestBlock == null) {
        logger.info("DB is empty - adding Genesis");
        BigInteger initialNonce = config.getBlockchainConfig().getCommonConstants().getInitialNonce();
        Genesis genesis = GenesisLoader.loadGenesis(config, config.genesisInfo(), initialNonce, true);
        for (RskAddress addr : genesis.getPremine().keySet()) {
            repository.createAccount(addr);
            InitialAddressState initialAddressState = genesis.getPremine().get(addr);
            repository.addBalance(addr, initialAddressState.getAccountState().getBalance());
            AccountState accountState = repository.getAccountState(addr);
            accountState.setNonce(initialAddressState.getAccountState().getNonce());
            if (initialAddressState.getContractDetails() != null) {
                repository.updateContractDetails(addr, initialAddressState.getContractDetails());
                accountState.setStateRoot(initialAddressState.getAccountState().getStateRoot());
                accountState.setCodeHash(initialAddressState.getAccountState().getCodeHash());
            }
            repository.updateAccountState(addr, accountState);
        }
        genesis.setStateRoot(repository.getRoot());
        genesis.flushRLP();
        blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
        blockchain.setBestBlock(genesis);
        blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
        listener.onBlock(genesis, new ArrayList<TransactionReceipt>());
        repository.dumpState(genesis, 0, 0, null);
        logger.info("Genesis block loaded");
    } else {
        BlockDifficulty totalDifficulty = blockStore.getTotalDifficultyForHash(bestBlock.getHash().getBytes());
        blockchain.setBestBlock(bestBlock);
        blockchain.setTotalDifficulty(totalDifficulty);
        logger.info("*** Loaded up to block [{}] totalDifficulty [{}] with stateRoot [{}]", blockchain.getBestBlock().getNumber(), blockchain.getTotalDifficulty().toString(), Hex.toHexString(blockchain.getBestBlock().getStateRoot()));
    }
    String rootHash = config.rootHashStart();
    if (StringUtils.isNotBlank(rootHash)) {
        // update world state by dummy hash
        byte[] rootHashArray = Hex.decode(rootHash);
        logger.info("Loading root hash from property file: [{}]", rootHash);
        this.repository.syncToRoot(rootHashArray);
    } else {
        // todo this is just a workaround, move EMPTY_TRIE_HASH logic to Trie implementation
        if (!Arrays.equals(blockchain.getBestBlock().getStateRoot(), EMPTY_TRIE_HASH)) {
            this.repository.syncToRoot(blockchain.getBestBlock().getStateRoot());
        }
    }
    return blockchain;
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 15 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class NodeBlockProcessorUnclesTest method addBlockWithTwoUnknownUncles.

@Test
public void addBlockWithTwoUnknownUncles() throws UnknownHostException {
    BlockChainImpl blockChain = new BlockChainBuilder().build();
    NodeBlockProcessor processor = createNodeBlockProcessor(blockChain);
    Block genesis = processor.getBlockchain().getBestBlock();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, new BlockGenerator());
    Block block1 = blockBuilder.parent(genesis).build();
    Block uncle1 = blockBuilder.parent(genesis).build();
    Block uncle2 = blockBuilder.parent(genesis).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block2 = blockBuilder.parent(block1).uncles(uncles).build();
    processor.processBlock(null, block1);
    SimpleMessageChannel sender = new SimpleMessageChannel();
    processor.processBlock(sender, block2);
    Assert.assertEquals(2, processor.getBlockchain().getBestBlock().getNumber());
    Assert.assertArrayEquals(block2.getHash().getBytes(), processor.getBlockchain().getBestBlockHash());
    Assert.assertEquals(0, sender.getGetBlockMessages().size());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockChainImpl (co.rsk.core.bc.BlockChainImpl)28 Test (org.junit.Test)15 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)14 ArrayList (java.util.ArrayList)14 World (co.rsk.test.World)11 BlockBuilder (co.rsk.test.builders.BlockBuilder)11 AccountBuilder (co.rsk.test.builders.AccountBuilder)10 BlockDifficulty (co.rsk.core.BlockDifficulty)7 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 Block (org.ethereum.core.Block)6 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)4 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)4 BigInteger (java.math.BigInteger)4 BlockHeader (org.ethereum.core.BlockHeader)4 HashMapDB (org.ethereum.datasource.HashMapDB)4 RskAddress (co.rsk.core.RskAddress)3 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)3 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)3 BlockStore (org.ethereum.db.BlockStore)3 SimpleEthereum (org.ethereum.rpc.Simples.SimpleEthereum)3