Coverage Summary for Class: ChunksDownloadHelper (co.rsk.net.sync)
Class |
Class, %
|
Method, %
|
Line, %
|
ChunksDownloadHelper |
0%
(0/1)
|
0%
(0/6)
|
0%
(0/17)
|
1 package co.rsk.net.sync;
2
3 import com.google.common.annotations.VisibleForTesting;
4 import org.ethereum.core.BlockIdentifier;
5
6 import javax.annotation.Nonnull;
7 import java.util.List;
8 import java.util.Optional;
9
10 public class ChunksDownloadHelper {
11 private SyncConfiguration syncConfiguration;
12
13 // Block identifiers retrieved in skeleton
14 private List<BlockIdentifier> skeleton;
15 private long connectionPoint;
16 private int lastRequestedLinkIndex;
17
18 public ChunksDownloadHelper(@Nonnull SyncConfiguration syncConfiguration, List<BlockIdentifier> skeleton, long connectionPoint) {
19 this.syncConfiguration = syncConfiguration;
20 this.connectionPoint = connectionPoint;
21 this.lastRequestedLinkIndex = 0;
22 this.skeleton = skeleton;
23 }
24
25 public boolean hasNextChunk() {
26 int linkIndex = this.lastRequestedLinkIndex + 1;
27 return linkIndex < skeleton.size() && linkIndex <= syncConfiguration.getMaxSkeletonChunks();
28 }
29
30 public Optional<ChunkDescriptor> getCurrentChunk() {
31 return Optional.of(getChunk(this.lastRequestedLinkIndex));
32 }
33
34 public ChunkDescriptor getNextChunk() {
35 // We use 0 so we start iterarting from the second element,
36 // because we always have the first element in our blockchain
37 return getChunk(this.lastRequestedLinkIndex + 1);
38 }
39
40 private ChunkDescriptor getChunk(int linkIndex) {
41 byte[] hash = skeleton.get(linkIndex).getHash();
42 long height = skeleton.get(linkIndex).getNumber();
43
44 long lastHeight = skeleton.get(linkIndex - 1).getNumber();
45 long previousKnownHeight = Math.max(lastHeight, connectionPoint);
46 int count = (int)(height - previousKnownHeight);
47 this.lastRequestedLinkIndex = linkIndex;
48
49 return new ChunkDescriptor(hash, count);
50 }
51
52 @VisibleForTesting
53 public List<BlockIdentifier> getSkeleton() {
54 return this.skeleton;
55 }
56 }