Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <bitcoin-build-config.h> // IWYU pragma: keep
7 : :
8 : : #include <rpc/server.h>
9 : :
10 : : #include <common/args.h>
11 : : #include <common/system.h>
12 : : #include <logging.h>
13 : : #include <node/context.h>
14 : : #include <node/kernel_notifications.h>
15 : : #include <rpc/server_util.h>
16 : : #include <rpc/util.h>
17 : : #include <sync.h>
18 : : #include <util/signalinterrupt.h>
19 : : #include <util/strencodings.h>
20 : : #include <util/string.h>
21 : : #include <util/time.h>
22 : : #include <validation.h>
23 : :
24 : : #include <cassert>
25 : : #include <chrono>
26 : : #include <memory>
27 : : #include <mutex>
28 : : #include <unordered_map>
29 : :
30 : : using util::SplitString;
31 : :
32 : : static GlobalMutex g_rpc_warmup_mutex;
33 : : static std::atomic<bool> g_rpc_running{false};
34 : : static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
35 : : static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started";
36 : : /* Timer-creating functions */
37 : : static RPCTimerInterface* timerInterface = nullptr;
38 : : /* Map of name to timer. */
39 : : static GlobalMutex g_deadline_timers_mutex;
40 : : static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers GUARDED_BY(g_deadline_timers_mutex);
41 : : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler);
42 : :
43 : 134 : struct RPCCommandExecutionInfo
44 : : {
45 : : std::string method;
46 : : SteadyClock::time_point start;
47 : : };
48 : :
49 : : struct RPCServerInfo
50 : : {
51 : : Mutex mutex;
52 : : std::list<RPCCommandExecutionInfo> active_commands GUARDED_BY(mutex);
53 : : };
54 : :
55 : : static RPCServerInfo g_rpc_server_info;
56 : :
57 : : struct RPCCommandExecution
58 : : {
59 : : std::list<RPCCommandExecutionInfo>::iterator it;
60 : 67 : explicit RPCCommandExecution(const std::string& method)
61 : 67 : {
62 : 67 : LOCK(g_rpc_server_info.mutex);
63 [ + - + - ]: 67 : it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, SteadyClock::now()});
64 [ + - + - ]: 201 : }
65 : 67 : ~RPCCommandExecution()
66 : : {
67 : 67 : LOCK(g_rpc_server_info.mutex);
68 [ + - ]: 67 : g_rpc_server_info.active_commands.erase(it);
69 : 67 : }
70 : : };
71 : :
72 : 0 : std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest& helpreq) const
73 : : {
74 [ # # ]: 0 : std::string strRet;
75 : 0 : std::string category;
76 [ # # ]: 0 : std::set<intptr_t> setDone;
77 : 0 : std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
78 [ # # ]: 0 : vCommands.reserve(mapCommands.size());
79 : :
80 [ # # ]: 0 : for (const auto& entry : mapCommands)
81 [ # # # # ]: 0 : vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
82 : 0 : sort(vCommands.begin(), vCommands.end());
83 : :
84 [ # # ]: 0 : JSONRPCRequest jreq = helpreq;
85 : 0 : jreq.mode = JSONRPCRequest::GET_HELP;
86 : 0 : jreq.params = UniValue();
87 : :
88 [ # # ]: 0 : for (const std::pair<std::string, const CRPCCommand*>& command : vCommands)
89 : : {
90 : 0 : const CRPCCommand *pcmd = command.second;
91 [ # # ]: 0 : std::string strMethod = pcmd->name;
92 [ # # # # : 0 : if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
# # ]
93 : 0 : continue;
94 [ # # ]: 0 : jreq.strMethod = strMethod;
95 : 0 : try
96 : : {
97 [ # # ]: 0 : UniValue unused_result;
98 [ # # # # ]: 0 : if (setDone.insert(pcmd->unique_id).second)
99 [ # # ]: 0 : pcmd->actor(jreq, unused_result, /*last_handler=*/true);
100 : 0 : }
101 [ - - ]: 0 : catch (const std::exception& e)
102 : : {
103 : : // Help text is returned in an exception
104 [ - - ]: 0 : std::string strHelp = std::string(e.what());
105 [ - - ]: 0 : if (strCommand == "")
106 : : {
107 [ - - ]: 0 : if (strHelp.find('\n') != std::string::npos)
108 [ - - ]: 0 : strHelp = strHelp.substr(0, strHelp.find('\n'));
109 : :
110 [ - - ]: 0 : if (category != pcmd->category)
111 : : {
112 [ - - ]: 0 : if (!category.empty())
113 [ - - ]: 0 : strRet += "\n";
114 [ - - ]: 0 : category = pcmd->category;
115 [ - - - - : 0 : strRet += "== " + Capitalize(category) + " ==\n";
- - - - ]
116 : : }
117 : : }
118 [ - - ]: 0 : strRet += strHelp + "\n";
119 : 0 : }
120 : 0 : }
121 [ # # ]: 0 : if (strRet == "")
122 [ # # ]: 0 : strRet = strprintf("help: unknown command: %s\n", strCommand);
123 [ # # ]: 0 : strRet = strRet.substr(0,strRet.size()-1);
124 : 0 : return strRet;
125 : 0 : }
126 : :
127 : 268 : static RPCHelpMan help()
128 : : {
129 : 268 : return RPCHelpMan{
130 : : "help",
131 : : "List all commands, or get help for a specified command.\n",
132 : : {
133 [ + - ]: 536 : {"command", RPCArg::Type::STR, RPCArg::DefaultHint{"all commands"}, "The command to get help on"},
134 : : },
135 : : {
136 [ + - + - : 536 : RPCResult{RPCResult::Type::STR, "", "The help text"},
+ - ]
137 [ + - + - : 536 : RPCResult{RPCResult::Type::ANY, "", ""},
+ - ]
138 : : },
139 [ + - + - ]: 804 : RPCExamples{""},
140 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
141 : : {
142 [ # # ]: 0 : std::string strCommand;
143 [ # # ]: 0 : if (jsonRequest.params.size() > 0) {
144 [ # # # # : 0 : strCommand = jsonRequest.params[0].get_str();
# # ]
145 : : }
146 [ # # ]: 0 : if (strCommand == "dump_all_command_conversions") {
147 : : // Used for testing only, undocumented
148 [ # # ]: 0 : return tableRPC.dumpArgMap(jsonRequest);
149 : : }
150 : :
151 [ # # # # ]: 0 : return tableRPC.help(strCommand, jsonRequest);
152 : 0 : },
153 [ + - + - : 3752 : };
+ - + - +
- + - + -
+ + + + -
- - - ]
154 [ + - + - : 1608 : }
+ - + - +
- - - ]
155 : :
156 : 268 : static RPCHelpMan stop()
157 : : {
158 [ + + + - : 268 : static const std::string RESULT{CLIENT_NAME " stopping"};
+ - ]
159 : 268 : return RPCHelpMan{
160 : : "stop",
161 : : // Also accept the hidden 'wait' integer argument (milliseconds)
162 : : // For instance, 'stop 1000' makes the call wait 1 second before returning
163 : : // to the client (intended for testing)
164 : : "Request a graceful shutdown of " CLIENT_NAME ".",
165 : : {
166 [ + - + - ]: 536 : {"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "how long to wait in ms", RPCArgOptions{.hidden=true}},
167 : : },
168 [ + - + - : 804 : RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
+ - ]
169 [ + - + - ]: 804 : RPCExamples{""},
170 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
171 : : {
172 : : // Event loop will exit after current HTTP requests have been handled, so
173 : : // this reply will get back to the client.
174 : 0 : CHECK_NONFATAL((CHECK_NONFATAL(EnsureAnyNodeContext(jsonRequest.context).shutdown_request))());
175 [ # # ]: 0 : if (jsonRequest.params[0].isNum()) {
176 : 0 : UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].getInt<int>()});
177 : : }
178 : 0 : return RESULT;
179 : : },
180 [ + - + - : 2412 : };
+ - + - +
- + - + +
- - ]
181 [ + - + - ]: 536 : }
182 : :
183 : 268 : static RPCHelpMan uptime()
184 : : {
185 : 268 : return RPCHelpMan{
186 : : "uptime",
187 : : "Returns the total uptime of the server.\n",
188 : : {},
189 : 0 : RPCResult{
190 : : RPCResult::Type::NUM, "", "The number of seconds that the server has been running"
191 [ + - + - : 536 : },
+ - ]
192 : 268 : RPCExamples{
193 [ + - + - : 536 : HelpExampleCli("uptime", "")
+ - ]
194 [ + - + - : 1072 : + HelpExampleRpc("uptime", "")
+ - + - ]
195 [ + - ]: 268 : },
196 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
197 : : {
198 : 0 : return GetTime() - GetStartupTime();
199 : : }
200 [ + - + - : 1608 : };
+ - + - ]
201 : : }
202 : :
203 : 268 : static RPCHelpMan getrpcinfo()
204 : : {
205 : 268 : return RPCHelpMan{
206 : : "getrpcinfo",
207 : : "Returns details of the RPC server.\n",
208 : : {},
209 : 0 : RPCResult{
210 : : RPCResult::Type::OBJ, "", "",
211 : : {
212 : : {RPCResult::Type::ARR, "active_commands", "All active commands",
213 : : {
214 : : {RPCResult::Type::OBJ, "", "Information about an active command",
215 : : {
216 : : {RPCResult::Type::STR, "method", "The name of the RPC command"},
217 : : {RPCResult::Type::NUM, "duration", "The running time in microseconds"},
218 : : }},
219 : : }},
220 : : {RPCResult::Type::STR, "logpath", "The complete file path to the debug log"},
221 : : }
222 [ + - + - : 2412 : },
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ + + + +
- - - - -
- ]
223 : 268 : RPCExamples{
224 [ + - + - : 536 : HelpExampleCli("getrpcinfo", "")
+ - ]
225 [ + - + - : 1340 : + HelpExampleRpc("getrpcinfo", "")},
+ - + - +
- ]
226 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
227 : : {
228 : 0 : LOCK(g_rpc_server_info.mutex);
229 : 0 : UniValue active_commands(UniValue::VARR);
230 [ # # ]: 0 : for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
231 : 0 : UniValue entry(UniValue::VOBJ);
232 [ # # # # : 0 : entry.pushKV("method", info.method);
# # ]
233 [ # # # # : 0 : entry.pushKV("duration", int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - info.start)});
# # ]
234 [ # # ]: 0 : active_commands.push_back(std::move(entry));
235 : 0 : }
236 : :
237 : 0 : UniValue result(UniValue::VOBJ);
238 [ # # # # ]: 0 : result.pushKV("active_commands", std::move(active_commands));
239 : :
240 [ # # # # ]: 0 : const std::string path = LogInstance().m_file_path.utf8string();
241 [ # # ]: 0 : UniValue log_path(UniValue::VSTR, path);
242 [ # # # # ]: 0 : result.pushKV("logpath", std::move(log_path));
243 : :
244 : 0 : return result;
245 [ # # ]: 0 : }
246 [ + - + - : 1608 : };
+ - + - ]
247 [ + - + - : 1608 : }
+ - + - +
- + - - -
- - ]
248 : :
249 : : static const CRPCCommand vRPCCommands[]{
250 : : /* Overall control/query calls */
251 : : {"control", &getrpcinfo},
252 : : {"control", &help},
253 : : {"control", &stop},
254 : : {"control", &uptime},
255 : : };
256 : :
257 : 146 : CRPCTable::CRPCTable()
258 : : {
259 [ + + ]: 730 : for (const auto& c : vRPCCommands) {
260 [ + - ]: 584 : appendCommand(c.name, &c);
261 : : }
262 : 146 : }
263 : :
264 : 19764 : void CRPCTable::appendCommand(const std::string& name, const CRPCCommand* pcmd)
265 : : {
266 : 19764 : CHECK_NONFATAL(!IsRPCRunning()); // Only add commands before rpc is running
267 : :
268 : 19764 : mapCommands[name].push_back(pcmd);
269 : 19764 : }
270 : :
271 : 1003 : bool CRPCTable::removeCommand(const std::string& name, const CRPCCommand* pcmd)
272 : : {
273 : 1003 : auto it = mapCommands.find(name);
274 [ + - ]: 1003 : if (it != mapCommands.end()) {
275 : 1003 : auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
276 [ + - ]: 1003 : if (it->second.end() != new_end) {
277 : 1003 : it->second.erase(new_end, it->second.end());
278 : 1003 : return true;
279 : : }
280 : : }
281 : : return false;
282 : : }
283 : :
284 : 0 : void StartRPC()
285 : : {
286 [ # # ]: 0 : LogDebug(BCLog::RPC, "Starting RPC\n");
287 : 0 : g_rpc_running = true;
288 : 0 : }
289 : :
290 : 0 : void InterruptRPC()
291 : : {
292 : 0 : static std::once_flag g_rpc_interrupt_flag;
293 : : // This function could be called twice if the GUI has been started with -server=1.
294 : 0 : std::call_once(g_rpc_interrupt_flag, []() {
295 [ # # ]: 0 : LogDebug(BCLog::RPC, "Interrupting RPC\n");
296 : : // Interrupt e.g. running longpolls
297 : 0 : g_rpc_running = false;
298 : 0 : });
299 : 0 : }
300 : :
301 : 0 : void StopRPC()
302 : : {
303 : 0 : static std::once_flag g_rpc_stop_flag;
304 : : // This function could be called twice if the GUI has been started with -server=1.
305 [ # # ]: 0 : assert(!g_rpc_running);
306 : 0 : std::call_once(g_rpc_stop_flag, [&]() {
307 [ # # ]: 0 : LogDebug(BCLog::RPC, "Stopping RPC\n");
308 [ # # ]: 0 : WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
309 : 0 : DeleteAuthCookie();
310 [ # # ]: 0 : LogDebug(BCLog::RPC, "RPC stopped.\n");
311 : 0 : });
312 : 0 : }
313 : :
314 : 19764 : bool IsRPCRunning()
315 : : {
316 : 19764 : return g_rpc_running;
317 : : }
318 : :
319 : 0 : void RpcInterruptionPoint()
320 : : {
321 [ # # # # : 0 : if (!IsRPCRunning()) throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
# # ]
322 : 0 : }
323 : :
324 : 0 : void SetRPCWarmupStatus(const std::string& newStatus)
325 : : {
326 : 0 : LOCK(g_rpc_warmup_mutex);
327 [ # # # # ]: 0 : rpcWarmupStatus = newStatus;
328 : 0 : }
329 : :
330 : 1 : void SetRPCWarmupFinished()
331 : : {
332 : 1 : LOCK(g_rpc_warmup_mutex);
333 [ - + ]: 1 : assert(fRPCInWarmup);
334 [ + - ]: 1 : fRPCInWarmup = false;
335 : 1 : }
336 : :
337 : 67 : bool RPCIsInWarmup(std::string *outStatus)
338 : : {
339 : 67 : LOCK(g_rpc_warmup_mutex);
340 [ - + ]: 67 : if (outStatus)
341 [ # # ]: 0 : *outStatus = rpcWarmupStatus;
342 [ + - ]: 67 : return fRPCInWarmup;
343 : 67 : }
344 : :
345 : 264 : bool IsDeprecatedRPCEnabled(const std::string& method)
346 : : {
347 [ + - ]: 264 : const std::vector<std::string> enabled_methods = gArgs.GetArgs("-deprecatedrpc");
348 : :
349 : 264 : return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
350 : 264 : }
351 : :
352 : 0 : UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors)
353 : : {
354 [ # # ]: 0 : UniValue result;
355 [ # # ]: 0 : if (catch_errors) {
356 : 0 : try {
357 [ # # ]: 0 : result = tableRPC.execute(jreq);
358 [ - - - ]: 0 : } catch (UniValue& e) {
359 [ - - - - : 0 : return JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version);
- - ]
360 : 0 : } catch (const std::exception& e) {
361 [ - - - - : 0 : return JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_MISC_ERROR, e.what()), jreq.id, jreq.m_json_version);
- - - - -
- ]
362 : 0 : }
363 : : } else {
364 [ # # ]: 0 : result = tableRPC.execute(jreq);
365 : : }
366 : :
367 [ # # # # : 0 : return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id, jreq.m_json_version);
# # ]
368 : 0 : }
369 : :
370 : : /**
371 : : * Process named arguments into a vector of positional arguments, based on the
372 : : * passed-in specification for the RPC call's arguments.
373 : : */
374 : 11 : static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, const std::vector<std::pair<std::string, bool>>& argNames)
375 : : {
376 : 11 : JSONRPCRequest out = in;
377 : 11 : out.params = UniValue(UniValue::VARR);
378 : : // Build a map of parameters, and remove ones that have been processed, so that we can throw a focused error if
379 : : // there is an unknown one.
380 [ + - ]: 11 : const std::vector<std::string>& keys = in.params.getKeys();
381 [ + - ]: 11 : const std::vector<UniValue>& values = in.params.getValues();
382 : 11 : std::unordered_map<std::string, const UniValue*> argsIn;
383 [ + + ]: 38 : for (size_t i=0; i<keys.size(); ++i) {
384 [ + - + + ]: 28 : auto [_, inserted] = argsIn.emplace(keys[i], &values[i]);
385 [ + + ]: 28 : if (!inserted) {
386 [ + - + - ]: 3 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + keys[i] + " specified multiple times");
387 : : }
388 : : }
389 : : // Process expected parameters. If any parameters were left unspecified in
390 : : // the request before a parameter that was specified, null values need to be
391 : : // inserted at the unspecified parameter positions, and the "hole" variable
392 : : // below tracks the number of null values that need to be inserted.
393 : : // The "initial_hole_size" variable stores the size of the initial hole,
394 : : // i.e. how many initial positional arguments were left unspecified. This is
395 : : // used after the for-loop to add initial positional arguments from the
396 : : // "args" parameter, if present.
397 : 10 : int hole = 0;
398 : 10 : int initial_hole_size = 0;
399 : 10 : const std::string* initial_param = nullptr;
400 : 10 : UniValue options{UniValue::VOBJ};
401 [ + - + + ]: 59 : for (const auto& [argNamePattern, named_only]: argNames) {
402 [ + - ]: 50 : std::vector<std::string> vargNames = SplitString(argNamePattern, '|');
403 : 50 : auto fr = argsIn.end();
404 [ + + ]: 79 : for (const std::string & argName : vargNames) {
405 : 50 : fr = argsIn.find(argName);
406 [ + + ]: 50 : if (fr != argsIn.end()) {
407 : : break;
408 : : }
409 : : }
410 : :
411 : : // Handle named-only parameters by pushing them into a temporary options
412 : : // object, and then pushing the accumulated options as the next
413 : : // positional argument.
414 [ + + ]: 50 : if (named_only) {
415 [ + + ]: 10 : if (fr != argsIn.end()) {
416 [ + - - + ]: 4 : if (options.exists(fr->first)) {
417 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + fr->first + " specified multiple times");
418 : : }
419 [ + - + - : 8 : options.pushKVEnd(fr->first, *fr->second);
+ - ]
420 : 4 : argsIn.erase(fr);
421 : : }
422 : 10 : continue;
423 : : }
424 : :
425 [ + + + + ]: 40 : if (!options.empty() || fr != argsIn.end()) {
426 [ + + ]: 29 : for (int i = 0; i < hole; ++i) {
427 : : // Fill hole between specified parameters with JSON nulls,
428 : : // but not at the end (for backwards compatibility with calls
429 : : // that act based on number of specified parameters).
430 [ + - ]: 10 : out.params.push_back(UniValue());
431 : : }
432 : 19 : hole = 0;
433 [ + + ]: 19 : if (!initial_param) initial_param = &argNamePattern;
434 : : } else {
435 : 21 : hole += 1;
436 [ + + ]: 21 : if (out.params.empty()) initial_hole_size = hole;
437 : : }
438 : :
439 : : // If named input parameter "fr" is present, push it onto out.params. If
440 : : // options are present, push them onto out.params. If both are present,
441 : : // throw an error.
442 [ + + ]: 40 : if (fr != argsIn.end()) {
443 [ + + ]: 17 : if (!options.empty()) {
444 [ + - + - : 3 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + fr->first + " conflicts with parameter " + options.getKeys().front());
+ - + - ]
445 : : }
446 [ + - + - ]: 16 : out.params.push_back(*fr->second);
447 : 16 : argsIn.erase(fr);
448 : : }
449 [ + + ]: 39 : if (!options.empty()) {
450 [ + - ]: 2 : out.params.push_back(std::move(options));
451 : 2 : options = UniValue{UniValue::VOBJ};
452 : : }
453 : 50 : }
454 : : // If leftover "args" param was found, use it as a source of positional
455 : : // arguments and add named arguments after. This is a convenience for
456 : : // clients that want to pass a combination of named and positional
457 : : // arguments as described in doc/JSON-RPC-interface.md#parameter-passing
458 [ + - ]: 18 : auto positional_args{argsIn.extract("args")};
459 [ + + + - ]: 9 : if (positional_args && positional_args.mapped()->isArray()) {
460 [ + + + + ]: 4 : if (initial_hole_size < (int)positional_args.mapped()->size() && initial_param) {
461 [ + - + - ]: 6 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + *initial_param + " specified twice both as positional and named argument");
462 : : }
463 : : // Assign positional_args to out.params and append named_args after.
464 : 2 : UniValue named_args{std::move(out.params)};
465 [ + - ]: 2 : out.params = *positional_args.mapped();
466 [ + + ]: 5 : for (size_t i{out.params.size()}; i < named_args.size(); ++i) {
467 [ + - + - : 3 : out.params.push_back(named_args[i]);
+ - ]
468 : : }
469 : 2 : }
470 : : // If there are still arguments in the argsIn map, this is an error.
471 [ + + ]: 7 : if (!argsIn.empty()) {
472 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown named parameter " + argsIn.begin()->first);
473 : : }
474 : : // Return request with named arguments transformed to positional arguments
475 [ + + ]: 6 : return out;
476 : 15 : }
477 : :
478 : 67 : static bool ExecuteCommands(const std::vector<const CRPCCommand*>& commands, const JSONRPCRequest& request, UniValue& result)
479 : : {
480 [ + - ]: 67 : for (const auto& command : commands) {
481 [ - + ]: 67 : if (ExecuteCommand(*command, request, result, &command == &commands.back())) {
482 : : return true;
483 : : }
484 : : }
485 : : return false;
486 : : }
487 : :
488 : 67 : UniValue CRPCTable::execute(const JSONRPCRequest &request) const
489 : : {
490 : : // Return immediately if in warmup
491 : 67 : {
492 : 67 : LOCK(g_rpc_warmup_mutex);
493 [ - + ]: 67 : if (fRPCInWarmup)
494 [ # # ]: 0 : throw JSONRPCError(RPC_IN_WARMUP, rpcWarmupStatus);
495 : 0 : }
496 : :
497 : : // Find method
498 : 67 : auto it = mapCommands.find(request.strMethod);
499 [ + - ]: 67 : if (it != mapCommands.end()) {
500 [ + + ]: 67 : UniValue result;
501 [ + + - + ]: 67 : if (ExecuteCommands(it->second, request, result)) {
502 : 43 : return result;
503 : : }
504 : 24 : }
505 [ # # # # ]: 0 : throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
506 : : }
507 : :
508 : 67 : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler)
509 : : {
510 : 67 : try {
511 [ + - ]: 67 : RPCCommandExecution execution(request.strMethod);
512 : : // Execute, convert arguments to array if necessary
513 [ + + ]: 67 : if (request.params.isObject()) {
514 [ + + + - ]: 11 : return command.actor(transformNamedArguments(request, command.argNames), result, last_handler);
515 : : } else {
516 [ + + ]: 56 : return command.actor(request, result, last_handler);
517 : : }
518 [ + - + ]: 91 : } catch (const UniValue::type_error& e) {
519 [ - - - - ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, e.what());
520 : 6 : } catch (const std::exception& e) {
521 [ + - + - ]: 12 : throw JSONRPCError(RPC_MISC_ERROR, e.what());
522 : 6 : }
523 : : }
524 : :
525 : 0 : std::vector<std::string> CRPCTable::listCommands() const
526 : : {
527 : 0 : std::vector<std::string> commandList;
528 [ # # ]: 0 : commandList.reserve(mapCommands.size());
529 [ # # # # ]: 0 : for (const auto& i : mapCommands) commandList.emplace_back(i.first);
530 : 0 : return commandList;
531 : 0 : }
532 : :
533 : 0 : UniValue CRPCTable::dumpArgMap(const JSONRPCRequest& args_request) const
534 : : {
535 : 0 : JSONRPCRequest request = args_request;
536 : 0 : request.mode = JSONRPCRequest::GET_ARGS;
537 : :
538 : 0 : UniValue ret{UniValue::VARR};
539 [ # # ]: 0 : for (const auto& cmd : mapCommands) {
540 [ # # ]: 0 : UniValue result;
541 [ # # # # ]: 0 : if (ExecuteCommands(cmd.second, request, result)) {
542 [ # # # # ]: 0 : for (const auto& values : result.getValues()) {
543 [ # # # # ]: 0 : ret.push_back(values);
544 : : }
545 : : }
546 : 0 : }
547 : 0 : return ret;
548 : 0 : }
549 : :
550 : 0 : void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
551 : : {
552 [ # # ]: 0 : if (!timerInterface)
553 : 0 : timerInterface = iface;
554 : 0 : }
555 : :
556 : 0 : void RPCSetTimerInterface(RPCTimerInterface *iface)
557 : : {
558 : 0 : timerInterface = iface;
559 : 0 : }
560 : :
561 : 0 : void RPCUnsetTimerInterface(RPCTimerInterface *iface)
562 : : {
563 [ # # ]: 0 : if (timerInterface == iface)
564 : 0 : timerInterface = nullptr;
565 : 0 : }
566 : :
567 : 0 : void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds)
568 : : {
569 [ # # ]: 0 : if (!timerInterface)
570 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC");
571 : 0 : LOCK(g_deadline_timers_mutex);
572 : 0 : deadlineTimers.erase(name);
573 [ # # # # : 0 : LogDebug(BCLog::RPC, "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name());
# # # # ]
574 [ # # # # : 0 : deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000)));
# # ]
575 : 0 : }
576 : :
577 : : CRPCTable tableRPC;
|