Skip to content

Commit

Permalink
Throw error if invalid block height is passed to getnetworkhashps RPC…
Browse files Browse the repository at this point in the history
… endpoint
  • Loading branch information
jlopp committed Oct 2, 2023
1 parent 5bbf735 commit 1fd3715
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
5 changes: 5 additions & 0 deletions test/functional/rpc_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1fd3715

Please sign in to comment.