rpc: Assert that RPCArg names are equal to CRPCCommand ones (misc)

This commit is contained in:
MarcoFalke 2020-07-15 21:26:22 +02:00
parent fa50bdc755
commit fa77de2baa
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -27,9 +27,9 @@
#include <univalue.h> #include <univalue.h>
static UniValue validateaddress(const JSONRPCRequest& request) static RPCHelpMan validateaddress()
{ {
RPCHelpMan{"validateaddress", return RPCHelpMan{"validateaddress",
"\nReturn information about the given bitcoin address.\n", "\nReturn information about the given bitcoin address.\n",
{ {
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to validate"}, {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to validate"},
@ -50,8 +50,8 @@ static UniValue validateaddress(const JSONRPCRequest& request)
HelpExampleCli("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") + HelpExampleCli("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
HelpExampleRpc("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") HelpExampleRpc("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
CTxDestination dest = DecodeDestination(request.params[0].get_str()); CTxDestination dest = DecodeDestination(request.params[0].get_str());
bool isValid = IsValidDestination(dest); bool isValid = IsValidDestination(dest);
@ -69,11 +69,13 @@ static UniValue validateaddress(const JSONRPCRequest& request)
ret.pushKVs(detail); ret.pushKVs(detail);
} }
return ret; return ret;
},
};
} }
static UniValue createmultisig(const JSONRPCRequest& request) static RPCHelpMan createmultisig()
{ {
RPCHelpMan{"createmultisig", return RPCHelpMan{"createmultisig",
"\nCreates a multi-signature address with n signature of m keys required.\n" "\nCreates a multi-signature address with n signature of m keys required.\n"
"It returns a json object with the address and redeemScript.\n", "It returns a json object with the address and redeemScript.\n",
{ {
@ -98,8 +100,8 @@ static UniValue createmultisig(const JSONRPCRequest& request)
"\nAs a JSON-RPC call\n" "\nAs a JSON-RPC call\n"
+ HelpExampleRpc("createmultisig", "2, \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"") + HelpExampleRpc("createmultisig", "2, \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
int required = request.params[0].get_int(); int required = request.params[0].get_int();
// Get the public keys // Get the public keys
@ -135,11 +137,13 @@ static UniValue createmultisig(const JSONRPCRequest& request)
result.pushKV("descriptor", descriptor->ToString()); result.pushKV("descriptor", descriptor->ToString());
return result; return result;
},
};
} }
UniValue getdescriptorinfo(const JSONRPCRequest& request) static RPCHelpMan getdescriptorinfo()
{ {
RPCHelpMan{"getdescriptorinfo", return RPCHelpMan{"getdescriptorinfo",
{"\nAnalyses a descriptor.\n"}, {"\nAnalyses a descriptor.\n"},
{ {
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."}, {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."},
@ -157,8 +161,9 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
RPCExamples{ RPCExamples{
"Analyse a descriptor\n" + "Analyse a descriptor\n" +
HelpExampleCli("getdescriptorinfo", "\"wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)\"") HelpExampleCli("getdescriptorinfo", "\"wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)\"")
}}.Check(request); },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
RPCTypeCheck(request.params, {UniValue::VSTR}); RPCTypeCheck(request.params, {UniValue::VSTR});
FlatSigningProvider provider; FlatSigningProvider provider;
@ -175,11 +180,13 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
result.pushKV("issolvable", desc->IsSolvable()); result.pushKV("issolvable", desc->IsSolvable());
result.pushKV("hasprivatekeys", provider.keys.size() > 0); result.pushKV("hasprivatekeys", provider.keys.size() > 0);
return result; return result;
},
};
} }
UniValue deriveaddresses(const JSONRPCRequest& request) static RPCHelpMan deriveaddresses()
{ {
RPCHelpMan{"deriveaddresses", return RPCHelpMan{"deriveaddresses",
{"\nDerives one or more addresses corresponding to an output descriptor.\n" {"\nDerives one or more addresses corresponding to an output descriptor.\n"
"Examples of output descriptors are:\n" "Examples of output descriptors are:\n"
" pkh(<pubkey>) P2PKH outputs for the given pubkey\n" " pkh(<pubkey>) P2PKH outputs for the given pubkey\n"
@ -202,8 +209,9 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
RPCExamples{ RPCExamples{
"First three native segwit receive addresses\n" + "First three native segwit receive addresses\n" +
HelpExampleCli("deriveaddresses", "\"wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu\" \"[0,2]\"") HelpExampleCli("deriveaddresses", "\"wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu\" \"[0,2]\"")
}}.Check(request); },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
RPCTypeCheck(request.params, {UniValue::VSTR, UniValueType()}); // Range argument is checked later RPCTypeCheck(request.params, {UniValue::VSTR, UniValueType()}); // Range argument is checked later
const std::string desc_str = request.params[0].get_str(); const std::string desc_str = request.params[0].get_str();
@ -254,11 +262,13 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
} }
return addresses; return addresses;
},
};
} }
static UniValue verifymessage(const JSONRPCRequest& request) static RPCHelpMan verifymessage()
{ {
RPCHelpMan{"verifymessage", return RPCHelpMan{"verifymessage",
"\nVerify a signed message\n", "\nVerify a signed message\n",
{ {
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."}, {"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)
"\nAs a JSON-RPC call\n" "\nAs a JSON-RPC call\n"
+ HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"") + HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main); LOCK(cs_main);
std::string strAddress = request.params[0].get_str(); std::string strAddress = request.params[0].get_str();
@ -301,11 +311,13 @@ static UniValue verifymessage(const JSONRPCRequest& request)
} }
return false; return false;
},
};
} }
static UniValue signmessagewithprivkey(const JSONRPCRequest& request) static RPCHelpMan signmessagewithprivkey()
{ {
RPCHelpMan{"signmessagewithprivkey", return RPCHelpMan{"signmessagewithprivkey",
"\nSign a message with the private key of an address\n", "\nSign a message with the private key of an address\n",
{ {
{"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."}, {"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)
"\nAs a JSON-RPC call\n" "\nAs a JSON-RPC call\n"
+ HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"") + HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::string strPrivkey = request.params[0].get_str(); std::string strPrivkey = request.params[0].get_str();
std::string strMessage = request.params[1].get_str(); std::string strMessage = request.params[1].get_str();
@ -339,11 +351,13 @@ static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
} }
return signature; return signature;
},
};
} }
static UniValue setmocktime(const JSONRPCRequest& request) static RPCHelpMan setmocktime()
{ {
RPCHelpMan{"setmocktime", return RPCHelpMan{"setmocktime",
"\nSet the local time to given timestamp (-regtest only)\n", "\nSet the local time to given timestamp (-regtest only)\n",
{ {
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n" {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n"
@ -351,8 +365,8 @@ static UniValue setmocktime(const JSONRPCRequest& request)
}, },
RPCResult{RPCResult::Type::NONE, "", ""}, RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{""}, RPCExamples{""},
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!Params().IsMockableChain()) { if (!Params().IsMockableChain()) {
throw std::runtime_error("setmocktime is for regression testing (-regtest mode) only"); throw std::runtime_error("setmocktime is for regression testing (-regtest mode) only");
} }
@ -374,19 +388,21 @@ static UniValue setmocktime(const JSONRPCRequest& request)
} }
return NullUniValue; return NullUniValue;
},
};
} }
static UniValue mockscheduler(const JSONRPCRequest& request) static RPCHelpMan mockscheduler()
{ {
RPCHelpMan{"mockscheduler", return RPCHelpMan{"mockscheduler",
"\nBump the scheduler into the future (-regtest only)\n", "\nBump the scheduler into the future (-regtest only)\n",
{ {
{"delta_time", RPCArg::Type::NUM, RPCArg::Optional::NO, "Number of seconds to forward the scheduler into the future." }, {"delta_time", RPCArg::Type::NUM, RPCArg::Optional::NO, "Number of seconds to forward the scheduler into the future." },
}, },
RPCResult{RPCResult::Type::NONE, "", ""}, RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{""}, RPCExamples{""},
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!Params().IsMockableChain()) { if (!Params().IsMockableChain()) {
throw std::runtime_error("mockscheduler is for regression testing (-regtest mode) only"); throw std::runtime_error("mockscheduler is for regression testing (-regtest mode) only");
} }
@ -405,6 +421,8 @@ static UniValue mockscheduler(const JSONRPCRequest& request)
node.scheduler->MockForward(std::chrono::seconds(delta_seconds)); node.scheduler->MockForward(std::chrono::seconds(delta_seconds));
return NullUniValue; return NullUniValue;
},
};
} }
static UniValue RPCLockedMemoryInfo() static UniValue RPCLockedMemoryInfo()
@ -439,12 +457,12 @@ static std::string RPCMallocInfo()
} }
#endif #endif
static UniValue getmemoryinfo(const JSONRPCRequest& request) static RPCHelpMan getmemoryinfo()
{ {
/* Please, avoid using the word "pool" here in the RPC interface or help, /* Please, avoid using the word "pool" here in the RPC interface or help,
* as users will undoubtedly confuse it with the other "memory pool" * as users will undoubtedly confuse it with the other "memory pool"
*/ */
RPCHelpMan{"getmemoryinfo", return RPCHelpMan{"getmemoryinfo",
"Returns an object containing information about memory usage.\n", "Returns an object containing information about memory usage.\n",
{ {
{"mode", RPCArg::Type::STR, /* default */ "\"stats\"", "determines what kind of information is returned.\n" {"mode", RPCArg::Type::STR, /* default */ "\"stats\"", "determines what kind of information is returned.\n"
@ -474,8 +492,8 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
HelpExampleCli("getmemoryinfo", "") HelpExampleCli("getmemoryinfo", "")
+ HelpExampleRpc("getmemoryinfo", "") + HelpExampleRpc("getmemoryinfo", "")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::string mode = request.params[0].isNull() ? "stats" : request.params[0].get_str(); std::string mode = request.params[0].isNull() ? "stats" : request.params[0].get_str();
if (mode == "stats") { if (mode == "stats") {
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
@ -490,6 +508,8 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
} else { } else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + mode); throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + mode);
} }
},
};
} }
static void EnableOrDisableLogCategories(UniValue cats, bool enable) { static void EnableOrDisableLogCategories(UniValue cats, bool enable) {
@ -510,9 +530,9 @@ static void EnableOrDisableLogCategories(UniValue cats, bool enable) {
} }
} }
UniValue logging(const JSONRPCRequest& request) static RPCHelpMan logging()
{ {
RPCHelpMan{"logging", return RPCHelpMan{"logging",
"Gets and sets the logging configuration.\n" "Gets and sets the logging configuration.\n"
"When called without an argument, returns the list of categories with status that are currently being debug logged or not.\n" "When called without an argument, returns the list of categories with status that are currently being debug logged or not.\n"
"When called with arguments, adds or removes categories from debug logging and return the lists above.\n" "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)
HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"") HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"")
+ HelpExampleRpc("logging", "[\"all\"], [\"libevent\"]") + HelpExampleRpc("logging", "[\"all\"], [\"libevent\"]")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
uint32_t original_log_categories = LogInstance().GetCategoryMask(); uint32_t original_log_categories = LogInstance().GetCategoryMask();
if (request.params[0].isArray()) { if (request.params[0].isArray()) {
EnableOrDisableLogCategories(request.params[0], true); EnableOrDisableLogCategories(request.params[0], true);
@ -575,13 +595,13 @@ UniValue logging(const JSONRPCRequest& request)
} }
return result; return result;
},
};
} }
static UniValue echo(const JSONRPCRequest& request) static RPCHelpMan echo(const std::string& name)
{ {
if (request.fHelp) return RPCHelpMan{name,
throw std::runtime_error(
RPCHelpMan{"echo|echojson ...",
"\nSimply echo back the input arguments. This command is for testing.\n" "\nSimply echo back the input arguments. This command is for testing.\n"
"\nIt will return an internal bug report when arg9='trigger_internal_bug' is passed.\n" "\nIt will return an internal bug report when arg9='trigger_internal_bug' is passed.\n"
"\nThe difference between echo and echojson is that echojson has argument conversion enabled in the client-side table in " "\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)
}, },
RPCResult{RPCResult::Type::NONE, "", "Returns whatever was passed in"}, RPCResult{RPCResult::Type::NONE, "", "Returns whatever was passed in"},
RPCExamples{""}, RPCExamples{""},
}.ToString() [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
); {
if (request.fHelp) throw std::runtime_error(self.ToString());
if (request.params[9].isStr()) { if (request.params[9].isStr()) {
CHECK_NONFATAL(request.params[9].get_str() != "trigger_internal_bug"); CHECK_NONFATAL(request.params[9].get_str() != "trigger_internal_bug");
} }
return request.params; return request.params;
},
};
} }
static RPCHelpMan echo() { return echo("echo"); }
static RPCHelpMan echojson() { return echo("echojson"); }
void RegisterMiscRPCCommands(CRPCTable &t) void RegisterMiscRPCCommands(CRPCTable &t)
{ {
// clang-format off // clang-format off
@ -629,7 +655,7 @@ static const CRPCCommand commands[] =
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}}, { "hidden", "setmocktime", &setmocktime, {"timestamp"}},
{ "hidden", "mockscheduler", &mockscheduler, {"delta_time"}}, { "hidden", "mockscheduler", &mockscheduler, {"delta_time"}},
{ "hidden", "echo", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}}, { "hidden", "echo", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
{ "hidden", "echojson", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}}, { "hidden", "echojson", &echojson, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
}; };
// clang-format on // clang-format on
for (const auto& c : commands) { for (const auto& c : commands) {