Skip to content

Commit b6fb617

Browse files
committed
rpc: switch to using RPCHelpMan.Check()
1 parent c7a9fc2 commit b6fb617

File tree

9 files changed

+154
-498
lines changed

9 files changed

+154
-498
lines changed

src/rpc/blockchain.cpp

Lines changed: 32 additions & 100 deletions
Large diffs are not rendered by default.

src/rpc/mining.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ static UniValue GetNetworkHashPS(int lookup, int height) {
7878

7979
static UniValue getnetworkhashps(const JSONRPCRequest& request)
8080
{
81-
if (request.fHelp || request.params.size() > 2)
82-
throw std::runtime_error(
8381
RPCHelpMan{"getnetworkhashps",
8482
"\nReturns the estimated network hashes per second based on the last n blocks.\n"
8583
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
@@ -95,7 +93,7 @@ static UniValue getnetworkhashps(const JSONRPCRequest& request)
9593
HelpExampleCli("getnetworkhashps", "")
9694
+ HelpExampleRpc("getnetworkhashps", "")
9795
},
98-
}.ToString());
96+
}.Check(request);
9997

10098
LOCK(cs_main);
10199
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
@@ -145,8 +143,6 @@ static UniValue generateBlocks(const CScript& coinbase_script, int nGenerate, ui
145143

146144
static UniValue generatetoaddress(const JSONRPCRequest& request)
147145
{
148-
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
149-
throw std::runtime_error(
150146
RPCHelpMan{"generatetoaddress",
151147
"\nMine blocks immediately to a specified address (before the RPC call returns)\n",
152148
{
@@ -163,7 +159,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
163159
+ "If you are running the bitcoin core wallet, you can get a new address to send the newly generated bitcoin to with:\n"
164160
+ HelpExampleCli("getnewaddress", "")
165161
},
166-
}.ToString());
162+
}.Check(request);
167163

168164
int nGenerate = request.params[0].get_int();
169165
uint64_t nMaxTries = 1000000;
@@ -183,8 +179,6 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
183179

184180
static UniValue getmininginfo(const JSONRPCRequest& request)
185181
{
186-
if (request.fHelp || request.params.size() != 0) {
187-
throw std::runtime_error(
188182
RPCHelpMan{"getmininginfo",
189183
"\nReturns a json object containing mining-related information.",
190184
{},
@@ -204,8 +198,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
204198
HelpExampleCli("getmininginfo", "")
205199
+ HelpExampleRpc("getmininginfo", "")
206200
},
207-
}.ToString());
208-
}
201+
}.Check(request);
209202

210203
LOCK(cs_main);
211204

@@ -225,8 +218,6 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
225218
// NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
226219
static UniValue prioritisetransaction(const JSONRPCRequest& request)
227220
{
228-
if (request.fHelp || request.params.size() != 3)
229-
throw std::runtime_error(
230221
RPCHelpMan{"prioritisetransaction",
231222
"Accepts the transaction into mined blocks at a higher (or lower) priority\n",
232223
{
@@ -245,7 +236,7 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
245236
HelpExampleCli("prioritisetransaction", "\"txid\" 0.0 10000")
246237
+ HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000")
247238
},
248-
}.ToString());
239+
}.Check(request);
249240

250241
LOCK(cs_main);
251242

@@ -291,8 +282,6 @@ static std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
291282

292283
static UniValue getblocktemplate(const JSONRPCRequest& request)
293284
{
294-
if (request.fHelp || request.params.size() > 1)
295-
throw std::runtime_error(
296285
RPCHelpMan{"getblocktemplate",
297286
"\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
298287
"It returns data needed to construct a block to work on.\n"
@@ -367,7 +356,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
367356
HelpExampleCli("getblocktemplate", "{\"rules\": [\"segwit\"]}")
368357
+ HelpExampleRpc("getblocktemplate", "{\"rules\": [\"segwit\"]}")
369358
},
370-
}.ToString());
359+
}.Check(request);
371360

372361
LOCK(cs_main);
373362

@@ -697,8 +686,6 @@ class submitblock_StateCatcher : public CValidationInterface
697686
static UniValue submitblock(const JSONRPCRequest& request)
698687
{
699688
// We allow 2 arguments for compliance with BIP22. Argument 2 is ignored.
700-
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
701-
throw std::runtime_error(
702689
RPCHelpMan{"submitblock",
703690
"\nAttempts to submit new block to network.\n"
704691
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n",
@@ -711,8 +698,7 @@ static UniValue submitblock(const JSONRPCRequest& request)
711698
HelpExampleCli("submitblock", "\"mydata\"")
712699
+ HelpExampleRpc("submitblock", "\"mydata\"")
713700
},
714-
}.ToString());
715-
}
701+
}.Check(request);
716702

717703
std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>();
718704
CBlock& block = *blockptr;
@@ -762,8 +748,6 @@ static UniValue submitblock(const JSONRPCRequest& request)
762748

763749
static UniValue submitheader(const JSONRPCRequest& request)
764750
{
765-
if (request.fHelp || request.params.size() != 1) {
766-
throw std::runtime_error(
767751
RPCHelpMan{"submitheader",
768752
"\nDecode the given hexdata as a header and submit it as a candidate chain tip if valid."
769753
"\nThrows when the header is invalid.\n",
@@ -777,8 +761,7 @@ static UniValue submitheader(const JSONRPCRequest& request)
777761
HelpExampleCli("submitheader", "\"aabbcc\"") +
778762
HelpExampleRpc("submitheader", "\"aabbcc\"")
779763
},
780-
}.ToString());
781-
}
764+
}.Check(request);
782765

783766
CBlockHeader h;
784767
if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
@@ -802,8 +785,6 @@ static UniValue submitheader(const JSONRPCRequest& request)
802785

803786
static UniValue estimatesmartfee(const JSONRPCRequest& request)
804787
{
805-
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
806-
throw std::runtime_error(
807788
RPCHelpMan{"estimatesmartfee",
808789
"\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
809790
"confirmation within conf_target blocks if possible and return the number of blocks\n"
@@ -836,7 +817,7 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
836817
RPCExamples{
837818
HelpExampleCli("estimatesmartfee", "6")
838819
},
839-
}.ToString());
820+
}.Check(request);
840821

841822
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
842823
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
@@ -867,8 +848,6 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
867848

868849
static UniValue estimaterawfee(const JSONRPCRequest& request)
869850
{
870-
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
871-
throw std::runtime_error(
872851
RPCHelpMan{"estimaterawfee",
873852
"\nWARNING: This interface is unstable and may disappear or change!\n"
874853
"\nWARNING: This is an advanced API call that is tightly coupled to the specific\n"
@@ -909,7 +888,7 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
909888
RPCExamples{
910889
HelpExampleCli("estimaterawfee", "6 0.9")
911890
},
912-
}.ToString());
891+
}.Check(request);
913892

914893
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
915894
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);

src/rpc/misc.cpp

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
static UniValue validateaddress(const JSONRPCRequest& request)
2727
{
28-
if (request.fHelp || request.params.size() != 1)
29-
throw std::runtime_error(
3028
RPCHelpMan{"validateaddress",
3129
"\nReturn information about the given bitcoin address.\n",
3230
{
@@ -47,7 +45,7 @@ static UniValue validateaddress(const JSONRPCRequest& request)
4745
HelpExampleCli("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
4846
+ HelpExampleRpc("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
4947
},
50-
}.ToString());
48+
}.Check(request);
5149

5250
CTxDestination dest = DecodeDestination(request.params[0].get_str());
5351
bool isValid = IsValidDestination(dest);
@@ -70,9 +68,6 @@ static UniValue validateaddress(const JSONRPCRequest& request)
7068

7169
static UniValue createmultisig(const JSONRPCRequest& request)
7270
{
73-
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
74-
{
75-
std::string msg =
7671
RPCHelpMan{"createmultisig",
7772
"\nCreates a multi-signature address with n signature of m keys required.\n"
7873
"It returns a json object with the address and redeemScript.\n",
@@ -96,9 +91,7 @@ static UniValue createmultisig(const JSONRPCRequest& request)
9691
"\nAs a JSON-RPC call\n"
9792
+ HelpExampleRpc("createmultisig", "2, \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"")
9893
},
99-
}.ToString();
100-
throw std::runtime_error(msg);
101-
}
94+
}.Check(request);
10295

10396
int required = request.params[0].get_int();
10497

@@ -135,8 +128,6 @@ static UniValue createmultisig(const JSONRPCRequest& request)
135128

136129
UniValue getdescriptorinfo(const JSONRPCRequest& request)
137130
{
138-
if (request.fHelp || request.params.size() != 1) {
139-
throw std::runtime_error(
140131
RPCHelpMan{"getdescriptorinfo",
141132
{"\nAnalyses a descriptor.\n"},
142133
{
@@ -153,9 +144,7 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
153144
RPCExamples{
154145
"Analyse a descriptor\n" +
155146
HelpExampleCli("getdescriptorinfo", "\"wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)\"")
156-
}}.ToString()
157-
);
158-
}
147+
}}.Check(request);
159148

160149
RPCTypeCheck(request.params, {UniValue::VSTR});
161150

@@ -175,8 +164,6 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
175164

176165
UniValue deriveaddresses(const JSONRPCRequest& request)
177166
{
178-
if (request.fHelp || request.params.empty() || request.params.size() > 2) {
179-
throw std::runtime_error(
180167
RPCHelpMan{"deriveaddresses",
181168
{"\nDerives one or more addresses corresponding to an output descriptor.\n"
182169
"Examples of output descriptors are:\n"
@@ -197,9 +184,7 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
197184
RPCExamples{
198185
"First three native segwit receive addresses\n" +
199186
HelpExampleCli("deriveaddresses", "\"wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu\" \"[0,2]\"")
200-
}}.ToString()
201-
);
202-
}
187+
}}.Check(request);
203188

204189
RPCTypeCheck(request.params, {UniValue::VSTR, UniValueType()}); // Range argument is checked later
205190
const std::string desc_str = request.params[0].get_str();
@@ -254,8 +239,6 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
254239

255240
static UniValue verifymessage(const JSONRPCRequest& request)
256241
{
257-
if (request.fHelp || request.params.size() != 3)
258-
throw std::runtime_error(
259242
RPCHelpMan{"verifymessage",
260243
"\nVerify a signed message\n",
261244
{
@@ -276,7 +259,7 @@ static UniValue verifymessage(const JSONRPCRequest& request)
276259
"\nAs a JSON-RPC call\n"
277260
+ HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
278261
},
279-
}.ToString());
262+
}.Check(request);
280263

281264
LOCK(cs_main);
282265

@@ -313,8 +296,6 @@ static UniValue verifymessage(const JSONRPCRequest& request)
313296

314297
static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
315298
{
316-
if (request.fHelp || request.params.size() != 2)
317-
throw std::runtime_error(
318299
RPCHelpMan{"signmessagewithprivkey",
319300
"\nSign a message with the private key of an address\n",
320301
{
@@ -332,7 +313,7 @@ static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
332313
"\nAs a JSON-RPC call\n"
333314
+ HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
334315
},
335-
}.ToString());
316+
}.Check(request);
336317

337318
std::string strPrivkey = request.params[0].get_str();
338319
std::string strMessage = request.params[1].get_str();
@@ -355,8 +336,6 @@ static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
355336

356337
static UniValue setmocktime(const JSONRPCRequest& request)
357338
{
358-
if (request.fHelp || request.params.size() != 1)
359-
throw std::runtime_error(
360339
RPCHelpMan{"setmocktime",
361340
"\nSet the local time to given timestamp (-regtest only)\n",
362341
{
@@ -365,8 +344,7 @@ static UniValue setmocktime(const JSONRPCRequest& request)
365344
},
366345
RPCResults{},
367346
RPCExamples{""},
368-
}.ToString()
369-
);
347+
}.Check(request);
370348

371349
if (!Params().MineBlocksOnDemand())
372350
throw std::runtime_error("setmocktime for regression testing (-regtest mode) only");
@@ -421,8 +399,6 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
421399
/* Please, avoid using the word "pool" here in the RPC interface or help,
422400
* as users will undoubtedly confuse it with the other "memory pool"
423401
*/
424-
if (request.fHelp || request.params.size() > 1)
425-
throw std::runtime_error(
426402
RPCHelpMan{"getmemoryinfo",
427403
"Returns an object containing information about memory usage.\n",
428404
{
@@ -451,7 +427,7 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
451427
HelpExampleCli("getmemoryinfo", "")
452428
+ HelpExampleRpc("getmemoryinfo", "")
453429
},
454-
}.ToString());
430+
}.Check(request);
455431

456432
std::string mode = request.params[0].isNull() ? "stats" : request.params[0].get_str();
457433
if (mode == "stats") {
@@ -489,8 +465,6 @@ static void EnableOrDisableLogCategories(UniValue cats, bool enable) {
489465

490466
UniValue logging(const JSONRPCRequest& request)
491467
{
492-
if (request.fHelp || request.params.size() > 2) {
493-
throw std::runtime_error(
494468
RPCHelpMan{"logging",
495469
"Gets and sets the logging configuration.\n"
496470
"When called without an argument, returns the list of categories with status that are currently being debug logged or not.\n"
@@ -522,8 +496,7 @@ UniValue logging(const JSONRPCRequest& request)
522496
HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"")
523497
+ HelpExampleRpc("logging", "[\"all\"], [\"libevent\"]")
524498
},
525-
}.ToString());
526-
}
499+
}.Check(request);
527500

528501
uint32_t original_log_categories = LogInstance().GetCategoryMask();
529502
if (request.params[0].isArray()) {

0 commit comments

Comments
 (0)