/Users/eugenesiegel/btc/bitcoin/src/rpc/util.cpp
| Line | Count | Source (jump to first uncovered line) | 
| 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 | 0 | { | 
| 51 | 0 |     std::vector<std::string> ret; | 
| 52 | 0 |     using U = std::underlying_type_t<TxoutType>; | 
| 53 | 0 |     for (U i = (U)TxoutType::NONSTANDARD; i <= (U)TxoutType::WITNESS_UNKNOWN; ++i) { | 
| 54 | 0 |         ret.emplace_back(GetTxnOutputType(static_cast<TxoutType>(i))); | 
| 55 | 0 |     } | 
| 56 | 0 |     return Join(ret, ", "); | 
| 57 | 0 | } | 
| 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) { | 
| 65 | 0 |         const UniValue& v = o.find_value(t.first); | 
| 66 | 0 |         if (!fAllowNull && v.isNull()) | 
| 67 | 0 |             throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 68 |  |  | 
| 69 | 0 |         if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull()))) | 
| 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)));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 71 | 0 |     } | 
| 72 |  |  | 
| 73 | 0 |     if (fStrict) | 
| 74 | 0 |     { | 
| 75 | 0 |         for (const std::string& k : o.getKeys()) | 
| 76 | 0 |         { | 
| 77 | 0 |             if (typesExpected.count(k) == 0) | 
| 78 | 0 |             { | 
| 79 | 0 |                 std::string err = strprintf("Unexpected key %s", k);| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 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()) { | 
| 89 | 0 |         if (arg.isBool()) { | 
| 90 | 0 |             if (!allow_bool) { | 
| 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()) | 
| 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)) | 
| 107 | 0 |         throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount"); | 
| 108 | 0 |     if (!MoneyRange(amount)) | 
| 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"); | 
| 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; | 
| 124 | 0 |     if (auto expected_len{uint256::size() * 2}; strHex.length() != expected_len) { | 
| 125 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, expected_len, strHex.length(), strHex));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 126 | 0 |     } | 
| 127 | 0 |     throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 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()) | 
| 137 | 0 |         strHex = v.get_str(); | 
| 138 | 0 |     if (!IsHex(strHex)) | 
| 139 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 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 | 0 | { | 
| 156 | 0 |     std::string result; | 
| 157 | 0 |     result.reserve(s.size() * 2); | 
| 158 | 0 |     for (const char ch: s) { | 
| 159 | 0 |         if (ch == '\'') { | 
| 160 | 0 |             result += "'\''"; | 
| 161 | 0 |         } else { | 
| 162 | 0 |             result += ch; | 
| 163 | 0 |         } | 
| 164 | 0 |     } | 
| 165 | 0 |     return "'" + result + "'"; | 
| 166 | 0 | } | 
| 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 | 0 | { | 
| 175 | 0 |     for (const char ch: s) { | 
| 176 | 0 |         if (ch == ' ' || ch == '\'' || ch == '"') { | 
| 177 | 0 |             return ShellQuote(s); | 
| 178 | 0 |         } | 
| 179 | 0 |     } | 
| 180 |  |  | 
| 181 | 0 |     return s; | 
| 182 | 0 | } | 
| 183 |  |  | 
| 184 |  | } | 
| 185 |  |  | 
| 186 |  | std::string HelpExampleCli(const std::string& methodname, const std::string& args) | 
| 187 | 0 | { | 
| 188 | 0 |     return "> bitcoin-cli " + methodname + " " + args + "\n"; | 
| 189 | 0 | } | 
| 190 |  |  | 
| 191 |  | std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList& args) | 
| 192 | 0 | { | 
| 193 | 0 |     std::string result = "> bitcoin-cli -named " + methodname; | 
| 194 | 0 |     for (const auto& argpair: args) { | 
| 195 | 0 |         const auto& value = argpair.second.isStr() | 
| 196 | 0 |                 ? argpair.second.get_str() | 
| 197 | 0 |                 : argpair.second.write(); | 
| 198 | 0 |         result += " " + argpair.first + "=" + ShellQuoteIfNeeded(value); | 
| 199 | 0 |     } | 
| 200 | 0 |     result += "\n"; | 
| 201 | 0 |     return result; | 
| 202 | 0 | } | 
| 203 |  |  | 
| 204 |  | std::string HelpExampleRpc(const std::string& methodname, const std::string& args) | 
| 205 | 0 | { | 
| 206 | 0 |     return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", " | 
| 207 | 0 |         "\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: application/json' http://127.0.0.1:8332/\n"; | 
| 208 | 0 | } | 
| 209 |  |  | 
| 210 |  | std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList& args) | 
| 211 | 0 | { | 
| 212 | 0 |     UniValue params(UniValue::VOBJ); | 
| 213 | 0 |     for (const auto& param: args) { | 
| 214 | 0 |         params.pushKV(param.first, param.second); | 
| 215 | 0 |     } | 
| 216 |  | 
 | 
| 217 | 0 |     return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", " | 
| 218 | 0 |            "\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: application/json' http://127.0.0.1:8332/\n"; | 
| 219 | 0 | } | 
| 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)) { | 
| 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) { | 
| 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()) { | 
| 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) { | 
| 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) { | 
| 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));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 246 | 0 |     } | 
| 247 | 0 |     if (pubkeys.size() > MAX_PUBKEYS_PER_MULTISIG) { | 
| 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));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 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) { | 
| 255 | 0 |         if (!pk.IsCompressed()) { | 
| 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) { | 
| 262 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE)));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 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()) { | 
| 363 | 0 |         return std::nullopt; | 
| 364 | 0 |     } | 
| 365 | 0 |     const auto result{SighashFromStr(sighash.get_str())}; | 
| 366 | 0 |     if (!result) { | 
| 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) { | 
| 377 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u and %u", 1, max_target));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 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: | 
| 386 | 0 |             return RPC_INVALID_PARAMETER; | 
| 387 | 0 |         case PSBTError::SIGHASH_MISMATCH: | 
| 388 | 0 |             return RPC_DESERIALIZATION_ERROR; | 
| 389 | 0 |         default: break; | 
| 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: | 
| 398 | 0 |             return RPC_TRANSACTION_REJECTED; | 
| 399 | 0 |         case TransactionError::ALREADY_IN_UTXO_SET: | 
| 400 | 0 |             return RPC_VERIFY_ALREADY_IN_UTXO_SET; | 
| 401 | 0 |         default: break; | 
| 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) { | 
| 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) { | 
| 457 | 0 |         case RPCArg::Type::STR_HEX: | 
| 458 | 0 |         case RPCArg::Type::STR: | 
| 459 | 0 |         case RPCArg::Type::NUM: | 
| 460 | 0 |         case RPCArg::Type::AMOUNT: | 
| 461 | 0 |         case RPCArg::Type::RANGE: | 
| 462 | 0 |         case RPCArg::Type::BOOL: | 
| 463 | 0 |         case RPCArg::Type::OBJ_NAMED_PARAMS: { | 
| 464 | 0 |             if (is_top_level_arg) return; // Nothing more to do for non-recursive types on first recursion | 
| 465 | 0 |             auto left = indent; | 
| 466 | 0 |             if (arg.m_opts.type_str.size() != 0 && push_name) { | 
| 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); | 
| 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: | 
| 476 | 0 |         case RPCArg::Type::OBJ_USER_KEYS: { | 
| 477 | 0 |             const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name); | 
| 478 | 0 |             PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right}); | 
| 479 | 0 |             for (const auto& arg_inner : arg.m_inner) { | 
| 480 | 0 |                 Push(arg_inner, current_indent + 2, OuterType::OBJ); | 
| 481 | 0 |             } | 
| 482 | 0 |             if (arg.m_type != RPCArg::Type::OBJ) { | 
| 483 | 0 |                 PushSection({indent_next + "...", ""}); | 
| 484 | 0 |             } | 
| 485 | 0 |             PushSection({indent + "}" + (is_top_level_arg ? "" : ","), ""}); | 
| 486 | 0 |             break; | 
| 487 | 0 |         } | 
| 488 | 0 |         case RPCArg::Type::ARR: { | 
| 489 | 0 |             auto left = indent; | 
| 490 | 0 |             left += push_name ? "\"" + arg.GetName() + "\": " : ""; | 
| 491 | 0 |             left += "["; | 
| 492 | 0 |             const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name); | 
| 493 | 0 |             PushSection({left, right}); | 
| 494 | 0 |             for (const auto& arg_inner : arg.m_inner) { | 
| 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 ? "" : ","), ""}); | 
| 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) { | 
| 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);| Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 515 | 0 |             if (s.m_right.empty()) { | 
| 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) { | 
| 530 | 0 |                 right += s.m_right.substr(begin, new_line_pos - begin); | 
| 531 | 0 |                 if (new_line_pos == std::string::npos) { | 
| 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) { | 
| 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} {}Unexecuted instantiation: _ZN10RPCHelpManC2ENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES6_NS0_6vectorI6RPCArgNS4_IS8_EEEE10RPCResults11RPCExamplesUnexecuted instantiation: _ZN10RPCHelpManC1ENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES6_NS0_6vectorI6RPCArgNS4_IS8_EEEE10RPCResults11RPCExamples | 
| 550 |  |  | 
| 551 |  | RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun) | 
| 552 | 0 |     : m_name{std::move(name)}, | 
| 553 | 0 |       m_fun{std::move(fun)}, | 
| 554 | 0 |       m_description{std::move(description)}, | 
| 555 | 0 |       m_args{std::move(args)}, | 
| 556 | 0 |       m_results{std::move(results)}, | 
| 557 | 0 |       m_examples{std::move(examples)} | 
| 558 | 0 | { | 
| 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 | 0 |     enum ParamType { POSITIONAL = 1, NAMED = 2, NAMED_ONLY = 4 }; | 
| 563 | 0 |     std::map<std::string, int> param_names; | 
| 564 |  | 
 | 
| 565 | 0 |     for (const auto& arg : m_args) { | 
| 566 | 0 |         std::vector<std::string> names = SplitString(arg.m_names, '|'); | 
| 567 |  |         // Should have unique named arguments | 
| 568 | 0 |         for (const std::string& name : names) { | 
| 569 | 0 |             auto& param_type = param_names[name]; | 
| 570 | 0 |             CHECK_NONFATAL(!(param_type & POSITIONAL)); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 571 | 0 |             CHECK_NONFATAL(!(param_type & NAMED_ONLY)); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 572 | 0 |             param_type |= POSITIONAL; | 
| 573 | 0 |         } | 
| 574 | 0 |         if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { | 
| 575 | 0 |             for (const auto& inner : arg.m_inner) { | 
| 576 | 0 |                 std::vector<std::string> inner_names = SplitString(inner.m_names, '|'); | 
| 577 | 0 |                 for (const std::string& inner_name : inner_names) { | 
| 578 | 0 |                     auto& param_type = param_names[inner_name]; | 
| 579 | 0 |                     CHECK_NONFATAL(!(param_type & POSITIONAL) || inner.m_opts.also_positional); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 580 | 0 |                     CHECK_NONFATAL(!(param_type & NAMED)); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 581 | 0 |                     CHECK_NONFATAL(!(param_type & NAMED_ONLY)); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 582 | 0 |                     param_type |= inner.m_opts.also_positional ? NAMED : NAMED_ONLY; | 
| 583 | 0 |                 } | 
| 584 | 0 |             } | 
| 585 | 0 |         } | 
| 586 |  |         // Default value type should match argument type only when defined | 
| 587 | 0 |         if (arg.m_fallback.index() == 2) { | 
| 588 | 0 |             const RPCArg::Type type = arg.m_type; | 
| 589 | 0 |             switch (std::get<RPCArg::Default>(arg.m_fallback).getType()) { | 
| 590 | 0 |             case UniValue::VOBJ: | 
| 591 | 0 |                 CHECK_NONFATAL(type == RPCArg::Type::OBJ); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 592 | 0 |                 break; | 
| 593 | 0 |             case UniValue::VARR: | 
| 594 | 0 |                 CHECK_NONFATAL(type == RPCArg::Type::ARR); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 595 | 0 |                 break; | 
| 596 | 0 |             case UniValue::VSTR: | 
| 597 | 0 |                 CHECK_NONFATAL(type == RPCArg::Type::STR || type == RPCArg::Type::STR_HEX || type == RPCArg::Type::AMOUNT); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 598 | 0 |                 break; | 
| 599 | 0 |             case UniValue::VNUM: | 
| 600 | 0 |                 CHECK_NONFATAL(type == RPCArg::Type::NUM || type == RPCArg::Type::AMOUNT || type == RPCArg::Type::RANGE); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 601 | 0 |                 break; | 
| 602 | 0 |             case UniValue::VBOOL: | 
| 603 | 0 |                 CHECK_NONFATAL(type == RPCArg::Type::BOOL); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 604 | 0 |                 break; | 
| 605 | 0 |             case UniValue::VNULL: | 
| 606 |  |                 // Null values are accepted in all arguments | 
| 607 | 0 |                 break; | 
| 608 | 0 |             default: | 
| 609 | 0 |                 NONFATAL_UNREACHABLE(); | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 610 | 0 |                 break; | 
| 611 | 0 |             } | 
| 612 | 0 |         } | 
| 613 | 0 |     } | 
| 614 | 0 | } | 
| 615 |  |  | 
| 616 |  | std::string RPCResults::ToDescriptionString() const | 
| 617 | 0 | { | 
| 618 | 0 |     std::string result; | 
| 619 | 0 |     for (const auto& r : m_results) { | 
| 620 | 0 |         if (r.m_type == RPCResult::Type::ANY) continue; // for testing only | 
| 621 | 0 |         if (r.m_cond.empty()) { | 
| 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; | 
| 636 | 0 | } | 
| 637 |  |  | 
| 638 |  | UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const | 
| 639 | 0 | { | 
| 640 | 0 |     if (request.mode == JSONRPCRequest::GET_ARGS) { | 
| 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 | 0 |     if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) { | 
| 648 | 0 |         throw HelpResult{ToString()}; | 
| 649 | 0 |     } | 
| 650 | 0 |     UniValue arg_mismatch{UniValue::VOBJ}; | 
| 651 | 0 |     for (size_t i{0}; i < m_args.size(); ++i) { | 
| 652 | 0 |         const auto& arg{m_args.at(i)}; | 
| 653 | 0 |         UniValue match{arg.MatchesType(request.params[i])}; | 
| 654 | 0 |         if (!match.isTrue()) { | 
| 655 | 0 |             arg_mismatch.pushKV(strprintf("Position %s (%s)", i + 1, arg.m_names), std::move(match));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 656 | 0 |         } | 
| 657 | 0 |     } | 
| 658 | 0 |     if (!arg_mismatch.empty()) { | 
| 659 | 0 |         throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Wrong type passed:\n%s", arg_mismatch.write(4)));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 660 | 0 |     } | 
| 661 | 0 |     CHECK_NONFATAL(m_req == nullptr); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 662 | 0 |     m_req = &request; | 
| 663 | 0 |     UniValue ret = m_fun(*this, request); | 
| 664 | 0 |     m_req = nullptr; | 
| 665 | 0 |     if (gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) { | 
| 666 | 0 |         UniValue mismatch{UniValue::VARR}; | 
| 667 | 0 |         for (const auto& res : m_results.m_results) { | 
| 668 | 0 |             UniValue match{res.MatchesType(ret)}; | 
| 669 | 0 |             if (match.isTrue()) { | 
| 670 | 0 |                 mismatch.setNull(); | 
| 671 | 0 |                 break; | 
| 672 | 0 |             } | 
| 673 | 0 |             mismatch.push_back(std::move(match)); | 
| 674 | 0 |         } | 
| 675 | 0 |         if (!mismatch.isNull()) { | 
| 676 | 0 |             std::string explain{ | 
| 677 | 0 |                 mismatch.empty() ? "no possible results defined" : | 
| 678 | 0 |                 mismatch.size() == 1 ? mismatch[0].write(4) : | 
| 679 | 0 |                 mismatch.write(4)}; | 
| 680 | 0 |             throw std::runtime_error{ | 
| 681 | 0 |                 strprintf("Internal bug detected: RPC call \"%s\" returned incorrect type:\n%s\n%s %s\nPlease report this issue here: %s\n",| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 682 | 0 |                           m_name, explain, | 
| 683 | 0 |                           CLIENT_NAME, FormatFullVersion(), | Line | Count | Source |  | 98 | 0 | #define CLIENT_NAME "Bitcoin Core" | 
 | 
| 684 | 0 |                           CLIENT_BUGREPORT)}; | Line | Count | Source |  | 95 | 0 | #define CLIENT_BUGREPORT "https://github.com/bitcoin/bitcoin/issues" | 
 | 
| 685 | 0 |         } | 
| 686 | 0 |     } | 
| 687 | 0 |     return ret; | 
| 688 | 0 | } | 
| 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 | 0 | { | 
| 693 | 0 |     CHECK_NONFATAL(i < params.size()); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 694 | 0 |     const UniValue& arg{CHECK_NONFATAL(req)->params[i]};| Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 695 | 0 |     const RPCArg& param{params.at(i)}; | 
| 696 | 0 |     if (check) check(param); | 
| 697 |  | 
 | 
| 698 | 0 |     if (!arg.isNull()) return &arg; | 
| 699 | 0 |     if (!std::holds_alternative<RPCArg::Default>(param.m_fallback)) return nullptr; | 
| 700 | 0 |     return &std::get<RPCArg::Default>(param.m_fallback); | 
| 701 | 0 | } | 
| 702 |  |  | 
| 703 |  | static void CheckRequiredOrDefault(const RPCArg& param) | 
| 704 | 0 | { | 
| 705 |  |     // Must use `Arg<Type>(key)` to get the argument or its default value. | 
| 706 | 0 |     const bool required{ | 
| 707 | 0 |         std::holds_alternative<RPCArg::Optional>(param.m_fallback) && RPCArg::Optional::NO == std::get<RPCArg::Optional>(param.m_fallback), | 
| 708 | 0 |     }; | 
| 709 | 0 |     CHECK_NONFATAL(required || std::holds_alternative<RPCArg::Default>(param.m_fallback)); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 710 | 0 | } | 
| 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 | 0 |     {                                                       \ | 
| 716 | 0 |         const UniValue* maybe_arg{                          \ | 
| 717 | 0 |             DetailMaybeArg(check_param, m_args, m_req, i),  \ | 
| 718 | 0 |         };                                                  \ | 
| 719 | 0 |         return return_code                                  \ | 
| 720 | 0 |     }                                                       \ Unexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIPK8UniValueEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueINSt3__18optionalIdEEEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueINSt3__18optionalIbEEEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIPKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIRK8UniValueEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIbEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIiEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIyEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIjEET_mUnexecuted instantiation: _ZNK10RPCHelpMan8ArgValueIRKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEET_m | 
| 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, uint32_t, CHECK_NONFATAL(maybe_arg)->getInt<uint32_t>();); | 
| 735 |  | TMPL_INST(CheckRequiredOrDefault, const std::string&, CHECK_NONFATAL(maybe_arg)->get_str();); | 
| 736 |  |  | 
| 737 |  | bool RPCHelpMan::IsValidNumArgs(size_t num_args) const | 
| 738 | 0 | { | 
| 739 | 0 |     size_t num_required_args = 0; | 
| 740 | 0 |     for (size_t n = m_args.size(); n > 0; --n) { | 
| 741 | 0 |         if (!m_args.at(n - 1).IsOptional()) { | 
| 742 | 0 |             num_required_args = n; | 
| 743 | 0 |             break; | 
| 744 | 0 |         } | 
| 745 | 0 |     } | 
| 746 | 0 |     return num_required_args <= num_args && num_args <= m_args.size(); | 
| 747 | 0 | } | 
| 748 |  |  | 
| 749 |  | std::vector<std::pair<std::string, bool>> RPCHelpMan::GetArgNames() const | 
| 750 | 0 | { | 
| 751 | 0 |     std::vector<std::pair<std::string, bool>> ret; | 
| 752 | 0 |     ret.reserve(m_args.size()); | 
| 753 | 0 |     for (const auto& arg : m_args) { | 
| 754 | 0 |         if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { | 
| 755 | 0 |             for (const auto& inner : arg.m_inner) { | 
| 756 | 0 |                 ret.emplace_back(inner.m_names, /*named_only=*/true); | 
| 757 | 0 |             } | 
| 758 | 0 |         } | 
| 759 | 0 |         ret.emplace_back(arg.m_names, /*named_only=*/false); | 
| 760 | 0 |     } | 
| 761 | 0 |     return ret; | 
| 762 | 0 | } | 
| 763 |  |  | 
| 764 |  | size_t RPCHelpMan::GetParamIndex(std::string_view key) const | 
| 765 | 0 | { | 
| 766 | 0 |     auto it{std::find_if( | 
| 767 | 0 |         m_args.begin(), m_args.end(), [&key](const auto& arg) { return arg.GetName() == key;} | 
| 768 | 0 |     )}; | 
| 769 |  | 
 | 
| 770 | 0 |     CHECK_NONFATAL(it != m_args.end());  // TODO: ideally this is checked at compile time | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 771 | 0 |     return std::distance(m_args.begin(), it); | 
| 772 | 0 | } | 
| 773 |  |  | 
| 774 |  | std::string RPCHelpMan::ToString() const | 
| 775 | 0 | { | 
| 776 | 0 |     std::string ret; | 
| 777 |  |  | 
| 778 |  |     // Oneline summary | 
| 779 | 0 |     ret += m_name; | 
| 780 | 0 |     bool was_optional{false}; | 
| 781 | 0 |     for (const auto& arg : m_args) { | 
| 782 | 0 |         if (arg.m_opts.hidden) break; // Any arg that follows is also hidden | 
| 783 | 0 |         const bool optional = arg.IsOptional(); | 
| 784 | 0 |         ret += " "; | 
| 785 | 0 |         if (optional) { | 
| 786 | 0 |             if (!was_optional) ret += "( "; | 
| 787 | 0 |             was_optional = true; | 
| 788 | 0 |         } else { | 
| 789 | 0 |             if (was_optional) ret += ") "; | 
| 790 | 0 |             was_optional = false; | 
| 791 | 0 |         } | 
| 792 | 0 |         ret += arg.ToString(/*oneline=*/true); | 
| 793 | 0 |     } | 
| 794 | 0 |     if (was_optional) ret += " )"; | 
| 795 |  |  | 
| 796 |  |     // Description | 
| 797 | 0 |     CHECK_NONFATAL(!m_description.starts_with('\n'));  // Historically \n was required, but reject it for new code.| Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 798 | 0 |     ret += "\n\n" + TrimString(m_description) + "\n"; | 
| 799 |  |  | 
| 800 |  |     // Arguments | 
| 801 | 0 |     Sections sections; | 
| 802 | 0 |     Sections named_only_sections; | 
| 803 | 0 |     for (size_t i{0}; i < m_args.size(); ++i) { | 
| 804 | 0 |         const auto& arg = m_args.at(i); | 
| 805 | 0 |         if (arg.m_opts.hidden) break; // Any arg that follows is also hidden | 
| 806 |  |  | 
| 807 |  |         // Push named argument name and description | 
| 808 | 0 |         sections.m_sections.emplace_back(util::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true)); | 
| 809 | 0 |         sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size()); | 
| 810 |  |  | 
| 811 |  |         // Recursively push nested args | 
| 812 | 0 |         sections.Push(arg); | 
| 813 |  |  | 
| 814 |  |         // Push named-only argument sections | 
| 815 | 0 |         if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { | 
| 816 | 0 |             for (const auto& arg_inner : arg.m_inner) { | 
| 817 | 0 |                 named_only_sections.PushSection({arg_inner.GetFirstName(), arg_inner.ToDescriptionString(/*is_named_arg=*/true)}); | 
| 818 | 0 |                 named_only_sections.Push(arg_inner); | 
| 819 | 0 |             } | 
| 820 | 0 |         } | 
| 821 | 0 |     } | 
| 822 |  | 
 | 
| 823 | 0 |     if (!sections.m_sections.empty()) ret += "\nArguments:\n"; | 
| 824 | 0 |     ret += sections.ToString(); | 
| 825 | 0 |     if (!named_only_sections.m_sections.empty()) ret += "\nNamed Arguments:\n"; | 
| 826 | 0 |     ret += named_only_sections.ToString(); | 
| 827 |  |  | 
| 828 |  |     // Result | 
| 829 | 0 |     ret += m_results.ToDescriptionString(); | 
| 830 |  |  | 
| 831 |  |     // Examples | 
| 832 | 0 |     ret += m_examples.ToDescriptionString(); | 
| 833 |  | 
 | 
| 834 | 0 |     return ret; | 
| 835 | 0 | } | 
| 836 |  |  | 
| 837 |  | UniValue RPCHelpMan::GetArgMap() const | 
| 838 | 0 | { | 
| 839 | 0 |     UniValue arr{UniValue::VARR}; | 
| 840 |  | 
 | 
| 841 | 0 |     auto push_back_arg_info = [&arr](const std::string& rpc_name, int pos, const std::string& arg_name, const RPCArg::Type& type) { | 
| 842 | 0 |         UniValue map{UniValue::VARR}; | 
| 843 | 0 |         map.push_back(rpc_name); | 
| 844 | 0 |         map.push_back(pos); | 
| 845 | 0 |         map.push_back(arg_name); | 
| 846 | 0 |         map.push_back(type == RPCArg::Type::STR || | 
| 847 | 0 |                       type == RPCArg::Type::STR_HEX); | 
| 848 | 0 |         arr.push_back(std::move(map)); | 
| 849 | 0 |     }; | 
| 850 |  | 
 | 
| 851 | 0 |     for (int i{0}; i < int(m_args.size()); ++i) { | 
| 852 | 0 |         const auto& arg = m_args.at(i); | 
| 853 | 0 |         std::vector<std::string> arg_names = SplitString(arg.m_names, '|'); | 
| 854 | 0 |         for (const auto& arg_name : arg_names) { | 
| 855 | 0 |             push_back_arg_info(m_name, i, arg_name, arg.m_type); | 
| 856 | 0 |             if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { | 
| 857 | 0 |                 for (const auto& inner : arg.m_inner) { | 
| 858 | 0 |                     std::vector<std::string> inner_names = SplitString(inner.m_names, '|'); | 
| 859 | 0 |                     for (const std::string& inner_name : inner_names) { | 
| 860 | 0 |                         push_back_arg_info(m_name, i, inner_name, inner.m_type); | 
| 861 | 0 |                     } | 
| 862 | 0 |                 } | 
| 863 | 0 |             } | 
| 864 | 0 |         } | 
| 865 | 0 |     } | 
| 866 | 0 |     return arr; | 
| 867 | 0 | } | 
| 868 |  |  | 
| 869 |  | static std::optional<UniValue::VType> ExpectedType(RPCArg::Type type) | 
| 870 | 0 | { | 
| 871 | 0 |     using Type = RPCArg::Type; | 
| 872 | 0 |     switch (type) { | 
| 873 | 0 |     case Type::STR_HEX: | 
| 874 | 0 |     case Type::STR: { | 
| 875 | 0 |         return UniValue::VSTR; | 
| 876 | 0 |     } | 
| 877 | 0 |     case Type::NUM: { | 
| 878 | 0 |         return UniValue::VNUM; | 
| 879 | 0 |     } | 
| 880 | 0 |     case Type::AMOUNT: { | 
| 881 |  |         // VNUM or VSTR, checked inside AmountFromValue() | 
| 882 | 0 |         return std::nullopt; | 
| 883 | 0 |     } | 
| 884 | 0 |     case Type::RANGE: { | 
| 885 |  |         // VNUM or VARR, checked inside ParseRange() | 
| 886 | 0 |         return std::nullopt; | 
| 887 | 0 |     } | 
| 888 | 0 |     case Type::BOOL: { | 
| 889 | 0 |         return UniValue::VBOOL; | 
| 890 | 0 |     } | 
| 891 | 0 |     case Type::OBJ: | 
| 892 | 0 |     case Type::OBJ_NAMED_PARAMS: | 
| 893 | 0 |     case Type::OBJ_USER_KEYS: { | 
| 894 | 0 |         return UniValue::VOBJ; | 
| 895 | 0 |     } | 
| 896 | 0 |     case Type::ARR: { | 
| 897 | 0 |         return UniValue::VARR; | 
| 898 | 0 |     } | 
| 899 | 0 |     } // no default case, so the compiler can warn about missing cases | 
| 900 | 0 |     NONFATAL_UNREACHABLE(); | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 901 | 0 | } | 
| 902 |  |  | 
| 903 |  | UniValue RPCArg::MatchesType(const UniValue& request) const | 
| 904 | 0 | { | 
| 905 | 0 |     if (m_opts.skip_type_check) return true; | 
| 906 | 0 |     if (IsOptional() && request.isNull()) return true; | 
| 907 | 0 |     const auto exp_type{ExpectedType(m_type)}; | 
| 908 | 0 |     if (!exp_type) return true; // nothing to check | 
| 909 |  |  | 
| 910 | 0 |     if (*exp_type != request.getType()) { | 
| 911 | 0 |         return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 912 | 0 |     } | 
| 913 | 0 |     return true; | 
| 914 | 0 | } | 
| 915 |  |  | 
| 916 |  | std::string RPCArg::GetFirstName() const | 
| 917 | 0 | { | 
| 918 | 0 |     return m_names.substr(0, m_names.find('|')); | 
| 919 | 0 | } | 
| 920 |  |  | 
| 921 |  | std::string RPCArg::GetName() const | 
| 922 | 0 | { | 
| 923 | 0 |     CHECK_NONFATAL(std::string::npos == m_names.find('|'));| Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 924 | 0 |     return m_names; | 
| 925 | 0 | } | 
| 926 |  |  | 
| 927 |  | bool RPCArg::IsOptional() const | 
| 928 | 0 | { | 
| 929 | 0 |     if (m_fallback.index() != 0) { | 
| 930 | 0 |         return true; | 
| 931 | 0 |     } else { | 
| 932 | 0 |         return RPCArg::Optional::NO != std::get<RPCArg::Optional>(m_fallback); | 
| 933 | 0 |     } | 
| 934 | 0 | } | 
| 935 |  |  | 
| 936 |  | std::string RPCArg::ToDescriptionString(bool is_named_arg) const | 
| 937 | 0 | { | 
| 938 | 0 |     std::string ret; | 
| 939 | 0 |     ret += "("; | 
| 940 | 0 |     if (m_opts.type_str.size() != 0) { | 
| 941 | 0 |         ret += m_opts.type_str.at(1); | 
| 942 | 0 |     } else { | 
| 943 | 0 |         switch (m_type) { | 
| 944 | 0 |         case Type::STR_HEX: | 
| 945 | 0 |         case Type::STR: { | 
| 946 | 0 |             ret += "string"; | 
| 947 | 0 |             break; | 
| 948 | 0 |         } | 
| 949 | 0 |         case Type::NUM: { | 
| 950 | 0 |             ret += "numeric"; | 
| 951 | 0 |             break; | 
| 952 | 0 |         } | 
| 953 | 0 |         case Type::AMOUNT: { | 
| 954 | 0 |             ret += "numeric or string"; | 
| 955 | 0 |             break; | 
| 956 | 0 |         } | 
| 957 | 0 |         case Type::RANGE: { | 
| 958 | 0 |             ret += "numeric or array"; | 
| 959 | 0 |             break; | 
| 960 | 0 |         } | 
| 961 | 0 |         case Type::BOOL: { | 
| 962 | 0 |             ret += "boolean"; | 
| 963 | 0 |             break; | 
| 964 | 0 |         } | 
| 965 | 0 |         case Type::OBJ: | 
| 966 | 0 |         case Type::OBJ_NAMED_PARAMS: | 
| 967 | 0 |         case Type::OBJ_USER_KEYS: { | 
| 968 | 0 |             ret += "json object"; | 
| 969 | 0 |             break; | 
| 970 | 0 |         } | 
| 971 | 0 |         case Type::ARR: { | 
| 972 | 0 |             ret += "json array"; | 
| 973 | 0 |             break; | 
| 974 | 0 |         } | 
| 975 | 0 |         } // no default case, so the compiler can warn about missing cases | 
| 976 | 0 |     } | 
| 977 | 0 |     if (m_fallback.index() == 1) { | 
| 978 | 0 |         ret += ", optional, default=" + std::get<RPCArg::DefaultHint>(m_fallback); | 
| 979 | 0 |     } else if (m_fallback.index() == 2) { | 
| 980 | 0 |         ret += ", optional, default=" + std::get<RPCArg::Default>(m_fallback).write(); | 
| 981 | 0 |     } else { | 
| 982 | 0 |         switch (std::get<RPCArg::Optional>(m_fallback)) { | 
| 983 | 0 |         case RPCArg::Optional::OMITTED: { | 
| 984 | 0 |             if (is_named_arg) ret += ", optional"; // Default value is "null" in dicts. Otherwise, | 
| 985 |  |             // nothing to do. Element is treated as if not present and has no default value | 
| 986 | 0 |             break; | 
| 987 | 0 |         } | 
| 988 | 0 |         case RPCArg::Optional::NO: { | 
| 989 | 0 |             ret += ", required"; | 
| 990 | 0 |             break; | 
| 991 | 0 |         } | 
| 992 | 0 |         } // no default case, so the compiler can warn about missing cases | 
| 993 | 0 |     } | 
| 994 | 0 |     ret += ")"; | 
| 995 | 0 |     if (m_type == Type::OBJ_NAMED_PARAMS) ret += " Options object that can be used to pass named arguments, listed below."; | 
| 996 | 0 |     ret += m_description.empty() ? "" : " " + m_description; | 
| 997 | 0 |     return ret; | 
| 998 | 0 | } | 
| 999 |  |  | 
| 1000 |  | // NOLINTNEXTLINE(misc-no-recursion) | 
| 1001 |  | void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const int current_indent) const | 
| 1002 | 0 | { | 
| 1003 |  |     // Indentation | 
| 1004 | 0 |     const std::string indent(current_indent, ' '); | 
| 1005 | 0 |     const std::string indent_next(current_indent + 2, ' '); | 
| 1006 |  |  | 
| 1007 |  |     // Elements in a JSON structure (dictionary or array) are separated by a comma | 
| 1008 | 0 |     const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""}; | 
| 1009 |  |  | 
| 1010 |  |     // The key name if recursed into a dictionary | 
| 1011 | 0 |     const std::string maybe_key{ | 
| 1012 | 0 |         outer_type == OuterType::OBJ ? | 
| 1013 | 0 |             "\"" + this->m_key_name + "\" : " : | 
| 1014 | 0 |             ""}; | 
| 1015 |  |  | 
| 1016 |  |     // Format description with type | 
| 1017 | 0 |     const auto Description = [&](const std::string& type) { | 
| 1018 | 0 |         return "(" + type + (this->m_optional ? ", optional" : "") + ")" + | 
| 1019 | 0 |                (this->m_description.empty() ? "" : " " + this->m_description); | 
| 1020 | 0 |     }; | 
| 1021 |  | 
 | 
| 1022 | 0 |     switch (m_type) { | 
| 1023 | 0 |     case Type::ELISION: { | 
| 1024 |  |         // If the inner result is empty, use three dots for elision | 
| 1025 | 0 |         sections.PushSection({indent + "..." + maybe_separator, m_description}); | 
| 1026 | 0 |         return; | 
| 1027 | 0 |     } | 
| 1028 | 0 |     case Type::ANY: { | 
| 1029 | 0 |         NONFATAL_UNREACHABLE(); // Only for testing | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 1030 | 0 |     } | 
| 1031 | 0 |     case Type::NONE: { | 
| 1032 | 0 |         sections.PushSection({indent + "null" + maybe_separator, Description("json null")}); | 
| 1033 | 0 |         return; | 
| 1034 | 0 |     } | 
| 1035 | 0 |     case Type::STR: { | 
| 1036 | 0 |         sections.PushSection({indent + maybe_key + "\"str\"" + maybe_separator, Description("string")}); | 
| 1037 | 0 |         return; | 
| 1038 | 0 |     } | 
| 1039 | 0 |     case Type::STR_AMOUNT: { | 
| 1040 | 0 |         sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")}); | 
| 1041 | 0 |         return; | 
| 1042 | 0 |     } | 
| 1043 | 0 |     case Type::STR_HEX: { | 
| 1044 | 0 |         sections.PushSection({indent + maybe_key + "\"hex\"" + maybe_separator, Description("string")}); | 
| 1045 | 0 |         return; | 
| 1046 | 0 |     } | 
| 1047 | 0 |     case Type::NUM: { | 
| 1048 | 0 |         sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")}); | 
| 1049 | 0 |         return; | 
| 1050 | 0 |     } | 
| 1051 | 0 |     case Type::NUM_TIME: { | 
| 1052 | 0 |         sections.PushSection({indent + maybe_key + "xxx" + maybe_separator, Description("numeric")}); | 
| 1053 | 0 |         return; | 
| 1054 | 0 |     } | 
| 1055 | 0 |     case Type::BOOL: { | 
| 1056 | 0 |         sections.PushSection({indent + maybe_key + "true|false" + maybe_separator, Description("boolean")}); | 
| 1057 | 0 |         return; | 
| 1058 | 0 |     } | 
| 1059 | 0 |     case Type::ARR_FIXED: | 
| 1060 | 0 |     case Type::ARR: { | 
| 1061 | 0 |         sections.PushSection({indent + maybe_key + "[", Description("json array")}); | 
| 1062 | 0 |         for (const auto& i : m_inner) { | 
| 1063 | 0 |             i.ToSections(sections, OuterType::ARR, current_indent + 2); | 
| 1064 | 0 |         } | 
| 1065 | 0 |         CHECK_NONFATAL(!m_inner.empty()); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 1066 | 0 |         if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) { | 
| 1067 | 0 |             sections.PushSection({indent_next + "...", ""}); | 
| 1068 | 0 |         } else { | 
| 1069 |  |             // Remove final comma, which would be invalid JSON | 
| 1070 | 0 |             sections.m_sections.back().m_left.pop_back(); | 
| 1071 | 0 |         } | 
| 1072 | 0 |         sections.PushSection({indent + "]" + maybe_separator, ""}); | 
| 1073 | 0 |         return; | 
| 1074 | 0 |     } | 
| 1075 | 0 |     case Type::OBJ_DYN: | 
| 1076 | 0 |     case Type::OBJ: { | 
| 1077 | 0 |         if (m_inner.empty()) { | 
| 1078 | 0 |             sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")}); | 
| 1079 | 0 |             return; | 
| 1080 | 0 |         } | 
| 1081 | 0 |         sections.PushSection({indent + maybe_key + "{", Description("json object")}); | 
| 1082 | 0 |         for (const auto& i : m_inner) { | 
| 1083 | 0 |             i.ToSections(sections, OuterType::OBJ, current_indent + 2); | 
| 1084 | 0 |         } | 
| 1085 | 0 |         if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) { | 
| 1086 |  |             // If the dictionary keys are dynamic, use three dots for continuation | 
| 1087 | 0 |             sections.PushSection({indent_next + "...", ""}); | 
| 1088 | 0 |         } else { | 
| 1089 |  |             // Remove final comma, which would be invalid JSON | 
| 1090 | 0 |             sections.m_sections.back().m_left.pop_back(); | 
| 1091 | 0 |         } | 
| 1092 | 0 |         sections.PushSection({indent + "}" + maybe_separator, ""}); | 
| 1093 | 0 |         return; | 
| 1094 | 0 |     } | 
| 1095 | 0 |     } // no default case, so the compiler can warn about missing cases | 
| 1096 | 0 |     NONFATAL_UNREACHABLE(); | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 1097 | 0 | } | 
| 1098 |  |  | 
| 1099 |  | static std::optional<UniValue::VType> ExpectedType(RPCResult::Type type) | 
| 1100 | 0 | { | 
| 1101 | 0 |     using Type = RPCResult::Type; | 
| 1102 | 0 |     switch (type) { | 
| 1103 | 0 |     case Type::ELISION: | 
| 1104 | 0 |     case Type::ANY: { | 
| 1105 | 0 |         return std::nullopt; | 
| 1106 | 0 |     } | 
| 1107 | 0 |     case Type::NONE: { | 
| 1108 | 0 |         return UniValue::VNULL; | 
| 1109 | 0 |     } | 
| 1110 | 0 |     case Type::STR: | 
| 1111 | 0 |     case Type::STR_HEX: { | 
| 1112 | 0 |         return UniValue::VSTR; | 
| 1113 | 0 |     } | 
| 1114 | 0 |     case Type::NUM: | 
| 1115 | 0 |     case Type::STR_AMOUNT: | 
| 1116 | 0 |     case Type::NUM_TIME: { | 
| 1117 | 0 |         return UniValue::VNUM; | 
| 1118 | 0 |     } | 
| 1119 | 0 |     case Type::BOOL: { | 
| 1120 | 0 |         return UniValue::VBOOL; | 
| 1121 | 0 |     } | 
| 1122 | 0 |     case Type::ARR_FIXED: | 
| 1123 | 0 |     case Type::ARR: { | 
| 1124 | 0 |         return UniValue::VARR; | 
| 1125 | 0 |     } | 
| 1126 | 0 |     case Type::OBJ_DYN: | 
| 1127 | 0 |     case Type::OBJ: { | 
| 1128 | 0 |         return UniValue::VOBJ; | 
| 1129 | 0 |     } | 
| 1130 | 0 |     } // no default case, so the compiler can warn about missing cases | 
| 1131 | 0 |     NONFATAL_UNREACHABLE(); | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 1132 | 0 | } | 
| 1133 |  |  | 
| 1134 |  | // NOLINTNEXTLINE(misc-no-recursion) | 
| 1135 |  | UniValue RPCResult::MatchesType(const UniValue& result) const | 
| 1136 | 0 | { | 
| 1137 | 0 |     if (m_skip_type_check) { | 
| 1138 | 0 |         return true; | 
| 1139 | 0 |     } | 
| 1140 |  |  | 
| 1141 | 0 |     const auto exp_type = ExpectedType(m_type); | 
| 1142 | 0 |     if (!exp_type) return true; // can be any type, so nothing to check | 
| 1143 |  |  | 
| 1144 | 0 |     if (*exp_type != result.getType()) { | 
| 1145 | 0 |         return strprintf("returned type is %s, but declared as %s in doc", uvTypeName(result.getType()), uvTypeName(*exp_type));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 1146 | 0 |     } | 
| 1147 |  |  | 
| 1148 | 0 |     if (UniValue::VARR == result.getType()) { | 
| 1149 | 0 |         UniValue errors(UniValue::VOBJ); | 
| 1150 | 0 |         for (size_t i{0}; i < result.get_array().size(); ++i) { | 
| 1151 |  |             // If there are more results than documented, reuse the last doc_inner. | 
| 1152 | 0 |             const RPCResult& doc_inner{m_inner.at(std::min(m_inner.size() - 1, i))}; | 
| 1153 | 0 |             UniValue match{doc_inner.MatchesType(result.get_array()[i])}; | 
| 1154 | 0 |             if (!match.isTrue()) errors.pushKV(strprintf("%d", i), std::move(match));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 1155 | 0 |         } | 
| 1156 | 0 |         if (errors.empty()) return true; // empty result array is valid | 
| 1157 | 0 |         return errors; | 
| 1158 | 0 |     } | 
| 1159 |  |  | 
| 1160 | 0 |     if (UniValue::VOBJ == result.getType()) { | 
| 1161 | 0 |         if (!m_inner.empty() && m_inner.at(0).m_type == Type::ELISION) return true; | 
| 1162 | 0 |         UniValue errors(UniValue::VOBJ); | 
| 1163 | 0 |         if (m_type == Type::OBJ_DYN) { | 
| 1164 | 0 |             const RPCResult& doc_inner{m_inner.at(0)}; // Assume all types are the same, randomly pick the first | 
| 1165 | 0 |             for (size_t i{0}; i < result.get_obj().size(); ++i) { | 
| 1166 | 0 |                 UniValue match{doc_inner.MatchesType(result.get_obj()[i])}; | 
| 1167 | 0 |                 if (!match.isTrue()) errors.pushKV(result.getKeys()[i], std::move(match)); | 
| 1168 | 0 |             } | 
| 1169 | 0 |             if (errors.empty()) return true; // empty result obj is valid | 
| 1170 | 0 |             return errors; | 
| 1171 | 0 |         } | 
| 1172 | 0 |         std::set<std::string> doc_keys; | 
| 1173 | 0 |         for (const auto& doc_entry : m_inner) { | 
| 1174 | 0 |             doc_keys.insert(doc_entry.m_key_name); | 
| 1175 | 0 |         } | 
| 1176 | 0 |         std::map<std::string, UniValue> result_obj; | 
| 1177 | 0 |         result.getObjMap(result_obj); | 
| 1178 | 0 |         for (const auto& result_entry : result_obj) { | 
| 1179 | 0 |             if (doc_keys.find(result_entry.first) == doc_keys.end()) { | 
| 1180 | 0 |                 errors.pushKV(result_entry.first, "key returned that was not in doc"); | 
| 1181 | 0 |             } | 
| 1182 | 0 |         } | 
| 1183 |  | 
 | 
| 1184 | 0 |         for (const auto& doc_entry : m_inner) { | 
| 1185 | 0 |             const auto result_it{result_obj.find(doc_entry.m_key_name)}; | 
| 1186 | 0 |             if (result_it == result_obj.end()) { | 
| 1187 | 0 |                 if (!doc_entry.m_optional) { | 
| 1188 | 0 |                     errors.pushKV(doc_entry.m_key_name, "key missing, despite not being optional in doc"); | 
| 1189 | 0 |                 } | 
| 1190 | 0 |                 continue; | 
| 1191 | 0 |             } | 
| 1192 | 0 |             UniValue match{doc_entry.MatchesType(result_it->second)}; | 
| 1193 | 0 |             if (!match.isTrue()) errors.pushKV(doc_entry.m_key_name, std::move(match)); | 
| 1194 | 0 |         } | 
| 1195 | 0 |         if (errors.empty()) return true; | 
| 1196 | 0 |         return errors; | 
| 1197 | 0 |     } | 
| 1198 |  |  | 
| 1199 | 0 |     return true; | 
| 1200 | 0 | } | 
| 1201 |  |  | 
| 1202 |  | void RPCResult::CheckInnerDoc() const | 
| 1203 | 0 | { | 
| 1204 | 0 |     if (m_type == Type::OBJ) { | 
| 1205 |  |         // May or may not be empty | 
| 1206 | 0 |         return; | 
| 1207 | 0 |     } | 
| 1208 |  |     // Everything else must either be empty or not | 
| 1209 | 0 |     const bool inner_needed{m_type == Type::ARR || m_type == Type::ARR_FIXED || m_type == Type::OBJ_DYN}; | 
| 1210 | 0 |     CHECK_NONFATAL(inner_needed != m_inner.empty()); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 1211 | 0 | } | 
| 1212 |  |  | 
| 1213 |  | // NOLINTNEXTLINE(misc-no-recursion) | 
| 1214 |  | std::string RPCArg::ToStringObj(const bool oneline) const | 
| 1215 | 0 | { | 
| 1216 | 0 |     std::string res; | 
| 1217 | 0 |     res += "\""; | 
| 1218 | 0 |     res += GetFirstName(); | 
| 1219 | 0 |     if (oneline) { | 
| 1220 | 0 |         res += "\":"; | 
| 1221 | 0 |     } else { | 
| 1222 | 0 |         res += "\": "; | 
| 1223 | 0 |     } | 
| 1224 | 0 |     switch (m_type) { | 
| 1225 | 0 |     case Type::STR: | 
| 1226 | 0 |         return res + "\"str\""; | 
| 1227 | 0 |     case Type::STR_HEX: | 
| 1228 | 0 |         return res + "\"hex\""; | 
| 1229 | 0 |     case Type::NUM: | 
| 1230 | 0 |         return res + "n"; | 
| 1231 | 0 |     case Type::RANGE: | 
| 1232 | 0 |         return res + "n or [n,n]"; | 
| 1233 | 0 |     case Type::AMOUNT: | 
| 1234 | 0 |         return res + "amount"; | 
| 1235 | 0 |     case Type::BOOL: | 
| 1236 | 0 |         return res + "bool"; | 
| 1237 | 0 |     case Type::ARR: | 
| 1238 | 0 |         res += "["; | 
| 1239 | 0 |         for (const auto& i : m_inner) { | 
| 1240 | 0 |             res += i.ToString(oneline) + ","; | 
| 1241 | 0 |         } | 
| 1242 | 0 |         return res + "...]"; | 
| 1243 | 0 |     case Type::OBJ: | 
| 1244 | 0 |     case Type::OBJ_NAMED_PARAMS: | 
| 1245 | 0 |     case Type::OBJ_USER_KEYS: | 
| 1246 |  |         // Currently unused, so avoid writing dead code | 
| 1247 | 0 |         NONFATAL_UNREACHABLE(); | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 1248 | 0 |     } // no default case, so the compiler can warn about missing cases | 
| 1249 | 0 |     NONFATAL_UNREACHABLE(); | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 1250 | 0 | } | 
| 1251 |  |  | 
| 1252 |  | // NOLINTNEXTLINE(misc-no-recursion) | 
| 1253 |  | std::string RPCArg::ToString(const bool oneline) const | 
| 1254 | 0 | { | 
| 1255 | 0 |     if (oneline && !m_opts.oneline_description.empty()) { | 
| 1256 | 0 |         if (m_opts.oneline_description[0] == '\"' && m_type != Type::STR_HEX && m_type != Type::STR && gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) { | 
| 1257 | 0 |             throw std::runtime_error{ | 
| 1258 | 0 |                 STR_INTERNAL_BUG(strprintf("non-string RPC arg \"%s\" quotes oneline_description:\n%s",| Line | Count | Source |  | 89 | 0 | #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__) | 
 | 
| 1259 | 0 |                     m_names, m_opts.oneline_description) | 
| 1260 | 0 |                 )}; | 
| 1261 | 0 |         } | 
| 1262 | 0 |         return m_opts.oneline_description; | 
| 1263 | 0 |     } | 
| 1264 |  |  | 
| 1265 | 0 |     switch (m_type) { | 
| 1266 | 0 |     case Type::STR_HEX: | 
| 1267 | 0 |     case Type::STR: { | 
| 1268 | 0 |         return "\"" + GetFirstName() + "\""; | 
| 1269 | 0 |     } | 
| 1270 | 0 |     case Type::NUM: | 
| 1271 | 0 |     case Type::RANGE: | 
| 1272 | 0 |     case Type::AMOUNT: | 
| 1273 | 0 |     case Type::BOOL: { | 
| 1274 | 0 |         return GetFirstName(); | 
| 1275 | 0 |     } | 
| 1276 | 0 |     case Type::OBJ: | 
| 1277 | 0 |     case Type::OBJ_NAMED_PARAMS: | 
| 1278 | 0 |     case Type::OBJ_USER_KEYS: { | 
| 1279 |  |         // NOLINTNEXTLINE(misc-no-recursion) | 
| 1280 | 0 |         const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); }); | 
| 1281 | 0 |         if (m_type == Type::OBJ) { | 
| 1282 | 0 |             return "{" + res + "}"; | 
| 1283 | 0 |         } else { | 
| 1284 | 0 |             return "{" + res + ",...}"; | 
| 1285 | 0 |         } | 
| 1286 | 0 |     } | 
| 1287 | 0 |     case Type::ARR: { | 
| 1288 | 0 |         std::string res; | 
| 1289 | 0 |         for (const auto& i : m_inner) { | 
| 1290 | 0 |             res += i.ToString(oneline) + ","; | 
| 1291 | 0 |         } | 
| 1292 | 0 |         return "[" + res + "...]"; | 
| 1293 | 0 |     } | 
| 1294 | 0 |     } // no default case, so the compiler can warn about missing cases | 
| 1295 | 0 |     NONFATAL_UNREACHABLE(); | Line | Count | Source |  | 124 | 0 |     throw NonFatalCheckError(                                         \ |  | 125 | 0 |         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__) | 
 | 
| 1296 | 0 | } | 
| 1297 |  |  | 
| 1298 |  | static std::pair<int64_t, int64_t> ParseRange(const UniValue& value) | 
| 1299 | 0 | { | 
| 1300 | 0 |     if (value.isNum()) { | 
| 1301 | 0 |         return {0, value.getInt<int64_t>()}; | 
| 1302 | 0 |     } | 
| 1303 | 0 |     if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) { | 
| 1304 | 0 |         int64_t low = value[0].getInt<int64_t>(); | 
| 1305 | 0 |         int64_t high = value[1].getInt<int64_t>(); | 
| 1306 | 0 |         if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end"); | 
| 1307 | 0 |         return {low, high}; | 
| 1308 | 0 |     } | 
| 1309 | 0 |     throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]"); | 
| 1310 | 0 | } | 
| 1311 |  |  | 
| 1312 |  | std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value) | 
| 1313 | 0 | { | 
| 1314 | 0 |     int64_t low, high; | 
| 1315 | 0 |     std::tie(low, high) = ParseRange(value); | 
| 1316 | 0 |     if (low < 0) { | 
| 1317 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0"); | 
| 1318 | 0 |     } | 
| 1319 | 0 |     if ((high >> 31) != 0) { | 
| 1320 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high"); | 
| 1321 | 0 |     } | 
| 1322 | 0 |     if (high >= low + 1000000) { | 
| 1323 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large"); | 
| 1324 | 0 |     } | 
| 1325 | 0 |     return {low, high}; | 
| 1326 | 0 | } | 
| 1327 |  |  | 
| 1328 |  | std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider, const bool expand_priv) | 
| 1329 | 0 | { | 
| 1330 | 0 |     std::string desc_str; | 
| 1331 | 0 |     std::pair<int64_t, int64_t> range = {0, 1000}; | 
| 1332 | 0 |     if (scanobject.isStr()) { | 
| 1333 | 0 |         desc_str = scanobject.get_str(); | 
| 1334 | 0 |     } else if (scanobject.isObject()) { | 
| 1335 | 0 |         const UniValue& desc_uni{scanobject.find_value("desc")}; | 
| 1336 | 0 |         if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object"); | 
| 1337 | 0 |         desc_str = desc_uni.get_str(); | 
| 1338 | 0 |         const UniValue& range_uni{scanobject.find_value("range")}; | 
| 1339 | 0 |         if (!range_uni.isNull()) { | 
| 1340 | 0 |             range = ParseDescriptorRange(range_uni); | 
| 1341 | 0 |         } | 
| 1342 | 0 |     } else { | 
| 1343 | 0 |         throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object"); | 
| 1344 | 0 |     } | 
| 1345 |  |  | 
| 1346 | 0 |     std::string error; | 
| 1347 | 0 |     auto descs = Parse(desc_str, provider, error); | 
| 1348 | 0 |     if (descs.empty()) { | 
| 1349 | 0 |         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error); | 
| 1350 | 0 |     } | 
| 1351 | 0 |     if (!descs.at(0)->IsRange()) { | 
| 1352 | 0 |         range.first = 0; | 
| 1353 | 0 |         range.second = 0; | 
| 1354 | 0 |     } | 
| 1355 | 0 |     std::vector<CScript> ret; | 
| 1356 | 0 |     for (int i = range.first; i <= range.second; ++i) { | 
| 1357 | 0 |         for (const auto& desc : descs) { | 
| 1358 | 0 |             std::vector<CScript> scripts; | 
| 1359 | 0 |             if (!desc->Expand(i, provider, scripts, provider)) { | 
| 1360 | 0 |                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));| Line | Count | Source |  | 1172 | 0 | #define strprintf tfm::format | 
 | 
| 1361 | 0 |             } | 
| 1362 | 0 |             if (expand_priv) { | 
| 1363 | 0 |                 desc->ExpandPrivate(/*pos=*/i, provider, /*out=*/provider); | 
| 1364 | 0 |             } | 
| 1365 | 0 |             std::move(scripts.begin(), scripts.end(), std::back_inserter(ret)); | 
| 1366 | 0 |         } | 
| 1367 | 0 |     } | 
| 1368 | 0 |     return ret; | 
| 1369 | 0 | } | 
| 1370 |  |  | 
| 1371 |  | /** Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values. */ | 
| 1372 |  | [[nodiscard]] static UniValue BilingualStringsToUniValue(const std::vector<bilingual_str>& bilingual_strings) | 
| 1373 | 0 | { | 
| 1374 | 0 |     CHECK_NONFATAL(!bilingual_strings.empty()); | Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 1375 | 0 |     UniValue result{UniValue::VARR}; | 
| 1376 | 0 |     for (const auto& s : bilingual_strings) { | 
| 1377 | 0 |         result.push_back(s.original); | 
| 1378 | 0 |     } | 
| 1379 | 0 |     return result; | 
| 1380 | 0 | } | 
| 1381 |  |  | 
| 1382 |  | void PushWarnings(const UniValue& warnings, UniValue& obj) | 
| 1383 | 0 | { | 
| 1384 | 0 |     if (warnings.empty()) return; | 
| 1385 | 0 |     obj.pushKV("warnings", warnings); | 
| 1386 | 0 | } | 
| 1387 |  |  | 
| 1388 |  | void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj) | 
| 1389 | 0 | { | 
| 1390 | 0 |     if (warnings.empty()) return; | 
| 1391 | 0 |     obj.pushKV("warnings", BilingualStringsToUniValue(warnings)); | 
| 1392 | 0 | } | 
| 1393 |  |  | 
| 1394 | 0 | std::vector<RPCResult> ScriptPubKeyDoc() { | 
| 1395 | 0 |     return | 
| 1396 | 0 |          { | 
| 1397 | 0 |              {RPCResult::Type::STR, "asm", "Disassembly of the output script"}, | 
| 1398 | 0 |              {RPCResult::Type::STR, "desc", "Inferred descriptor for the output"}, | 
| 1399 | 0 |              {RPCResult::Type::STR_HEX, "hex", "The raw output script bytes, hex-encoded"}, | 
| 1400 | 0 |              {RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"}, | 
| 1401 | 0 |              {RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"}, | 
| 1402 | 0 |          }; | 
| 1403 | 0 | } | 
| 1404 |  |  | 
| 1405 |  | uint256 GetTarget(const CBlockIndex& blockindex, const uint256 pow_limit) | 
| 1406 | 0 | { | 
| 1407 | 0 |     arith_uint256 target{*CHECK_NONFATAL(DeriveTarget(blockindex.nBits, pow_limit))};| Line | Count | Source |  | 103 | 0 |     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) | 
 | 
| 1408 | 0 |     return ArithToUint256(target); | 
| 1409 | 0 | } |