Search in sources :

Example 46 with ProofOfWorkRule

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

the class NodeMessageHandlerTest method processBlockHeaderRequestMessageUsingBlockInStore.

@Test
public void processBlockHeaderRequestMessageUsingBlockInStore() 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 BlockHeadersRequestMessage(1, block.getHash().getBytes(), 1));
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(1, sender.getMessages().size());
    final Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_RESPONSE_MESSAGE, message.getMessageType());
    final BlockHeadersResponseMessage bMessage = (BlockHeadersResponseMessage) message;
    Assert.assertEquals(block.getHash(), bMessage.getBlockHeaders().get(0).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 47 with ProofOfWorkRule

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

the class NodeMessageHandlerTest method processGetBlockHeadersMessage.

@Test
@Ignore("Block headers message is deprecated must be rewrited or deleted")
public void processGetBlockHeadersMessage() 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(), 10);
    for (Block b : blocks) {
        ImportResult result = blockchain.tryToConnect(b);
        System.out.println(result);
    }
    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));
    int baseBlock = 9;
    /*
            TestCase represents a GetBlockHeaderMessage test case: it receives an input and the expected BlockHeaders.
            if expected == null, we are not expecting to receive any message.
        */
    class TestCase {

        protected final GetBlockHeadersMessage gbhMessage;

        protected final List<BlockHeader> expected;

        public TestCase(@Nonnull final GetBlockHeadersMessage gbhMessage, final List<BlockHeader> expected) {
            this.gbhMessage = gbhMessage;
            this.expected = expected;
        }
    }
    TestCase[] testCases = { new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 0), null), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 5), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 1).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 3).getHeader(), blocks.get(baseBlock - 4).getHeader())), new TestCase(new GetBlockHeadersMessage(0, blocks.get(baseBlock).getHash().getBytes(), 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), null, 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())) };
    for (int i = 0; i < testCases.length; i += 1) {
        final TestCase testCase = testCases[i];
        final SimpleMessageChannel sender = new SimpleMessageChannel();
        handler.processMessage(sender, testCase.gbhMessage);
        if (testCase.expected == null) {
            Assert.assertEquals(0, sender.getMessages().size());
            continue;
        }
        Assert.assertEquals(1, sender.getMessages().size());
        final Message message = sender.getMessages().get(0);
        Assert.assertEquals(MessageType.BLOCK_HEADERS_MESSAGE, message.getMessageType());
        final List<BlockHeader> headers = ((BlockHeadersMessage) message).getBlockHeaders();
        equalBlockHeaders(testCase.expected, headers);
    }
}
Also used : Nonnull(javax.annotation.Nonnull) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 48 with ProofOfWorkRule

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

the class SyncProcessorTest method dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers.

@Test
public void dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(0, hash, parentHash, blockchain.getTotalDifficulty());
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.DEFAULT, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    processor.processStatus(sender, status);
    Assert.assertEquals(1, processor.getPeersCount());
    Assert.assertEquals(0, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertFalse(ids.isEmpty());
    Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
    Assert.assertTrue(sender.getMessages().isEmpty());
    processor.onTimePassed(Duration.ofMinutes(1));
    Assert.assertTrue(sender.getMessages().isEmpty());
    processor.onTimePassed(Duration.ofMinutes(1));
    Assert.assertTrue(sender.getMessages().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) RskSystemProperties(co.rsk.config.RskSystemProperties) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 49 with ProofOfWorkRule

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

the class SyncProcessorTest method processSkeletonResponseWithoutBlockIdentifiers.

@Test
public void processSkeletonResponseWithoutBlockIdentifiers() {
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, new BlockStore(), blockchain, new BlockNodeInformation(), syncConfiguration);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), syncConfiguration, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    List<BlockIdentifier> blockIdentifiers = new ArrayList<>();
    SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
    processor.registerExpectedMessage(response);
    processor.processSkeletonResponse(sender, response);
    Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
    Assert.assertTrue(sender.getMessages().isEmpty());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 50 with ProofOfWorkRule

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

the class SyncProcessorTest method processStatusWithAdvancedPeers.

@Test
public void processStatusWithAdvancedPeers() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(100, hash, parentHash, blockchain.getTotalDifficulty().add(new BlockDifficulty(BigInteger.TEN)));
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
    final List<Message> messages = new ArrayList<>();
    when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
        messages.add(invocation.getArgumentAt(1, Message.class));
        return true;
    });
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.processStatus(sender, status);
    Assert.assertEquals(1, processor.getPeersCount());
    Assert.assertEquals(1, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertFalse(ids.isEmpty());
    Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
    Assert.assertFalse(messages.isEmpty());
    Assert.assertEquals(1, messages.size());
    Message message = messages.get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
Also used : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) 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