* 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. */
| 35 | * If 'height' is nonnegative, compute the estimate at the time when a given block was found. |
| 36 | */ |
| 37 | UniValue 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 | |
| 74 | UniValue getnetworkhashps(const UniValue& params, bool fHelp) |
| 75 | { |
no test coverage detected