use of co.rsk.validators.ProofOfWorkRule 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 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 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());
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processTooMuchGasTransactionMessage.
@Test
public void processTooMuchGasTransactionMessage() throws UnknownHostException {
PeerScoringManager scoring = createPeerScoringManager();
final SimpleChannelManager channelManager = new SimpleChannelManager();
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
TransactionPool state = mock(TransactionPool.class);
BlockProcessor blockProcessor = mock(BlockProcessor.class);
Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(false);
TxHandler txHandler = new TxHandlerImpl(config, mock(CompositeEthereumListener.class), mock(RepositoryImpl.class), blockchain);
final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, channelManager, state, txHandler, scoring, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
final SimpleMessageChannel sender = new SimpleMessageChannel();
final List<Transaction> txs = new ArrayList<>();
BigInteger value = BigInteger.ONE;
BigInteger nonce = BigInteger.ZERO;
BigInteger gasPrice = BigInteger.ONE;
BigInteger gasLimit = BigDecimal.valueOf(Math.pow(2, 60)).add(BigDecimal.ONE).toBigInteger();
txs.add(TransactionUtils.createTransaction(TransactionUtils.getPrivateKeyBytes(), TransactionUtils.getAddress(), value, nonce, gasPrice, gasLimit));
final TransactionsMessage message = new TransactionsMessage(txs);
handler.processMessage(sender, message);
Assert.assertNotNull(channelManager.getTransactions());
Assert.assertEquals(0, channelManager.getTransactions().size());
Assert.assertFalse(scoring.isEmpty());
PeerScoring pscoring = scoring.getPeerScoring(sender.getPeerNodeID());
Assert.assertNotNull(pscoring);
Assert.assertFalse(pscoring.isEmpty());
// besides this
Assert.assertEquals(1, pscoring.getTotalEventCounter());
Assert.assertEquals(1, pscoring.getEventCounter(EventType.VALID_TRANSACTION));
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processTransactionsMessageDoesNothingBecauseNodeIsSyncing.
@Test
public void processTransactionsMessageDoesNothingBecauseNodeIsSyncing() {
ChannelManager channelManager = mock(ChannelManager.class);
TxHandler txHandler = mock(TxHandler.class);
BlockProcessor blockProcessor = mock(BlockProcessor.class);
Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(true);
final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, channelManager, null, txHandler, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
Message message = mock(Message.class);
Mockito.when(message.getMessageType()).thenReturn(MessageType.TRANSACTIONS);
handler.processMessage(null, message);
verify(channelManager, never()).broadcastTransactionMessage(any(), any());
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processTransactionsMessageUsingTransactionPool.
@Test
public void processTransactionsMessageUsingTransactionPool() throws UnknownHostException {
final SimpleTransactionPool transactionPool = new SimpleTransactionPool();
final SimpleChannelManager channelManager = new SimpleChannelManager();
TxHandler txmock = mock(TxHandler.class);
BlockProcessor blockProcessor = mock(BlockProcessor.class);
Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(false);
final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, channelManager, transactionPool, txmock, RskMockFactory.getPeerScoringManager(), new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
final SimpleMessageChannel sender = new SimpleMessageChannel();
sender.setPeerNodeID(new NodeID(new byte[] { 1 }));
final SimpleMessageChannel sender2 = new SimpleMessageChannel();
sender2.setPeerNodeID(new NodeID(new byte[] { 2 }));
final List<Transaction> txs = TransactionUtils.getTransactions(10);
Mockito.when(txmock.retrieveValidTxs(any(List.class))).thenReturn(txs);
final TransactionsMessage message = new TransactionsMessage(txs);
handler.processMessage(sender, message);
Assert.assertNotNull(channelManager.getTransactions());
Assert.assertEquals(10, channelManager.getTransactions().size());
for (int k = 0; k < 10; k++) Assert.assertSame(txs.get(k), channelManager.getTransactions().get(k));
Assert.assertNotNull(channelManager.getLastSkip());
Assert.assertEquals(1, channelManager.getLastSkip().size());
Assert.assertTrue(channelManager.getLastSkip().contains(sender.getPeerNodeID()));
channelManager.setLastSkip(null);
handler.processMessage(sender2, message);
Assert.assertNull(channelManager.getLastSkip());
}
use of co.rsk.validators.ProofOfWorkRule 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 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 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());
}
Aggregations