Branch data Line data Source code
1 : : // Copyright (c) 2017-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <bitcoin-build-config.h> // IWYU pragma: keep
6 : :
7 : : #include <chain.h>
8 : : #include <clientversion.h>
9 : : #include <common/args.h>
10 : : #include <common/messages.h>
11 : : #include <common/types.h>
12 : : #include <consensus/amount.h>
13 : : #include <core_io.h>
14 : : #include <key_io.h>
15 : : #include <node/types.h>
16 : : #include <outputtype.h>
17 : : #include <pow.h>
18 : : #include <rpc/util.h>
19 : : #include <script/descriptor.h>
20 : : #include <script/interpreter.h>
21 : : #include <script/signingprovider.h>
22 : : #include <script/solver.h>
23 : : #include <tinyformat.h>
24 : : #include <uint256.h>
25 : : #include <univalue.h>
26 : : #include <util/check.h>
27 : : #include <util/result.h>
28 : : #include <util/strencodings.h>
29 : : #include <util/string.h>
30 : : #include <util/translation.h>
31 : :
32 : : #include <algorithm>
33 : : #include <iterator>
34 : : #include <string_view>
35 : : #include <tuple>
36 : : #include <utility>
37 : :
38 : : using common::PSBTError;
39 : : using common::PSBTErrorString;
40 : : using common::TransactionErrorString;
41 : : using node::TransactionError;
42 : : using util::Join;
43 : : using util::SplitString;
44 : : using util::TrimString;
45 : :
46 : : const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
47 : : const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};
48 : :
49 : 24350 : std::string GetAllOutputTypes()
50 : : {
51 : 24350 : std::vector<std::string> ret;
52 : 24350 : using U = std::underlying_type_t<TxoutType>;
53 [ + + ]: 292200 : for (U i = (U)TxoutType::NONSTANDARD; i <= (U)TxoutType::WITNESS_UNKNOWN; ++i) {
54 [ + - + - ]: 535700 : ret.emplace_back(GetTxnOutputType(static_cast<TxoutType>(i)));
55 : : }
56 [ + - ]: 48700 : return Join(ret, ", ");
57 : 24350 : }
58 : :
59 : 1195 : void RPCTypeCheckObj(const UniValue& o,
60 : : const std::map<std::string, UniValueType>& typesExpected,
61 : : bool fAllowNull,
62 : : bool fStrict)
63 : : {
64 [ + + ]: 17237 : for (const auto& t : typesExpected) {
65 : 16078 : const UniValue& v = o.find_value(t.first);
66 [ + + + + ]: 16078 : if (!fAllowNull && v.isNull())
67 [ + - + - ]: 22 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
68 : :
69 [ + + + + : 16067 : if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull())))
+ + + + ]
70 [ + - + - : 50 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("JSON value of type %s for field %s is not of expected type %s", uvTypeName(v.type()), t.first, uvTypeName(t.second.type)));
+ - + - ]
71 : : }
72 : :
73 [ + + ]: 1159 : if (fStrict)
74 : : {
75 [ + + ]: 2514 : for (const std::string& k : o.getKeys())
76 : : {
77 [ + + ]: 1780 : if (typesExpected.count(k) == 0)
78 : : {
79 : 4 : std::string err = strprintf("Unexpected key %s", k);
80 [ + - ]: 4 : throw JSONRPCError(RPC_TYPE_ERROR, err);
81 : 4 : }
82 : : }
83 : : }
84 : 1155 : }
85 : :
86 : 11159 : int ParseVerbosity(const UniValue& arg, int default_verbosity, bool allow_bool)
87 : : {
88 [ + + ]: 11159 : if (!arg.isNull()) {
89 [ + + ]: 9552 : if (arg.isBool()) {
90 [ + + ]: 3209 : if (!allow_bool) {
91 [ + - + - ]: 4 : throw JSONRPCError(RPC_TYPE_ERROR, "Verbosity was boolean but only integer allowed");
92 : : }
93 : 3207 : return arg.get_bool(); // true = 1
94 : : } else {
95 : 6343 : return arg.getInt<int>();
96 : : }
97 : : }
98 : : return default_verbosity;
99 : : }
100 : :
101 : 38067 : CAmount AmountFromValue(const UniValue& value, int decimals)
102 : : {
103 [ + + + + ]: 38067 : if (!value.isNum() && !value.isStr())
104 [ + - + - ]: 24 : throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
105 : 38055 : CAmount amount;
106 [ + + ]: 38055 : if (!ParseFixedPoint(value.getValStr(), decimals, &amount))
107 [ + - + - ]: 166 : throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
108 [ + + ]: 37972 : if (!MoneyRange(amount))
109 [ + - + - ]: 24 : throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
110 : 37960 : return amount;
111 : : }
112 : :
113 : 22853 : CFeeRate ParseFeeRate(const UniValue& json)
114 : : {
115 : 22853 : CAmount val{AmountFromValue(json)};
116 [ + + + - : 22853 : if (val >= COIN) throw JSONRPCError(RPC_INVALID_PARAMETER, "Fee rates larger than or equal to 1BTC/kvB are not accepted");
+ - ]
117 : 22851 : return CFeeRate{val};
118 : : }
119 : :
120 : 22931 : uint256 ParseHashV(const UniValue& v, std::string_view name)
121 : : {
122 : 22931 : const std::string& strHex(v.get_str());
123 [ + + ]: 22930 : if (auto rv{uint256::FromHex(strHex)}) return *rv;
124 [ + + ]: 28 : if (auto expected_len{uint256::size() * 2}; strHex.length() != expected_len) {
125 [ + - + - ]: 32 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, expected_len, strHex.length(), strHex));
126 : : }
127 [ + - + - ]: 24 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
128 : : }
129 : 5972 : uint256 ParseHashO(const UniValue& o, std::string_view strKey)
130 : : {
131 : 5972 : return ParseHashV(o.find_value(strKey), strKey);
132 : : }
133 : 422 : std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name)
134 : : {
135 [ + - ]: 422 : std::string strHex;
136 [ + - ]: 422 : if (v.isStr())
137 [ + - + - ]: 422 : strHex = v.get_str();
138 [ + - + + ]: 422 : if (!IsHex(strHex))
139 [ + - + - ]: 8 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
140 [ + - ]: 418 : return ParseHex(strHex);
141 : 418 : }
142 : 181 : std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey)
143 : : {
144 : 181 : return ParseHexV(o.find_value(strKey), strKey);
145 : : }
146 : :
147 : : namespace {
148 : :
149 : : /**
150 : : * Quote an argument for shell.
151 : : *
152 : : * @note This is intended for help, not for security-sensitive purposes.
153 : : */
154 : 805 : std::string ShellQuote(const std::string& s)
155 : : {
156 [ + - ]: 805 : std::string result;
157 [ + - ]: 805 : result.reserve(s.size() * 2);
158 [ + + ]: 24061 : for (const char ch: s) {
159 [ + + ]: 23256 : if (ch == '\'') {
160 [ + - ]: 1 : result += "'\''";
161 : : } else {
162 [ + - ]: 46511 : result += ch;
163 : : }
164 : : }
165 [ + - ]: 1610 : return "'" + result + "'";
166 : 805 : }
167 : :
168 : : /**
169 : : * Shell-quotes the argument if it needs quoting, else returns it literally, to save typing.
170 : : *
171 : : * @note This is intended for help, not for security-sensitive purposes.
172 : : */
173 : 7858 : std::string ShellQuoteIfNeeded(const std::string& s)
174 : : {
175 [ + + ]: 72217 : for (const char ch: s) {
176 [ + + ]: 65164 : if (ch == ' ' || ch == '\'' || ch == '"') {
177 : 805 : return ShellQuote(s);
178 : : }
179 : : }
180 : :
181 : 7053 : return s;
182 : : }
183 : :
184 : : }
185 : :
186 : 646997 : std::string HelpExampleCli(const std::string& methodname, const std::string& args)
187 : : {
188 [ + - + - ]: 1940991 : return "> bitcoin-cli " + methodname + " " + args + "\n";
189 : : }
190 : :
191 : 2893 : std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList& args)
192 : : {
193 : 2893 : std::string result = "> bitcoin-cli -named " + methodname;
194 [ + + ]: 10751 : for (const auto& argpair: args) {
195 [ + + ]: 7858 : const auto& value = argpair.second.isStr()
196 [ + + + - ]: 7858 : ? argpair.second.get_str()
197 [ + - + - ]: 7858 : : argpair.second.write();
198 [ + - + - : 23574 : result += " " + argpair.first + "=" + ShellQuoteIfNeeded(value);
+ - ]
199 : 7858 : }
200 [ + - ]: 2893 : result += "\n";
201 : 2893 : return result;
202 : 0 : }
203 : :
204 : 440144 : std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
205 : : {
206 : 440144 : return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", "
207 [ + - + - ]: 1320432 : "\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: application/json' http://127.0.0.1:8332/\n";
208 : : }
209 : :
210 : 2890 : std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList& args)
211 : : {
212 : 2890 : UniValue params(UniValue::VOBJ);
213 [ + + ]: 10745 : for (const auto& param: args) {
214 [ + - + - : 15710 : params.pushKV(param.first, param.second);
+ - ]
215 : : }
216 : :
217 : 2890 : return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", "
218 [ + - + - : 11560 : "\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: application/json' http://127.0.0.1:8332/\n";
+ - ]
219 : 2890 : }
220 : :
221 : : // Converts a hex string to a public key if possible
222 : 565 : CPubKey HexToPubKey(const std::string& hex_in)
223 : : {
224 [ + + ]: 565 : if (!IsHex(hex_in)) {
225 [ + - + - ]: 3 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + hex_in + "\" must be a hex string");
226 : : }
227 [ + + + + ]: 564 : if (hex_in.length() != 66 && hex_in.length() != 130) {
228 [ + - + - ]: 3 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + hex_in + "\" must have a length of either 33 or 65 bytes");
229 : : }
230 : 563 : CPubKey vchPubKey(ParseHex(hex_in));
231 [ - + ]: 563 : if (!vchPubKey.IsFullyValid()) {
232 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + hex_in + "\" must be cryptographically valid.");
233 : : }
234 : 563 : return vchPubKey;
235 : : }
236 : :
237 : : // Creates a multisig address from a given list of public keys, number of signatures required, and the address type
238 : 82 : CTxDestination AddAndGetMultisigDestination(const int required, const std::vector<CPubKey>& pubkeys, OutputType type, FlatSigningProvider& keystore, CScript& script_out)
239 : : {
240 : : // Gather public keys
241 [ - + ]: 82 : if (required < 1) {
242 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem");
243 : : }
244 [ - + ]: 82 : if ((int)pubkeys.size() < required) {
245 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));
246 : : }
247 [ + + ]: 82 : if (pubkeys.size() > MAX_PUBKEYS_PER_MULTISIG) {
248 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Number of keys involved in the multisignature address creation > %d\nReduce the number", MAX_PUBKEYS_PER_MULTISIG));
249 : : }
250 : :
251 : 80 : script_out = GetScriptForMultisig(required, pubkeys);
252 : :
253 : : // Check if any keys are uncompressed. If so, the type is legacy
254 [ + + ]: 539 : for (const CPubKey& pk : pubkeys) {
255 [ + + ]: 477 : if (!pk.IsCompressed()) {
256 : : type = OutputType::LEGACY;
257 : : break;
258 : : }
259 : : }
260 : :
261 [ + + + - : 80 : if (type == OutputType::LEGACY && script_out.size() > MAX_SCRIPT_ELEMENT_SIZE) {
+ + ]
262 [ + - + - : 3 : throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE)));
+ - ]
263 : : }
264 : :
265 : : // Make the address
266 : 79 : CTxDestination dest = AddAndGetDestinationForScript(keystore, script_out, type);
267 : :
268 : 79 : return dest;
269 : : }
270 : :
271 : : class DescribeAddressVisitor
272 : : {
273 : : public:
274 : : explicit DescribeAddressVisitor() = default;
275 : :
276 : 0 : UniValue operator()(const CNoDestination& dest) const
277 : : {
278 : 0 : return UniValue(UniValue::VOBJ);
279 : : }
280 : :
281 : 0 : UniValue operator()(const PubKeyDestination& dest) const
282 : : {
283 : 0 : return UniValue(UniValue::VOBJ);
284 : : }
285 : :
286 : 117 : UniValue operator()(const PKHash& keyID) const
287 : : {
288 : 117 : UniValue obj(UniValue::VOBJ);
289 [ + - + - : 234 : obj.pushKV("isscript", false);
+ - ]
290 [ + - + - : 234 : obj.pushKV("iswitness", false);
+ - ]
291 : 117 : return obj;
292 : 0 : }
293 : :
294 : 144 : UniValue operator()(const ScriptHash& scriptID) const
295 : : {
296 : 144 : UniValue obj(UniValue::VOBJ);
297 [ + - + - : 288 : obj.pushKV("isscript", true);
+ - ]
298 [ + - + - : 288 : obj.pushKV("iswitness", false);
+ - ]
299 : 144 : return obj;
300 : 0 : }
301 : :
302 : 524 : UniValue operator()(const WitnessV0KeyHash& id) const
303 : : {
304 : 524 : UniValue obj(UniValue::VOBJ);
305 [ + - + - : 1048 : obj.pushKV("isscript", false);
+ - ]
306 [ + - + - : 1048 : obj.pushKV("iswitness", true);
+ - ]
307 [ + - + - : 1048 : obj.pushKV("witness_version", 0);
+ - ]
308 [ + - + - : 1048 : obj.pushKV("witness_program", HexStr(id));
+ - + - ]
309 : 524 : return obj;
310 : 0 : }
311 : :
312 : 42 : UniValue operator()(const WitnessV0ScriptHash& id) const
313 : : {
314 : 42 : UniValue obj(UniValue::VOBJ);
315 [ + - + - : 84 : obj.pushKV("isscript", true);
+ - ]
316 [ + - + - : 84 : obj.pushKV("iswitness", true);
+ - ]
317 [ + - + - : 84 : obj.pushKV("witness_version", 0);
+ - ]
318 [ + - + - : 84 : obj.pushKV("witness_program", HexStr(id));
+ - + - ]
319 : 42 : return obj;
320 : 0 : }
321 : :
322 : 123 : UniValue operator()(const WitnessV1Taproot& tap) const
323 : : {
324 : 123 : UniValue obj(UniValue::VOBJ);
325 [ + - + - : 246 : obj.pushKV("isscript", true);
+ - ]
326 [ + - + - : 246 : obj.pushKV("iswitness", true);
+ - ]
327 [ + - + - : 246 : obj.pushKV("witness_version", 1);
+ - ]
328 [ + - + - : 246 : obj.pushKV("witness_program", HexStr(tap));
+ - + - ]
329 : 123 : return obj;
330 : 0 : }
331 : :
332 : 1 : UniValue operator()(const PayToAnchor& anchor) const
333 : : {
334 : 1 : UniValue obj(UniValue::VOBJ);
335 [ + - + - : 2 : obj.pushKV("isscript", true);
+ - ]
336 [ + - + - : 2 : obj.pushKV("iswitness", true);
+ - ]
337 : 1 : return obj;
338 : 0 : }
339 : :
340 : 5 : UniValue operator()(const WitnessUnknown& id) const
341 : : {
342 : 5 : UniValue obj(UniValue::VOBJ);
343 [ + - + - : 10 : obj.pushKV("iswitness", true);
+ - ]
344 [ + - + - : 10 : obj.pushKV("witness_version", id.GetWitnessVersion());
+ - ]
345 [ + - + - : 10 : obj.pushKV("witness_program", HexStr(id.GetWitnessProgram()));
+ - + - ]
346 : 5 : return obj;
347 : 0 : }
348 : : };
349 : :
350 : 956 : UniValue DescribeAddress(const CTxDestination& dest)
351 : : {
352 : 956 : return std::visit(DescribeAddressVisitor(), dest);
353 : : }
354 : :
355 : : /**
356 : : * Returns a sighash value corresponding to the passed in argument.
357 : : *
358 : : * @pre The sighash argument should be string or null.
359 : : */
360 : 745 : std::optional<int> ParseSighashString(const UniValue& sighash)
361 : : {
362 [ + + ]: 745 : if (sighash.isNull()) {
363 : 673 : return std::nullopt;
364 : : }
365 : 72 : const auto result{SighashFromStr(sighash.get_str())};
366 [ + + ]: 72 : if (!result) {
367 [ + - + - ]: 8 : throw JSONRPCError(RPC_INVALID_PARAMETER, util::ErrorString(result).original);
368 : : }
369 : 68 : return result.value();
370 : 68 : }
371 : :
372 : 356 : unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
373 : : {
374 : 356 : const int target{value.getInt<int>()};
375 : 356 : const unsigned int unsigned_target{static_cast<unsigned int>(target)};
376 [ + + + + ]: 356 : if (target < 1 || unsigned_target > max_target) {
377 [ + - + - ]: 62 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u and %u", 1, max_target));
378 : : }
379 : 325 : return unsigned_target;
380 : : }
381 : :
382 : 14 : RPCErrorCode RPCErrorFromPSBTError(PSBTError err)
383 : : {
384 [ + - - ]: 14 : switch (err) {
385 : : case PSBTError::UNSUPPORTED:
386 : : return RPC_INVALID_PARAMETER;
387 : 14 : case PSBTError::SIGHASH_MISMATCH:
388 : 14 : return RPC_DESERIALIZATION_ERROR;
389 : 0 : default: break;
390 : : }
391 : 0 : return RPC_TRANSACTION_ERROR;
392 : : }
393 : :
394 : 4243 : RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
395 : : {
396 [ + + + ]: 4243 : switch (terr) {
397 : : case TransactionError::MEMPOOL_REJECTED:
398 : : return RPC_TRANSACTION_REJECTED;
399 : 3 : case TransactionError::ALREADY_IN_UTXO_SET:
400 : 3 : return RPC_VERIFY_ALREADY_IN_UTXO_SET;
401 : 9 : default: break;
402 : : }
403 : 9 : return RPC_TRANSACTION_ERROR;
404 : : }
405 : :
406 : 14 : UniValue JSONRPCPSBTError(PSBTError err)
407 : : {
408 [ + - + - ]: 28 : return JSONRPCError(RPCErrorFromPSBTError(err), PSBTErrorString(err).original);
409 : : }
410 : :
411 : 4243 : UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string)
412 : : {
413 [ + + ]: 4243 : if (err_string.length() > 0) {
414 : 4233 : return JSONRPCError(RPCErrorFromTransactionError(terr), err_string);
415 : : } else {
416 [ + - + - ]: 20 : return JSONRPCError(RPCErrorFromTransactionError(terr), TransactionErrorString(terr).original);
417 : : }
418 : : }
419 : :
420 : : /**
421 : : * A pair of strings that can be aligned (through padding) with other Sections
422 : : * later on
423 : : */
424 : 57364 : struct Section {
425 : 18522 : Section(const std::string& left, const std::string& right)
426 [ + - ]: 18522 : : m_left{left}, m_right{right} {}
427 : : std::string m_left;
428 : : const std::string m_right;
429 : : };
430 : :
431 : : /**
432 : : * Keeps track of RPCArgs by transforming them into sections for the purpose
433 : : * of serializing everything to a single string
434 : : */
435 : 0 : struct Sections {
436 : : std::vector<Section> m_sections;
437 : : size_t m_max_pad{0};
438 : :
439 : 16752 : void PushSection(const Section& s)
440 : : {
441 [ + + ]: 16752 : m_max_pad = std::max(m_max_pad, s.m_left.size());
442 : 16752 : m_sections.push_back(s);
443 : 16752 : }
444 : :
445 : : /**
446 : : * Recursive helper to translate an RPCArg into sections
447 : : */
448 : : // NOLINTNEXTLINE(misc-no-recursion)
449 : 2991 : void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE)
450 : : {
451 [ + - ]: 2991 : const auto indent = std::string(current_indent, ' ');
452 [ + - + + : 2991 : const auto indent_next = std::string(current_indent + 2, ' ');
+ - ]
453 : 2991 : const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name
454 : 2991 : const bool is_top_level_arg{outer_type == OuterType::NONE}; // True on the first recursion
455 : :
456 [ + + + - ]: 2991 : switch (arg.m_type) {
457 : 2461 : case RPCArg::Type::STR_HEX:
458 : 2461 : case RPCArg::Type::STR:
459 : 2461 : case RPCArg::Type::NUM:
460 : 2461 : case RPCArg::Type::AMOUNT:
461 : 2461 : case RPCArg::Type::RANGE:
462 : 2461 : case RPCArg::Type::BOOL:
463 : 2461 : case RPCArg::Type::OBJ_NAMED_PARAMS: {
464 [ + + ]: 2461 : if (is_top_level_arg) return; // Nothing more to do for non-recursive types on first recursion
465 [ + - ]: 671 : auto left = indent;
466 [ + + + - ]: 671 : if (arg.m_opts.type_str.size() != 0 && push_name) {
467 [ + - + - : 9 : left += "\"" + arg.GetName() + "\": " + arg.m_opts.type_str.at(0);
+ - + - ]
468 : : } else {
469 [ + + + - : 1336 : left += push_name ? arg.ToStringObj(/*oneline=*/false) : arg.ToString(/*oneline=*/false);
+ - ]
470 : : }
471 [ + - ]: 671 : left += ",";
472 [ + - + - : 1342 : PushSection({left, arg.ToDescriptionString(/*is_named_arg=*/push_name)});
+ - ]
473 : 671 : break;
474 : 671 : }
475 : 202 : case RPCArg::Type::OBJ:
476 : 202 : case RPCArg::Type::OBJ_USER_KEYS: {
477 [ + + + - : 202 : const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
+ - ]
478 [ - + - - : 606 : PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right});
- - - - +
- + - + -
+ - - + -
+ - - -
- ]
479 [ + + ]: 674 : for (const auto& arg_inner : arg.m_inner) {
480 [ + - ]: 472 : Push(arg_inner, current_indent + 2, OuterType::OBJ);
481 : : }
482 [ + + ]: 202 : if (arg.m_type != RPCArg::Type::OBJ) {
483 [ + - + - : 80 : PushSection({indent_next + "...", ""});
+ - + - ]
484 : : }
485 [ + + + - : 784 : PushSection({indent + "}" + (is_top_level_arg ? "" : ","), ""});
+ - + - +
- ]
486 : 202 : break;
487 : 202 : }
488 : 328 : case RPCArg::Type::ARR: {
489 [ + - ]: 328 : auto left = indent;
490 [ + + + - : 764 : left += push_name ? "\"" + arg.GetName() + "\": " : "";
+ - + - +
- + + + +
- - - - ]
491 [ + - ]: 328 : left += "[";
492 [ + + + - : 328 : const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
+ - ]
493 [ + - + - ]: 328 : PushSection({left, right});
494 [ + + ]: 759 : for (const auto& arg_inner : arg.m_inner) {
495 [ + - ]: 431 : Push(arg_inner, current_indent + 2, OuterType::ARR);
496 : : }
497 [ + - + - : 656 : PushSection({indent_next + "...", ""});
+ - + - ]
498 [ + + + - : 1038 : PushSection({indent + "]" + (is_top_level_arg ? "" : ","), ""});
+ - + - +
- ]
499 : 328 : break;
500 : 328 : }
501 : : } // no default case, so the compiler can warn about missing cases
502 : 2991 : }
503 : :
504 : : /**
505 : : * Concatenate all sections with proper padding
506 : : */
507 : 3317 : std::string ToString() const
508 : : {
509 : 3317 : std::string ret;
510 : 3317 : const size_t pad = m_max_pad + 4;
511 [ + + ]: 21839 : for (const auto& s : m_sections) {
512 : : // The left part of a section is assumed to be a single line, usually it is the name of the JSON struct or a
513 : : // brace like {, }, [, or ]
514 [ + - ]: 18522 : CHECK_NONFATAL(s.m_left.find('\n') == std::string::npos);
515 [ + + ]: 18522 : if (s.m_right.empty()) {
516 [ + - ]: 4996 : ret += s.m_left;
517 [ + - ]: 4996 : ret += "\n";
518 : 4996 : continue;
519 : : }
520 : :
521 [ + - ]: 13526 : std::string left = s.m_left;
522 [ + - ]: 13526 : left.resize(pad, ' ');
523 [ + - ]: 13526 : ret += left;
524 : :
525 : : // Properly pad after newlines
526 : 13526 : std::string right;
527 : 13526 : size_t begin = 0;
528 : 13526 : size_t new_line_pos = s.m_right.find_first_of('\n');
529 : 17310 : while (true) {
530 [ + - ]: 30836 : right += s.m_right.substr(begin, new_line_pos - begin);
531 [ + + ]: 15418 : if (new_line_pos == std::string::npos) {
532 : : break; //No new line
533 : : }
534 [ + - + - ]: 4168 : right += "\n" + std::string(pad, ' ');
535 : 2084 : begin = s.m_right.find_first_not_of(' ', new_line_pos + 1);
536 [ + + ]: 2084 : if (begin == std::string::npos) {
537 : : break; // Empty line
538 : : }
539 : 1892 : new_line_pos = s.m_right.find_first_of('\n', begin + 1);
540 : : }
541 [ + - ]: 13526 : ret += right;
542 [ + - ]: 13526 : ret += "\n";
543 : 13526 : }
544 : 3317 : return ret;
545 : 0 : }
546 : : };
547 : :
548 : 19 : RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
549 [ + - + - : 62 : : RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
+ + ]
550 : :
551 : 484017 : RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
552 : 484025 : : m_name{std::move(name)},
553 : 484025 : m_fun{std::move(fun)},
554 : 484025 : m_description{std::move(description)},
555 [ + - ]: 484017 : m_args{std::move(args)},
556 [ + - ]: 484025 : m_results{std::move(results)},
557 [ + - ]: 484025 : m_examples{std::move(examples)}
558 : : {
559 : : // Map of parameter names and types just used to check whether the names are
560 : : // unique. Parameter names always need to be unique, with the exception that
561 : : // there can be pairs of POSITIONAL and NAMED parameters with the same name.
562 : 484017 : enum ParamType { POSITIONAL = 1, NAMED = 2, NAMED_ONLY = 4 };
563 : 484017 : std::map<std::string, int> param_names;
564 : :
565 [ + + ]: 1364668 : for (const auto& arg : m_args) {
566 [ + - ]: 880659 : std::vector<std::string> names = SplitString(arg.m_names, '|');
567 : : // Should have unique named arguments
568 [ + + ]: 1776654 : for (const std::string& name : names) {
569 [ + - ]: 895997 : auto& param_type = param_names[name];
570 [ + + ]: 895997 : CHECK_NONFATAL(!(param_type & POSITIONAL));
571 [ + + ]: 895996 : CHECK_NONFATAL(!(param_type & NAMED_ONLY));
572 : 895995 : param_type |= POSITIONAL;
573 : : }
574 [ + + ]: 880657 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
575 [ + + ]: 112149 : for (const auto& inner : arg.m_inner) {
576 [ + - ]: 96564 : std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
577 [ + + ]: 193123 : for (const std::string& inner_name : inner_names) {
578 [ + - ]: 96565 : auto& param_type = param_names[inner_name];
579 [ + + + + : 96567 : CHECK_NONFATAL(!(param_type & POSITIONAL) || inner.m_opts.also_positional);
+ + ]
580 [ + + ]: 96563 : CHECK_NONFATAL(!(param_type & NAMED));
581 [ + + ]: 96561 : CHECK_NONFATAL(!(param_type & NAMED_ONLY));
582 [ + + ]: 183672 : param_type |= inner.m_opts.also_positional ? NAMED : NAMED_ONLY;
583 : : }
584 : 96564 : }
585 : : }
586 : : // Default value type should match argument type only when defined
587 [ + + ]: 880651 : if (arg.m_fallback.index() == 2) {
588 : 275119 : const RPCArg::Type type = arg.m_type;
589 [ - + + + : 275119 : switch (std::get<RPCArg::Default>(arg.m_fallback).getType()) {
+ - - ]
590 : 0 : case UniValue::VOBJ:
591 [ # # ]: 0 : CHECK_NONFATAL(type == RPCArg::Type::OBJ);
592 : : break;
593 : 1949 : case UniValue::VARR:
594 [ + - ]: 1949 : CHECK_NONFATAL(type == RPCArg::Type::ARR);
595 : : break;
596 : 96045 : case UniValue::VSTR:
597 [ + + - + : 192090 : CHECK_NONFATAL(type == RPCArg::Type::STR || type == RPCArg::Type::STR_HEX || type == RPCArg::Type::AMOUNT);
- ]
598 : : break;
599 : 61093 : case UniValue::VNUM:
600 [ + - - + : 122186 : CHECK_NONFATAL(type == RPCArg::Type::NUM || type == RPCArg::Type::AMOUNT || type == RPCArg::Type::RANGE);
- ]
601 : : break;
602 : 116032 : case UniValue::VBOOL:
603 [ + - ]: 116032 : CHECK_NONFATAL(type == RPCArg::Type::BOOL);
604 : : break;
605 : : case UniValue::VNULL:
606 : : // Null values are accepted in all arguments
607 : : break;
608 : 0 : default:
609 [ # # ]: 0 : NONFATAL_UNREACHABLE();
610 : : break;
611 : : }
612 : : }
613 : 880659 : }
614 : 484049 : }
615 : :
616 : 1036 : std::string RPCResults::ToDescriptionString() const
617 : : {
618 : 1036 : std::string result;
619 [ + + ]: 2291 : for (const auto& r : m_results) {
620 [ + + ]: 1255 : if (r.m_type == RPCResult::Type::ANY) continue; // for testing only
621 [ + + ]: 1245 : if (r.m_cond.empty()) {
622 [ + - ]: 908 : result += "\nResult:\n";
623 : : } else {
624 [ + - + - ]: 1011 : result += "\nResult (" + r.m_cond + "):\n";
625 : : }
626 : 1245 : Sections sections;
627 [ + - ]: 1245 : r.ToSections(sections);
628 [ + - ]: 2490 : result += sections.ToString();
629 : 1245 : }
630 : 1036 : return result;
631 : 0 : }
632 : :
633 : 1036 : std::string RPCExamples::ToDescriptionString() const
634 : : {
635 [ + + ]: 1036 : return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
636 : : }
637 : :
638 : 199628 : UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const
639 : : {
640 [ + + ]: 199628 : if (request.mode == JSONRPCRequest::GET_ARGS) {
641 : 169 : return GetArgMap();
642 : : }
643 : : /*
644 : : * Check if the given request is valid according to this command or if
645 : : * the user is asking for help information, and throw help when appropriate.
646 : : */
647 [ + + + + ]: 199459 : if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
648 [ + - + - ]: 2066 : throw std::runtime_error(ToString());
649 : : }
650 : 198426 : UniValue arg_mismatch{UniValue::VOBJ};
651 [ + + ]: 560513 : for (size_t i{0}; i < m_args.size(); ++i) {
652 [ + - ]: 362087 : const auto& arg{m_args.at(i)};
653 [ + - + - ]: 362087 : UniValue match{arg.MatchesType(request.params[i])};
654 [ + + ]: 362087 : if (!match.isTrue()) {
655 [ + - + - ]: 60 : arg_mismatch.pushKV(strprintf("Position %s (%s)", i + 1, arg.m_names), std::move(match));
656 : : }
657 : 362087 : }
658 [ + + ]: 198426 : if (!arg_mismatch.empty()) {
659 [ + - + - : 56 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Wrong type passed:\n%s", arg_mismatch.write(4)));
+ - ]
660 : : }
661 [ + - ]: 198398 : CHECK_NONFATAL(m_req == nullptr);
662 : 198398 : m_req = &request;
663 [ + + ]: 198398 : UniValue ret = m_fun(*this, request);
664 : 192821 : m_req = nullptr;
665 [ + - + - : 192821 : if (gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) {
+ + ]
666 : 192783 : UniValue mismatch{UniValue::VARR};
667 [ + - ]: 210784 : for (const auto& res : m_results.m_results) {
668 [ + - ]: 210784 : UniValue match{res.MatchesType(ret)};
669 [ + + ]: 210784 : if (match.isTrue()) {
670 [ + - ]: 192783 : mismatch.setNull();
671 : 192783 : break;
672 : : }
673 [ + - ]: 18001 : mismatch.push_back(std::move(match));
674 : 210784 : }
675 [ - + ]: 192783 : if (!mismatch.isNull()) {
676 [ # # ]: 0 : std::string explain{
677 [ # # ]: 0 : mismatch.empty() ? "no possible results defined" :
678 [ # # # # ]: 0 : mismatch.size() == 1 ? mismatch[0].write(4) :
679 [ # # # # : 0 : mismatch.write(4)};
# # ]
680 : 0 : throw std::runtime_error{
681 : 0 : strprintf("Internal bug detected: RPC call \"%s\" returned incorrect type:\n%s\n%s %s\nPlease report this issue here: %s\n",
682 [ # # ]: 0 : m_name, explain,
683 [ # # ]: 0 : CLIENT_NAME, FormatFullVersion(),
684 [ # # ]: 0 : CLIENT_BUGREPORT)};
685 : 0 : }
686 : 192783 : }
687 : 192821 : return ret;
688 : 198426 : }
689 : :
690 : : using CheckFn = void(const RPCArg&);
691 : 30554 : static const UniValue* DetailMaybeArg(CheckFn* check, const std::vector<RPCArg>& params, const JSONRPCRequest* req, size_t i)
692 : : {
693 : 30554 : CHECK_NONFATAL(i < params.size());
694 : 30554 : const UniValue& arg{CHECK_NONFATAL(req)->params[i]};
695 : 30554 : const RPCArg& param{params.at(i)};
696 [ + + ]: 30554 : if (check) check(param);
697 : :
698 [ + + ]: 30554 : if (!arg.isNull()) return &arg;
699 [ + + ]: 8262 : if (!std::holds_alternative<RPCArg::Default>(param.m_fallback)) return nullptr;
700 : 6360 : return &std::get<RPCArg::Default>(param.m_fallback);
701 : : }
702 : :
703 : 28601 : static void CheckRequiredOrDefault(const RPCArg& param)
704 : : {
705 : : // Must use `Arg<Type>(key)` to get the argument or its default value.
706 : 28601 : const bool required{
707 [ + + - + ]: 28601 : std::holds_alternative<RPCArg::Optional>(param.m_fallback) && RPCArg::Optional::NO == std::get<RPCArg::Optional>(param.m_fallback),
708 : 53351 : };
709 [ + - ]: 53351 : CHECK_NONFATAL(required || std::holds_alternative<RPCArg::Default>(param.m_fallback));
710 : 28601 : }
711 : :
712 : : #define TMPL_INST(check_param, ret_type, return_code) \
713 : : template <> \
714 : : ret_type RPCHelpMan::ArgValue<ret_type>(size_t i) const \
715 : : { \
716 : : const UniValue* maybe_arg{ \
717 : : DetailMaybeArg(check_param, m_args, m_req, i), \
718 : : }; \
719 : : return return_code \
720 : : } \
721 : : void force_semicolon(ret_type)
722 : :
723 : : // Optional arg (without default). Can also be called on required args, if needed.
724 : 0 : TMPL_INST(nullptr, const UniValue*, maybe_arg;);
725 [ + + ]: 719 : TMPL_INST(nullptr, std::optional<double>, maybe_arg ? std::optional{maybe_arg->get_real()} : std::nullopt;);
726 [ + + ]: 734 : TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get_bool()} : std::nullopt;);
727 [ + + ]: 500 : TMPL_INST(nullptr, const std::string*, maybe_arg ? &maybe_arg->get_str() : nullptr;);
728 : :
729 : : // Required arg or optional arg with default value.
730 : 22853 : TMPL_INST(CheckRequiredOrDefault, const UniValue&, *CHECK_NONFATAL(maybe_arg););
731 : 650 : TMPL_INST(CheckRequiredOrDefault, bool, CHECK_NONFATAL(maybe_arg)->get_bool(););
732 : 1376 : TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>(););
733 : 824 : TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>(););
734 : 2898 : TMPL_INST(CheckRequiredOrDefault, const std::string&, CHECK_NONFATAL(maybe_arg)->get_str(););
735 : :
736 : 198467 : bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
737 : : {
738 : 198467 : size_t num_required_args = 0;
739 [ + + ]: 391618 : for (size_t n = m_args.size(); n > 0; --n) {
740 [ + + ]: 322261 : if (!m_args.at(n - 1).IsOptional()) {
741 : : num_required_args = n;
742 : : break;
743 : : }
744 : : }
745 [ + + + + ]: 198467 : return num_required_args <= num_args && num_args <= m_args.size();
746 : : }
747 : :
748 : 142185 : std::vector<std::pair<std::string, bool>> RPCHelpMan::GetArgNames() const
749 : : {
750 : 142185 : std::vector<std::pair<std::string, bool>> ret;
751 [ + - ]: 142185 : ret.reserve(m_args.size());
752 [ + + ]: 400404 : for (const auto& arg : m_args) {
753 [ + + ]: 258219 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
754 [ + + ]: 47517 : for (const auto& inner : arg.m_inner) {
755 [ + - ]: 40430 : ret.emplace_back(inner.m_names, /*named_only=*/true);
756 : : }
757 : : }
758 [ + - ]: 258219 : ret.emplace_back(arg.m_names, /*named_only=*/false);
759 : : }
760 : 142185 : return ret;
761 : 0 : }
762 : :
763 : 30554 : size_t RPCHelpMan::GetParamIndex(std::string_view key) const
764 : : {
765 : 30554 : auto it{std::find_if(
766 : 61674 : m_args.begin(), m_args.end(), [&key](const auto& arg) { return arg.GetName() == key;}
767 : : )};
768 : :
769 : 30554 : CHECK_NONFATAL(it != m_args.end()); // TODO: ideally this is checked at compile time
770 : 30554 : return std::distance(m_args.begin(), it);
771 : : }
772 : :
773 : 1036 : std::string RPCHelpMan::ToString() const
774 : : {
775 [ + - ]: 1036 : std::string ret;
776 : :
777 : : // Oneline summary
778 [ + - ]: 1036 : ret += m_name;
779 : 1036 : bool was_optional{false};
780 [ + + ]: 2806 : for (const auto& arg : m_args) {
781 [ + + ]: 1779 : if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
782 [ + - ]: 1770 : const bool optional = arg.IsOptional();
783 [ + - ]: 1770 : ret += " ";
784 [ + + ]: 1770 : if (optional) {
785 [ + + + - ]: 991 : if (!was_optional) ret += "( ";
786 : : was_optional = true;
787 : : } else {
788 [ + + + - ]: 779 : if (was_optional) ret += ") ";
789 : : was_optional = false;
790 : : }
791 [ + - ]: 3540 : ret += arg.ToString(/*oneline=*/true);
792 : : }
793 [ + + + - ]: 1036 : if (was_optional) ret += " )";
794 : :
795 : : // Description
796 [ + - + - ]: 2072 : CHECK_NONFATAL(!m_description.starts_with('\n')); // Historically \n was required, but reject it for new code.
797 [ + - + - : 3108 : ret += "\n\n" + TrimString(m_description) + "\n";
+ - ]
798 : :
799 : : // Arguments
800 : 1036 : Sections sections;
801 : 1036 : Sections named_only_sections;
802 [ + + ]: 2806 : for (size_t i{0}; i < m_args.size(); ++i) {
803 [ + - ]: 1779 : const auto& arg = m_args.at(i);
804 [ + + ]: 1779 : if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
805 : :
806 : : // Push named argument name and description
807 [ + - + - : 5310 : sections.m_sections.emplace_back(util::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true));
+ - + - +
- ]
808 [ + + ]: 1770 : sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
809 : :
810 : : // Recursively push nested args
811 [ + - ]: 1770 : sections.Push(arg);
812 : :
813 : : // Push named-only argument sections
814 [ + + ]: 1770 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
815 [ + + ]: 375 : for (const auto& arg_inner : arg.m_inner) {
816 [ + - + - : 636 : named_only_sections.PushSection({arg_inner.GetFirstName(), arg_inner.ToDescriptionString(/*is_named_arg=*/true)});
+ - + - ]
817 [ + - ]: 318 : named_only_sections.Push(arg_inner);
818 : : }
819 : : }
820 : : }
821 : :
822 [ + + + - ]: 1036 : if (!sections.m_sections.empty()) ret += "\nArguments:\n";
823 [ + - ]: 2072 : ret += sections.ToString();
824 [ + + + - ]: 1036 : if (!named_only_sections.m_sections.empty()) ret += "\nNamed Arguments:\n";
825 [ + - ]: 2072 : ret += named_only_sections.ToString();
826 : :
827 : : // Result
828 [ + - ]: 2072 : ret += m_results.ToDescriptionString();
829 : :
830 : : // Examples
831 [ + - ]: 2072 : ret += m_examples.ToDescriptionString();
832 : :
833 : 1036 : return ret;
834 : 1036 : }
835 : :
836 : 169 : UniValue RPCHelpMan::GetArgMap() const
837 : : {
838 : 169 : UniValue arr{UniValue::VARR};
839 : :
840 : 598 : auto push_back_arg_info = [&arr](const std::string& rpc_name, int pos, const std::string& arg_name, const RPCArg::Type& type) {
841 : 429 : UniValue map{UniValue::VARR};
842 [ + - + - ]: 429 : map.push_back(rpc_name);
843 [ + - + - ]: 429 : map.push_back(pos);
844 [ + - + - ]: 429 : map.push_back(arg_name);
845 [ + - + - ]: 429 : map.push_back(type == RPCArg::Type::STR ||
846 : : type == RPCArg::Type::STR_HEX);
847 [ + - ]: 429 : arr.push_back(std::move(map));
848 : 429 : };
849 : :
850 [ + + ]: 500 : for (int i{0}; i < int(m_args.size()); ++i) {
851 [ + - ]: 331 : const auto& arg = m_args.at(i);
852 [ + - ]: 331 : std::vector<std::string> arg_names = SplitString(arg.m_names, '|');
853 [ + + ]: 664 : for (const auto& arg_name : arg_names) {
854 [ + - ]: 333 : push_back_arg_info(m_name, i, arg_name, arg.m_type);
855 [ + + ]: 333 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
856 [ + + ]: 109 : for (const auto& inner : arg.m_inner) {
857 [ + - ]: 96 : std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
858 [ + + ]: 192 : for (const std::string& inner_name : inner_names) {
859 [ + - ]: 96 : push_back_arg_info(m_name, i, inner_name, inner.m_type);
860 : : }
861 : 96 : }
862 : : }
863 : : }
864 : 331 : }
865 : 169 : return arr;
866 : 0 : }
867 : :
868 : 200250 : static std::optional<UniValue::VType> ExpectedType(RPCArg::Type type)
869 : : {
870 : 200250 : using Type = RPCArg::Type;
871 [ + + + + : 200250 : switch (type) {
+ + + - ]
872 : 119109 : case Type::STR_HEX:
873 : 119109 : case Type::STR: {
874 : 119109 : return UniValue::VSTR;
875 : : }
876 : 50263 : case Type::NUM: {
877 : 50263 : return UniValue::VNUM;
878 : : }
879 : 19742 : case Type::AMOUNT: {
880 : : // VNUM or VSTR, checked inside AmountFromValue()
881 : 19742 : return std::nullopt;
882 : : }
883 : 86 : case Type::RANGE: {
884 : : // VNUM or VARR, checked inside ParseRange()
885 : 86 : return std::nullopt;
886 : : }
887 : 3136 : case Type::BOOL: {
888 : 3136 : return UniValue::VBOOL;
889 : : }
890 : 2755 : case Type::OBJ:
891 : 2755 : case Type::OBJ_NAMED_PARAMS:
892 : 2755 : case Type::OBJ_USER_KEYS: {
893 : 2755 : return UniValue::VOBJ;
894 : : }
895 : 5159 : case Type::ARR: {
896 : 5159 : return UniValue::VARR;
897 : : }
898 : : } // no default case, so the compiler can warn about missing cases
899 [ # # ]: 0 : NONFATAL_UNREACHABLE();
900 : : }
901 : :
902 : 362087 : UniValue RPCArg::MatchesType(const UniValue& request) const
903 : : {
904 [ + + ]: 362087 : if (m_opts.skip_type_check) return true;
905 [ + + + + ]: 349597 : if (IsOptional() && request.isNull()) return true;
906 : 200250 : const auto exp_type{ExpectedType(m_type)};
907 [ + + ]: 200250 : if (!exp_type) return true; // nothing to check
908 : :
909 [ + + ]: 180422 : if (*exp_type != request.getType()) {
910 [ + - ]: 60 : return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));
911 : : }
912 : 180392 : return true;
913 : : }
914 : :
915 : 4703 : std::string RPCArg::GetFirstName() const
916 : : {
917 : 4703 : return m_names.substr(0, m_names.find('|'));
918 : : }
919 : :
920 : 61731 : std::string RPCArg::GetName() const
921 : : {
922 : 61731 : CHECK_NONFATAL(std::string::npos == m_names.find('|'));
923 : 61731 : return m_names;
924 : : }
925 : :
926 : 673628 : bool RPCArg::IsOptional() const
927 : : {
928 [ + + ]: 673628 : if (m_fallback.index() != 0) {
929 : : return true;
930 : : } else {
931 : 322909 : return RPCArg::Optional::NO != std::get<RPCArg::Optional>(m_fallback);
932 : : }
933 : : }
934 : :
935 : 2991 : std::string RPCArg::ToDescriptionString(bool is_named_arg) const
936 : : {
937 [ + - ]: 2991 : std::string ret;
938 [ + - ]: 2991 : ret += "(";
939 [ + + ]: 2991 : if (m_opts.type_str.size() != 0) {
940 [ + - + - ]: 32 : ret += m_opts.type_str.at(1);
941 : : } else {
942 [ + + + + : 2959 : switch (m_type) {
+ + + - ]
943 : 1256 : case Type::STR_HEX:
944 : 1256 : case Type::STR: {
945 [ + - ]: 1256 : ret += "string";
946 : : break;
947 : : }
948 : 471 : case Type::NUM: {
949 [ + - ]: 471 : ret += "numeric";
950 : : break;
951 : : }
952 : 150 : case Type::AMOUNT: {
953 [ + - ]: 150 : ret += "numeric or string";
954 : : break;
955 : : }
956 : 58 : case Type::RANGE: {
957 [ + - ]: 58 : ret += "numeric or array";
958 : : break;
959 : : }
960 : 437 : case Type::BOOL: {
961 [ + - ]: 437 : ret += "boolean";
962 : : break;
963 : : }
964 : 259 : case Type::OBJ:
965 : 259 : case Type::OBJ_NAMED_PARAMS:
966 : 259 : case Type::OBJ_USER_KEYS: {
967 [ + - ]: 259 : ret += "json object";
968 : : break;
969 : : }
970 : 328 : case Type::ARR: {
971 [ + - + + ]: 2991 : ret += "json array";
972 : : break;
973 : : }
974 : : } // no default case, so the compiler can warn about missing cases
975 : : }
976 [ + + ]: 2991 : if (m_fallback.index() == 1) {
977 [ + - ]: 778 : ret += ", optional, default=" + std::get<RPCArg::DefaultHint>(m_fallback);
978 [ + + ]: 2602 : } else if (m_fallback.index() == 2) {
979 [ + - + - ]: 1534 : ret += ", optional, default=" + std::get<RPCArg::Default>(m_fallback).write();
980 : : } else {
981 [ - + + + : 1835 : switch (std::get<RPCArg::Optional>(m_fallback)) {
- ]
982 : 769 : case RPCArg::Optional::OMITTED: {
983 [ + + + - ]: 769 : if (is_named_arg) ret += ", optional"; // Default value is "null" in dicts. Otherwise,
984 : : // nothing to do. Element is treated as if not present and has no default value
985 : : break;
986 : : }
987 : 1066 : case RPCArg::Optional::NO: {
988 [ + - ]: 1066 : ret += ", required";
989 : : break;
990 : : }
991 : : } // no default case, so the compiler can warn about missing cases
992 : : }
993 [ + - ]: 2991 : ret += ")";
994 [ + + + - ]: 2991 : if (m_type == Type::OBJ_NAMED_PARAMS) ret += " Options object that can be used to pass named arguments, listed below.";
995 [ + + + - : 5982 : ret += m_description.empty() ? "" : " " + m_description;
+ - ]
996 : 2991 : return ret;
997 : 0 : }
998 : :
999 : : // NOLINTNEXTLINE(misc-no-recursion)
1000 : 10553 : void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const int current_indent) const
1001 : : {
1002 : : // Indentation
1003 [ + - ]: 10553 : const std::string indent(current_indent, ' ');
1004 [ + - + + ]: 10553 : const std::string indent_next(current_indent + 2, ' ');
1005 : :
1006 : : // Elements in a JSON structure (dictionary or array) are separated by a comma
1007 [ + + + - ]: 11798 : const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""};
1008 : :
1009 : : // The key name if recursed into a dictionary
1010 : 10553 : const std::string maybe_key{
1011 [ + + ]: 10553 : outer_type == OuterType::OBJ ?
1012 [ + - - - ]: 16490 : "\"" + this->m_key_name + "\" : " :
1013 [ + - + - ]: 18798 : ""};
1014 : :
1015 : : // Format description with type
1016 : 21014 : const auto Description = [&](const std::string& type) {
1017 [ + + + - : 39742 : return "(" + type + (this->m_optional ? ", optional" : "") + ")" +
+ - ]
1018 [ + + + - ]: 31383 : (this->m_description.empty() ? "" : " " + this->m_description);
1019 : 10553 : };
1020 : :
1021 [ + - + + : 10553 : switch (m_type) {
+ + + + +
+ + - ]
1022 : 92 : case Type::ELISION: {
1023 : : // If the inner result is empty, use three dots for elision
1024 [ + - + - : 184 : sections.PushSection({indent + "..." + maybe_separator, m_description});
+ - + - ]
1025 : 92 : return;
1026 : : }
1027 : 0 : case Type::ANY: {
1028 [ # # ]: 0 : NONFATAL_UNREACHABLE(); // Only for testing
1029 : : }
1030 : 139 : case Type::NONE: {
1031 [ + - + - : 278 : sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
+ - + - +
- + - ]
1032 : 139 : return;
1033 : : }
1034 : 1819 : case Type::STR: {
1035 [ + - + - : 5457 : sections.PushSection({indent + maybe_key + "\"str\"" + maybe_separator, Description("string")});
+ - + - +
- + - ]
1036 : 1819 : return;
1037 : : }
1038 : 504 : case Type::STR_AMOUNT: {
1039 [ + - + - : 1512 : sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
+ - + - +
- + - ]
1040 : 504 : return;
1041 : : }
1042 : 1727 : case Type::STR_HEX: {
1043 [ + - + - : 5181 : sections.PushSection({indent + maybe_key + "\"hex\"" + maybe_separator, Description("string")});
+ - + - +
- + - ]
1044 : 1727 : return;
1045 : : }
1046 : 2645 : case Type::NUM: {
1047 [ + - + - : 7935 : sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
+ - + - +
- + - ]
1048 : 2645 : return;
1049 : : }
1050 : 321 : case Type::NUM_TIME: {
1051 [ + - + - : 963 : sections.PushSection({indent + maybe_key + "xxx" + maybe_separator, Description("numeric")});
+ - + - +
- + - ]
1052 : 321 : return;
1053 : : }
1054 : 689 : case Type::BOOL: {
1055 [ + - + - : 2067 : sections.PushSection({indent + maybe_key + "true|false" + maybe_separator, Description("boolean")});
+ - + - +
- + - ]
1056 : 689 : return;
1057 : : }
1058 : 1001 : case Type::ARR_FIXED:
1059 : 1001 : case Type::ARR: {
1060 [ + - + - : 3003 : sections.PushSection({indent + maybe_key + "[", Description("json array")});
+ - + - +
- ]
1061 [ + + ]: 2064 : for (const auto& i : m_inner) {
1062 [ + - ]: 1063 : i.ToSections(sections, OuterType::ARR, current_indent + 2);
1063 : : }
1064 [ + - ]: 1001 : CHECK_NONFATAL(!m_inner.empty());
1065 [ + + + + ]: 1001 : if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) {
1066 [ + - + - : 1962 : sections.PushSection({indent_next + "...", ""});
+ - + - ]
1067 : : } else {
1068 : : // Remove final comma, which would be invalid JSON
1069 : 20 : sections.m_sections.back().m_left.pop_back();
1070 : : }
1071 [ + - + - : 2002 : sections.PushSection({indent + "]" + maybe_separator, ""});
+ - + - +
- ]
1072 : 1001 : return;
1073 : : }
1074 : 1616 : case Type::OBJ_DYN:
1075 : 1616 : case Type::OBJ: {
1076 [ + + ]: 1616 : if (m_inner.empty()) {
1077 [ + - + - : 54 : sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")});
+ - + - +
- ]
1078 : 18 : return;
1079 : : }
1080 [ + - + - : 4794 : sections.PushSection({indent + maybe_key + "{", Description("json object")});
+ - + - +
- ]
1081 [ + + ]: 9843 : for (const auto& i : m_inner) {
1082 [ + - ]: 8245 : i.ToSections(sections, OuterType::OBJ, current_indent + 2);
1083 : : }
1084 [ + + - + ]: 1598 : if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) {
1085 : : // If the dictionary keys are dynamic, use three dots for continuation
1086 [ + - + - : 404 : sections.PushSection({indent_next + "...", ""});
+ - + - ]
1087 : : } else {
1088 : : // Remove final comma, which would be invalid JSON
1089 : 1396 : sections.m_sections.back().m_left.pop_back();
1090 : : }
1091 [ + - + - : 3196 : sections.PushSection({indent + "}" + maybe_separator, ""});
+ - + - +
- ]
1092 : 1598 : return;
1093 : : }
1094 : : } // no default case, so the compiler can warn about missing cases
1095 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1096 : 10553 : }
1097 : :
1098 : 4428638 : static std::optional<UniValue::VType> ExpectedType(RPCResult::Type type)
1099 : : {
1100 : 4428638 : using Type = RPCResult::Type;
1101 [ + + + + : 4428638 : switch (type) {
+ + + - ]
1102 : 12 : case Type::ELISION:
1103 : 12 : case Type::ANY: {
1104 : 12 : return std::nullopt;
1105 : : }
1106 : 36843 : case Type::NONE: {
1107 : 36843 : return UniValue::VNULL;
1108 : : }
1109 : 1759598 : case Type::STR:
1110 : 1759598 : case Type::STR_HEX: {
1111 : 1759598 : return UniValue::VSTR;
1112 : : }
1113 : 1633426 : case Type::NUM:
1114 : 1633426 : case Type::STR_AMOUNT:
1115 : 1633426 : case Type::NUM_TIME: {
1116 : 1633426 : return UniValue::VNUM;
1117 : : }
1118 : 410265 : case Type::BOOL: {
1119 : 410265 : return UniValue::VBOOL;
1120 : : }
1121 : 175484 : case Type::ARR_FIXED:
1122 : 175484 : case Type::ARR: {
1123 : 175484 : return UniValue::VARR;
1124 : : }
1125 : 413010 : case Type::OBJ_DYN:
1126 : 413010 : case Type::OBJ: {
1127 : 413010 : return UniValue::VOBJ;
1128 : : }
1129 : : } // no default case, so the compiler can warn about missing cases
1130 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1131 : : }
1132 : :
1133 : : // NOLINTNEXTLINE(misc-no-recursion)
1134 : 4428987 : UniValue RPCResult::MatchesType(const UniValue& result) const
1135 : : {
1136 [ + + ]: 4428987 : if (m_skip_type_check) {
1137 : 349 : return true;
1138 : : }
1139 : :
1140 : 4428638 : const auto exp_type = ExpectedType(m_type);
1141 [ + + ]: 4428638 : if (!exp_type) return true; // can be any type, so nothing to check
1142 : :
1143 [ + + ]: 4428626 : if (*exp_type != result.getType()) {
1144 [ + - ]: 55892 : return strprintf("returned type is %s, but declared as %s in doc", uvTypeName(result.getType()), uvTypeName(*exp_type));
1145 : : }
1146 : :
1147 [ + + ]: 4400680 : if (UniValue::VARR == result.getType()) {
1148 : 174382 : UniValue errors(UniValue::VOBJ);
1149 [ + - + + ]: 902434 : for (size_t i{0}; i < result.get_array().size(); ++i) {
1150 : : // If there are more results than documented, reuse the last doc_inner.
1151 [ + + + - ]: 1455780 : const RPCResult& doc_inner{m_inner.at(std::min(m_inner.size() - 1, i))};
1152 [ + - + - : 728052 : UniValue match{doc_inner.MatchesType(result.get_array()[i])};
+ - ]
1153 [ + + + - : 738269 : if (!match.isTrue()) errors.pushKV(strprintf("%d", i), std::move(match));
+ - ]
1154 : 728052 : }
1155 [ + + + - ]: 174382 : if (errors.empty()) return true; // empty result array is valid
1156 : 272 : return errors;
1157 : 174382 : }
1158 : :
1159 [ + + ]: 4226298 : if (UniValue::VOBJ == result.getType()) {
1160 [ + + + + ]: 412542 : if (!m_inner.empty() && m_inner.at(0).m_type == Type::ELISION) return true;
1161 : 411397 : UniValue errors(UniValue::VOBJ);
1162 [ + + ]: 411397 : if (m_type == Type::OBJ_DYN) {
1163 [ + - ]: 27272 : const RPCResult& doc_inner{m_inner.at(0)}; // Assume all types are the same, randomly pick the first
1164 [ + - + + ]: 261756 : for (size_t i{0}; i < result.get_obj().size(); ++i) {
1165 [ + - + - : 234484 : UniValue match{doc_inner.MatchesType(result.get_obj()[i])};
+ - ]
1166 [ + + + - : 234516 : if (!match.isTrue()) errors.pushKV(result.getKeys()[i], std::move(match));
+ - + - ]
1167 : 234484 : }
1168 [ + + + - ]: 27272 : if (errors.empty()) return true; // empty result obj is valid
1169 : 16 : return errors;
1170 : : }
1171 : 384125 : std::set<std::string> doc_keys;
1172 [ + + ]: 4053995 : for (const auto& doc_entry : m_inner) {
1173 [ + - ]: 3669870 : doc_keys.insert(doc_entry.m_key_name);
1174 : : }
1175 [ + - ]: 384125 : std::map<std::string, UniValue> result_obj;
1176 [ + - ]: 384125 : result.getObjMap(result_obj);
1177 [ + + ]: 3639813 : for (const auto& result_entry : result_obj) {
1178 [ + + ]: 3255688 : if (doc_keys.find(result_entry.first) == doc_keys.end()) {
1179 [ + - + - : 42 : errors.pushKV(result_entry.first, "key returned that was not in doc");
+ - ]
1180 : : }
1181 : : }
1182 : :
1183 [ + + ]: 4053995 : for (const auto& doc_entry : m_inner) {
1184 : 3669870 : const auto result_it{result_obj.find(doc_entry.m_key_name)};
1185 [ + + ]: 3669870 : if (result_it == result_obj.end()) {
1186 [ - + ]: 414203 : if (!doc_entry.m_optional) {
1187 [ # # # # : 0 : errors.pushKV(doc_entry.m_key_name, "key missing, despite not being optional in doc");
# # ]
1188 : : }
1189 : 414203 : continue;
1190 : : }
1191 [ + - ]: 3255667 : UniValue match{doc_entry.MatchesType(result_it->second)};
1192 [ + + + - : 3255797 : if (!match.isTrue()) errors.pushKV(doc_entry.m_key_name, std::move(match));
+ - ]
1193 : 3255667 : }
1194 [ + + + - ]: 384125 : if (errors.empty()) return true;
1195 : 146 : return errors;
1196 : 411397 : }
1197 : :
1198 : 3813756 : return true;
1199 : : }
1200 : :
1201 : 4476571 : void RPCResult::CheckInnerDoc() const
1202 : : {
1203 [ + + ]: 4476571 : if (m_type == Type::OBJ) {
1204 : : // May or may not be empty
1205 : : return;
1206 : : }
1207 : : // Everything else must either be empty or not
1208 [ + + + + ]: 3932644 : const bool inner_needed{m_type == Type::ARR || m_type == Type::ARR_FIXED || m_type == Type::OBJ_DYN};
1209 : 3932644 : CHECK_NONFATAL(inner_needed != m_inner.empty());
1210 : : }
1211 : :
1212 : : // NOLINTNEXTLINE(misc-no-recursion)
1213 : 756 : std::string RPCArg::ToStringObj(const bool oneline) const
1214 : : {
1215 [ + - ]: 756 : std::string res;
1216 [ + - ]: 756 : res += "\"";
1217 [ + - ]: 1512 : res += GetFirstName();
1218 [ + + ]: 756 : if (oneline) {
1219 [ + - ]: 341 : res += "\":";
1220 : : } else {
1221 [ + - ]: 415 : res += "\": ";
1222 : : }
1223 [ + + + + : 756 : switch (m_type) {
+ + + -
- ]
1224 : 109 : case Type::STR:
1225 [ + - ]: 109 : return res + "\"str\"";
1226 : 259 : case Type::STR_HEX:
1227 [ + - ]: 259 : return res + "\"hex\"";
1228 : 187 : case Type::NUM:
1229 [ + - ]: 187 : return res + "n";
1230 : 67 : case Type::RANGE:
1231 [ + - ]: 67 : return res + "n or [n,n]";
1232 : 98 : case Type::AMOUNT:
1233 [ + - ]: 98 : return res + "amount";
1234 : 18 : case Type::BOOL:
1235 [ + - ]: 18 : return res + "bool";
1236 : 18 : case Type::ARR:
1237 [ + - ]: 18 : res += "[";
1238 [ + + ]: 45 : for (const auto& i : m_inner) {
1239 [ + - + - ]: 81 : res += i.ToString(oneline) + ",";
1240 : : }
1241 [ + - ]: 18 : return res + "...]";
1242 : 0 : case Type::OBJ:
1243 : 0 : case Type::OBJ_NAMED_PARAMS:
1244 : 0 : case Type::OBJ_USER_KEYS:
1245 : : // Currently unused, so avoid writing dead code
1246 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1247 : : } // no default case, so the compiler can warn about missing cases
1248 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1249 : 756 : }
1250 : :
1251 : : // NOLINTNEXTLINE(misc-no-recursion)
1252 : 2307 : std::string RPCArg::ToString(const bool oneline) const
1253 : : {
1254 [ + + + + ]: 2307 : if (oneline && !m_opts.oneline_description.empty()) {
1255 [ + + + - : 84 : if (m_opts.oneline_description[0] == '\"' && m_type != Type::STR_HEX && m_type != Type::STR && gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) {
- + - - -
- - + ]
1256 : 0 : throw std::runtime_error{
1257 [ # # # # ]: 0 : STR_INTERNAL_BUG(strprintf("non-string RPC arg \"%s\" quotes oneline_description:\n%s",
1258 : : m_names, m_opts.oneline_description)
1259 [ # # # # ]: 0 : )};
1260 : : }
1261 : 84 : return m_opts.oneline_description;
1262 : : }
1263 : :
1264 [ + + + + : 2223 : switch (m_type) {
- ]
1265 : 1165 : case Type::STR_HEX:
1266 : 1165 : case Type::STR: {
1267 [ + - ]: 2330 : return "\"" + GetFirstName() + "\"";
1268 : : }
1269 : 694 : case Type::NUM:
1270 : 694 : case Type::RANGE:
1271 : 694 : case Type::AMOUNT:
1272 : 694 : case Type::BOOL: {
1273 : 694 : return GetFirstName();
1274 : : }
1275 : 156 : case Type::OBJ:
1276 : 156 : case Type::OBJ_NAMED_PARAMS:
1277 : 156 : case Type::OBJ_USER_KEYS: {
1278 : : // NOLINTNEXTLINE(misc-no-recursion)
1279 [ + - ]: 497 : const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); });
1280 [ + + ]: 156 : if (m_type == Type::OBJ) {
1281 [ + - ]: 208 : return "{" + res + "}";
1282 : : } else {
1283 [ + - ]: 104 : return "{" + res + ",...}";
1284 : : }
1285 : 156 : }
1286 : 208 : case Type::ARR: {
1287 : 208 : std::string res;
1288 [ + + ]: 465 : for (const auto& i : m_inner) {
1289 [ + - + - ]: 771 : res += i.ToString(oneline) + ",";
1290 : : }
1291 [ + - ]: 416 : return "[" + res + "...]";
1292 : 208 : }
1293 : : } // no default case, so the compiler can warn about missing cases
1294 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1295 : : }
1296 : :
1297 : 280 : static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
1298 : : {
1299 [ + + ]: 280 : if (value.isNum()) {
1300 : 86 : return {0, value.getInt<int64_t>()};
1301 : : }
1302 [ + - + - : 194 : if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
+ - + - ]
1303 : 194 : int64_t low = value[0].getInt<int64_t>();
1304 : 194 : int64_t high = value[1].getInt<int64_t>();
1305 [ + + + - : 198 : if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
+ - ]
1306 : 190 : return {low, high};
1307 : : }
1308 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
1309 : : }
1310 : :
1311 : 280 : std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value)
1312 : : {
1313 : 280 : int64_t low, high;
1314 [ + + ]: 280 : std::tie(low, high) = ParseRange(value);
1315 [ + + ]: 276 : if (low < 0) {
1316 [ + - + - ]: 8 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0");
1317 : : }
1318 [ + + ]: 272 : if ((high >> 31) != 0) {
1319 [ + - + - ]: 12 : throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high");
1320 : : }
1321 [ + + ]: 266 : if (high >= low + 1000000) {
1322 [ + - + - ]: 8 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large");
1323 : : }
1324 : 262 : return {low, high};
1325 : : }
1326 : :
1327 : 1180 : std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider, const bool expand_priv)
1328 : : {
1329 [ + + ]: 1180 : std::string desc_str;
1330 : 1180 : std::pair<int64_t, int64_t> range = {0, 1000};
1331 [ + + ]: 1180 : if (scanobject.isStr()) {
1332 [ + - + - ]: 1082 : desc_str = scanobject.get_str();
1333 [ + - ]: 98 : } else if (scanobject.isObject()) {
1334 [ + - ]: 98 : const UniValue& desc_uni{scanobject.find_value("desc")};
1335 [ - + - - : 98 : if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
- - ]
1336 [ + - + - ]: 98 : desc_str = desc_uni.get_str();
1337 [ + - ]: 98 : const UniValue& range_uni{scanobject.find_value("range")};
1338 [ + + ]: 98 : if (!range_uni.isNull()) {
1339 [ + + ]: 91 : range = ParseDescriptorRange(range_uni);
1340 : : }
1341 : : } else {
1342 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
1343 : : }
1344 : :
1345 [ + - ]: 1175 : std::string error;
1346 [ + - ]: 1175 : auto descs = Parse(desc_str, provider, error);
1347 [ + + ]: 1175 : if (descs.empty()) {
1348 [ + - ]: 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
1349 : : }
1350 [ + - + - : 1174 : if (!descs.at(0)->IsRange()) {
+ + ]
1351 : 1088 : range.first = 0;
1352 : 1088 : range.second = 0;
1353 : : }
1354 : 1174 : std::vector<CScript> ret;
1355 [ + + ]: 20450 : for (int i = range.first; i <= range.second; ++i) {
1356 [ + + ]: 38553 : for (const auto& desc : descs) {
1357 : 19277 : std::vector<CScript> scripts;
1358 [ + - - + ]: 19277 : if (!desc->Expand(i, provider, scripts, provider)) {
1359 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
1360 : : }
1361 [ + + ]: 19277 : if (expand_priv) {
1362 [ + - ]: 84 : desc->ExpandPrivate(/*pos=*/i, provider, /*out=*/provider);
1363 : : }
1364 [ + - ]: 19277 : std::move(scripts.begin(), scripts.end(), std::back_inserter(ret));
1365 : 19277 : }
1366 : : }
1367 : 2348 : return ret;
1368 : 1176 : }
1369 : :
1370 : : /** Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values. */
1371 : 15 : [[nodiscard]] static UniValue BilingualStringsToUniValue(const std::vector<bilingual_str>& bilingual_strings)
1372 : : {
1373 : 15 : CHECK_NONFATAL(!bilingual_strings.empty());
1374 : 15 : UniValue result{UniValue::VARR};
1375 [ + + ]: 30 : for (const auto& s : bilingual_strings) {
1376 [ + - + - ]: 15 : result.push_back(s.original);
1377 : : }
1378 : 15 : return result;
1379 : 0 : }
1380 : :
1381 : 712 : void PushWarnings(const UniValue& warnings, UniValue& obj)
1382 : : {
1383 [ + + ]: 712 : if (warnings.empty()) return;
1384 [ + - + - ]: 612 : obj.pushKV("warnings", warnings);
1385 : : }
1386 : :
1387 : 917 : void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj)
1388 : : {
1389 [ + + ]: 917 : if (warnings.empty()) return;
1390 [ + - + - ]: 30 : obj.pushKV("warnings", BilingualStringsToUniValue(warnings));
1391 : : }
1392 : :
1393 : 20916 : std::vector<RPCResult> ScriptPubKeyDoc() {
1394 : 20916 : return
1395 : : {
1396 : : {RPCResult::Type::STR, "asm", "Disassembly of the output script"},
1397 : : {RPCResult::Type::STR, "desc", "Inferred descriptor for the output"},
1398 : : {RPCResult::Type::STR_HEX, "hex", "The raw output script bytes, hex-encoded"},
1399 : : {RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"},
1400 [ + - + - ]: 41832 : {RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"},
1401 [ + - + - : 188244 : };
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + + -
- ]
1402 [ + - + - : 125496 : }
+ - + - +
- + - -
- ]
1403 : :
1404 : 24139 : uint256 GetTarget(const CBlockIndex& blockindex, const uint256 pow_limit)
1405 : : {
1406 : 24139 : arith_uint256 target{*CHECK_NONFATAL(DeriveTarget(blockindex.nBits, pow_limit))};
1407 : 24139 : return ArithToUint256(target);
1408 : : }
|