use of co.rsk.net.Peer in project rskj by rsksmart.
the class DecidingSyncStateTest method forwardsSynchronization_genesisIsConnected.
@Test
public void forwardsSynchronization_genesisIsConnected() {
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);
when(blockStore.getMinNumber()).thenReturn(0L);
Block block = mock(Block.class);
long myBestBlockNumber = 90L;
when(block.getNumber()).thenReturn(myBestBlockNumber);
when(blockStore.getBestBlock()).thenReturn(block);
when(status.getBestBlockNumber()).thenReturn(myBestBlockNumber + 1);
syncState.newPeerStatus();
verify(syncEventsHandler).startSyncing(peer);
}
use of co.rsk.net.Peer in project rskj by rsksmart.
the class DownloadingBackwardsBodiesSyncStateTest method connecting_notGenesis.
@Test
public void connecting_notGenesis() {
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 = 2; 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)));
}
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));
}
}
use of co.rsk.net.Peer in project rskj by rsksmart.
the class DecidingSyncState method tryStartSyncing.
private void tryStartSyncing() {
Optional<Peer> bestPeerOpt = peersInformation.getBestPeer();
Optional<Long> peerBestBlockNumOpt = bestPeerOpt.flatMap(this::getPeerBestBlockNumber);
if (!bestPeerOpt.isPresent() || !peerBestBlockNumOpt.isPresent()) {
// no best peer, skip
syncEventsHandler.onLongSyncUpdate(false, null);
return;
}
Peer bestPeer = bestPeerOpt.get();
long peerBestBlockNum = peerBestBlockNumOpt.get();
if (shouldLongSync(peerBestBlockNum)) {
// start "long" / "forward" sync
syncEventsHandler.onLongSyncUpdate(true, peerBestBlockNum);
syncEventsHandler.startSyncing(bestPeer);
} else {
// start "short" / "backward" sync
syncEventsHandler.onLongSyncUpdate(false, null);
syncEventsHandler.backwardSyncing(bestPeer);
}
}
use of co.rsk.net.Peer in project rskj by rsksmart.
the class SimpleChannelManager method getMessageChannel.
public Peer getMessageChannel(SimpleNode sender, SimpleNode receiver) {
Peer channel = simpleChannels.get(sender.getNodeID());
if (channel != null) {
return channel;
}
channel = new SimpleNodeChannel(sender, receiver);
simpleChannels.put(channel.getPeerNodeID(), channel);
return channel;
}
use of co.rsk.net.Peer in project rskj by rsksmart.
the class DecidingSyncStateTest method backwardsSynchronization.
@Test
public void backwardsSynchronization() {
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);
when(blockStore.getMinNumber()).thenReturn(1L);
Block block = mock(Block.class);
long myBestBlockNumber = 90L;
when(block.getNumber()).thenReturn(myBestBlockNumber);
when(blockStore.getBestBlock()).thenReturn(block);
when(status.getBestBlockNumber()).thenReturn(myBestBlockNumber + syncConfiguration.getLongSyncLimit());
syncState.newPeerStatus();
verify(syncEventsHandler).backwardSyncing(peer);
}
Aggregations