Coverage Report

Created: 2026-04-29 19:21

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