use of co.rsk.net.simples.SimplePeer 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 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 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.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method processTransactionsMessage.
@Test
public void processTransactionsMessage() throws UnknownHostException {
PeerScoringManager scoring = createPeerScoringManager();
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, scoring, mock(StatusResolver.class));
final SimplePeer sender = new SimplePeer(new NodeID(new byte[] { 1 }));
final SimplePeer sender2 = new SimplePeer(new NodeID(new byte[] { 2 }));
final List<Transaction> txs = TransactionUtils.getTransactions(10);
final TransactionsMessage message = new TransactionsMessage(txs);
handler.processMessage(sender, message);
Mockito.verify(transactionGateway, times(1)).receiveTransactionsFrom(txs, Collections.singleton(sender.getPeerNodeID()));
handler.processMessage(sender2, message);
Mockito.verify(transactionGateway, times(1)).receiveTransactionsFrom(txs, Collections.singleton(sender2.getPeerNodeID()));
Assert.assertFalse(scoring.isEmpty());
PeerScoring pscoring = scoring.getPeerScoring(sender.getPeerNodeID());
Assert.assertNotNull(pscoring);
Assert.assertFalse(pscoring.isEmpty());
Assert.assertEquals(10, pscoring.getTotalEventCounter());
Assert.assertEquals(10, pscoring.getEventCounter(EventType.VALID_TRANSACTION));
pscoring = scoring.getPeerScoring(sender2.getPeerNodeID());
Assert.assertNotNull(pscoring);
Assert.assertFalse(pscoring.isEmpty());
Assert.assertEquals(10, pscoring.getTotalEventCounter());
Assert.assertEquals(10, pscoring.getEventCounter(EventType.VALID_TRANSACTION));
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method processBlockByHashRequestMessageUsingProcessor.
@Test
public void processBlockByHashRequestMessageUsingProcessor() throws 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 BlockRequestMessage(100, block.getHash().getBytes());
processor.processMessage(new SimplePeer(), message);
Assert.assertEquals(100, sbp.getRequestId());
Assert.assertArrayEquals(block.getHash().getBytes(), sbp.getHash());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method processGetBlockMessageUsingEmptyStore.
@Test
public void processGetBlockMessageUsingEmptyStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final NetBlockStore store = new NetBlockStore();
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.assertTrue(sender.getMessages().isEmpty());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeMessageHandlerTest method processNewBlockHashesMessageDoesNothingBecauseNodeIsSyncing.
@Test
public void processNewBlockHashesMessageDoesNothingBecauseNodeIsSyncing() {
BlockProcessor blockProcessor = mock(BlockProcessor.class);
Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(true);
final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, null, null, null, mock(StatusResolver.class));
Message message = mock(Message.class);
Mockito.when(message.getMessageType()).thenReturn(MessageType.NEW_BLOCK_HASHES);
final SimplePeer sender = new SimplePeer(new NodeID(new byte[] { 1 }));
handler.processMessage(sender, message);
verify(blockProcessor, never()).processNewBlockHashesMessage(any(), any());
}
Aggregations