Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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/overloaded.h>
19 : : #include <util/signalinterrupt.h>
20 : : #include <util/strencodings.h>
21 : : #include <util/string.h>
22 : : #include <util/time.h>
23 : : #include <validation.h>
24 : :
25 : : #include <algorithm>
26 : : #include <cassert>
27 : : #include <chrono>
28 : : #include <memory>
29 : : #include <mutex>
30 : : #include <span>
31 : : #include <string_view>
32 : : #include <unordered_map>
33 : : #include <unordered_set>
34 : : #include <variant>
35 : :
36 : : using util::SplitString;
37 : :
38 : : static GlobalMutex g_rpc_warmup_mutex;
39 : : static std::atomic<bool> g_rpc_running{false};
40 : : static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
41 : : static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started";
42 : : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler);
43 : :
44 : 134 : struct RPCCommandExecutionInfo
45 : : {
46 : : std::string method;
47 : : SteadyClock::time_point start;
48 : : };
49 : :
50 : : struct RPCServerInfo
51 : : {
52 : : Mutex mutex;
53 : : std::list<RPCCommandExecutionInfo> active_commands GUARDED_BY(mutex);
54 : : };
55 : :
56 : : static RPCServerInfo g_rpc_server_info;
57 : :
58 : : struct RPCCommandExecution
59 : : {
60 : : std::list<RPCCommandExecutionInfo>::iterator it;
61 : 67 : explicit RPCCommandExecution(const std::string& method)
62 : 67 : {
63 : 67 : LOCK(g_rpc_server_info.mutex);
64 [ - + + - : 134 : it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, SteadyClock::now()});
+ - ]
65 [ + - ]: 134 : }
66 : 67 : ~RPCCommandExecution()
67 : : {
68 : 67 : LOCK(g_rpc_server_info.mutex);
69 [ + - ]: 67 : g_rpc_server_info.active_commands.erase(it);
70 : 67 : }
71 : : };
72 : :
73 : 0 : std::string CRPCTable::help(std::string_view strCommand, const JSONRPCRequest& helpreq) const
74 : : {
75 [ # # ]: 0 : std::string strRet;
76 : 0 : std::string category;
77 [ # # ]: 0 : std::set<intptr_t> setDone;
78 : 0 : std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
79 [ # # ]: 0 : vCommands.reserve(mapCommands.size());
80 : :
81 [ # # ]: 0 : for (const auto& entry : mapCommands)
82 [ # # # # ]: 0 : vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
83 : 0 : std::ranges::sort(vCommands);
84 : :
85 [ # # ]: 0 : JSONRPCRequest jreq = helpreq;
86 : 0 : jreq.mode = JSONRPCRequest::GET_HELP;
87 : 0 : jreq.params = UniValue();
88 : :
89 [ # # # # ]: 0 : for (const auto& [_, pcmd] : vCommands) {
90 [ # # ]: 0 : std::string strMethod = pcmd->name;
91 [ # # # # : 0 : if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
# # ]
92 : 0 : continue;
93 [ # # ]: 0 : jreq.strMethod = strMethod;
94 : 0 : try
95 : : {
96 [ # # ]: 0 : UniValue unused_result;
97 [ # # # # ]: 0 : if (setDone.insert(pcmd->unique_id).second)
98 [ # # ]: 0 : pcmd->actor(jreq, unused_result, /*last_handler=*/true);
99 [ # # ]: 0 : } catch (const HelpResult& e) {
100 [ - - ]: 0 : std::string strHelp{e.what()};
101 [ - - ]: 0 : if (strCommand == "")
102 : : {
103 [ - - ]: 0 : if (strHelp.find('\n') != std::string::npos)
104 [ - - ]: 0 : strHelp = strHelp.substr(0, strHelp.find('\n'));
105 : :
106 [ - - ]: 0 : if (category != pcmd->category)
107 : : {
108 [ - - ]: 0 : if (!category.empty())
109 [ - - ]: 0 : strRet += "\n";
110 [ - - ]: 0 : category = pcmd->category;
111 [ - - - - : 0 : strRet += "== " + Capitalize(category) + " ==\n";
- - - - ]
112 : : }
113 : : }
114 [ - - ]: 0 : strRet += strHelp + "\n";
115 [ - - ]: 0 : }
116 : 0 : }
117 [ # # ]: 0 : if (strRet == "")
118 [ # # ]: 0 : strRet = strprintf("help: unknown command: %s\n", strCommand);
119 [ # # # # ]: 0 : strRet = strRet.substr(0,strRet.size()-1);
120 : 0 : return strRet;
121 : 0 : }
122 : :
123 : 307 : static RPCMethod help()
124 : : {
125 : 307 : return RPCMethod{
126 : 307 : "help",
127 [ + - ]: 614 : "List all commands, or get help for a specified command.\n",
128 : : {
129 [ + - + - : 921 : {"command", RPCArg::Type::STR, RPCArg::DefaultHint{"all commands"}, "The command to get help on"},
+ - ]
130 : : },
131 : : {
132 [ + - + - ]: 614 : RPCResult{RPCResult::Type::STR, "", "The help text"},
133 [ + - + - ]: 614 : RPCResult{RPCResult::Type::ANY, "", "The command conversions. (Hidden in dump_all_command_conversions)", /*inner=*/{},
134 : 0 : RPCResultOptions{
135 : : .print_elision = HelpElisionSkip{},
136 : 307 : }},
137 : : },
138 [ + - ]: 614 : RPCExamples{""},
139 : 307 : [](const RPCMethod& self, const JSONRPCRequest& jsonRequest) -> UniValue
140 : : {
141 : 0 : auto command{self.MaybeArg<std::string_view>("command")};
142 [ # # ]: 0 : if (command == "dump_all_command_conversions") {
143 : : // Used for testing only, undocumented
144 : 0 : return tableRPC.dumpArgMap(jsonRequest);
145 : : }
146 : :
147 [ # # ]: 0 : return tableRPC.help(command.value_or(""), jsonRequest);
148 : : },
149 [ + - + - : 3070 : };
+ - + - +
+ + + - -
- - ]
150 [ + - + - : 1535 : }
+ - - - ]
151 : :
152 : 307 : static RPCMethod stop()
153 : : {
154 [ + + + - : 307 : static const std::string RESULT{CLIENT_NAME " stopping"};
+ - ]
155 : 307 : return RPCMethod{
156 : 307 : "stop",
157 : : // Also accept the hidden 'wait' integer argument (milliseconds)
158 : : // For instance, 'stop 1000' makes the call wait 1 second before returning
159 : : // to the client (intended for testing)
160 [ + - ]: 614 : "Request a graceful shutdown of " CLIENT_NAME ".",
161 : : {
162 [ + - + - : 614 : {"wait", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "how long to wait in ms", RPCArgOptions{.hidden=true}},
+ - ]
163 : : },
164 [ + - + - : 921 : RPCResult{RPCResult::Type::STR, "", "A string with the content '" + RESULT + "'"},
+ - + - ]
165 [ + - + - ]: 921 : RPCExamples{""},
166 : 307 : [](const RPCMethod& self, const JSONRPCRequest& jsonRequest) -> UniValue
167 : : {
168 : : // Event loop will exit after current HTTP requests have been handled, so
169 : : // this reply will get back to the client.
170 : 0 : CHECK_NONFATAL((CHECK_NONFATAL(EnsureAnyNodeContext(jsonRequest.context).shutdown_request))());
171 [ # # ]: 0 : if (jsonRequest.params[0].isNum()) {
172 : 0 : UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].getInt<int>()});
173 : : }
174 : 0 : return RESULT;
175 : : },
176 [ + - + - : 1535 : };
+ + - - ]
177 [ + - ]: 307 : }
178 : :
179 : 307 : static RPCMethod uptime()
180 : : {
181 : 307 : return RPCMethod{
182 : 307 : "uptime",
183 [ + - ]: 614 : "Returns the total uptime of the server.\n",
184 : : {},
185 [ + - ]: 614 : RPCResult{
186 [ + - ]: 614 : RPCResult::Type::NUM, "", "The number of seconds that the server has been running"
187 [ + - ]: 614 : },
188 : 307 : RPCExamples{
189 [ + - + - : 307 : HelpExampleCli("uptime", "")
+ - ]
190 [ + - + - : 1228 : + HelpExampleRpc("uptime", "")
+ - + - ]
191 [ + - ]: 307 : },
192 : 307 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
193 : : {
194 : 0 : return TicksSeconds(GetUptime());
195 : : }
196 [ + - + - ]: 1228 : };
197 : : }
198 : :
199 : 307 : static RPCMethod getrpcinfo()
200 : : {
201 : 307 : return RPCMethod{
202 : 307 : "getrpcinfo",
203 [ + - ]: 614 : "Returns details of the RPC server.\n",
204 : : {},
205 [ + - ]: 614 : RPCResult{
206 [ + - ]: 614 : RPCResult::Type::OBJ, "", "",
207 : : {
208 [ + - + - ]: 614 : {RPCResult::Type::ARR, "active_commands", "All active commands",
209 : : {
210 [ + - + - ]: 614 : {RPCResult::Type::OBJ, "", "Information about an active command",
211 : : {
212 [ + - + - ]: 614 : {RPCResult::Type::STR, "method", "The name of the RPC command"},
213 [ + - + - ]: 614 : {RPCResult::Type::NUM, "duration", "The running time in microseconds"},
214 : : }},
215 : : }},
216 [ + - + - ]: 614 : {RPCResult::Type::STR, "logpath", "The complete file path to the debug log"},
217 : : }
218 [ + - + - : 4298 : },
+ - + - +
+ + + + +
- - - - -
- ]
219 : 307 : RPCExamples{
220 [ + - + - : 614 : HelpExampleCli("getrpcinfo", "")
+ - ]
221 [ + - + - : 1535 : + HelpExampleRpc("getrpcinfo", "")},
+ - + - +
- ]
222 : 307 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
223 : : {
224 : 0 : LOCK(g_rpc_server_info.mutex);
225 : 0 : UniValue active_commands(UniValue::VARR);
226 [ # # ]: 0 : for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
227 : 0 : UniValue entry(UniValue::VOBJ);
228 [ # # # # : 0 : entry.pushKV("method", info.method);
# # ]
229 [ # # # # : 0 : entry.pushKV("duration", Ticks<std::chrono::microseconds>(SteadyClock::now() - info.start));
# # ]
230 [ # # ]: 0 : active_commands.push_back(std::move(entry));
231 : 0 : }
232 : :
233 : 0 : UniValue result(UniValue::VOBJ);
234 [ # # # # ]: 0 : result.pushKV("active_commands", std::move(active_commands));
235 : :
236 [ # # # # ]: 0 : const std::string path = LogInstance().m_file_path.utf8string();
237 [ # # ]: 0 : UniValue log_path(UniValue::VSTR, path);
238 [ # # # # ]: 0 : result.pushKV("logpath", std::move(log_path));
239 : :
240 : 0 : return result;
241 [ # # ]: 0 : }
242 [ + - + - ]: 1228 : };
243 [ + - + - : 3070 : }
+ - + - +
- - - -
- ]
244 : :
245 : : namespace {
246 : : UniValue OpenRPCArgSchema(const RPCArg& arg, bool include_hidden);
247 : : UniValue OpenRPCResultSchema(const RPCResult& result);
248 : :
249 : 36 : UniValue MakeObject(std::initializer_list<std::pair<std::string, UniValue>> entries)
250 : : {
251 : 36 : UniValue obj{UniValue::VOBJ};
252 [ + - + + ]: 72 : for (const auto& [key, value] : entries) {
253 [ + - + - ]: 108 : obj.pushKV(key, value);
254 : : }
255 : 36 : return obj;
256 : 0 : }
257 : :
258 : 0 : void PushUniqueSchema(UniValue& schemas, std::unordered_set<std::string>& seen, UniValue schema)
259 : : {
260 : 0 : const std::string serialized{schema.write()};
261 [ # # # # : 0 : if (seen.insert(serialized).second) schemas.push_back(std::move(schema));
# # ]
262 : 0 : }
263 : :
264 : : // NOLINTNEXTLINE(misc-no-recursion)
265 : 0 : UniValue DedupArrayItemsSchema(std::span<const RPCArg> inner, bool include_hidden)
266 : : {
267 [ # # ]: 0 : if (inner.empty()) return UniValue{UniValue::VOBJ};
268 [ # # # # ]: 0 : if (inner.size() == 1) return OpenRPCArgSchema(inner.front(), include_hidden);
269 : :
270 : 0 : UniValue one_of{UniValue::VARR};
271 : 0 : std::unordered_set<std::string> seen;
272 [ # # ]: 0 : for (const auto& item : inner) {
273 [ # # # # ]: 0 : PushUniqueSchema(one_of, seen, OpenRPCArgSchema(item, include_hidden));
274 : : }
275 : :
276 [ # # # # : 0 : if (one_of.size() == 1) return one_of[0];
# # # # ]
277 : :
278 : 0 : UniValue items{UniValue::VOBJ};
279 [ # # # # ]: 0 : items.pushKV("oneOf", std::move(one_of));
280 : 0 : return items;
281 : 0 : }
282 : :
283 : : // NOLINTNEXTLINE(misc-no-recursion)
284 : 7 : UniValue DedupArrayItemsSchema(std::span<const RPCResult> inner)
285 : : {
286 [ - + ]: 7 : if (inner.empty()) return UniValue{UniValue::VOBJ};
287 [ + - ]: 7 : if (inner.size() == 1) return OpenRPCResultSchema(inner.front());
288 : :
289 : 0 : UniValue one_of{UniValue::VARR};
290 : 0 : std::unordered_set<std::string> seen;
291 [ # # ]: 0 : for (const auto& item : inner) {
292 [ # # # # ]: 0 : PushUniqueSchema(one_of, seen, OpenRPCResultSchema(item));
293 : : }
294 : :
295 [ # # # # : 0 : if (one_of.size() == 1) return one_of[0];
# # # # ]
296 : :
297 : 0 : UniValue items{UniValue::VOBJ};
298 [ # # # # ]: 0 : items.pushKV("oneOf", std::move(one_of));
299 : 0 : return items;
300 : 0 : }
301 : :
302 : 2 : void ApplyTypeStrOverride(UniValue& schema, const RPCArg& arg)
303 : : {
304 [ - + - + ]: 2 : if (arg.m_opts.type_str.size() != 2) return;
305 [ # # ]: 0 : const std::string& type_label{arg.m_opts.type_str[1]};
306 [ # # ]: 0 : if (type_label.empty()) return;
307 : :
308 : 0 : static const std::unordered_set<std::string> number_or_string{
309 : : "integer / string",
310 : : "string or numeric",
311 [ # # # # : 0 : };
# # # # #
# # # # #
# # ]
312 [ # # ]: 0 : if (number_or_string.contains(type_label)) {
313 : 0 : UniValue one_of{UniValue::VARR};
314 [ # # # # : 0 : one_of.push_back(MakeObject({{"type", "number"}}));
# # # # ]
315 [ # # # # : 0 : one_of.push_back(MakeObject({{"type", "string"}}));
# # # # ]
316 : 0 : schema = UniValue{UniValue::VOBJ};
317 [ # # # # ]: 0 : schema.pushKV("oneOf", std::move(one_of));
318 : 0 : } else {
319 [ # # # # ]: 0 : schema.pushKV("x-bitcoin-type-override", type_label);
320 : : }
321 [ # # # # ]: 0 : }
322 : :
323 : 2 : void ApplyArgFallback(UniValue& schema, const RPCArg& arg)
324 : : {
325 : 2 : std::visit(util::Overloaded{
326 [ + - + - ]: 2 : [&](const RPCArg::Default& def) { schema.pushKV("default", def); },
327 [ + - + - ]: 2 : [&](const RPCArg::DefaultHint& hint) { schema.pushKV("x-bitcoin-default-hint", hint); },
328 : : [](const RPCArg::Optional&) {},
329 : : },
330 : 2 : arg.m_fallback);
331 : 2 : }
332 : :
333 : : // NOLINTNEXTLINE(misc-no-recursion)
334 : 2 : UniValue OpenRPCArgSchema(const RPCArg& arg, bool include_hidden)
335 : : {
336 : 2 : UniValue schema{UniValue::VOBJ};
337 [ - + ]: 2 : if (arg.m_opts.skip_type_check) {
338 [ # # ]: 0 : ApplyTypeStrOverride(schema, arg);
339 [ # # # # : 0 : if (schema.empty() && arg.m_type == RPCArg::Type::ARR) {
# # ]
340 : 0 : UniValue one_of{UniValue::VARR};
341 [ # # # # : 0 : one_of.push_back(MakeObject({{"type", "array"}}));
# # # # ]
342 [ # # # # : 0 : one_of.push_back(MakeObject({{"type", "object"}}));
# # # # ]
343 [ # # # # ]: 0 : schema.pushKV("oneOf", std::move(one_of));
344 : 0 : }
345 [ # # ]: 0 : ApplyArgFallback(schema, arg);
346 : : return schema;
347 : : }
348 : :
349 [ + - - + : 2 : switch (arg.m_type) {
- - - - -
- ]
350 : 1 : case RPCArg::Type::STR:
351 [ - + + + : 2 : schema = MakeObject({{"type", "string"}});
- - ]
352 : : break;
353 : 0 : case RPCArg::Type::STR_HEX:
354 [ # # # # : 0 : schema = MakeObject({{"type", "string"}, {"pattern", "^[0-9a-fA-F]+$"}});
# # ]
355 : : break;
356 : 0 : case RPCArg::Type::NUM:
357 [ # # # # : 0 : schema = MakeObject({{"type", "number"}});
# # ]
358 : : break;
359 : 1 : case RPCArg::Type::BOOL:
360 [ - + + + : 2 : schema = MakeObject({{"type", "boolean"}});
- - ]
361 : : break;
362 : 0 : case RPCArg::Type::AMOUNT: {
363 : 0 : UniValue one_of{UniValue::VARR};
364 [ # # # # : 0 : one_of.push_back(MakeObject({{"type", "number"}}));
# # # # ]
365 [ # # # # : 0 : one_of.push_back(MakeObject({{"type", "string"}}));
# # # # ]
366 [ # # # # ]: 0 : schema.pushKV("oneOf", std::move(one_of));
367 : 0 : break;
368 : 0 : }
369 : 0 : case RPCArg::Type::RANGE: {
370 : 0 : UniValue items{UniValue::VARR};
371 [ # # # # : 0 : items.push_back(MakeObject({{"type", "number"}}));
# # # # ]
372 [ # # # # : 0 : items.push_back(MakeObject({{"type", "number"}}));
# # # # ]
373 : 0 : UniValue range_schema{UniValue::VOBJ};
374 [ # # # # : 0 : range_schema.pushKV("type", "array");
# # ]
375 [ # # # # ]: 0 : range_schema.pushKV("items", std::move(items));
376 [ # # # # : 0 : range_schema.pushKV("additionalItems", false);
# # ]
377 [ # # # # : 0 : range_schema.pushKV("minItems", 2);
# # ]
378 [ # # # # : 0 : range_schema.pushKV("maxItems", 2);
# # ]
379 : 0 : UniValue one_of{UniValue::VARR};
380 [ # # # # : 0 : one_of.push_back(MakeObject({{"type", "number"}}));
# # # # ]
381 [ # # ]: 0 : one_of.push_back(std::move(range_schema));
382 [ # # # # ]: 0 : schema.pushKV("oneOf", std::move(one_of));
383 : 0 : break;
384 : 0 : }
385 : 0 : case RPCArg::Type::ARR: {
386 [ # # # # ]: 0 : UniValue items{DedupArrayItemsSchema(arg.m_inner, include_hidden)};
387 [ # # # # : 0 : schema.pushKV("type", "array");
# # ]
388 [ # # # # ]: 0 : schema.pushKV("items", std::move(items));
389 : 0 : break;
390 : 0 : }
391 : 0 : case RPCArg::Type::OBJ:
392 : 0 : case RPCArg::Type::OBJ_NAMED_PARAMS: {
393 : 0 : UniValue properties{UniValue::VOBJ};
394 : 0 : UniValue required{UniValue::VARR};
395 [ # # ]: 0 : for (const auto& inner : arg.m_inner) {
396 [ # # # # ]: 0 : if (!include_hidden && inner.m_opts.hidden) continue;
397 [ # # ]: 0 : UniValue prop{OpenRPCArgSchema(inner, include_hidden)};
398 [ # # # # : 0 : if (!inner.m_description.empty()) prop.pushKV("description", inner.m_description);
# # # # ]
399 [ # # # # : 0 : if (inner.m_opts.placeholder) prop.pushKV("x-bitcoin-placeholder", true);
# # # # ]
400 [ # # # # : 0 : if (inner.m_opts.also_positional) prop.pushKV("x-bitcoin-also-positional", true);
# # # # ]
401 [ # # # # ]: 0 : properties.pushKV(inner.GetFirstName(), std::move(prop));
402 [ # # # # : 0 : if (!inner.IsOptional()) required.push_back(inner.GetFirstName());
# # # # #
# ]
403 : 0 : }
404 [ # # # # : 0 : schema.pushKV("type", "object");
# # ]
405 [ # # # # ]: 0 : schema.pushKV("properties", std::move(properties));
406 [ # # # # : 0 : schema.pushKV("additionalProperties", false);
# # ]
407 [ # # # # : 0 : if (!required.empty()) schema.pushKV("required", std::move(required));
# # # # ]
408 : 0 : break;
409 : 0 : }
410 : 0 : case RPCArg::Type::OBJ_USER_KEYS: {
411 [ # # # # : 0 : schema.pushKV("type", "object");
# # ]
412 [ # # ]: 0 : if (!arg.m_inner.empty()) {
413 [ # # # # : 0 : schema.pushKV("additionalProperties", OpenRPCArgSchema(arg.m_inner[0], include_hidden));
# # ]
414 : : } else {
415 [ # # # # : 0 : schema.pushKV("additionalProperties", true);
# # ]
416 : : }
417 : : break;
418 : : }
419 : : } // no default case, so the compiler can warn about missing cases
420 [ + - ]: 2 : ApplyTypeStrOverride(schema, arg);
421 [ + - ]: 2 : ApplyArgFallback(schema, arg);
422 : : return schema;
423 [ - - - - : 2 : }
+ - - - -
- - - + -
- - - - -
- - - - -
- - ]
424 : :
425 : : // NOLINTNEXTLINE(misc-no-recursion)
426 : 59 : UniValue OpenRPCResultSchema(const RPCResult& result)
427 : : {
428 [ + + ]: 59 : if (result.m_opts.skip_type_check) {
429 : 2 : RPCResultOptions opts{result.m_opts};
430 : 2 : opts.skip_type_check = false;
431 [ + - ]: 2 : if (result.m_type == RPCResult::Type::OBJ) {
432 [ + - + - ]: 2 : UniValue obj_schema{OpenRPCResultSchema(RPCResult{result, std::move(opts)})};
433 [ + - ]: 2 : if (result.m_key_name.empty()) return obj_schema;
434 : :
435 : 0 : UniValue one_of{UniValue::VARR};
436 [ # # ]: 0 : one_of.push_back(std::move(obj_schema));
437 [ # # # # : 0 : one_of.push_back(MakeObject({{"const", false}}));
# # # # ]
438 : 0 : UniValue schema{UniValue::VOBJ};
439 [ # # # # ]: 0 : schema.pushKV("oneOf", std::move(one_of));
440 : 0 : return schema;
441 : 2 : }
442 [ # # # # : 0 : if (result.m_type == RPCResult::Type::ARR) return OpenRPCResultSchema(RPCResult{result, std::move(opts)});
# # ]
443 : 0 : return UniValue{UniValue::VOBJ};
444 : 2 : }
445 : :
446 [ + - - + : 57 : switch (result.m_type) {
- + - + -
+ - + - ]
447 : 26 : case RPCResult::Type::STR:
448 [ - + + + : 52 : return MakeObject({{"type", "string"}});
- - ]
449 : 0 : case RPCResult::Type::STR_AMOUNT:
450 [ # # # # : 0 : return MakeObject({{"type", "number"}, {"x-bitcoin-unit", "amount"}});
# # ]
451 : 0 : case RPCResult::Type::STR_HEX:
452 [ # # # # : 0 : return MakeObject({{"type", "string"}, {"pattern", "^[0-9a-fA-F]+$"}});
# # ]
453 : 2 : case RPCResult::Type::NUM:
454 [ - + + + : 4 : return MakeObject({{"type", "number"}});
- - ]
455 : 0 : case RPCResult::Type::NUM_TIME: {
456 : 0 : UniValue schema{UniValue::VOBJ};
457 [ # # # # : 0 : schema.pushKV("type", "number");
# # ]
458 [ # # # # : 0 : schema.pushKV("x-bitcoin-unit", "unix-time");
# # ]
459 : 0 : return schema;
460 : 0 : }
461 : 6 : case RPCResult::Type::BOOL:
462 [ - + + + : 12 : return MakeObject({{"type", "boolean"}});
- - ]
463 : 0 : case RPCResult::Type::NONE:
464 [ # # # # : 0 : return MakeObject({{"type", "null"}});
# # ]
465 : 7 : case RPCResult::Type::ARR: {
466 [ - + ]: 7 : UniValue items{DedupArrayItemsSchema(result.m_inner)};
467 : 7 : UniValue schema{UniValue::VOBJ};
468 [ + - + - : 14 : schema.pushKV("type", "array");
+ - ]
469 [ + - + - ]: 14 : schema.pushKV("items", std::move(items));
470 : 7 : return schema;
471 : 7 : }
472 : 0 : case RPCResult::Type::ARR_FIXED: {
473 : 0 : UniValue items{UniValue::VARR};
474 [ # # ]: 0 : for (const auto& inner : result.m_inner) {
475 [ # # # # ]: 0 : items.push_back(OpenRPCResultSchema(inner));
476 : : }
477 : 0 : UniValue schema{UniValue::VOBJ};
478 [ # # # # : 0 : schema.pushKV("type", "array");
# # ]
479 [ # # # # ]: 0 : schema.pushKV("items", std::move(items));
480 [ # # # # : 0 : schema.pushKV("additionalItems", false);
# # ]
481 [ # # # # : 0 : schema.pushKV("minItems", uint64_t(result.m_inner.size()));
# # # # ]
482 [ # # # # : 0 : schema.pushKV("maxItems", uint64_t(result.m_inner.size()));
# # # # ]
483 : 0 : return schema;
484 : 0 : }
485 : 12 : case RPCResult::Type::OBJ: {
486 : 12 : UniValue properties{UniValue::VOBJ};
487 : 12 : UniValue required{UniValue::VARR};
488 [ + + ]: 56 : for (const auto& inner : result.m_inner) {
489 [ - + ]: 44 : if (inner.m_key_name.empty()) continue;
490 [ + - ]: 44 : UniValue prop{OpenRPCResultSchema(inner)};
491 [ + - + - : 88 : if (!inner.m_description.empty()) prop.pushKV("description", inner.m_description);
+ - + - ]
492 [ - + + - ]: 132 : properties.pushKV(inner.m_key_name, std::move(prop));
493 [ + + + - : 44 : if (!inner.m_optional) required.push_back(inner.m_key_name);
+ - ]
494 : 44 : }
495 : 12 : UniValue schema{UniValue::VOBJ};
496 [ + - + - : 24 : schema.pushKV("type", "object");
+ - ]
497 [ + - + - ]: 24 : schema.pushKV("properties", std::move(properties));
498 [ + - + - : 24 : schema.pushKV("additionalProperties", false);
+ - ]
499 [ - + + - : 24 : if (!required.empty()) schema.pushKV("required", std::move(required));
+ - + - ]
500 : 12 : return schema;
501 : 12 : }
502 : 0 : case RPCResult::Type::OBJ_DYN: {
503 : 0 : UniValue schema{UniValue::VOBJ};
504 [ # # # # : 0 : schema.pushKV("type", "object");
# # ]
505 [ # # ]: 0 : if (!result.m_inner.empty()) {
506 [ # # # # : 0 : schema.pushKV("additionalProperties", OpenRPCResultSchema(result.m_inner[0]));
# # ]
507 : : } else {
508 [ # # # # ]: 0 : schema.pushKV("additionalProperties", UniValue{UniValue::VOBJ});
509 : : }
510 : 0 : return schema;
511 : 0 : }
512 : 4 : case RPCResult::Type::ANY:
513 : 4 : return UniValue{UniValue::VOBJ};
514 : : } // no default case, so the compiler can warn about missing cases
515 [ # # ]: 0 : NONFATAL_UNREACHABLE();
516 [ - - - - : 34 : }
- - - - -
- - - -
- ]
517 : : } // namespace
518 : :
519 : 614 : static RPCResult OpenRPCDocResult()
520 : : {
521 : 614 : return RPCResult{
522 [ + - ]: 1228 : RPCResult::Type::OBJ, "", "",
523 : : {
524 [ + - + - ]: 1228 : {RPCResult::Type::STR, "openrpc", "OpenRPC specification version."},
525 [ + - + - ]: 1228 : {RPCResult::Type::OBJ, "info", "Metadata about this JSON-RPC interface.",
526 : : {
527 [ + - + - ]: 1228 : {RPCResult::Type::STR, "title", "API title."},
528 [ + - + - ]: 1228 : {RPCResult::Type::STR, "version", "Bitcoin Core version string."},
529 [ + - + - ]: 1228 : {RPCResult::Type::STR, "description", "API description."},
530 : : }},
531 [ + - + - ]: 1228 : {RPCResult::Type::ARR, "methods", "Documented RPC methods.",
532 [ + - + - ]: 1228 : {{RPCResult::Type::OBJ, "", "An RPC method description object.",
533 : : {
534 [ + - + - ]: 1228 : {RPCResult::Type::STR, "name", "Method name."},
535 [ + - + - ]: 1228 : {RPCResult::Type::STR, "description", "Method description."},
536 [ + - + - ]: 1228 : {RPCResult::Type::ARR, "params", "Method parameters.",
537 [ + - + - ]: 1228 : {{RPCResult::Type::OBJ, "", "A parameter.",
538 : : {
539 [ + - + - ]: 1228 : {RPCResult::Type::STR, "name", "Parameter name."},
540 [ + - + - ]: 1228 : {RPCResult::Type::BOOL, "required", "Whether the parameter is required."},
541 [ + - + - ]: 1228 : {RPCResult::Type::ANY, "schema", "JSON Schema for the parameter."},
542 [ + - + - ]: 1228 : {RPCResult::Type::STR, "description", /*optional=*/true, "Parameter description."},
543 [ + - + - ]: 1228 : {RPCResult::Type::ARR, "x-bitcoin-aliases", /*optional=*/true, "Alternative parameter names.",
544 [ + - + - ]: 1228 : {{RPCResult::Type::STR, "", "An alias."}}},
545 [ + - + - ]: 1228 : {RPCResult::Type::BOOL, "x-bitcoin-placeholder", /*optional=*/true, "Whether the parameter is retained only for compatibility."},
546 [ + - + - ]: 1228 : {RPCResult::Type::BOOL, "x-bitcoin-also-positional", /*optional=*/true, "Whether the parameter can also be passed positionally."},
547 : : }}}},
548 [ + - + - ]: 1228 : {RPCResult::Type::OBJ, "result", "Method result.",
549 : : {
550 [ + - + - ]: 1228 : {RPCResult::Type::STR, "name", "Result name."},
551 [ + - + - ]: 1228 : {RPCResult::Type::ANY, "schema", "JSON Schema for the result."},
552 : : }},
553 [ + - + - ]: 1228 : {RPCResult::Type::STR, "x-bitcoin-category", "RPC category."},
554 : : }}}},
555 : : },
556 [ + - + - : 34384 : {.skip_type_check = true}};
+ - + - +
- + - + -
+ - + - +
+ + + + +
+ + + + +
+ + + + +
- - - - -
- - - - -
- - - - -
- ]
557 [ + - + - : 28244 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - ]
558 : :
559 : 307 : static RPCMethod getopenrpcinfo()
560 : : {
561 : 307 : return RPCMethod{
562 : 307 : "getopenrpcinfo",
563 [ + - ]: 614 : "Returns an OpenRPC document for currently available RPC commands.\n",
564 : : {
565 [ + - + - : 921 : {"show_hidden", RPCArg::Type::BOOL, RPCArg::Default{false}, "Also include hidden RPC commands and arguments."},
+ - ]
566 : : },
567 [ + - + - ]: 614 : OpenRPCDocResult(),
568 : 307 : RPCExamples{
569 [ + - + - : 614 : HelpExampleCli("getopenrpcinfo", "")
+ - ]
570 [ + - + - : 1228 : + HelpExampleRpc("getopenrpcinfo", "")
+ - + - ]
571 [ + - ]: 307 : },
572 : 307 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
573 : : {
574 : 0 : const bool include_hidden{self.Arg<bool>("show_hidden")};
575 : 0 : return tableRPC.buildOpenRPCDoc(include_hidden);
576 : : },
577 [ + - + - : 1535 : };
+ + - - ]
578 [ + - ]: 614 : }
579 : :
580 : 307 : static RPCMethod rpc_discover()
581 : : {
582 : 307 : return RPCMethod{
583 : 307 : "rpc.discover",
584 [ + - ]: 614 : "Returns an OpenRPC schema as a description of this service.\n",
585 : : {},
586 [ + - ]: 614 : OpenRPCDocResult(),
587 : 307 : RPCExamples{
588 [ + - + - : 614 : HelpExampleCli("rpc.discover", "")
+ - ]
589 [ + - + - : 1228 : + HelpExampleRpc("rpc.discover", "")
+ - + - ]
590 [ + - ]: 307 : },
591 : 307 : [](const RPCMethod&, const JSONRPCRequest&) -> UniValue
592 : : {
593 : 0 : return tableRPC.buildOpenRPCDoc(/*include_hidden=*/false);
594 : : },
595 [ + - + - ]: 1228 : };
596 : : }
597 : :
598 : : static const CRPCCommand vRPCCommands[]{
599 : : /* Overall control/query calls */
600 : : {"control", &getopenrpcinfo},
601 : : {"control", &rpc_discover},
602 : : {"control", &getrpcinfo},
603 : : {"control", &help},
604 : : {"control", &stop},
605 : : {"control", &uptime},
606 : : };
607 : :
608 : 166 : CRPCTable::CRPCTable()
609 : : {
610 [ + + ]: 1162 : for (const auto& c : vRPCCommands) {
611 [ + - ]: 996 : appendCommand(c.name, &c);
612 : : }
613 : 166 : }
614 : :
615 : 19764 : void CRPCTable::appendCommand(const std::string& name, const CRPCCommand* pcmd)
616 : : {
617 : 19764 : CHECK_NONFATAL(!IsRPCRunning()); // Only add commands before rpc is running
618 : :
619 : 19764 : mapCommands[name].push_back(pcmd);
620 : 19764 : }
621 : :
622 : 1045 : bool CRPCTable::removeCommand(const std::string& name, const CRPCCommand* pcmd)
623 : : {
624 : 1045 : auto it = mapCommands.find(name);
625 [ + - ]: 1045 : if (it != mapCommands.end()) {
626 : 1045 : auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
627 [ + - ]: 1045 : if (it->second.end() != new_end) {
628 : 1045 : it->second.erase(new_end, it->second.end());
629 [ + - ]: 1045 : if (it->second.empty()) {
630 : 1045 : mapCommands.erase(it);
631 : : }
632 : 1045 : return true;
633 : : }
634 : : }
635 : : return false;
636 : : }
637 : :
638 : 0 : void StartRPC()
639 : : {
640 [ # # ]: 0 : LogDebug(BCLog::RPC, "Starting RPC\n");
641 : 0 : g_rpc_running = true;
642 : 0 : }
643 : :
644 : 1 : void InterruptRPC()
645 : : {
646 : 1 : static std::once_flag g_rpc_interrupt_flag;
647 : : // This function could be called twice if the GUI has been started with -server=1.
648 : 1 : std::call_once(g_rpc_interrupt_flag, []() {
649 [ + - ]: 1 : LogDebug(BCLog::RPC, "Interrupting RPC\n");
650 : : // Interrupt e.g. running longpolls
651 : 1 : g_rpc_running = false;
652 : 1 : });
653 : 1 : }
654 : :
655 : 1 : void StopRPC()
656 : : {
657 : 1 : static std::once_flag g_rpc_stop_flag;
658 : : // This function could be called twice if the GUI has been started with -server=1.
659 [ - + ]: 1 : assert(!g_rpc_running);
660 : 1 : std::call_once(g_rpc_stop_flag, [&]() {
661 [ + - ]: 1 : LogDebug(BCLog::RPC, "Stopping RPC\n");
662 : 1 : DeleteAuthCookie();
663 [ + - ]: 1 : LogDebug(BCLog::RPC, "RPC stopped.\n");
664 : 1 : });
665 : 1 : }
666 : :
667 : 19764 : bool IsRPCRunning()
668 : : {
669 : 19764 : return g_rpc_running;
670 : : }
671 : :
672 : 0 : void RpcInterruptionPoint()
673 : : {
674 [ # # # # : 0 : if (!IsRPCRunning()) throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
# # ]
675 : 0 : }
676 : :
677 : 0 : void SetRPCWarmupStatus(const std::string& newStatus)
678 : : {
679 : 0 : LOCK(g_rpc_warmup_mutex);
680 [ # # # # ]: 0 : rpcWarmupStatus = newStatus;
681 : 0 : }
682 : :
683 : 707 : void SetRPCWarmupStarting()
684 : : {
685 : 707 : LOCK(g_rpc_warmup_mutex);
686 [ + - ]: 707 : fRPCInWarmup = true;
687 : 707 : }
688 : :
689 : 8 : void SetRPCWarmupFinished()
690 : : {
691 : 8 : LOCK(g_rpc_warmup_mutex);
692 [ - + ]: 8 : assert(fRPCInWarmup);
693 [ + - ]: 8 : fRPCInWarmup = false;
694 : 8 : }
695 : :
696 : 67 : bool RPCIsInWarmup(std::string *outStatus)
697 : : {
698 : 67 : LOCK(g_rpc_warmup_mutex);
699 [ - + ]: 67 : if (outStatus)
700 [ # # ]: 0 : *outStatus = rpcWarmupStatus;
701 [ + - ]: 67 : return fRPCInWarmup;
702 : 67 : }
703 : :
704 : 662 : bool IsDeprecatedRPCEnabled(const std::string& method)
705 : : {
706 [ + - ]: 662 : const std::vector<std::string> enabled_methods = gArgs.GetArgs("-deprecatedrpc");
707 : :
708 : 662 : return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
709 : 662 : }
710 : :
711 : 0 : UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors)
712 : : {
713 [ # # ]: 0 : UniValue result;
714 [ # # ]: 0 : if (catch_errors) {
715 : 0 : try {
716 [ # # ]: 0 : result = tableRPC.execute(jreq);
717 [ - - - ]: 0 : } catch (UniValue& e) {
718 [ - - - - : 0 : return JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version);
- - ]
719 : 0 : } catch (const std::exception& e) {
720 [ - - - - : 0 : return JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_MISC_ERROR, e.what()), jreq.id, jreq.m_json_version);
- - - - -
- ]
721 : 0 : }
722 : : } else {
723 [ # # ]: 0 : result = tableRPC.execute(jreq);
724 : : }
725 : :
726 [ # # # # : 0 : return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id, jreq.m_json_version);
# # ]
727 : 0 : }
728 : :
729 : : /**
730 : : * Process named arguments into a vector of positional arguments, based on the
731 : : * passed-in specification for the RPC call's arguments.
732 : : */
733 : 11 : static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, const std::vector<std::pair<std::string, bool>>& argNames)
734 : : {
735 : 11 : JSONRPCRequest out = in;
736 : 11 : out.params = UniValue(UniValue::VARR);
737 : : // Build a map of parameters, and remove ones that have been processed, so that we can throw a focused error if
738 : : // there is an unknown one.
739 [ + - ]: 11 : const std::vector<std::string>& keys = in.params.getKeys();
740 [ + - ]: 11 : const std::vector<UniValue>& values = in.params.getValues();
741 : 11 : std::unordered_map<std::string, const UniValue*> argsIn;
742 [ - + + + ]: 38 : for (size_t i=0; i<keys.size(); ++i) {
743 [ + - + + ]: 28 : auto [_, inserted] = argsIn.emplace(keys[i], &values[i]);
744 [ + + ]: 28 : if (!inserted) {
745 [ + - + - ]: 3 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + keys[i] + " specified multiple times");
746 : : }
747 : : }
748 : : // Process expected parameters. If any parameters were left unspecified in
749 : : // the request before a parameter that was specified, null values need to be
750 : : // inserted at the unspecified parameter positions, and the "hole" variable
751 : : // below tracks the number of null values that need to be inserted.
752 : : // The "initial_hole_size" variable stores the size of the initial hole,
753 : : // i.e. how many initial positional arguments were left unspecified. This is
754 : : // used after the for-loop to add initial positional arguments from the
755 : : // "args" parameter, if present.
756 : 10 : int hole = 0;
757 : 10 : int initial_hole_size = 0;
758 : 10 : const std::string* initial_param = nullptr;
759 : 10 : UniValue options{UniValue::VOBJ};
760 [ - + + + ]: 59 : for (const auto& [argNamePattern, named_only]: argNames) {
761 [ - + + - ]: 50 : std::vector<std::string> vargNames = SplitString(argNamePattern, '|');
762 : 50 : auto fr = argsIn.end();
763 [ + + ]: 79 : for (const std::string & argName : vargNames) {
764 : 50 : fr = argsIn.find(argName);
765 [ + + ]: 50 : if (fr != argsIn.end()) {
766 : : break;
767 : : }
768 : : }
769 : :
770 : : // Handle named-only parameters by pushing them into a temporary options
771 : : // object, and then pushing the accumulated options as the next
772 : : // positional argument.
773 [ + + ]: 50 : if (named_only) {
774 [ + + ]: 10 : if (fr != argsIn.end()) {
775 [ + - - + ]: 4 : if (options.exists(fr->first)) {
776 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + fr->first + " specified multiple times");
777 : : }
778 [ + - - + : 12 : options.pushKVEnd(fr->first, *fr->second);
+ - ]
779 : 4 : argsIn.erase(fr);
780 : : }
781 : 10 : continue;
782 : : }
783 : :
784 [ - + + + : 40 : if (!options.empty() || fr != argsIn.end()) {
+ + ]
785 [ + + ]: 29 : for (int i = 0; i < hole; ++i) {
786 : : // Fill hole between specified parameters with JSON nulls,
787 : : // but not at the end (for backwards compatibility with calls
788 : : // that act based on number of specified parameters).
789 [ + - ]: 10 : out.params.push_back(UniValue());
790 : : }
791 : 19 : hole = 0;
792 [ + + ]: 19 : if (!initial_param) initial_param = &argNamePattern;
793 : : } else {
794 : 21 : hole += 1;
795 [ - + + + ]: 21 : if (out.params.empty()) initial_hole_size = hole;
796 : : }
797 : :
798 : : // If named input parameter "fr" is present, push it onto out.params. If
799 : : // options are present, push them onto out.params. If both are present,
800 : : // throw an error.
801 [ + + ]: 40 : if (fr != argsIn.end()) {
802 [ - + + + ]: 17 : if (!options.empty()) {
803 [ + - + - : 3 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + fr->first + " conflicts with parameter " + options.getKeys().front());
+ - + - ]
804 : : }
805 [ + - + - ]: 16 : out.params.push_back(*fr->second);
806 : 16 : argsIn.erase(fr);
807 : : }
808 [ - + + + ]: 39 : if (!options.empty()) {
809 [ + - ]: 2 : out.params.push_back(std::move(options));
810 : 2 : options = UniValue{UniValue::VOBJ};
811 : : }
812 : 50 : }
813 : : // If leftover "args" param was found, use it as a source of positional
814 : : // arguments and add named arguments after. This is a convenience for
815 : : // clients that want to pass a combination of named and positional
816 : : // arguments as described in doc/JSON-RPC-interface.md#parameter-passing
817 [ + - ]: 18 : auto positional_args{argsIn.extract("args")};
818 [ + + + - ]: 9 : if (positional_args && positional_args.mapped()->isArray()) {
819 [ - + + + : 4 : if (initial_hole_size < (int)positional_args.mapped()->size() && initial_param) {
+ + ]
820 [ + - + - ]: 6 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter " + *initial_param + " specified twice both as positional and named argument");
821 : : }
822 : : // Assign positional_args to out.params and append named_args after.
823 : 2 : UniValue named_args{std::move(out.params)};
824 [ + - ]: 2 : out.params = *positional_args.mapped();
825 [ - + - + : 5 : for (size_t i{out.params.size()}; i < named_args.size(); ++i) {
+ + ]
826 [ + - + - : 3 : out.params.push_back(named_args[i]);
+ - ]
827 : : }
828 : 2 : }
829 : : // If there are still arguments in the argsIn map, this is an error.
830 [ + + ]: 7 : if (!argsIn.empty()) {
831 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown named parameter " + argsIn.begin()->first);
832 : : }
833 : : // Return request with named arguments transformed to positional arguments
834 [ + + ]: 6 : return out;
835 : 15 : }
836 : :
837 : 67 : static bool ExecuteCommands(const std::vector<const CRPCCommand*>& commands, const JSONRPCRequest& request, UniValue& result)
838 : : {
839 [ + - ]: 67 : for (const auto& command : commands) {
840 [ - + ]: 67 : if (ExecuteCommand(*command, request, result, &command == &commands.back())) {
841 : : return true;
842 : : }
843 : : }
844 : : return false;
845 : : }
846 : :
847 : 67 : UniValue CRPCTable::execute(const JSONRPCRequest &request) const
848 : : {
849 : : // Return immediately if in warmup
850 : 67 : {
851 : 67 : LOCK(g_rpc_warmup_mutex);
852 [ - + ]: 67 : if (fRPCInWarmup)
853 [ # # ]: 0 : throw JSONRPCError(RPC_IN_WARMUP, rpcWarmupStatus);
854 : 0 : }
855 : :
856 : : // Find method
857 : 67 : auto it = mapCommands.find(request.strMethod);
858 [ + - ]: 67 : if (it != mapCommands.end()) {
859 [ + + ]: 67 : UniValue result;
860 [ + + - + ]: 67 : if (ExecuteCommands(it->second, request, result)) {
861 : 43 : return result;
862 : : }
863 : 24 : }
864 [ # # # # ]: 0 : throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
865 : : }
866 : :
867 : 67 : static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler)
868 : : {
869 : 67 : try {
870 [ + - ]: 67 : RPCCommandExecution execution(request.strMethod);
871 : : // Execute, convert arguments to array if necessary
872 [ + + ]: 67 : if (request.params.isObject()) {
873 [ + + + - ]: 11 : return command.actor(transformNamedArguments(request, command.argNames), result, last_handler);
874 : : } else {
875 [ + + ]: 56 : return command.actor(request, result, last_handler);
876 : : }
877 [ + - + ]: 91 : } catch (const UniValue::type_error& e) {
878 [ - - - - ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, e.what());
879 : 6 : } catch (const std::exception& e) {
880 [ + - + - ]: 12 : throw JSONRPCError(RPC_MISC_ERROR, e.what());
881 : 6 : }
882 : : }
883 : :
884 : 1 : std::vector<std::string> CRPCTable::listCommands() const
885 : : {
886 : 1 : std::vector<std::string> commandList;
887 [ + - ]: 1 : commandList.reserve(mapCommands.size());
888 [ + - + + ]: 7 : for (const auto& i : mapCommands) commandList.emplace_back(i.first);
889 : 1 : return commandList;
890 : 0 : }
891 : :
892 : 1 : UniValue CRPCTable::buildOpenRPCDoc(bool include_hidden) const
893 : : {
894 : 1 : std::vector<std::string> method_names;
895 [ - + + + ]: 7 : for (const auto& [name, cmds] : mapCommands) {
896 [ - + ]: 6 : if (cmds.empty()) continue;
897 [ + - ]: 6 : const CRPCCommand* cmd{cmds.front()};
898 [ + - + - : 6 : if ((!include_hidden && cmd->category == "hidden") || !cmd->metadata_fn) continue;
- + ]
899 [ + - ]: 6 : method_names.push_back(name);
900 : : }
901 : 1 : std::sort(method_names.begin(), method_names.end());
902 : :
903 : 1 : UniValue methods{UniValue::VARR};
904 [ + + ]: 7 : for (const auto& method_name : method_names) {
905 [ + - + - ]: 6 : const CRPCCommand* cmd{mapCommands.at(method_name).front()};
906 [ + - ]: 6 : RPCMethod helpman{cmd->metadata_fn()};
907 : :
908 : 6 : UniValue params{UniValue::VARR};
909 [ + + ]: 9 : for (const auto& arg : helpman.GetArgs()) {
910 [ + - + + ]: 3 : if (!include_hidden && arg.m_opts.hidden) continue;
911 : 2 : UniValue param{UniValue::VOBJ};
912 [ + - + - : 4 : param.pushKV("name", arg.GetFirstName());
+ - + - ]
913 [ + - + - : 4 : param.pushKV("required", !arg.IsOptional());
+ - + - ]
914 [ + - + - : 4 : param.pushKV("schema", OpenRPCArgSchema(arg, include_hidden));
+ - ]
915 : :
916 [ - + + - ]: 2 : std::vector<std::string> names{SplitString(arg.m_names, '|')};
917 [ - + - + ]: 2 : if (names.size() > 1) {
918 : 0 : UniValue aliases{UniValue::VARR};
919 [ # # # # : 0 : for (size_t i{1}; i < names.size(); ++i) aliases.push_back(names[i]);
# # # # ]
920 [ # # # # ]: 0 : param.pushKV("x-bitcoin-aliases", std::move(aliases));
921 : 0 : }
922 [ - + - - : 2 : if (arg.m_opts.placeholder) param.pushKV("x-bitcoin-placeholder", true);
- - - - ]
923 [ - + - - : 2 : if (arg.m_opts.also_positional) param.pushKV("x-bitcoin-also-positional", true);
- - - - ]
924 [ + - + - : 4 : if (!arg.m_description.empty()) param.pushKV("description", arg.m_description);
+ - + - ]
925 [ + - ]: 2 : params.push_back(std::move(param));
926 : 2 : }
927 : :
928 : 6 : UniValue result_schema{UniValue::VOBJ};
929 : 6 : const auto& results{helpman.GetResults().m_results};
930 [ - + + + : 6 : if (results.size() == 1 && results[0].m_type != RPCResult::Type::ANY) {
- + ]
931 [ + - ]: 5 : result_schema = OpenRPCResultSchema(results[0]);
932 [ + - ]: 1 : } else if (results.size() > 1) {
933 : 1 : UniValue one_of{UniValue::VARR};
934 [ + + ]: 3 : for (const auto& r : results) {
935 [ + + ]: 2 : if (r.m_type == RPCResult::Type::ANY) continue;
936 [ + - ]: 1 : UniValue schema{OpenRPCResultSchema(r)};
937 [ - + - - : 1 : if (!r.m_cond.empty()) schema.pushKV("description", r.m_cond);
- - - - ]
938 [ + - ]: 1 : one_of.push_back(std::move(schema));
939 : 1 : }
940 [ - + + - ]: 1 : if (one_of.size() == 1) {
941 [ + - + - ]: 1 : result_schema = one_of[0];
942 [ # # ]: 0 : } else if (one_of.size() > 1) {
943 [ # # # # ]: 0 : result_schema.pushKV("oneOf", std::move(one_of));
944 : : }
945 : 1 : }
946 : :
947 : 6 : UniValue method{UniValue::VOBJ};
948 [ + - + - : 12 : method.pushKV("name", method_name);
+ - ]
949 [ - + + - : 12 : method.pushKV("description", util::TrimString(helpman.GetDescription()));
+ - + - +
- ]
950 [ + - + - ]: 12 : method.pushKV("params", std::move(params));
951 : 6 : UniValue result{UniValue::VOBJ};
952 [ + - + - : 12 : result.pushKV("name", "result");
+ - ]
953 [ + - + - ]: 12 : result.pushKV("schema", std::move(result_schema));
954 [ + - + - ]: 12 : method.pushKV("result", std::move(result));
955 [ + - + - : 12 : method.pushKV("x-bitcoin-category", cmd->category);
+ - ]
956 [ + - ]: 6 : methods.push_back(std::move(method));
957 : 6 : }
958 : :
959 [ + - ]: 1 : std::string version{"v" CLIENT_VERSION_STRING};
960 [ + - ]: 1 : if (!CLIENT_VERSION_IS_RELEASE) version += "-dev";
961 : :
962 : 1 : UniValue info{UniValue::VOBJ};
963 [ + - + - : 2 : info.pushKV("title", CLIENT_NAME " JSON-RPC");
+ - ]
964 [ + - + - : 2 : info.pushKV("version", version);
+ - ]
965 [ + - + - : 2 : info.pushKV("description", "Autogenerated from " CLIENT_NAME " RPC metadata.");
+ - ]
966 : :
967 : 1 : UniValue doc{UniValue::VOBJ};
968 [ + - + - : 2 : doc.pushKV("openrpc", "1.4.1");
+ - ]
969 [ + - + - ]: 2 : doc.pushKV("info", std::move(info));
970 [ + - + - ]: 2 : doc.pushKV("methods", std::move(methods));
971 : 1 : return doc;
972 : 1 : }
973 : :
974 : 0 : UniValue CRPCTable::dumpArgMap(const JSONRPCRequest& args_request) const
975 : : {
976 : 0 : JSONRPCRequest request = args_request;
977 : 0 : request.mode = JSONRPCRequest::GET_ARGS;
978 : :
979 : 0 : UniValue ret{UniValue::VARR};
980 [ # # ]: 0 : for (const auto& cmd : mapCommands) {
981 [ # # ]: 0 : UniValue result;
982 [ # # # # ]: 0 : if (ExecuteCommands(cmd.second, request, result)) {
983 [ # # # # ]: 0 : for (const auto& values : result.getValues()) {
984 [ # # # # ]: 0 : ret.push_back(values);
985 : : }
986 : : }
987 : 0 : }
988 : 0 : return ret;
989 : 0 : }
990 : :
991 : : CRPCTable tableRPC;
|