Search in sources :

Example 1 with SimpleMessageChannel

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

the class NodeMessageHandlerTest method processGetBlockHeaderMessageUsingEmptyStore.

@Test
public void processGetBlockHeaderMessageUsingEmptyStore() throws UnknownHostException {
    final Block block = new BlockGenerator().getBlock(3);
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    final BlockStore store = new BlockStore();
    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 DummyBlockValidationRule());
    final SimpleMessageChannel sender = new SimpleMessageChannel();
    handler.processMessage(sender, new GetBlockHeadersMessage(block.getHash().getBytes(), 1));
    Assert.assertTrue(sender.getMessages().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 2 with SimpleMessageChannel

use of co.rsk.net.simples.SimpleMessageChannel 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)

Aggregations

BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)2 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)2 World (co.rsk.test.World)2 Test (org.junit.Test)2 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)1 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)1 Nonnull (javax.annotation.Nonnull)1 Ignore (org.junit.Ignore)1