aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/mining.cpp
diff options
context:
space:
mode:
authorJameson Lopp <jameson.lopp@gmail.com>2023-09-30 15:06:36 -0400
committerJameson Lopp <jameson.lopp@gmail.com>2023-11-07 12:58:42 -0500
commit9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658 (patch)
tree873a5a9d73b62a675ec357bd407117fb7f471e38 /src/rpc/mining.cpp
parent3da69c464f16841a5c8d9fcc9c63238ab807d5ff (diff)
Throw error if invalid parameters passed to getnetworkhashps RPC endpoint
Diffstat (limited to 'src/rpc/mining.cpp')
-rw-r--r--src/rpc/mining.cpp19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index 76170c3201..a1894a3030 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -49,13 +49,22 @@ using node::UpdateTime;
/**
* 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.
+ * or from the last difficulty change if 'lookup' is -1.
+ * If 'height' is -1, compute the estimate from current chain tip.
+ * If 'height' is a valid block height, 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 < -1 || 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];
}
@@ -63,7 +72,7 @@ static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_ch
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.
@@ -97,7 +106,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 previous blocks to calculate estimate from, 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{