diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -174,59 +174,64 @@ return strRet; } -static UniValue help(Config &config, const JSONRPCRequest &jsonRequest) { - if (jsonRequest.fHelp || jsonRequest.params.size() > 1) { - throw std::runtime_error(RPCHelpMan{ - "help", - "List all commands, or get help for a specified command.\n", - { - {"command", RPCArg::Type::STR, /* default */ "all commands", - "The command to get help on"}, - }, - RPCResult{RPCResult::Type::STR, "", "The help text"}, - RPCExamples{""}, - } - .ToString()); - } - - std::string strCommand; - if (jsonRequest.params.size() > 0) { - strCommand = jsonRequest.params[0].get_str(); - } +static RPCHelpMan help() { + return RPCHelpMan{ + "help", + "List all commands, or get help for a specified command.\n", + { + {"command", RPCArg::Type::STR, /* default */ "all commands", + "The command to get help on"}, + }, + RPCResult{RPCResult::Type::STR, "", "The help text"}, + RPCExamples{""}, + [&](const RPCHelpMan &self, Config &config, + const JSONRPCRequest &jsonRequest) -> UniValue { + std::string strCommand; + if (jsonRequest.params.size() > 0) { + strCommand = jsonRequest.params[0].get_str(); + } - return tableRPC.help(config, strCommand, jsonRequest); + return tableRPC.help(config, strCommand, jsonRequest); + }, + }; } -static UniValue stop(const Config &config, const JSONRPCRequest &jsonRequest) { +static RPCHelpMan stop() { static const std::string RESULT{PACKAGE_NAME " stopping"}; - // Accept the deprecated and ignored 'detach' boolean argument - // Also accept the hidden 'wait' integer argument (milliseconds) - // For instance, 'stop 1000' makes the call wait 1 second before returning - // 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 ".", - {}, - RPCResult{RPCResult::Type::STR, "", - "A string with the content '" + RESULT + "'"}, - RPCExamples{""}, - } - .ToString()); - } - - // Event loop will exit after current HTTP requests have been handled, so - // this reply will get back to the client. - StartShutdown(); - if (jsonRequest.params[0].isNum()) { - UninterruptibleSleep( - std::chrono::milliseconds{jsonRequest.params[0].get_int()}); - } - return RESULT; + return RPCHelpMan{ + "stop", + // Also accept the hidden 'wait' integer argument (milliseconds) + // For instance, 'stop 1000' makes the call wait 1 second before + // returning to the client (intended for testing) + "\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 + "'"}, + RPCExamples{""}, + [&](const RPCHelpMan &self, Config &config, + const JSONRPCRequest &jsonRequest) -> UniValue { + // Event loop will exit after current HTTP requests have been + // handled, so this reply will get back to the client. + StartShutdown(); + if (jsonRequest.params[0].isNum()) { + UninterruptibleSleep( + std::chrono::milliseconds{jsonRequest.params[0].get_int()}); + } + return RESULT; + }, + }; } -static UniValue uptime(const Config &config, const JSONRPCRequest &request) { - RPCHelpMan{ +static RPCHelpMan uptime() { + return RPCHelpMan{ "uptime", "Returns the total uptime of the server.\n", {}, @@ -234,15 +239,14 @@ "The number of seconds that the server has been running"}, RPCExamples{HelpExampleCli("uptime", "") + HelpExampleRpc("uptime", "")}, - } - .Check(request); - - return GetTime() - GetStartupTime(); + [&](const RPCHelpMan &self, Config &config, + const JSONRPCRequest &request) -> UniValue { + return GetTime() - GetStartupTime(); + }}; } -static UniValue getrpcinfo(const Config &config, - const JSONRPCRequest &request) { - RPCHelpMan{ +static RPCHelpMan getrpcinfo() { + return RPCHelpMan{ "getrpcinfo", "Returns details of the RPC server.\n", {}, @@ -269,27 +273,28 @@ }}, RPCExamples{HelpExampleCli("getrpcinfo", "") + HelpExampleRpc("getrpcinfo", "")}, - } - .Check(request); - - LOCK(g_rpc_server_info.mutex); - UniValue active_commands(UniValue::VARR); - for (const RPCCommandExecutionInfo &info : - g_rpc_server_info.active_commands) { - UniValue entry(UniValue::VOBJ); - entry.pushKV("method", info.method); - entry.pushKV("duration", GetTimeMicros() - info.start); - active_commands.push_back(entry); - } - UniValue result(UniValue::VOBJ); - result.pushKV("active_commands", active_commands); + [&](const RPCHelpMan &self, Config &config, + const JSONRPCRequest &request) -> UniValue { + LOCK(g_rpc_server_info.mutex); + UniValue active_commands(UniValue::VARR); + for (const RPCCommandExecutionInfo &info : + g_rpc_server_info.active_commands) { + UniValue entry(UniValue::VOBJ); + entry.pushKV("method", info.method); + entry.pushKV("duration", GetTimeMicros() - info.start); + active_commands.push_back(entry); + } + + UniValue result(UniValue::VOBJ); + result.pushKV("active_commands", active_commands); - const std::string path = LogInstance().m_file_path.string(); - UniValue log_path(UniValue::VSTR, path); - result.pushKV("logpath", log_path); + const std::string path = LogInstance().m_file_path.string(); + UniValue log_path(UniValue::VSTR, path); + result.pushKV("logpath", log_path); - return result; + return result; + }}; } // clang-format off diff --git a/src/rpc/util.h b/src/rpc/util.h --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -343,6 +343,7 @@ std::string ToString() const; UniValue HandleRequest(Config &config, const JSONRPCRequest &request) { + Check(request); return m_fun(*this, config, request); } /** If the supplied number of args is neither too small nor too high */