Search in sources :

Example 1 with DownloadingHeadersSyncState

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

Aggregations

RskSystemProperties (co.rsk.config.RskSystemProperties)1 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)1 DownloadingHeadersSyncState (co.rsk.net.sync.DownloadingHeadersSyncState)1 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)1 Channel (org.ethereum.net.server.Channel)1 ChannelManager (org.ethereum.net.server.ChannelManager)1 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1