use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class NodeMessageHandlerTest method processStatusMessageUsingNodeBlockProcessor.
@Test
@Ignore("This should be reviewed with sync processor or deleted")
public void processStatusMessageUsingNodeBlockProcessor() throws UnknownHostException {
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 SimpleMessageChannel sender = new SimpleMessageChannel();
final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
BlockGenerator blockGenerator = new BlockGenerator();
final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
final Status status = new Status(block.getNumber(), block.getHash().getBytes());
final Message message = new StatusMessage(status);
handler.processMessage(sender, message);
Assert.assertNotNull(sender.getGetBlockMessages());
Assert.assertEquals(1, sender.getGetBlockMessages().size());
final Message msg = sender.getGetBlockMessages().get(0);
Assert.assertEquals(MessageType.GET_BLOCK_MESSAGE, msg.getMessageType());
final GetBlockMessage gbMessage = (GetBlockMessage) msg;
Assert.assertArrayEquals(block.getHash().getBytes(), gbMessage.getBlockHash());
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class NodeMessageHandlerTest method processNewBlockHashesMessage.
@Test
public void processNewBlockHashesMessage() 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(), 15);
final List<Block> bcBlocks = blocks.subList(0, 10);
for (Block b : bcBlocks) blockchain.tryToConnect(b);
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));
class TestCase {
protected final NewBlockHashesMessage message;
protected final List<Block> expected;
public TestCase(@Nonnull final NewBlockHashesMessage message, final List<Block> expected) {
this.message = message;
this.expected = expected;
}
}
TestCase[] testCases = { new TestCase(new NewBlockHashesMessage(new LinkedList<>()), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(12).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11), blocks.get(12))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))) };
for (int i = 0; i < testCases.length; i += 1) {
final TestCase testCase = testCases[i];
final SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, testCase.message);
if (testCase.expected == null) {
Assert.assertTrue(sender.getMessages().isEmpty());
continue;
}
Assert.assertEquals(testCase.expected.size(), sender.getMessages().size());
Assert.assertTrue(sender.getMessages().stream().allMatch(m -> m.getMessageType() == MessageType.GET_BLOCK_MESSAGE));
List<Keccak256> msgs = sender.getMessages().stream().map(m -> (GetBlockMessage) m).map(m -> m.getBlockHash()).map(h -> new Keccak256(h)).collect(Collectors.toList());
Set<Keccak256> expected = testCase.expected.stream().map(b -> b.getHash().getBytes()).map(h -> new Keccak256(h)).collect(Collectors.toSet());
for (Keccak256 h : msgs) {
Assert.assertTrue(expected.contains(h));
}
for (Keccak256 h : expected) {
Assert.assertTrue(msgs.stream().filter(h1 -> h.equals(h1)).count() == 1);
}
}
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class NodeMessageHandlerTest method processStatusMessageWithKnownBestBlock.
@Test
public void processStatusMessageWithKnownBestBlock() throws UnknownHostException {
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 SimpleMessageChannel sender = new SimpleMessageChannel();
final SyncProcessor syncProcessor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), RskMockFactory.getChannelManager(), syncConfiguration, new DummyBlockValidationRule(), null);
final NodeMessageHandler handler = new NodeMessageHandler(config, bp, syncProcessor, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
BlockGenerator blockGenerator = new BlockGenerator();
final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
final Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), blockchain.getTotalDifficulty());
final Message message = new StatusMessage(status);
store.saveBlock(block);
handler.processMessage(sender, message);
Assert.assertNotNull(sender.getMessages());
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class NodeMessageHandlerTest method processGetBlockMessageUsingBlockInBlockchain.
@Test
public void processGetBlockMessageUsingBlockInBlockchain() throws UnknownHostException {
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 10);
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 bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, new GetBlockMessage(blocks.get(4).getHash().getBytes()));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_MESSAGE, message.getMessageType());
BlockMessage bmessage = (BlockMessage) message;
Assert.assertEquals(blocks.get(4).getHash(), bmessage.getBlock().getHash());
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class NodeMessageHandlerTest method processBlockHeaderRequestMessageUsingBlockInStore.
@Test
public void processBlockHeaderRequestMessageUsingBlockInStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
store.saveBlock(block);
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));
final SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, new BlockHeadersRequestMessage(1, block.getHash().getBytes(), 1));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_HEADERS_RESPONSE_MESSAGE, message.getMessageType());
final BlockHeadersResponseMessage bMessage = (BlockHeadersResponseMessage) message;
Assert.assertEquals(block.getHash(), bMessage.getBlockHeaders().get(0).getHash());
}
Aggregations