Coverage Summary for Class: DecidingSyncState (co.rsk.net.sync)
Class |
Class, %
|
Method, %
|
Line, %
|
DecidingSyncState |
0%
(0/1)
|
0%
(0/4)
|
0%
(0/17)
|
1 package co.rsk.net.sync;
2
3
4 import co.rsk.net.Peer;
5 import org.ethereum.db.BlockStore;
6
7 import java.time.Duration;
8 import java.util.Optional;
9
10 public class DecidingSyncState extends BaseSyncState {
11 private PeersInformation peersInformation;
12 private final BlockStore blockStore;
13
14 public DecidingSyncState(SyncConfiguration syncConfiguration,
15 SyncEventsHandler syncEventsHandler,
16 PeersInformation peersInformation,
17 BlockStore blockStore) {
18 super(syncEventsHandler, syncConfiguration);
19
20 this.peersInformation = peersInformation;
21 this.blockStore = blockStore;
22 }
23
24 @Override
25 public void newPeerStatus() {
26 if (peersInformation.count() >= syncConfiguration.getExpectedPeers()) {
27 tryStartSyncing();
28 }
29 }
30
31 @Override
32 public void tick(Duration duration) {
33 peersInformation.cleanExpired();
34 timeElapsed = timeElapsed.plus(duration);
35 if (peersInformation.count() > 0 &&
36 timeElapsed.compareTo(syncConfiguration.getTimeoutWaitingPeers()) >= 0) {
37
38 tryStartSyncing();
39 }
40 }
41
42 private void tryStartSyncing() {
43
44 Optional<Peer> bestPeer = peersInformation.getBestPeer();
45 if (!bestPeer.isPresent()) {
46 return;
47 }
48
49 long bpBestBlockNumber = peersInformation.getPeer(bestPeer.get()).getStatus().getBestBlockNumber();
50 long distanceToTip = bpBestBlockNumber - blockStore.getBestBlock().getNumber();
51 if (distanceToTip > syncConfiguration.getLongSyncLimit() || blockStore.getMinNumber() == 0) {
52 syncEventsHandler.startSyncing(bestPeer.get());
53 return;
54 }
55 syncEventsHandler.backwardSyncing(bestPeer.get());
56 }
57 }