Skip to content

Commit fa7592b

Browse files
author
MarcoFalke
committed
rpc: Update server to use new RPCHelpMan
Also, move Check to inside HandleRequest
1 parent aaaaad5 commit fa7592b

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

src/rpc/server.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,9 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
130130
return strRet;
131131
}
132132

133-
UniValue help(const JSONRPCRequest& jsonRequest)
133+
static RPCHelpMan help()
134134
{
135-
if (jsonRequest.fHelp || jsonRequest.params.size() > 1)
136-
throw std::runtime_error(
137-
RPCHelpMan{"help",
135+
return RPCHelpMan{"help",
138136
"\nList all commands, or get help for a specified command.\n",
139137
{
140138
{"command", RPCArg::Type::STR, /* default */ "all commands", "The command to get help on"},
@@ -143,44 +141,46 @@ UniValue help(const JSONRPCRequest& jsonRequest)
143141
RPCResult::Type::STR, "", "The help text"
144142
},
145143
RPCExamples{""},
146-
}.ToString()
147-
);
148-
144+
[&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
145+
{
149146
std::string strCommand;
150147
if (jsonRequest.params.size() > 0)
151148
strCommand = jsonRequest.params[0].get_str();
152149

153150
return tableRPC.help(strCommand, jsonRequest);
151+
},
152+
};
154153
}
155154

156-
157-
UniValue stop(const JSONRPCRequest& jsonRequest)
155+
static RPCHelpMan stop()
158156
{
159157
static const std::string RESULT{PACKAGE_NAME " stopping"};
160-
// Accept the deprecated and ignored 'detach' boolean argument
158+
return RPCHelpMan{"stop",
161159
// Also accept the hidden 'wait' integer argument (milliseconds)
162160
// For instance, 'stop 1000' makes the call wait 1 second before returning
163161
// to the client (intended for testing)
164-
if (jsonRequest.fHelp || jsonRequest.params.size() > 1)
165-
throw std::runtime_error(
166-
RPCHelpMan{"stop",
167162
"\nRequest a graceful shutdown of " PACKAGE_NAME ".",
168-
{},
163+
{
164+
{"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "how long to wait in ms", "", {}, /* hidden */ true},
165+
},
169166
RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
170167
RPCExamples{""},
171-
}.ToString());
168+
[&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
169+
{
172170
// Event loop will exit after current HTTP requests have been handled, so
173171
// this reply will get back to the client.
174172
StartShutdown();
175173
if (jsonRequest.params[0].isNum()) {
176174
UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].get_int()});
177175
}
178176
return RESULT;
177+
},
178+
};
179179
}
180180

181-
static UniValue uptime(const JSONRPCRequest& jsonRequest)
181+
static RPCHelpMan uptime()
182182
{
183-
RPCHelpMan{"uptime",
183+
return RPCHelpMan{"uptime",
184184
"\nReturns the total uptime of the server.\n",
185185
{},
186186
RPCResult{
@@ -190,14 +190,16 @@ static UniValue uptime(const JSONRPCRequest& jsonRequest)
190190
HelpExampleCli("uptime", "")
191191
+ HelpExampleRpc("uptime", "")
192192
},
193-
}.Check(jsonRequest);
194-
193+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
194+
{
195195
return GetTime() - GetStartupTime();
196196
}
197+
};
198+
}
197199

198-
static UniValue getrpcinfo(const JSONRPCRequest& request)
200+
static RPCHelpMan getrpcinfo()
199201
{
200-
RPCHelpMan{"getrpcinfo",
202+
return RPCHelpMan{"getrpcinfo",
201203
"\nReturns details of the RPC server.\n",
202204
{},
203205
RPCResult{
@@ -217,8 +219,8 @@ static UniValue getrpcinfo(const JSONRPCRequest& request)
217219
RPCExamples{
218220
HelpExampleCli("getrpcinfo", "")
219221
+ HelpExampleRpc("getrpcinfo", "")},
220-
}.Check(request);
221-
222+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
223+
{
222224
LOCK(g_rpc_server_info.mutex);
223225
UniValue active_commands(UniValue::VARR);
224226
for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
@@ -237,6 +239,8 @@ static UniValue getrpcinfo(const JSONRPCRequest& request)
237239

238240
return result;
239241
}
242+
};
243+
}
240244

241245
// clang-format off
242246
static const CRPCCommand vRPCCommands[] =

src/rpc/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ class RPCHelpMan
336336
std::string ToString() const;
337337
UniValue HandleRequest(const JSONRPCRequest& request)
338338
{
339+
Check(request);
339340
return m_fun(*this, request);
340341
}
341342
/** If the supplied number of args is neither too small nor too high */

0 commit comments

Comments
 (0)