aboutsummaryrefslogtreecommitdiff
path: root/src/rpc
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-11-28 16:20:20 -0500
committerAndrew Chow <github@achow101.com>2023-11-28 16:26:04 -0500
commit75462b39d2e35ce2ee585453f905445349103e30 (patch)
tree99796abc58b1548c3e22bef3e7b567630edd6555 /src/rpc
parent535424a10b4462a813b9797f3c607b97a0ca9b19 (diff)
parent9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658 (diff)
Merge bitcoin/bitcoin#28554: bugfix: throw an error if an invalid parameter is passed to getnetworkhashps RPC
9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658 Throw error if invalid parameters passed to getnetworkhashps RPC endpoint (Jameson Lopp) Pull request description: When writing some scripts that iterated over many blocks to generate hashrate estimates I realized that my script was going out of range of the current chain tip height but was not encountering any errors. I believe that passing an invalid block height to this function but receiving the hashrate estimate for the chain tip instead should be considered unexpected behavior. ACKs for top commit: Sjors: re-utACK 9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658 kevkevinpal: reACK [9ac114e](https://github.com/bitcoin/bitcoin/pull/28554/commits/9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658) achow101: ACK 9ac114e5cd9d8ade3a1d9f3d76a08ff59a3f1658 Tree-SHA512: eefb465c2dd654fc48267f444e1809597ec5363cdd131ea9ec812458fed1e4bffbbbb0617d74687c9f7bb16274b598d8292f5eeb7953421e5d2a8dc2cc081f2b
Diffstat (limited to 'src/rpc')
-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 1bdc1e1029..3d80656507 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{