Skip to content

Commit 7a9116f

Browse files
committed
rpc: convert settxfee to RPCHelpMan, fix below-minimum error path
Convert settxfee to the RPCHelpMan pattern landed in gridcoin-community#2923. As a side effect, fix the below-minimum-amount UX bug: today the help condition is `fHelp || params.size() < 1 || params.size() > 1 || AmountFromValue(params[0]) < nMinFee`, so a below-min amount throws the help text wrapped as RPC_MISC_ERROR (-1) — useless to the caller. Hoist the AmountFromValue and minimum-fee check out of the help condition; below-min now throws JSONRPCError(RPC_INVALID_PARAMETER, "txfee X below minimum Y") with both values formatted, so the caller gets actionable feedback. The 0-arg path is unchanged in behavior: IsValidNumArgs(0) returns false and the help is thrown, just as the short-circuit `params.size() < 1` produced before. Add a per-command help-render test in rpchelpman_tests.cpp asserting that fHelp=true throws a runtime_error whose message contains the expected RPCHelpMan markers. Refs: gridcoin-community#2922
1 parent c98f5d0 commit 7a9116f

2 files changed

Lines changed: 48 additions & 13 deletions

File tree

src/rpc/blockchain.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "gridcoin/scraper/scraper_registry.h"
1212
#include "gridcoin/sidestake.h"
1313
#include "node/blockstorage.h"
14+
#include <rpc/util.h>
1415
#include <util/string.h>
1516
#include "gridcoin/mrc.h"
1617
#include "gridcoin/support/block_finder.h"
@@ -839,23 +840,34 @@ UniValue getdifficulty(const UniValue& params, bool fHelp)
839840

840841
UniValue settxfee(const UniValue& params, bool fHelp)
841842
{
843+
static const RPCHelpMan help{
844+
"settxfee",
845+
"Set the transaction fee per kB (rounded to the nearest 0.00000001 GRC). "
846+
"Overwrites the paytxfee parameter.",
847+
{
848+
{"amount", RPCArg::Type::AMOUNT, RPCArg::Optional::NO,
849+
"The transaction fee in GRC/kB."},
850+
},
851+
RPCResult{RPCResult::Type::BOOL, "", "Returns true if successful."},
852+
RPCExamples{
853+
HelpExampleCli("settxfee", "0.00001") +
854+
HelpExampleRpc("settxfee", "0.00001")},
855+
};
856+
if (fHelp || !help.IsValidNumArgs(params.size()))
857+
throw runtime_error(help.ToString());
858+
842859
LOCK(cs_main);
843860

844861
CTransaction txDummy;
862+
const CAmount nMinFee = GetBaseFee(txDummy, GMF_SEND);
863+
const CAmount amount = AmountFromValue(params[0]);
864+
if (amount < nMinFee) {
865+
throw JSONRPCError(RPC_INVALID_PARAMETER,
866+
strprintf("txfee %s below minimum %s",
867+
FormatMoney(amount), FormatMoney(nMinFee)));
868+
}
845869

846-
// Min Fee
847-
CAmount nMinFee = GetBaseFee(txDummy, GMF_SEND);
848-
849-
if (fHelp || params.size() < 1 || params.size() > 1 || AmountFromValue(params[0]) < nMinFee)
850-
throw runtime_error(
851-
"settxfee <amount>\n"
852-
"\n"
853-
"<amount> is a real and is rounded to the nearest 0.00000001\n"
854-
"\n"
855-
"Sets the txfee for transactions\n");
856-
857-
nTransactionFee = AmountFromValue(params[0]);
858-
870+
nTransactionFee = amount;
859871
return true;
860872
}
861873

src/test/rpchelpman_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
#include <stdexcept>
1313
#include <string>
1414

15+
// Forward declarations of the Tier 2 commands under test. These must live in
16+
// the global namespace; if placed inside BOOST_AUTO_TEST_SUITE(...) they get
17+
// captured into the suite's namespace and fail to link against the
18+
// definitions in src/rpc/blockchain.cpp, src/rpc/net.cpp, src/wallet/rpcwallet.cpp.
19+
// Each command's converted body throws for fHelp=true before touching any
20+
// globals (vNodes, g_banman, pwalletMain, mapBlockIndex, locks), so calling
21+
// these with fHelp=true and an empty params array is safe in unit tests.
22+
UniValue settxfee(const UniValue& params, bool fHelp);
23+
1524
BOOST_AUTO_TEST_SUITE(rpchelpman_tests)
1625

1726
// Helper: build a minimal RPCHelpMan with one required string arg and one result.
@@ -319,4 +328,18 @@ BOOST_AUTO_TEST_CASE(tier1_throw_pattern)
319328
}
320329
}
321330

331+
BOOST_AUTO_TEST_CASE(settxfee_help_renders)
332+
{
333+
const UniValue params(UniValue::VARR);
334+
try {
335+
settxfee(params, /*fHelp=*/true);
336+
BOOST_FAIL("expected runtime_error");
337+
} catch (const std::runtime_error& e) {
338+
const std::string what{e.what()};
339+
BOOST_CHECK(what.find("settxfee") != std::string::npos);
340+
BOOST_CHECK(what.find("amount") != std::string::npos);
341+
BOOST_CHECK(what.find("Examples:") != std::string::npos);
342+
}
343+
}
344+
322345
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)