From 1fd371535648254eafcd3221478471e8c6db759b Mon Sep 17 00:00:00 2001 From: Jameson Lopp Date: Sat, 30 Sep 2023 15:06:36 -0400 Subject: [PATCH] Throw error if invalid block height is passed to getnetworkhashps RPC endpoint --- src/rpc/mining.cpp | 6 +++++- test/functional/rpc_blockchain.py | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 76170c32018dda..389aab089b5aa9 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -53,9 +53,13 @@ using node::UpdateTime; * If 'height' is nonnegative, compute the estimate at the time when a given block was found. */ static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_chain) { + if (height > active_chain.Height()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Block does not exist at specified height"); + } + const CBlockIndex* pb = active_chain.Tip(); - if (height >= 0 && height < active_chain.Height()) { + if (height >= 0) { pb = active_chain[height]; } diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py index 18a0a0c6ccedc7..265389622eea14 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, + "Block does not exist at specified height", + lambda: self.nodes[0].getnetworkhashps(100, self.nodes[0].getblockcount() + 1), + ) # This should be 2 hashes every 10 minutes or 1/300 assert abs(hashes_per_second * 300 - 1) < 0.0001