Skip to content

Commit

Permalink
Throw error if invalid parameters passed to getnetworkhashps RPC endp…
Browse files Browse the repository at this point in the history
…oint
  • Loading branch information
jlopp committed Oct 3, 2023
1 parent 5bbf735 commit 9c6ce7a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,25 @@ 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 (lookup < -1 || lookup == 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid nblocks. Must be a positive number or -1.");
}

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];
}

if (pb == nullptr || !pb->nHeight)
return 0;

// If lookup is -1, then use blocks since last difficulty change.
if (lookup <= 0)
if (lookup == -1)
lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;

// If lookup is larger than chain, then set it to chain length.
Expand Down Expand Up @@ -97,7 +105,7 @@ static RPCHelpMan getnetworkhashps()
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n",
{
{"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of blocks, or -1 for blocks since last difficulty change."},
{"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of trailing blocks, or -1 for blocks since last difficulty change."},
{"height", RPCArg::Type::NUM, RPCArg::Default{-1}, "To estimate at the time of the given height."},
},
RPCResult{
Expand Down
21 changes: 20 additions & 1 deletion test/functional/rpc_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ def _test_getdifficulty(self):

def _test_getnetworkhashps(self):
self.log.info("Test getnetworkhashps")
hashes_per_second = self.nodes[0].getnetworkhashps()
assert_raises_rpc_error(
-3,
textwrap.dedent("""
Expand All @@ -448,9 +447,29 @@ 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),
)
assert_raises_rpc_error(
-8,
"Invalid nblocks. Must be a positive number or -1.",
lambda: self.nodes[0].getnetworkhashps(-100),
)
assert_raises_rpc_error(
-8,
"Invalid nblocks. Must be a positive number or -1.",
lambda: self.nodes[0].getnetworkhashps(0),
)
# This should be 2 hashes every 10 minutes or 1/300
hashes_per_second = self.nodes[0].getnetworkhashps()
assert abs(hashes_per_second * 300 - 1) < 0.0001

# Ensure long lookups get truncated to chain length
hashes_per_second = self.nodes[0].getnetworkhashps(self.nodes[0].getblockcount() + 1000)
assert hashes_per_second > 0.003

def _test_stopatheight(self):
self.log.info("Test stopping at height")
assert_equal(self.nodes[0].getblockcount(), HEIGHT)
Expand Down

0 comments on commit 9c6ce7a

Please sign in to comment.