Search in sources :

Example 41 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule 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());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 42 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.

the class NodeMessageHandlerTest method processTransactionsMessage.

@Test
public void processTransactionsMessage() throws UnknownHostException {
    PeerScoringManager scoring = createPeerScoringManager();
    final SimpleChannelManager channelManager = new SimpleChannelManager();
    TxHandler txmock = mock(TxHandler.class);
    TransactionPool state = mock(TransactionPool.class);
    Mockito.when(state.addTransactions(any())).thenAnswer(i -> i.getArguments()[0]);
    BlockProcessor blockProcessor = mock(BlockProcessor.class);
    Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(false);
    final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, channelManager, state, txmock, scoring, 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()));
    handler.processMessage(sender2, message);
    Assert.assertNotNull(channelManager.getLastSkip());
    Assert.assertEquals(2, channelManager.getLastSkip().size());
    Assert.assertTrue(channelManager.getLastSkip().contains(sender.getPeerNodeID()));
    Assert.assertTrue(channelManager.getLastSkip().contains(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));
}
Also used : SimpleTransactionPool(co.rsk.net.simples.SimpleTransactionPool) TxHandler(co.rsk.net.handler.TxHandler) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) PeerScoring(co.rsk.scoring.PeerScoring) PeerScoringManager(co.rsk.scoring.PeerScoringManager) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) Test(org.junit.Test)

Example 43 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule 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());
}
Also used : 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) Test(org.junit.Test)

Example 44 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.

the class NodeMessageHandlerTest method processBlockMessageUsingProcessor.

@Test
public void processBlockMessageUsingProcessor() throws UnknownHostException {
    SimpleMessageChannel sender = new SimpleMessageChannel();
    PeerScoringManager scoring = createPeerScoringManager();
    SimpleBlockProcessor sbp = new SimpleBlockProcessor();
    NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, scoring, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    Block block = BlockChainBuilder.ofSize(1, true).getBestBlock();
    Message message = new BlockMessage(block);
    processor.processMessage(sender, message);
    Assert.assertNotNull(sbp.getBlocks());
    Assert.assertEquals(1, sbp.getBlocks().size());
    Assert.assertSame(block, sbp.getBlocks().get(0));
    Assert.assertFalse(scoring.isEmpty());
    PeerScoring pscoring = scoring.getPeerScoring(sender.getPeerNodeID());
    Assert.assertNotNull(pscoring);
    Assert.assertFalse(pscoring.isEmpty());
    Assert.assertEquals(1, pscoring.getTotalEventCounter());
    Assert.assertEquals(1, pscoring.getEventCounter(EventType.VALID_BLOCK));
    pscoring = scoring.getPeerScoring(sender.getAddress());
    Assert.assertNotNull(pscoring);
    Assert.assertFalse(pscoring.isEmpty());
    Assert.assertEquals(1, pscoring.getTotalEventCounter());
    Assert.assertEquals(1, pscoring.getEventCounter(EventType.VALID_BLOCK));
}
Also used : PeerScoring(co.rsk.scoring.PeerScoring) PeerScoringManager(co.rsk.scoring.PeerScoringManager) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 45 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.

the class NodeMessageHandlerTest method processNewBlockHashesMessageDoesNothingBecauseNodeIsSyncing.

@Test
public void processNewBlockHashesMessageDoesNothingBecauseNodeIsSyncing() {
    TxHandler txHandler = mock(TxHandler.class);
    BlockProcessor blockProcessor = mock(BlockProcessor.class);
    Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(true);
    final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, null, null, txHandler, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    Message message = mock(Message.class);
    Mockito.when(message.getMessageType()).thenReturn(MessageType.NEW_BLOCK_HASHES);
    handler.processMessage(null, message);
    verify(blockProcessor, never()).processNewBlockHashesMessage(any(), any());
}
Also used : TxHandler(co.rsk.net.handler.TxHandler) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Aggregations

ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)61 Test (org.junit.Test)57 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)40 RskSystemProperties (co.rsk.config.RskSystemProperties)21 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)16 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)16 SimpleBlockProcessor (co.rsk.net.simples.SimpleBlockProcessor)14 EthereumImpl (org.ethereum.facade.EthereumImpl)14 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)13 World (co.rsk.test.World)11 BlockUnclesValidationRule (co.rsk.validators.BlockUnclesValidationRule)11 PeerScoringManager (co.rsk.scoring.PeerScoringManager)9 ChannelManager (org.ethereum.net.server.ChannelManager)9 Channel (org.ethereum.net.server.Channel)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 TxHandler (co.rsk.net.handler.TxHandler)7 PeerScoring (co.rsk.scoring.PeerScoring)7 BlockDifficulty (co.rsk.core.BlockDifficulty)6 SimpleTransactionPool (co.rsk.net.simples.SimpleTransactionPool)5 DownloadingBodiesSyncState (co.rsk.net.sync.DownloadingBodiesSyncState)4