Search in sources :

Example 31 with TestSystemProperties

use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method processBodyResponseWithTransactionAddsToBlockchain.

@Test
public void processBodyResponseWithTransactionAddsToBlockchain() {
    Account senderAccount = createAccount("sender");
    Account receiverAccount = createAccount("receiver");
    Map<RskAddress, AccountState> accounts = new HashMap<>();
    accounts.put(senderAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.valueOf(20000000)));
    accounts.put(receiverAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.ZERO));
    final NetBlockStore store = new NetBlockStore();
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    Blockchain blockchain = blockChainBuilder.ofSize(0, false, accounts);
    Block genesis = blockchain.getBestBlock();
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    List<Transaction> txs = Collections.singletonList(createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO));
    Block block = new BlockGenerator().createChildBlock(genesis, txs, blockChainBuilder.getRepository().getRoot());
    StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
    BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(blockChainBuilder.getTrieStore(), stateRootHandler), new TransactionExecutorFactory(config, blockChainBuilder.getBlockStore(), null, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
    Assert.assertEquals(1, block.getTransactionsList().size());
    blockExecutor.executeAndFillAll(block, genesis.getHeader());
    Assert.assertEquals(21000, block.getFeesPaidToMiner().asBigInteger().intValueExact());
    Assert.assertEquals(1, block.getTransactionsList().size());
    Assert.assertEquals(1, block.getNumber());
    Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    List<Transaction> transactions = block.getTransactionsList();
    List<BlockHeader> uncles = block.getUncleList();
    long lastRequestId = new Random().nextLong();
    BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
    processor.registerExpectedMessage(response);
    Deque<BlockHeader> headerStack = new ArrayDeque<>();
    headerStack.add(block.getHeader());
    List<Deque<BlockHeader>> headers = new ArrayList<>();
    headers.add(headerStack);
    List<BlockIdentifier> bids = new ArrayList<>();
    bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
    bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
    processor.startDownloadingBodies(headers, Collections.singletonMap(sender, bids), sender);
    ((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
    processor.processBodyResponse(sender, response);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Also used : StateRootsStoreImpl(co.rsk.db.StateRootsStoreImpl) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) EthereumListener(org.ethereum.listener.EthereumListener) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) StateRootHandler(co.rsk.db.StateRootHandler) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) RepositoryLocator(co.rsk.db.RepositoryLocator) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) BlockExecutor(co.rsk.core.bc.BlockExecutor) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 32 with TestSystemProperties

use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.

the class AsyncNodeBlockProcessorTest method processSkeletonRequestWithThreeResults.

@Test
public void processSkeletonRequestWithThreeResults() {
    final int skeletonStep = 192;
    final Blockchain blockchain = new BlockChainBuilder().ofSize(300);
    final NetBlockStore store = new NetBlockStore();
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    final TestSystemProperties config = new TestSystemProperties();
    final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final SimplePeer sender = new SimplePeer();
    processor.processSkeletonRequest(sender, 100, 5);
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(1, sender.getMessages().size());
    final Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.SKELETON_RESPONSE_MESSAGE, message.getMessageType());
    final SkeletonResponseMessage bMessage = (SkeletonResponseMessage) message;
    Assert.assertEquals(100, bMessage.getId());
    final Block b1 = blockchain.getBlockByNumber(0);
    final Block b2 = blockchain.getBlockByNumber(skeletonStep);
    final Block b3 = blockchain.getBestBlock();
    final BlockIdentifier[] expected = { new BlockIdentifier(b1.getHash().getBytes(), b1.getNumber()), new BlockIdentifier(b2.getHash().getBytes(), b2.getNumber()), new BlockIdentifier(b3.getHash().getBytes(), b3.getNumber()) };
    assertBlockIdentifiers(expected, bMessage.getBlockIdentifiers());
}
Also used : BlockIdentifier(org.ethereum.core.BlockIdentifier) Blockchain(org.ethereum.core.Blockchain) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) Block(org.ethereum.core.Block) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 33 with TestSystemProperties

use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.

the class BlockSyncServiceTest method sendBlockMessagesAndAddThemToBlockchainInReverseOrder.

@Test
public void sendBlockMessagesAndAddThemToBlockchainInReverseOrder() {
    for (int i = 1; i < 52; i += 5) {
        Blockchain blockchain = new BlockChainBuilder().ofSize(10 * i);
        NetBlockStore store = new NetBlockStore();
        BlockNodeInformation nodeInformation = new BlockNodeInformation();
        TestSystemProperties config = new TestSystemProperties();
        BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
        Assert.assertEquals(10 * i, blockchain.getBestBlock().getNumber());
        Block initialBestBlock = blockchain.getBestBlock();
        List<Block> extendedChain = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), i);
        Collections.reverse(extendedChain);
        for (int j = 0; j < extendedChain.size() - 1; j++) {
            Block block = extendedChain.get(j);
            blockSyncService.processBlock(block, null, false);
            // we don't have all the parents, so we wait to update the best chain
            Assert.assertEquals(initialBestBlock.getNumber(), blockchain.getBestBlock().getNumber());
            Assert.assertEquals(initialBestBlock.getHash(), blockchain.getBestBlock().getHash());
        }
        // the chain is complete, we have a new best block
        Block closingBlock = extendedChain.get(extendedChain.size() - 1);
        Block newBestBlock = extendedChain.get(0);
        blockSyncService.processBlock(closingBlock, null, false);
        Assert.assertEquals(newBestBlock.getNumber(), blockchain.getBestBlock().getNumber());
        Assert.assertEquals(newBestBlock.getHash(), blockchain.getBestBlock().getHash());
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 34 with TestSystemProperties

use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.

the class NodeBlockProcessorUnclesTest method createNodeBlockProcessor.

private static NodeBlockProcessor createNodeBlockProcessor(BlockChainImpl blockChain) {
    NetBlockStore store = new NetBlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockChain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
    return new NodeBlockProcessor(store, blockChain, nodeInformation, blockSyncService, syncConfiguration);
}
Also used : TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration)

Example 35 with TestSystemProperties

use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.

the class AsyncNodeBlockProcessorTest method processTwoBlockListsAddingToBlockchainWithFork.

@Test(timeout = WAIT_TIME)
public void processTwoBlockListsAddingToBlockchainWithFork() throws InterruptedException {
    final NetBlockStore store = new NetBlockStore();
    final Blockchain blockchain = new BlockChainBuilder().ofSize(0);
    final Block genesis = blockchain.getBestBlock();
    final BlockGenerator blockGenerator = new BlockGenerator();
    final List<Block> blocks = blockGenerator.getBlockChain(genesis, 10);
    final List<Block> blocks2 = blockGenerator.getBlockChain(blocks.get(4), 20);
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    final TestSystemProperties config = new TestSystemProperties();
    final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final AsyncNodeBlockProcessorListener listener = new AsyncNodeBlockProcessorListener();
    final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE, listener);
    processor.start();
    BlockProcessResult blockProcessResult = processor.processBlock(null, genesis);
    if (blockProcessResult.isScheduledForProcessing()) {
        listener.waitForBlock(genesis.getHash());
    }
    Assert.assertEquals(0, store.size());
    Block blockToWait = null;
    for (Block b : blocks) {
        blockProcessResult = processor.processBlock(null, b);
        if (blockProcessResult.isScheduledForProcessing()) {
            blockToWait = b;
        }
    }
    for (Block b : blocks2) {
        blockProcessResult = processor.processBlock(null, b);
        if (blockProcessResult.isScheduledForProcessing()) {
            blockToWait = b;
        }
    }
    if (blockToWait != null) {
        listener.waitForBlock(blockToWait.getHash());
    }
    Assert.assertEquals(25, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(0, store.size());
    processor.stopAndWait(WAIT_TIME);
}
Also used : Blockchain(org.ethereum.core.Blockchain) AsyncNodeBlockProcessorListener(co.rsk.net.utils.AsyncNodeBlockProcessorListener) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Block(org.ethereum.core.Block) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Aggregations

TestSystemProperties (co.rsk.config.TestSystemProperties)158 Test (org.junit.Test)130 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)109 Blockchain (org.ethereum.core.Blockchain)78 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)75 SimplePeer (co.rsk.net.simples.SimplePeer)69 Block (org.ethereum.core.Block)69 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)56 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)23 EthereumListener (org.ethereum.listener.EthereumListener)23 BlockStore (org.ethereum.db.BlockStore)21 RskSystemProperties (co.rsk.config.RskSystemProperties)17 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)16 Keccak256 (co.rsk.crypto.Keccak256)13 RepositoryLocator (co.rsk.db.RepositoryLocator)10 World (co.rsk.test.World)10 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)9 Ignore (org.junit.Ignore)9 TransactionExecutorFactory (co.rsk.core.TransactionExecutorFactory)8 AsyncNodeBlockProcessorListener (co.rsk.net.utils.AsyncNodeBlockProcessorListener)8