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