Skip to content

Commit

Permalink
rpc: Assert that RPCArg names are equal to CRPCCommand ones (misc)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Aug 2, 2020
1 parent fab5b0f commit fae864b
Showing 1 changed file with 73 additions and 47 deletions.
120 changes: 73 additions & 47 deletions src/rpc/misc.cpp
Expand Up @@ -27,9 +27,9 @@

#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",
{
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to validate"},
Expand All @@ -50,8 +50,8 @@ static UniValue validateaddress(const JSONRPCRequest& request)
HelpExampleCli("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());
bool isValid = IsValidDestination(dest);

Expand All @@ -69,11 +69,13 @@ static UniValue validateaddress(const JSONRPCRequest& request)
ret.pushKVs(detail);
}
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"
"It returns a json object with the address and redeemScript.\n",
{
Expand All @@ -98,8 +100,8 @@ static UniValue createmultisig(const JSONRPCRequest& request)
"\nAs a JSON-RPC call\n"
+ HelpExampleRpc("createmultisig", "2, \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"")
},
}.Check(request);

[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
int required = request.params[0].get_int();

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

return result;
},
};
}

UniValue getdescriptorinfo(const JSONRPCRequest& request)
static RPCHelpMan getdescriptorinfo()
{
RPCHelpMan{"getdescriptorinfo",
return RPCHelpMan{"getdescriptorinfo",
{"\nAnalyses a descriptor.\n"},
{
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."},
Expand All @@ -157,8 +161,9 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
RPCExamples{
"Analyse a descriptor\n" +
HelpExampleCli("getdescriptorinfo", "\"wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)\"")
}}.Check(request);

},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
RPCTypeCheck(request.params, {UniValue::VSTR});

FlatSigningProvider provider;
Expand All @@ -175,11 +180,13 @@ UniValue getdescriptorinfo(const JSONRPCRequest& request)
result.pushKV("issolvable", desc->IsSolvable());
result.pushKV("hasprivatekeys", provider.keys.size() > 0);
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"
"Examples of output descriptors are:\n"
" pkh(<pubkey>) P2PKH outputs for the given pubkey\n"
Expand All @@ -202,8 +209,9 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
RPCExamples{
"First three native segwit receive addresses\n" +
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
const std::string desc_str = request.params[0].get_str();

Expand Down Expand Up @@ -254,11 +262,13 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
}

return addresses;
},
};
}

static UniValue verifymessage(const JSONRPCRequest& request)
static RPCHelpMan verifymessage()
{
RPCHelpMan{"verifymessage",
return RPCHelpMan{"verifymessage",
"\nVerify a signed message\n",
{
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."},
Expand All @@ -278,8 +288,8 @@ static UniValue verifymessage(const JSONRPCRequest& request)
"\nAs a JSON-RPC call\n"
+ HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
},
}.Check(request);

[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main);

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

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",
{
{"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},
Expand All @@ -322,8 +334,8 @@ static UniValue signmessagewithprivkey(const JSONRPCRequest& request)
"\nAs a JSON-RPC call\n"
+ HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
},
}.Check(request);

[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::string strPrivkey = request.params[0].get_str();
std::string strMessage = request.params[1].get_str();

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

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",
{
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n"
" Pass 0 to go back to using the system time."},
},
RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{""},
}.Check(request);

[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!Params().IsMockableChain()) {
throw std::runtime_error("setmocktime is for regression testing (-regtest mode) only");
}
Expand All @@ -374,19 +388,21 @@ static UniValue setmocktime(const JSONRPCRequest& request)
}

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",
{
{"delta_time", RPCArg::Type::NUM, RPCArg::Optional::NO, "Number of seconds to forward the scheduler into the future." },
},
RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{""},
}.Check(request);

[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!Params().IsMockableChain()) {
throw std::runtime_error("mockscheduler is for regression testing (-regtest mode) only");
}
Expand All @@ -405,6 +421,8 @@ static UniValue mockscheduler(const JSONRPCRequest& request)
node.scheduler->MockForward(std::chrono::seconds(delta_seconds));

return NullUniValue;
},
};
}

static UniValue RPCLockedMemoryInfo()
Expand Down Expand Up @@ -439,12 +457,12 @@ static std::string RPCMallocInfo()
}
#endif

static UniValue getmemoryinfo(const JSONRPCRequest& request)
static RPCHelpMan getmemoryinfo()
{
/* Please, avoid using the word "pool" here in the RPC interface or help,
* 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",
{
{"mode", RPCArg::Type::STR, /* default */ "\"stats\"", "determines what kind of information is returned.\n"
Expand Down Expand Up @@ -474,8 +492,8 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
HelpExampleCli("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();
if (mode == "stats") {
UniValue obj(UniValue::VOBJ);
Expand All @@ -490,6 +508,8 @@ static UniValue getmemoryinfo(const JSONRPCRequest& request)
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + mode);
}
},
};
}

static void EnableOrDisableLogCategories(UniValue cats, bool enable) {
Expand All @@ -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"
"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"
Expand Down Expand Up @@ -543,8 +563,8 @@ UniValue logging(const JSONRPCRequest& request)
HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"")
+ HelpExampleRpc("logging", "[\"all\"], [\"libevent\"]")
},
}.Check(request);

[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
uint32_t original_log_categories = LogInstance().GetCategoryMask();
if (request.params[0].isArray()) {
EnableOrDisableLogCategories(request.params[0], true);
Expand Down Expand Up @@ -575,13 +595,13 @@ UniValue logging(const JSONRPCRequest& request)
}

return result;
},
};
}

static UniValue echo(const JSONRPCRequest& request)
static RPCHelpMan echo(const std::string& name)
{
if (request.fHelp)
throw std::runtime_error(
RPCHelpMan{"echo|echojson ...",
return RPCHelpMan{name,
"\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"
"\nThe difference between echo and echojson is that echojson has argument conversion enabled in the client-side table in "
Expand All @@ -600,16 +620,22 @@ static UniValue echo(const JSONRPCRequest& request)
},
RPCResult{RPCResult::Type::NONE, "", "Returns whatever was passed in"},
RPCExamples{""},
}.ToString()
);
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (request.fHelp) throw std::runtime_error(self.ToString());

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

return request.params;
},
};
}

static RPCHelpMan echo() { return echo("echo"); }
static RPCHelpMan echojson() { return echo("echojson"); }

void RegisterMiscRPCCommands(CRPCTable &t)
{
// clang-format off
Expand All @@ -629,7 +655,7 @@ static const CRPCCommand commands[] =
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},
{ "hidden", "mockscheduler", &mockscheduler, {"delta_time"}},
{ "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
for (const auto& c : commands) {
Expand Down

0 comments on commit fae864b

Please sign in to comment.