Search in sources :

Example 36 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule 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 37 with ProofOfWorkRule

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

the class NodeMessageHandlerTest method processNewBlockHashesMessage.

@Test
public void processNewBlockHashesMessage() throws UnknownHostException {
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    final BlockStore store = new BlockStore();
    final List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 15);
    final List<Block> bcBlocks = blocks.subList(0, 10);
    for (Block b : bcBlocks) blockchain.tryToConnect(b);
    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));
    class TestCase {

        protected final NewBlockHashesMessage message;

        protected final List<Block> expected;

        public TestCase(@Nonnull final NewBlockHashesMessage message, final List<Block> expected) {
            this.message = message;
            this.expected = expected;
        }
    }
    TestCase[] testCases = { new TestCase(new NewBlockHashesMessage(new LinkedList<>()), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(12).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11), blocks.get(12))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))) };
    for (int i = 0; i < testCases.length; i += 1) {
        final TestCase testCase = testCases[i];
        final SimpleMessageChannel sender = new SimpleMessageChannel();
        handler.processMessage(sender, testCase.message);
        if (testCase.expected == null) {
            Assert.assertTrue(sender.getMessages().isEmpty());
            continue;
        }
        Assert.assertEquals(testCase.expected.size(), sender.getMessages().size());
        Assert.assertTrue(sender.getMessages().stream().allMatch(m -> m.getMessageType() == MessageType.GET_BLOCK_MESSAGE));
        List<Keccak256> msgs = sender.getMessages().stream().map(m -> (GetBlockMessage) m).map(m -> m.getBlockHash()).map(h -> new Keccak256(h)).collect(Collectors.toList());
        Set<Keccak256> expected = testCase.expected.stream().map(b -> b.getHash().getBytes()).map(h -> new Keccak256(h)).collect(Collectors.toSet());
        for (Keccak256 h : msgs) {
            Assert.assertTrue(expected.contains(h));
        }
        for (Keccak256 h : expected) {
            Assert.assertTrue(msgs.stream().filter(h1 -> h.equals(h1)).count() == 1);
        }
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) java.util(java.util) BeforeClass(org.junit.BeforeClass) Keccak256(co.rsk.crypto.Keccak256) PeerScoringManager(co.rsk.scoring.PeerScoringManager) BigDecimal(java.math.BigDecimal) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TxHandlerImpl(co.rsk.net.handler.TxHandlerImpl) EventType(co.rsk.scoring.EventType) BigInteger(java.math.BigInteger) ChannelManager(org.ethereum.net.server.ChannelManager) Nonnull(javax.annotation.Nonnull) PunishmentParameters(co.rsk.scoring.PunishmentParameters) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) co.rsk.net.messages(co.rsk.net.messages) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) PeerScoring(co.rsk.scoring.PeerScoring) CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) HashUtil(org.ethereum.crypto.HashUtil) RepositoryImpl(co.rsk.db.RepositoryImpl) Test(org.junit.Test) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) World(co.rsk.test.World) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) RskMockFactory(org.ethereum.util.RskMockFactory) SimpleTransactionPool(co.rsk.net.simples.SimpleTransactionPool) Ignore(org.junit.Ignore) TxHandler(co.rsk.net.handler.TxHandler) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Channel(org.ethereum.net.server.Channel) TransactionUtils(co.rsk.net.utils.TransactionUtils) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) RskSystemProperties(co.rsk.config.RskSystemProperties) RegTestConfig(org.ethereum.config.blockchain.RegTestConfig) Assert(org.junit.Assert) org.ethereum.core(org.ethereum.core) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Nonnull(javax.annotation.Nonnull) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 38 with ProofOfWorkRule

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

the class NodeMessageHandlerTest method processBlockHeadersRequestMessageUsingProcessor.

@Test
public void processBlockHeadersRequestMessageUsingProcessor() throws UnknownHostException {
    byte[] hash = HashUtil.randomHash();
    SimpleBlockProcessor sbp = new SimpleBlockProcessor();
    NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    Message message = new BlockHeadersRequestMessage(100, hash, 50);
    processor.processMessage(new SimpleMessageChannel(), message);
    Assert.assertEquals(100, sbp.getRequestId());
    Assert.assertArrayEquals(hash, sbp.getHash());
}
Also used : SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 39 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule 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 40 with ProofOfWorkRule

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

the class NodeMessageHandlerTest method processFutureBlockMessageUsingProcessor.

@Test
public void processFutureBlockMessageUsingProcessor() throws UnknownHostException {
    SimpleBlockProcessor sbp = new SimpleBlockProcessor();
    NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    Block block = new BlockGenerator().getGenesisBlock();
    Message message = new BlockMessage(block);
    SimpleMessageChannel sender = new SimpleMessageChannel();
    processor.processMessage(sender, message);
    Assert.assertNotNull(sbp.getBlocks());
    Assert.assertEquals(0, sbp.getBlocks().size());
}
Also used : SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) 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