Search in sources :

Example 56 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.

the class NodeMessageHandlerTest method processGetBlockHeadersMessage.

@Test
@Ignore("Block headers message is deprecated must be rewrited or deleted")
public void processGetBlockHeadersMessage() throws UnknownHostException {
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    final BlockStore store = new BlockStore();
    final List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 10);
    for (Block b : blocks) {
        ImportResult result = blockchain.tryToConnect(b);
        System.out.println(result);
    }
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    int baseBlock = 9;
    /*
            TestCase represents a GetBlockHeaderMessage test case: it receives an input and the expected BlockHeaders.
            if expected == null, we are not expecting to receive any message.
        */
    class TestCase {

        protected final GetBlockHeadersMessage gbhMessage;

        protected final List<BlockHeader> expected;

        public TestCase(@Nonnull final GetBlockHeadersMessage gbhMessage, final List<BlockHeader> expected) {
            this.gbhMessage = gbhMessage;
            this.expected = expected;
        }
    }
    TestCase[] testCases = { new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 0), null), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 5), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 1).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 3).getHeader(), blocks.get(baseBlock - 4).getHeader())), new TestCase(new GetBlockHeadersMessage(0, blocks.get(baseBlock).getHash().getBytes(), 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), null, 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())) };
    for (int i = 0; i < testCases.length; i += 1) {
        final TestCase testCase = testCases[i];
        final SimpleMessageChannel sender = new SimpleMessageChannel();
        handler.processMessage(sender, testCase.gbhMessage);
        if (testCase.expected == null) {
            Assert.assertEquals(0, sender.getMessages().size());
            continue;
        }
        Assert.assertEquals(1, sender.getMessages().size());
        final Message message = sender.getMessages().get(0);
        Assert.assertEquals(MessageType.BLOCK_HEADERS_MESSAGE, message.getMessageType());
        final List<BlockHeader> headers = ((BlockHeadersMessage) message).getBlockHeaders();
        equalBlockHeaders(testCase.expected, headers);
    }
}
Also used : Nonnull(javax.annotation.Nonnull) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 57 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.

the class OneAsyncNodeTest method createNode.

private static SimpleAsyncNode createNode() {
    final World world = new World();
    final BlockStore store = new BlockStore();
    final Blockchain blockchain = world.getBlockChain();
    RskSystemProperties config = new RskSystemProperties();
    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);
    SimpleChannelManager channelManager = new SimpleChannelManager();
    SyncProcessor syncProcessor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, syncConfiguration, new DummyBlockValidationRule(), new DifficultyCalculator(config));
    NodeMessageHandler handler = new NodeMessageHandler(config, processor, syncProcessor, channelManager, null, null, RskMockFactory.getPeerScoringManager(), new DummyBlockValidationRule());
    return new SimpleAsyncNode(handler, syncProcessor, channelManager);
}
Also used : DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) DifficultyCalculator(co.rsk.core.DifficultyCalculator) SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration)

Example 58 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.

the class SyncProcessorTest method processSkeletonResponseWithoutBlockIdentifiers.

@Test
public void processSkeletonResponseWithoutBlockIdentifiers() {
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, new BlockStore(), blockchain, new BlockNodeInformation(), syncConfiguration);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), syncConfiguration, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    List<BlockIdentifier> blockIdentifiers = new ArrayList<>();
    SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
    processor.registerExpectedMessage(response);
    processor.processSkeletonResponse(sender, response);
    Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
    Assert.assertTrue(sender.getMessages().isEmpty());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 59 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.

the class SyncProcessorTest method processBlockHeadersResponseRejectsNonSolicitedMessages.

@Test
public void processBlockHeadersResponseRejectsNonSolicitedMessages() {
    Blockchain blockchain = BlockChainBuilder.ofSize(3);
    Block block = blockchain.getBlockByNumber(2);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, new BlockStore(), blockchain, new BlockNodeInformation(), syncConfiguration);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), syncConfiguration, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    List<BlockHeader> headers = new ArrayList<>();
    headers.add(block.getHeader());
    BlockHeadersResponseMessage response = new BlockHeadersResponseMessage(new Random().nextLong(), headers);
    processor.registerExpectedMessage(response);
    processor.processBlockHeadersResponse(sender, response);
    Assert.assertEquals(0, sender.getMessages().size());
    Assert.assertEquals(0, processor.getExpectedResponses().size());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 60 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.

the class ThreeAsyncNodeUsingSyncProcessorTest method synchronizeNewNodeWithTwoPeersDefault.

@Test
public void synchronizeNewNodeWithTwoPeersDefault() {
    Blockchain b1 = BlockChainBuilder.ofSize(50, true);
    Blockchain b2 = BlockChainBuilder.ofSize(0, true);
    SimpleAsyncNode node1 = SimpleAsyncNode.createDefaultNode(b1);
    SimpleAsyncNode node2 = SimpleAsyncNode.createDefaultNode(b1);
    SyncConfiguration syncConfiguration = new SyncConfiguration(2, 1, 1, 1, 20, 192);
    SimpleAsyncNode node3 = SimpleAsyncNode.createNode(b2, syncConfiguration);
    Assert.assertEquals(50, node1.getBestBlock().getNumber());
    Assert.assertEquals(50, node2.getBestBlock().getNumber());
    Assert.assertEquals(0, node3.getBestBlock().getNumber());
    node1.sendFullStatusTo(node3);
    node2.sendFullStatusTo(node3);
    // sync setup
    node3.waitUntilNTasksWithTimeout(SyncUtils.syncSetupRequests(50, 0, SyncConfiguration.IMMEDIATE_FOR_TESTING));
    // synchronize 50 new blocks from node 1
    node3.waitExactlyNTasksWithTimeout(50 + 2);
    Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node3.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertEquals(50, node3.getBestBlock().getNumber());
    Assert.assertEquals(node1.getBestBlock().getHash(), node3.getBestBlock().getHash());
    Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node2.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node3.getSyncProcessor().getExpectedResponses().isEmpty());
    node1.joinWithTimeout();
    node2.joinWithTimeout();
    node3.joinWithTimeout();
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node3.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node3.getNodeID()));
    Assert.assertFalse(node3.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
}
Also used : SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Blockchain(org.ethereum.core.Blockchain) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Aggregations

SyncConfiguration (co.rsk.net.sync.SyncConfiguration)65 Test (org.junit.Test)59 RskSystemProperties (co.rsk.config.RskSystemProperties)48 Blockchain (org.ethereum.core.Blockchain)47 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)42 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)36 Block (org.ethereum.core.Block)36 World (co.rsk.test.World)15 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)15 Ignore (org.junit.Ignore)10 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)9 Keccak256 (co.rsk.crypto.Keccak256)8 SimpleAsyncNode (co.rsk.net.simples.SimpleAsyncNode)8 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)4 BlockDifficulty (co.rsk.core.BlockDifficulty)3 BlockIdentifier (org.ethereum.core.BlockIdentifier)3 Nonnull (javax.annotation.Nonnull)2 DifficultyCalculator (co.rsk.core.DifficultyCalculator)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 TxHandler (co.rsk.net.handler.TxHandler)1