Skip to content

Commit fa77de2

Browse files
author
MarcoFalke
committed
rpc: Assert that RPCArg names are equal to CRPCCommand ones (misc)
1 parent fa50bdc commit fa77de2

File tree

1 file changed

+73
-47
lines changed

1 file changed

+73
-47
lines changed

src/rpc/misc.cpp

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
#include <univalue.h>
2929

30-
static UniValue validateaddress(const JSONRPCRequest& request)
30+
static RPCHelpMan validateaddress()
3131
{
32-
RPCHelpMan{"validateaddress",
32+
return RPCHelpMan{"validateaddress",
3333
"\nReturn information about the given bitcoin address.\n",
3434
{
3535
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to validate"},
@@ -50,8 +50,8 @@ static UniValue validateaddress(const JSONRPCRequest& request)
5050
HelpExampleCli("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
5151
HelpExampleRpc("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"")
5252
},
53-
}.Check(request);
54-
53+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
54+
{
5555
CTxDestination dest = DecodeDestination(request.params[0].get_str());
5656
bool isValid = IsValidDestination(dest);
5757

@@ -69,11 +69,13 @@ static UniValue validateaddress(const JSONRPCRequest& request)
6969
ret.pushKVs(detail);
7070
}
7171
return ret;
72+
},
73+
};
7274
}
7375

74-
static UniValue createmultisig(const JSONRPCRequest& request)
76+
static RPCHelpMan createmultisig()
7577
{
76-
RPCHelpMan{"createmultisig",
78+
return RPCHelpMan{"createmultisig",
7779
"\nCreates a multi-signature address with n signature of m keys required.\n"
7880
"It returns a json object with the address and redeemScript.\n",
7981
{
@@ -98,8 +100,8 @@ static UniValue createmultisig(const JSONRPCRequest& request)
98100
"\nAs a JSON-RPC call\n"
99101
+ HelpExampleRpc("createmultisig", "2, \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"")
100102
},
101-
}.Check(request);
102-
103+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
104+
{
103105
int required = request.params[0].get_int();
104106

105107
// Get the public keys
@@ -135,11 +137,13 @@ static UniValue createmultisig(const JSONRPCRequest& request)
135137
result.pushKV("descriptor", descriptor->ToString());
136138

137139
return result;
140+
},
141+
};
138142
}
139143

140-
UniValue getdescriptorinfo(const JSONRPCRequest& request)
144+
static RPCHelpMan getdescriptorinfo()
141145
{
142-
RPCHelpMan{"getdescriptorinfo",
146+
return RPCHelpMan{"getdescriptorinfo",
143147
{"\nAnalyses a descriptor.\n"},
144148
{
145149
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."},
@@ -157,8 +161,9 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
157161
RPCExamples{
158162
"Analyse a descriptor\n" +
159163
HelpExampleCli("getdescriptorinfo", "\"wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)\"")
160-
}}.Check(request);
161-
164+
},
165+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
166+
{
162167
RPCTypeCheck(request.params, {UniValue::VSTR});
163168

164169
FlatSigningProvider provider;
@@ -175,11 +180,13 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
175180
result.pushKV("issolvable", desc->IsSolvable());
176181
result.pushKV("hasprivatekeys", provider.keys.size() > 0);
177182
return result;
183+
},
184+
};
178185
}
179186

180-
UniValue deriveaddresses(const JSONRPCRequest& request)
187+
static RPCHelpMan deriveaddresses()
181188
{
182-
RPCHelpMan{"deriveaddresses",
189+
return RPCHelpMan{"deriveaddresses",
183190
{"\nDerives one or more addresses corresponding to an output descriptor.\n"
184191
"Examples of output descriptors are:\n"
185192
" pkh(<pubkey>) P2PKH outputs for the given pubkey\n"
@@ -202,8 +209,9 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
202209
RPCExamples{
203210
"First three native segwit receive addresses\n" +
204211
HelpExampleCli("deriveaddresses", "\"wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu\" \"[0,2]\"")
205-
}}.Check(request);
206-
212+
},
213+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
214+
{
207215
RPCTypeCheck(request.params, {UniValue::VSTR, UniValueType()}); // Range argument is checked later
208216
const std::string desc_str = request.params[0].get_str();
209217

@@ -254,11 +262,13 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
254262
}
255263

256264
return addresses;
265+
},
266+
};
257267
}
258268

259-
static UniValue verifymessage(const JSONRPCRequest& request)
269+
static RPCHelpMan verifymessage()
260270
{
261-
RPCHelpMan{"verifymessage",
271+
return RPCHelpMan{"verifymessage",
262272
"\nVerify a signed message\n",
263273
{
264274
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."},
@@ -278,8 +288,8 @@ static UniValue verifymessage(const JSONRPCRequest& request)
278288
"\nAs a JSON-RPC call\n"
279289
+ HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
280290
},
281-
}.Check(request);
282-
291+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
292+
{
283293
LOCK(cs_main);
284294

285295
std::string strAddress = request.params[0].get_str();
@@ -301,11 +311,13 @@ static UniValue verifymessage(const JSONRPCRequest& request)
301311
}
302312

303313
return false;
314+
},
315+
};
304316
}
305317

306-
static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
318+
static RPCHelpMan signmessagewithprivkey()
307319
{
308-
RPCHelpMan{"signmessagewithprivkey",
320+
return RPCHelpMan{"signmessagewithprivkey",
309321
"\nSign a message with the private key of an address\n",
310322
{
311323
{"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},
@@ -322,8 +334,8 @@ static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
322334
"\nAs a JSON-RPC call\n"
323335
+ HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
324336
},
325-
}.Check(request);
326-
337+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
338+
{
327339
std::string strPrivkey = request.params[0].get_str();
328340
std::string strMessage = request.params[1].get_str();
329341

@@ -339,20 +351,22 @@ static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
339351
}
340352

341353
return signature;
354+
},
355+
};
342356
}
343357

344-
static UniValue setmocktime(const JSONRPCRequest& request)
358+
static RPCHelpMan setmocktime()
345359
{
346-
RPCHelpMan{"setmocktime",
360+
return RPCHelpMan{"setmocktime",
347361
"\nSet the local time to given timestamp (-regtest only)\n",
348362
{
349363
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n"
350364
" Pass 0 to go back to using the system time."},
351365
},
352366
RPCResult{RPCResult::Type::NONE, "", ""},
353367
RPCExamples{""},
354-
}.Check(request);
355-
368+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
369+
{
356370
if (!Params().IsMockableChain()) {
357371
throw std::runtime_error("setmocktime is for regression testing (-regtest mode) only");
358372
}
@@ -374,19 +388,21 @@ static UniValue setmocktime(const JSONRPCRequest& request)
374388
}
375389

376390
return NullUniValue;
391+
},
392+
};
377393
}
378394

379-
static UniValue mockscheduler(const JSONRPCRequest& request)
395+
static RPCHelpMan mockscheduler()
380396
{
381-
RPCHelpMan{"mockscheduler",
397+
return RPCHelpMan{"mockscheduler",
382398
"\nBump the scheduler into the future (-regtest only)\n",
383399
{
384400
{"delta_time", RPCArg::Type::NUM, RPCArg::Optional::NO, "Number of seconds to forward the scheduler into the future." },
385401
},
386402
RPCResult{RPCResult::Type::NONE, "", ""},
387403
RPCExamples{""},
388-
}.Check(request);
389-
404+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
405+
{
390406
if (!Params().IsMockableChain()) {
391407
throw std::runtime_error("mockscheduler is for regression testing (-regtest mode) only");
392408
}
@@ -405,6 +421,8 @@ static UniValue mockscheduler(const JSONRPCRequest& request)
405421
node.scheduler->MockForward(std::chrono::seconds(delta_seconds));
406422

407423
return NullUniValue;
424+
},
425+
};
408426
}
409427

410428
static UniValue RPCLockedMemoryInfo()
@@ -439,12 +457,12 @@ static std::string RPCMallocInfo()
439457
}
440458
#endif
441459

442-
static UniValue getmemoryinfo(const JSONRPCRequest& request)
460+
static RPCHelpMan getmemoryinfo()
443461
{
444462
/* Please, avoid using the word "pool" here in the RPC interface or help,
445463
* as users will undoubtedly confuse it with the other "memory pool"
446464
*/
447-
RPCHelpMan{"getmemoryinfo",
465+
return RPCHelpMan{"getmemoryinfo",
448466
"Returns an object containing information about memory usage.\n",
449467
{
450468
{"mode", RPCArg::Type::STR, /* default */ "\"stats\"", "determines what kind of information is returned.\n"
@@ -474,8 +492,8 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
474492
HelpExampleCli("getmemoryinfo", "")
475493
+ HelpExampleRpc("getmemoryinfo", "")
476494
},
477-
}.Check(request);
478-
495+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
496+
{
479497
std::string mode = request.params[0].isNull() ? "stats" : request.params[0].get_str();
480498
if (mode == "stats") {
481499
UniValue obj(UniValue::VOBJ);
@@ -490,6 +508,8 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
490508
} else {
491509
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + mode);
492510
}
511+
},
512+
};
493513
}
494514

495515
static void EnableOrDisableLogCategories(UniValue cats, bool enable) {
@@ -510,9 +530,9 @@ static void EnableOrDisableLogCategories(UniValue cats, bool enable) {
510530
}
511531
}
512532

513-
UniValue logging(const JSONRPCRequest& request)
533+
static RPCHelpMan logging()
514534
{
515-
RPCHelpMan{"logging",
535+
return RPCHelpMan{"logging",
516536
"Gets and sets the logging configuration.\n"
517537
"When called without an argument, returns the list of categories with status that are currently being debug logged or not.\n"
518538
"When called with arguments, adds or removes categories from debug logging and return the lists above.\n"
@@ -543,8 +563,8 @@ UniValue logging(const JSONRPCRequest& request)
543563
HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"")
544564
+ HelpExampleRpc("logging", "[\"all\"], [\"libevent\"]")
545565
},
546-
}.Check(request);
547-
566+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
567+
{
548568
uint32_t original_log_categories = LogInstance().GetCategoryMask();
549569
if (request.params[0].isArray()) {
550570
EnableOrDisableLogCategories(request.params[0], true);
@@ -575,13 +595,13 @@ UniValue logging(const JSONRPCRequest& request)
575595
}
576596

577597
return result;
598+
},
599+
};
578600
}
579601

580-
static UniValue echo(const JSONRPCRequest& request)
602+
static RPCHelpMan echo(const std::string& name)
581603
{
582-
if (request.fHelp)
583-
throw std::runtime_error(
584-
RPCHelpMan{"echo|echojson ...",
604+
return RPCHelpMan{name,
585605
"\nSimply echo back the input arguments. This command is for testing.\n"
586606
"\nIt will return an internal bug report when arg9='trigger_internal_bug' is passed.\n"
587607
"\nThe difference between echo and echojson is that echojson has argument conversion enabled in the client-side table in "
@@ -600,16 +620,22 @@ static UniValue echo(const JSONRPCRequest& request)
600620
},
601621
RPCResult{RPCResult::Type::NONE, "", "Returns whatever was passed in"},
602622
RPCExamples{""},
603-
}.ToString()
604-
);
623+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
624+
{
625+
if (request.fHelp) throw std::runtime_error(self.ToString());
605626

606627
if (request.params[9].isStr()) {
607628
CHECK_NONFATAL(request.params[9].get_str() != "trigger_internal_bug");
608629
}
609630

610631
return request.params;
632+
},
633+
};
611634
}
612635

636+
static RPCHelpMan echo() { return echo("echo"); }
637+
static RPCHelpMan echojson() { return echo("echojson"); }
638+
613639
void RegisterMiscRPCCommands(CRPCTable &t)
614640
{
615641
// clang-format off
@@ -629,7 +655,7 @@ static const CRPCCommand commands[] =
629655
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},
630656
{ "hidden", "mockscheduler", &mockscheduler, {"delta_time"}},
631657
{ "hidden", "echo", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
632-
{ "hidden", "echojson", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
658+
{ "hidden", "echojson", &echojson, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
633659
};
634660
// clang-format on
635661
for (const auto& c : commands) {

0 commit comments

Comments
 (0)