rpc: Update server to use new RPCHelpMan

Also, move Check to inside HandleRequest
This commit is contained in:
MarcoFalke 2020-06-26 14:12:46 -04:00
parent aaaaad5627
commit fa7592bfa8
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
2 changed files with 28 additions and 23 deletions

View File

@ -130,11 +130,9 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
return strRet; return strRet;
} }
UniValue help(const JSONRPCRequest& jsonRequest) static RPCHelpMan help()
{ {
if (jsonRequest.fHelp || jsonRequest.params.size() > 1) return RPCHelpMan{"help",
throw std::runtime_error(
RPCHelpMan{"help",
"\nList all commands, or get help for a specified command.\n", "\nList all commands, or get help for a specified command.\n",
{ {
{"command", RPCArg::Type::STR, /* default */ "all commands", "The command to get help on"}, {"command", RPCArg::Type::STR, /* default */ "all commands", "The command to get help on"},
@ -143,32 +141,32 @@ UniValue help(const JSONRPCRequest& jsonRequest)
RPCResult::Type::STR, "", "The help text" RPCResult::Type::STR, "", "The help text"
}, },
RPCExamples{""}, RPCExamples{""},
}.ToString() [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
); {
std::string strCommand; std::string strCommand;
if (jsonRequest.params.size() > 0) if (jsonRequest.params.size() > 0)
strCommand = jsonRequest.params[0].get_str(); strCommand = jsonRequest.params[0].get_str();
return tableRPC.help(strCommand, jsonRequest); return tableRPC.help(strCommand, jsonRequest);
},
};
} }
static RPCHelpMan stop()
UniValue stop(const JSONRPCRequest& jsonRequest)
{ {
static const std::string RESULT{PACKAGE_NAME " stopping"}; static const std::string RESULT{PACKAGE_NAME " stopping"};
// Accept the deprecated and ignored 'detach' boolean argument return RPCHelpMan{"stop",
// Also accept the hidden 'wait' integer argument (milliseconds) // Also accept the hidden 'wait' integer argument (milliseconds)
// For instance, 'stop 1000' makes the call wait 1 second before returning // For instance, 'stop 1000' makes the call wait 1 second before returning
// to the client (intended for testing) // to the client (intended for testing)
if (jsonRequest.fHelp || jsonRequest.params.size() > 1)
throw std::runtime_error(
RPCHelpMan{"stop",
"\nRequest a graceful shutdown of " PACKAGE_NAME ".", "\nRequest a graceful shutdown of " PACKAGE_NAME ".",
{}, {
{"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "how long to wait in ms", "", {}, /* hidden */ true},
},
RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"}, RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
RPCExamples{""}, RPCExamples{""},
}.ToString()); [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
{
// Event loop will exit after current HTTP requests have been handled, so // Event loop will exit after current HTTP requests have been handled, so
// this reply will get back to the client. // this reply will get back to the client.
StartShutdown(); StartShutdown();
@ -176,11 +174,13 @@ UniValue stop(const JSONRPCRequest& jsonRequest)
UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].get_int()}); UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].get_int()});
} }
return RESULT; return RESULT;
},
};
} }
static UniValue uptime(const JSONRPCRequest& jsonRequest) static RPCHelpMan uptime()
{ {
RPCHelpMan{"uptime", return RPCHelpMan{"uptime",
"\nReturns the total uptime of the server.\n", "\nReturns the total uptime of the server.\n",
{}, {},
RPCResult{ RPCResult{
@ -190,14 +190,16 @@ static UniValue uptime(const JSONRPCRequest& jsonRequest)
HelpExampleCli("uptime", "") HelpExampleCli("uptime", "")
+ HelpExampleRpc("uptime", "") + HelpExampleRpc("uptime", "")
}, },
}.Check(jsonRequest); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
return GetTime() - GetStartupTime(); return GetTime() - GetStartupTime();
} }
};
}
static UniValue getrpcinfo(const JSONRPCRequest& request) static RPCHelpMan getrpcinfo()
{ {
RPCHelpMan{"getrpcinfo", return RPCHelpMan{"getrpcinfo",
"\nReturns details of the RPC server.\n", "\nReturns details of the RPC server.\n",
{}, {},
RPCResult{ RPCResult{
@ -217,8 +219,8 @@ static UniValue getrpcinfo(const JSONRPCRequest& request)
RPCExamples{ RPCExamples{
HelpExampleCli("getrpcinfo", "") HelpExampleCli("getrpcinfo", "")
+ HelpExampleRpc("getrpcinfo", "")}, + HelpExampleRpc("getrpcinfo", "")},
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(g_rpc_server_info.mutex); LOCK(g_rpc_server_info.mutex);
UniValue active_commands(UniValue::VARR); UniValue active_commands(UniValue::VARR);
for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) { for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
@ -237,6 +239,8 @@ static UniValue getrpcinfo(const JSONRPCRequest& request)
return result; return result;
} }
};
}
// clang-format off // clang-format off
static const CRPCCommand vRPCCommands[] = static const CRPCCommand vRPCCommands[] =

View File

@ -336,6 +336,7 @@ public:
std::string ToString() const; std::string ToString() const;
UniValue HandleRequest(const JSONRPCRequest& request) UniValue HandleRequest(const JSONRPCRequest& request)
{ {
Check(request);
return m_fun(*this, request); return m_fun(*this, request);
} }
/** If the supplied number of args is neither too small nor too high */ /** If the supplied number of args is neither too small nor too high */