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

Function GetNetworkHashPS

src/rpc/mining.cpp:96–140  ·  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 -1. * If 'height' is -1, compute the estimate from current chain tip. * If 'height' is a valid block height, compute the estimate at the time when a given block was found. */

Source from the content-addressed store, hash-verified

94 * If 'height' is a valid block height, compute the estimate at the time when a given block was found.
95 */
96static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_chain) {
97 if (lookup < -1 || lookup == 0) {
98 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid nblocks. Must be a positive number or -1.");
99 }
100
101 if (height < -1 || height > active_chain.Height()) {
102 throw JSONRPCError(RPC_INVALID_PARAMETER, "Block does not exist at specified height");
103 }
104
105 const CBlockIndex* pb = active_chain.Tip();
106
107 if (height >= 0) {
108 pb = active_chain[height];
109 }
110
111 if (pb == nullptr || !pb->nHeight)
112 return 0;
113
114 // If lookup is -1, then use blocks since last difficulty change.
115 if (lookup == -1)
116 lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
117
118 // If lookup is larger than chain, then set it to chain length.
119 if (lookup > pb->nHeight)
120 lookup = pb->nHeight;
121
122 const CBlockIndex* pb0 = pb;
123 int64_t minTime = pb0->GetBlockTime();
124 int64_t maxTime = minTime;
125 for (int i = 0; i < lookup; i++) {
126 pb0 = pb0->pprev;
127 int64_t time = pb0->GetBlockTime();
128 minTime = std::min(time, minTime);
129 maxTime = std::max(time, maxTime);
130 }
131
132 // In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
133 if (minTime == maxTime)
134 return 0;
135
136 arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork;
137 int64_t timeDiff = maxTime - minTime;
138
139 return workDiff.getdouble() / timeDiff;
140}
141
142static RPCMethod getnetworkhashps()
143{

Callers 1

getnetworkhashpsFunction · 0.85

Calls 7

JSONRPCErrorFunction · 0.85
HeightMethod · 0.80
getdoubleMethod · 0.80
ParamsClass · 0.50
TipMethod · 0.45
GetBlockTimeMethod · 0.45

Tested by

no test coverage detected