From ac68fcca701e0b3b90c6bb81d66bfa38b57f39bf Mon Sep 17 00:00:00 2001 From: tdb3 <106488469+tdb3@users.noreply.github.com> Date: Sun, 6 Oct 2024 14:45:07 -0400 Subject: [PATCH] rpc: disallow undefined verbosity in getorphantxs --- src/rpc/mempool.cpp | 7 ++++--- test/functional/rpc_getorphantxs.py | 13 ++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp index 27a00c5d917..9a6a8d87756 100644 --- a/src/rpc/mempool.cpp +++ b/src/rpc/mempool.cpp @@ -891,7 +891,7 @@ static RPCHelpMan getorphantxs() UniValue ret(UniValue::VARR); - if (verbosity <= 0) { + if (verbosity == 0) { for (auto const& orphan : orphanage) { ret.push_back(orphan.tx->GetHash().ToString()); } @@ -899,13 +899,14 @@ static RPCHelpMan getorphantxs() for (auto const& orphan : orphanage) { ret.push_back(OrphanToJSON(orphan)); } - } else { - // >= 2 + } else if (verbosity == 2) { for (auto const& orphan : orphanage) { UniValue o{OrphanToJSON(orphan)}; o.pushKV("hex", EncodeHexTx(*orphan.tx)); ret.push_back(o); } + } else { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid verbosity value " + ToString(verbosity)); } return ret; diff --git a/test/functional/rpc_getorphantxs.py b/test/functional/rpc_getorphantxs.py index dfa1168b2ec..1e08f15065f 100755 --- a/test/functional/rpc_getorphantxs.py +++ b/test/functional/rpc_getorphantxs.py @@ -7,7 +7,10 @@ from test_framework.mempool_util import tx_in_orphanage from test_framework.messages import msg_tx from test_framework.p2p import P2PInterface -from test_framework.util import assert_equal +from test_framework.util import ( + assert_equal, + assert_raises_rpc_error, +) from test_framework.test_framework import BitcoinTestFramework from test_framework.wallet import MiniWallet @@ -37,13 +40,13 @@ class GetOrphanTxsTest(BitcoinTestFramework): self.log.info("Check that neither parent is in the mempool") assert_equal(node.getmempoolinfo()["size"], 0) - self.log.info("Check that both children are in the orphanage") - orphanage = node.getorphantxs(verbosity=0) self.log.info("Check the size of the orphanage") assert_equal(len(orphanage), 2) - self.log.info("Check that negative verbosity is treated as 0") - assert_equal(orphanage, node.getorphantxs(verbosity=-1)) + self.log.info("Check that undefined verbosity is disallowed") + assert_raises_rpc_error(-8, "Invalid verbosity value -1", node.getorphantxs, verbosity=-1) + assert_raises_rpc_error(-8, "Invalid verbosity value 3", node.getorphantxs, verbosity=3) + self.log.info("Check that both children are in the orphanage") assert tx_in_orphanage(node, tx_child_1["tx"]) assert tx_in_orphanage(node, tx_child_2["tx"])