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