diff --git a/doc/release-notes.md b/doc/release-notes.md --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -9,4 +9,5 @@ (previously was `Bitcoin Signed Message:`). - A new `getindexinfo` RPC returns the actively running indices of the node, including their current sync status and height. It also accepts an `index_name` - to specify returning only the status of that index. \ No newline at end of file + to specify returning only the status of that index. + - The `getnetworkhashps` RPC no longer accepts a negative number of blocks. diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -42,10 +42,10 @@ /** * 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. + * If 'height' is nonnegative, compute the estimate at the time when a given + * block was found. */ -static UniValue GetNetworkHashPS(int lookup, int height) { +static UniValue GetNetworkHashPS(unsigned int lookup, int height) { CBlockIndex *pb = ::ChainActive().Tip(); if (height >= 0 && height < ::ChainActive().Height()) { @@ -56,22 +56,16 @@ return 0; } - // If lookup is -1, then use blocks since last difficulty change. - if (lookup <= 0) { - lookup = pb->nHeight % - Params().GetConsensus().DifficultyAdjustmentInterval() + - 1; - } - + CHECK_NONFATAL(pb->nHeight > 0); // If lookup is larger than chain, then set it to chain length. - if (lookup > pb->nHeight) { + if (lookup > (unsigned int)pb->nHeight) { lookup = pb->nHeight; } CBlockIndex *pb0 = pb; int64_t minTime = pb0->GetBlockTime(); int64_t maxTime = minTime; - for (int i = 0; i < lookup; i++) { + for (size_t i = 0; i < lookup; i++) { pb0 = pb0->pprev; int64_t time = pb0->GetBlockTime(); minTime = std::min(time, minTime); @@ -96,14 +90,12 @@ "getnetworkhashps", "Returns the estimated network hashes per second based on the last n " "blocks.\n" - "Pass in [blocks] to override # of blocks, -1 specifies since last " - "difficulty change.\n" + "Pass in [nblocks] to override # of blocks.\n" "Pass in [height] to estimate the network speed at the time when a " "certain block was found.\n", { {"nblocks", RPCArg::Type::NUM, /* default */ "120", - "The number of blocks, or -1 for blocks since last difficulty " - "change."}, + "The number of blocks."}, {"height", RPCArg::Type::NUM, /* default */ "-1", "To estimate at the time of the given height."}, }, @@ -113,10 +105,17 @@ } .Check(request); + int nblocks = + !request.params[0].isNull() ? request.params[0].get_int() : 120; + if (nblocks <= 0) { + throw JSONRPCError(RPC_INVALID_PARAMETER, + "Number of blocks cannot be less than 1"); + } + LOCK(cs_main); - return GetNetworkHashPS( - !request.params[0].isNull() ? request.params[0].get_int() : 120, - !request.params[1].isNull() ? request.params[1].get_int() : -1); + return GetNetworkHashPS(nblocks, !request.params[1].isNull() + ? request.params[1].get_int() + : -1); } static bool GenerateBlock(const Config &config, ChainstateManager &chainman, diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -337,6 +337,12 @@ # This should be 2 hashes every 10 minutes or 1/300 assert abs(hashes_per_second * 300 - 1) < 0.0001 + # Passing nblocks = -1 used to mean "since last difficulty change". + # This is now meaningless, as the difficulty adjusts for every block. + assert_raises_rpc_error( + -8, "Number of blocks cannot be less than 1", + self.nodes[0].getnetworkhashps, -1) + def _test_stopatheight(self): assert_equal(self.nodes[0].getblockcount(), 200) self.nodes[0].generatetoaddress(