Coverage Report

Created: 2026-06-16 16:41

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