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());
}
Aggregations