LCOV - code coverage report
Current view: top level - src/rpc - util.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 72 809 8.9 %
Date: 2023-11-06 23:13:05 Functions: 6 82 7.3 %
Branches: 69 1885 3.7 %

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

Generated by: LCOV version 1.14