LCOV - code coverage report
Current view: top level - src/wallet/rpc - transactions.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 755 777 97.2 %
Date: 2022-04-21 14:51:19 Functions: 21 22 95.5 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 237 272 87.1 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2011-2021 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 <core_io.h>
#       6                 :            : #include <key_io.h>
#       7                 :            : #include <policy/rbf.h>
#       8                 :            : #include <rpc/util.h>
#       9                 :            : #include <util/vector.h>
#      10                 :            : #include <wallet/receive.h>
#      11                 :            : #include <wallet/rpc/util.h>
#      12                 :            : #include <wallet/wallet.h>
#      13                 :            : 
#      14                 :            : using interfaces::FoundBlock;
#      15                 :            : 
#      16                 :            : namespace wallet {
#      17                 :            : static void WalletTxToJSON(const CWallet& wallet, const CWalletTx& wtx, UniValue& entry)
#      18                 :       3846 : {
#      19                 :       3846 :     interfaces::Chain& chain = wallet.chain();
#      20                 :       3846 :     int confirms = wallet.GetTxDepthInMainChain(wtx);
#      21                 :       3846 :     entry.pushKV("confirmations", confirms);
#      22         [ +  + ]:       3846 :     if (wtx.IsCoinBase())
#      23                 :       2393 :         entry.pushKV("generated", true);
#      24         [ +  + ]:       3846 :     if (auto* conf = wtx.state<TxStateConfirmed>())
#      25                 :       2815 :     {
#      26                 :       2815 :         entry.pushKV("blockhash", conf->confirmed_block_hash.GetHex());
#      27                 :       2815 :         entry.pushKV("blockheight", conf->confirmed_block_height);
#      28                 :       2815 :         entry.pushKV("blockindex", conf->position_in_block);
#      29                 :       2815 :         int64_t block_time;
#      30         [ -  + ]:       2815 :         CHECK_NONFATAL(chain.findBlock(conf->confirmed_block_hash, FoundBlock().time(block_time)));
#      31                 :       2815 :         entry.pushKV("blocktime", block_time);
#      32                 :       2815 :     } else {
#      33                 :       1031 :         entry.pushKV("trusted", CachedTxIsTrusted(wallet, wtx));
#      34                 :       1031 :     }
#      35                 :       3846 :     uint256 hash = wtx.GetHash();
#      36                 :       3846 :     entry.pushKV("txid", hash.GetHex());
#      37                 :       3846 :     entry.pushKV("wtxid", wtx.GetWitnessHash().GetHex());
#      38                 :       3846 :     UniValue conflicts(UniValue::VARR);
#      39         [ +  + ]:       3846 :     for (const uint256& conflict : wallet.GetTxConflicts(wtx))
#      40                 :        202 :         conflicts.push_back(conflict.GetHex());
#      41                 :       3846 :     entry.pushKV("walletconflicts", conflicts);
#      42                 :       3846 :     entry.pushKV("time", wtx.GetTxTime());
#      43                 :       3846 :     entry.pushKV("timereceived", int64_t{wtx.nTimeReceived});
#      44                 :            : 
#      45                 :            :     // Add opt-in RBF status
#      46                 :       3846 :     std::string rbfStatus = "no";
#      47         [ +  + ]:       3846 :     if (confirms <= 0) {
#      48                 :       1031 :         RBFTransactionState rbfState = chain.isRBFOptIn(*wtx.tx);
#      49         [ +  + ]:       1031 :         if (rbfState == RBFTransactionState::UNKNOWN)
#      50                 :        469 :             rbfStatus = "unknown";
#      51         [ +  + ]:        562 :         else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125)
#      52                 :        146 :             rbfStatus = "yes";
#      53                 :       1031 :     }
#      54                 :       3846 :     entry.pushKV("bip125-replaceable", rbfStatus);
#      55                 :            : 
#      56         [ +  + ]:       3846 :     for (const std::pair<const std::string, std::string>& item : wtx.mapValue)
#      57                 :         25 :         entry.pushKV(item.first, item.second);
#      58                 :       3846 : }
#      59                 :            : 
#      60                 :            : struct tallyitem
#      61                 :            : {
#      62                 :            :     CAmount nAmount{0};
#      63                 :            :     int nConf{std::numeric_limits<int>::max()};
#      64                 :            :     std::vector<uint256> txids;
#      65                 :            :     bool fIsWatchonly{false};
#      66                 :            :     tallyitem()
#      67                 :        400 :     {
#      68                 :        400 :     }
#      69                 :            : };
#      70                 :            : 
#      71                 :            : static UniValue ListReceived(const CWallet& wallet, const UniValue& params, const bool by_label, const bool include_immature_coinbase) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
#      72                 :        394 : {
#      73                 :            :     // Minimum confirmations
#      74                 :        394 :     int nMinDepth = 1;
#      75         [ +  + ]:        394 :     if (!params[0].isNull())
#      76                 :        360 :         nMinDepth = params[0].get_int();
#      77                 :            : 
#      78                 :            :     // Whether to include empty labels
#      79                 :        394 :     bool fIncludeEmpty = false;
#      80         [ +  + ]:        394 :     if (!params[1].isNull())
#      81                 :         26 :         fIncludeEmpty = params[1].get_bool();
#      82                 :            : 
#      83                 :        394 :     isminefilter filter = ISMINE_SPENDABLE;
#      84                 :            : 
#      85         [ +  + ]:        394 :     if (ParseIncludeWatchonly(params[2], wallet)) {
#      86                 :        346 :         filter |= ISMINE_WATCH_ONLY;
#      87                 :        346 :     }
#      88                 :            : 
#      89                 :        394 :     bool has_filtered_address = false;
#      90                 :        394 :     CTxDestination filtered_address = CNoDestination();
#      91 [ +  + ][ +  + ]:        394 :     if (!by_label && !params[3].isNull() && !params[3].get_str().empty()) {
#                 [ +  + ]
#      92         [ +  + ]:        338 :         if (!IsValidDestinationString(params[3].get_str())) {
#      93                 :          2 :             throw JSONRPCError(RPC_WALLET_ERROR, "address_filter parameter was invalid");
#      94                 :          2 :         }
#      95                 :        336 :         filtered_address = DecodeDestination(params[3].get_str());
#      96                 :        336 :         has_filtered_address = true;
#      97                 :        336 :     }
#      98                 :            : 
#      99                 :            :     // Excluding coinbase outputs is deprecated
#     100                 :            :     // It can be enabled by setting deprecatedrpc=exclude_coinbase
#     101                 :        392 :     const bool include_coinbase{!wallet.chain().rpcEnableDeprecated("exclude_coinbase")};
#     102                 :            : 
#     103 [ +  + ][ +  + ]:        392 :     if (include_immature_coinbase && !include_coinbase) {
#     104                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "include_immature_coinbase is incompatible with deprecated exclude_coinbase");
#     105                 :          2 :     }
#     106                 :            : 
#     107                 :            :     // Tally
#     108                 :        390 :     std::map<CTxDestination, tallyitem> mapTally;
#     109         [ +  + ]:      19774 :     for (const std::pair<const uint256, CWalletTx>& pairWtx : wallet.mapWallet) {
#     110                 :      19774 :         const CWalletTx& wtx = pairWtx.second;
#     111                 :            : 
#     112                 :      19774 :         int nDepth = wallet.GetTxDepthInMainChain(wtx);
#     113         [ +  + ]:      19774 :         if (nDepth < nMinDepth)
#     114                 :         34 :             continue;
#     115                 :            : 
#     116                 :            :         // Coinbase with less than 1 confirmation is no longer in the main chain
#     117 [ +  + ][ +  + ]:      19740 :         if ((wtx.IsCoinBase() && (nDepth < 1 || !include_coinbase))
#                 [ +  + ]
#     118 [ +  + ][ +  + ]:      19740 :             || (wallet.IsTxImmatureCoinBase(wtx) && !include_immature_coinbase))
#     119                 :       3740 :         {
#     120                 :       3740 :             continue;
#     121                 :       3740 :         }
#     122                 :            : 
#     123         [ +  + ]:      16000 :         for (const CTxOut& txout : wtx.tx->vout)
#     124                 :      32000 :         {
#     125                 :      32000 :             CTxDestination address;
#     126         [ +  + ]:      32000 :             if (!ExtractDestination(txout.scriptPubKey, address))
#     127                 :       6348 :                 continue;
#     128                 :            : 
#     129 [ +  + ][ +  + ]:      25652 :             if (has_filtered_address && !(filtered_address == address)) {
#     130                 :      24216 :                 continue;
#     131                 :      24216 :             }
#     132                 :            : 
#     133                 :       1436 :             isminefilter mine = wallet.IsMine(address);
#     134         [ +  + ]:       1436 :             if(!(mine & filter))
#     135                 :        132 :                 continue;
#     136                 :            : 
#     137                 :       1304 :             tallyitem& item = mapTally[address];
#     138                 :       1304 :             item.nAmount += txout.nValue;
#     139                 :       1304 :             item.nConf = std::min(item.nConf, nDepth);
#     140                 :       1304 :             item.txids.push_back(wtx.GetHash());
#     141         [ +  + ]:       1304 :             if (mine & ISMINE_WATCH_ONLY)
#     142                 :        184 :                 item.fIsWatchonly = true;
#     143                 :       1304 :         }
#     144                 :      16000 :     }
#     145                 :            : 
#     146                 :            :     // Reply
#     147                 :        390 :     UniValue ret(UniValue::VARR);
#     148                 :        390 :     std::map<std::string, tallyitem> label_tally;
#     149                 :            : 
#     150                 :            :     // Create m_address_book iterator
#     151                 :            :     // If we aren't filtering, go from begin() to end()
#     152                 :        390 :     auto start = wallet.m_address_book.begin();
#     153                 :        390 :     auto end = wallet.m_address_book.end();
#     154                 :            :     // If we are filtering, find() the applicable entry
#     155         [ +  + ]:        390 :     if (has_filtered_address) {
#     156                 :        336 :         start = wallet.m_address_book.find(filtered_address);
#     157         [ +  + ]:        336 :         if (start != end) {
#     158                 :        334 :             end = std::next(start);
#     159                 :        334 :         }
#     160                 :        336 :     }
#     161                 :            : 
#     162         [ +  + ]:        992 :     for (auto item_it = start; item_it != end; ++item_it)
#     163                 :        602 :     {
#     164         [ -  + ]:        602 :         if (item_it->second.IsChange()) continue;
#     165                 :        602 :         const CTxDestination& address = item_it->first;
#     166                 :        602 :         const std::string& label = item_it->second.GetLabel();
#     167                 :        602 :         auto it = mapTally.find(address);
#     168 [ +  + ][ +  + ]:        602 :         if (it == mapTally.end() && !fIncludeEmpty)
#                 [ +  + ]
#     169                 :        272 :             continue;
#     170                 :            : 
#     171                 :        330 :         CAmount nAmount = 0;
#     172                 :        330 :         int nConf = std::numeric_limits<int>::max();
#     173                 :        330 :         bool fIsWatchonly = false;
#     174         [ +  + ]:        330 :         if (it != mapTally.end())
#     175                 :        298 :         {
#     176                 :        298 :             nAmount = (*it).second.nAmount;
#     177                 :        298 :             nConf = (*it).second.nConf;
#     178                 :        298 :             fIsWatchonly = (*it).second.fIsWatchonly;
#     179                 :        298 :         }
#     180                 :            : 
#     181         [ +  + ]:        330 :         if (by_label)
#     182                 :         54 :         {
#     183                 :         54 :             tallyitem& _item = label_tally[label];
#     184                 :         54 :             _item.nAmount += nAmount;
#     185                 :         54 :             _item.nConf = std::min(_item.nConf, nConf);
#     186                 :         54 :             _item.fIsWatchonly = fIsWatchonly;
#     187                 :         54 :         }
#     188                 :        276 :         else
#     189                 :        276 :         {
#     190                 :        276 :             UniValue obj(UniValue::VOBJ);
#     191         [ +  + ]:        276 :             if(fIsWatchonly)
#     192                 :        146 :                 obj.pushKV("involvesWatchonly", true);
#     193                 :        276 :             obj.pushKV("address",       EncodeDestination(address));
#     194                 :        276 :             obj.pushKV("amount",        ValueFromAmount(nAmount));
#     195         [ +  + ]:        276 :             obj.pushKV("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf));
#     196                 :        276 :             obj.pushKV("label", label);
#     197                 :        276 :             UniValue transactions(UniValue::VARR);
#     198         [ +  + ]:        276 :             if (it != mapTally.end())
#     199                 :        254 :             {
#     200         [ +  + ]:        254 :                 for (const uint256& _item : (*it).second.txids)
#     201                 :        515 :                 {
#     202                 :        515 :                     transactions.push_back(_item.GetHex());
#     203                 :        515 :                 }
#     204                 :        254 :             }
#     205                 :        276 :             obj.pushKV("txids", transactions);
#     206                 :        276 :             ret.push_back(obj);
#     207                 :        276 :         }
#     208                 :        330 :     }
#     209                 :            : 
#     210         [ +  + ]:        390 :     if (by_label)
#     211                 :         24 :     {
#     212         [ +  + ]:         24 :         for (const auto& entry : label_tally)
#     213                 :         28 :         {
#     214                 :         28 :             CAmount nAmount = entry.second.nAmount;
#     215                 :         28 :             int nConf = entry.second.nConf;
#     216                 :         28 :             UniValue obj(UniValue::VOBJ);
#     217         [ +  + ]:         28 :             if (entry.second.fIsWatchonly)
#     218                 :          2 :                 obj.pushKV("involvesWatchonly", true);
#     219                 :         28 :             obj.pushKV("amount",        ValueFromAmount(nAmount));
#     220         [ +  + ]:         28 :             obj.pushKV("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf));
#     221                 :         28 :             obj.pushKV("label",         entry.first);
#     222                 :         28 :             ret.push_back(obj);
#     223                 :         28 :         }
#     224                 :         24 :     }
#     225                 :            : 
#     226                 :        390 :     return ret;
#     227                 :        392 : }
#     228                 :            : 
#     229                 :            : RPCHelpMan listreceivedbyaddress()
#     230                 :       1954 : {
#     231                 :       1954 :     return RPCHelpMan{"listreceivedbyaddress",
#     232                 :       1954 :                 "\nList balances by receiving address.\n",
#     233                 :       1954 :                 {
#     234                 :       1954 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "The minimum number of confirmations before payments are included."},
#     235                 :       1954 :                     {"include_empty", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether to include addresses that haven't received any payments."},
#     236                 :       1954 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Whether to include watch-only addresses (see 'importaddress')"},
#     237                 :       1954 :                     {"address_filter", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "If present and non-empty, only return information on this address."},
#     238                 :       1954 :                     {"include_immature_coinbase", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include immature coinbase transactions."},
#     239                 :       1954 :                 },
#     240                 :       1954 :                 RPCResult{
#     241                 :       1954 :                     RPCResult::Type::ARR, "", "",
#     242                 :       1954 :                     {
#     243                 :       1954 :                         {RPCResult::Type::OBJ, "", "",
#     244                 :       1954 :                         {
#     245                 :       1954 :                             {RPCResult::Type::BOOL, "involvesWatchonly", /*optional=*/true, "Only returns true if imported addresses were involved in transaction"},
#     246                 :       1954 :                             {RPCResult::Type::STR, "address", "The receiving address"},
#     247                 :       1954 :                             {RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " received by the address"},
#     248                 :       1954 :                             {RPCResult::Type::NUM, "confirmations", "The number of confirmations of the most recent transaction included"},
#     249                 :       1954 :                             {RPCResult::Type::STR, "label", "The label of the receiving address. The default label is \"\""},
#     250                 :       1954 :                             {RPCResult::Type::ARR, "txids", "",
#     251                 :       1954 :                             {
#     252                 :       1954 :                                 {RPCResult::Type::STR_HEX, "txid", "The ids of transactions received with the address"},
#     253                 :       1954 :                             }},
#     254                 :       1954 :                         }},
#     255                 :       1954 :                     }
#     256                 :       1954 :                 },
#     257                 :       1954 :                 RPCExamples{
#     258                 :       1954 :                     HelpExampleCli("listreceivedbyaddress", "")
#     259                 :       1954 :             + HelpExampleCli("listreceivedbyaddress", "6 true")
#     260                 :       1954 :             + HelpExampleCli("listreceivedbyaddress", "6 true true \"\" true")
#     261                 :       1954 :             + HelpExampleRpc("listreceivedbyaddress", "6, true, true")
#     262                 :       1954 :             + HelpExampleRpc("listreceivedbyaddress", "6, true, true, \"" + EXAMPLE_ADDRESS[0] + "\", true")
#     263                 :       1954 :                 },
#     264                 :       1954 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     265                 :       1954 : {
#     266                 :        370 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
#     267         [ -  + ]:        370 :     if (!pwallet) return NullUniValue;
#     268                 :            : 
#     269                 :            :     // Make sure the results are valid at least up to the most recent block
#     270                 :            :     // the user could have gotten from another RPC command prior to now
#     271                 :        370 :     pwallet->BlockUntilSyncedToCurrentChain();
#     272                 :            : 
#     273         [ +  + ]:        370 :     const bool include_immature_coinbase{request.params[4].isNull() ? false : request.params[4].get_bool()};
#     274                 :            : 
#     275                 :        370 :     LOCK(pwallet->cs_wallet);
#     276                 :            : 
#     277                 :        370 :     return ListReceived(*pwallet, request.params, false, include_immature_coinbase);
#     278                 :        370 : },
#     279                 :       1954 :     };
#     280                 :       1954 : }
#     281                 :            : 
#     282                 :            : RPCHelpMan listreceivedbylabel()
#     283                 :       1608 : {
#     284                 :       1608 :     return RPCHelpMan{"listreceivedbylabel",
#     285                 :       1608 :                 "\nList received transactions by label.\n",
#     286                 :       1608 :                 {
#     287                 :       1608 :                     {"minconf", RPCArg::Type::NUM, RPCArg::Default{1}, "The minimum number of confirmations before payments are included."},
#     288                 :       1608 :                     {"include_empty", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether to include labels that haven't received any payments."},
#     289                 :       1608 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Whether to include watch-only addresses (see 'importaddress')"},
#     290                 :       1608 :                     {"include_immature_coinbase", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include immature coinbase transactions."},
#     291                 :       1608 :                 },
#     292                 :       1608 :                 RPCResult{
#     293                 :       1608 :                     RPCResult::Type::ARR, "", "",
#     294                 :       1608 :                     {
#     295                 :       1608 :                         {RPCResult::Type::OBJ, "", "",
#     296                 :       1608 :                         {
#     297                 :       1608 :                             {RPCResult::Type::BOOL, "involvesWatchonly", /*optional=*/true, "Only returns true if imported addresses were involved in transaction"},
#     298                 :       1608 :                             {RPCResult::Type::STR_AMOUNT, "amount", "The total amount received by addresses with this label"},
#     299                 :       1608 :                             {RPCResult::Type::NUM, "confirmations", "The number of confirmations of the most recent transaction included"},
#     300                 :       1608 :                             {RPCResult::Type::STR, "label", "The label of the receiving address. The default label is \"\""},
#     301                 :       1608 :                         }},
#     302                 :       1608 :                     }
#     303                 :       1608 :                 },
#     304                 :       1608 :                 RPCExamples{
#     305                 :       1608 :                     HelpExampleCli("listreceivedbylabel", "")
#     306                 :       1608 :             + HelpExampleCli("listreceivedbylabel", "6 true")
#     307                 :       1608 :             + HelpExampleRpc("listreceivedbylabel", "6, true, true, true")
#     308                 :       1608 :                 },
#     309                 :       1608 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     310                 :       1608 : {
#     311                 :         24 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
#     312         [ -  + ]:         24 :     if (!pwallet) return NullUniValue;
#     313                 :            : 
#     314                 :            :     // Make sure the results are valid at least up to the most recent block
#     315                 :            :     // the user could have gotten from another RPC command prior to now
#     316                 :         24 :     pwallet->BlockUntilSyncedToCurrentChain();
#     317                 :            : 
#     318         [ +  + ]:         24 :     const bool include_immature_coinbase{request.params[3].isNull() ? false : request.params[3].get_bool()};
#     319                 :            : 
#     320                 :         24 :     LOCK(pwallet->cs_wallet);
#     321                 :            : 
#     322                 :         24 :     return ListReceived(*pwallet, request.params, true, include_immature_coinbase);
#     323                 :         24 : },
#     324                 :       1608 :     };
#     325                 :       1608 : }
#     326                 :            : 
#     327                 :            : static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
#     328                 :       3971 : {
#     329         [ +  + ]:       3971 :     if (IsValidDestination(dest)) {
#     330                 :       3969 :         entry.pushKV("address", EncodeDestination(dest));
#     331                 :       3969 :     }
#     332                 :       3971 : }
#     333                 :            : 
#     334                 :            : /**
#     335                 :            :  * List transactions based on the given criteria.
#     336                 :            :  *
#     337                 :            :  * @param  wallet         The wallet.
#     338                 :            :  * @param  wtx            The wallet transaction.
#     339                 :            :  * @param  nMinDepth      The minimum confirmation depth.
#     340                 :            :  * @param  fLong          Whether to include the JSON version of the transaction.
#     341                 :            :  * @param  ret            The UniValue into which the result is stored.
#     342                 :            :  * @param  filter_ismine  The "is mine" filter flags.
#     343                 :            :  * @param  filter_label   Optional label string to filter incoming transactions.
#     344                 :            :  */
#     345                 :            : static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter_ismine, const std::string* filter_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
#     346                 :      18505 : {
#     347                 :      18505 :     CAmount nFee;
#     348                 :      18505 :     std::list<COutputEntry> listReceived;
#     349                 :      18505 :     std::list<COutputEntry> listSent;
#     350                 :            : 
#     351                 :      18505 :     CachedTxGetAmounts(wallet, wtx, listReceived, listSent, nFee, filter_ismine);
#     352                 :            : 
#     353                 :      18505 :     bool involvesWatchonly = CachedTxIsFromMe(wallet, wtx, ISMINE_WATCH_ONLY);
#     354                 :            : 
#     355                 :            :     // Sent
#     356         [ +  + ]:      18505 :     if (!filter_label)
#     357                 :       3375 :     {
#     358         [ +  + ]:       3375 :         for (const COutputEntry& s : listSent)
#     359                 :        821 :         {
#     360                 :        821 :             UniValue entry(UniValue::VOBJ);
#     361 [ +  + ][ +  + ]:        821 :             if (involvesWatchonly || (wallet.IsMine(s.destination) & ISMINE_WATCH_ONLY)) {
#     362                 :          9 :                 entry.pushKV("involvesWatchonly", true);
#     363                 :          9 :             }
#     364                 :        821 :             MaybePushAddress(entry, s.destination);
#     365                 :        821 :             entry.pushKV("category", "send");
#     366                 :        821 :             entry.pushKV("amount", ValueFromAmount(-s.amount));
#     367                 :        821 :             const auto* address_book_entry = wallet.FindAddressBookEntry(s.destination);
#     368         [ +  + ]:        821 :             if (address_book_entry) {
#     369                 :        241 :                 entry.pushKV("label", address_book_entry->GetLabel());
#     370                 :        241 :             }
#     371                 :        821 :             entry.pushKV("vout", s.vout);
#     372                 :        821 :             entry.pushKV("fee", ValueFromAmount(-nFee));
#     373         [ +  + ]:        821 :             if (fLong)
#     374                 :        378 :                 WalletTxToJSON(wallet, wtx, entry);
#     375                 :        821 :             entry.pushKV("abandoned", wtx.isAbandoned());
#     376                 :        821 :             ret.push_back(entry);
#     377                 :        821 :         }
#     378                 :       3375 :     }
#     379                 :            : 
#     380                 :            :     // Received
#     381 [ +  + ][ +  + ]:      18505 :     if (listReceived.size() > 0 && wallet.GetTxDepthInMainChain(wtx) >= nMinDepth) {
#     382         [ +  + ]:      17956 :         for (const COutputEntry& r : listReceived)
#     383                 :      18004 :         {
#     384                 :      18004 :             std::string label;
#     385                 :      18004 :             const auto* address_book_entry = wallet.FindAddressBookEntry(r.destination);
#     386         [ +  + ]:      18004 :             if (address_book_entry) {
#     387                 :      16870 :                 label = address_book_entry->GetLabel();
#     388                 :      16870 :             }
#     389 [ +  + ][ +  + ]:      18004 :             if (filter_label && label != *filter_label) {
#     390                 :      14854 :                 continue;
#     391                 :      14854 :             }
#     392                 :       3150 :             UniValue entry(UniValue::VOBJ);
#     393 [ -  + ][ +  + ]:       3150 :             if (involvesWatchonly || (wallet.IsMine(r.destination) & ISMINE_WATCH_ONLY)) {
#     394                 :        212 :                 entry.pushKV("involvesWatchonly", true);
#     395                 :        212 :             }
#     396                 :       3150 :             MaybePushAddress(entry, r.destination);
#     397         [ +  + ]:       3150 :             if (wtx.IsCoinBase())
#     398                 :       2393 :             {
#     399         [ +  + ]:       2393 :                 if (wallet.GetTxDepthInMainChain(wtx) < 1)
#     400                 :        406 :                     entry.pushKV("category", "orphan");
#     401         [ +  + ]:       1987 :                 else if (wallet.IsTxImmatureCoinBase(wtx))
#     402                 :       1356 :                     entry.pushKV("category", "immature");
#     403                 :        631 :                 else
#     404                 :        631 :                     entry.pushKV("category", "generate");
#     405                 :       2393 :             }
#     406                 :        757 :             else
#     407                 :        757 :             {
#     408                 :        757 :                 entry.pushKV("category", "receive");
#     409                 :        757 :             }
#     410                 :       3150 :             entry.pushKV("amount", ValueFromAmount(r.amount));
#     411         [ +  + ]:       3150 :             if (address_book_entry) {
#     412                 :       2016 :                 entry.pushKV("label", label);
#     413                 :       2016 :             }
#     414                 :       3150 :             entry.pushKV("vout", r.vout);
#     415         [ +  + ]:       3150 :             if (fLong)
#     416                 :       2994 :                 WalletTxToJSON(wallet, wtx, entry);
#     417                 :       3150 :             ret.push_back(entry);
#     418                 :       3150 :         }
#     419                 :      17956 :     }
#     420                 :      18505 : }
#     421                 :            : 
#     422                 :            : 
#     423                 :            : static const std::vector<RPCResult> TransactionDescriptionString()
#     424                 :       5737 : {
#     425                 :       5737 :     return{{RPCResult::Type::NUM, "confirmations", "The number of confirmations for the transaction. Negative confirmations means the\n"
#     426                 :       5737 :                "transaction conflicted that many blocks ago."},
#     427                 :       5737 :            {RPCResult::Type::BOOL, "generated", /*optional=*/true, "Only present if the transaction's only input is a coinbase one."},
#     428                 :       5737 :            {RPCResult::Type::BOOL, "trusted", /*optional=*/true, "Whether we consider the transaction to be trusted and safe to spend from.\n"
#     429                 :       5737 :                 "Only present when the transaction has 0 confirmations (or negative confirmations, if conflicted)."},
#     430                 :       5737 :            {RPCResult::Type::STR_HEX, "blockhash", /*optional=*/true, "The block hash containing the transaction."},
#     431                 :       5737 :            {RPCResult::Type::NUM, "blockheight", /*optional=*/true, "The block height containing the transaction."},
#     432                 :       5737 :            {RPCResult::Type::NUM, "blockindex", /*optional=*/true, "The index of the transaction in the block that includes it."},
#     433                 :       5737 :            {RPCResult::Type::NUM_TIME, "blocktime", /*optional=*/true, "The block time expressed in " + UNIX_EPOCH_TIME + "."},
#     434                 :       5737 :            {RPCResult::Type::STR_HEX, "txid", "The transaction id."},
#     435                 :       5737 :            {RPCResult::Type::STR_HEX, "wtxid", "The hash of serialized transaction, including witness data."},
#     436                 :       5737 :            {RPCResult::Type::ARR, "walletconflicts", "Conflicting transaction ids.",
#     437                 :       5737 :            {
#     438                 :       5737 :                {RPCResult::Type::STR_HEX, "txid", "The transaction id."},
#     439                 :       5737 :            }},
#     440                 :       5737 :            {RPCResult::Type::STR_HEX, "replaced_by_txid", /*optional=*/true, "The txid if this tx was replaced."},
#     441                 :       5737 :            {RPCResult::Type::STR_HEX, "replaces_txid", /*optional=*/true, "The txid if the tx replaces one."},
#     442                 :       5737 :            {RPCResult::Type::STR, "comment", /*optional=*/true, ""},
#     443                 :       5737 :            {RPCResult::Type::STR, "to", /*optional=*/true, "If a comment to is associated with the transaction."},
#     444                 :       5737 :            {RPCResult::Type::NUM_TIME, "time", "The transaction time expressed in " + UNIX_EPOCH_TIME + "."},
#     445                 :       5737 :            {RPCResult::Type::NUM_TIME, "timereceived", "The time received expressed in " + UNIX_EPOCH_TIME + "."},
#     446                 :       5737 :            {RPCResult::Type::STR, "comment", /*optional=*/true, "If a comment is associated with the transaction, only present if not empty."},
#     447                 :       5737 :            {RPCResult::Type::STR, "bip125-replaceable", "(\"yes|no|unknown\") Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
#     448                 :       5737 :                "may be unknown for unconfirmed transactions not in the mempool."}};
#     449                 :       5737 : }
#     450                 :            : 
#     451                 :            : RPCHelpMan listtransactions()
#     452                 :       2040 : {
#     453                 :       2040 :     return RPCHelpMan{"listtransactions",
#     454                 :       2040 :                 "\nIf a label name is provided, this will return only incoming transactions paying to addresses with the specified label.\n"
#     455                 :       2040 :                 "\nReturns up to 'count' most recent transactions skipping the first 'from' transactions.\n",
#     456                 :       2040 :                 {
#     457                 :       2040 :                     {"label|dummy", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "If set, should be a valid label name to return only incoming transactions\n"
#     458                 :       2040 :                           "with the specified label, or \"*\" to disable filtering and return all transactions."},
#     459                 :       2040 :                     {"count", RPCArg::Type::NUM, RPCArg::Default{10}, "The number of transactions to return"},
#     460                 :       2040 :                     {"skip", RPCArg::Type::NUM, RPCArg::Default{0}, "The number of transactions to skip"},
#     461                 :       2040 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Include transactions to watch-only addresses (see 'importaddress')"},
#     462                 :       2040 :                 },
#     463                 :       2040 :                 RPCResult{
#     464                 :       2040 :                     RPCResult::Type::ARR, "", "",
#     465                 :       2040 :                     {
#     466                 :       2040 :                         {RPCResult::Type::OBJ, "", "", Cat(Cat<std::vector<RPCResult>>(
#     467                 :       2040 :                         {
#     468                 :       2040 :                             {RPCResult::Type::BOOL, "involvesWatchonly", /*optional=*/true, "Only returns true if imported addresses were involved in transaction."},
#     469                 :       2040 :                             {RPCResult::Type::STR, "address", "The bitcoin address of the transaction."},
#     470                 :       2040 :                             {RPCResult::Type::STR, "category", "The transaction category.\n"
#     471                 :       2040 :                                 "\"send\"                  Transactions sent.\n"
#     472                 :       2040 :                                 "\"receive\"               Non-coinbase transactions received.\n"
#     473                 :       2040 :                                 "\"generate\"              Coinbase transactions received with more than 100 confirmations.\n"
#     474                 :       2040 :                                 "\"immature\"              Coinbase transactions received with 100 or fewer confirmations.\n"
#     475                 :       2040 :                                 "\"orphan\"                Orphaned coinbase transactions received."},
#     476                 :       2040 :                             {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and is positive\n"
#     477                 :       2040 :                                 "for all other categories"},
#     478                 :       2040 :                             {RPCResult::Type::STR, "label", /*optional=*/true, "A comment for the address/transaction, if any"},
#     479                 :       2040 :                             {RPCResult::Type::NUM, "vout", "the vout value"},
#     480                 :       2040 :                             {RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true, "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the\n"
#     481                 :       2040 :                                  "'send' category of transactions."},
#     482                 :       2040 :                         },
#     483                 :       2040 :                         TransactionDescriptionString()),
#     484                 :       2040 :                         {
#     485                 :       2040 :                             {RPCResult::Type::BOOL, "abandoned", /*optional=*/true, "'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
#     486                 :       2040 :                                  "'send' category of transactions."},
#     487                 :       2040 :                         })},
#     488                 :       2040 :                     }
#     489                 :       2040 :                 },
#     490                 :       2040 :                 RPCExamples{
#     491                 :       2040 :             "\nList the most recent 10 transactions in the systems\n"
#     492                 :       2040 :             + HelpExampleCli("listtransactions", "") +
#     493                 :       2040 :             "\nList transactions 100 to 120\n"
#     494                 :       2040 :             + HelpExampleCli("listtransactions", "\"*\" 20 100") +
#     495                 :       2040 :             "\nAs a JSON-RPC call\n"
#     496                 :       2040 :             + HelpExampleRpc("listtransactions", "\"*\", 20, 100")
#     497                 :       2040 :                 },
#     498                 :       2040 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     499                 :       2040 : {
#     500                 :        456 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
#     501         [ -  + ]:        456 :     if (!pwallet) return NullUniValue;
#     502                 :            : 
#     503                 :            :     // Make sure the results are valid at least up to the most recent block
#     504                 :            :     // the user could have gotten from another RPC command prior to now
#     505                 :        456 :     pwallet->BlockUntilSyncedToCurrentChain();
#     506                 :            : 
#     507                 :        456 :     const std::string* filter_label = nullptr;
#     508 [ +  + ][ +  + ]:        456 :     if (!request.params[0].isNull() && request.params[0].get_str() != "*") {
#     509                 :        332 :         filter_label = &request.params[0].get_str();
#     510         [ +  + ]:        332 :         if (filter_label->empty()) {
#     511                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Label argument must be a valid label name or \"*\".");
#     512                 :          2 :         }
#     513                 :        332 :     }
#     514                 :        454 :     int nCount = 10;
#     515         [ +  + ]:        454 :     if (!request.params[1].isNull())
#     516                 :        342 :         nCount = request.params[1].get_int();
#     517                 :        454 :     int nFrom = 0;
#     518         [ +  + ]:        454 :     if (!request.params[2].isNull())
#     519                 :         14 :         nFrom = request.params[2].get_int();
#     520                 :        454 :     isminefilter filter = ISMINE_SPENDABLE;
#     521                 :            : 
#     522         [ +  + ]:        454 :     if (ParseIncludeWatchonly(request.params[3], *pwallet)) {
#     523                 :        353 :         filter |= ISMINE_WATCH_ONLY;
#     524                 :        353 :     }
#     525                 :            : 
#     526         [ +  + ]:        454 :     if (nCount < 0)
#     527                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
#     528         [ +  + ]:        452 :     if (nFrom < 0)
#     529                 :          2 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
#     530                 :            : 
#     531                 :        450 :     UniValue ret(UniValue::VARR);
#     532                 :            : 
#     533                 :        450 :     {
#     534                 :        450 :         LOCK(pwallet->cs_wallet);
#     535                 :            : 
#     536                 :        450 :         const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
#     537                 :            : 
#     538                 :            :         // iterate backwards until we have nCount items to return:
#     539         [ +  + ]:      17012 :         for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
#     540                 :      16640 :         {
#     541                 :      16640 :             CWalletTx *const pwtx = (*it).second;
#     542                 :      16640 :             ListTransactions(*pwallet, *pwtx, 0, true, ret, filter, filter_label);
#     543         [ +  + ]:      16640 :             if ((int)ret.size() >= (nCount+nFrom)) break;
#     544                 :      16640 :         }
#     545                 :        450 :     }
#     546                 :            : 
#     547                 :            :     // ret is newest to oldest
#     548                 :            : 
#     549         [ -  + ]:        450 :     if (nFrom > (int)ret.size())
#     550                 :          0 :         nFrom = ret.size();
#     551         [ +  + ]:        450 :     if ((nFrom + nCount) > (int)ret.size())
#     552                 :        372 :         nCount = ret.size() - nFrom;
#     553                 :            : 
#     554                 :        450 :     const std::vector<UniValue>& txs = ret.getValues();
#     555                 :        450 :     UniValue result{UniValue::VARR};
#     556                 :        450 :     result.push_backV({ txs.rend() - nFrom - nCount, txs.rend() - nFrom }); // Return oldest to newest
#     557                 :        450 :     return result;
#     558                 :        452 : },
#     559                 :       2040 :     };
#     560                 :       2040 : }
#     561                 :            : 
#     562                 :            : RPCHelpMan listsinceblock()
#     563                 :       1636 : {
#     564                 :       1636 :     return RPCHelpMan{"listsinceblock",
#     565                 :       1636 :                 "\nGet all transactions in blocks since block [blockhash], or all transactions if omitted.\n"
#     566                 :       1636 :                 "If \"blockhash\" is no longer a part of the main chain, transactions from the fork point onward are included.\n"
#     567                 :       1636 :                 "Additionally, if include_removed is set, transactions affecting the wallet which were removed are returned in the \"removed\" array.\n",
#     568                 :       1636 :                 {
#     569                 :       1636 :                     {"blockhash", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "If set, the block hash to list transactions since, otherwise list all transactions."},
#     570                 :       1636 :                     {"target_confirmations", RPCArg::Type::NUM, RPCArg::Default{1}, "Return the nth block hash from the main chain. e.g. 1 would mean the best block hash. Note: this is not used as a filter, but only affects [lastblock] in the return value"},
#     571                 :       1636 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Include transactions to watch-only addresses (see 'importaddress')"},
#     572                 :       1636 :                     {"include_removed", RPCArg::Type::BOOL, RPCArg::Default{true}, "Show transactions that were removed due to a reorg in the \"removed\" array\n"
#     573                 :       1636 :                                                                        "(not guaranteed to work on pruned nodes)"},
#     574                 :       1636 :                 },
#     575                 :       1636 :                 RPCResult{
#     576                 :       1636 :                     RPCResult::Type::OBJ, "", "",
#     577                 :       1636 :                     {
#     578                 :       1636 :                         {RPCResult::Type::ARR, "transactions", "",
#     579                 :       1636 :                         {
#     580                 :       1636 :                             {RPCResult::Type::OBJ, "", "", Cat(Cat<std::vector<RPCResult>>(
#     581                 :       1636 :                             {
#     582                 :       1636 :                                 {RPCResult::Type::BOOL, "involvesWatchonly", /*optional=*/true, "Only returns true if imported addresses were involved in transaction."},
#     583                 :       1636 :                                 {RPCResult::Type::STR, "address", "The bitcoin address of the transaction."},
#     584                 :       1636 :                                 {RPCResult::Type::STR, "category", "The transaction category.\n"
#     585                 :       1636 :                                     "\"send\"                  Transactions sent.\n"
#     586                 :       1636 :                                     "\"receive\"               Non-coinbase transactions received.\n"
#     587                 :       1636 :                                     "\"generate\"              Coinbase transactions received with more than 100 confirmations.\n"
#     588                 :       1636 :                                     "\"immature\"              Coinbase transactions received with 100 or fewer confirmations.\n"
#     589                 :       1636 :                                     "\"orphan\"                Orphaned coinbase transactions received."},
#     590                 :       1636 :                                 {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and is positive\n"
#     591                 :       1636 :                                     "for all other categories"},
#     592                 :       1636 :                                 {RPCResult::Type::NUM, "vout", "the vout value"},
#     593                 :       1636 :                                 {RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true, "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the\n"
#     594                 :       1636 :                                      "'send' category of transactions."},
#     595                 :       1636 :                             },
#     596                 :       1636 :                             TransactionDescriptionString()),
#     597                 :       1636 :                             {
#     598                 :       1636 :                                 {RPCResult::Type::BOOL, "abandoned", /*optional=*/true, "'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
#     599                 :       1636 :                                      "'send' category of transactions."},
#     600                 :       1636 :                                 {RPCResult::Type::STR, "label", /*optional=*/true, "A comment for the address/transaction, if any"},
#     601                 :       1636 :                             })},
#     602                 :       1636 :                         }},
#     603                 :       1636 :                         {RPCResult::Type::ARR, "removed", /*optional=*/true, "<structure is the same as \"transactions\" above, only present if include_removed=true>\n"
#     604                 :       1636 :                             "Note: transactions that were re-added in the active chain will appear as-is in this array, and may thus have a positive confirmation count."
#     605                 :       1636 :                         , {{RPCResult::Type::ELISION, "", ""},}},
#     606                 :       1636 :                         {RPCResult::Type::STR_HEX, "lastblock", "The hash of the block (target_confirmations-1) from the best block on the main chain, or the genesis hash if the referenced block does not exist yet. This is typically used to feed back into listsinceblock the next time you call it. So you would generally use a target_confirmations of say 6, so you will be continually re-notified of transactions until they've reached 6 confirmations plus any new ones"},
#     607                 :       1636 :                     }
#     608                 :       1636 :                 },
#     609                 :       1636 :                 RPCExamples{
#     610                 :       1636 :                     HelpExampleCli("listsinceblock", "")
#     611                 :       1636 :             + HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6")
#     612                 :       1636 :             + HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6")
#     613                 :       1636 :                 },
#     614                 :       1636 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     615                 :       1636 : {
#     616                 :         52 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
#     617         [ -  + ]:         52 :     if (!pwallet) return NullUniValue;
#     618                 :            : 
#     619                 :         52 :     const CWallet& wallet = *pwallet;
#     620                 :            :     // Make sure the results are valid at least up to the most recent block
#     621                 :            :     // the user could have gotten from another RPC command prior to now
#     622                 :         52 :     wallet.BlockUntilSyncedToCurrentChain();
#     623                 :            : 
#     624                 :         52 :     LOCK(wallet.cs_wallet);
#     625                 :            : 
#     626                 :         52 :     std::optional<int> height;    // Height of the specified block or the common ancestor, if the block provided was in a deactivated chain.
#     627                 :         52 :     std::optional<int> altheight; // Height of the specified block, even if it's in a deactivated chain.
#     628                 :         52 :     int target_confirms = 1;
#     629                 :         52 :     isminefilter filter = ISMINE_SPENDABLE;
#     630                 :            : 
#     631                 :         52 :     uint256 blockId;
#     632 [ +  + ][ +  + ]:         52 :     if (!request.params[0].isNull() && !request.params[0].get_str().empty()) {
#     633                 :         30 :         blockId = ParseHashV(request.params[0], "blockhash");
#     634                 :         30 :         height = int{};
#     635                 :         30 :         altheight = int{};
#     636         [ +  + ]:         30 :         if (!wallet.chain().findCommonAncestor(blockId, wallet.GetLastBlockHash(), /* ancestor out */ FoundBlock().height(*height), /* blockId out */ FoundBlock().height(*altheight))) {
#     637                 :          4 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
#     638                 :          4 :         }
#     639                 :         30 :     }
#     640                 :            : 
#     641         [ +  + ]:         48 :     if (!request.params[1].isNull()) {
#     642                 :          6 :         target_confirms = request.params[1].get_int();
#     643                 :            : 
#     644         [ +  + ]:          6 :         if (target_confirms < 1) {
#     645                 :          2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
#     646                 :          2 :         }
#     647                 :          6 :     }
#     648                 :            : 
#     649         [ +  + ]:         46 :     if (ParseIncludeWatchonly(request.params[2], wallet)) {
#     650                 :          2 :         filter |= ISMINE_WATCH_ONLY;
#     651                 :          2 :     }
#     652                 :            : 
#     653 [ +  + ][ -  + ]:         46 :     bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
#     654                 :            : 
#     655         [ +  + ]:         46 :     int depth = height ? wallet.GetLastBlockHeight() + 1 - *height : -1;
#     656                 :            : 
#     657                 :         46 :     UniValue transactions(UniValue::VARR);
#     658                 :            : 
#     659         [ +  + ]:       2027 :     for (const std::pair<const uint256, CWalletTx>& pairWtx : wallet.mapWallet) {
#     660                 :       2027 :         const CWalletTx& tx = pairWtx.second;
#     661                 :            : 
#     662 [ +  + ][ +  + ]:       2027 :         if (depth == -1 || abs(wallet.GetTxDepthInMainChain(tx)) < depth) {
#     663                 :       1387 :             ListTransactions(wallet, tx, 0, true, transactions, filter, nullptr /* filter_label */);
#     664                 :       1387 :         }
#     665                 :       2027 :     }
#     666                 :            : 
#     667                 :            :     // when a reorg'd block is requested, we also list any relevant transactions
#     668                 :            :     // in the blocks of the chain that was detached
#     669                 :         46 :     UniValue removed(UniValue::VARR);
#     670 [ +  + ][ +  + ]:         70 :     while (include_removed && altheight && *altheight > *height) {
#                 [ +  + ]
#     671                 :         24 :         CBlock block;
#     672 [ -  + ][ -  + ]:         24 :         if (!wallet.chain().findBlock(blockId, FoundBlock().data(block)) || block.IsNull()) {
#                 [ -  + ]
#     673                 :          0 :             throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
#     674                 :          0 :         }
#     675         [ +  + ]:         28 :         for (const CTransactionRef& tx : block.vtx) {
#     676                 :         28 :             auto it = wallet.mapWallet.find(tx->GetHash());
#     677         [ +  + ]:         28 :             if (it != wallet.mapWallet.end()) {
#     678                 :            :                 // We want all transactions regardless of confirmation count to appear here,
#     679                 :            :                 // even negative confirmation ones, hence the big negative.
#     680                 :          4 :                 ListTransactions(wallet, it->second, -100000000, true, removed, filter, nullptr /* filter_label */);
#     681                 :          4 :             }
#     682                 :         28 :         }
#     683                 :         24 :         blockId = block.hashPrevBlock;
#     684                 :         24 :         --*altheight;
#     685                 :         24 :     }
#     686                 :            : 
#     687                 :         46 :     uint256 lastblock;
#     688                 :         46 :     target_confirms = std::min(target_confirms, wallet.GetLastBlockHeight() + 1);
#     689         [ -  + ]:         46 :     CHECK_NONFATAL(wallet.chain().findAncestorByHeight(wallet.GetLastBlockHash(), wallet.GetLastBlockHeight() + 1 - target_confirms, FoundBlock().hash(lastblock)));
#     690                 :            : 
#     691                 :         46 :     UniValue ret(UniValue::VOBJ);
#     692                 :         46 :     ret.pushKV("transactions", transactions);
#     693         [ +  + ]:         46 :     if (include_removed) ret.pushKV("removed", removed);
#     694                 :         46 :     ret.pushKV("lastblock", lastblock.GetHex());
#     695                 :            : 
#     696                 :         46 :     return ret;
#     697                 :         46 : },
#     698                 :       1636 :     };
#     699                 :       1636 : }
#     700                 :            : 
#     701                 :            : RPCHelpMan gettransaction()
#     702                 :       2061 : {
#     703                 :       2061 :     return RPCHelpMan{"gettransaction",
#     704                 :       2061 :                 "\nGet detailed information about in-wallet transaction <txid>\n",
#     705                 :       2061 :                 {
#     706                 :       2061 :                     {"txid", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction id"},
#     707                 :       2061 :                     {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"},
#     708                 :       2061 :                             "Whether to include watch-only addresses in balance calculation and details[]"},
#     709                 :       2061 :                     {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false},
#     710                 :       2061 :                             "Whether to include a `decoded` field containing the decoded transaction (equivalent to RPC decoderawtransaction)"},
#     711                 :       2061 :                 },
#     712                 :       2061 :                 RPCResult{
#     713                 :       2061 :                     RPCResult::Type::OBJ, "", "", Cat(Cat<std::vector<RPCResult>>(
#     714                 :       2061 :                     {
#     715                 :       2061 :                         {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT},
#     716                 :       2061 :                         {RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true, "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the\n"
#     717                 :       2061 :                                      "'send' category of transactions."},
#     718                 :       2061 :                     },
#     719                 :       2061 :                     TransactionDescriptionString()),
#     720                 :       2061 :                     {
#     721                 :       2061 :                         {RPCResult::Type::ARR, "details", "",
#     722                 :       2061 :                         {
#     723                 :       2061 :                             {RPCResult::Type::OBJ, "", "",
#     724                 :       2061 :                             {
#     725                 :       2061 :                                 {RPCResult::Type::BOOL, "involvesWatchonly", /*optional=*/true, "Only returns true if imported addresses were involved in transaction."},
#     726                 :       2061 :                                 {RPCResult::Type::STR, "address", /*optional=*/true, "The bitcoin address involved in the transaction."},
#     727                 :       2061 :                                 {RPCResult::Type::STR, "category", "The transaction category.\n"
#     728                 :       2061 :                                     "\"send\"                  Transactions sent.\n"
#     729                 :       2061 :                                     "\"receive\"               Non-coinbase transactions received.\n"
#     730                 :       2061 :                                     "\"generate\"              Coinbase transactions received with more than 100 confirmations.\n"
#     731                 :       2061 :                                     "\"immature\"              Coinbase transactions received with 100 or fewer confirmations.\n"
#     732                 :       2061 :                                     "\"orphan\"                Orphaned coinbase transactions received."},
#     733                 :       2061 :                                 {RPCResult::Type::STR_AMOUNT, "amount", "The amount in " + CURRENCY_UNIT},
#     734                 :       2061 :                                 {RPCResult::Type::STR, "label", /*optional=*/true, "A comment for the address/transaction, if any"},
#     735                 :       2061 :                                 {RPCResult::Type::NUM, "vout", "the vout value"},
#     736                 :       2061 :                                 {RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true, "The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
#     737                 :       2061 :                                     "'send' category of transactions."},
#     738                 :       2061 :                                 {RPCResult::Type::BOOL, "abandoned", /*optional=*/true, "'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
#     739                 :       2061 :                                      "'send' category of transactions."},
#     740                 :       2061 :                             }},
#     741                 :       2061 :                         }},
#     742                 :       2061 :                         {RPCResult::Type::STR_HEX, "hex", "Raw data for transaction"},
#     743                 :       2061 :                         {RPCResult::Type::OBJ, "decoded", /*optional=*/true, "The decoded transaction (only present when `verbose` is passed)",
#     744                 :       2061 :                         {
#     745                 :       2061 :                             {RPCResult::Type::ELISION, "", "Equivalent to the RPC decoderawtransaction method, or the RPC getrawtransaction method when `verbose` is passed."},
#     746                 :       2061 :                         }},
#     747                 :       2061 :                     })
#     748                 :       2061 :                 },
#     749                 :       2061 :                 RPCExamples{
#     750                 :       2061 :                     HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#     751                 :       2061 :             + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
#     752                 :       2061 :             + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" false true")
#     753                 :       2061 :             + HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#     754                 :       2061 :                 },
#     755                 :       2061 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     756                 :       2061 : {
#     757                 :        477 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
#     758         [ -  + ]:        477 :     if (!pwallet) return NullUniValue;
#     759                 :            : 
#     760                 :            :     // Make sure the results are valid at least up to the most recent block
#     761                 :            :     // the user could have gotten from another RPC command prior to now
#     762                 :        477 :     pwallet->BlockUntilSyncedToCurrentChain();
#     763                 :            : 
#     764                 :        477 :     LOCK(pwallet->cs_wallet);
#     765                 :            : 
#     766                 :        477 :     uint256 hash(ParseHashV(request.params[0], "txid"));
#     767                 :            : 
#     768                 :        477 :     isminefilter filter = ISMINE_SPENDABLE;
#     769                 :            : 
#     770         [ +  + ]:        477 :     if (ParseIncludeWatchonly(request.params[1], *pwallet)) {
#     771                 :         75 :         filter |= ISMINE_WATCH_ONLY;
#     772                 :         75 :     }
#     773                 :            : 
#     774         [ +  + ]:        477 :     bool verbose = request.params[2].isNull() ? false : request.params[2].get_bool();
#     775                 :            : 
#     776                 :        477 :     UniValue entry(UniValue::VOBJ);
#     777                 :        477 :     auto it = pwallet->mapWallet.find(hash);
#     778         [ +  + ]:        477 :     if (it == pwallet->mapWallet.end()) {
#     779                 :          3 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
#     780                 :          3 :     }
#     781                 :        474 :     const CWalletTx& wtx = it->second;
#     782                 :            : 
#     783                 :        474 :     CAmount nCredit = CachedTxGetCredit(*pwallet, wtx, filter);
#     784                 :        474 :     CAmount nDebit = CachedTxGetDebit(*pwallet, wtx, filter);
#     785                 :        474 :     CAmount nNet = nCredit - nDebit;
#     786         [ +  + ]:        474 :     CAmount nFee = (CachedTxIsFromMe(*pwallet, wtx, filter) ? wtx.tx->GetValueOut() - nDebit : 0);
#     787                 :            : 
#     788                 :        474 :     entry.pushKV("amount", ValueFromAmount(nNet - nFee));
#     789         [ +  + ]:        474 :     if (CachedTxIsFromMe(*pwallet, wtx, filter))
#     790                 :        424 :         entry.pushKV("fee", ValueFromAmount(nFee));
#     791                 :            : 
#     792                 :        474 :     WalletTxToJSON(*pwallet, wtx, entry);
#     793                 :            : 
#     794                 :        474 :     UniValue details(UniValue::VARR);
#     795                 :        474 :     ListTransactions(*pwallet, wtx, 0, false, details, filter, nullptr /* filter_label */);
#     796                 :        474 :     entry.pushKV("details", details);
#     797                 :            : 
#     798                 :        474 :     std::string strHex = EncodeHexTx(*wtx.tx, pwallet->chain().rpcSerializationFlags());
#     799                 :        474 :     entry.pushKV("hex", strHex);
#     800                 :            : 
#     801         [ +  + ]:        474 :     if (verbose) {
#     802                 :         72 :         UniValue decoded(UniValue::VOBJ);
#     803                 :         72 :         TxToUniv(*wtx.tx, /*block_hash=*/uint256(), /*entry=*/decoded, /*include_hex=*/false);
#     804                 :         72 :         entry.pushKV("decoded", decoded);
#     805                 :         72 :     }
#     806                 :            : 
#     807                 :        474 :     return entry;
#     808                 :        477 : },
#     809                 :       2061 :     };
#     810                 :       2061 : }
#     811                 :            : 
#     812                 :            : RPCHelpMan abandontransaction()
#     813                 :       1597 : {
#     814                 :       1597 :     return RPCHelpMan{"abandontransaction",
#     815                 :       1597 :                 "\nMark in-wallet transaction <txid> as abandoned\n"
#     816                 :       1597 :                 "This will mark this transaction and all its in-wallet descendants as abandoned which will allow\n"
#     817                 :       1597 :                 "for their inputs to be respent.  It can be used to replace \"stuck\" or evicted transactions.\n"
#     818                 :       1597 :                 "It only works on transactions which are not included in a block and are not currently in the mempool.\n"
#     819                 :       1597 :                 "It has no effect on transactions which are already abandoned.\n",
#     820                 :       1597 :                 {
#     821                 :       1597 :                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
#     822                 :       1597 :                 },
#     823                 :       1597 :                 RPCResult{RPCResult::Type::NONE, "", ""},
#     824                 :       1597 :                 RPCExamples{
#     825                 :       1597 :                     HelpExampleCli("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#     826                 :       1597 :             + HelpExampleRpc("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
#     827                 :       1597 :                 },
#     828                 :       1597 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     829                 :       1597 : {
#     830                 :         13 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     831         [ -  + ]:         13 :     if (!pwallet) return NullUniValue;
#     832                 :            : 
#     833                 :            :     // Make sure the results are valid at least up to the most recent block
#     834                 :            :     // the user could have gotten from another RPC command prior to now
#     835                 :         13 :     pwallet->BlockUntilSyncedToCurrentChain();
#     836                 :            : 
#     837                 :         13 :     LOCK(pwallet->cs_wallet);
#     838                 :            : 
#     839                 :         13 :     uint256 hash(ParseHashV(request.params[0], "txid"));
#     840                 :            : 
#     841         [ +  + ]:         13 :     if (!pwallet->mapWallet.count(hash)) {
#     842                 :          2 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
#     843                 :          2 :     }
#     844         [ +  + ]:         11 :     if (!pwallet->AbandonTransaction(hash)) {
#     845                 :          4 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not eligible for abandonment");
#     846                 :          4 :     }
#     847                 :            : 
#     848                 :          7 :     return NullUniValue;
#     849                 :         11 : },
#     850                 :       1597 :     };
#     851                 :       1597 : }
#     852                 :            : 
#     853                 :            : RPCHelpMan rescanblockchain()
#     854                 :       1598 : {
#     855                 :       1598 :     return RPCHelpMan{"rescanblockchain",
#     856                 :       1598 :                 "\nRescan the local blockchain for wallet related transactions.\n"
#     857                 :       1598 :                 "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
#     858                 :       1598 :                 {
#     859                 :       1598 :                     {"start_height", RPCArg::Type::NUM, RPCArg::Default{0}, "block height where the rescan should start"},
#     860                 :       1598 :                     {"stop_height", RPCArg::Type::NUM, RPCArg::Optional::OMITTED_NAMED_ARG, "the last block height that should be scanned. If none is provided it will rescan up to the tip at return time of this call."},
#     861                 :       1598 :                 },
#     862                 :       1598 :                 RPCResult{
#     863                 :       1598 :                     RPCResult::Type::OBJ, "", "",
#     864                 :       1598 :                     {
#     865                 :       1598 :                         {RPCResult::Type::NUM, "start_height", "The block height where the rescan started (the requested height or 0)"},
#     866                 :       1598 :                         {RPCResult::Type::NUM, "stop_height", "The height of the last rescanned block. May be null in rare cases if there was a reorg and the call didn't scan any blocks because they were already scanned in the background."},
#     867                 :       1598 :                     }
#     868                 :       1598 :                 },
#     869                 :       1598 :                 RPCExamples{
#     870                 :       1598 :                     HelpExampleCli("rescanblockchain", "100000 120000")
#     871                 :       1598 :             + HelpExampleRpc("rescanblockchain", "100000, 120000")
#     872                 :       1598 :                 },
#     873                 :       1598 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     874                 :       1598 : {
#     875                 :         14 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     876         [ -  + ]:         14 :     if (!pwallet) return NullUniValue;
#     877                 :         14 :     CWallet& wallet{*pwallet};
#     878                 :            : 
#     879                 :            :     // Make sure the results are valid at least up to the most recent block
#     880                 :            :     // the user could have gotten from another RPC command prior to now
#     881                 :         14 :     wallet.BlockUntilSyncedToCurrentChain();
#     882                 :            : 
#     883                 :         14 :     WalletRescanReserver reserver(*pwallet);
#     884         [ -  + ]:         14 :     if (!reserver.reserve()) {
#     885                 :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
#     886                 :          0 :     }
#     887                 :            : 
#     888                 :         14 :     int start_height = 0;
#     889                 :         14 :     std::optional<int> stop_height;
#     890                 :         14 :     uint256 start_block;
#     891                 :         14 :     {
#     892                 :         14 :         LOCK(pwallet->cs_wallet);
#     893                 :         14 :         int tip_height = pwallet->GetLastBlockHeight();
#     894                 :            : 
#     895         [ +  + ]:         14 :         if (!request.params[0].isNull()) {
#     896                 :          4 :             start_height = request.params[0].get_int();
#     897 [ -  + ][ -  + ]:          4 :             if (start_height < 0 || start_height > tip_height) {
#     898                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid start_height");
#     899                 :          0 :             }
#     900                 :          4 :         }
#     901                 :            : 
#     902         [ +  + ]:         14 :         if (!request.params[1].isNull()) {
#     903                 :          2 :             stop_height = request.params[1].get_int();
#     904 [ -  + ][ -  + ]:          2 :             if (*stop_height < 0 || *stop_height > tip_height) {
#     905                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid stop_height");
#     906         [ -  + ]:          2 :             } else if (*stop_height < start_height) {
#     907                 :          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "stop_height must be greater than start_height");
#     908                 :          0 :             }
#     909                 :          2 :         }
#     910                 :            : 
#     911                 :            :         // We can't rescan beyond non-pruned blocks, stop and throw an error
#     912         [ -  + ]:         14 :         if (!pwallet->chain().hasBlocks(pwallet->GetLastBlockHash(), start_height, stop_height)) {
#     913                 :          0 :             throw JSONRPCError(RPC_MISC_ERROR, "Can't rescan beyond pruned data. Use RPC call getblockchaininfo to determine your pruned height.");
#     914                 :          0 :         }
#     915                 :            : 
#     916         [ -  + ]:         14 :         CHECK_NONFATAL(pwallet->chain().findAncestorByHeight(pwallet->GetLastBlockHash(), start_height, FoundBlock().hash(start_block)));
#     917                 :         14 :     }
#     918                 :            : 
#     919                 :         14 :     CWallet::ScanResult result =
#     920                 :         14 :         pwallet->ScanForWalletTransactions(start_block, start_height, stop_height, reserver, true /* fUpdate */);
#     921         [ -  + ]:         14 :     switch (result.status) {
#     922         [ +  - ]:         14 :     case CWallet::ScanResult::SUCCESS:
#     923                 :         14 :         break;
#     924         [ -  + ]:          0 :     case CWallet::ScanResult::FAILURE:
#     925                 :          0 :         throw JSONRPCError(RPC_MISC_ERROR, "Rescan failed. Potentially corrupted data files.");
#     926         [ -  + ]:          0 :     case CWallet::ScanResult::USER_ABORT:
#     927                 :          0 :         throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted.");
#     928                 :            :         // no default case, so the compiler can warn about missing cases
#     929                 :         14 :     }
#     930                 :         14 :     UniValue response(UniValue::VOBJ);
#     931                 :         14 :     response.pushKV("start_height", start_height);
#     932         [ +  - ]:         14 :     response.pushKV("stop_height", result.last_scanned_height ? *result.last_scanned_height : UniValue());
#     933                 :         14 :     return response;
#     934                 :         14 : },
#     935                 :       1598 :     };
#     936                 :       1598 : }
#     937                 :            : 
#     938                 :            : RPCHelpMan abortrescan()
#     939                 :       1584 : {
#     940                 :       1584 :     return RPCHelpMan{"abortrescan",
#     941                 :       1584 :                 "\nStops current wallet rescan triggered by an RPC call, e.g. by an importprivkey call.\n"
#     942                 :       1584 :                 "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
#     943                 :       1584 :                 {},
#     944                 :       1584 :                 RPCResult{RPCResult::Type::BOOL, "", "Whether the abort was successful"},
#     945                 :       1584 :                 RPCExamples{
#     946                 :       1584 :             "\nImport a private key\n"
#     947                 :       1584 :             + HelpExampleCli("importprivkey", "\"mykey\"") +
#     948                 :       1584 :             "\nAbort the running wallet rescan\n"
#     949                 :       1584 :             + HelpExampleCli("abortrescan", "") +
#     950                 :       1584 :             "\nAs a JSON-RPC call\n"
#     951                 :       1584 :             + HelpExampleRpc("abortrescan", "")
#     952                 :       1584 :                 },
#     953                 :       1584 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
#     954                 :       1584 : {
#     955                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
#     956         [ #  # ]:          0 :     if (!pwallet) return NullUniValue;
#     957                 :            : 
#     958 [ #  # ][ #  # ]:          0 :     if (!pwallet->IsScanning() || pwallet->IsAbortingRescan()) return false;
#     959                 :          0 :     pwallet->AbortRescan();
#     960                 :          0 :     return true;
#     961                 :          0 : },
#     962                 :       1584 :     };
#     963                 :       1584 : }
#     964                 :            : } // namespace wallet

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a