Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <rpc/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 : 25313 : static RPCMethod sendrawtransaction()
48 : : {
49 : 25313 : return RPCMethod{
50 : 25313 : "sendrawtransaction",
51 [ + - ]: 50626 : "Submit a raw transaction (serialized, hex-encoded) to the network.\n"
52 : :
53 : : "\nIf -privatebroadcast is disabled, then the transaction will be put into the\n"
54 : : "local mempool of the node and will be sent unconditionally to all currently\n"
55 : : "connected peers, so using sendrawtransaction for manual rebroadcast will degrade\n"
56 : : "privacy by leaking the transaction's origin, as nodes will normally not\n"
57 : : "rebroadcast non-wallet transactions already in their mempool.\n"
58 : :
59 : : "\nIf -privatebroadcast is enabled, then the transaction will be sent only via\n"
60 : : "dedicated, short-lived connections to Tor or I2P peers or IPv4/IPv6 peers\n"
61 : : "via the Tor network. This conceals the transaction's origin. The transaction\n"
62 : : "will only enter the local mempool when it is received back from the network.\n"
63 : :
64 : : "\nA specific exception, RPC_TRANSACTION_ALREADY_IN_UTXO_SET, may throw if the transaction cannot be added to the mempool.\n"
65 : :
66 : : "\nRelated RPCs: createrawtransaction, signrawtransactionwithkey\n",
67 : : {
68 [ + - + - ]: 50626 : {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex string of the raw transaction"},
69 [ + - + - : 50626 : {"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
+ - ]
70 [ + - ]: 50626 : "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
71 : 25313 : "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
72 [ + - + - : 50626 : {"maxburnamount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_BURN_AMOUNT)},
+ - ]
73 [ + - ]: 50626 : "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 : : "If burning funds through unspendable outputs is desired, increase this value.\n"
75 : 25313 : "This check is based on heuristics and does not guarantee spendability of outputs.\n"},
76 : : },
77 [ + - ]: 50626 : RPCResult{
78 [ + - + - ]: 50626 : RPCResult::Type::STR_HEX, "", "The transaction hash in hex"
79 [ + - ]: 50626 : },
80 : 25313 : RPCExamples{
81 : : "\nCreate a transaction\n"
82 [ + - + - : 50626 : + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
+ - + - ]
83 : 25313 : "Sign the transaction, and get back the hex\n"
84 [ + - + - : 101252 : + HelpExampleCli("signrawtransactionwithwallet", "\"myhex\"") +
+ - + - ]
85 : 25313 : "\nSend the transaction (signed hex)\n"
86 [ + - + - : 101252 : + HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
+ - + - ]
87 : 25313 : "\nAs a JSON-RPC call\n"
88 [ + - + - : 101252 : + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
+ - + - ]
89 [ + - ]: 25313 : },
90 : 25313 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
91 : : {
92 [ + + ]: 22881 : const CAmount max_burn_amount = request.params[2].isNull() ? 0 : AmountFromValue(request.params[2]);
93 : :
94 : 22881 : CMutableTransaction mtx;
95 [ + - + - : 22881 : if (!DecodeHexTx(mtx, request.params[0].get_str())) {
+ - + + ]
96 [ + - + - ]: 4 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
97 : : }
98 : :
99 [ + + ]: 80683 : for (const auto& out : mtx.vout) {
100 [ + + + - : 57808 : if((out.scriptPubKey.IsUnspendable() || !out.scriptPubKey.HasValidOps()) && out.nValue > max_burn_amount) {
+ + + + ]
101 [ + - + - ]: 8 : throw JSONRPCTransactionError(TransactionError::MAX_BURN_EXCEEDED);
102 : : }
103 : : }
104 : :
105 [ + - ]: 22875 : CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
106 : :
107 [ + - + - ]: 22875 : const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>("maxfeerate"))};
108 : :
109 [ + - ]: 22875 : int64_t virtual_size = GetVirtualTransactionSize(*tx);
110 [ + - ]: 22875 : CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
111 : :
112 [ + - ]: 22875 : std::string err_string;
113 : 22875 : AssertLockNotHeld(cs_main);
114 [ + - ]: 22875 : NodeContext& node = EnsureAnyNodeContext(request.context);
115 [ + - + - ]: 22875 : const bool private_broadcast_enabled{gArgs.GetBoolArg("-privatebroadcast", DEFAULT_PRIVATE_BROADCAST)};
116 [ + + ]: 12 : if (private_broadcast_enabled &&
117 [ + + + - : 22876 : !g_reachable_nets.Contains(NET_ONION) &&
- + ]
118 [ + - ]: 1 : !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 : : "reachable. Maybe the location of the Tor proxy couldn't be retrieved "
122 : : "from the Tor daemon at startup. Check whether the Tor daemon is running "
123 [ + - ]: 3 : "and that -torcontrol, -torpassword and -i2psam are configured properly.");
124 : : }
125 [ + + ]: 22874 : const auto method = private_broadcast_enabled ? node::TxBroadcast::NO_MEMPOOL_PRIVATE_BROADCAST
126 : : : node::TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL;
127 [ + - + - : 45748 : const TransactionError err = BroadcastTransaction(node,
+ - ]
128 : : tx,
129 : : err_string,
130 : : max_raw_tx_fee,
131 : : method,
132 : : /*wait_callback=*/true);
133 [ + + ]: 22874 : if (TransactionError::OK != err) {
134 [ + - ]: 4301 : throw JSONRPCTransactionError(err, err_string);
135 : : }
136 : :
137 [ + - + - ]: 37146 : return tx->GetHash().GetHex();
138 [ + - ]: 64323 : },
139 [ + - + - : 177191 : };
+ + - - ]
140 [ + - + - : 151878 : }
+ - - - ]
141 : :
142 : 2439 : static RPCMethod getprivatebroadcastinfo()
143 : : {
144 : 2439 : return RPCMethod{
145 : 2439 : "getprivatebroadcastinfo",
146 [ + - ]: 4878 : "Returns information about transactions that are currently being privately broadcast.\n"
147 : : "This method is only available when running with -privatebroadcast enabled.\n",
148 : : {},
149 [ + - ]: 4878 : RPCResult{
150 [ + - ]: 4878 : RPCResult::Type::OBJ, "", "",
151 : : {
152 [ + - + - ]: 4878 : {RPCResult::Type::ARR, "transactions", "",
153 : : {
154 [ + - + - ]: 4878 : {RPCResult::Type::OBJ, "", "",
155 : : {
156 [ + - + - ]: 4878 : {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
157 [ + - + - ]: 4878 : {RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
158 [ + - + - ]: 4878 : {RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded transaction data"},
159 [ + - + - ]: 4878 : {RPCResult::Type::NUM_TIME, "time_added", "The time this transaction was added to the private broadcast queue (seconds since epoch)"},
160 [ + - + - ]: 4878 : {RPCResult::Type::ARR, "peers", "Per-peer send and acknowledgment information for this transaction",
161 : : {
162 [ + - + - ]: 4878 : {RPCResult::Type::OBJ, "", "",
163 : : {
164 [ + - + - ]: 4878 : {RPCResult::Type::STR, "address", "The address of the peer to which the transaction was sent"},
165 [ + - + - ]: 4878 : {RPCResult::Type::NUM_TIME, "sent", "The time this transaction was picked for sending to this peer via private broadcast (seconds since epoch)"},
166 [ + - + - ]: 4878 : {RPCResult::Type::NUM_TIME, "received", /*optional=*/true, "The time this peer acknowledged reception of the transaction (seconds since epoch)"},
167 : : }},
168 : : }},
169 : : }},
170 : : }},
171 [ + - + - : 68292 : }},
+ - + - +
- + - + +
+ + + + +
+ + + - -
- - - - -
- - - ]
172 : 2439 : RPCExamples{
173 [ + - + - : 4878 : HelpExampleCli("getprivatebroadcastinfo", "")
+ - ]
174 [ + - + - : 9756 : + HelpExampleRpc("getprivatebroadcastinfo", "")
+ - + - ]
175 [ + - ]: 2439 : },
176 : 2439 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
177 : : {
178 : 8 : const NodeContext& node{EnsureAnyNodeContext(request.context)};
179 : 8 : const PeerManager& peerman{EnsurePeerman(node)};
180 [ + + ]: 8 : if (!peerman.GetInfo().private_broadcast) {
181 [ + - + - ]: 2 : throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Private broadcast is not enabled. Ensure you're running Bitcoin Core with -privatebroadcast=1.");
182 : : }
183 : :
184 : 7 : const auto txs{peerman.GetPrivateBroadcastInfo()};
185 : :
186 : 7 : UniValue transactions(UniValue::VARR);
187 [ + + ]: 19 : for (const auto& tx_info : txs) {
188 : 12 : UniValue o(UniValue::VOBJ);
189 [ + - + - : 24 : o.pushKV("txid", tx_info.tx->GetHash().ToString());
+ - + - ]
190 [ + - + - : 24 : o.pushKV("wtxid", tx_info.tx->GetWitnessHash().ToString());
+ - + - ]
191 [ + - + - : 24 : o.pushKV("hex", EncodeHexTx(*tx_info.tx));
+ - + - ]
192 [ + - + - : 24 : o.pushKV("time_added", TicksSinceEpoch<std::chrono::seconds>(tx_info.time_added));
+ - ]
193 : 12 : UniValue peers(UniValue::VARR);
194 [ + + ]: 36 : for (const auto& peer : tx_info.peers) {
195 : 24 : UniValue p(UniValue::VOBJ);
196 [ + - + - : 48 : p.pushKV("address", peer.address.ToStringAddrPort());
+ - + - ]
197 [ + - + - : 48 : p.pushKV("sent", TicksSinceEpoch<std::chrono::seconds>(peer.sent));
+ - ]
198 [ + - ]: 24 : if (peer.received.has_value()) {
199 [ + - + - : 48 : p.pushKV("received", TicksSinceEpoch<std::chrono::seconds>(*peer.received));
+ - ]
200 : : }
201 [ + - ]: 24 : peers.push_back(std::move(p));
202 : 24 : }
203 [ + - + - ]: 24 : o.pushKV("peers", std::move(peers));
204 [ + - ]: 12 : transactions.push_back(std::move(o));
205 : 12 : }
206 : :
207 : 7 : UniValue ret(UniValue::VOBJ);
208 [ + - + - ]: 14 : ret.pushKV("transactions", std::move(transactions));
209 : 7 : return ret;
210 : 7 : },
211 [ + - + - ]: 9756 : };
212 [ + - + - : 53658 : }
+ - + - +
- + - + -
+ - + - +
- + - - -
- - ]
213 : :
214 : 2434 : static RPCMethod abortprivatebroadcast()
215 : : {
216 : 2434 : return RPCMethod{
217 : 2434 : "abortprivatebroadcast",
218 [ + - ]: 4868 : "Abort private broadcast attempts for a transaction currently being privately broadcast.\n"
219 : : "The transaction will be removed from the private broadcast queue.\n"
220 : : "This method is only available when running with -privatebroadcast enabled.\n",
221 : : {
222 [ + - + - ]: 4868 : {"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 : : "If the provided id matches a txid that corresponds to multiple transactions with different wtxids, multiple transactions will be removed and returned."},
224 : : },
225 [ + - ]: 4868 : RPCResult{
226 [ + - + - ]: 4868 : RPCResult::Type::OBJ, "", "",
227 : : {
228 [ + - + - ]: 4868 : {RPCResult::Type::ARR, "removed_transactions", "Transactions removed from the private broadcast queue",
229 : : {
230 [ + - + - ]: 4868 : {RPCResult::Type::OBJ, "", "",
231 : : {
232 [ + - + - ]: 4868 : {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
233 [ + - + - ]: 4868 : {RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
234 [ + - + - ]: 4868 : {RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded transaction data"},
235 : : }},
236 : : }},
237 : : }
238 [ + - + - : 34076 : },
+ - + - +
+ + + + +
- - - - -
- ]
239 : 2434 : RPCExamples{
240 [ + - + - : 4868 : HelpExampleCli("abortprivatebroadcast", "\"id\"")
+ - ]
241 [ + - + - : 9736 : + HelpExampleRpc("abortprivatebroadcast", "\"id\"")
+ - + - ]
242 [ + - ]: 2434 : },
243 : 2434 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
244 : : {
245 : :
246 : 3 : const NodeContext& node{EnsureAnyNodeContext(request.context)};
247 : 3 : PeerManager& peerman{EnsurePeerman(node)};
248 [ + + ]: 3 : if (!peerman.GetInfo().private_broadcast) {
249 [ + - + - ]: 2 : throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Private broadcast is not enabled. Ensure you're running Bitcoin Core with -privatebroadcast=1.");
250 : : }
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 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in private broadcast queue. Check getprivatebroadcastinfo.");
257 : : }
258 : :
259 : 1 : UniValue removed_transactions(UniValue::VARR);
260 [ + + ]: 2 : for (const auto& tx : removed_txs) {
261 : 1 : UniValue o(UniValue::VOBJ);
262 [ + - + - : 2 : o.pushKV("txid", tx->GetHash().ToString());
+ - + - ]
263 [ + - + - : 2 : o.pushKV("wtxid", tx->GetWitnessHash().ToString());
+ - + - ]
264 [ + - + - : 2 : o.pushKV("hex", EncodeHexTx(*tx));
+ - + - ]
265 [ + - ]: 1 : removed_transactions.push_back(std::move(o));
266 : 1 : }
267 : 1 : UniValue ret(UniValue::VOBJ);
268 [ + - + - ]: 2 : ret.pushKV("removed_transactions", std::move(removed_transactions));
269 : 1 : return ret;
270 : 2 : },
271 [ + - + - : 12170 : };
+ + - - ]
272 [ + - + - : 29208 : }
+ - + - +
- + - -
- ]
273 : :
274 : 3856 : static RPCMethod testmempoolaccept()
275 : : {
276 : 3856 : return RPCMethod{
277 : 3856 : "testmempoolaccept",
278 : : "Returns result of mempool acceptance tests indicating if raw transaction(s) (serialized, hex-encoded) would be accepted by mempool.\n"
279 : : "\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 : : "\nIf one transaction fails, other transactions may not be fully validated (the 'allowed' key will be blank).\n"
281 [ + - + - ]: 7712 : "\nThe maximum number of transactions allowed is " + ToString(MAX_PACKAGE_COUNT) + ".\n"
282 : : "\nThis checks if transactions violate the consensus or policy rules.\n"
283 : 3856 : "\nSee sendrawtransaction call.\n",
284 : : {
285 [ + - + - ]: 7712 : {"rawtxs", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of hex strings of raw transactions.",
286 : : {
287 [ + - + - ]: 7712 : {"rawtx", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
288 : : },
289 : : },
290 [ + - + - : 7712 : {"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
+ - ]
291 [ + - ]: 7712 : "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
292 : 3856 : "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
293 : : },
294 [ + - ]: 7712 : RPCResult{
295 [ + - + - ]: 7712 : RPCResult::Type::ARR, "", "The result of the mempool acceptance test for each raw transaction in the input array.\n"
296 : : "Returns results for each transaction in the same order they were passed in.\n"
297 : : "Transactions that cannot be fully validated due to failures in other transactions will not contain an 'allowed' result.\n",
298 : : {
299 [ + - + - ]: 7712 : {RPCResult::Type::OBJ, "", "",
300 : : {
301 [ + - + - ]: 7712 : {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
302 [ + - + - ]: 7712 : {RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
303 [ + - + - ]: 7712 : {RPCResult::Type::STR, "package-error", /*optional=*/true, "Package validation error, if any (only possible if rawtxs had more than 1 transaction)."},
304 [ + - + - ]: 7712 : {RPCResult::Type::BOOL, "allowed", /*optional=*/true, "Whether this tx would be accepted to the mempool and pass client-specified maxfeerate. "
305 : : "If not present, the tx was not fully validated due to a failure in another tx in the list."},
306 [ + - + - ]: 7712 : {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 [ + - + - ]: 7712 : {RPCResult::Type::OBJ, "fees", /*optional=*/true, "Transaction fees (only present if 'allowed' is true)",
308 : : {
309 [ + - + - ]: 7712 : {RPCResult::Type::STR_AMOUNT, "base", "transaction fee in " + CURRENCY_UNIT},
310 [ + - + - ]: 7712 : {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 [ + - + - ]: 7712 : {RPCResult::Type::ARR, "effective-includes", /*optional=*/false, "transactions whose fees and vsizes are included in effective-feerate.",
312 [ + - + - ]: 7712 : {RPCResult{RPCResult::Type::STR_HEX, "", "transaction wtxid in hex"},
313 : : }},
314 : : }},
315 [ + - + - ]: 7712 : {RPCResult::Type::STR, "reject-reason", /*optional=*/true, "Rejection reason (only present when 'allowed' is false)"},
316 [ + - + - ]: 7712 : {RPCResult::Type::STR, "reject-details", /*optional=*/true, "Rejection details (only present when 'allowed' is false and rejection details exist)"},
317 : : }},
318 : : }
319 [ + - + - : 115680 : },
+ - + - +
- + + + +
+ + + + -
- - - - -
- - ]
320 : 3856 : RPCExamples{
321 : : "\nCreate a transaction\n"
322 [ + - + - : 7712 : + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
+ - + - ]
323 : 3856 : "Sign the transaction, and get back the hex\n"
324 [ + - + - : 15424 : + HelpExampleCli("signrawtransactionwithwallet", "\"myhex\"") +
+ - + - ]
325 : 3856 : "\nTest acceptance of the transaction (signed hex)\n"
326 [ + - + - : 15424 : + HelpExampleCli("testmempoolaccept", R"('["signedhex"]')") +
+ - + - ]
327 : 3856 : "\nAs a JSON-RPC call\n"
328 [ + - + - : 15424 : + HelpExampleRpc("testmempoolaccept", "[\"signedhex\"]")
+ - + - ]
329 [ + - ]: 3856 : },
330 : 3856 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
331 : : {
332 : 1424 : const UniValue raw_transactions = request.params[0].get_array();
333 [ - + + + : 1424 : if (raw_transactions.size() < 1 || raw_transactions.size() > MAX_PACKAGE_COUNT) {
+ + ]
334 : 2 : throw JSONRPCError(RPC_INVALID_PARAMETER,
335 [ + - + - : 6 : "Array must contain between 1 and " + ToString(MAX_PACKAGE_COUNT) + " transactions.");
+ - ]
336 : : }
337 : :
338 [ + - + + ]: 1422 : const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>("maxfeerate"))};
339 : :
340 : 1420 : std::vector<CTransactionRef> txns;
341 [ - + + - ]: 1420 : txns.reserve(raw_transactions.size());
342 [ + - + + ]: 3424 : for (const auto& rawtx : raw_transactions.getValues()) {
343 [ + - ]: 2005 : CMutableTransaction mtx;
344 [ + - + - : 2005 : if (!DecodeHexTx(mtx, rawtx.get_str())) {
+ + ]
345 : 1 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR,
346 [ + - + - : 3 : "TX decode failed: " + rawtx.get_str() + " Make sure the tx has at least one input.");
+ - ]
347 : : }
348 [ + - + - ]: 6012 : txns.emplace_back(MakeTransactionRef(std::move(mtx)));
349 : 2005 : }
350 : :
351 [ + - ]: 1419 : NodeContext& node = EnsureAnyNodeContext(request.context);
352 [ + - ]: 1419 : CTxMemPool& mempool = EnsureMemPool(node);
353 [ + - ]: 1419 : ChainstateManager& chainman = EnsureChainman(node);
354 [ + - ]: 1419 : Chainstate& chainstate = chainman.ActiveChainstate();
355 : 2838 : const PackageMempoolAcceptResult package_result = [&] {
356 : 1419 : LOCK(::cs_main);
357 [ - + + + : 1419 : if (txns.size() > 1) return ProcessNewPackage(chainstate, mempool, txns, /*test_accept=*/true, /*client_maxfeerate=*/{});
+ - ]
358 [ + - ]: 1343 : return PackageMempoolAcceptResult(txns[0]->GetWitnessHash(),
359 [ + - + - ]: 1343 : chainman.ProcessTransaction(txns[0], /*test_accept=*/true));
360 [ + - ]: 2838 : }();
361 : :
362 : 1419 : 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 : 1419 : bool exit_early{false};
368 [ + + ]: 3423 : for (const auto& tx : txns) {
369 : 2004 : UniValue result_inner(UniValue::VOBJ);
370 [ + - + - : 4008 : result_inner.pushKV("txid", tx->GetHash().GetHex());
+ - + - ]
371 [ + - + - : 4008 : result_inner.pushKV("wtxid", tx->GetWitnessHash().GetHex());
+ - + - ]
372 [ + + ]: 2004 : if (package_result.m_state.GetResult() == PackageValidationResult::PCKG_POLICY) {
373 [ + - + - : 198 : result_inner.pushKV("package-error", package_result.m_state.ToString());
+ - + - ]
374 : : }
375 : 2004 : auto it = package_result.m_tx_results.find(tx->GetWitnessHash());
376 [ + + + + ]: 2004 : if (exit_early || it == package_result.m_tx_results.end()) {
377 : : // Validation unfinished. Just return the txid and wtxid.
378 [ + - ]: 139 : rpc_result.push_back(std::move(result_inner));
379 : 139 : continue;
380 : : }
381 [ + - ]: 1865 : const auto& tx_result = it->second;
382 : : // Package testmempoolaccept doesn't allow transactions to already be in the mempool.
383 [ + - ]: 1865 : CHECK_NONFATAL(tx_result.m_result_type != MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
384 [ + + ]: 1865 : if (tx_result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
385 [ + - ]: 1616 : const CAmount fee = tx_result.m_base_fees.value();
386 : : // Check that fee does not exceed maximum fee
387 [ + - ]: 1616 : const int64_t virtual_size = tx_result.m_vsize.value();
388 [ + - ]: 1616 : const CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
389 [ + + ]: 1616 : if (max_raw_tx_fee && fee > max_raw_tx_fee) {
390 [ + - + - : 8 : result_inner.pushKV("allowed", false);
+ - ]
391 [ + - + - : 4 : result_inner.pushKV("reject-reason", "max-fee-exceeded");
+ - ]
392 : 4 : exit_early = true;
393 : : } else {
394 : : // Only return the fee and vsize if the transaction would pass ATMP.
395 : : // These can be used to calculate the feerate.
396 [ + - + - : 3224 : result_inner.pushKV("allowed", true);
+ - ]
397 [ + - + - : 3224 : result_inner.pushKV("vsize", virtual_size);
+ - ]
398 : 1612 : UniValue fees(UniValue::VOBJ);
399 [ + - + - : 3224 : fees.pushKV("base", ValueFromAmount(fee));
+ - ]
400 [ + - + - : 3224 : fees.pushKV("effective-feerate", ValueFromAmount(tx_result.m_effective_feerate.value().GetFeePerK()));
+ - + - ]
401 : 1612 : UniValue effective_includes_res(UniValue::VARR);
402 [ + - + + ]: 3224 : for (const auto& wtxid : tx_result.m_wtxids_fee_calculations.value()) {
403 [ + - + - : 1612 : effective_includes_res.push_back(wtxid.ToString());
+ - ]
404 : : }
405 [ + - + - ]: 3224 : fees.pushKV("effective-includes", std::move(effective_includes_res));
406 [ + - + - ]: 3224 : result_inner.pushKV("fees", std::move(fees));
407 : 1612 : }
408 : : } else {
409 [ + - + - : 498 : result_inner.pushKV("allowed", false);
+ - ]
410 [ + - ]: 249 : const TxValidationState state = tx_result.m_state;
411 [ + + ]: 249 : if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) {
412 [ + - + - : 232 : result_inner.pushKV("reject-reason", "missing-inputs");
+ - ]
413 : : } else {
414 [ - + + - : 399 : result_inner.pushKV("reject-reason", state.GetRejectReason());
+ - + - ]
415 [ + - + - : 266 : result_inner.pushKV("reject-details", state.ToString());
+ - + - ]
416 : : }
417 : 249 : }
418 [ + - ]: 1865 : rpc_result.push_back(std::move(result_inner));
419 : 2004 : }
420 : 1419 : return rpc_result;
421 : 1425 : },
422 [ + - + - : 34704 : };
+ - + + +
+ - - -
- ]
423 [ + - + - : 123392 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - - - ]
424 : :
425 : 3576 : static std::vector<RPCResult> ClusterDescription()
426 : : {
427 : 3576 : return {
428 [ + - + - ]: 7152 : RPCResult{RPCResult::Type::NUM, "clusterweight", "total sigops-adjusted weight (as defined in BIP 141 and modified by '-bytespersigop')"},
429 [ + - + - ]: 7152 : RPCResult{RPCResult::Type::NUM, "txcount", "number of transactions"},
430 [ + - + - ]: 7152 : RPCResult{RPCResult::Type::ARR, "chunks", "chunks in this cluster (in mining order)",
431 [ + - + - ]: 7152 : {RPCResult{RPCResult::Type::OBJ, "chunk", "",
432 : : {
433 [ + - + - ]: 7152 : RPCResult{RPCResult::Type::NUM, "chunkfee", "fees of the transactions in this chunk"},
434 [ + - + - ]: 7152 : RPCResult{RPCResult::Type::NUM, "chunkweight", "sigops-adjusted weight of all transactions in this chunk"},
435 [ + - + - ]: 7152 : RPCResult{RPCResult::Type::ARR, "txs", "transactions in this chunk in mining order",
436 [ + - + - : 17880 : {RPCResult{RPCResult::Type::STR_HEX, "txid", "transaction id"}}},
+ - + + -
- ]
437 : : }
438 [ + - + + : 17880 : }}
- - ]
439 [ + - + + : 10728 : }
- - ]
440 [ + - + + : 21456 : };
- - ]
441 [ + - + - : 57216 : }
+ - + - +
- + - + -
+ - - - -
- ]
442 : :
443 : 28237 : static std::vector<RPCResult> MempoolEntryDescription()
444 : : {
445 : 28237 : std::vector<RPCResult> list = {
446 [ + - + - ]: 56474 : 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 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM, "weight", "transaction weight as defined in BIP 141."},
448 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM_TIME, "time", "local time transaction entered pool in seconds since 1 Jan 1970 GMT"},
449 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM, "height", "block height when transaction entered pool"},
450 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM, "descendantcount", "number of in-mempool descendant transactions (including this one)"},
451 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM, "descendantsize", "virtual transaction size of in-mempool descendants (including this one)"},
452 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM, "ancestorcount", "number of in-mempool ancestor transactions (including this one)"},
453 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM, "ancestorsize", "virtual transaction size of in-mempool ancestors (including this one)"},
454 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::NUM, "chunkweight", "sigops-adjusted weight (as defined in BIP 141 and modified by '-bytespersigop') of this transaction's chunk"},
455 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::STR_HEX, "wtxid", "hash of serialized transaction, including witness data"},
456 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::OBJ, "fees", "",
457 : : {
458 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::STR_AMOUNT, "base", "transaction fee, denominated in " + CURRENCY_UNIT},
459 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::STR_AMOUNT, "modified", "transaction fee with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
460 [ + - + - ]: 56474 : 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 [ + - + - ]: 56474 : 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 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::STR_AMOUNT, "chunk", "transaction fees of chunk, denominated in " + CURRENCY_UNIT},
463 [ + - + + : 197659 : }},
- - ]
464 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::ARR, "depends", "unconfirmed transactions used as inputs for this transaction",
465 [ + - + - : 141185 : {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "parent transaction id"}}},
+ - + + -
- ]
466 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::ARR, "spentby", "unconfirmed transactions spending outputs from this transaction",
467 [ + - + - : 141185 : {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "child transaction id"}}},
+ - + + -
- ]
468 [ + - + - ]: 56474 : RPCResult{RPCResult::Type::BOOL, "unbroadcast", "Whether this transaction is currently unbroadcast (initial broadcast not yet acknowledged by any peers)"},
469 [ + - + + : 451792 : };
- - ]
470 [ + - + - : 28237 : 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 : : }
473 : 28237 : return list;
474 [ + - + - : 1185954 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - ]
475 : :
476 : 25890 : void AppendChunkInfo(UniValue& all_chunks, FeePerWeight chunk_feerate, std::vector<const CTxMemPoolEntry *> chunk_txs)
477 : : {
478 : 25890 : UniValue chunk(UniValue::VOBJ);
479 [ + - + - : 51780 : chunk.pushKV("chunkfee", ValueFromAmount(chunk_feerate.fee));
+ - ]
480 [ + - + - : 51780 : chunk.pushKV("chunkweight", chunk_feerate.size);
+ - ]
481 : 25890 : UniValue chunk_txids(UniValue::VARR);
482 [ + + ]: 51792 : for (const auto& chunk_tx : chunk_txs) {
483 [ + - + - : 25902 : chunk_txids.push_back(chunk_tx->GetTx().GetHash().ToString());
+ - ]
484 : : }
485 [ + - + - ]: 51780 : chunk.pushKV("txs", std::move(chunk_txids));
486 [ + - ]: 25890 : all_chunks.push_back(std::move(chunk));
487 : 25890 : }
488 : :
489 : 1144 : static void clusterToJSON(const CTxMemPool& pool, UniValue& info, std::vector<const CTxMemPoolEntry *> cluster) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
490 : : {
491 : 1144 : AssertLockHeld(pool.cs);
492 : 1144 : int total_weight{0};
493 [ + + ]: 27046 : for (const auto& tx : cluster) {
494 : 25902 : total_weight += tx->GetAdjustedWeight();
495 : : }
496 [ + - + - ]: 2288 : info.pushKV("clusterweight", total_weight);
497 [ - + + - : 2288 : 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 : 1144 : FeePerWeight current_chunk_feerate = pool.GetMainChunkFeerate(*cluster[0]);
503 : 1144 : std::vector<const CTxMemPoolEntry *> current_chunk;
504 [ - + + - ]: 1144 : current_chunk.reserve(cluster.size());
505 : :
506 : 1144 : UniValue all_chunks(UniValue::VARR);
507 [ + + ]: 27046 : for (const auto& tx : cluster) {
508 [ + + ]: 25902 : 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 [ + - + - ]: 24746 : AppendChunkInfo(all_chunks, pool.GetMainChunkFeerate(*current_chunk[0]), current_chunk);
512 [ + - ]: 24746 : current_chunk.clear();
513 : 24746 : current_chunk_feerate = pool.GetMainChunkFeerate(*tx);
514 : : }
515 [ + - ]: 25902 : current_chunk.push_back(tx);
516 [ + - ]: 25902 : current_chunk_feerate.size -= tx->GetAdjustedWeight();
517 : : }
518 [ + - + - ]: 1144 : AppendChunkInfo(all_chunks, pool.GetMainChunkFeerate(*current_chunk[0]), current_chunk);
519 [ + - ]: 1144 : current_chunk.clear();
520 [ + - + - ]: 2288 : info.pushKV("chunks", std::move(all_chunks));
521 : 1144 : }
522 : :
523 : 8530 : static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
524 : : {
525 : 8530 : AssertLockHeld(pool.cs);
526 : :
527 : 8530 : auto [ancestor_count, ancestor_size, ancestor_fees] = pool.CalculateAncestorData(e);
528 : 8530 : auto [descendant_count, descendant_size, descendant_fees] = pool.CalculateDescendantData(e);
529 : :
530 [ + - + - ]: 17060 : info.pushKV("vsize", e.GetTxSize());
531 [ + - + - ]: 17060 : info.pushKV("weight", e.GetTxWeight());
532 [ + - + - ]: 17060 : info.pushKV("time", count_seconds(e.GetTime()));
533 [ + - + - ]: 17060 : info.pushKV("height", e.GetHeight());
534 [ + - + - ]: 17060 : info.pushKV("descendantcount", descendant_count);
535 [ + - + - ]: 17060 : info.pushKV("descendantsize", descendant_size);
536 [ + - + - ]: 17060 : info.pushKV("ancestorcount", ancestor_count);
537 [ + - + - ]: 17060 : info.pushKV("ancestorsize", ancestor_size);
538 [ + - + - : 17060 : info.pushKV("wtxid", e.GetTx().GetWitnessHash().ToString());
+ - ]
539 : 8530 : auto feerate = pool.GetMainChunkFeerate(e);
540 [ + - + - ]: 17060 : info.pushKV("chunkweight", feerate.size);
541 : :
542 : 8530 : UniValue fees(UniValue::VOBJ);
543 [ + - + - : 17060 : fees.pushKV("base", ValueFromAmount(e.GetFee()));
+ - ]
544 [ + - + - : 17060 : fees.pushKV("modified", ValueFromAmount(e.GetModifiedFee()));
+ - ]
545 [ + - + - : 17060 : fees.pushKV("ancestor", ValueFromAmount(ancestor_fees));
+ - ]
546 [ + - + - : 17060 : fees.pushKV("descendant", ValueFromAmount(descendant_fees));
+ - ]
547 [ + - + - : 17060 : fees.pushKV("chunk", ValueFromAmount(feerate.fee));
+ - ]
548 [ + - + - ]: 17060 : info.pushKV("fees", std::move(fees));
549 : :
550 : 8530 : const CTransaction& tx = e.GetTx();
551 : 8530 : std::set<std::string> setDepends;
552 [ + + ]: 20734 : for (const CTxIn& txin : tx.vin)
553 : : {
554 [ + - + + ]: 12204 : if (pool.exists(txin.prevout.hash))
555 [ + - + - ]: 14336 : setDepends.insert(txin.prevout.hash.ToString());
556 : : }
557 : :
558 : 8530 : UniValue depends(UniValue::VARR);
559 [ + + ]: 15698 : for (const std::string& dep : setDepends)
560 : : {
561 [ + - + - ]: 7168 : depends.push_back(dep);
562 : : }
563 : :
564 [ + - + - ]: 17060 : info.pushKV("depends", std::move(depends));
565 : :
566 : 8530 : UniValue spent(UniValue::VARR);
567 [ + - + - : 15723 : for (const CTxMemPoolEntry& child : pool.GetChildren(e)) {
+ + ]
568 [ + - + - : 7193 : spent.push_back(child.GetTx().GetHash().ToString());
+ - ]
569 : 0 : }
570 : :
571 [ + - + - ]: 17060 : info.pushKV("spentby", std::move(spent));
572 [ + - + - : 17060 : info.pushKV("unbroadcast", pool.IsUnbroadcastTx(tx.GetHash()));
+ - ]
573 : :
574 : : // Add opt-in RBF status
575 [ + - + - : 8530 : 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 : : }
583 [ + - + - : 2 : info.pushKV("bip125-replaceable", rbfStatus);
+ - ]
584 : : }
585 : 8530 : }
586 : :
587 : 7602 : UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
588 : : {
589 [ + + ]: 7602 : if (verbose) {
590 [ + + ]: 1066 : if (include_mempool_sequence) {
591 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
592 : : }
593 : 1065 : LOCK(pool.cs);
594 : 1065 : UniValue o(UniValue::VOBJ);
595 [ + - + + ]: 4660 : for (const CTxMemPoolEntry& e : pool.entryAll()) {
596 : 3595 : UniValue info(UniValue::VOBJ);
597 [ + - ]: 3595 : 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 [ + - + - ]: 7190 : o.pushKVEnd(e.GetTx().GetHash().ToString(), std::move(info));
602 : 3595 : }
603 [ + - ]: 1065 : return o;
604 : 1065 : } else {
605 : 6536 : UniValue a(UniValue::VARR);
606 : 6536 : uint64_t mempool_sequence;
607 : 6536 : {
608 [ + - ]: 6536 : LOCK(pool.cs);
609 [ + - + - : 248851 : for (const CTxMemPoolEntry& e : pool.entryAll()) {
+ + ]
610 [ + - + - : 242315 : a.push_back(e.GetTx().GetHash().ToString());
+ - ]
611 : : }
612 [ + - ]: 6536 : mempool_sequence = pool.GetSequence();
613 : 0 : }
614 [ + + ]: 6536 : if (!include_mempool_sequence) {
615 : 6515 : return a;
616 : : } else {
617 : 21 : UniValue o(UniValue::VOBJ);
618 [ + - + - ]: 42 : o.pushKV("txids", std::move(a));
619 [ + - + - : 42 : o.pushKV("mempool_sequence", mempool_sequence);
+ - ]
620 : 21 : return o;
621 : 21 : }
622 : 6536 : }
623 : : }
624 : :
625 : 2427 : static RPCMethod getmempoolfeeratediagram()
626 : : {
627 : 2427 : return RPCMethod{"getmempoolfeeratediagram",
628 [ + - ]: 4854 : "Returns the feerate diagram for the whole mempool.",
629 : : {},
630 : : {
631 : 0 : RPCResult{"mempool chunks",
632 [ + - + - ]: 4854 : RPCResult::Type::ARR, "", "",
633 : : {
634 : : {
635 [ + - + - ]: 4854 : RPCResult::Type::OBJ, "", "",
636 : : {
637 [ + - + - ]: 4854 : {RPCResult::Type::NUM, "weight", "cumulative sigops-adjusted weight"},
638 [ + - + - ]: 4854 : {RPCResult::Type::NUM, "fee", "cumulative fee"}
639 : : }
640 : : }
641 : : }
642 [ + - + - : 21843 : }
+ + + + -
- - - ]
643 : : },
644 : 2427 : RPCExamples{
645 [ + - + - : 4854 : HelpExampleCli("getmempoolfeeratediagram", "")
+ - ]
646 [ + - + - : 9708 : + HelpExampleRpc("getmempoolfeeratediagram", "")
+ - ]
647 [ + - ]: 2427 : },
648 : 2427 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
649 : : {
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 [ + + ]: 136 : for (auto f : diagram) {
658 : 131 : UniValue o(UniValue::VOBJ);
659 [ + - + - : 262 : o.pushKV("weight", f.size);
+ - ]
660 [ + - + - : 262 : o.pushKV("fee", ValueFromAmount(f.fee));
+ - ]
661 [ + - + - ]: 131 : result.push_back(o);
662 : 131 : }
663 : 5 : return result;
664 [ + - ]: 10 : }
665 [ + - + - : 19416 : };
+ - + - +
+ - - ]
666 [ + - + - : 19416 : }
+ - + - -
- ]
667 : :
668 : 10029 : static RPCMethod getrawmempool()
669 : : {
670 : 10029 : return RPCMethod{
671 : 10029 : "getrawmempool",
672 [ + - ]: 20058 : "Returns all transaction ids in memory pool as a json array of string transaction ids.\n"
673 : : "\nHint: use getmempoolentry to fetch a specific transaction from the mempool.\n",
674 : : {
675 [ + - + - : 30087 : {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
+ - ]
676 [ + - + - : 30087 : {"mempool_sequence", RPCArg::Type::BOOL, RPCArg::Default{false}, "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
+ - ]
677 : : },
678 : : {
679 [ + - ]: 10029 : RPCResult{"for verbose = false",
680 [ + - + - ]: 20058 : RPCResult::Type::ARR, "", "",
681 : : {
682 [ + - + - ]: 20058 : {RPCResult::Type::STR_HEX, "", "The transaction id"},
683 [ + - + + : 40116 : }},
- - ]
684 [ + - ]: 20058 : RPCResult{"for verbose = true",
685 [ + - + - ]: 20058 : RPCResult::Type::OBJ_DYN, "", "",
686 : : {
687 [ + - + - : 20058 : {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
+ - ]
688 [ + - + + : 30087 : }},
- - ]
689 [ + - ]: 20058 : RPCResult{"for verbose = false and mempool_sequence = true",
690 [ + - + - ]: 20058 : RPCResult::Type::OBJ, "", "",
691 : : {
692 [ + - + - ]: 20058 : {RPCResult::Type::ARR, "txids", "",
693 : : {
694 [ + - + - ]: 20058 : {RPCResult::Type::STR_HEX, "", "The transaction id"},
695 : : }},
696 [ + - + - ]: 20058 : {RPCResult::Type::NUM, "mempool_sequence", "The mempool sequence value."},
697 [ + - + - : 90261 : }},
+ + + + -
- - - ]
698 : : },
699 : 10029 : RPCExamples{
700 [ + - + - : 20058 : HelpExampleCli("getrawmempool", "true")
+ - ]
701 [ + - + - : 40116 : + HelpExampleRpc("getrawmempool", "true")
+ - ]
702 [ + - ]: 10029 : },
703 : 10029 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
704 : : {
705 : 7598 : bool fVerbose = false;
706 [ + + ]: 7598 : if (!request.params[0].isNull())
707 : 1123 : fVerbose = request.params[0].get_bool();
708 : :
709 : 7598 : bool include_mempool_sequence = false;
710 [ + + ]: 7598 : if (!request.params[1].isNull()) {
711 : 22 : include_mempool_sequence = request.params[1].get_bool();
712 : : }
713 : :
714 : 7598 : return MempoolToJSON(EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
715 : : },
716 [ + - + - : 120348 : };
+ - + - +
+ + + - -
- - ]
717 [ + - + - : 200580 : }
+ - + - +
- + - + -
+ - + - +
- - - - -
- - ]
718 : :
719 : 3044 : static RPCMethod getmempoolancestors()
720 : : {
721 : 3044 : return RPCMethod{
722 : 3044 : "getmempoolancestors",
723 [ + - ]: 6088 : "If txid is in the mempool, returns all in-mempool ancestors.\n",
724 : : {
725 [ + - + - ]: 6088 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
726 [ + - + - : 9132 : {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
+ - ]
727 : : },
728 : : {
729 [ + - ]: 3044 : RPCResult{"for verbose = false",
730 [ + - + - ]: 6088 : RPCResult::Type::ARR, "", "",
731 [ + - + - : 15220 : {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool ancestor transaction"}}},
+ - + + -
- ]
732 [ + - ]: 6088 : RPCResult{"for verbose = true",
733 [ + - + - ]: 6088 : RPCResult::Type::OBJ_DYN, "", "",
734 : : {
735 [ + - + - : 6088 : {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
+ - ]
736 [ + - + + : 9132 : }},
- - ]
737 : : },
738 : 3044 : RPCExamples{
739 [ + - + - : 6088 : HelpExampleCli("getmempoolancestors", "\"mytxid\"")
+ - ]
740 [ + - + - : 12176 : + HelpExampleRpc("getmempoolancestors", "\"mytxid\"")
+ - ]
741 [ + - ]: 3044 : },
742 : 3044 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
743 : : {
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 : : }
757 : :
758 [ + - ]: 613 : auto ancestors{mempool.CalculateMemPoolAncestors(*entry)};
759 : :
760 [ + + ]: 613 : if (!fVerbose) {
761 : 548 : UniValue o(UniValue::VARR);
762 [ + + ]: 11954 : for (CTxMemPool::txiter ancestorIt : ancestors) {
763 [ + - + - : 11406 : o.push_back(ancestorIt->GetTx().GetHash().ToString());
+ - ]
764 : : }
765 : : return o;
766 : 0 : } else {
767 : 65 : UniValue o(UniValue::VOBJ);
768 [ + + ]: 2144 : for (CTxMemPool::txiter ancestorIt : ancestors) {
769 : 2079 : const CTxMemPoolEntry &e = *ancestorIt;
770 : 2079 : UniValue info(UniValue::VOBJ);
771 [ + - ]: 2079 : entryToJSON(mempool, info, e);
772 [ + - + - ]: 4158 : o.pushKV(e.GetTx().GetHash().ToString(), std::move(info));
773 : 2079 : }
774 : 65 : return o;
775 : 65 : }
776 [ + - ]: 1226 : },
777 [ + - + - : 33484 : };
+ - + - +
+ + + - -
- - ]
778 [ + - + - : 36528 : }
+ - + - +
- + - - -
- - ]
779 : :
780 : 11950 : static RPCMethod getmempooldescendants()
781 : : {
782 : 11950 : return RPCMethod{
783 : 11950 : "getmempooldescendants",
784 [ + - ]: 23900 : "If txid is in the mempool, returns all in-mempool descendants.\n",
785 : : {
786 [ + - + - ]: 23900 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
787 [ + - + - : 35850 : {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
+ - ]
788 : : },
789 : : {
790 [ + - ]: 11950 : RPCResult{"for verbose = false",
791 [ + - + - ]: 23900 : RPCResult::Type::ARR, "", "",
792 [ + - + - : 59750 : {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool descendant transaction"}}},
+ - + + -
- ]
793 [ + - ]: 23900 : RPCResult{"for verbose = true",
794 [ + - + - ]: 23900 : RPCResult::Type::OBJ_DYN, "", "",
795 : : {
796 [ + - + - : 23900 : {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
+ - ]
797 [ + - + + : 35850 : }},
- - ]
798 : : },
799 : 11950 : RPCExamples{
800 [ + - + - : 23900 : HelpExampleCli("getmempooldescendants", "\"mytxid\"")
+ - ]
801 [ + - + - : 47800 : + HelpExampleRpc("getmempooldescendants", "\"mytxid\"")
+ - ]
802 [ + - ]: 11950 : },
803 : 11950 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
804 : : {
805 : 9519 : bool fVerbose = false;
806 [ + + ]: 9519 : if (!request.params[1].isNull())
807 : 65 : fVerbose = request.params[1].get_bool();
808 : :
809 : 9519 : auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
810 : :
811 : 9519 : const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
812 : 9519 : LOCK(mempool.cs);
813 : :
814 [ + - ]: 9519 : const auto it{mempool.GetIter(txid)};
815 [ - + ]: 9519 : if (!it) {
816 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
817 : : }
818 : :
819 [ + - ]: 9519 : CTxMemPool::setEntries setDescendants;
820 [ + - ]: 9519 : mempool.CalculateDescendants(*it, setDescendants);
821 : : // CTxMemPool::CalculateDescendants will include the given tx
822 : 9519 : setDescendants.erase(*it);
823 : :
824 [ + + ]: 9519 : if (!fVerbose) {
825 : 9454 : UniValue o(UniValue::VARR);
826 [ + + ]: 175782 : for (CTxMemPool::txiter descendantIt : setDescendants) {
827 [ + - + - : 166328 : o.push_back(descendantIt->GetTx().GetHash().ToString());
+ - ]
828 : : }
829 : :
830 : : return o;
831 : 0 : } else {
832 : 65 : UniValue o(UniValue::VOBJ);
833 [ + + ]: 2144 : for (CTxMemPool::txiter descendantIt : setDescendants) {
834 : 2079 : const CTxMemPoolEntry &e = *descendantIt;
835 : 2079 : UniValue info(UniValue::VOBJ);
836 [ + - ]: 2079 : entryToJSON(mempool, info, e);
837 [ + - + - ]: 4158 : o.pushKV(e.GetTx().GetHash().ToString(), std::move(info));
838 : 2079 : }
839 : 65 : return o;
840 : 65 : }
841 [ + - ]: 19038 : },
842 [ + - + - : 131450 : };
+ - + - +
+ + + - -
- - ]
843 [ + - + - : 143400 : }
+ - + - +
- + - - -
- - ]
844 : :
845 : 3576 : static RPCMethod getmempoolcluster()
846 : : {
847 : 3576 : return RPCMethod{"getmempoolcluster",
848 [ + - ]: 7152 : "Returns mempool data for given cluster\n",
849 : : {
850 [ + - + - ]: 7152 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The txid of a transaction in the cluster"},
851 : : },
852 [ + - ]: 7152 : RPCResult{
853 [ + - + - : 7152 : RPCResult::Type::OBJ, "", "", ClusterDescription()},
+ - + - ]
854 : 3576 : RPCExamples{
855 [ + - + - : 7152 : HelpExampleCli("getmempoolcluster", "txid")
+ - ]
856 [ + - + - : 14304 : + HelpExampleRpc("getmempoolcluster", "txid")
+ - + - ]
857 [ + - ]: 3576 : },
858 : 3576 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
859 : : {
860 : 1145 : uint256 hash = ParseHashV(request.params[0], "txid");
861 : :
862 : 1145 : const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
863 : 1145 : LOCK(mempool.cs);
864 : :
865 [ + - ]: 1145 : auto txid = Txid::FromUint256(hash);
866 [ + - ]: 1145 : const auto entry{mempool.GetEntry(txid)};
867 [ + + ]: 1145 : if (entry == nullptr) {
868 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
869 : : }
870 : :
871 [ + - ]: 1144 : auto cluster = mempool.GetCluster(txid);
872 : :
873 : 1144 : UniValue info(UniValue::VOBJ);
874 [ + - + - ]: 1144 : clusterToJSON(mempool, info, cluster);
875 : 1144 : return info;
876 [ + - ]: 2288 : },
877 [ + - + - : 17880 : };
+ + - - ]
878 [ + - ]: 7152 : }
879 : :
880 : 3214 : static RPCMethod getmempoolentry()
881 : : {
882 : 3214 : return RPCMethod{
883 : 3214 : "getmempoolentry",
884 [ + - ]: 6428 : "Returns mempool data for given transaction\n",
885 : : {
886 [ + - + - ]: 6428 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
887 : : },
888 [ + - ]: 6428 : RPCResult{
889 [ + - + - : 6428 : RPCResult::Type::OBJ, "", "", MempoolEntryDescription()},
+ - + - ]
890 : 3214 : RPCExamples{
891 [ + - + - : 6428 : HelpExampleCli("getmempoolentry", "\"mytxid\"")
+ - ]
892 [ + - + - : 12856 : + HelpExampleRpc("getmempoolentry", "\"mytxid\"")
+ - + - ]
893 [ + - ]: 3214 : },
894 : 3214 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
895 : : {
896 : 783 : auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
897 : :
898 : 783 : const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
899 : 783 : LOCK(mempool.cs);
900 : :
901 [ + - ]: 783 : const auto entry{mempool.GetEntry(txid)};
902 [ + + ]: 783 : if (entry == nullptr) {
903 [ + - + - ]: 12 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
904 : : }
905 : :
906 : 777 : UniValue info(UniValue::VOBJ);
907 [ + - ]: 777 : entryToJSON(mempool, info, *entry);
908 [ + - ]: 777 : return info;
909 : 777 : },
910 [ + - + - : 16070 : };
+ + - - ]
911 [ + - ]: 6428 : }
912 : :
913 : 2522 : static RPCMethod gettxspendingprevout()
914 : : {
915 : 2522 : return RPCMethod{"gettxspendingprevout",
916 [ + - ]: 5044 : "Scans the mempool (and the txospenderindex, if available) to find transactions spending any of the given outputs",
917 : : {
918 [ + - + - ]: 5044 : {"outputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The transaction outputs that we want to check, and within each, the txid (string) vout (numeric).",
919 : : {
920 [ + - + - ]: 5044 : {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
921 : : {
922 [ + - + - ]: 5044 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
923 [ + - + - ]: 5044 : {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
924 : : },
925 : : },
926 : : },
927 : : },
928 [ + - + - ]: 5044 : {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
929 : : {
930 [ + - + - : 7566 : {"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 [ + - + - : 7566 : {"return_spending_tx", RPCArg::Type::BOOL, RPCArg::DefaultHint{"false"}, "If true, return the full spending tx."},
+ - ]
932 : : },
933 : : },
934 : : },
935 [ + - ]: 5044 : RPCResult{
936 [ + - + - ]: 5044 : RPCResult::Type::ARR, "", "",
937 : : {
938 [ + - + - ]: 5044 : {RPCResult::Type::OBJ, "", "",
939 : : {
940 [ + - + - ]: 5044 : {RPCResult::Type::STR_HEX, "txid", "the transaction id of the checked output"},
941 [ + - + - ]: 5044 : {RPCResult::Type::NUM, "vout", "the vout value of the checked output"},
942 [ + - + - ]: 5044 : {RPCResult::Type::STR_HEX, "spendingtxid", /*optional=*/true, "the transaction id of the mempool transaction spending this output (omitted if unspent)"},
943 [ + - + - ]: 5044 : {RPCResult::Type::STR_HEX, "spendingtx", /*optional=*/true, "the transaction spending this output (only if return_spending_tx is set, omitted if unspent)"},
944 [ + - + - ]: 5044 : {RPCResult::Type::STR_HEX, "blockhash", /*optional=*/true, "the hash of the spending block (omitted if unspent or the spending tx is not confirmed)"},
945 : : }},
946 : : }
947 [ + - + - : 37830 : },
+ - + + +
+ - - -
- ]
948 : 2522 : RPCExamples{
949 [ + - + - : 5044 : HelpExampleCli("gettxspendingprevout", "\"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":3}]\"")
+ - ]
950 [ + - + - : 10088 : + HelpExampleRpc("gettxspendingprevout", "\"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":3}]\"")
+ - + - ]
951 [ + - + - : 15132 : + HelpExampleCliNamed("gettxspendingprevout", {{"outputs", "[{\"txid\":\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\",\"vout\":3}]"}, {"return_spending_tx", true}})
+ - + - +
+ - - ]
952 [ + - ]: 2522 : },
953 : 2522 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
954 : : {
955 : 91 : const UniValue& output_params = request.params[0].get_array();
956 [ - + + + ]: 91 : if (output_params.empty()) {
957 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, outputs are missing");
958 : : }
959 [ + + ]: 90 : const UniValue options{request.params[1].isNull() ? UniValue::VOBJ : request.params[1]};
960 [ + - + + : 360 : RPCTypeCheckObj(options,
- - ]
961 : : {
962 [ + - ]: 90 : {"mempool_only", UniValueType(UniValue::VBOOL)},
963 [ + - ]: 90 : {"return_spending_tx", UniValueType(UniValue::VBOOL)},
964 : : }, /*fAllowNull=*/true, /*fStrict=*/true);
965 : :
966 [ + - + + : 182 : const bool mempool_only{options.exists("mempool_only") ? options["mempool_only"].get_bool() : !g_txospenderindex};
+ - + - +
- ]
967 [ + - + + : 192 : 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 : : COutPoint outpoint;
972 : : const UniValue* raw;
973 : : };
974 : 90 : std::vector<Entry> prevouts_to_process;
975 [ - + + - ]: 90 : prevouts_to_process.reserve(output_params.size());
976 [ - + + + ]: 184 : for (unsigned int idx = 0; idx < output_params.size(); idx++) {
977 [ + - + - ]: 99 : const UniValue& o = output_params[idx].get_obj();
978 : :
979 [ + + + + : 400 : RPCTypeCheckObj(o,
+ + ]
980 : : {
981 [ + - ]: 99 : {"txid", UniValueType(UniValue::VSTR)},
982 [ + - ]: 99 : {"vout", UniValueType(UniValue::VNUM)},
983 : : }, /*fAllowNull=*/false, /*fStrict=*/true);
984 : :
985 [ + - ]: 95 : const Txid txid = Txid::FromUint256(ParseHashO(o, "txid"));
986 [ + - + - ]: 95 : const int nOutput{o.find_value("vout").getInt<int>()};
987 [ + + ]: 95 : if (nOutput < 0) {
988 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
989 : : }
990 [ + - ]: 94 : prevouts_to_process.emplace_back(COutPoint{txid, static_cast<uint32_t>(nOutput)}, &o);
991 : : }
992 : :
993 : 179 : auto make_output = [return_spending_tx](const Entry& prevout, const CTransaction* spending_tx = nullptr) {
994 : 89 : UniValue o{*prevout.raw};
995 [ + + ]: 89 : if (spending_tx) {
996 [ + - + - : 168 : o.pushKV("spendingtxid", spending_tx->GetHash().ToString());
+ - + - ]
997 [ + + ]: 84 : if (return_spending_tx) {
998 [ + - + - : 22 : o.pushKV("spendingtx", EncodeHexTx(*spending_tx));
+ - + - ]
999 : : }
1000 : : }
1001 : 89 : return o;
1002 : 0 : };
1003 : :
1004 : 85 : UniValue result{UniValue::VARR};
1005 : :
1006 : : // Search the mempool first
1007 : 85 : {
1008 [ + - ]: 85 : const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
1009 [ + - ]: 85 : LOCK(mempool.cs);
1010 : :
1011 : : // Make the result if the spending tx appears in the mempool or this is a mempool_only request
1012 [ + + ]: 179 : 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 : : }
1021 : :
1022 [ + - + - ]: 79 : result.push_back(make_output(*it, spending_tx));
1023 : 79 : it = prevouts_to_process.erase(it);
1024 : : }
1025 : 0 : }
1026 : :
1027 : : // Return early if all requests have been handled by the mempool search
1028 [ + + ]: 85 : if (prevouts_to_process.empty()) {
1029 : : return result;
1030 : : }
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 [ + - + - : 13 : if (!g_txospenderindex || !g_txospenderindex->BlockUntilSyncedToCurrentChain()) {
+ - ]
1035 [ # # # # ]: 0 : throw JSONRPCError(RPC_MISC_ERROR, "Mempool lacks a relevant spend, and txospenderindex is unavailable.");
1036 : : }
1037 : :
1038 [ + + ]: 28 : 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 : : }
1043 : :
1044 [ + - + + ]: 15 : if (const auto& spender_opt{spender.value()}) {
1045 [ + - ]: 10 : UniValue o{make_output(prevout, spender_opt->tx.get())};
1046 [ + - + - : 20 : 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 : : }
1052 : 15 : }
1053 : :
1054 : : return result;
1055 [ + - + - : 283 : },
+ - + - +
- + - - -
- + ]
1056 [ + - + - : 42874 : };
+ - + - +
- + + + +
+ + + + -
- - - - -
- - ]
1057 [ + - + - : 68094 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- ]
1058 : :
1059 : 1286 : UniValue MempoolInfoToJSON(const CTxMemPool& pool)
1060 : : {
1061 : : // Make sure this call is atomic in the pool.
1062 : 1286 : LOCK(pool.cs);
1063 : 1286 : UniValue ret(UniValue::VOBJ);
1064 [ + - + - : 2572 : ret.pushKV("loaded", pool.GetLoadTried());
+ - + - ]
1065 [ + - + - : 2572 : ret.pushKV("size", pool.size());
+ - + - ]
1066 [ + - + - : 2572 : ret.pushKV("bytes", pool.GetTotalTxSize());
+ - ]
1067 [ + - + - : 2572 : ret.pushKV("usage", pool.DynamicMemoryUsage());
+ - + - ]
1068 [ + - + - : 2572 : ret.pushKV("total_fee", ValueFromAmount(pool.GetTotalFee()));
+ - ]
1069 [ + - + - : 2572 : ret.pushKV("maxmempool", pool.m_opts.max_size_bytes);
+ - ]
1070 [ + - + - : 2572 : ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(), pool.m_opts.min_relay_feerate).GetFeePerK()));
+ - + - ]
1071 [ + - + - : 2572 : ret.pushKV("minrelaytxfee", ValueFromAmount(pool.m_opts.min_relay_feerate.GetFeePerK()));
+ - ]
1072 [ + - + - : 2572 : ret.pushKV("incrementalrelayfee", ValueFromAmount(pool.m_opts.incremental_relay_feerate.GetFeePerK()));
+ - ]
1073 [ + - + - : 2572 : ret.pushKV("unbroadcastcount", pool.GetUnbroadcastTxs().size());
+ - + - ]
1074 [ + - + - : 2572 : ret.pushKV("permitbaremultisig", pool.m_opts.permit_bare_multisig);
+ - ]
1075 [ + + + - : 3856 : ret.pushKV("maxdatacarriersize", pool.m_opts.max_datacarrier_bytes.value_or(0));
+ - + - ]
1076 [ + - + - : 2572 : ret.pushKV("limitclustercount", pool.m_opts.limits.cluster_count);
+ - ]
1077 [ + - + - : 2572 : ret.pushKV("limitclustersize", pool.m_opts.limits.cluster_size_vbytes);
+ - ]
1078 [ + - + - : 2572 : ret.pushKV("optimal", pool.m_txgraph->DoWork(0)); // 0 work is a quick check for known optimality
+ - ]
1079 [ + - + - : 1286 : if (IsDeprecatedRPCEnabled("fullrbf")) {
+ + ]
1080 [ + - + - : 6 : ret.pushKV("fullrbf", true);
+ - ]
1081 : : }
1082 [ + - ]: 1286 : return ret;
1083 : 1286 : }
1084 : :
1085 : 3716 : static RPCMethod getmempoolinfo()
1086 : : {
1087 : 3716 : return RPCMethod{"getmempoolinfo",
1088 [ + - ]: 7432 : "Returns details on the active state of the TX memory pool.",
1089 : : {},
1090 [ + - ]: 7432 : RPCResult{
1091 [ + - ]: 7432 : RPCResult::Type::OBJ, "", "",
1092 : 3716 : [](){
1093 : 3716 : std::vector<RPCResult> list = {
1094 [ + - + - ]: 7432 : {RPCResult::Type::BOOL, "loaded", "True if the initial load attempt of the persisted mempool finished"},
1095 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "size", "Current tx count"},
1096 [ + - + - ]: 7432 : {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 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "usage", "Total memory usage for the mempool"},
1098 [ + - + - ]: 7432 : {RPCResult::Type::STR_AMOUNT, "total_fee", "Total fees for the mempool in " + CURRENCY_UNIT + ", ignoring modified fees through prioritisetransaction"},
1099 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "maxmempool", "Maximum memory usage for the mempool"},
1100 [ + - + - ]: 7432 : {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 [ + - + - ]: 7432 : {RPCResult::Type::STR_AMOUNT, "minrelaytxfee", "Current minimum relay fee for transactions"},
1102 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "incrementalrelayfee", "minimum fee rate increment for mempool limiting or replacement in " + CURRENCY_UNIT + "/kvB"},
1103 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "unbroadcastcount", "Current number of transactions that haven't passed initial broadcast yet"},
1104 [ + - + - ]: 7432 : {RPCResult::Type::BOOL, "permitbaremultisig", "True if the mempool accepts transactions with bare multisig outputs"},
1105 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "maxdatacarriersize", "Maximum number of bytes that can be used by OP_RETURN outputs in the mempool"},
1106 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "limitclustercount", "Maximum number of transactions that can be in a cluster (configured by -limitclustercount)"},
1107 [ + - + - ]: 7432 : {RPCResult::Type::NUM, "limitclustersize", "Maximum size of a cluster in virtual bytes (configured by -limitclustersize)"},
1108 [ + - + - ]: 7432 : {RPCResult::Type::BOOL, "optimal", "If the mempool is in a known-optimal transaction ordering"},
1109 [ + - + + : 118912 : };
- - ]
1110 [ + - + - : 3716 : if (IsDeprecatedRPCEnabled("fullrbf")) {
+ + ]
1111 [ + - ]: 5 : list.emplace_back(RPCResult::Type::BOOL, "fullrbf", "True if the mempool accepts RBF without replaceability signaling inspection (DEPRECATED)");
1112 : : }
1113 : 3716 : return list;
1114 [ + - + - : 118912 : }()
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - -
- ]
1115 [ + - ]: 7432 : },
1116 : 3716 : RPCExamples{
1117 [ + - + - : 7432 : HelpExampleCli("getmempoolinfo", "")
+ - ]
1118 [ + - + - : 14864 : + HelpExampleRpc("getmempoolinfo", "")
+ - + - ]
1119 [ + - ]: 3716 : },
1120 : 3716 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1121 : : {
1122 : 1285 : return MempoolInfoToJSON(EnsureAnyMemPool(request.context));
1123 : : },
1124 [ + - + - ]: 14864 : };
1125 : : }
1126 : :
1127 : 2434 : static RPCMethod importmempool()
1128 : : {
1129 : 2434 : return RPCMethod{
1130 : 2434 : "importmempool",
1131 [ + - ]: 4868 : "Import a mempool.dat file and attempt to add its contents to the mempool.\n"
1132 : : "Warning: Importing untrusted files is dangerous, especially if metadata from the file is taken over.",
1133 : : {
1134 [ + - + - ]: 4868 : {"filepath", RPCArg::Type::STR, RPCArg::Optional::NO, "The mempool file"},
1135 [ + - ]: 4868 : {"options",
1136 : : RPCArg::Type::OBJ_NAMED_PARAMS,
1137 : 2434 : RPCArg::Optional::OMITTED,
1138 [ + - ]: 4868 : "",
1139 : : {
1140 [ + - + - ]: 4868 : {"use_current_time", RPCArg::Type::BOOL, RPCArg::Default{true},
1141 [ + - ]: 4868 : "Whether to use the current system time or use the entry time metadata from the mempool file.\n"
1142 : : "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior."},
1143 [ + - + - ]: 4868 : {"apply_fee_delta_priority", RPCArg::Type::BOOL, RPCArg::Default{false},
1144 [ + - ]: 4868 : "Whether to apply the fee delta metadata from the mempool file.\n"
1145 : : "It will be added to any existing fee deltas.\n"
1146 : : "The fee delta can be set by the prioritisetransaction RPC.\n"
1147 : : "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior.\n"
1148 : : "Only set this bool if you understand what it does."},
1149 [ + - + - ]: 4868 : {"apply_unbroadcast_set", RPCArg::Type::BOOL, RPCArg::Default{false},
1150 [ + - ]: 4868 : "Whether to apply the unbroadcast set metadata from the mempool file.\n"
1151 : : "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior."},
1152 : : },
1153 [ + - ]: 2434 : RPCArgOptions{.oneline_description = "options"}},
1154 : : },
1155 [ + - + - : 4868 : RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
+ - + - ]
1156 [ + - + - : 7302 : RPCExamples{HelpExampleCli("importmempool", "/path/to/mempool.dat") + HelpExampleRpc("importmempool", "/path/to/mempool.dat")},
+ - + - +
- + - + -
+ - ]
1157 : 2434 : [](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 : : }
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 [ - + + + : 5 : };
+ + ]
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 : : }
1181 : :
1182 : 3 : UniValue ret{UniValue::VOBJ};
1183 : 3 : return ret;
1184 : 6 : },
1185 [ + - + - : 26774 : };
+ - + + +
+ - - -
- ]
1186 [ + - + - : 21906 : }
+ - + - +
- - - -
- ]
1187 : :
1188 : 2435 : static RPCMethod savemempool()
1189 : : {
1190 : 2435 : return RPCMethod{
1191 : 2435 : "savemempool",
1192 [ + - ]: 4870 : "Dumps the mempool to disk. It will fail until the previous dump is fully loaded.\n",
1193 : : {},
1194 [ + - ]: 4870 : RPCResult{
1195 [ + - ]: 4870 : RPCResult::Type::OBJ, "", "",
1196 : : {
1197 [ + - + - ]: 4870 : {RPCResult::Type::STR, "filename", "the directory and file where the mempool was saved"},
1198 [ + - + - : 9740 : }},
+ + - - ]
1199 : 2435 : RPCExamples{
1200 [ + - + - : 4870 : HelpExampleCli("savemempool", "")
+ - ]
1201 [ + - + - : 9740 : + HelpExampleRpc("savemempool", "")
+ - + - ]
1202 [ + - ]: 2435 : },
1203 : 2435 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1204 : : {
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 : : }
1211 : :
1212 : 4 : const fs::path& dump_path = MempoolPath(args);
1213 : :
1214 [ + - + + ]: 4 : if (!DumpMempool(mempool, dump_path)) {
1215 [ + - + - ]: 2 : throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
1216 : : }
1217 : :
1218 : 3 : UniValue ret(UniValue::VOBJ);
1219 [ + - + - : 6 : ret.pushKV("filename", dump_path.utf8string());
+ - + - ]
1220 : :
1221 : 6 : return ret;
1222 : 0 : },
1223 [ + - + - ]: 9740 : };
1224 [ + - ]: 4870 : }
1225 : :
1226 : 5296 : static std::vector<RPCResult> OrphanDescription()
1227 : : {
1228 : 5296 : return {
1229 [ + - + - ]: 10592 : RPCResult{RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
1230 [ + - + - ]: 10592 : RPCResult{RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
1231 [ + - + - ]: 10592 : RPCResult{RPCResult::Type::NUM, "bytes", "The serialized transaction size in bytes"},
1232 [ + - + - ]: 10592 : 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 [ + - + - ]: 10592 : RPCResult{RPCResult::Type::NUM, "weight", "The transaction weight as defined in BIP 141."},
1234 [ + - + - ]: 10592 : RPCResult{RPCResult::Type::ARR, "from", "",
1235 : : {
1236 [ + - + - ]: 10592 : RPCResult{RPCResult::Type::NUM, "peer_id", "Peer ID"},
1237 [ + - + + : 15888 : }},
- - ]
1238 [ + - + + : 47664 : };
- - ]
1239 [ + - + - : 74144 : }
+ - + - +
- + - + -
- - ]
1240 : :
1241 : 42 : static UniValue OrphanToJSON(const node::TxOrphanage::OrphanInfo& orphan)
1242 : : {
1243 : 42 : UniValue o(UniValue::VOBJ);
1244 [ + - + - : 84 : o.pushKV("txid", orphan.tx->GetHash().ToString());
+ - + - ]
1245 [ + - + - : 84 : o.pushKV("wtxid", orphan.tx->GetWitnessHash().ToString());
+ - + - ]
1246 [ + - + - : 84 : o.pushKV("bytes", orphan.tx->ComputeTotalSize());
+ - + - ]
1247 [ + - + - : 84 : o.pushKV("vsize", GetVirtualTransactionSize(*orphan.tx));
+ - + - ]
1248 [ + - + - : 84 : o.pushKV("weight", GetTransactionWeight(*orphan.tx));
+ - ]
1249 : 42 : UniValue from(UniValue::VARR);
1250 [ + + ]: 90 : for (const auto fromPeer: orphan.announcers) {
1251 [ + - + - ]: 48 : from.push_back(fromPeer);
1252 : : }
1253 [ + - + - : 84 : o.pushKV("from", from);
+ - ]
1254 : 42 : return o;
1255 : 42 : }
1256 : :
1257 : 2648 : static RPCMethod getorphantxs()
1258 : : {
1259 : 2648 : return RPCMethod{
1260 : 2648 : "getorphantxs",
1261 [ + - ]: 5296 : "Shows transactions in the tx orphanage.\n"
1262 : : "\nEXPERIMENTAL warning: this call may be changed in future releases.\n",
1263 : : {
1264 [ + - + - : 7944 : {"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 [ + - ]: 5296 : RPCArgOptions{.skip_type_check = true}},
1266 : : },
1267 : : {
1268 [ + - ]: 2648 : RPCResult{"for verbose = 0",
1269 [ + - + - ]: 5296 : RPCResult::Type::ARR, "", "",
1270 : : {
1271 [ + - + - ]: 5296 : {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
1272 [ + - + + : 10592 : }},
- - ]
1273 [ + - ]: 5296 : RPCResult{"for verbose = 1",
1274 [ + - + - ]: 5296 : RPCResult::Type::ARR, "", "",
1275 : : {
1276 [ + - + - : 5296 : {RPCResult::Type::OBJ, "", "", OrphanDescription()},
+ - ]
1277 [ + - + + : 7944 : }},
- - ]
1278 [ + - ]: 5296 : RPCResult{"for verbose = 2",
1279 [ + - + - ]: 5296 : RPCResult::Type::ARR, "", "",
1280 : : {
1281 [ + - + - ]: 5296 : {RPCResult::Type::OBJ, "", "",
1282 [ + - + - : 13240 : Cat<std::vector<RPCResult>>(
+ + - - ]
1283 [ + - ]: 5296 : OrphanDescription(),
1284 [ + - + - ]: 5296 : {{RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded transaction data"}}
1285 : : )
1286 : : },
1287 [ + - + + : 7944 : }},
- - ]
1288 : : },
1289 : 2648 : RPCExamples{
1290 [ + - + - : 5296 : HelpExampleCli("getorphantxs", "2")
+ - ]
1291 [ + - + - : 10592 : + HelpExampleRpc("getorphantxs", "2")
+ - ]
1292 [ + - ]: 2648 : },
1293 : 2648 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1294 : : {
1295 : 225 : const NodeContext& node = EnsureAnyNodeContext(request.context);
1296 : 225 : PeerManager& peerman = EnsurePeerman(node);
1297 : 225 : std::vector<node::TxOrphanage::OrphanInfo> orphanage = peerman.GetOrphanTransactions();
1298 : :
1299 [ + - + + ]: 225 : int verbosity{ParseVerbosity(request.params[0], /*default_verbosity=*/0, /*allow_bool=*/false)};
1300 : :
1301 : 223 : UniValue ret(UniValue::VARR);
1302 : :
1303 [ + + ]: 223 : if (verbosity == 0) {
1304 [ + + ]: 18491 : for (auto const& orphan : orphanage) {
1305 [ + - + - : 18302 : ret.push_back(orphan.tx->GetHash().ToString());
+ - ]
1306 : : }
1307 [ + + ]: 34 : } else if (verbosity == 1) {
1308 [ + + ]: 55 : for (auto const& orphan : orphanage) {
1309 [ + - + - ]: 32 : ret.push_back(OrphanToJSON(orphan));
1310 : : }
1311 [ + + ]: 11 : } else if (verbosity == 2) {
1312 [ + + ]: 19 : for (auto const& orphan : orphanage) {
1313 [ + - ]: 10 : UniValue o{OrphanToJSON(orphan)};
1314 [ + - + - : 20 : o.pushKV("hex", EncodeHexTx(*orphan.tx));
+ - + - ]
1315 [ + - + - ]: 10 : ret.push_back(o);
1316 : 10 : }
1317 : : } else {
1318 [ + - + - : 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid verbosity value " + ToString(verbosity));
+ - ]
1319 : : }
1320 : :
1321 : 221 : return ret;
1322 : 227 : },
1323 [ + - + - : 29128 : };
+ - + - +
+ + + - -
- - ]
1324 [ + - + - : 39720 : }
+ - + - +
- + - + -
+ - - - ]
1325 : :
1326 : 2549 : static RPCMethod submitpackage()
1327 : : {
1328 : 2549 : return RPCMethod{"submitpackage",
1329 [ + - ]: 5098 : "Submit a package of raw transactions (serialized, hex-encoded) to local node.\n"
1330 : : "The package will be validated according to consensus and mempool policy rules. If any transaction passes, it will be accepted to mempool.\n"
1331 : : "This RPC is experimental and the interface may be unstable. Refer to doc/policy/packages.md for documentation on package policies.\n"
1332 : : "Warning: successful submission does not mean the transactions will propagate throughout the network.\n"
1333 : : ,
1334 : : {
1335 [ + - + - ]: 5098 : {"package", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of raw transactions.\n"
1336 : : "The package must consist of a transaction with (some, all, or none of) its unconfirmed parents. A single transaction is permitted.\n"
1337 : : "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 : : "The package must be topologically sorted, with the child being the last element in the array if there are multiple elements.",
1339 : : {
1340 [ + - + - ]: 5098 : {"rawtx", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
1341 : : },
1342 : : },
1343 [ + - + - : 5098 : {"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
+ - ]
1344 [ + - ]: 5098 : "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
1345 : 2549 : "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
1346 [ + - + - : 5098 : {"maxburnamount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_BURN_AMOUNT)},
+ - ]
1347 [ + - ]: 5098 : "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 : : "If burning funds through unspendable outputs is desired, increase this value.\n"
1349 : 2549 : "This check is based on heuristics and does not guarantee spendability of outputs.\n"
1350 : : },
1351 : : },
1352 [ + - ]: 5098 : RPCResult{
1353 [ + - + - ]: 5098 : RPCResult::Type::OBJ, "", "",
1354 : : {
1355 [ + - + - ]: 5098 : {RPCResult::Type::STR, "package_msg", "The transaction package result message. \"success\" indicates all transactions were accepted into or are already in the mempool."},
1356 [ + - + - ]: 5098 : {RPCResult::Type::OBJ_DYN, "tx-results", "The transaction results keyed by wtxid. An entry is returned for every submitted wtxid.",
1357 : : {
1358 [ + - + - ]: 5098 : {RPCResult::Type::OBJ, "wtxid", "transaction wtxid", {
1359 [ + - + - ]: 5098 : {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
1360 [ + - + - ]: 5098 : {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 [ + - + - ]: 5098 : {RPCResult::Type::NUM, "vsize", /*optional=*/true, "Sigops-adjusted virtual transaction size."},
1362 [ + - + - ]: 5098 : {RPCResult::Type::OBJ, "fees", /*optional=*/true, "Transaction fees", {
1363 [ + - + - ]: 5098 : {RPCResult::Type::STR_AMOUNT, "base", "transaction fee in " + CURRENCY_UNIT},
1364 [ + - + - ]: 5098 : {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 [ + - + - ]: 5098 : {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 [ + - + - ]: 5098 : {{RPCResult::Type::STR_HEX, "", "transaction wtxid in hex"},
1367 : : }},
1368 : : }},
1369 [ + - + - ]: 5098 : {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 : : }}
1371 : : }},
1372 [ + - + - ]: 5098 : {RPCResult::Type::ARR, "replaced-transactions", /*optional=*/true, "List of txids of replaced transactions",
1373 : : {
1374 [ + - + - ]: 5098 : {RPCResult::Type::STR_HEX, "", "The transaction id"},
1375 : : }},
1376 : : },
1377 [ + - + - : 89215 : },
+ - + - +
- + - + -
+ + + + +
+ + + + +
+ + - - -
- - - - -
- - - - ]
1378 : 2549 : RPCExamples{
1379 [ + - + - : 5098 : HelpExampleRpc("submitpackage", R"(["raw-parent-tx-1", "raw-parent-tx-2", "raw-child-tx"])") +
+ - ]
1380 [ + - + - : 7647 : HelpExampleCli("submitpackage", R"('["raw-tx-without-unconfirmed-parents"]')")
+ - + - ]
1381 [ + - ]: 2549 : },
1382 : 2549 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1383 : : {
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 [ + - + - : 6 : "Array must contain between 1 and " + ToString(MAX_PACKAGE_COUNT) + " transactions.");
+ - ]
1388 : : }
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 : : }
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 [ + - + + ]: 495 : 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 [ + - + - : 3 : "TX decode failed: " + rawtx.get_str() + " Make sure the tx has at least one input.");
+ - ]
1408 : : }
1409 : :
1410 [ + + ]: 893 : for (const auto& out : mtx.vout) {
1411 [ + + + - : 514 : if((out.scriptPubKey.IsUnspendable() || !out.scriptPubKey.HasValidOps()) && out.nValue > max_burn_amount) {
- + + + ]
1412 [ + - + - ]: 2 : throw JSONRPCTransactionError(TransactionError::MAX_BURN_EXCEEDED);
1413 : : }
1414 : : }
1415 : :
1416 [ + - + - ]: 1137 : txns.emplace_back(MakeTransactionRef(std::move(mtx)));
1417 : 381 : }
1418 [ + - ]: 114 : CHECK_NONFATAL(!txns.empty());
1419 [ - + + + : 114 : if (txns.size() > 1 && !IsChildWithParentsTree(txns)) {
+ - + + ]
1420 [ + - + - ]: 4 : throw JSONRPCTransactionError(TransactionError::INVALID_PACKAGE, "package topology disallowed. not child-with-parents or parents depend on each other.");
1421 : : }
1422 : :
1423 [ + - ]: 112 : NodeContext& node = EnsureAnyNodeContext(request.context);
1424 [ + - ]: 112 : CTxMemPool& mempool = EnsureMemPool(node);
1425 [ + - + - ]: 112 : Chainstate& chainstate = EnsureChainman(node).ActiveChainstate();
1426 [ + - + - ]: 336 : 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 [ + + ]: 293 : for (const auto& tx : txns) {
1437 [ + - + - ]: 227 : CHECK_NONFATAL(mempool.exists(tx->GetHash()));
1438 : : }
1439 : : break;
1440 : : }
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 : : }
1447 : 46 : 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 [ - + + + : 48 : CHECK_NONFATAL(package_result.m_tx_results.size() == txns.size() ||
+ - + - ]
1453 : : package_result.m_tx_results.empty());
1454 : : break;
1455 : : }
1456 : : }
1457 : :
1458 : 112 : size_t num_broadcast{0};
1459 [ + + ]: 483 : 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 : : }
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 : : tx,
1469 : : err_string,
1470 [ + - ]: 289 : /*max_tx_fee=*/0,
1471 : : node::TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL,
1472 : : /*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 : : }
1478 : 289 : num_broadcast++;
1479 : 289 : }
1480 : :
1481 : 112 : UniValue rpc_result{UniValue::VOBJ};
1482 [ + - + - : 224 : rpc_result.pushKV("package_msg", package_msg);
+ - ]
1483 : 224 : UniValue tx_result_map{UniValue::VOBJ};
1484 : 224 : std::set<Txid> replaced_txids;
1485 [ + + ]: 483 : for (const auto& tx : txns) {
1486 : 371 : UniValue result_inner{UniValue::VOBJ};
1487 [ + - + - : 742 : 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 [ + - + - : 12 : result_inner.pushKV("error", "package-not-validated");
+ - ]
1496 [ - + + - ]: 18 : tx_result_map.pushKV(wtxid_hex, std::move(result_inner));
1497 : 6 : continue;
1498 : : }
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 [ + - + - : 154 : result_inner.pushKV("error", it->second.m_state.ToString());
+ - + - ]
1506 : 77 : break;
1507 : 288 : case MempoolAcceptResult::ResultType::VALID:
1508 : 288 : case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
1509 [ + - + - : 576 : result_inner.pushKV("vsize", it->second.m_vsize.value());
+ - + - ]
1510 : 288 : UniValue fees(UniValue::VOBJ);
1511 [ + - + - : 576 : 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 [ + - + - : 404 : fees.pushKV("effective-feerate", ValueFromAmount(tx_result.m_effective_feerate.value().GetFeePerK()));
+ - + - ]
1517 : 202 : UniValue effective_includes_res(UniValue::VARR);
1518 [ + - + + ]: 472 : for (const auto& wtxid : tx_result.m_wtxids_fee_calculations.value()) {
1519 [ + - + - : 270 : effective_includes_res.push_back(wtxid.ToString());
+ - ]
1520 : : }
1521 [ + - + - ]: 404 : fees.pushKV("effective-includes", std::move(effective_includes_res));
1522 : 202 : }
1523 [ + - + - ]: 576 : result_inner.pushKV("fees", std::move(fees));
1524 [ + + ]: 681 : for (const auto& ptx : it->second.m_replaced_transactions) {
1525 [ + - ]: 393 : replaced_txids.insert(ptx->GetHash());
1526 : : }
1527 : 288 : break;
1528 : : }
1529 [ - + + - ]: 1095 : tx_result_map.pushKV(wtxid_hex, std::move(result_inner));
1530 : 371 : }
1531 [ + - + - ]: 224 : rpc_result.pushKV("tx-results", std::move(tx_result_map));
1532 : 224 : UniValue replaced_list(UniValue::VARR);
1533 [ + - + - : 505 : for (const auto& txid : replaced_txids) replaced_list.push_back(txid.ToString());
+ - + + ]
1534 [ + - + - ]: 224 : rpc_result.pushKV("replaced-transactions", std::move(replaced_list));
1535 : 224 : return rpc_result;
1536 : 122 : },
1537 [ + - + - : 25490 : };
+ - + + +
+ - - -
- ]
1538 [ + - + - : 91764 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - -
- ]
1539 : :
1540 : 1325 : void RegisterMempoolRPCCommands(CRPCTable& t)
1541 : : {
1542 : 1325 : static const CRPCCommand commands[]{
1543 [ + - ]: 2420 : {"rawtransactions", &sendrawtransaction},
1544 [ + - ]: 2420 : {"rawtransactions", &getprivatebroadcastinfo},
1545 [ + - ]: 2420 : {"rawtransactions", &abortprivatebroadcast},
1546 [ + - ]: 2420 : {"rawtransactions", &testmempoolaccept},
1547 [ + - ]: 2420 : {"blockchain", &getmempoolancestors},
1548 [ + - ]: 2420 : {"blockchain", &getmempooldescendants},
1549 [ + - ]: 2420 : {"blockchain", &getmempoolentry},
1550 [ + - ]: 2420 : {"blockchain", &getmempoolcluster},
1551 [ + - ]: 2420 : {"blockchain", &gettxspendingprevout},
1552 [ + - ]: 2420 : {"blockchain", &getmempoolinfo},
1553 [ + - ]: 2420 : {"hidden", &getmempoolfeeratediagram},
1554 [ + - ]: 2420 : {"blockchain", &getrawmempool},
1555 [ + - ]: 2420 : {"blockchain", &importmempool},
1556 [ + - ]: 2420 : {"blockchain", &savemempool},
1557 [ + - ]: 2420 : {"hidden", &getorphantxs},
1558 [ + - ]: 2420 : {"rawtransactions", &submitpackage},
1559 [ + + + - : 20685 : };
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - ]
1560 [ + + ]: 22525 : for (const auto& c : commands) {
1561 : 21200 : t.appendCommand(c.name, &c);
1562 : : }
1563 : 1325 : }
|