use of co.rsk.net.Peer in project rskj by rsksmart.
the class DecidingSyncStateTest method forwardsSynchronization.
@Test
public void forwardsSynchronization() {
SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
PeersInformation peersInformation = mock(PeersInformation.class);
SyncEventsHandler syncEventsHandler = mock(SyncEventsHandler.class);
BlockStore blockStore = mock(BlockStore.class);
SyncState syncState = new DecidingSyncState(syncConfiguration, syncEventsHandler, peersInformation, blockStore);
when(peersInformation.count()).thenReturn(syncConfiguration.getExpectedPeers() + 1);
Peer peer = mock(Peer.class);
when(peersInformation.getBestPeer()).thenReturn(Optional.of(peer));
SyncPeerStatus syncPeerStatus = mock(SyncPeerStatus.class);
Status status = mock(Status.class);
when(syncPeerStatus.getStatus()).thenReturn(status);
when(peersInformation.getPeer(peer)).thenReturn(syncPeerStatus);
Block block = mock(Block.class);
long myBestBlockNumber = 90L;
when(block.getNumber()).thenReturn(myBestBlockNumber);
when(blockStore.getBestBlock()).thenReturn(block);
when(blockStore.getMinNumber()).thenReturn(1L);
when(status.getBestBlockNumber()).thenReturn(myBestBlockNumber + 1 + syncConfiguration.getLongSyncLimit());
syncState.newPeerStatus();
verify(syncEventsHandler).startSyncing(peer);
}
use of co.rsk.net.Peer in project rskj by rsksmart.
the class DownloadingBackwardsBodiesSyncStateTest method connectingUntilGenesis.
@Test
public void connectingUntilGenesis() {
LinkedList<BlockHeader> toRequest = new LinkedList<>();
LinkedList<BodyResponseMessage> responses = new LinkedList<>();
LinkedList<Block> expectedBlocks = new LinkedList<>();
Function<Long, BlockDifficulty> difficultyForBlockNumber = (n) -> new BlockDifficulty(BigInteger.valueOf(n * (n + 1) / 2));
// their indexes and each one is the children of the previous block.
for (long i = 1; i <= 10; i++) {
BlockHeader headerToRequest = mock(BlockHeader.class);
when(headerToRequest.getNumber()).thenReturn(i);
Keccak256 headerHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.longToBytes(i), 32));
when(headerToRequest.getHash()).thenReturn(headerHash);
toRequest.addFirst(headerToRequest);
when(syncEventsHandler.sendBodyRequest(any(), eq(headerToRequest))).thenReturn(i);
BodyResponseMessage response = new BodyResponseMessage(i, new LinkedList<>(), new LinkedList<>());
responses.addFirst(response);
Block block = mock(Block.class);
expectedBlocks.addFirst(block);
when(block.getNumber()).thenReturn(i);
when(block.getHash()).thenReturn(headerHash);
when(blockFactory.newBlock(headerToRequest, response.getTransactions(), response.getUncles())).thenReturn(block);
when(block.isParentOf(any())).thenReturn(true);
when(blockStore.getTotalDifficultyForHash(headerHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(i));
when(block.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(i)));
}
when(genesis.isParentOf(expectedBlocks.getLast())).thenReturn(true);
when(genesis.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(0L)));
Keccak256 childHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.intToBytes(11), 32));
when(child.getHash()).thenReturn(childHash);
when(blockStore.getTotalDifficultyForHash(childHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(11L));
when(child.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(11L)));
when(child.getNumber()).thenReturn(11L);
DownloadingBackwardsBodiesSyncState target = new DownloadingBackwardsBodiesSyncState(syncConfiguration, syncEventsHandler, peersInformation, genesis, blockFactory, blockStore, child, toRequest, peer);
while (!responses.isEmpty()) {
target.onEnter();
target.newBody(responses.pop(), mock(Peer.class));
Block block = expectedBlocks.pop();
BlockDifficulty expectedDifficulty = difficultyForBlockNumber.apply(block.getNumber());
verify(blockStore).saveBlock(eq(block), eq(expectedDifficulty), eq(true));
}
}
Aggregations