Search in sources :

Example 1 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncProcessorTest method sendSkeletonRequest.

@Test
public void sendSkeletonRequest() {
    Blockchain blockchain = 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 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, null, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    processor.sendSkeletonRequest(sender.getPeerNodeID(), 0);
    Message message = msg[0];
    Assert.assertNotNull(message);
    Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
    SkeletonRequestMessage request = (SkeletonRequestMessage) message;
    Assert.assertNotEquals(0, request.getId());
    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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 2 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncProcessorTest method processSkeletonResponseWithTenBlockIdentifiers.

@Test
public void processSkeletonResponseWithTenBlockIdentifiers() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    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 = 0;
    int step = 10;
    int linkCount = 9;
    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.assertArrayEquals(blockIdentifiers.get(1).getHash(), request.getHash());
    Assert.assertEquals(10, request.getCount());
    DownloadingHeadersSyncState syncState = (DownloadingHeadersSyncState) processor.getSyncState();
    List<BlockIdentifier> skeleton = syncState.getSkeleton();
    Assert.assertEquals(10, skeleton.size());
    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) DownloadingHeadersSyncState(co.rsk.net.sync.DownloadingHeadersSyncState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 3 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncProcessorTest method syncWithAdvancedPeerAfterTimeoutWaitingPeers.

@Test
public void syncWithAdvancedPeerAfterTimeoutWaitingPeers() {
    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);
    SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
    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, 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.assertTrue(messages.isEmpty());
    processor.onTimePassed(syncConfiguration.getTimeoutWaitingPeers().dividedBy(2));
    Assert.assertTrue(messages.isEmpty());
    processor.onTimePassed(syncConfiguration.getTimeoutWaitingPeers().dividedBy(2));
    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) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 4 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncProcessorTest method findConnectionPointBlockchainWith30BlocksVsBlockchainWith100Blocks.

@Test
public void findConnectionPointBlockchainWith30BlocksVsBlockchainWith100Blocks() {
    Blockchain blockchain = BlockChainBuilder.ofSize(30);
    Blockchain advancedBlockchain = BlockChainBuilder.copyAndExtend(blockchain, 70);
    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);
    Status status = StatusUtils.fromBlockchain(advancedBlockchain);
    processor.processStatus(sender, status);
    BlockHeadersRequestMessage requestMessage = (BlockHeadersRequestMessage) messages.get(0);
    processor.processBlockHeadersResponse(sender, new BlockHeadersResponseMessage(requestMessage.getId(), Collections.singletonList(advancedBlockchain.getBestBlock().getHeader())));
    long[] binarySearchHeights = new long[] { 50, 25, 37, 31, 28, 29, 30 };
    for (int k = 0; k < binarySearchHeights.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(binarySearchHeights[k], request.getHeight());
        Block block = advancedBlockchain.getBlockByNumber(binarySearchHeights[k]);
        processor.processBlockHashResponse(sender, new BlockHashResponseMessage(requestId, block.getHash().getBytes()));
    }
    Assert.assertEquals(binarySearchHeights.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(30, 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 5 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SimpleChannelManager method getMockedChannel.

private Channel getMockedChannel(MessageChannel mc) {
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(mc.getPeerNodeID());
    return channel;
}
Also used : Channel(org.ethereum.net.server.Channel) MessageChannel(co.rsk.net.MessageChannel) SimpleNodeChannel(co.rsk.net.simples.SimpleNodeChannel)

Aggregations

Channel (org.ethereum.net.server.Channel)17 ChannelManager (org.ethereum.net.server.ChannelManager)13 Test (org.junit.Test)13 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)10 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 RskSystemProperties (co.rsk.config.RskSystemProperties)7 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)7 BlockDifficulty (co.rsk.core.BlockDifficulty)4 NodeID (co.rsk.net.NodeID)3 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)3 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 DownloadingHeadersSyncState (co.rsk.net.sync.DownloadingHeadersSyncState)2 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Coin (co.rsk.core.Coin)1 DifficultyCalculator (co.rsk.core.DifficultyCalculator)1 BlockExecutor (co.rsk.core.bc.BlockExecutor)1 MessageChannel (co.rsk.net.MessageChannel)1