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