* 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. */
| 49 | * If 'height' is nonnegative, compute the estimate at the time when a given block was found. |
| 50 | */ |
| 51 | static UniValue GetNetworkHashPS(int lookup, int height) { |
| 52 | CBlockIndex *pb = chainActive.Tip(); |
| 53 | |
| 54 | if (height >= 0 && height < chainActive.Height()) |
| 55 | pb = chainActive[height]; |
| 56 | |
| 57 | if (pb == nullptr || !pb->nHeight) |
| 58 | return 0; |
| 59 | |
| 60 | // If lookup is -1, then use blocks since last difficulty change. |
| 61 | if (lookup <= 0) |
| 62 | lookup = Params().GetConsensus().nZawyLwmaAveragingWindow; |
| 63 | |
| 64 | // If lookup is larger than chain, then set it to chain length. |
| 65 | if (lookup > pb->nHeight) |
| 66 | lookup = pb->nHeight; |
| 67 | |
| 68 | CBlockIndex *pb0 = pb; |
| 69 | int64_t minTime = pb0->GetBlockTime(); |
| 70 | int64_t maxTime = minTime; |
| 71 | for (int i = 0; i < lookup; i++) { |
| 72 | pb0 = pb0->pprev; |
| 73 | int64_t time = pb0->GetBlockTime(); |
| 74 | minTime = std::min(time, minTime); |
| 75 | maxTime = std::max(time, maxTime); |
| 76 | } |
| 77 | |
| 78 | // In case there's a situation where minTime == maxTime, we don't want a divide by zero exception. |
| 79 | if (minTime == maxTime) |
| 80 | return 0; |
| 81 | |
| 82 | arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork; |
| 83 | int64_t timeDiff = maxTime - minTime; |
| 84 | |
| 85 | return workDiff.getdouble() / timeDiff; |
| 86 | } |
| 87 | |
| 88 | // TODO(h4x3rotab): Implement RPC `getlocalsolps`, and maybe `getnetworksolps` as a alias of `getnetworkhashps`. |
| 89 |
no test coverage detected