Coverage Summary for Class: HashRateCalculator (co.rsk.metrics)
Class |
Method, %
|
Line, %
|
HashRateCalculator |
0%
(0/9)
|
0%
(0/35)
|
HashRateCalculator$MockitoMock$1985098751 |
HashRateCalculator$MockitoMock$1985098751$auxiliary$19dUDt19 |
HashRateCalculator$MockitoMock$1985098751$auxiliary$eKwBnOoc |
HashRateCalculator$MockitoMock$1985098751$auxiliary$faNVVR5x |
HashRateCalculator$MockitoMock$1985098751$auxiliary$lIIjrnfF |
HashRateCalculator$MockitoMock$1985098751$auxiliary$PuYi2kyx |
HashRateCalculator$MockitoMock$1985098751$auxiliary$Wuf6Rejf |
Total |
0%
(0/9)
|
0%
(0/35)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.metrics;
20
21 import co.rsk.crypto.Keccak256;
22 import co.rsk.util.RskCustomCache;
23 import org.ethereum.core.Block;
24 import org.ethereum.db.BlockStore;
25
26 import java.math.BigInteger;
27 import java.time.Clock;
28 import java.time.Duration;
29 import java.time.Instant;
30 import java.util.function.Predicate;
31
32 public abstract class HashRateCalculator {
33
34 private final BlockStore blockStore;
35 private final RskCustomCache<Keccak256, BlockHeaderElement> headerCache;
36
37 public HashRateCalculator(BlockStore blockStore, RskCustomCache<Keccak256, BlockHeaderElement> headerCache) {
38 this.blockStore = blockStore;
39 this.headerCache = headerCache;
40 }
41
42 public void start() {
43 headerCache.start();
44 }
45
46 public void stop() {
47 headerCache.stop();
48 }
49
50 public abstract BigInteger calculateNodeHashRate(Duration duration);
51
52 public BigInteger calculateNetHashRate(Duration period) {
53 return calculateHashRate(b -> true, period);
54 }
55
56 protected BigInteger calculateHashRate(Predicate<BlockHeaderElement> countCondition, Duration period) {
57 if (hasBestBlock()) {
58 Instant upto = Clock.systemUTC().instant();
59 Instant from = upto.minus(period);
60 return this.hashRate(getHeaderElement(blockStore.getBestBlock().getHash()), countCondition, b -> checkBlockTimeRange(b, from, upto));
61 }
62 return BigInteger.ZERO;
63 }
64
65 private BigInteger hashRate(BlockHeaderElement elem, Predicate<BlockHeaderElement> countCondition, Predicate<BlockHeaderElement> cutCondition) {
66 BigInteger hashRate = BigInteger.ZERO;
67 BlockHeaderElement element = elem;
68
69 while (element != null && cutCondition.test(element)) {
70 if (countCondition.test(element)) {
71 hashRate = hashRate.add(element.getDifficulty().asBigInteger());
72 }
73
74 Keccak256 parentHash = element.getBlockHeader().getParentHash();
75
76 element = getHeaderElement(parentHash);
77 }
78 return hashRate;
79 }
80
81 private boolean checkBlockTimeRange(BlockHeaderElement element, Instant from, Instant upto) {
82 Instant ts = Instant.ofEpochSecond(element.getBlockHeader().getTimestamp());
83 return !ts.isBefore(from) && !ts.isAfter(upto);
84 }
85
86 private Boolean hasBestBlock() {
87 return blockStore.getBestBlock() != null;
88 }
89
90 private BlockHeaderElement getHeaderElement(Keccak256 hash) {
91 BlockHeaderElement element = null;
92 if (hash != null) {
93 element = this.headerCache.get(hash);
94 if (element == null) {
95 Block block = this.blockStore.getBlockByHash(hash.getBytes());
96 if (block != null) {
97 element = new BlockHeaderElement(block.getHeader(), this.blockStore.getBlockByHash(hash.getBytes()).getCumulativeDifficulty());
98 this.headerCache.put(hash, element);
99 }
100 }
101 }
102 return element;
103 }
104 }