MCPcopy Create free account
hub / github.com/bitcoinxt/bitcoinxt / GetNetworkHashPS

Function GetNetworkHashPS

src/rpcmining.cpp:37–72  ·  view source on GitHub ↗

* Return average network hashes per second based on the last 'lookup' blocks, * or from the last difficulty change if 'lookup' is nonpositive. * If 'height' is nonnegative, compute the estimate at the time when a given block was found. */

Source from the content-addressed store, hash-verified

35 * If 'height' is nonnegative, compute the estimate at the time when a given block was found.
36 */
37UniValue GetNetworkHashPS(int lookup, int height) {
38 CBlockIndex *pb = chainActive.Tip();
39
40 if (height >= 0 && height < chainActive.Height())
41 pb = chainActive[height];
42
43 if (pb == NULL || !pb->nHeight)
44 return 0;
45
46 // If lookup is -1, then use blocks since last difficulty change.
47 if (lookup <= 0)
48 lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
49
50 // If lookup is larger than chain, then set it to chain length.
51 if (lookup > pb->nHeight)
52 lookup = pb->nHeight;
53
54 CBlockIndex *pb0 = pb;
55 int64_t minTime = pb0->GetBlockTime();
56 int64_t maxTime = minTime;
57 for (int i = 0; i < lookup; i++) {
58 pb0 = pb0->pprev;
59 int64_t time = pb0->GetBlockTime();
60 minTime = std::min(time, minTime);
61 maxTime = std::max(time, maxTime);
62 }
63
64 // In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
65 if (minTime == maxTime)
66 return 0;
67
68 arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork;
69 int64_t timeDiff = maxTime - minTime;
70
71 return (int64_t)(workDiff.getdouble() / timeDiff);
72}
73
74UniValue getnetworkhashps(const UniValue& params, bool fHelp)
75{

Callers 1

getnetworkhashpsFunction · 0.85

Calls 6

HeightMethod · 0.80
getdoubleMethod · 0.80
ParamsClass · 0.70
TipMethod · 0.45
GetBlockTimeMethod · 0.45

Tested by

no test coverage detected