Search in sources :

Example 76 with SimplePeer

use of co.rsk.net.simples.SimplePeer 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, mock(StatusResolver.class));
    Message message = new BlockHeadersRequestMessage(100, hash, 50);
    processor.processMessage(new SimplePeer(), message);
    Assert.assertEquals(100, sbp.getRequestId());
    Assert.assertArrayEquals(hash, sbp.getHash());
}
Also used : SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 77 with SimplePeer

use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.

the class NodeMessageHandlerTest method processStatusMessageUsingSyncProcessor.

@Test()
public void processStatusMessageUsingSyncProcessor() {
    final SimplePeer sender = new SimplePeer();
    final ChannelManager channelManager = mock(ChannelManager.class);
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    final NodeMessageHandler handler = NodeMessageHandlerUtil.createHandlerWithSyncProcessor(SyncConfiguration.IMMEDIATE_FOR_TESTING, channelManager);
    BlockGenerator blockGenerator = new BlockGenerator();
    final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
    final Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
    final Message message = new StatusMessage(status);
    handler.processMessage(sender, message);
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, sender.getMessages().get(0).getMessageType());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 78 with SimplePeer

use of co.rsk.net.simples.SimplePeer 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 NetBlockStore store = new NetBlockStore();
    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, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, mock(StatusResolver.class));
    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 SimplePeer sender = new SimplePeer();
        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) RskAddress(co.rsk.core.RskAddress) Keccak256(co.rsk.crypto.Keccak256) PeerScoringManager(co.rsk.scoring.PeerScoringManager) BigDecimal(java.math.BigDecimal) EventType(co.rsk.scoring.EventType) BigInteger(java.math.BigInteger) ChannelManager(org.ethereum.net.server.ChannelManager) Nonnull(javax.annotation.Nonnull) PunishmentParameters(co.rsk.scoring.PunishmentParameters) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) 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) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) BlockStore(org.ethereum.db.BlockStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) EthereumListener(org.ethereum.listener.EthereumListener) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) SimplePeer(co.rsk.net.simples.SimplePeer) World(co.rsk.test.World) Mockito(org.mockito.Mockito) RskMockFactory(org.ethereum.util.RskMockFactory) TestSystemProperties(co.rsk.config.TestSystemProperties) Ignore(org.junit.Ignore) TransactionUtils(co.rsk.net.utils.TransactionUtils) PeersInformation(co.rsk.net.sync.PeersInformation) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) co.rsk.validators(co.rsk.validators) RskSystemProperties(co.rsk.config.RskSystemProperties) Assert(org.junit.Assert) org.ethereum.core(org.ethereum.core) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Nonnull(javax.annotation.Nonnull) Keccak256(co.rsk.crypto.Keccak256) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 79 with SimplePeer

use of co.rsk.net.simples.SimplePeer 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 NetBlockStore store = new NetBlockStore();
    store.saveBlock(block);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, mock(StatusResolver.class));
    final SimplePeer sender = new SimplePeer();
    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 : World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimplePeer(co.rsk.net.simples.SimplePeer) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 80 with SimplePeer

use of co.rsk.net.simples.SimplePeer 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, mock(StatusResolver.class));
    Block block = new BlockGenerator().getGenesisBlock();
    Message message = new BlockMessage(block);
    SimplePeer sender = new SimplePeer();
    processor.processMessage(sender, message);
    Assert.assertNotNull(sbp.getBlocks());
    Assert.assertEquals(0, sbp.getBlocks().size());
}
Also used : SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Aggregations

SimplePeer (co.rsk.net.simples.SimplePeer)109 Test (org.junit.Test)108 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)81 TestSystemProperties (co.rsk.config.TestSystemProperties)68 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)54 Blockchain (org.ethereum.core.Blockchain)49 Block (org.ethereum.core.Block)46 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)41 BlockStore (org.ethereum.db.BlockStore)28 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)26 EthereumListener (org.ethereum.listener.EthereumListener)26 SimpleBlockProcessor (co.rsk.net.simples.SimpleBlockProcessor)19 PeerScoringManager (co.rsk.scoring.PeerScoringManager)16 Keccak256 (co.rsk.crypto.Keccak256)13 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)13 ChannelManager (org.ethereum.net.server.ChannelManager)11 PeerScoring (co.rsk.scoring.PeerScoring)10 World (co.rsk.test.World)8 Ignore (org.junit.Ignore)7 BlockBuilder (co.rsk.test.builders.BlockBuilder)6