LCOV - code coverage report
Current view: top level - src/rpc - mempool.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 68.6 % 1054 723
Test Date: 2026-11-07 05:48:57 Functions: 88.9 % 45 40
Branches: 38.5 % 3686 1418

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <rpc/mempool.h>
       7                 :             : #include <rpc/register.h> // IWYU pragma: associated
       8                 :             : 
       9                 :             : #include <common/args.h>
      10                 :             : #include <consensus/amount.h>
      11                 :             : #include <consensus/validation.h>
      12                 :             : #include <core_io.h>
      13                 :             : #include <index/txospenderindex.h>
      14                 :             : #include <net.h>
      15                 :             : #include <net_processing.h>
      16                 :             : #include <netaddress.h>
      17                 :             : #include <netbase.h>
      18                 :             : #include <node/mempool_persist.h>
      19                 :             : #include <node/mempool_persist_args.h>
      20                 :             : #include <node/transaction.h>
      21                 :             : #include <node/txorphanage.h>
      22                 :             : #include <node/types.h>
      23                 :             : #include <policy/feerate.h>
      24                 :             : #include <policy/packages.h>
      25                 :             : #include <policy/policy.h>
      26                 :             : #include <policy/rbf.h>
      27                 :             : #include <primitives/transaction.h>
      28                 :             : #include <rpc/protocol.h>
      29                 :             : #include <rpc/request.h>
      30                 :             : #include <rpc/server.h>
      31                 :             : #include <rpc/server_util.h>
      32                 :             : #include <rpc/util.h>
      33                 :             : #include <script/script.h>
      34                 :             : #include <sync.h>
      35                 :             : #include <tinyformat.h>
      36                 :             : #include <txgraph.h>
      37                 :             : #include <txmempool.h>
      38                 :             : #include <uint256.h>
      39                 :             : #include <univalue.h>
      40                 :             : #include <util/check.h>
      41                 :             : #include <util/expected.h>
      42                 :             : #include <util/feefrac.h>
      43                 :             : #include <util/fs.h>
      44                 :             : #include <util/moneystr.h>
      45                 :             : #include <util/string.h>
      46                 :             : #include <util/time.h>
      47                 :             : #include <util/vector.h>
      48                 :             : #include <validation.h>
      49                 :             : 
      50                 :             : #include <algorithm>
      51                 :             : #include <cstddef>
      52                 :             : #include <cstdint>
      53                 :             : #include <functional>
      54                 :             : #include <list>
      55                 :             : #include <map>
      56                 :             : #include <memory>
      57                 :             : #include <optional>
      58                 :             : #include <ranges>
      59                 :             : #include <set>
      60                 :             : #include <string>
      61                 :             : #include <string_view>
      62                 :             : #include <tuple>
      63                 :             : #include <utility>
      64                 :             : #include <vector>
      65                 :             : 
      66                 :             : namespace node {
      67                 :             : struct NodeContext;
      68                 :             : } // namespace node
      69                 :             : 
      70                 :             : using node::DumpMempool;
      71                 :             : 
      72                 :             : using node::DEFAULT_MAX_BURN_AMOUNT;
      73                 :             : using node::DEFAULT_MAX_RAW_TX_FEE_RATE;
      74                 :             : using node::MempoolPath;
      75                 :             : using node::NodeContext;
      76                 :             : using node::TransactionError;
      77                 :             : using util::ToString;
      78                 :             : 
      79                 :         262 : static RPCMethod sendrawtransaction()
      80                 :             : {
      81                 :         262 :     return RPCMethod{
      82                 :         262 :         "sendrawtransaction",
      83         [ +  - ]:         524 :         "Submit a raw transaction (serialized, hex-encoded) to the network.\n"
      84                 :             : 
      85                 :             :         "\nIf -privatebroadcast is disabled, then the transaction will be put into the\n"
      86                 :             :         "local mempool of the node and will be sent unconditionally to all currently\n"
      87                 :             :         "connected peers, so using sendrawtransaction for manual rebroadcast will degrade\n"
      88                 :             :         "privacy by leaking the transaction's origin, as nodes will normally not\n"
      89                 :             :         "rebroadcast non-wallet transactions already in their mempool.\n"
      90                 :             : 
      91                 :             :         "\nIf -privatebroadcast is enabled, then the transaction will be sent via\n"
      92                 :             :         "dedicated, short-lived connections to Tor or I2P peers, or to IPv4/IPv6 peers\n"
      93                 :             :         "via the Tor network. This provides best-effort concealment of the transaction's origin.\n"
      94                 :             :         "Private broadcast is experimental and may change in future releases.\n"
      95                 :             :         "Submission does not itself add the transaction to the local mempool; normal\n"
      96                 :             :         "mempool acceptance and relay apply when it is received back from the network.\n"
      97                 :             :         "The private broadcast queue is bounded: when it is full, this RPC fails and\n"
      98                 :             :         "the transaction is not scheduled until an existing one completes or is\n"
      99                 :             :         "aborted. Use getprivatebroadcastinfo to inspect the queue and abortprivatebroadcast to abort.\n"
     100                 :             : 
     101                 :             :         "\nA specific exception, RPC_TRANSACTION_ALREADY_IN_UTXO_SET, may throw if the transaction cannot be added to the mempool.\n"
     102                 :             : 
     103                 :             :         "\nRelated RPCs: createrawtransaction, signrawtransactionwithkey\n",
     104                 :             :         {
     105   [ +  -  +  - ]:         524 :             {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex string of the raw transaction"},
     106   [ +  -  +  -  :         524 :             {"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
             +  -  +  - ]
     107         [ +  - ]:         524 :              "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
     108                 :         262 :                  "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
     109   [ +  -  +  -  :         524 :             {"maxburnamount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_BURN_AMOUNT)},
                   +  - ]
     110         [ +  - ]:         524 :              "Reject transactions with provably unspendable outputs (e.g. 'datacarrier' outputs that use the OP_RETURN opcode) greater than the specified value, expressed in " + CURRENCY_UNIT + ".\n"
     111                 :             :              "If burning funds through unspendable outputs is desired, increase this value.\n"
     112                 :         262 :              "This check is based on heuristics and does not guarantee spendability of outputs.\n"},
     113                 :             :         },
     114         [ +  - ]:         524 :         RPCResult{
     115   [ +  -  +  - ]:         524 :             RPCResult::Type::STR_HEX, "", "The transaction hash in hex"
     116         [ +  - ]:         524 :         },
     117                 :         262 :         RPCExamples{
     118                 :             :             "\nCreate a transaction\n"
     119   [ +  -  +  -  :         524 :             + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
             +  -  +  - ]
     120                 :         262 :             "Sign the transaction, and get back the hex\n"
     121   [ +  -  +  -  :        1048 :             + HelpExampleCli("signrawtransactionwithwallet", "\"myhex\"") +
             +  -  +  - ]
     122                 :         262 :             "\nSend the transaction (signed hex)\n"
     123   [ +  -  +  -  :        1048 :             + HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
             +  -  +  - ]
     124                 :         262 :             "\nAs a JSON-RPC call\n"
     125   [ +  -  +  -  :        1048 :             + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
             +  -  +  - ]
     126         [ +  - ]:         262 :                 },
     127                 :         262 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     128                 :             :         {
     129         [ +  + ]:         119 :             const CAmount max_burn_amount{AmountFromValue(self.Arg<UniValue>("maxburnamount"))};
     130                 :             : 
     131                 :         116 :             CMutableTransaction mtx;
     132   [ +  -  +  -  :         116 :             if (!DecodeHexTx(mtx, request.params[0].get_str())) {
             +  -  +  + ]
     133   [ +  -  +  - ]:          84 :                 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
     134                 :             :             }
     135                 :             : 
     136         [ +  + ]:        2323 :             for (const auto& out : mtx.vout) {
     137   [ +  +  +  -  :        2259 :                 if((out.scriptPubKey.IsUnspendable() || !out.scriptPubKey.HasValidOps()) && out.nValue > max_burn_amount) {
             +  +  +  + ]
     138   [ +  -  +  - ]:          20 :                     throw JSONRPCTransactionError(TransactionError::MAX_BURN_EXCEEDED);
     139                 :             :                 }
     140                 :             :             }
     141                 :             : 
     142         [ +  - ]:          64 :             CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
     143                 :             : 
     144   [ +  -  +  + ]:          64 :             const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>("maxfeerate"))};
     145                 :             : 
     146                 :             : 
     147         [ +  - ]:          61 :             std::string err_string;
     148                 :          61 :             AssertLockNotHeld(cs_main);
     149         [ +  - ]:          61 :             NodeContext& node = EnsureAnyNodeContext(request.context);
     150   [ +  -  +  - ]:          61 :             const bool private_broadcast_enabled{gArgs.GetBoolArg("-privatebroadcast", DEFAULT_PRIVATE_BROADCAST)};
     151         [ #  # ]:           0 :             if (private_broadcast_enabled &&
     152   [ -  +  -  -  :          61 :                 !g_reachable_nets.Contains(NET_ONION) &&
                   -  - ]
     153         [ #  # ]:           0 :                 !g_reachable_nets.Contains(NET_I2P)) {
     154                 :           0 :                 throw JSONRPCError(RPC_MISC_ERROR,
     155         [ #  # ]:           0 :                                    "-privatebroadcast is enabled, but none of the Tor or I2P networks is "
     156                 :             :                                    "reachable. Maybe the location of the Tor proxy couldn't be retrieved "
     157                 :             :                                    "from the Tor daemon at startup. Check whether the Tor daemon is running "
     158         [ #  # ]:           0 :                                    "and that -torcontrol, -torpassword and -i2psam are configured properly.");
     159                 :             :             }
     160         [ +  - ]:          61 :             const auto method = private_broadcast_enabled ? node::TxBroadcast::NO_MEMPOOL_PRIVATE_BROADCAST
     161                 :             :                                                           : node::TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL;
     162   [ +  -  +  - ]:          61 :             const TransactionError err = BroadcastTransaction(node,
     163                 :             :                                                               tx,
     164                 :             :                                                               err_string,
     165         [ +  - ]:          61 :                                                               /*max_tx_fee=*/0,
     166                 :             :                                                               max_raw_tx_fee_rate,
     167                 :             :                                                               method,
     168                 :             :                                                               /*wait_callback=*/true);
     169         [ +  - ]:          61 :             if (TransactionError::OK != err) {
     170         [ +  - ]:          61 :                 throw JSONRPCTransactionError(err, err_string);
     171                 :             :             }
     172                 :             : 
     173   [ #  #  #  # ]:           0 :             return tx->GetHash().GetHex();
     174         [ -  - ]:         125 :         },
     175   [ +  -  +  -  :        1834 :     };
             +  +  -  - ]
     176   [ +  -  +  -  :        1572 : }
             +  -  -  - ]
     177                 :             : 
     178                 :          73 : static RPCMethod getprivatebroadcastinfo()
     179                 :             : {
     180                 :          73 :     return RPCMethod{
     181                 :          73 :         "getprivatebroadcastinfo",
     182         [ +  - ]:         146 :         "Returns information about transactions tracked for private broadcast.\n"
     183                 :             :         "Transactions that have reached the send-attempt limit remain in the result with attempts_remaining=0.\n"
     184                 :             :         "This method is only available when running with -privatebroadcast enabled.\n",
     185                 :             :         {},
     186         [ +  - ]:         146 :         RPCResult{
     187         [ +  - ]:         146 :             RPCResult::Type::OBJ, "", "",
     188                 :             :             {
     189   [ +  -  +  - ]:          73 :                 {RPCResult::Type::ARR, "transactions", "",
     190                 :             :                     {
     191   [ +  -  +  - ]:         146 :                         {RPCResult::Type::OBJ, "", "",
     192                 :             :                             {
     193   [ +  -  +  - ]:         146 :                                 {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
     194   [ +  -  +  - ]:         146 :                                 {RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
     195   [ +  -  +  - ]:         146 :                                 {RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded transaction data"},
     196   [ +  -  +  - ]:         146 :                                 {RPCResult::Type::NUM_TIME, "time_added", "The time this transaction was added to the private broadcast queue (seconds since epoch)"},
     197   [ +  -  +  - ]:         146 :                                 {RPCResult::Type::NUM, "attempts_remaining", "The number of additional private broadcast send attempts allowed for this transaction"},
     198   [ +  -  +  - ]:         146 :                                 {RPCResult::Type::ARR, "peers", "Per-peer send and acknowledgment information for this transaction",
     199                 :             :                                     {
     200   [ +  -  +  - ]:         146 :                                         {RPCResult::Type::OBJ, "", "",
     201                 :             :                                             {
     202   [ +  -  +  - ]:         146 :                                                 {RPCResult::Type::STR, "address", "The address of the peer to which the transaction was sent"},
     203   [ +  -  +  - ]:         146 :                                                 {RPCResult::Type::NUM_TIME, "sent", "The time this transaction was picked for sending to this peer via private broadcast (seconds since epoch)"},
     204   [ +  -  +  - ]:         146 :                                                 {RPCResult::Type::NUM_TIME, "received", /*optional=*/true, "The time this peer acknowledged reception of the transaction (seconds since epoch)"},
     205                 :             :                                             }},
     206                 :             :                                     }},
     207                 :             :                             }},
     208                 :             :                     }},
     209                 :        2190 :             }},
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  +  
          +  +  +  +  +  
          +  +  +  -  -  
          -  -  -  -  -  
                -  -  - ]
     210                 :          73 :         RPCExamples{
     211   [ +  -  +  -  :         146 :             HelpExampleCli("getprivatebroadcastinfo", "")
                   +  - ]
     212   [ +  -  +  -  :         292 :             + HelpExampleRpc("getprivatebroadcastinfo", "")
             +  -  +  - ]
     213         [ +  - ]:          73 :         },
     214                 :          73 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     215                 :             :         {
     216                 :           2 :             const NodeContext& node{EnsureAnyNodeContext(request.context)};
     217                 :           2 :             const PeerManager& peerman{EnsurePeerman(node)};
     218         [ +  - ]:           2 :             if (!peerman.GetInfo().private_broadcast) {
     219   [ +  -  +  - ]:           4 :                 throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Private broadcast is not enabled. Ensure you're running Bitcoin Core with -privatebroadcast=1.");
     220                 :             :             }
     221                 :             : 
     222                 :           0 :             const auto txs{peerman.GetPrivateBroadcastInfo()};
     223                 :             : 
     224                 :           0 :             UniValue transactions(UniValue::VARR);
     225         [ #  # ]:           0 :             for (const auto& tx_info : txs) {
     226                 :           0 :                 UniValue o(UniValue::VOBJ);
     227   [ #  #  #  #  :           0 :                 o.pushKV("txid", tx_info.tx->GetHash().ToString());
             #  #  #  # ]
     228   [ #  #  #  #  :           0 :                 o.pushKV("wtxid", tx_info.tx->GetWitnessHash().ToString());
             #  #  #  # ]
     229   [ #  #  #  #  :           0 :                 o.pushKV("hex", EncodeHexTx(*tx_info.tx));
             #  #  #  # ]
     230   [ #  #  #  #  :           0 :                 o.pushKV("time_added", TicksSinceEpoch<std::chrono::seconds>(tx_info.time_added));
                   #  # ]
     231   [ #  #  #  #  :           0 :                 o.pushKV("attempts_remaining", tx_info.attempts_remaining);
                   #  # ]
     232                 :           0 :                 UniValue peers(UniValue::VARR);
     233         [ #  # ]:           0 :                 for (const auto& peer : tx_info.peers) {
     234                 :           0 :                     UniValue p(UniValue::VOBJ);
     235   [ #  #  #  #  :           0 :                     p.pushKV("address", peer.address.ToStringAddrPort());
             #  #  #  # ]
     236   [ #  #  #  #  :           0 :                     p.pushKV("sent", TicksSinceEpoch<std::chrono::seconds>(peer.sent));
                   #  # ]
     237         [ #  # ]:           0 :                     if (peer.received.has_value()) {
     238   [ #  #  #  #  :           0 :                         p.pushKV("received", TicksSinceEpoch<std::chrono::seconds>(*peer.received));
                   #  # ]
     239                 :             :                     }
     240         [ #  # ]:           0 :                     peers.push_back(std::move(p));
     241                 :           0 :                 }
     242   [ #  #  #  # ]:           0 :                 o.pushKV("peers", std::move(peers));
     243         [ #  # ]:           0 :                 transactions.push_back(std::move(o));
     244                 :           0 :             }
     245                 :             : 
     246                 :           0 :             UniValue ret(UniValue::VOBJ);
     247   [ #  #  #  # ]:           0 :             ret.pushKV("transactions", std::move(transactions));
     248                 :           0 :             return ret;
     249                 :           0 :         },
     250   [ +  -  +  - ]:         292 :     };
     251                 :        1752 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             -  -  -  - ]
     252                 :             : 
     253                 :          75 : static RPCMethod abortprivatebroadcast()
     254                 :             : {
     255                 :          75 :     return RPCMethod{
     256                 :          75 :         "abortprivatebroadcast",
     257         [ +  - ]:         150 :         "Abort private broadcast attempts for a transaction currently being privately broadcast.\n"
     258                 :             :         "The transaction will be removed from the private broadcast queue.\n"
     259                 :             :         "This method is only available when running with -privatebroadcast enabled.\n",
     260                 :             :         {
     261   [ +  -  +  - ]:         150 :             {"id", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A transaction identifier to abort. It will be matched against both txid and wtxid for all transactions in the private broadcast queue.\n"
     262                 :             :                                                                 "If the provided id matches a txid that corresponds to multiple transactions with different wtxids, multiple transactions will be removed and returned."},
     263                 :             :         },
     264         [ +  - ]:         150 :         RPCResult{
     265   [ +  -  +  - ]:         150 :             RPCResult::Type::OBJ, "", "",
     266                 :             :             {
     267   [ +  -  +  - ]:         150 :                 {RPCResult::Type::ARR, "removed_transactions", "Transactions removed from the private broadcast queue",
     268                 :             :                     {
     269   [ +  -  +  - ]:         150 :                         {RPCResult::Type::OBJ, "", "",
     270                 :             :                             {
     271   [ +  -  +  - ]:         150 :                                 {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
     272   [ +  -  +  - ]:         150 :                                 {RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
     273   [ +  -  +  - ]:         150 :                                 {RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded transaction data"},
     274                 :             :                             }},
     275                 :             :                     }},
     276                 :             :             }
     277                 :        1050 :         },
           [ +  -  +  -  
          +  -  +  -  +  
          +  +  +  +  +  
          -  -  -  -  -  
                      - ]
     278                 :          75 :         RPCExamples{
     279   [ +  -  +  -  :         150 :             HelpExampleCli("abortprivatebroadcast", "\"id\"")
                   +  - ]
     280   [ +  -  +  -  :         300 :             + HelpExampleRpc("abortprivatebroadcast", "\"id\"")
             +  -  +  - ]
     281         [ +  - ]:          75 :         },
     282                 :          75 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     283                 :             :         {
     284                 :             : 
     285                 :           2 :             const NodeContext& node{EnsureAnyNodeContext(request.context)};
     286                 :           2 :             PeerManager& peerman{EnsurePeerman(node)};
     287         [ +  - ]:           2 :             if (!peerman.GetInfo().private_broadcast) {
     288   [ +  -  +  - ]:           4 :                 throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Private broadcast is not enabled. Ensure you're running Bitcoin Core with -privatebroadcast=1.");
     289                 :             :             }
     290                 :             : 
     291         [ #  # ]:           0 :             const uint256 id{ParseHashV(self.Arg<UniValue>("id"), "id")};
     292                 :             : 
     293                 :           0 :             const auto removed_txs{peerman.AbortPrivateBroadcast(id)};
     294         [ #  # ]:           0 :             if (removed_txs.empty()) {
     295   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in private broadcast queue. Check getprivatebroadcastinfo.");
     296                 :             :             }
     297                 :             : 
     298                 :           0 :             UniValue removed_transactions(UniValue::VARR);
     299         [ #  # ]:           0 :             for (const auto& tx : removed_txs) {
     300                 :           0 :                 UniValue o(UniValue::VOBJ);
     301   [ #  #  #  #  :           0 :                 o.pushKV("txid", tx->GetHash().ToString());
             #  #  #  # ]
     302   [ #  #  #  #  :           0 :                 o.pushKV("wtxid", tx->GetWitnessHash().ToString());
             #  #  #  # ]
     303   [ #  #  #  #  :           0 :                 o.pushKV("hex", EncodeHexTx(*tx));
             #  #  #  # ]
     304         [ #  # ]:           0 :                 removed_transactions.push_back(std::move(o));
     305                 :           0 :             }
     306                 :           0 :             UniValue ret(UniValue::VOBJ);
     307   [ #  #  #  # ]:           0 :             ret.pushKV("removed_transactions", std::move(removed_transactions));
     308                 :           0 :             return ret;
     309                 :           0 :         },
     310   [ +  -  +  -  :         375 :     };
             +  +  -  - ]
     311                 :         900 : }
           [ +  -  +  -  
          +  -  +  -  +  
             -  +  -  -  
                      - ]
     312                 :             : 
     313                 :         349 : static RPCMethod testmempoolaccept()
     314                 :             : {
     315                 :         349 :     return RPCMethod{
     316                 :         349 :         "testmempoolaccept",
     317                 :             :         "Returns result of mempool acceptance tests indicating if raw transaction(s) (serialized, hex-encoded) would be accepted by mempool.\n"
     318                 :             :         "\nIf multiple transactions are passed in, parents must come before children and package policies apply: the transactions cannot conflict with any mempool transactions or each other.\n"
     319                 :             :         "\nIf one transaction fails, other transactions may not be fully validated (the 'allowed' key will be blank).\n"
     320   [ +  -  +  - ]:         698 :         "\nThe maximum number of transactions allowed is " + ToString(MAX_PACKAGE_COUNT) + ".\n"
     321                 :             :         "\nThis checks if transactions violate the consensus or policy rules.\n"
     322                 :         349 :         "\nSee sendrawtransaction call.\n",
     323                 :             :         {
     324   [ +  -  +  - ]:         698 :             {"rawtxs", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of hex strings of raw transactions.",
     325                 :             :                 {
     326   [ +  -  +  - ]:         698 :                     {"rawtx", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
     327                 :             :                 },
     328                 :             :             },
     329   [ +  -  +  -  :         698 :             {"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
             +  -  +  - ]
     330         [ +  - ]:         698 :              "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
     331                 :         349 :                  "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
     332                 :             :         },
     333         [ +  - ]:         698 :         RPCResult{
     334   [ +  -  +  - ]:         698 :             RPCResult::Type::ARR, "", "The result of the mempool acceptance test for each raw transaction in the input array.\n"
     335                 :             :                                       "Returns results for each transaction in the same order they were passed in.\n"
     336                 :             :                                       "Transactions that cannot be fully validated due to failures in other transactions will not contain an 'allowed' result.\n",
     337                 :             :             {
     338   [ +  -  +  - ]:         698 :                 {RPCResult::Type::OBJ, "", "",
     339                 :             :                 {
     340   [ +  -  +  - ]:         698 :                     {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
     341   [ +  -  +  - ]:         698 :                     {RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
     342   [ +  -  +  - ]:         698 :                     {RPCResult::Type::STR, "package-error", /*optional=*/true, "Package validation error, if any (only possible if rawtxs had more than 1 transaction)."},
     343   [ +  -  +  - ]:         698 :                     {RPCResult::Type::BOOL, "allowed", /*optional=*/true, "Whether this tx would be accepted to the mempool and pass client-specified maxfeerate. "
     344                 :             :                                                        "If not present, the tx was not fully validated due to a failure in another tx in the list."},
     345   [ +  -  +  - ]:         698 :                     {RPCResult::Type::NUM, "vsize_adjusted", /*optional=*/true, "Maximum of sigop-adjusted size (-bytespersigop) and virtual transaction size as defined in BIP 141 (only present when 'allowed' is true)."},
     346   [ +  -  +  - ]:         698 :                     {RPCResult::Type::NUM, "vsize", /*optional=*/true, "(DEPRECATED) Was previously erroneously described as the BIP 141 vsize, but is actually sigops-adjusted vsize.\n"
     347                 :             :                                                                 "Use vsize_bip141 to actually get that behavior or switch to the explicit vsize_adjusted for retained behavior."},
     348   [ +  -  +  - ]:         698 :                     {RPCResult::Type::NUM, "vsize_bip141", /*optional=*/true, "Virtual transaction size as defined in BIP 141.\n"
     349                 :             :                                                                        "This is different from actual serialized size for witness transactions as witness data is discounted (only present when 'allowed' is true)."},
     350   [ +  -  +  - ]:         698 :                     {RPCResult::Type::OBJ, "fees", /*optional=*/true, "Transaction fees (only present if 'allowed' is true)",
     351                 :             :                     {
     352   [ +  -  +  - ]:         698 :                         {RPCResult::Type::STR_AMOUNT, "base", "transaction fee in " + CURRENCY_UNIT},
     353   [ +  -  +  - ]:         698 :                         {RPCResult::Type::STR_AMOUNT, "effective-feerate", /*optional=*/false, "the effective feerate in " + CURRENCY_UNIT + " per KvB. May differ from the base feerate if, for example, there are modified fees from prioritisetransaction or a package feerate was used."},
     354   [ +  -  +  - ]:         698 :                         {RPCResult::Type::ARR, "effective-includes", /*optional=*/false, "transactions whose fees and vsizes are included in effective-feerate.",
     355   [ +  -  +  - ]:         698 :                             {RPCResult{RPCResult::Type::STR_HEX, "", "transaction wtxid in hex"},
     356                 :             :                         }},
     357                 :             :                     }},
     358   [ +  -  +  - ]:         698 :                     {RPCResult::Type::STR, "reject-reason", /*optional=*/true, "Rejection reason (only present when 'allowed' is false)"},
     359   [ +  -  +  - ]:         698 :                     {RPCResult::Type::STR, "reject-details", /*optional=*/true, "Rejection details (only present when 'allowed' is false and rejection details exist)"},
     360                 :             :                 }},
     361                 :             :             }
     362                 :       11866 :         },
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  +  +  +  
          +  +  +  +  -  
          -  -  -  -  -  
                   -  - ]
     363                 :         349 :         RPCExamples{
     364                 :             :             "\nCreate a transaction\n"
     365   [ +  -  +  -  :         698 :             + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
             +  -  +  - ]
     366                 :         349 :             "Sign the transaction, and get back the hex\n"
     367   [ +  -  +  -  :        1396 :             + HelpExampleCli("signrawtransactionwithwallet", "\"myhex\"") +
             +  -  +  - ]
     368                 :         349 :             "\nTest acceptance of the transaction (signed hex)\n"
     369   [ +  -  +  -  :        1396 :             + HelpExampleCli("testmempoolaccept", R"('["signedhex"]')") +
             +  -  +  - ]
     370                 :         349 :             "\nAs a JSON-RPC call\n"
     371   [ +  -  +  -  :        1396 :             + HelpExampleRpc("testmempoolaccept", "[\"signedhex\"]")
             +  -  +  - ]
     372         [ +  - ]:         349 :                 },
     373                 :         349 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     374                 :             :         {
     375                 :         197 :             const UniValue raw_transactions = request.params[0].get_array();
     376   [ -  +  +  +  :         197 :             if (raw_transactions.size() < 1 || raw_transactions.size() > MAX_PACKAGE_COUNT) {
                   +  + ]
     377                 :           8 :                 throw JSONRPCError(RPC_INVALID_PARAMETER,
     378   [ +  -  +  -  :          24 :                                    "Array must contain between 1 and " + ToString(MAX_PACKAGE_COUNT) + " transactions.");
                   +  - ]
     379                 :             :             }
     380                 :             : 
     381   [ +  -  +  + ]:         189 :             const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>("maxfeerate"))};
     382                 :             : 
     383                 :         180 :             std::vector<CTransactionRef> txns;
     384   [ -  +  +  - ]:         180 :             txns.reserve(raw_transactions.size());
     385   [ +  -  +  + ]:         594 :             for (const auto& rawtx : raw_transactions.getValues()) {
     386         [ +  - ]:         500 :                 CMutableTransaction mtx;
     387   [ +  +  +  -  :         500 :                 if (!DecodeHexTx(mtx, rawtx.get_str())) {
                   +  + ]
     388                 :           2 :                     throw JSONRPCError(RPC_DESERIALIZATION_ERROR,
     389   [ +  -  +  -  :           6 :                                        "TX decode failed: " + rawtx.get_str() + " Make sure the tx has at least one input.");
                   +  - ]
     390                 :             :                 }
     391   [ +  -  +  - ]:        1242 :                 txns.emplace_back(MakeTransactionRef(std::move(mtx)));
     392                 :         500 :             }
     393                 :             : 
     394         [ +  - ]:          94 :             NodeContext& node = EnsureAnyNodeContext(request.context);
     395         [ +  - ]:          94 :             CTxMemPool& mempool = EnsureMemPool(node);
     396         [ +  - ]:          94 :             ChainstateManager& chainman = EnsureChainman(node);
     397         [ +  - ]:          94 :             Chainstate& chainstate = chainman.ActiveChainstate();
     398                 :         188 :             const PackageMempoolAcceptResult package_result = [&] {
     399                 :          94 :                 LOCK(::cs_main);
     400   [ -  +  +  +  :          94 :                 if (txns.size() > 1) return ProcessNewPackage(chainstate, mempool, txns, /*test_accept=*/true, /*client_maxfeerate=*/{});
                   +  - ]
     401         [ +  - ]:          39 :                 return PackageMempoolAcceptResult(txns[0]->GetWitnessHash(),
     402   [ +  -  +  - ]:          39 :                                                   chainman.ProcessTransaction(txns[0], /*test_accept=*/true));
     403         [ +  - ]:         188 :             }();
     404                 :             : 
     405                 :          94 :             UniValue rpc_result(UniValue::VARR);
     406                 :             :             // We will check transaction fees while we iterate through txns in order. If any transaction fee
     407                 :             :             // exceeds maxfeerate, we will leave the rest of the validation results blank, because it
     408                 :             :             // doesn't make sense to return a validation result for a transaction if its ancestor(s) would
     409                 :             :             // not be submitted.
     410                 :          94 :             bool exit_early{false};
     411         [ +  + ]:         453 :             for (const auto& tx : txns) {
     412                 :         359 :                 UniValue result_inner(UniValue::VOBJ);
     413   [ +  -  +  -  :         718 :                 result_inner.pushKV("txid", tx->GetHash().GetHex());
             +  -  +  - ]
     414   [ +  -  +  -  :         718 :                 result_inner.pushKV("wtxid", tx->GetWitnessHash().GetHex());
             +  -  +  - ]
     415         [ +  + ]:         359 :                 if (package_result.m_state.GetResult() == PackageValidationResult::PCKG_POLICY) {
     416   [ +  -  +  -  :         370 :                     result_inner.pushKV("package-error", package_result.m_state.ToString());
             +  -  +  - ]
     417                 :             :                 }
     418                 :         359 :                 auto it = package_result.m_tx_results.find(tx->GetWitnessHash());
     419   [ +  -  +  + ]:         359 :                 if (exit_early || it == package_result.m_tx_results.end()) {
     420                 :             :                     // Validation unfinished. Just return the txid and wtxid.
     421         [ +  - ]:         293 :                     rpc_result.push_back(std::move(result_inner));
     422                 :         293 :                     continue;
     423                 :             :                 }
     424         [ +  - ]:          66 :                 const auto& tx_result = it->second;
     425                 :             :                 // Package testmempoolaccept doesn't allow transactions to already be in the mempool.
     426         [ +  - ]:          66 :                 CHECK_NONFATAL(tx_result.m_result_type != MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
     427         [ -  + ]:          66 :                 if (tx_result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
     428         [ #  # ]:           0 :                     const CAmount fee = tx_result.m_base_fees.value();
     429                 :             :                     // Check that fee does not exceed maximum fee
     430         [ #  # ]:           0 :                     const int64_t virtual_size = tx_result.m_vsize.value();
     431         [ #  # ]:           0 :                     const CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
     432         [ #  # ]:           0 :                     if (max_raw_tx_fee && fee > max_raw_tx_fee) {
     433   [ #  #  #  #  :           0 :                         result_inner.pushKV("allowed", false);
                   #  # ]
     434   [ #  #  #  #  :           0 :                         result_inner.pushKV("reject-reason", "max-fee-exceeded");
                   #  # ]
     435                 :           0 :                         exit_early = true;
     436                 :             :                     } else {
     437                 :             :                         // Only return the fee and vsize if the transaction would pass ATMP.
     438                 :             :                         // These can be used to calculate the feerate.
     439   [ #  #  #  #  :           0 :                         result_inner.pushKV("allowed", true);
                   #  # ]
     440   [ #  #  #  #  :           0 :                         result_inner.pushKV("vsize_adjusted", virtual_size);
                   #  # ]
     441   [ #  #  #  #  :           0 :                         result_inner.pushKV("vsize", virtual_size);
                   #  # ]
     442   [ #  #  #  #  :           0 :                         result_inner.pushKV("vsize_bip141", GetVirtualTransactionSize(*tx));
             #  #  #  # ]
     443                 :           0 :                         UniValue fees(UniValue::VOBJ);
     444   [ #  #  #  #  :           0 :                         fees.pushKV("base", ValueFromAmount(fee));
                   #  # ]
     445   [ #  #  #  #  :           0 :                         fees.pushKV("effective-feerate", ValueFromAmount(tx_result.m_effective_feerate.value().GetFeePerK()));
          #  #  #  #  #  
                      # ]
     446                 :           0 :                         UniValue effective_includes_res(UniValue::VARR);
     447   [ #  #  #  # ]:           0 :                         for (const auto& wtxid : tx_result.m_wtxids_fee_calculations.value()) {
     448   [ #  #  #  #  :           0 :                             effective_includes_res.push_back(wtxid.ToString());
                   #  # ]
     449                 :             :                         }
     450   [ #  #  #  # ]:           0 :                         fees.pushKV("effective-includes", std::move(effective_includes_res));
     451   [ #  #  #  # ]:           0 :                         result_inner.pushKV("fees", std::move(fees));
     452                 :           0 :                     }
     453                 :             :                 } else {
     454   [ +  -  +  -  :         132 :                     result_inner.pushKV("allowed", false);
                   +  - ]
     455         [ +  - ]:          66 :                     const TxValidationState state = tx_result.m_state;
     456         [ +  + ]:          66 :                     if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) {
     457   [ +  -  +  -  :          10 :                         result_inner.pushKV("reject-reason", "missing-inputs");
                   +  - ]
     458                 :             :                     } else {
     459   [ -  +  +  -  :         183 :                         result_inner.pushKV("reject-reason", state.GetRejectReason());
             +  -  +  - ]
     460   [ +  -  +  -  :         122 :                         result_inner.pushKV("reject-details", state.ToString());
             +  -  +  - ]
     461                 :             :                     }
     462                 :          66 :                 }
     463         [ +  - ]:          66 :                 rpc_result.push_back(std::move(result_inner));
     464                 :         359 :             }
     465                 :          94 :             return rpc_result;
     466                 :         283 :         },
     467                 :        3141 :     };
           [ +  -  +  -  
          +  -  +  +  +  
             +  -  -  -  
                      - ]
     468                 :       12564 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
                -  -  - ]
     469                 :             : 
     470                 :          89 : static std::vector<RPCResult> ClusterDescription()
     471                 :             : {
     472                 :          89 :     return {
     473   [ +  -  +  - ]:         178 :         RPCResult{RPCResult::Type::NUM, "clusterweight", "total sigops-adjusted weight (as defined in BIP 141 and modified by '-bytespersigop')"},
     474   [ +  -  +  - ]:         178 :         RPCResult{RPCResult::Type::NUM, "txcount", "number of transactions"},
     475   [ +  -  +  - ]:         178 :         RPCResult{RPCResult::Type::ARR, "chunks", "chunks in this cluster (in mining order)",
     476   [ +  -  +  - ]:         178 :             {RPCResult{RPCResult::Type::OBJ, "chunk", "",
     477                 :             :                 {
     478   [ +  -  +  - ]:         178 :                     RPCResult{RPCResult::Type::STR_AMOUNT, "chunkfee", "fees of the transactions in this chunk"},
     479   [ +  -  +  - ]:         178 :                     RPCResult{RPCResult::Type::NUM, "chunkweight", "sigops-adjusted weight of all transactions in this chunk"},
     480   [ +  -  +  - ]:         178 :                     RPCResult{RPCResult::Type::ARR, "txs", "transactions in this chunk in mining order",
     481   [ +  -  +  -  :         445 :                         {RPCResult{RPCResult::Type::STR_HEX, "txid", "transaction id"}}},
          +  -  +  +  -  
                      - ]
     482                 :             :                 }
     483   [ +  -  +  +  :         445 :             }}
                   -  - ]
     484   [ +  -  +  +  :         267 :         }
                   -  - ]
     485   [ +  -  +  +  :         534 :     };
                   -  - ]
     486                 :        1424 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
                      - ]
     487                 :             : 
     488                 :         315 : static std::vector<RPCResult> MempoolEntryDescription()
     489                 :             : {
     490                 :         315 :     std::vector<RPCResult> list = {
     491   [ +  -  +  - ]:         630 :         {RPCResult::Type::NUM, "vsize", "(DEPRECATED) Was previously erroneously described as the BIP 141 vsize, but is actually sigops-adjusted vsize.\n"
     492                 :             :         "Use vsize_bip141 to actually get that behavior or switch to the explicit vsize_adjusted for retained behavior."},
     493   [ +  -  +  - ]:         630 :         {RPCResult::Type::NUM, "vsize_bip141", "Virtual transaction size as defined in BIP 141.\n"
     494                 :             :         "This is different from actual serialized size for witness transactions as witness data is discounted."},
     495   [ +  -  +  - ]:         630 :         {RPCResult::Type::NUM, "vsize_adjusted", "Maximum of sigop-adjusted size (-bytespersigop) and virtual transaction size as defined in BIP 141."},
     496   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM, "weight", "transaction weight as defined in BIP 141."},
     497   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM_TIME, "time", "local time transaction entered pool in seconds since 1 Jan 1970 GMT"},
     498   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM, "height", "block height when transaction entered pool"},
     499   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM, "descendantcount", "number of in-mempool descendant transactions (including this one)"},
     500   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM, "descendantsize", "virtual transaction size of in-mempool descendants (including this one)"},
     501   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM, "ancestorcount", "number of in-mempool ancestor transactions (including this one)"},
     502   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM, "ancestorsize", "virtual transaction size of in-mempool ancestors (including this one)"},
     503   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::NUM, "chunkweight", "sigops-adjusted weight (as defined in BIP 141 and modified by '-bytespersigop') of this transaction's chunk"},
     504   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::STR_HEX, "wtxid", "hash of serialized transaction, including witness data"},
     505   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::OBJ, "fees", "",
     506                 :             :             {
     507   [ +  -  +  - ]:         630 :                 RPCResult{RPCResult::Type::STR_AMOUNT, "base", "transaction fee, denominated in " + CURRENCY_UNIT},
     508   [ +  -  +  - ]:         630 :                 RPCResult{RPCResult::Type::STR_AMOUNT, "modified", "transaction fee with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
     509   [ +  -  +  - ]:         630 :                 RPCResult{RPCResult::Type::STR_AMOUNT, "ancestor", "transaction fees of in-mempool ancestors (including this one) with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
     510   [ +  -  +  - ]:         630 :                 RPCResult{RPCResult::Type::STR_AMOUNT, "descendant", "transaction fees of in-mempool descendants (including this one) with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
     511   [ +  -  +  - ]:         630 :                 RPCResult{RPCResult::Type::STR_AMOUNT, "chunk", "transaction fees of chunk, denominated in " + CURRENCY_UNIT},
     512   [ +  -  +  +  :        2205 :             }},
                   -  - ]
     513   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::ARR, "depends", "unconfirmed transactions used as inputs for this transaction",
     514   [ +  -  +  -  :        1575 :             {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "parent transaction id"}}},
          +  -  +  +  -  
                      - ]
     515   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::ARR, "spentby", "unconfirmed transactions spending outputs from this transaction",
     516   [ +  -  +  -  :        1575 :             {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "child transaction id"}}},
          +  -  +  +  -  
                      - ]
     517   [ +  -  +  - ]:         630 :         RPCResult{RPCResult::Type::BOOL, "unbroadcast", "Whether this transaction is currently unbroadcast (initial broadcast not yet acknowledged by any peers)"},
     518   [ +  -  +  +  :        6615 :     };
                   -  - ]
     519   [ +  -  +  -  :         315 :     if (IsDeprecatedRPCEnabled("bip125")) {
                   -  + ]
     520         [ #  # ]:           0 :         list.emplace_back(RPCResult::Type::BOOL, "bip125-replaceable", "Whether this transaction signals BIP125 replaceability or has an unconfirmed ancestor signaling BIP125 replaceability. (DEPRECATED)\n");
     521                 :             :     }
     522                 :         315 :     return list;
     523                 :       14490 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
                      - ]
     524                 :             : 
     525                 :           0 : void AppendChunkInfo(UniValue& all_chunks, FeePerWeight chunk_feerate, std::vector<const CTxMemPoolEntry *> chunk_txs)
     526                 :             : {
     527                 :           0 :     UniValue chunk(UniValue::VOBJ);
     528   [ #  #  #  #  :           0 :     chunk.pushKV("chunkfee", ValueFromAmount(chunk_feerate.fee));
                   #  # ]
     529   [ #  #  #  #  :           0 :     chunk.pushKV("chunkweight", chunk_feerate.size);
                   #  # ]
     530                 :           0 :     UniValue chunk_txids(UniValue::VARR);
     531         [ #  # ]:           0 :     for (const auto& chunk_tx : chunk_txs) {
     532   [ #  #  #  #  :           0 :         chunk_txids.push_back(chunk_tx->GetTx().GetHash().ToString());
                   #  # ]
     533                 :             :     }
     534   [ #  #  #  # ]:           0 :     chunk.pushKV("txs", std::move(chunk_txids));
     535         [ #  # ]:           0 :     all_chunks.push_back(std::move(chunk));
     536                 :           0 : }
     537                 :             : 
     538                 :           0 : static void clusterToJSON(const CTxMemPool& pool, UniValue& info, std::vector<const CTxMemPoolEntry *> cluster) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
     539                 :             : {
     540                 :           0 :     AssertLockHeld(pool.cs);
     541                 :           0 :     int total_weight{0};
     542         [ #  # ]:           0 :     for (const auto& tx : cluster) {
     543                 :           0 :         total_weight += tx->GetAdjustedWeight();
     544                 :             :     }
     545   [ #  #  #  # ]:           0 :     info.pushKV("clusterweight", total_weight);
     546   [ #  #  #  #  :           0 :     info.pushKV("txcount", cluster.size());
                   #  # ]
     547                 :             : 
     548                 :             :     // Output the cluster by chunk. This isn't handed to us by the mempool, but
     549                 :             :     // we can calculate it by looking at the chunk feerates of each transaction
     550                 :             :     // in the cluster.
     551                 :           0 :     FeePerWeight current_chunk_feerate = pool.GetMainChunkFeerate(*cluster[0]);
     552                 :           0 :     std::vector<const CTxMemPoolEntry *> current_chunk;
     553   [ #  #  #  # ]:           0 :     current_chunk.reserve(cluster.size());
     554                 :             : 
     555                 :           0 :     UniValue all_chunks(UniValue::VARR);
     556         [ #  # ]:           0 :     for (const auto& tx : cluster) {
     557         [ #  # ]:           0 :         if (current_chunk_feerate.size == 0) {
     558                 :             :             // We've iterated all the transactions in the previous chunk; so
     559                 :             :             // append it to the output.
     560   [ #  #  #  # ]:           0 :             AppendChunkInfo(all_chunks, pool.GetMainChunkFeerate(*current_chunk[0]), current_chunk);
     561         [ #  # ]:           0 :             current_chunk.clear();
     562                 :           0 :             current_chunk_feerate = pool.GetMainChunkFeerate(*tx);
     563                 :             :         }
     564         [ #  # ]:           0 :         current_chunk.push_back(tx);
     565         [ #  # ]:           0 :         current_chunk_feerate.size -= tx->GetAdjustedWeight();
     566                 :             :     }
     567   [ #  #  #  # ]:           0 :     AppendChunkInfo(all_chunks, pool.GetMainChunkFeerate(*current_chunk[0]), current_chunk);
     568         [ #  # ]:           0 :     current_chunk.clear();
     569   [ #  #  #  # ]:           0 :     info.pushKV("chunks", std::move(all_chunks));
     570                 :           0 : }
     571                 :             : 
     572                 :           0 : static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
     573                 :             : {
     574                 :           0 :     AssertLockHeld(pool.cs);
     575                 :             : 
     576                 :           0 :     auto [ancestor_count, ancestor_size, ancestor_fees] = pool.CalculateAncestorData(e);
     577                 :           0 :     auto [descendant_count, descendant_size, descendant_fees] = pool.CalculateDescendantData(e);
     578                 :             : 
     579   [ #  #  #  # ]:           0 :     info.pushKV("vsize_adjusted", e.GetTxSize());
     580   [ #  #  #  # ]:           0 :     info.pushKV("vsize", e.GetTxSize());
     581   [ #  #  #  # ]:           0 :     info.pushKV("vsize_bip141", GetVirtualTransactionSize(e.GetTx()));
     582   [ #  #  #  # ]:           0 :     info.pushKV("weight", e.GetTxWeight());
     583   [ #  #  #  # ]:           0 :     info.pushKV("time", count_seconds(e.GetTime()));
     584   [ #  #  #  # ]:           0 :     info.pushKV("height", e.GetHeight());
     585   [ #  #  #  # ]:           0 :     info.pushKV("descendantcount", descendant_count);
     586   [ #  #  #  # ]:           0 :     info.pushKV("descendantsize", descendant_size);
     587   [ #  #  #  # ]:           0 :     info.pushKV("ancestorcount", ancestor_count);
     588   [ #  #  #  # ]:           0 :     info.pushKV("ancestorsize", ancestor_size);
     589   [ #  #  #  #  :           0 :     info.pushKV("wtxid", e.GetTx().GetWitnessHash().ToString());
                   #  # ]
     590                 :           0 :     auto feerate = pool.GetMainChunkFeerate(e);
     591   [ #  #  #  # ]:           0 :     info.pushKV("chunkweight", feerate.size);
     592                 :             : 
     593                 :           0 :     UniValue fees(UniValue::VOBJ);
     594   [ #  #  #  #  :           0 :     fees.pushKV("base", ValueFromAmount(e.GetFee()));
                   #  # ]
     595   [ #  #  #  #  :           0 :     fees.pushKV("modified", ValueFromAmount(e.GetModifiedFee()));
                   #  # ]
     596   [ #  #  #  #  :           0 :     fees.pushKV("ancestor", ValueFromAmount(ancestor_fees));
                   #  # ]
     597   [ #  #  #  #  :           0 :     fees.pushKV("descendant", ValueFromAmount(descendant_fees));
                   #  # ]
     598   [ #  #  #  #  :           0 :     fees.pushKV("chunk", ValueFromAmount(feerate.fee));
                   #  # ]
     599   [ #  #  #  # ]:           0 :     info.pushKV("fees", std::move(fees));
     600                 :             : 
     601                 :           0 :     const CTransaction& tx = e.GetTx();
     602                 :           0 :     std::set<std::string> setDepends;
     603         [ #  # ]:           0 :     for (const CTxIn& txin : tx.vin)
     604                 :             :     {
     605   [ #  #  #  # ]:           0 :         if (pool.exists(txin.prevout.hash))
     606   [ #  #  #  # ]:           0 :             setDepends.insert(txin.prevout.hash.ToString());
     607                 :             :     }
     608                 :             : 
     609                 :           0 :     UniValue depends(UniValue::VARR);
     610         [ #  # ]:           0 :     for (const std::string& dep : setDepends)
     611                 :             :     {
     612   [ #  #  #  # ]:           0 :         depends.push_back(dep);
     613                 :             :     }
     614                 :             : 
     615   [ #  #  #  # ]:           0 :     info.pushKV("depends", std::move(depends));
     616                 :             : 
     617                 :           0 :     UniValue spent(UniValue::VARR);
     618   [ #  #  #  #  :           0 :     for (const CTxMemPoolEntry& child : pool.GetChildren(e)) {
                   #  # ]
     619   [ #  #  #  #  :           0 :         spent.push_back(child.GetTx().GetHash().ToString());
                   #  # ]
     620                 :           0 :     }
     621                 :             : 
     622   [ #  #  #  # ]:           0 :     info.pushKV("spentby", std::move(spent));
     623   [ #  #  #  #  :           0 :     info.pushKV("unbroadcast", pool.IsUnbroadcastTx(tx.GetHash()));
                   #  # ]
     624                 :             : 
     625                 :             :     // Add opt-in RBF status
     626   [ #  #  #  #  :           0 :     if (IsDeprecatedRPCEnabled("bip125")) {
                   #  # ]
     627                 :           0 :         bool rbfStatus = false;
     628         [ #  # ]:           0 :         RBFTransactionState rbfState = IsRBFOptIn(tx, pool);
     629         [ #  # ]:           0 :         if (rbfState == RBFTransactionState::UNKNOWN) {
     630   [ #  #  #  # ]:           0 :             throw JSONRPCError(RPC_MISC_ERROR, "Transaction is not in mempool");
     631         [ #  # ]:           0 :         } else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125) {
     632                 :           0 :             rbfStatus = true;
     633                 :             :         }
     634   [ #  #  #  #  :           0 :         info.pushKV("bip125-replaceable", rbfStatus);
                   #  # ]
     635                 :             :     }
     636                 :           0 : }
     637                 :             : 
     638                 :           6 : UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
     639                 :             : {
     640         [ +  + ]:           6 :     if (verbose) {
     641         [ +  + ]:           3 :         if (include_mempool_sequence) {
     642   [ +  -  +  - ]:           2 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
     643                 :             :         }
     644                 :           2 :         LOCK(pool.cs);
     645                 :           2 :         UniValue o(UniValue::VOBJ);
     646   [ +  -  -  + ]:           2 :         for (const CTxMemPoolEntry& e : pool.entryAll()) {
     647                 :           0 :             UniValue info(UniValue::VOBJ);
     648         [ #  # ]:           0 :             entryToJSON(pool, info, e);
     649                 :             :             // Mempool has unique entries so there is no advantage in using
     650                 :             :             // UniValue::pushKV, which checks if the key already exists in O(N).
     651                 :             :             // UniValue::pushKVEnd is used instead which currently is O(1).
     652   [ #  #  #  # ]:           0 :             o.pushKVEnd(e.GetTx().GetHash().ToString(), std::move(info));
     653                 :           0 :         }
     654         [ +  - ]:           2 :         return o;
     655                 :           2 :     } else {
     656                 :           3 :         UniValue a(UniValue::VARR);
     657                 :           3 :         uint64_t mempool_sequence;
     658                 :           3 :         {
     659         [ +  - ]:           3 :             LOCK(pool.cs);
     660   [ +  -  -  -  :           3 :             for (const CTxMemPoolEntry& e : pool.entryAll()) {
                   -  + ]
     661   [ #  #  #  #  :           0 :                 a.push_back(e.GetTx().GetHash().ToString());
                   #  # ]
     662                 :             :             }
     663         [ +  - ]:           3 :             mempool_sequence = pool.GetSequence();
     664                 :           0 :         }
     665         [ +  + ]:           3 :         if (!include_mempool_sequence) {
     666                 :           2 :             return a;
     667                 :             :         } else {
     668                 :           1 :             UniValue o(UniValue::VOBJ);
     669   [ +  -  +  - ]:           2 :             o.pushKV("txids", std::move(a));
     670   [ +  -  +  -  :           2 :             o.pushKV("mempool_sequence", mempool_sequence);
                   +  - ]
     671                 :           1 :             return o;
     672                 :           1 :         }
     673                 :           3 :     }
     674                 :             : }
     675                 :             : 
     676                 :          65 : static RPCMethod getmempoolfeeratediagram()
     677                 :             : {
     678                 :          65 :     return RPCMethod{"getmempoolfeeratediagram",
     679         [ +  - ]:         130 :         "Returns the feerate diagram for the whole mempool.",
     680                 :             :         {},
     681                 :             :         {
     682                 :           0 :             RPCResult{"mempool chunks",
     683   [ +  -  +  - ]:         130 :                 RPCResult::Type::ARR, "", "",
     684                 :             :                 {
     685                 :             :                     {
     686   [ +  -  +  - ]:         130 :                         RPCResult::Type::OBJ, "", "",
     687                 :             :                         {
     688   [ +  -  +  - ]:         130 :                             {RPCResult::Type::NUM, "weight", "cumulative sigops-adjusted weight"},
     689   [ +  -  +  - ]:         130 :                             {RPCResult::Type::STR_AMOUNT, "fee", "cumulative fee"}
     690                 :             :                         }
     691                 :             :                     }
     692                 :             :                 }
     693                 :         585 :             }
           [ +  -  +  -  
          +  +  +  +  -  
                -  -  - ]
     694                 :             :         },
     695                 :          65 :         RPCExamples{
     696   [ +  -  +  -  :         130 :             HelpExampleCli("getmempoolfeeratediagram", "")
                   +  - ]
     697   [ +  -  +  -  :         260 :             + HelpExampleRpc("getmempoolfeeratediagram", "")
                   +  - ]
     698         [ +  - ]:          65 :         },
     699                 :          65 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     700                 :             :         {
     701                 :           1 :             const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
     702                 :           1 :             LOCK(mempool.cs);
     703                 :             : 
     704                 :           1 :             UniValue result(UniValue::VARR);
     705                 :             : 
     706         [ +  - ]:           1 :             auto diagram = mempool.GetFeerateDiagram();
     707                 :             : 
     708         [ +  + ]:           2 :             for (auto f : diagram) {
     709                 :           1 :                 UniValue o(UniValue::VOBJ);
     710   [ +  -  +  -  :           2 :                 o.pushKV("weight", f.size);
                   +  - ]
     711   [ +  -  +  -  :           2 :                 o.pushKV("fee", ValueFromAmount(f.fee));
                   +  - ]
     712   [ +  -  +  - ]:           1 :                 result.push_back(o);
     713                 :           1 :             }
     714                 :           1 :             return result;
     715         [ +  - ]:           2 :         }
     716                 :         520 :     };
           [ +  -  +  -  
          +  -  +  -  +  
                +  -  - ]
     717   [ +  -  +  -  :         520 : }
          +  -  +  -  -  
                      - ]
     718                 :             : 
     719                 :          81 : static RPCMethod getrawmempool()
     720                 :             : {
     721                 :          81 :     return RPCMethod{
     722                 :          81 :         "getrawmempool",
     723         [ +  - ]:         162 :         "Returns all transaction ids in memory pool as a json array of string transaction ids.\n"
     724                 :             :         "\nHint: use getmempoolentry to fetch a specific transaction from the mempool.\n",
     725                 :             :         {
     726   [ +  -  +  -  :         243 :             {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
                   +  - ]
     727   [ +  -  +  -  :         243 :             {"mempool_sequence", RPCArg::Type::BOOL, RPCArg::Default{false}, "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
                   +  - ]
     728                 :             :         },
     729                 :             :         {
     730         [ +  - ]:          81 :             RPCResult{"for verbose = false",
     731   [ +  -  +  - ]:         162 :                 RPCResult::Type::ARR, "", "",
     732                 :             :                 {
     733   [ +  -  +  - ]:         162 :                     {RPCResult::Type::STR_HEX, "", "The transaction id"},
     734   [ +  -  +  +  :         324 :                 }},
                   -  - ]
     735         [ +  - ]:         162 :             RPCResult{"for verbose = true",
     736   [ +  -  +  - ]:         162 :                 RPCResult::Type::OBJ_DYN, "", "",
     737                 :             :                 {
     738   [ +  -  +  -  :         162 :                     {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
                   +  - ]
     739   [ +  -  +  +  :         243 :                 }},
                   -  - ]
     740         [ +  - ]:         162 :             RPCResult{"for verbose = false and mempool_sequence = true",
     741   [ +  -  +  - ]:         162 :                 RPCResult::Type::OBJ, "", "",
     742                 :             :                 {
     743   [ +  -  +  - ]:         162 :                     {RPCResult::Type::ARR, "txids", "",
     744                 :             :                     {
     745   [ +  -  +  - ]:         162 :                         {RPCResult::Type::STR_HEX, "", "The transaction id"},
     746                 :             :                     }},
     747   [ +  -  +  - ]:         162 :                     {RPCResult::Type::NUM, "mempool_sequence", "The mempool sequence value."},
     748                 :         729 :                 }},
           [ +  -  +  -  
          +  +  +  +  -  
                -  -  - ]
     749                 :             :         },
     750                 :          81 :         RPCExamples{
     751   [ +  -  +  -  :         162 :             HelpExampleCli("getrawmempool", "true")
                   +  - ]
     752   [ +  -  +  -  :         324 :             + HelpExampleRpc("getrawmempool", "true")
                   +  - ]
     753         [ +  - ]:          81 :         },
     754                 :          81 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     755                 :             : {
     756                 :           6 :     const bool fVerbose{self.Arg<bool>("verbose")};
     757                 :           6 :     const bool include_mempool_sequence{self.Arg<bool>("mempool_sequence")};
     758                 :             : 
     759                 :           6 :     return MempoolToJSON(EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
     760                 :             : },
     761                 :         972 :     };
           [ +  -  +  -  
          +  -  +  -  +  
          +  +  +  -  -  
                   -  - ]
     762                 :        1620 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
                   -  - ]
     763                 :             : 
     764                 :          77 : static RPCMethod getmempoolancestors()
     765                 :             : {
     766                 :          77 :     return RPCMethod{
     767                 :          77 :         "getmempoolancestors",
     768         [ +  - ]:         154 :         "If txid is in the mempool, returns all in-mempool ancestors.\n",
     769                 :             :         {
     770   [ +  -  +  - ]:         154 :             {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
     771   [ +  -  +  -  :         231 :             {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
                   +  - ]
     772                 :             :         },
     773                 :             :         {
     774         [ +  - ]:          77 :             RPCResult{"for verbose = false",
     775   [ +  -  +  - ]:         154 :                 RPCResult::Type::ARR, "", "",
     776   [ +  -  +  -  :         385 :                 {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool ancestor transaction"}}},
          +  -  +  +  -  
                      - ]
     777         [ +  - ]:         154 :             RPCResult{"for verbose = true",
     778   [ +  -  +  - ]:         154 :                 RPCResult::Type::OBJ_DYN, "", "",
     779                 :             :                 {
     780   [ +  -  +  -  :         154 :                     {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
                   +  - ]
     781   [ +  -  +  +  :         231 :                 }},
                   -  - ]
     782                 :             :         },
     783                 :          77 :         RPCExamples{
     784   [ +  -  +  -  :         154 :             HelpExampleCli("getmempoolancestors", "\"mytxid\"")
                   +  - ]
     785   [ +  -  +  -  :         308 :             + HelpExampleRpc("getmempoolancestors", "\"mytxid\"")
                   +  - ]
     786         [ +  - ]:          77 :         },
     787                 :          77 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     788                 :             : {
     789                 :           4 :     const bool fVerbose{self.Arg<bool>("verbose")};
     790                 :             : 
     791                 :           4 :     auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
     792                 :             : 
     793                 :           2 :     const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
     794                 :           2 :     LOCK(mempool.cs);
     795                 :             : 
     796         [ +  - ]:           2 :     const auto entry{mempool.GetEntry(txid)};
     797         [ +  - ]:           2 :     if (entry == nullptr) {
     798   [ +  -  +  - ]:           4 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
     799                 :             :     }
     800                 :             : 
     801         [ #  # ]:           0 :     auto ancestors{mempool.CalculateMemPoolAncestors(*entry)};
     802                 :             : 
     803         [ #  # ]:           0 :     if (!fVerbose) {
     804                 :           0 :         UniValue o(UniValue::VARR);
     805         [ #  # ]:           0 :         for (CTxMemPool::txiter ancestorIt : ancestors) {
     806   [ #  #  #  #  :           0 :             o.push_back(ancestorIt->GetTx().GetHash().ToString());
                   #  # ]
     807                 :             :         }
     808                 :             :         return o;
     809                 :           0 :     } else {
     810                 :           0 :         UniValue o(UniValue::VOBJ);
     811         [ #  # ]:           0 :         for (CTxMemPool::txiter ancestorIt : ancestors) {
     812                 :           0 :             const CTxMemPoolEntry &e = *ancestorIt;
     813                 :           0 :             UniValue info(UniValue::VOBJ);
     814         [ #  # ]:           0 :             entryToJSON(mempool, info, e);
     815   [ #  #  #  # ]:           0 :             o.pushKVEnd(e.GetTx().GetHash().ToString(), std::move(info));
     816                 :           0 :         }
     817                 :           0 :         return o;
     818                 :           0 :     }
     819         [ #  # ]:           0 : },
     820                 :         847 :     };
           [ +  -  +  -  
          +  -  +  -  +  
          +  +  +  -  -  
                   -  - ]
     821                 :         924 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
                   -  - ]
     822                 :             : 
     823                 :          79 : static RPCMethod getmempooldescendants()
     824                 :             : {
     825                 :          79 :     return RPCMethod{
     826                 :          79 :         "getmempooldescendants",
     827         [ +  - ]:         158 :         "If txid is in the mempool, returns all in-mempool descendants.\n",
     828                 :             :         {
     829   [ +  -  +  - ]:         158 :             {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
     830   [ +  -  +  -  :         237 :             {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
                   +  - ]
     831                 :             :         },
     832                 :             :         {
     833         [ +  - ]:          79 :             RPCResult{"for verbose = false",
     834   [ +  -  +  - ]:         158 :                 RPCResult::Type::ARR, "", "",
     835   [ +  -  +  -  :         395 :                 {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool descendant transaction"}}},
          +  -  +  +  -  
                      - ]
     836         [ +  - ]:         158 :             RPCResult{"for verbose = true",
     837   [ +  -  +  - ]:         158 :                 RPCResult::Type::OBJ_DYN, "", "",
     838                 :             :                 {
     839   [ +  -  +  -  :         158 :                     {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
                   +  - ]
     840   [ +  -  +  +  :         237 :                 }},
                   -  - ]
     841                 :             :         },
     842                 :          79 :         RPCExamples{
     843   [ +  -  +  -  :         158 :             HelpExampleCli("getmempooldescendants", "\"mytxid\"")
                   +  - ]
     844   [ +  -  +  -  :         316 :             + HelpExampleRpc("getmempooldescendants", "\"mytxid\"")
                   +  - ]
     845         [ +  - ]:          79 :         },
     846                 :          79 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     847                 :             : {
     848                 :           5 :     const bool fVerbose{self.Arg<bool>("verbose")};
     849                 :             : 
     850                 :           5 :     auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
     851                 :             : 
     852                 :           2 :     const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
     853                 :           2 :     LOCK(mempool.cs);
     854                 :             : 
     855         [ +  - ]:           2 :     const auto it{mempool.GetIter(txid)};
     856         [ +  - ]:           2 :     if (!it) {
     857   [ +  -  +  - ]:           4 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
     858                 :             :     }
     859                 :             : 
     860         [ #  # ]:           0 :     CTxMemPool::setEntries setDescendants;
     861         [ #  # ]:           0 :     mempool.CalculateDescendants(*it, setDescendants);
     862                 :             :     // CTxMemPool::CalculateDescendants will include the given tx
     863                 :           0 :     setDescendants.erase(*it);
     864                 :             : 
     865         [ #  # ]:           0 :     if (!fVerbose) {
     866                 :           0 :         UniValue o(UniValue::VARR);
     867         [ #  # ]:           0 :         for (CTxMemPool::txiter descendantIt : setDescendants) {
     868   [ #  #  #  #  :           0 :             o.push_back(descendantIt->GetTx().GetHash().ToString());
                   #  # ]
     869                 :             :         }
     870                 :             : 
     871                 :             :         return o;
     872                 :           0 :     } else {
     873                 :           0 :         UniValue o(UniValue::VOBJ);
     874         [ #  # ]:           0 :         for (CTxMemPool::txiter descendantIt : setDescendants) {
     875                 :           0 :             const CTxMemPoolEntry &e = *descendantIt;
     876                 :           0 :             UniValue info(UniValue::VOBJ);
     877         [ #  # ]:           0 :             entryToJSON(mempool, info, e);
     878   [ #  #  #  # ]:           0 :             o.pushKVEnd(e.GetTx().GetHash().ToString(), std::move(info));
     879                 :           0 :         }
     880                 :           0 :         return o;
     881                 :           0 :     }
     882         [ #  # ]:           0 : },
     883                 :         869 :     };
           [ +  -  +  -  
          +  -  +  -  +  
          +  +  +  -  -  
                   -  - ]
     884                 :         948 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
                   -  - ]
     885                 :             : 
     886                 :          89 : static RPCMethod getmempoolcluster()
     887                 :             : {
     888                 :          89 :     return RPCMethod{"getmempoolcluster",
     889         [ +  - ]:         178 :         "Returns mempool data for given cluster\n",
     890                 :             :         {
     891   [ +  -  +  - ]:         178 :             {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The txid of a transaction in the cluster"},
     892                 :             :         },
     893         [ +  - ]:         178 :         RPCResult{
     894   [ +  -  +  -  :         178 :             RPCResult::Type::OBJ, "", "", ClusterDescription()},
             +  -  +  - ]
     895                 :         178 :         RPCExamples{
     896   [ +  -  +  -  :         178 :             HelpExampleCli("getmempoolcluster", "txid")
                   +  - ]
     897   [ +  -  +  -  :         356 :             + HelpExampleRpc("getmempoolcluster", R"("txid")")
             +  -  +  - ]
     898         [ +  - ]:          89 :         },
     899                 :          89 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     900                 :             : {
     901                 :           2 :     uint256 hash = ParseHashV(request.params[0], "txid");
     902                 :             : 
     903                 :           2 :     const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
     904                 :           2 :     LOCK(mempool.cs);
     905                 :             : 
     906         [ +  - ]:           2 :     auto txid = Txid::FromUint256(hash);
     907         [ +  - ]:           2 :     const auto entry{mempool.GetEntry(txid)};
     908         [ +  - ]:           2 :     if (entry == nullptr) {
     909   [ +  -  +  - ]:           4 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
     910                 :             :     }
     911                 :             : 
     912         [ #  # ]:           0 :     auto cluster = mempool.GetCluster(txid);
     913                 :             : 
     914                 :           0 :     UniValue info(UniValue::VOBJ);
     915   [ #  #  #  # ]:           0 :     clusterToJSON(mempool, info, cluster);
     916                 :           0 :     return info;
     917         [ #  # ]:           0 : },
     918   [ +  -  +  -  :         445 :     };
             +  +  -  - ]
     919         [ +  - ]:         178 : }
     920                 :             : 
     921                 :          78 : static RPCMethod getmempoolentry()
     922                 :             : {
     923                 :          78 :     return RPCMethod{
     924                 :          78 :         "getmempoolentry",
     925         [ +  - ]:         156 :         "Returns mempool data for given transaction\n",
     926                 :             :         {
     927   [ +  -  +  - ]:         156 :             {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
     928                 :             :         },
     929         [ +  - ]:         156 :         RPCResult{
     930   [ +  -  +  -  :         156 :             RPCResult::Type::OBJ, "", "", MempoolEntryDescription()},
             +  -  +  - ]
     931                 :          78 :         RPCExamples{
     932   [ +  -  +  -  :         156 :             HelpExampleCli("getmempoolentry", "\"mytxid\"")
                   +  - ]
     933   [ +  -  +  -  :         312 :             + HelpExampleRpc("getmempoolentry", "\"mytxid\"")
             +  -  +  - ]
     934         [ +  - ]:          78 :         },
     935                 :          78 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     936                 :             : {
     937                 :           3 :     auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
     938                 :             : 
     939                 :           2 :     const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
     940                 :           2 :     LOCK(mempool.cs);
     941                 :             : 
     942         [ +  - ]:           2 :     const auto entry{mempool.GetEntry(txid)};
     943         [ +  - ]:           2 :     if (entry == nullptr) {
     944   [ +  -  +  - ]:           4 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
     945                 :             :     }
     946                 :             : 
     947                 :           0 :     UniValue info(UniValue::VOBJ);
     948         [ #  # ]:           0 :     entryToJSON(mempool, info, *entry);
     949         [ #  # ]:           0 :     return info;
     950                 :           0 : },
     951   [ +  -  +  -  :         390 :     };
             +  +  -  - ]
     952         [ +  - ]:         156 : }
     953                 :             : 
     954                 :         109 : static RPCMethod gettxspendingprevout()
     955                 :             : {
     956                 :         109 :     return RPCMethod{"gettxspendingprevout",
     957         [ +  - ]:         218 :         "Scans the mempool (and the txospenderindex, if available) to find transactions spending any of the given outputs",
     958                 :             :         {
     959   [ +  -  +  - ]:         218 :             {"outputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The transaction outputs that we want to check, and within each, the txid (string) vout (numeric).",
     960                 :             :                 {
     961   [ +  -  +  - ]:         218 :                     {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
     962                 :             :                         {
     963   [ +  -  +  - ]:         218 :                             {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
     964   [ +  -  +  - ]:         218 :                             {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
     965                 :             :                         },
     966                 :             :                     },
     967                 :             :                 },
     968                 :             :             },
     969   [ +  -  +  - ]:         218 :             {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
     970                 :             :                 {
     971   [ +  -  +  -  :         327 :                     {"mempool_only", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true if txospenderindex unavailable, otherwise false"}, "If false and mempool lacks a relevant spend, use txospenderindex (throws an exception if not available)."},
                   +  - ]
     972   [ +  -  +  -  :         327 :                     {"return_spending_tx", RPCArg::Type::BOOL, RPCArg::DefaultHint{"false"}, "If true, return the full spending tx."},
                   +  - ]
     973                 :             :                 },
     974                 :             :             },
     975                 :             :         },
     976         [ +  - ]:         218 :         RPCResult{
     977   [ +  -  +  - ]:         218 :             RPCResult::Type::ARR, "", "",
     978                 :             :             {
     979   [ +  -  +  - ]:         218 :                 {RPCResult::Type::OBJ, "", "",
     980                 :             :                 {
     981   [ +  -  +  - ]:         218 :                     {RPCResult::Type::STR_HEX, "txid", "the transaction id of the checked output"},
     982   [ +  -  +  - ]:         218 :                     {RPCResult::Type::NUM, "vout", "the vout value of the checked output"},
     983   [ +  -  +  - ]:         218 :                     {RPCResult::Type::STR_HEX, "spendingtxid", /*optional=*/true, "the transaction id of the mempool transaction spending this output (omitted if unspent)"},
     984   [ +  -  +  - ]:         218 :                     {RPCResult::Type::STR_HEX, "spendingtx", /*optional=*/true, "the transaction spending this output (only if return_spending_tx is set, omitted if unspent)"},
     985   [ +  -  +  - ]:         218 :                     {RPCResult::Type::STR_HEX, "blockhash", /*optional=*/true, "the hash of the spending block (omitted if unspent or the spending tx is not confirmed)"},
     986                 :             :                 }},
     987                 :             :             }
     988                 :        1635 :         },
           [ +  -  +  -  
          +  -  +  +  +  
             +  -  -  -  
                      - ]
     989                 :         109 :         RPCExamples{
     990   [ +  -  +  -  :         218 :             HelpExampleCli("gettxspendingprevout", "\"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":3}]\"")
                   +  - ]
     991   [ +  -  +  -  :         436 :             + HelpExampleRpc("gettxspendingprevout", "\"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":3}]\"")
             +  -  +  - ]
     992                 :         654 :             + HelpExampleCliNamed("gettxspendingprevout", {{"outputs", "[{\"txid\":\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\",\"vout\":3}]"}, {"return_spending_tx", true}})
           [ +  -  +  -  
          +  -  +  -  +  
                +  -  - ]
     993         [ +  - ]:         109 :         },
     994                 :         109 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
     995                 :             :         {
     996                 :          31 :             const UniValue& output_params = request.params[0].get_array();
     997   [ -  +  +  + ]:          31 :             if (output_params.empty()) {
     998   [ +  -  +  - ]:           6 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, outputs are missing");
     999                 :             :             }
    1000         [ +  + ]:          28 :             const UniValue options{request.params[1].isNull() ? UniValue::VOBJ : request.params[1]};
    1001   [ +  +  +  +  :         129 :             RPCTypeCheckObj(options,
                   +  + ]
    1002                 :             :                             {
    1003         [ +  - ]:          28 :                                 {"mempool_only", UniValueType(UniValue::VBOOL)},
    1004         [ +  - ]:          28 :                                 {"return_spending_tx", UniValueType(UniValue::VBOOL)},
    1005                 :             :                             }, /*fAllowNull=*/true, /*fStrict=*/true);
    1006                 :             : 
    1007   [ +  -  +  +  :          24 :             const bool mempool_only{options.exists("mempool_only") ? options["mempool_only"].get_bool() : !g_txospenderindex};
          +  -  +  -  +  
                      - ]
    1008   [ +  -  -  +  :          22 :             const bool return_spending_tx{options.exists("return_spending_tx") ? options["return_spending_tx"].get_bool() : false};
          -  -  -  -  -  
                      - ]
    1009                 :             : 
    1010                 :             :             // Worklist of outpoints to resolve
    1011                 :          11 :             struct Entry {
    1012                 :             :                 COutPoint outpoint;
    1013                 :             :                 size_t request_index;
    1014                 :             :             };
    1015                 :          11 :             std::vector<Entry> prevouts_to_process;
    1016   [ -  +  +  - ]:          11 :             prevouts_to_process.reserve(output_params.size());
    1017   [ -  +  +  - ]:          11 :             for (const size_t idx : std::views::iota(size_t{0}, output_params.size())) {
    1018   [ +  -  +  + ]:          11 :                 const UniValue& o = output_params[idx].get_obj();
    1019                 :             : 
    1020   [ -  +  -  -  :          35 :                 RPCTypeCheckObj(o,
                   +  + ]
    1021                 :             :                                 {
    1022         [ +  - ]:           7 :                                     {"txid", UniValueType(UniValue::VSTR)},
    1023         [ +  - ]:           7 :                                     {"vout", UniValueType(UniValue::VNUM)},
    1024                 :             :                                 }, /*fAllowNull=*/false, /*fStrict=*/true);
    1025                 :             : 
    1026         [ #  # ]:           0 :                 const Txid txid = Txid::FromUint256(ParseHashO(o, "txid"));
    1027   [ #  #  #  # ]:           0 :                 const int nOutput{o.find_value("vout").getInt<int>()};
    1028         [ #  # ]:           0 :                 if (nOutput < 0) {
    1029   [ #  #  #  # ]:           0 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
    1030                 :             :                 }
    1031         [ #  # ]:           0 :                 prevouts_to_process.emplace_back(COutPoint{txid, static_cast<uint32_t>(nOutput)}, idx);
    1032                 :             :             }
    1033                 :             : 
    1034                 :           0 :             auto make_output = [&output_params, return_spending_tx](const Entry& prevout, const CTransaction* spending_tx = nullptr) {
    1035         [ #  # ]:           0 :                 UniValue o{output_params[prevout.request_index]};
    1036         [ #  # ]:           0 :                 if (spending_tx) {
    1037   [ #  #  #  #  :           0 :                     o.pushKV("spendingtxid", spending_tx->GetHash().ToString());
             #  #  #  # ]
    1038         [ #  # ]:           0 :                     if (return_spending_tx) {
    1039   [ #  #  #  #  :           0 :                         o.pushKV("spendingtx", EncodeHexTx(*spending_tx));
             #  #  #  # ]
    1040                 :             :                     }
    1041                 :             :                 }
    1042                 :           0 :                 return o;
    1043                 :           0 :             };
    1044                 :             : 
    1045   [ #  #  #  # ]:           0 :             std::vector<UniValue> results(output_params.size());
    1046                 :             : 
    1047                 :             :             // Search the mempool first
    1048                 :           0 :             std::vector<Entry> unresolved;
    1049   [ #  #  #  # ]:           0 :             unresolved.reserve(prevouts_to_process.size());
    1050                 :           0 :             {
    1051         [ #  # ]:           0 :                 const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
    1052         [ #  # ]:           0 :                 LOCK(mempool.cs);
    1053                 :             : 
    1054                 :             :                 // Make the result if the spending tx appears in the mempool or this is a mempool_only request
    1055         [ #  # ]:           0 :                 for (const auto& prevout : prevouts_to_process) {
    1056         [ #  # ]:           0 :                     const auto* spending_tx{mempool.GetConflictTx(prevout.outpoint)};
    1057                 :             : 
    1058                 :             :                     // If the outpoint is not spent in the mempool and this is not a mempool-only
    1059                 :             :                     // request, we cannot answer it yet.
    1060         [ #  # ]:           0 :                     if (!spending_tx && !mempool_only) {
    1061         [ #  # ]:           0 :                         unresolved.push_back(prevout);
    1062                 :             :                     } else {
    1063         [ #  # ]:           0 :                         results[prevout.request_index] = make_output(prevout, spending_tx);
    1064                 :             :                     }
    1065                 :             :                 }
    1066                 :           0 :             }
    1067                 :             : 
    1068                 :             :             // mempool_only requests resolve every outpoint above, so only other requests reach the index.
    1069   [ #  #  #  #  :           0 :             if (!unresolved.empty() && (!g_txospenderindex || !g_txospenderindex->BlockUntilSyncedToCurrentChain())) {
             #  #  #  # ]
    1070   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_MISC_ERROR, "Mempool lacks a relevant spend, and txospenderindex is unavailable.");
    1071                 :             :             }
    1072                 :             : 
    1073         [ #  # ]:           0 :             for (const auto& prevout : unresolved) {
    1074         [ #  # ]:           0 :                 const auto spender{g_txospenderindex->FindSpender(prevout.outpoint)};
    1075         [ #  # ]:           0 :                 if (!spender) {
    1076         [ #  # ]:           0 :                     throw JSONRPCError(RPC_MISC_ERROR, spender.error());
    1077                 :             :                 }
    1078                 :             : 
    1079   [ #  #  #  # ]:           0 :                 if (const auto& spender_opt{spender.value()}) {
    1080         [ #  # ]:           0 :                     UniValue o{make_output(prevout, spender_opt->tx.get())};
    1081   [ #  #  #  #  :           0 :                     o.pushKV("blockhash", spender_opt->block_hash.GetHex());
             #  #  #  # ]
    1082                 :           0 :                     results[prevout.request_index] = std::move(o);
    1083                 :           0 :                 } else {
    1084                 :             :                     // Only return the input outpoint itself, which indicates it is unspent.
    1085         [ #  # ]:           0 :                     results[prevout.request_index] = make_output(prevout);
    1086                 :             :                 }
    1087                 :           0 :             }
    1088                 :             : 
    1089                 :           0 :             UniValue result{UniValue::VARR};
    1090   [ #  #  #  # ]:           0 :             result.reserve(results.size());
    1091   [ #  #  #  # ]:           0 :             for (auto& output : results) result.push_back(std::move(output));
    1092                 :           0 :             return result;
    1093                 :          87 :         },
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
                   -  + ]
    1094                 :        1853 :     };
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  +  +  +  
          +  +  +  +  -  
          -  -  -  -  -  
                   -  - ]
    1095                 :        2943 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
                      - ]
    1096                 :             : 
    1097                 :           1 : UniValue MempoolInfoToJSON(const CTxMemPool& pool)
    1098                 :             : {
    1099                 :             :     // Make sure this call is atomic in the pool.
    1100                 :           1 :     LOCK(pool.cs);
    1101                 :           1 :     UniValue ret(UniValue::VOBJ);
    1102   [ +  -  +  -  :           2 :     ret.pushKV("loaded", pool.GetLoadTried());
             +  -  +  - ]
    1103   [ +  -  +  -  :           2 :     ret.pushKV("size", pool.size());
             +  -  +  - ]
    1104   [ +  -  +  -  :           2 :     ret.pushKV("bytes", pool.GetTotalTxSize());
                   +  - ]
    1105   [ +  -  +  -  :           2 :     ret.pushKV("usage", pool.DynamicMemoryUsage());
             +  -  +  - ]
    1106   [ +  -  +  -  :           2 :     ret.pushKV("total_fee", ValueFromAmount(pool.GetTotalFee()));
                   +  - ]
    1107   [ +  -  +  -  :           2 :     ret.pushKV("maxmempool", pool.m_opts.max_size_bytes);
                   +  - ]
    1108   [ +  -  +  -  :           2 :     ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(), pool.m_opts.min_relay_feerate).GetFeePerK()));
          +  -  +  -  +  
                      - ]
    1109   [ +  -  +  -  :           2 :     ret.pushKV("minrelaytxfee", ValueFromAmount(pool.m_opts.min_relay_feerate.GetFeePerK()));
             +  -  +  - ]
    1110   [ +  -  +  -  :           2 :     ret.pushKV("incrementalrelayfee", ValueFromAmount(pool.m_opts.incremental_relay_feerate.GetFeePerK()));
             +  -  +  - ]
    1111   [ +  -  +  -  :           2 :     ret.pushKV("unbroadcastcount", pool.GetUnbroadcastTxCount());
             +  -  +  - ]
    1112   [ +  -  +  -  :           2 :     ret.pushKV("permitbaremultisig", pool.m_opts.permit_bare_multisig);
                   +  - ]
    1113   [ +  -  +  -  :           3 :     ret.pushKV("maxdatacarriersize", pool.m_opts.max_datacarrier_bytes.value_or(0));
             +  -  +  - ]
    1114   [ +  -  +  -  :           2 :     ret.pushKV("limitclustercount", pool.m_opts.limits.cluster_count);
                   +  - ]
    1115   [ +  -  +  -  :           2 :     ret.pushKV("limitclustersize", pool.m_opts.limits.cluster_size_vbytes);
                   +  - ]
    1116   [ +  -  +  -  :           2 :     ret.pushKV("optimal", pool.m_txgraph->DoWork(0)); // 0 work is a quick check for known optimality
                   +  - ]
    1117   [ +  -  +  -  :           1 :     if (IsDeprecatedRPCEnabled("fullrbf")) {
                   -  + ]
    1118   [ #  #  #  #  :           0 :         ret.pushKV("fullrbf", true);
                   #  # ]
    1119                 :             :     }
    1120         [ +  - ]:           1 :     return ret;
    1121                 :           1 : }
    1122                 :             : 
    1123                 :          77 : static RPCMethod getmempoolinfo()
    1124                 :             : {
    1125                 :          77 :     return RPCMethod{"getmempoolinfo",
    1126         [ +  - ]:         154 :         "Returns details on the active state of the TX memory pool.",
    1127                 :             :         {},
    1128         [ +  - ]:         154 :         RPCResult{
    1129         [ +  - ]:         154 :             RPCResult::Type::OBJ, "", "",
    1130                 :          77 :             [](){
    1131                 :          77 :                 std::vector<RPCResult> list = {
    1132   [ +  -  +  - ]:         154 :                     {RPCResult::Type::BOOL, "loaded", "True if the initial load attempt of the persisted mempool finished"},
    1133   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "size", "Current tx count"},
    1134   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "bytes", "Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted"},
    1135   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "usage", "Total memory usage for the mempool"},
    1136   [ +  -  +  - ]:         154 :                     {RPCResult::Type::STR_AMOUNT, "total_fee", "Total fees for the mempool in " + CURRENCY_UNIT + ", ignoring modified fees through prioritisetransaction"},
    1137   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "maxmempool", "Maximum memory usage for the mempool"},
    1138   [ +  -  +  - ]:         154 :                     {RPCResult::Type::STR_AMOUNT, "mempoolminfee", "Minimum fee rate in " + CURRENCY_UNIT + "/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee"},
    1139   [ +  -  +  - ]:         154 :                     {RPCResult::Type::STR_AMOUNT, "minrelaytxfee", "Current minimum relay fee for transactions"},
    1140   [ +  -  +  - ]:         154 :                     {RPCResult::Type::STR_AMOUNT, "incrementalrelayfee", "minimum fee rate increment for mempool limiting or replacement in " + CURRENCY_UNIT + "/kvB"},
    1141   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "unbroadcastcount", "Current number of transactions that haven't passed initial broadcast yet"},
    1142   [ +  -  +  - ]:         154 :                     {RPCResult::Type::BOOL, "permitbaremultisig", "True if the mempool accepts transactions with bare multisig outputs"},
    1143   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "maxdatacarriersize", "Maximum number of bytes that can be used by OP_RETURN outputs in the mempool"},
    1144   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "limitclustercount", "Maximum number of transactions that can be in a cluster (configured by -limitclustercount)"},
    1145   [ +  -  +  - ]:         154 :                     {RPCResult::Type::NUM, "limitclustersize", "Maximum size of a cluster in virtual bytes (configured by -limitclustersize)"},
    1146   [ +  -  +  - ]:         154 :                     {RPCResult::Type::BOOL, "optimal", "If the mempool is in a known-optimal transaction ordering"},
    1147   [ +  -  +  +  :        2464 :                 };
                   -  - ]
    1148   [ +  -  +  -  :          77 :                 if (IsDeprecatedRPCEnabled("fullrbf")) {
                   -  + ]
    1149         [ #  # ]:           0 :                     list.emplace_back(RPCResult::Type::BOOL, "fullrbf", "True if the mempool accepts RBF without replaceability signaling inspection (DEPRECATED)");
    1150                 :             :                 }
    1151                 :          77 :                 return list;
    1152                 :        2464 :             }()
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  -  
                      - ]
    1153         [ +  - ]:         154 :             },
    1154                 :          77 :         RPCExamples{
    1155   [ +  -  +  -  :         154 :             HelpExampleCli("getmempoolinfo", "")
                   +  - ]
    1156   [ +  -  +  -  :         308 :             + HelpExampleRpc("getmempoolinfo", "")
             +  -  +  - ]
    1157         [ +  - ]:          77 :         },
    1158                 :          77 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
    1159                 :             : {
    1160                 :           1 :     return MempoolInfoToJSON(EnsureAnyMemPool(request.context));
    1161                 :             : },
    1162   [ +  -  +  - ]:         308 :     };
    1163                 :             : }
    1164                 :             : 
    1165                 :          70 : static RPCMethod importmempool()
    1166                 :             : {
    1167                 :          70 :     return RPCMethod{
    1168                 :          70 :         "importmempool",
    1169         [ +  - ]:         140 :         "Import a mempool.dat file and attempt to add its contents to the mempool.\n"
    1170                 :             :         "Warning: Importing untrusted files is dangerous, especially if metadata from the file is taken over.",
    1171                 :             :         {
    1172   [ +  -  +  - ]:         140 :             {"filepath", RPCArg::Type::STR, RPCArg::Optional::NO, "The mempool file"},
    1173         [ +  - ]:         140 :             {"options",
    1174                 :             :              RPCArg::Type::OBJ_NAMED_PARAMS,
    1175                 :          70 :              RPCArg::Optional::OMITTED,
    1176         [ +  - ]:         140 :              "",
    1177                 :             :              {
    1178   [ +  -  +  - ]:         140 :                  {"use_current_time", RPCArg::Type::BOOL, RPCArg::Default{true},
    1179         [ +  - ]:         140 :                   "Whether to use the current system time or use the entry time metadata from the mempool file.\n"
    1180                 :             :                   "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior."},
    1181   [ +  -  +  - ]:         140 :                  {"apply_fee_delta_priority", RPCArg::Type::BOOL, RPCArg::Default{false},
    1182         [ +  - ]:         140 :                   "Whether to apply the fee delta metadata from the mempool file.\n"
    1183                 :             :                   "It will be added to any existing fee deltas.\n"
    1184                 :             :                   "The fee delta can be set by the prioritisetransaction RPC.\n"
    1185                 :             :                   "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior.\n"
    1186                 :             :                   "Only set this bool if you understand what it does."},
    1187   [ +  -  +  - ]:         140 :                  {"apply_unbroadcast_set", RPCArg::Type::BOOL, RPCArg::Default{false},
    1188         [ +  - ]:         140 :                   "Whether to apply the unbroadcast set metadata from the mempool file.\n"
    1189                 :             :                   "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior."},
    1190                 :             :              },
    1191         [ +  - ]:          70 :              RPCArgOptions{.oneline_description = "options"}},
    1192                 :             :         },
    1193   [ +  -  +  -  :         140 :         RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
             +  -  +  - ]
    1194                 :         210 :         RPCExamples{HelpExampleCli("importmempool", "/path/to/mempool.dat") + HelpExampleRpc("importmempool", R"("/path/to/mempool.dat")")},
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1195                 :          70 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue {
    1196                 :           0 :             const NodeContext& node{EnsureAnyNodeContext(request.context)};
    1197                 :             : 
    1198                 :           0 :             CTxMemPool& mempool{EnsureMemPool(node)};
    1199                 :           0 :             ChainstateManager& chainman = EnsureChainman(node);
    1200                 :           0 :             Chainstate& chainstate = chainman.ActiveChainstate();
    1201                 :             : 
    1202         [ #  # ]:           0 :             if (chainman.IsInitialBlockDownload()) {
    1203   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Can only import the mempool after the block download and sync is done.");
    1204                 :             :             }
    1205                 :             : 
    1206                 :           0 :             const fs::path load_path{fs::u8path(self.Arg<std::string_view>("filepath"))};
    1207   [ #  #  #  #  :           0 :             const UniValue& use_current_time{request.params[1]["use_current_time"]};
                   #  # ]
    1208   [ #  #  #  #  :           0 :             const UniValue& apply_fee_delta{request.params[1]["apply_fee_delta_priority"]};
                   #  # ]
    1209   [ #  #  #  #  :           0 :             const UniValue& apply_unbroadcast{request.params[1]["apply_unbroadcast_set"]};
                   #  # ]
    1210         [ #  # ]:           0 :             node::ImportMempoolOptions opts{
    1211   [ #  #  #  # ]:           0 :                 .use_current_time = use_current_time.isNull() ? true : use_current_time.get_bool(),
    1212   [ #  #  #  # ]:           0 :                 .apply_fee_delta_priority = apply_fee_delta.isNull() ? false : apply_fee_delta.get_bool(),
    1213   [ #  #  #  # ]:           0 :                 .apply_unbroadcast_set = apply_unbroadcast.isNull() ? false : apply_unbroadcast.get_bool(),
    1214   [ #  #  #  #  :           0 :             };
                   #  # ]
    1215                 :             : 
    1216   [ #  #  #  # ]:           0 :             if (!node::LoadMempool(mempool, load_path, chainstate, std::move(opts))) {
    1217   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_MISC_ERROR, "Unable to import mempool file, see debug log for details.");
    1218                 :             :             }
    1219                 :             : 
    1220                 :           0 :             UniValue ret{UniValue::VOBJ};
    1221                 :           0 :             return ret;
    1222                 :           0 :         },
    1223                 :         770 :     };
           [ +  -  +  -  
          +  -  +  +  +  
             +  -  -  -  
                      - ]
    1224                 :         630 : }
           [ +  -  +  -  
          +  -  +  -  +  
             -  -  -  -  
                      - ]
    1225                 :             : 
    1226                 :          69 : static RPCMethod savemempool()
    1227                 :             : {
    1228                 :          69 :     return RPCMethod{
    1229                 :          69 :         "savemempool",
    1230         [ +  - ]:         138 :         "Dumps the mempool to disk. It will fail until the previous dump is fully loaded.\n",
    1231                 :             :         {},
    1232         [ +  - ]:         138 :         RPCResult{
    1233         [ +  - ]:         138 :             RPCResult::Type::OBJ, "", "",
    1234                 :             :             {
    1235   [ +  -  +  - ]:         138 :                 {RPCResult::Type::STR, "filename", "the directory and file where the mempool was saved"},
    1236   [ +  -  +  -  :         276 :             }},
             +  +  -  - ]
    1237                 :          69 :         RPCExamples{
    1238   [ +  -  +  -  :         138 :             HelpExampleCli("savemempool", "")
                   +  - ]
    1239   [ +  -  +  -  :         276 :             + HelpExampleRpc("savemempool", "")
             +  -  +  - ]
    1240         [ +  - ]:          69 :         },
    1241                 :          69 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
    1242                 :             : {
    1243                 :           0 :     const ArgsManager& args{EnsureAnyArgsman(request.context)};
    1244                 :           0 :     const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
    1245                 :             : 
    1246         [ #  # ]:           0 :     if (!mempool.GetLoadTried()) {
    1247   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
    1248                 :             :     }
    1249                 :             : 
    1250                 :           0 :     const fs::path& dump_path = MempoolPath(args);
    1251                 :             : 
    1252   [ #  #  #  # ]:           0 :     if (!DumpMempool(mempool, dump_path)) {
    1253   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
    1254                 :             :     }
    1255                 :             : 
    1256                 :           0 :     UniValue ret(UniValue::VOBJ);
    1257   [ #  #  #  #  :           0 :     ret.pushKV("filename", dump_path.utf8string());
             #  #  #  # ]
    1258                 :             : 
    1259                 :           0 :     return ret;
    1260                 :           0 : },
    1261   [ +  -  +  - ]:         276 :     };
    1262         [ +  - ]:         138 : }
    1263                 :             : 
    1264                 :         150 : static std::vector<RPCResult> OrphanDescription()
    1265                 :             : {
    1266                 :         150 :     return {
    1267   [ +  -  +  - ]:         300 :         RPCResult{RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
    1268   [ +  -  +  - ]:         300 :         RPCResult{RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
    1269   [ +  -  +  - ]:         300 :         RPCResult{RPCResult::Type::NUM, "bytes", "The serialized transaction size in bytes"},
    1270   [ +  -  +  - ]:         300 :         RPCResult{RPCResult::Type::NUM, "vsize", "(DEPRECATED) use vsize_bip141 instead. The virtual transaction size as defined in BIP 141. This is different from actual serialized size for witness transactions as witness data is discounted."},
    1271   [ +  -  +  - ]:         300 :         RPCResult{RPCResult::Type::NUM, "vsize_bip141", "The virtual transaction size as defined in BIP 141. This is different from actual serialized size for witness transactions as witness data is discounted."},
    1272   [ +  -  +  - ]:         300 :         RPCResult{RPCResult::Type::NUM, "weight", "The transaction weight as defined in BIP 141."},
    1273   [ +  -  +  - ]:         300 :         RPCResult{RPCResult::Type::ARR, "from", "",
    1274                 :             :         {
    1275   [ +  -  +  - ]:         300 :             RPCResult{RPCResult::Type::NUM, "peer_id", "Peer ID"},
    1276   [ +  -  +  +  :         450 :         }},
                   -  - ]
    1277   [ +  -  +  +  :        1500 :     };
                   -  - ]
    1278                 :        2400 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  - ]
    1279                 :             : 
    1280                 :           0 : static UniValue OrphanToJSON(const node::TxOrphanage::OrphanInfo& orphan)
    1281                 :             : {
    1282                 :           0 :     UniValue o(UniValue::VOBJ);
    1283   [ #  #  #  #  :           0 :     o.pushKV("txid", orphan.tx->GetHash().ToString());
             #  #  #  # ]
    1284   [ #  #  #  #  :           0 :     o.pushKV("wtxid", orphan.tx->GetWitnessHash().ToString());
             #  #  #  # ]
    1285   [ #  #  #  #  :           0 :     o.pushKV("bytes", orphan.tx->ComputeTotalSize());
             #  #  #  # ]
    1286   [ #  #  #  #  :           0 :     o.pushKV("vsize", GetVirtualTransactionSize(*orphan.tx));
             #  #  #  # ]
    1287   [ #  #  #  #  :           0 :     o.pushKV("vsize_bip141", GetVirtualTransactionSize(*orphan.tx));
             #  #  #  # ]
    1288   [ #  #  #  #  :           0 :     o.pushKV("weight", GetTransactionWeight(*orphan.tx));
                   #  # ]
    1289                 :           0 :     UniValue from(UniValue::VARR);
    1290         [ #  # ]:           0 :     for (const auto fromPeer: orphan.announcers) {
    1291   [ #  #  #  # ]:           0 :         from.push_back(fromPeer);
    1292                 :             :     }
    1293   [ #  #  #  #  :           0 :     o.pushKV("from", from);
                   #  # ]
    1294                 :           0 :     return o;
    1295                 :           0 : }
    1296                 :             : 
    1297                 :          75 : static RPCMethod getorphantxs()
    1298                 :             : {
    1299                 :          75 :     return RPCMethod{
    1300                 :          75 :         "getorphantxs",
    1301         [ +  - ]:         150 :         "Shows transactions in the tx orphanage.\n"
    1302                 :             :         "\nEXPERIMENTAL warning: this call may be changed in future releases.\n",
    1303                 :             :         {
    1304   [ +  -  +  -  :         225 :             {"verbosity", RPCArg::Type::NUM, RPCArg::Default{0}, "0 for an array of txids (may contain duplicates), 1 for an array of objects with tx details, and 2 for details from (1) and tx hex",
                   +  - ]
    1305         [ +  - ]:         150 :              RPCArgOptions{.skip_type_check = true}},
    1306                 :             :         },
    1307                 :             :         {
    1308         [ +  - ]:          75 :             RPCResult{"for verbose = 0",
    1309   [ +  -  +  - ]:         150 :                 RPCResult::Type::ARR, "", "",
    1310                 :             :                 {
    1311   [ +  -  +  - ]:         150 :                     {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
    1312   [ +  -  +  +  :         300 :                 }},
                   -  - ]
    1313         [ +  - ]:         150 :             RPCResult{"for verbose = 1",
    1314   [ +  -  +  - ]:         150 :                 RPCResult::Type::ARR, "", "",
    1315                 :             :                 {
    1316   [ +  -  +  -  :         150 :                     {RPCResult::Type::OBJ, "", "", OrphanDescription()},
                   +  - ]
    1317   [ +  -  +  +  :         225 :                 }},
                   -  - ]
    1318         [ +  - ]:         150 :             RPCResult{"for verbose = 2",
    1319   [ +  -  +  - ]:         150 :                 RPCResult::Type::ARR, "", "",
    1320                 :             :                 {
    1321   [ +  -  +  - ]:         150 :                     {RPCResult::Type::OBJ, "", "",
    1322   [ +  -  +  -  :         375 :                         Cat<std::vector<RPCResult>>(
             +  +  -  - ]
    1323         [ +  - ]:         150 :                             OrphanDescription(),
    1324   [ +  -  +  - ]:         150 :                             {{RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded transaction data"}}
    1325                 :             :                         )
    1326                 :             :                     },
    1327   [ +  -  +  +  :         225 :                 }},
                   -  - ]
    1328                 :             :         },
    1329                 :          75 :         RPCExamples{
    1330   [ +  -  +  -  :         150 :             HelpExampleCli("getorphantxs", "2")
                   +  - ]
    1331   [ +  -  +  -  :         300 :             + HelpExampleRpc("getorphantxs", "2")
                   +  - ]
    1332         [ +  - ]:          75 :         },
    1333                 :          75 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
    1334                 :             :         {
    1335                 :          13 :             const NodeContext& node = EnsureAnyNodeContext(request.context);
    1336                 :          13 :             PeerManager& peerman = EnsurePeerman(node);
    1337                 :          13 :             std::vector<node::TxOrphanage::OrphanInfo> orphanage = peerman.GetOrphanTransactions();
    1338                 :             : 
    1339   [ +  -  +  + ]:          13 :             int verbosity{ParseVerbosity(request.params[0], /*default_verbosity=*/0, /*allow_bool=*/false)};
    1340                 :             : 
    1341                 :           6 :             UniValue ret(UniValue::VARR);
    1342                 :             : 
    1343         [ +  + ]:           6 :             if (verbosity == 0) {
    1344         [ -  + ]:           1 :                 for (auto const& orphan : orphanage) {
    1345   [ #  #  #  #  :           0 :                     ret.push_back(orphan.tx->GetHash().ToString());
                   #  # ]
    1346                 :             :                 }
    1347         [ +  + ]:           5 :             } else if (verbosity == 1) {
    1348         [ -  + ]:           1 :                 for (auto const& orphan : orphanage) {
    1349   [ #  #  #  # ]:           0 :                     ret.push_back(OrphanToJSON(orphan));
    1350                 :             :                 }
    1351         [ +  + ]:           4 :             } else if (verbosity == 2) {
    1352         [ -  + ]:           1 :                 for (auto const& orphan : orphanage) {
    1353         [ #  # ]:           0 :                     UniValue o{OrphanToJSON(orphan)};
    1354   [ #  #  #  #  :           0 :                     o.pushKV("hex", EncodeHexTx(*orphan.tx));
             #  #  #  # ]
    1355   [ #  #  #  # ]:           0 :                     ret.push_back(o);
    1356                 :           0 :                 }
    1357                 :             :             } else {
    1358   [ +  -  +  -  :           6 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid verbosity value " + ToString(verbosity));
                   +  - ]
    1359                 :             :             }
    1360                 :             : 
    1361                 :           3 :             return ret;
    1362                 :          16 :         },
    1363                 :         825 :     };
           [ +  -  +  -  
          +  -  +  -  +  
          +  +  +  -  -  
                   -  - ]
    1364                 :        1125 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  - ]
    1365                 :             : 
    1366                 :         361 : static RPCMethod submitpackage()
    1367                 :             : {
    1368                 :         361 :     return RPCMethod{"submitpackage",
    1369         [ +  - ]:         722 :         "Submit a package of raw transactions (serialized, hex-encoded) to local node.\n"
    1370                 :             :         "The package will be validated according to consensus and mempool policy rules. If any transaction passes, it will be accepted to mempool.\n"
    1371                 :             :         "This RPC is experimental and the interface may be unstable. Refer to doc/policy/packages.md for documentation on package policies.\n"
    1372                 :             :         "Warning: successful submission does not mean the transactions will propagate throughout the network.\n"
    1373                 :             :         ,
    1374                 :             :         {
    1375   [ +  -  +  - ]:         722 :             {"package", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of raw transactions.\n"
    1376                 :             :                 "The package must consist of a transaction with (some, all, or none of) its unconfirmed parents. A single transaction is permitted.\n"
    1377                 :             :                 "None of the parents may depend on each other. Parents that are already in mempool do not need to be present in the package.\n"
    1378                 :             :                 "The package must be topologically sorted, with the child being the last element in the array if there are multiple elements.",
    1379                 :             :                 {
    1380   [ +  -  +  - ]:         722 :                     {"rawtx", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
    1381                 :             :                 },
    1382                 :             :             },
    1383   [ +  -  +  -  :         722 :             {"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
             +  -  +  - ]
    1384         [ +  - ]:         722 :              "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
    1385                 :         361 :                  "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
    1386   [ +  -  +  -  :         722 :             {"maxburnamount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_BURN_AMOUNT)},
                   +  - ]
    1387         [ +  - ]:         722 :              "Reject transactions with provably unspendable outputs (e.g. 'datacarrier' outputs that use the OP_RETURN opcode) greater than the specified value, expressed in " + CURRENCY_UNIT + ".\n"
    1388                 :             :              "If burning funds through unspendable outputs is desired, increase this value.\n"
    1389                 :         361 :              "This check is based on heuristics and does not guarantee spendability of outputs.\n"
    1390                 :             :             },
    1391                 :             :         },
    1392         [ +  - ]:         722 :         RPCResult{
    1393   [ +  -  +  - ]:         722 :             RPCResult::Type::OBJ, "", "",
    1394                 :             :             {
    1395   [ +  -  +  - ]:         722 :                 {RPCResult::Type::STR, "package_msg", "The transaction package result message. \"success\" indicates all transactions were accepted into or are already in the mempool."},
    1396   [ +  -  +  - ]:         722 :                 {RPCResult::Type::OBJ_DYN, "tx-results", "The transaction results keyed by wtxid. An entry is returned for every submitted wtxid.",
    1397                 :             :                 {
    1398   [ +  -  +  - ]:         722 :                     {RPCResult::Type::OBJ, "wtxid", "transaction wtxid", {
    1399   [ +  -  +  - ]:         722 :                         {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
    1400   [ +  -  +  - ]:         722 :                         {RPCResult::Type::STR_HEX, "other-wtxid", /*optional=*/true, "The wtxid of a different transaction with the same txid but different witness found in the mempool. This means the submitted transaction was ignored."},
    1401   [ +  -  +  - ]:         722 :                         {RPCResult::Type::NUM, "vsize_adjusted", /*optional=*/true, "Maximum of sigop-adjusted size (-bytespersigop) and virtual transaction size as defined in BIP 141."},
    1402   [ +  -  +  - ]:         722 :                         {RPCResult::Type::NUM, "vsize", /*optional=*/true, "(DEPRECATED) Was previously erroneously described as the BIP 141 vsize, but is actually sigops-adjusted vsize.\n"
    1403                 :             :                                                                     "Use vsize_bip141 to actually get that behavior or switch to the explicit vsize_adjusted for retained behavior."},
    1404   [ +  -  +  - ]:         722 :                         {RPCResult::Type::NUM, "vsize_bip141", /*optional=*/true, "Virtual transaction size as defined in BIP 141."},
    1405   [ +  -  +  - ]:         722 :                         {RPCResult::Type::OBJ, "fees", /*optional=*/true, "Transaction fees", {
    1406   [ +  -  +  - ]:         722 :                             {RPCResult::Type::STR_AMOUNT, "base", "transaction fee in " + CURRENCY_UNIT},
    1407   [ +  -  +  - ]:         722 :                             {RPCResult::Type::STR_AMOUNT, "effective-feerate", /*optional=*/true, "if the transaction was not already in the mempool, the effective feerate in " + CURRENCY_UNIT + " per KvB. For example, the package feerate and/or feerate with modified fees from prioritisetransaction."},
    1408   [ +  -  +  - ]:         722 :                             {RPCResult::Type::ARR, "effective-includes", /*optional=*/true, "if effective-feerate is provided, the wtxids of the transactions whose fees and vsizes are included in effective-feerate.",
    1409   [ +  -  +  - ]:         722 :                                 {{RPCResult::Type::STR_HEX, "", "transaction wtxid in hex"},
    1410                 :             :                             }},
    1411                 :             :                         }},
    1412   [ +  -  +  - ]:         722 :                         {RPCResult::Type::STR, "error", /*optional=*/true, "Error string if rejected from mempool, or \"package-not-validated\" when the package aborts before any per-tx processing."},
    1413                 :             :                     }}
    1414                 :             :                 }},
    1415   [ +  -  +  - ]:         722 :                 {RPCResult::Type::ARR, "replaced-transactions", /*optional=*/true, "List of txids of replaced transactions",
    1416                 :             :                 {
    1417   [ +  -  +  - ]:         722 :                     {RPCResult::Type::STR_HEX, "", "The transaction id"},
    1418                 :             :                 }},
    1419                 :             :             },
    1420                 :       14079 :         },
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
    1421                 :         361 :         RPCExamples{
    1422   [ +  -  +  -  :         722 :             HelpExampleRpc("submitpackage", R"(["raw-parent-tx-1", "raw-parent-tx-2", "raw-child-tx"])") +
                   +  - ]
    1423   [ +  -  +  -  :        1083 :             HelpExampleCli("submitpackage", R"('["raw-tx-without-unconfirmed-parents"]')")
             +  -  +  - ]
    1424         [ +  - ]:         361 :         },
    1425                 :         361 :         [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
    1426                 :             :         {
    1427                 :         163 :             const UniValue raw_transactions = request.params[0].get_array();
    1428   [ -  +  +  +  :         163 :             if (raw_transactions.empty() || raw_transactions.size() > MAX_PACKAGE_COUNT) {
                   +  + ]
    1429                 :           5 :                 throw JSONRPCError(RPC_INVALID_PARAMETER,
    1430   [ +  -  +  -  :          15 :                                    "Array must contain between 1 and " + ToString(MAX_PACKAGE_COUNT) + " transactions.");
                   +  - ]
    1431                 :             :             }
    1432                 :             : 
    1433                 :             :             // Fee check needs to be run with chainstate and package context
    1434   [ +  -  +  + ]:         158 :             const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>("maxfeerate"))};
    1435         [ +  + ]:         155 :             std::optional<CFeeRate> client_maxfeerate{max_raw_tx_fee_rate};
    1436                 :             :             // 0-value is special; it's mapped to no sanity check
    1437         [ +  + ]:         155 :             if (max_raw_tx_fee_rate == CFeeRate(0)) {
    1438                 :           2 :                 client_maxfeerate = std::nullopt;
    1439                 :             :             }
    1440                 :             : 
    1441                 :             :             // Burn sanity check is run with no context
    1442   [ +  -  +  + ]:         155 :             const CAmount max_burn_amount{AmountFromValue(self.Arg<UniValue>("maxburnamount"))};
    1443                 :             : 
    1444                 :         153 :             std::vector<CTransactionRef> txns;
    1445   [ -  +  +  - ]:         153 :             txns.reserve(raw_transactions.size());
    1446   [ +  -  +  + ]:         334 :             for (const auto& rawtx : raw_transactions.getValues()) {
    1447         [ +  - ]:         227 :                 CMutableTransaction mtx;
    1448   [ +  +  +  -  :         227 :                 if (!DecodeHexTx(mtx, rawtx.get_str())) {
                   +  + ]
    1449                 :           6 :                     throw JSONRPCError(RPC_DESERIALIZATION_ERROR,
    1450   [ +  -  +  -  :          18 :                                        "TX decode failed: " + rawtx.get_str() + " Make sure the tx has at least one input.");
                   +  - ]
    1451                 :             :                 }
    1452                 :             : 
    1453         [ +  + ]:        1584 :                 for (const auto& out : mtx.vout) {
    1454   [ +  +  +  -  :        1403 :                     if((out.scriptPubKey.IsUnspendable() || !out.scriptPubKey.HasValidOps()) && out.nValue > max_burn_amount) {
             +  +  +  + ]
    1455   [ +  -  +  - ]:          26 :                         throw JSONRPCTransactionError(TransactionError::MAX_BURN_EXCEEDED);
    1456                 :             :                     }
    1457                 :             :                 }
    1458                 :             : 
    1459   [ +  -  +  - ]:         543 :                 txns.emplace_back(MakeTransactionRef(std::move(mtx)));
    1460                 :         227 :             }
    1461         [ +  - ]:         107 :             CHECK_NONFATAL(!txns.empty());
    1462   [ -  +  +  +  :         107 :             if (txns.size() > 1 && !IsChildWithParentsTree(txns)) {
             +  -  -  + ]
    1463   [ +  -  +  - ]:          54 :                 throw JSONRPCTransactionError(TransactionError::INVALID_PACKAGE, "package topology disallowed. not child-with-parents or parents depend on each other.");
    1464                 :             :             }
    1465                 :             : 
    1466         [ +  - ]:          80 :             NodeContext& node = EnsureAnyNodeContext(request.context);
    1467         [ +  - ]:          80 :             CTxMemPool& mempool = EnsureMemPool(node);
    1468   [ +  -  +  - ]:          80 :             Chainstate& chainstate = EnsureChainman(node).ActiveChainstate();
    1469   [ +  -  +  - ]:         240 :             const auto package_result = WITH_LOCK(::cs_main, return ProcessNewPackage(chainstate, mempool, txns, /*test_accept=*/ false, client_maxfeerate));
    1470                 :             : 
    1471         [ +  - ]:          80 :             std::string package_msg = "success";
    1472                 :             : 
    1473                 :             :             // First catch package-wide errors, continue if we can
    1474   [ -  -  +  - ]:          80 :             switch(package_result.m_state.GetResult()) {
    1475                 :           0 :                 case PackageValidationResult::PCKG_RESULT_UNSET:
    1476                 :           0 :                 {
    1477                 :             :                     // Belt-and-suspenders check; everything should be successful here
    1478   [ #  #  #  # ]:           0 :                     CHECK_NONFATAL(package_result.m_tx_results.size() == txns.size());
    1479         [ #  # ]:           0 :                     for (const auto& tx : txns) {
    1480   [ #  #  #  # ]:           0 :                         CHECK_NONFATAL(mempool.exists(tx->GetHash()));
    1481                 :             :                     }
    1482                 :             :                     break;
    1483                 :             :                 }
    1484                 :           0 :                 case PackageValidationResult::PCKG_MEMPOOL_ERROR:
    1485                 :           0 :                 {
    1486                 :             :                     // This only happens with internal bug; user should stop and report
    1487                 :           0 :                     throw JSONRPCTransactionError(TransactionError::MEMPOOL_ERROR,
    1488   [ #  #  #  # ]:           0 :                         package_result.m_state.GetRejectReason());
    1489                 :             :                 }
    1490                 :          80 :                 case PackageValidationResult::PCKG_POLICY:
    1491                 :          80 :                 case PackageValidationResult::PCKG_TX:
    1492                 :          80 :                 {
    1493                 :             :                     // Package-wide error we want to return, but we also want to return individual responses
    1494         [ +  - ]:          80 :                     package_msg = package_result.m_state.ToString();
    1495   [ -  +  +  +  :          82 :                     CHECK_NONFATAL(package_result.m_tx_results.size() == txns.size() ||
             +  -  +  - ]
    1496                 :             :                             package_result.m_tx_results.empty());
    1497                 :             :                     break;
    1498                 :             :                 }
    1499                 :             :             }
    1500                 :             : 
    1501                 :          80 :             size_t num_broadcast{0};
    1502         [ +  + ]:         160 :             for (const auto& tx : txns) {
    1503                 :             :                 // We don't want to re-submit the txn for validation in BroadcastTransaction
    1504   [ +  -  +  - ]:          80 :                 if (!mempool.exists(tx->GetHash())) {
    1505                 :          80 :                     continue;
    1506                 :             :                 }
    1507                 :             : 
    1508                 :             :                 // We do not expect an error here; we are only broadcasting things already/still in mempool
    1509         [ #  # ]:           0 :                 std::string err_string;
    1510   [ #  #  #  #  :           0 :                 const auto err = BroadcastTransaction(node, tx, err_string, /*max_tx_fee=*/0, /*max_tx_fee_rate=*/CFeeRate(0), /*broadcast_method=*/node::TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL, /*wait_callback=*/true);
                   #  # ]
    1511         [ #  # ]:           0 :                 if (err != TransactionError::OK) {
    1512                 :           0 :                     throw JSONRPCTransactionError(err,
    1513         [ #  # ]:           0 :                         strprintf("transaction broadcast failed: %s (%d transactions were broadcast successfully)",
    1514         [ #  # ]:           0 :                             err_string, num_broadcast));
    1515                 :             :                 }
    1516                 :           0 :                 num_broadcast++;
    1517                 :           0 :             }
    1518                 :             : 
    1519                 :          80 :             UniValue rpc_result{UniValue::VOBJ};
    1520   [ +  -  +  -  :         160 :             rpc_result.pushKV("package_msg", package_msg);
                   +  - ]
    1521                 :         160 :             UniValue tx_result_map{UniValue::VOBJ};
    1522                 :         160 :             std::set<Txid> replaced_txids;
    1523         [ +  + ]:         160 :             for (const auto& tx : txns) {
    1524                 :          80 :                 UniValue result_inner{UniValue::VOBJ};
    1525   [ +  -  +  -  :         160 :                 result_inner.pushKV("txid", tx->GetHash().GetHex());
             +  -  +  - ]
    1526         [ +  - ]:          80 :                 const auto wtxid_hex = tx->GetWitnessHash().GetHex();
    1527                 :          80 :                 auto it = package_result.m_tx_results.find(tx->GetWitnessHash());
    1528         [ +  + ]:          80 :                 if (it == package_result.m_tx_results.end()) {
    1529                 :             :                     // No per-tx result for this wtxid
    1530                 :             :                     // Current invariant: per-tx results are all-or-none (every member or empty on package abort).
    1531                 :             :                     // If any exist yet this one is missing, it's an unexpected partial map.
    1532         [ +  - ]:           2 :                     CHECK_NONFATAL(package_result.m_tx_results.empty());
    1533   [ +  -  +  -  :           4 :                     result_inner.pushKV("error", "package-not-validated");
                   +  - ]
    1534   [ -  +  +  - ]:           6 :                     tx_result_map.pushKV(wtxid_hex, std::move(result_inner));
    1535                 :           2 :                     continue;
    1536                 :             :                 }
    1537   [ -  +  -  - ]:          78 :                 const auto& tx_result = it->second;
    1538   [ -  +  -  - ]:          78 :                 switch(it->second.m_result_type) {
    1539                 :           0 :                 case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
    1540   [ #  #  #  #  :           0 :                     result_inner.pushKV("other-wtxid", it->second.m_other_wtxid.value().GetHex());
          #  #  #  #  #  
                      # ]
    1541                 :           0 :                     break;
    1542                 :          78 :                 case MempoolAcceptResult::ResultType::INVALID:
    1543   [ +  -  +  -  :         156 :                     result_inner.pushKV("error", it->second.m_state.ToString());
             +  -  +  - ]
    1544                 :          78 :                     break;
    1545                 :           0 :                 case MempoolAcceptResult::ResultType::VALID:
    1546                 :           0 :                 case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
    1547   [ #  #  #  #  :           0 :                     result_inner.pushKV("vsize_adjusted", it->second.m_vsize.value());
             #  #  #  # ]
    1548   [ #  #  #  #  :           0 :                     result_inner.pushKV("vsize", it->second.m_vsize.value());
             #  #  #  # ]
    1549   [ #  #  #  #  :           0 :                     result_inner.pushKV("vsize_bip141", GetVirtualTransactionSize(*tx));
             #  #  #  # ]
    1550                 :           0 :                     UniValue fees(UniValue::VOBJ);
    1551   [ #  #  #  #  :           0 :                     fees.pushKV("base", ValueFromAmount(it->second.m_base_fees.value()));
             #  #  #  # ]
    1552         [ #  # ]:           0 :                     if (tx_result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
    1553                 :             :                         // Effective feerate is not provided for MEMPOOL_ENTRY transactions even
    1554                 :             :                         // though modified fees is known, because it is unknown whether package
    1555                 :             :                         // feerate was used when it was originally submitted.
    1556   [ #  #  #  #  :           0 :                         fees.pushKV("effective-feerate", ValueFromAmount(tx_result.m_effective_feerate.value().GetFeePerK()));
          #  #  #  #  #  
                      # ]
    1557                 :           0 :                         UniValue effective_includes_res(UniValue::VARR);
    1558   [ #  #  #  # ]:           0 :                         for (const auto& wtxid : tx_result.m_wtxids_fee_calculations.value()) {
    1559   [ #  #  #  #  :           0 :                             effective_includes_res.push_back(wtxid.ToString());
                   #  # ]
    1560                 :             :                         }
    1561   [ #  #  #  # ]:           0 :                         fees.pushKV("effective-includes", std::move(effective_includes_res));
    1562                 :           0 :                     }
    1563   [ #  #  #  # ]:           0 :                     result_inner.pushKV("fees", std::move(fees));
    1564         [ #  # ]:           0 :                     for (const auto& ptx : it->second.m_replaced_transactions) {
    1565         [ #  # ]:           0 :                         replaced_txids.insert(ptx->GetHash());
    1566                 :             :                     }
    1567                 :           0 :                     break;
    1568                 :             :                 }
    1569   [ -  +  +  - ]:         234 :                 tx_result_map.pushKV(wtxid_hex, std::move(result_inner));
    1570                 :          80 :             }
    1571   [ +  -  +  - ]:         160 :             rpc_result.pushKV("tx-results", std::move(tx_result_map));
    1572                 :         160 :             UniValue replaced_list(UniValue::VARR);
    1573   [ -  -  -  -  :          80 :             for (const auto& txid : replaced_txids) replaced_list.push_back(txid.ToString());
             -  -  -  + ]
    1574   [ +  -  +  - ]:         160 :             rpc_result.pushKV("replaced-transactions", std::move(replaced_list));
    1575                 :         160 :             return rpc_result;
    1576                 :         236 :         },
    1577                 :        3610 :     };
           [ +  -  +  -  
          +  -  +  +  +  
             +  -  -  -  
                      - ]
    1578                 :       14440 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
             -  -  -  - ]
    1579                 :             : 
    1580                 :          32 : void RegisterMempoolRPCCommands(CRPCTable& t)
    1581                 :             : {
    1582                 :          32 :     static const CRPCCommand commands[]{
    1583         [ +  - ]:          60 :         {"rawtransactions", &sendrawtransaction},
    1584         [ +  - ]:          60 :         {"rawtransactions", &getprivatebroadcastinfo},
    1585         [ +  - ]:          60 :         {"rawtransactions", &abortprivatebroadcast},
    1586         [ +  - ]:          60 :         {"rawtransactions", &testmempoolaccept},
    1587         [ +  - ]:          60 :         {"blockchain", &getmempoolancestors},
    1588         [ +  - ]:          60 :         {"blockchain", &getmempooldescendants},
    1589         [ +  - ]:          60 :         {"blockchain", &getmempoolentry},
    1590         [ +  - ]:          60 :         {"blockchain", &getmempoolcluster},
    1591         [ +  - ]:          60 :         {"blockchain", &gettxspendingprevout},
    1592         [ +  - ]:          60 :         {"blockchain", &getmempoolinfo},
    1593         [ +  - ]:          60 :         {"hidden", &getmempoolfeeratediagram},
    1594         [ +  - ]:          60 :         {"blockchain", &getrawmempool},
    1595         [ +  - ]:          60 :         {"blockchain", &importmempool},
    1596         [ +  - ]:          60 :         {"blockchain", &savemempool},
    1597         [ +  - ]:          60 :         {"hidden", &getorphantxs},
    1598         [ +  - ]:          60 :         {"rawtransactions", &submitpackage},
    1599                 :         512 :     };
           [ +  +  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  - ]
    1600         [ +  + ]:         544 :     for (const auto& c : commands) {
    1601                 :         512 :         t.appendCommand(c.name, &c);
    1602                 :             :     }
    1603                 :          32 : }
        

Generated by: LCOV version 2.5.0-full