Search in sources :

Example 51 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.

the class NodeMessageHandlerTest method processStatusMessageUsingNodeBlockProcessor.

@Test
@Ignore("This should be reviewed with sync processor or deleted")
public void processStatusMessageUsingNodeBlockProcessor() 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 NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, 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());
    final Message message = new StatusMessage(status);
    handler.processMessage(sender, message);
    Assert.assertNotNull(sender.getGetBlockMessages());
    Assert.assertEquals(1, sender.getGetBlockMessages().size());
    final Message msg = sender.getGetBlockMessages().get(0);
    Assert.assertEquals(MessageType.GET_BLOCK_MESSAGE, msg.getMessageType());
    final GetBlockMessage gbMessage = (GetBlockMessage) msg;
    Assert.assertArrayEquals(block.getHash().getBytes(), gbMessage.getBlockHash());
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 52 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration 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 53 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration 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 54 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration 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 55 with SyncConfiguration

use of co.rsk.net.sync.SyncConfiguration 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)

Aggregations

SyncConfiguration (co.rsk.net.sync.SyncConfiguration)65 Test (org.junit.Test)59 RskSystemProperties (co.rsk.config.RskSystemProperties)48 Blockchain (org.ethereum.core.Blockchain)47 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)42 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)36 Block (org.ethereum.core.Block)36 World (co.rsk.test.World)15 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)15 Ignore (org.junit.Ignore)10 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)9 Keccak256 (co.rsk.crypto.Keccak256)8 SimpleAsyncNode (co.rsk.net.simples.SimpleAsyncNode)8 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)4 BlockDifficulty (co.rsk.core.BlockDifficulty)3 BlockIdentifier (org.ethereum.core.BlockIdentifier)3 Nonnull (javax.annotation.Nonnull)2 DifficultyCalculator (co.rsk.core.DifficultyCalculator)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 TxHandler (co.rsk.net.handler.TxHandler)1