Coverage Report

Created: 2025-06-10 13:21

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