* 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. */
| 61 | * If 'height' is nonnegative, compute the estimate at the time when a given block was found. |
| 62 | */ |
| 63 | static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_chain) { |
| 64 | const CBlockIndex* pb = active_chain.Tip(); |
| 65 | |
| 66 | if (height >= 0 && height < active_chain.Height()) { |
| 67 | pb = active_chain[height]; |
| 68 | } |
| 69 | |
| 70 | if (pb == nullptr || !pb->nHeight) |
| 71 | return 0; |
| 72 | |
| 73 | // If lookup is -1, then use blocks since last difficulty change. |
| 74 | if (lookup <= 0) |
| 75 | lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1; |
| 76 | |
| 77 | // If lookup is larger than chain, then set it to chain length. |
| 78 | if (lookup > pb->nHeight) |
| 79 | lookup = pb->nHeight; |
| 80 | |
| 81 | const CBlockIndex* pb0 = pb; |
| 82 | int64_t minTime = pb0->GetBlockTime(); |
| 83 | int64_t maxTime = minTime; |
| 84 | for (int i = 0; i < lookup; i++) { |
| 85 | pb0 = pb0->pprev; |
| 86 | int64_t time = pb0->GetBlockTime(); |
| 87 | minTime = std::min(time, minTime); |
| 88 | maxTime = std::max(time, maxTime); |
| 89 | } |
| 90 | |
| 91 | // In case there's a situation where minTime == maxTime, we don't want a divide by zero exception. |
| 92 | if (minTime == maxTime) |
| 93 | return 0; |
| 94 | |
| 95 | arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork; |
| 96 | int64_t timeDiff = maxTime - minTime; |
| 97 | |
| 98 | return workDiff.getdouble() / timeDiff; |
| 99 | } |
| 100 | |
| 101 | static RPCHelpMan getnetworkhashps() |
| 102 | { |
no test coverage detected