Search in sources :

Example 6 with SimpleAsyncNode

use of co.rsk.net.simples.SimpleAsyncNode in project rskj by rsksmart.

the class TwoAsyncNodeTest method createNode.

private static SimpleAsyncNode createNode(int size) {
    final World world = new World();
    final BlockStore store = new BlockStore();
    final Blockchain blockchain = world.getBlockChain();
    List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), size);
    for (Block b : blocks) blockchain.tryToConnect(b);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    NodeMessageHandler handler = new NodeMessageHandler(config, processor, null, null, null, null, null, new DummyBlockValidationRule());
    return new SimpleAsyncNode(handler);
}
Also used : DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Block(org.ethereum.core.Block) SyncConfiguration(co.rsk.net.sync.SyncConfiguration)

Example 7 with SimpleAsyncNode

use of co.rsk.net.simples.SimpleAsyncNode in project rskj by rsksmart.

the class TwoAsyncNodeUsingSyncProcessorTest method syncInMultipleStepsWithLongBlockchain.

@Test
public void syncInMultipleStepsWithLongBlockchain() {
    Blockchain b1 = BlockChainBuilder.ofSize(300, false);
    Blockchain b2 = BlockChainBuilder.copyAndExtend(b1, 4000, false);
    SimpleAsyncNode node1 = SimpleAsyncNode.createNode(b1, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SimpleAsyncNode node2 = SimpleAsyncNode.createNode(b2, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    Assert.assertEquals(300, node1.getBestBlock().getNumber());
    Assert.assertEquals(4300, node2.getBestBlock().getNumber());
    for (int i = 0; i < 5; i++) {
        int skippedChunks = 300 / 192;
        int expectedBestBlockNumber = Math.min(4300, 192 * skippedChunks + 192 * 6 * (i + 1));
        long currentBestBlock = node1.getBestBlock().getNumber();
        // at the beginning and the end we might have different number of blocks to download
        int blocksToDownload = Math.toIntExact(expectedBestBlockNumber - currentBestBlock);
        node2.sendFullStatusTo(node1);
        node1.waitUntilNTasksWithTimeout(SyncUtils.syncSetupRequests(4300, currentBestBlock, SyncConfiguration.IMMEDIATE_FOR_TESTING));
        // request bodies
        node1.waitExactlyNTasksWithTimeout(blocksToDownload);
        Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
        Assert.assertTrue(node2.getSyncProcessor().getExpectedResponses().isEmpty());
        Assert.assertEquals(expectedBestBlockNumber, node1.getBestBlock().getNumber());
        Assert.assertEquals(4300, node2.getBestBlock().getNumber());
        // this prevents node2's queue to get full
        node2.clearQueue();
    }
    node1.joinWithTimeout();
    node2.joinWithTimeout();
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
}
Also used : SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Blockchain(org.ethereum.core.Blockchain) Test(org.junit.Test)

Example 8 with SimpleAsyncNode

use of co.rsk.net.simples.SimpleAsyncNode in project rskj by rsksmart.

the class TwoAsyncNodeUsingSyncProcessorTest method sendNewBlock.

@Test
public void sendNewBlock() throws InterruptedException {
    SimpleAsyncNode node1 = SimpleAsyncNode.createNodeWithWorldBlockChain(1, false, true);
    SimpleAsyncNode node2 = SimpleAsyncNode.createNodeWithWorldBlockChain(0, false, true);
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
    node2.receiveMessageFrom(node1, new NewBlockHashMessage(node1.getBestBlock().getHash().getBytes()));
    // process new block hash
    node2.waitUntilNTasksWithTimeout(1);
    // process block response
    node2.waitExactlyNTasksWithTimeout(1);
    node1.joinWithTimeout();
    node2.joinWithTimeout();
    Assert.assertEquals(1, node1.getBestBlock().getNumber());
    Assert.assertEquals(1, node2.getBestBlock().getNumber());
    Assert.assertEquals(node1.getBestBlock().getHash(), node2.getBestBlock().getHash());
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
}
Also used : SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) NewBlockHashMessage(co.rsk.net.messages.NewBlockHashMessage) Test(org.junit.Test)

Example 9 with SimpleAsyncNode

use of co.rsk.net.simples.SimpleAsyncNode in project rskj by rsksmart.

the class TwoAsyncNodeUsingSyncProcessorTest method buildBlockchainAndSynchronize400Blocks.

@Test
public void buildBlockchainAndSynchronize400Blocks() throws InterruptedException {
    SimpleAsyncNode node1 = SimpleAsyncNode.createNodeWithWorldBlockChain(400, false, true);
    SimpleAsyncNode node2 = SimpleAsyncNode.createNodeWithWorldBlockChain(0, false, true);
    node1.sendFullStatusTo(node2);
    // sync setup
    node2.waitUntilNTasksWithTimeout(SyncUtils.syncSetupRequests(400, 0, SyncConfiguration.IMMEDIATE_FOR_TESTING));
    // get blocks
    node2.waitExactlyNTasksWithTimeout(400);
    node1.joinWithTimeout();
    node2.joinWithTimeout();
    Assert.assertEquals(400, node1.getBestBlock().getNumber());
    Assert.assertEquals(400, node2.getBestBlock().getNumber());
    Assert.assertEquals(node1.getBestBlock().getHash(), node2.getBestBlock().getHash());
    Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node2.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
}
Also used : SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Test(org.junit.Test)

Example 10 with SimpleAsyncNode

use of co.rsk.net.simples.SimpleAsyncNode in project rskj by rsksmart.

the class TwoAsyncNodeUsingSyncProcessorTest method buildBlockchainAndSynchronize.

@Test
public void buildBlockchainAndSynchronize() throws InterruptedException {
    SimpleAsyncNode node1 = SimpleAsyncNode.createNodeWithWorldBlockChain(100, false, true);
    SimpleAsyncNode node2 = SimpleAsyncNode.createNodeWithWorldBlockChain(0, false, true);
    node1.sendFullStatusTo(node2);
    // sync setup
    node2.waitUntilNTasksWithTimeout(SyncUtils.syncSetupRequests(100, 0, SyncConfiguration.IMMEDIATE_FOR_TESTING));
    // get blocks
    node2.waitExactlyNTasksWithTimeout(100);
    node1.joinWithTimeout();
    node2.joinWithTimeout();
    Assert.assertEquals(100, node1.getBestBlock().getNumber());
    Assert.assertEquals(100, node2.getBestBlock().getNumber());
    Assert.assertEquals(node1.getBestBlock().getHash(), node2.getBestBlock().getHash());
    Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node2.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
}
Also used : SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Test(org.junit.Test)

Aggregations

SimpleAsyncNode (co.rsk.net.simples.SimpleAsyncNode)27 Test (org.junit.Test)24 Blockchain (org.ethereum.core.Blockchain)14 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)8 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)6 Block (org.ethereum.core.Block)6 BlockMessage (co.rsk.net.messages.BlockMessage)4 NewBlockHashMessage (co.rsk.net.messages.NewBlockHashMessage)3 World (co.rsk.test.World)3 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)3 Ignore (org.junit.Ignore)3 RskSystemProperties (co.rsk.config.RskSystemProperties)1 DifficultyCalculator (co.rsk.core.DifficultyCalculator)1 BodyResponseMessage (co.rsk.net.messages.BodyResponseMessage)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)1