Search in sources :

Example 86 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method syncWithAdvancedStatusAnd5Peers.

@Test
public void syncWithAdvancedStatusAnd5Peers() {
    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);
    final ChannelManager channelManager = mock(ChannelManager.class);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.DEFAULT, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    List<SimpleMessageChannel> senders = new ArrayList<>();
    List<Channel> channels = new ArrayList<>();
    Map<NodeID, List<Message>> messagesByNode = new HashMap<>();
    int lessPeers = SyncConfiguration.DEFAULT.getExpectedPeers() - 1;
    for (int i = 0; i < lessPeers; i++) {
        SimpleMessageChannel sender = new SimpleMessageChannel();
        senders.add(sender);
        Channel channel = mock(Channel.class);
        messagesByNode.put(sender.getPeerNodeID(), new ArrayList<>());
        when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
        when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
        when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
            messagesByNode.get(sender.getPeerNodeID()).add(invocation.getArgumentAt(1, Message.class));
            return true;
        });
    }
    messagesByNode.values().forEach(m -> Assert.assertTrue(m.isEmpty()));
    senders.forEach(s -> processor.processStatus(s, status));
    messagesByNode.values().forEach(m -> Assert.assertTrue(m.isEmpty()));
    Assert.assertEquals(lessPeers, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    senders.stream().map(SimpleMessageChannel::getPeerNodeID).forEach(peerId -> Assert.assertTrue(ids.contains(peerId)));
    SimpleMessageChannel lastSender = new SimpleMessageChannel();
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(lastSender.getPeerNodeID());
    Assert.assertFalse(ids.contains(lastSender.getPeerNodeID()));
    processor.processStatus(lastSender, status);
    // now test with all senders
    senders.add(lastSender);
    Assert.assertTrue(ids.contains(lastSender.getPeerNodeID()));
    Assert.assertFalse(messagesByNode.values().stream().allMatch(m -> m.isEmpty()));
    Assert.assertEquals(1, messagesByNode.values().stream().mapToInt(List::size).sum());
    Message message = messagesByNode.values().stream().filter(m -> !m.isEmpty()).findFirst().get().get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) java.util(java.util) StatusUtils(co.rsk.net.utils.StatusUtils) DifficultyCalculator(co.rsk.core.DifficultyCalculator) Hex(org.spongycastle.util.encoders.Hex) Coin(co.rsk.core.Coin) PeerScoringManager(co.rsk.scoring.PeerScoringManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DownloadingHeadersSyncState(co.rsk.net.sync.DownloadingHeadersSyncState) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) BigInteger(java.math.BigInteger) ChannelManager(org.ethereum.net.server.ChannelManager) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) co.rsk.net.messages(co.rsk.net.messages) DownloadingBodiesSyncState(co.rsk.net.sync.DownloadingBodiesSyncState) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) BlockExecutor(co.rsk.core.bc.BlockExecutor) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Mockito.when(org.mockito.Mockito.when) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Matchers.any(org.mockito.Matchers.any) RskMockFactory(org.ethereum.util.RskMockFactory) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Channel(org.ethereum.net.server.Channel) RskSystemProperties(co.rsk.config.RskSystemProperties) Assert(org.junit.Assert) org.ethereum.core(org.ethereum.core) ECKey(org.ethereum.crypto.ECKey) Mockito.mock(org.mockito.Mockito.mock) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) 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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 87 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks.

@Test
public void findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks() {
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    Blockchain advancedBlockchain = BlockChainBuilder.ofSize(100);
    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;
    });
    BlockStore store = new BlockStore();
    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(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new DummyBlockValidationRule(), DIFFICULTY_CALCULATOR);
    processor.processStatus(sender, StatusUtils.fromBlockchain(advancedBlockchain));
    BlockHeadersRequestMessage requestMessage = (BlockHeadersRequestMessage) messages.get(0);
    processor.processBlockHeadersResponse(sender, new BlockHeadersResponseMessage(requestMessage.getId(), Collections.singletonList(advancedBlockchain.getBestBlock().getHeader())));
    long[] expectedHeights = new long[] { 50, 25, 12, 6, 3, 1 };
    for (int k = 0; k < expectedHeights.length; k++) {
        Assert.assertEquals(k + 2, messages.size());
        Message message = messages.get(k + 1);
        Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
        BlockHashRequestMessage request = (BlockHashRequestMessage) message;
        long requestId = request.getId();
        Assert.assertEquals(expectedHeights[k], request.getHeight());
        Block block = advancedBlockchain.getBlockByNumber(expectedHeights[k]);
        processor.processBlockHashResponse(sender, new BlockHashResponseMessage(requestId, block.getHash().getBytes()));
    }
    Assert.assertEquals(expectedHeights.length + 2, messages.size());
    Message message = messages.get(messages.size() - 1);
    Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
    SkeletonRequestMessage request = (SkeletonRequestMessage) message;
    Assert.assertEquals(0, request.getStartNumber());
    Assert.assertEquals(1, processor.getExpectedResponses().size());
}
Also used : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 88 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method doesntProcessInvalidBodyResponse.

@Test
public void doesntProcessInvalidBodyResponse() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(10);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
    Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
    Assert.assertEquals(11, block.getNumber());
    Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
    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.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    List<Transaction> transactions = blockchain.getBestBlock().getTransactionsList();
    List<BlockHeader> uncles = blockchain.getBestBlock().getUncleList();
    Account senderAccount = createAccount("sender");
    Account receiverAccount = createAccount("receiver");
    Transaction tx = createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    long lastRequestId = new Random().nextLong();
    BodyResponseMessage response = new BodyResponseMessage(lastRequestId, txs, uncles);
    processor.registerExpectedMessage(response);
    Deque<BlockHeader> headerStack = new ArrayDeque<>();
    headerStack.add(block.getHeader());
    headerStack.add(block.getHeader());
    headerStack.add(block.getHeader());
    List<Deque<BlockHeader>> headers = new ArrayList<>();
    headers.add(headerStack);
    List<BlockIdentifier> bids = new ArrayList<>();
    bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
    bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
    processor.startDownloadingBodies(headers, Collections.singletonMap(sender.getPeerNodeID(), bids));
    ((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
    processor.processBodyResponse(sender, response);
    Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
    Assert.assertNotEquals(block.getNumber(), blockchain.getBestBlock().getNumber());
    // if an unexpected body arrives then stops syncing
    Assert.assertFalse(processor.getSyncState().isSyncing());
}
Also used : BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) DownloadingBodiesSyncState(co.rsk.net.sync.DownloadingBodiesSyncState) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 89 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method processBlockHeadersResponseRejectsNonSolicitedMessages.

@Test
public void processBlockHeadersResponseRejectsNonSolicitedMessages() {
    Blockchain blockchain = BlockChainBuilder.ofSize(3);
    Block block = blockchain.getBlockByNumber(2);
    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<BlockHeader> headers = new ArrayList<>();
    headers.add(block.getHeader());
    BlockHeadersResponseMessage response = new BlockHeadersResponseMessage(new Random().nextLong(), headers);
    processor.registerExpectedMessage(response);
    processor.processBlockHeadersResponse(sender, response);
    Assert.assertEquals(0, sender.getMessages().size());
    Assert.assertEquals(0, processor.getExpectedResponses().size());
}
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 90 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method processSkeletonResponseWithConnectionPoint.

@Test
public void processSkeletonResponseWithConnectionPoint() {
    Blockchain blockchain = BlockChainBuilder.ofSize(25);
    final BlockStore store = new BlockStore();
    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 Message[] msg = new Message[1];
    when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
        msg[0] = 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);
    int connectionPoint = 25;
    int step = 10;
    int linkCount = 10;
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), connectionPoint);
    processor.startDownloadingSkeleton(connectionPoint);
    List<BlockIdentifier> blockIdentifiers = buildSkeleton(blockchain, connectionPoint, step, linkCount);
    SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
    processor.registerExpectedMessage(response);
    processor.processSkeletonResponse(sender, response);
    Message message = msg[0];
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(5, request.getCount());
    Assert.assertArrayEquals(blockIdentifiers.get(1).getHash(), request.getHash());
    Assert.assertEquals(1, processor.getExpectedResponses().size());
}
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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Aggregations

RskSystemProperties (co.rsk.config.RskSystemProperties)112 Test (org.junit.Test)81 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)48 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)45 Blockchain (org.ethereum.core.Blockchain)45 Block (org.ethereum.core.Block)41 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)35 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)20 Ignore (org.junit.Ignore)10 Keccak256 (co.rsk.crypto.Keccak256)8 RegTestConfig (org.ethereum.config.blockchain.RegTestConfig)8 Repository (org.ethereum.core.Repository)8 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)8 Channel (org.ethereum.net.server.Channel)7 ChannelManager (org.ethereum.net.server.ChannelManager)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 BlockDifficulty (co.rsk.core.BlockDifficulty)6 RskAddress (co.rsk.core.RskAddress)6 PersonalModule (co.rsk.rpc.modules.personal.PersonalModule)6 BeforeClass (org.junit.BeforeClass)6