Search in sources :

Example 6 with PeerScoring

use of co.rsk.scoring.PeerScoring in project rskj by rsksmart.

the class NodeMessageHandlerTest method postBlockMessageTwice.

@Test
public void postBlockMessageTwice() throws InterruptedException, UnknownHostException {
    MessageChannel 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.postMessage(sender, message);
    processor.postMessage(sender, message);
    PeerScoring pscoring = scoring.getPeerScoring(sender.getPeerNodeID());
    Assert.assertNotNull(pscoring);
    Assert.assertFalse(pscoring.isEmpty());
    Assert.assertEquals(1, pscoring.getTotalEventCounter());
    Assert.assertEquals(1, pscoring.getEventCounter(EventType.REPEATED_MESSAGE));
}
Also used : PeerScoring(co.rsk.scoring.PeerScoring) PeerScoringManager(co.rsk.scoring.PeerScoringManager) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 7 with PeerScoring

use of co.rsk.scoring.PeerScoring in project rskj by rsksmart.

the class NodeMessageHandlerTest method processMissingPoWBlockMessageUsingProcessor.

@Test
public void processMissingPoWBlockMessageUsingProcessor() 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 DummyBlockValidationRule());
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block = blockGenerator.getGenesisBlock();
    for (int i = 0; i < 50; i++) {
        block = blockGenerator.createChildBlock(block);
    }
    Message message = new BlockMessage(block);
    processor.processMessage(sender, message);
    Assert.assertNotNull(sbp.getBlocks());
    Assert.assertEquals(1, sbp.getBlocks().size());
    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));
    Assert.assertEquals(0, pscoring.getEventCounter(EventType.INVALID_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) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 8 with PeerScoring

use of co.rsk.scoring.PeerScoring in project rskj by rsksmart.

the class NodeMessageHandlerTest method processInvalidPoWMessageUsingProcessor.

// TODO: Difficulty in RegTest is so small that this test will sometimes pass and other times fail
// This should be executed in a special mode where difficulty is high.
@Ignore
public void processInvalidPoWMessageUsingProcessor() 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();
    byte[] mergedMiningHeader = block.getBitcoinMergedMiningHeader();
    // change merged mining nonce.
    mergedMiningHeader[76] += 3;
    Message message = new BlockMessage(block);
    processor.processMessage(sender, message);
    Assert.assertNotNull(sbp.getBlocks());
    Assert.assertEquals(0, sbp.getBlocks().size());
    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.INVALID_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) Ignore(org.junit.Ignore)

Example 9 with PeerScoring

use of co.rsk.scoring.PeerScoring 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 10 with PeerScoring

use of co.rsk.scoring.PeerScoring 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)

Aggregations

PeerScoring (co.rsk.scoring.PeerScoring)10 PeerScoringManager (co.rsk.scoring.PeerScoringManager)10 SimpleBlockProcessor (co.rsk.net.simples.SimpleBlockProcessor)9 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)9 Test (org.junit.Test)8 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)6 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 TxHandler (co.rsk.net.handler.TxHandler)3 SimpleTransactionPool (co.rsk.net.simples.SimpleTransactionPool)3 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)3 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)3 RepositoryImpl (co.rsk.db.RepositoryImpl)1 NodeID (co.rsk.net.NodeID)1 TxHandlerImpl (co.rsk.net.handler.TxHandlerImpl)1 World (co.rsk.test.World)1 BigInteger (java.math.BigInteger)1 CompositeEthereumListener (org.ethereum.listener.CompositeEthereumListener)1 Ignore (org.junit.Ignore)1