diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 76170c32018dda..419c89319d747d 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -59,6 +59,10 @@ static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_ch pb = active_chain[height]; } + if (height > active_chain.Height()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Block does not exist at specified height"); + } + if (pb == nullptr || !pb->nHeight) return 0; diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py index 18a0a0c6ccedc7..2871a9faaf79be 100755 --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -448,6 +448,11 @@ def _test_getnetworkhashps(self): """).strip(), lambda: self.nodes[0].getnetworkhashps("a", []), ) + assert_raises_rpc_error( + -8, + textwrap.dedent("Block does not exist at specified height").strip(), + lambda: self.nodes[0].getnetworkhashps(100, 99999999), + ) # This should be 2 hashes every 10 minutes or 1/300 assert abs(hashes_per_second * 300 - 1) < 0.0001