use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeBlockProcessorTest method processGetBlockMessageUsingBlockInBlockchain.
@Test
public void processGetBlockMessageUsingBlockInBlockchain() throws UnknownHostException {
final Blockchain blockchain = new BlockChainBuilder().ofSize(10);
final Block block = blockchain.getBlockByNumber(5);
final Keccak256 blockHash = block.getHash();
final 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);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimplePeer sender = new SimplePeer();
Assert.assertTrue(nodeInformation.getNodesByBlock(block.getHash()).isEmpty());
processor.processGetBlock(sender, block.getHash().getBytes());
Assert.assertTrue(nodeInformation.getNodesByBlock(block.getHash()).contains(sender.getPeerNodeID()));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_MESSAGE, message.getMessageType());
final BlockMessage bMessage = (BlockMessage) message;
Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method postBlockMessageUsingProcessor.
@Test
public void postBlockMessageUsingProcessor() throws InterruptedException, UnknownHostException {
SimpleBlockProcessor sbp = new SimpleBlockProcessor();
NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, mock(StatusResolver.class));
Block block = new BlockChainBuilder().ofSize(1, true).getBestBlock();
Message message = new BlockMessage(block);
processor.start();
processor.postMessage(new SimplePeer(), message);
Thread.sleep(1000);
processor.stop();
Assert.assertNotNull(sbp.getBlocks());
Assert.assertEquals(1, sbp.getBlocks().size());
Assert.assertSame(block, sbp.getBlocks().get(0));
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method fillMessageQueue_thenBlockNewMessages.
@Test
public void fillMessageQueue_thenBlockNewMessages() throws UnknownHostException {
TransactionGateway transactionGateway = mock(TransactionGateway.class);
BlockProcessor blockProcessor = mock(BlockProcessor.class);
Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(false);
final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, null, transactionGateway, RskMockFactory.getPeerScoringManager(), mock(StatusResolver.class));
final SimplePeer sender = new SimplePeer(new NodeID(new byte[] { 1 }));
// Add more than the queue supports
int numMsgToAdd = config.getMessageQueueMaxSize() + 50;
for (int i = 0; i < numMsgToAdd; i++) {
final TransactionsMessage message = new TransactionsMessage(TransactionUtils.getTransactions(1));
handler.postMessage(sender, message);
}
// assert that the surplus was not added
Assert.assertEquals(config.getMessageQueueMaxSize(), (Integer) handler.getMessageQueueSize(sender));
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method processGetBlockMessageUsingBlockInStore.
@Test
public void processGetBlockMessageUsingBlockInStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final NetBlockStore store = new NetBlockStore();
store.saveBlock(block);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, mock(StatusResolver.class));
final SimplePeer sender = new SimplePeer();
handler.processMessage(sender, new GetBlockMessage(block.getHash().getBytes()));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_MESSAGE, message.getMessageType());
final BlockMessage bMessage = (BlockMessage) message;
Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method processBlockHeaderRequestMessageUsingBlockInBlockchain.
@Test
public void processBlockHeaderRequestMessageUsingBlockInBlockchain() throws UnknownHostException {
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final NetBlockStore store = new NetBlockStore();
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, DummyBlockValidator.VALID_RESULT_INSTANCE);
NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, mock(StatusResolver.class));
SimplePeer sender = new SimplePeer();
handler.processMessage(sender, new BlockHeadersRequestMessage(1, blocks.get(4).getHash().getBytes(), 1));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_HEADERS_RESPONSE_MESSAGE, message.getMessageType());
BlockHeadersResponseMessage bMessage = (BlockHeadersResponseMessage) message;
Assert.assertEquals(blocks.get(4).getHash(), bMessage.getBlockHeaders().get(0).getHash());
}
Aggregations