mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
Merge #16240: JSONRPCRequest-aware RPCHelpMan
b6fb617aaarpc: switch to using RPCHelpMan.Check() (Karl-Johan Alm)c7a9fc234fMake the RPCHelpMan aware of JSONRPCRequest and add Check() helper (Karl-Johan Alm)5c5e32bbe3rpc: migrate JSONRPCRequest functionality into request.cpp (Karl-Johan Alm)0ab8ba1ac6rpc: fix RPC help requirements for getblocktemplate (Karl-Johan Alm) Pull request description: Every single RPC call has a helper-section at the start, which throws a help string if the user asks for help or if the user provided too few/many arguments. ```C++ const RPCHelpMan help{...}; if (request.fHelp || !help.IsValidNumArgs(request.params.size())) { throw std::runtime_error(help.ToString()); } ``` or (older version) ```C++ if (request.fHelp || request.params.size() < min || request.params.size() > max) throw std::runtime_error( RPCHelpMan{...}.ToString() ); ``` It seems like an obvious improvement, and less copy-pasting, to make `RPCHelpMan` aware of `JSONRPCRequest`, and to let it handle the checks instead. Both of the above become ```C++ RPCHelpMan{...}.Check(request); ``` which means we save roughly 3 lines per RPC command, and the `RPCHelpMan` instance is never referenced afterwards, so the approach is a tiny fraction cleaner. This is a complete update, sans a few special case locations that had special rules. 623 lines turn into 284 (which includes the addition to `RPCHelpMan`). ACKs for top commit: laanwj: code rview and lightly tested ACKb6fb617aaaMarcoFalke: ACKb6fb617aaa, looked at the diff, verified move-only where applicable Tree-SHA512: eb73f47f812512905b852e313281d1c8df803db40a6188aa39d5a7586631664db6764491152a8a96769946c796dc56d38c6e3a66ddd06ba3fb9d20050e6274e1
This commit is contained in:
@@ -78,8 +78,6 @@ static UniValue GetNetworkHashPS(int lookup, int height) {
|
||||
|
||||
static UniValue getnetworkhashps(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() > 2)
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"getnetworkhashps",
|
||||
"\nReturns the estimated network hashes per second based on the last n blocks.\n"
|
||||
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
|
||||
@@ -95,7 +93,7 @@ static UniValue getnetworkhashps(const JSONRPCRequest& request)
|
||||
HelpExampleCli("getnetworkhashps", "")
|
||||
+ HelpExampleRpc("getnetworkhashps", "")
|
||||
},
|
||||
}.ToString());
|
||||
}.Check(request);
|
||||
|
||||
LOCK(cs_main);
|
||||
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
|
||||
@@ -144,8 +142,6 @@ static UniValue generateBlocks(const CScript& coinbase_script, int nGenerate, ui
|
||||
|
||||
static UniValue generatetoaddress(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"generatetoaddress",
|
||||
"\nMine blocks immediately to a specified address (before the RPC call returns)\n",
|
||||
{
|
||||
@@ -162,7 +158,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
|
||||
+ "If you are running the bitcoin core wallet, you can get a new address to send the newly generated bitcoin to with:\n"
|
||||
+ HelpExampleCli("getnewaddress", "")
|
||||
},
|
||||
}.ToString());
|
||||
}.Check(request);
|
||||
|
||||
int nGenerate = request.params[0].get_int();
|
||||
uint64_t nMaxTries = 1000000;
|
||||
@@ -182,8 +178,6 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
|
||||
|
||||
static UniValue getmininginfo(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() != 0) {
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"getmininginfo",
|
||||
"\nReturns a json object containing mining-related information.",
|
||||
{},
|
||||
@@ -203,8 +197,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
|
||||
HelpExampleCli("getmininginfo", "")
|
||||
+ HelpExampleRpc("getmininginfo", "")
|
||||
},
|
||||
}.ToString());
|
||||
}
|
||||
}.Check(request);
|
||||
|
||||
LOCK(cs_main);
|
||||
|
||||
@@ -224,8 +217,6 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
|
||||
// NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
|
||||
static UniValue prioritisetransaction(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() != 3)
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"prioritisetransaction",
|
||||
"Accepts the transaction into mined blocks at a higher (or lower) priority\n",
|
||||
{
|
||||
@@ -244,7 +235,7 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
|
||||
HelpExampleCli("prioritisetransaction", "\"txid\" 0.0 10000")
|
||||
+ HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000")
|
||||
},
|
||||
}.ToString());
|
||||
}.Check(request);
|
||||
|
||||
LOCK(cs_main);
|
||||
|
||||
@@ -290,8 +281,6 @@ static std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
|
||||
|
||||
static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() > 1)
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"getblocktemplate",
|
||||
"\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
|
||||
"It returns data needed to construct a block to work on.\n"
|
||||
@@ -301,7 +290,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||
" https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes\n"
|
||||
" https://github.com/bitcoin/bips/blob/master/bip-0145.mediawiki\n",
|
||||
{
|
||||
{"template_request", RPCArg::Type::OBJ, RPCArg::Optional::NO, "A json object in the following spec",
|
||||
{"template_request", RPCArg::Type::OBJ, "{}", "A json object in the following spec",
|
||||
{
|
||||
{"mode", RPCArg::Type::STR, /* treat as named arg */ RPCArg::Optional::OMITTED_NAMED_ARG, "This must be set to \"template\", \"proposal\" (see BIP 23), or omitted"},
|
||||
{"capabilities", RPCArg::Type::ARR, /* treat as named arg */ RPCArg::Optional::OMITTED_NAMED_ARG, "A list of strings",
|
||||
@@ -366,7 +355,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||
HelpExampleCli("getblocktemplate", "{\"rules\": [\"segwit\"]}")
|
||||
+ HelpExampleRpc("getblocktemplate", "{\"rules\": [\"segwit\"]}")
|
||||
},
|
||||
}.ToString());
|
||||
}.Check(request);
|
||||
|
||||
LOCK(cs_main);
|
||||
|
||||
@@ -696,8 +685,6 @@ protected:
|
||||
static UniValue submitblock(const JSONRPCRequest& request)
|
||||
{
|
||||
// We allow 2 arguments for compliance with BIP22. Argument 2 is ignored.
|
||||
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"submitblock",
|
||||
"\nAttempts to submit new block to network.\n"
|
||||
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n",
|
||||
@@ -710,8 +697,7 @@ static UniValue submitblock(const JSONRPCRequest& request)
|
||||
HelpExampleCli("submitblock", "\"mydata\"")
|
||||
+ HelpExampleRpc("submitblock", "\"mydata\"")
|
||||
},
|
||||
}.ToString());
|
||||
}
|
||||
}.Check(request);
|
||||
|
||||
std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>();
|
||||
CBlock& block = *blockptr;
|
||||
@@ -761,8 +747,6 @@ static UniValue submitblock(const JSONRPCRequest& request)
|
||||
|
||||
static UniValue submitheader(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() != 1) {
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"submitheader",
|
||||
"\nDecode the given hexdata as a header and submit it as a candidate chain tip if valid."
|
||||
"\nThrows when the header is invalid.\n",
|
||||
@@ -776,8 +760,7 @@ static UniValue submitheader(const JSONRPCRequest& request)
|
||||
HelpExampleCli("submitheader", "\"aabbcc\"") +
|
||||
HelpExampleRpc("submitheader", "\"aabbcc\"")
|
||||
},
|
||||
}.ToString());
|
||||
}
|
||||
}.Check(request);
|
||||
|
||||
CBlockHeader h;
|
||||
if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
|
||||
@@ -801,8 +784,6 @@ static UniValue submitheader(const JSONRPCRequest& request)
|
||||
|
||||
static UniValue estimatesmartfee(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"estimatesmartfee",
|
||||
"\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
|
||||
"confirmation within conf_target blocks if possible and return the number of blocks\n"
|
||||
@@ -835,7 +816,7 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
|
||||
RPCExamples{
|
||||
HelpExampleCli("estimatesmartfee", "6")
|
||||
},
|
||||
}.ToString());
|
||||
}.Check(request);
|
||||
|
||||
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
|
||||
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
|
||||
@@ -866,8 +847,6 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
|
||||
|
||||
static UniValue estimaterawfee(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"estimaterawfee",
|
||||
"\nWARNING: This interface is unstable and may disappear or change!\n"
|
||||
"\nWARNING: This is an advanced API call that is tightly coupled to the specific\n"
|
||||
@@ -908,7 +887,7 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
|
||||
RPCExamples{
|
||||
HelpExampleCli("estimaterawfee", "6 0.9")
|
||||
},
|
||||
}.ToString());
|
||||
}.Check(request);
|
||||
|
||||
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
|
||||
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
|
||||
|
||||
Reference in New Issue
Block a user