Bitcoin Core  26.99.0
P2P Digital Currency
mempool.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 
9 
10 #include <chainparams.h>
11 #include <core_io.h>
12 #include <kernel/mempool_entry.h>
14 #include <policy/rbf.h>
15 #include <policy/settings.h>
16 #include <primitives/transaction.h>
17 #include <rpc/server.h>
18 #include <rpc/server_util.h>
19 #include <rpc/util.h>
20 #include <txmempool.h>
21 #include <univalue.h>
22 #include <util/fs.h>
23 #include <util/moneystr.h>
24 #include <util/strencodings.h>
25 #include <util/time.h>
26 
27 #include <utility>
28 
30 
32 using node::MempoolPath;
33 using node::NodeContext;
34 
36 {
37  return RPCHelpMan{"sendrawtransaction",
38  "\nSubmit a raw transaction (serialized, hex-encoded) to local node and network.\n"
39  "\nThe transaction will be sent unconditionally to all peers, so using sendrawtransaction\n"
40  "for manual rebroadcast may degrade privacy by leaking the transaction's origin, as\n"
41  "nodes will normally not rebroadcast non-wallet transactions already in their mempool.\n"
42  "\nA specific exception, RPC_TRANSACTION_ALREADY_IN_CHAIN, may throw if the transaction cannot be added to the mempool.\n"
43  "\nRelated RPCs: createrawtransaction, signrawtransactionwithkey\n",
44  {
45  {"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex string of the raw transaction"},
47  "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
48  "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
49  {"maxburnamount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(0)},
50  "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"
51  "If burning funds through unspendable outputs is desired, increase this value.\n"
52  "This check is based on heuristics and does not guarantee spendability of outputs.\n"},
53  },
54  RPCResult{
55  RPCResult::Type::STR_HEX, "", "The transaction hash in hex"
56  },
58  "\nCreate a transaction\n"
59  + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
60  "Sign the transaction, and get back the hex\n"
61  + HelpExampleCli("signrawtransactionwithwallet", "\"myhex\"") +
62  "\nSend the transaction (signed hex)\n"
63  + HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
64  "\nAs a JSON-RPC call\n"
65  + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
66  },
67  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
68  {
69  const CAmount max_burn_amount = request.params[2].isNull() ? 0 : AmountFromValue(request.params[2]);
70 
72  if (!DecodeHexTx(mtx, request.params[0].get_str())) {
73  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
74  }
75 
76  for (const auto& out : mtx.vout) {
77  if((out.scriptPubKey.IsUnspendable() || !out.scriptPubKey.HasValidOps()) && out.nValue > max_burn_amount) {
79  }
80  }
81 
82  CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
83 
84  const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>(1))};
85 
86  int64_t virtual_size = GetVirtualTransactionSize(*tx);
87  CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
88 
89  std::string err_string;
91  NodeContext& node = EnsureAnyNodeContext(request.context);
92  const TransactionError err = BroadcastTransaction(node, tx, err_string, max_raw_tx_fee, /*relay=*/true, /*wait_callback=*/true);
93  if (TransactionError::OK != err) {
94  throw JSONRPCTransactionError(err, err_string);
95  }
96 
97  return tx->GetHash().GetHex();
98  },
99  };
100 }
101 
103 {
104  return RPCHelpMan{"testmempoolaccept",
105  "\nReturns result of mempool acceptance tests indicating if raw transaction(s) (serialized, hex-encoded) would be accepted by mempool.\n"
106  "\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"
107  "\nIf one transaction fails, other transactions may not be fully validated (the 'allowed' key will be blank).\n"
108  "\nThe maximum number of transactions allowed is " + ToString(MAX_PACKAGE_COUNT) + ".\n"
109  "\nThis checks if transactions violate the consensus or policy rules.\n"
110  "\nSee sendrawtransaction call.\n",
111  {
112  {"rawtxs", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of hex strings of raw transactions.",
113  {
115  },
116  },
118  "Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
119  "/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
120  },
121  RPCResult{
122  RPCResult::Type::ARR, "", "The result of the mempool acceptance test for each raw transaction in the input array.\n"
123  "Returns results for each transaction in the same order they were passed in.\n"
124  "Transactions that cannot be fully validated due to failures in other transactions will not contain an 'allowed' result.\n",
125  {
126  {RPCResult::Type::OBJ, "", "",
127  {
128  {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
129  {RPCResult::Type::STR_HEX, "wtxid", "The transaction witness hash in hex"},
130  {RPCResult::Type::STR, "package-error", /*optional=*/true, "Package validation error, if any (only possible if rawtxs had more than 1 transaction)."},
131  {RPCResult::Type::BOOL, "allowed", /*optional=*/true, "Whether this tx would be accepted to the mempool and pass client-specified maxfeerate. "
132  "If not present, the tx was not fully validated due to a failure in another tx in the list."},
133  {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)"},
134  {RPCResult::Type::OBJ, "fees", /*optional=*/true, "Transaction fees (only present if 'allowed' is true)",
135  {
136  {RPCResult::Type::STR_AMOUNT, "base", "transaction fee in " + CURRENCY_UNIT},
137  {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."},
138  {RPCResult::Type::ARR, "effective-includes", /*optional=*/false, "transactions whose fees and vsizes are included in effective-feerate.",
139  {RPCResult{RPCResult::Type::STR_HEX, "", "transaction wtxid in hex"},
140  }},
141  }},
142  {RPCResult::Type::STR, "reject-reason", /*optional=*/true, "Rejection string (only present when 'allowed' is false)"},
143  }},
144  }
145  },
146  RPCExamples{
147  "\nCreate a transaction\n"
148  + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
149  "Sign the transaction, and get back the hex\n"
150  + HelpExampleCli("signrawtransactionwithwallet", "\"myhex\"") +
151  "\nTest acceptance of the transaction (signed hex)\n"
152  + HelpExampleCli("testmempoolaccept", R"('["signedhex"]')") +
153  "\nAs a JSON-RPC call\n"
154  + HelpExampleRpc("testmempoolaccept", "[\"signedhex\"]")
155  },
156  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
157  {
158  const UniValue raw_transactions = request.params[0].get_array();
159  if (raw_transactions.size() < 1 || raw_transactions.size() > MAX_PACKAGE_COUNT) {
161  "Array must contain between 1 and " + ToString(MAX_PACKAGE_COUNT) + " transactions.");
162  }
163 
164  const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>(1))};
165 
166  std::vector<CTransactionRef> txns;
167  txns.reserve(raw_transactions.size());
168  for (const auto& rawtx : raw_transactions.getValues()) {
170  if (!DecodeHexTx(mtx, rawtx.get_str())) {
172  "TX decode failed: " + rawtx.get_str() + " Make sure the tx has at least one input.");
173  }
174  txns.emplace_back(MakeTransactionRef(std::move(mtx)));
175  }
176 
177  NodeContext& node = EnsureAnyNodeContext(request.context);
178  CTxMemPool& mempool = EnsureMemPool(node);
180  Chainstate& chainstate = chainman.ActiveChainstate();
181  const PackageMempoolAcceptResult package_result = [&] {
182  LOCK(::cs_main);
183  if (txns.size() > 1) return ProcessNewPackage(chainstate, mempool, txns, /*test_accept=*/true);
184  return PackageMempoolAcceptResult(txns[0]->GetWitnessHash(),
185  chainman.ProcessTransaction(txns[0], /*test_accept=*/true));
186  }();
187 
188  UniValue rpc_result(UniValue::VARR);
189  // We will check transaction fees while we iterate through txns in order. If any transaction fee
190  // exceeds maxfeerate, we will leave the rest of the validation results blank, because it
191  // doesn't make sense to return a validation result for a transaction if its ancestor(s) would
192  // not be submitted.
193  bool exit_early{false};
194  for (const auto& tx : txns) {
195  UniValue result_inner(UniValue::VOBJ);
196  result_inner.pushKV("txid", tx->GetHash().GetHex());
197  result_inner.pushKV("wtxid", tx->GetWitnessHash().GetHex());
198  if (package_result.m_state.GetResult() == PackageValidationResult::PCKG_POLICY) {
199  result_inner.pushKV("package-error", package_result.m_state.ToString());
200  }
201  auto it = package_result.m_tx_results.find(tx->GetWitnessHash());
202  if (exit_early || it == package_result.m_tx_results.end()) {
203  // Validation unfinished. Just return the txid and wtxid.
204  rpc_result.push_back(result_inner);
205  continue;
206  }
207  const auto& tx_result = it->second;
208  // Package testmempoolaccept doesn't allow transactions to already be in the mempool.
210  if (tx_result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
211  const CAmount fee = tx_result.m_base_fees.value();
212  // Check that fee does not exceed maximum fee
213  const int64_t virtual_size = tx_result.m_vsize.value();
214  const CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
215  if (max_raw_tx_fee && fee > max_raw_tx_fee) {
216  result_inner.pushKV("allowed", false);
217  result_inner.pushKV("reject-reason", "max-fee-exceeded");
218  exit_early = true;
219  } else {
220  // Only return the fee and vsize if the transaction would pass ATMP.
221  // These can be used to calculate the feerate.
222  result_inner.pushKV("allowed", true);
223  result_inner.pushKV("vsize", virtual_size);
224  UniValue fees(UniValue::VOBJ);
225  fees.pushKV("base", ValueFromAmount(fee));
226  fees.pushKV("effective-feerate", ValueFromAmount(tx_result.m_effective_feerate.value().GetFeePerK()));
227  UniValue effective_includes_res(UniValue::VARR);
228  for (const auto& wtxid : tx_result.m_wtxids_fee_calculations.value()) {
229  effective_includes_res.push_back(wtxid.ToString());
230  }
231  fees.pushKV("effective-includes", effective_includes_res);
232  result_inner.pushKV("fees", fees);
233  }
234  } else {
235  result_inner.pushKV("allowed", false);
236  const TxValidationState state = tx_result.m_state;
238  result_inner.pushKV("reject-reason", "missing-inputs");
239  } else {
240  result_inner.pushKV("reject-reason", state.GetRejectReason());
241  }
242  }
243  rpc_result.push_back(result_inner);
244  }
245  return rpc_result;
246  },
247  };
248 }
249 
250 static std::vector<RPCResult> MempoolEntryDescription()
251 {
252  return {
253  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."},
254  RPCResult{RPCResult::Type::NUM, "weight", "transaction weight as defined in BIP 141."},
255  RPCResult{RPCResult::Type::NUM_TIME, "time", "local time transaction entered pool in seconds since 1 Jan 1970 GMT"},
256  RPCResult{RPCResult::Type::NUM, "height", "block height when transaction entered pool"},
257  RPCResult{RPCResult::Type::NUM, "descendantcount", "number of in-mempool descendant transactions (including this one)"},
258  RPCResult{RPCResult::Type::NUM, "descendantsize", "virtual transaction size of in-mempool descendants (including this one)"},
259  RPCResult{RPCResult::Type::NUM, "ancestorcount", "number of in-mempool ancestor transactions (including this one)"},
260  RPCResult{RPCResult::Type::NUM, "ancestorsize", "virtual transaction size of in-mempool ancestors (including this one)"},
261  RPCResult{RPCResult::Type::STR_HEX, "wtxid", "hash of serialized transaction, including witness data"},
262  RPCResult{RPCResult::Type::OBJ, "fees", "",
263  {
264  RPCResult{RPCResult::Type::STR_AMOUNT, "base", "transaction fee, denominated in " + CURRENCY_UNIT},
265  RPCResult{RPCResult::Type::STR_AMOUNT, "modified", "transaction fee with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
266  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},
267  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},
268  }},
269  RPCResult{RPCResult::Type::ARR, "depends", "unconfirmed transactions used as inputs for this transaction",
270  {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "parent transaction id"}}},
271  RPCResult{RPCResult::Type::ARR, "spentby", "unconfirmed transactions spending outputs from this transaction",
272  {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "child transaction id"}}},
273  RPCResult{RPCResult::Type::BOOL, "bip125-replaceable", "Whether this transaction signals BIP125 replaceability or has an unconfirmed ancestor signaling BIP125 replaceability.\n"},
274  RPCResult{RPCResult::Type::BOOL, "unbroadcast", "Whether this transaction is currently unbroadcast (initial broadcast not yet acknowledged by any peers)"},
275  };
276 }
277 
278 static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
279 {
280  AssertLockHeld(pool.cs);
281 
282  info.pushKV("vsize", (int)e.GetTxSize());
283  info.pushKV("weight", (int)e.GetTxWeight());
284  info.pushKV("time", count_seconds(e.GetTime()));
285  info.pushKV("height", (int)e.GetHeight());
286  info.pushKV("descendantcount", e.GetCountWithDescendants());
287  info.pushKV("descendantsize", e.GetSizeWithDescendants());
288  info.pushKV("ancestorcount", e.GetCountWithAncestors());
289  info.pushKV("ancestorsize", e.GetSizeWithAncestors());
290  info.pushKV("wtxid", e.GetTx().GetWitnessHash().ToString());
291 
292  UniValue fees(UniValue::VOBJ);
293  fees.pushKV("base", ValueFromAmount(e.GetFee()));
294  fees.pushKV("modified", ValueFromAmount(e.GetModifiedFee()));
295  fees.pushKV("ancestor", ValueFromAmount(e.GetModFeesWithAncestors()));
296  fees.pushKV("descendant", ValueFromAmount(e.GetModFeesWithDescendants()));
297  info.pushKV("fees", fees);
298 
299  const CTransaction& tx = e.GetTx();
300  std::set<std::string> setDepends;
301  for (const CTxIn& txin : tx.vin)
302  {
303  if (pool.exists(GenTxid::Txid(txin.prevout.hash)))
304  setDepends.insert(txin.prevout.hash.ToString());
305  }
306 
307  UniValue depends(UniValue::VARR);
308  for (const std::string& dep : setDepends)
309  {
310  depends.push_back(dep);
311  }
312 
313  info.pushKV("depends", depends);
314 
315  UniValue spent(UniValue::VARR);
316  for (const CTxMemPoolEntry& child : e.GetMemPoolChildrenConst()) {
317  spent.push_back(child.GetTx().GetHash().ToString());
318  }
319 
320  info.pushKV("spentby", spent);
321 
322  // Add opt-in RBF status
323  bool rbfStatus = false;
324  RBFTransactionState rbfState = IsRBFOptIn(tx, pool);
325  if (rbfState == RBFTransactionState::UNKNOWN) {
326  throw JSONRPCError(RPC_MISC_ERROR, "Transaction is not in mempool");
327  } else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125) {
328  rbfStatus = true;
329  }
330 
331  info.pushKV("bip125-replaceable", rbfStatus);
332  info.pushKV("unbroadcast", pool.IsUnbroadcastTx(tx.GetHash()));
333 }
334 
335 UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
336 {
337  if (verbose) {
338  if (include_mempool_sequence) {
339  throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
340  }
341  LOCK(pool.cs);
343  for (const CTxMemPoolEntry& e : pool.entryAll()) {
344  UniValue info(UniValue::VOBJ);
345  entryToJSON(pool, info, e);
346  // Mempool has unique entries so there is no advantage in using
347  // UniValue::pushKV, which checks if the key already exists in O(N).
348  // UniValue::pushKVEnd is used instead which currently is O(1).
349  o.pushKVEnd(e.GetTx().GetHash().ToString(), info);
350  }
351  return o;
352  } else {
354  uint64_t mempool_sequence;
355  {
356  LOCK(pool.cs);
357  for (const CTxMemPoolEntry& e : pool.entryAll()) {
358  a.push_back(e.GetTx().GetHash().ToString());
359  }
360  mempool_sequence = pool.GetSequence();
361  }
362  if (!include_mempool_sequence) {
363  return a;
364  } else {
366  o.pushKV("txids", a);
367  o.pushKV("mempool_sequence", mempool_sequence);
368  return o;
369  }
370  }
371 }
372 
374 {
375  return RPCHelpMan{"getrawmempool",
376  "\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n"
377  "\nHint: use getmempoolentry to fetch a specific transaction from the mempool.\n",
378  {
379  {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
380  {"mempool_sequence", RPCArg::Type::BOOL, RPCArg::Default{false}, "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
381  },
382  {
383  RPCResult{"for verbose = false",
384  RPCResult::Type::ARR, "", "",
385  {
386  {RPCResult::Type::STR_HEX, "", "The transaction id"},
387  }},
388  RPCResult{"for verbose = true",
389  RPCResult::Type::OBJ_DYN, "", "",
390  {
391  {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
392  }},
393  RPCResult{"for verbose = false and mempool_sequence = true",
394  RPCResult::Type::OBJ, "", "",
395  {
396  {RPCResult::Type::ARR, "txids", "",
397  {
398  {RPCResult::Type::STR_HEX, "", "The transaction id"},
399  }},
400  {RPCResult::Type::NUM, "mempool_sequence", "The mempool sequence value."},
401  }},
402  },
403  RPCExamples{
404  HelpExampleCli("getrawmempool", "true")
405  + HelpExampleRpc("getrawmempool", "true")
406  },
407  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
408 {
409  bool fVerbose = false;
410  if (!request.params[0].isNull())
411  fVerbose = request.params[0].get_bool();
412 
413  bool include_mempool_sequence = false;
414  if (!request.params[1].isNull()) {
415  include_mempool_sequence = request.params[1].get_bool();
416  }
417 
418  return MempoolToJSON(EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
419 },
420  };
421 }
422 
424 {
425  return RPCHelpMan{"getmempoolancestors",
426  "\nIf txid is in the mempool, returns all in-mempool ancestors.\n",
427  {
428  {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
429  {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
430  },
431  {
432  RPCResult{"for verbose = false",
433  RPCResult::Type::ARR, "", "",
434  {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool ancestor transaction"}}},
435  RPCResult{"for verbose = true",
436  RPCResult::Type::OBJ_DYN, "", "",
437  {
438  {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
439  }},
440  },
441  RPCExamples{
442  HelpExampleCli("getmempoolancestors", "\"mytxid\"")
443  + HelpExampleRpc("getmempoolancestors", "\"mytxid\"")
444  },
445  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
446 {
447  bool fVerbose = false;
448  if (!request.params[1].isNull())
449  fVerbose = request.params[1].get_bool();
450 
451  uint256 hash = ParseHashV(request.params[0], "parameter 1");
452 
453  const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
454  LOCK(mempool.cs);
455 
456  const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
457  if (entry == nullptr) {
458  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
459  }
460 
461  auto ancestors{mempool.AssumeCalculateMemPoolAncestors(self.m_name, *entry, CTxMemPool::Limits::NoLimits(), /*fSearchForParents=*/false)};
462 
463  if (!fVerbose) {
465  for (CTxMemPool::txiter ancestorIt : ancestors) {
466  o.push_back(ancestorIt->GetTx().GetHash().ToString());
467  }
468  return o;
469  } else {
471  for (CTxMemPool::txiter ancestorIt : ancestors) {
472  const CTxMemPoolEntry &e = *ancestorIt;
473  const uint256& _hash = e.GetTx().GetHash();
474  UniValue info(UniValue::VOBJ);
475  entryToJSON(mempool, info, e);
476  o.pushKV(_hash.ToString(), info);
477  }
478  return o;
479  }
480 },
481  };
482 }
483 
485 {
486  return RPCHelpMan{"getmempooldescendants",
487  "\nIf txid is in the mempool, returns all in-mempool descendants.\n",
488  {
489  {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
490  {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
491  },
492  {
493  RPCResult{"for verbose = false",
494  RPCResult::Type::ARR, "", "",
495  {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool descendant transaction"}}},
496  RPCResult{"for verbose = true",
497  RPCResult::Type::OBJ_DYN, "", "",
498  {
499  {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
500  }},
501  },
502  RPCExamples{
503  HelpExampleCli("getmempooldescendants", "\"mytxid\"")
504  + HelpExampleRpc("getmempooldescendants", "\"mytxid\"")
505  },
506  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
507 {
508  bool fVerbose = false;
509  if (!request.params[1].isNull())
510  fVerbose = request.params[1].get_bool();
511 
512  uint256 hash = ParseHashV(request.params[0], "parameter 1");
513 
514  const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
515  LOCK(mempool.cs);
516 
517  const auto it{mempool.GetIter(hash)};
518  if (!it) {
519  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
520  }
521 
522  CTxMemPool::setEntries setDescendants;
523  mempool.CalculateDescendants(*it, setDescendants);
524  // CTxMemPool::CalculateDescendants will include the given tx
525  setDescendants.erase(*it);
526 
527  if (!fVerbose) {
529  for (CTxMemPool::txiter descendantIt : setDescendants) {
530  o.push_back(descendantIt->GetTx().GetHash().ToString());
531  }
532 
533  return o;
534  } else {
536  for (CTxMemPool::txiter descendantIt : setDescendants) {
537  const CTxMemPoolEntry &e = *descendantIt;
538  const uint256& _hash = e.GetTx().GetHash();
539  UniValue info(UniValue::VOBJ);
540  entryToJSON(mempool, info, e);
541  o.pushKV(_hash.ToString(), info);
542  }
543  return o;
544  }
545 },
546  };
547 }
548 
550 {
551  return RPCHelpMan{"getmempoolentry",
552  "\nReturns mempool data for given transaction\n",
553  {
554  {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
555  },
556  RPCResult{
558  RPCExamples{
559  HelpExampleCli("getmempoolentry", "\"mytxid\"")
560  + HelpExampleRpc("getmempoolentry", "\"mytxid\"")
561  },
562  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
563 {
564  uint256 hash = ParseHashV(request.params[0], "parameter 1");
565 
566  const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
567  LOCK(mempool.cs);
568 
569  const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
570  if (entry == nullptr) {
571  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
572  }
573 
574  UniValue info(UniValue::VOBJ);
575  entryToJSON(mempool, info, *entry);
576  return info;
577 },
578  };
579 }
580 
582 {
583  return RPCHelpMan{"gettxspendingprevout",
584  "Scans the mempool to find transactions spending any of the given outputs",
585  {
586  {"outputs", RPCArg::Type::ARR, RPCArg::Optional::NO, "The transaction outputs that we want to check, and within each, the txid (string) vout (numeric).",
587  {
589  {
590  {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
591  {"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The output number"},
592  },
593  },
594  },
595  },
596  },
597  RPCResult{
598  RPCResult::Type::ARR, "", "",
599  {
600  {RPCResult::Type::OBJ, "", "",
601  {
602  {RPCResult::Type::STR_HEX, "txid", "the transaction id of the checked output"},
603  {RPCResult::Type::NUM, "vout", "the vout value of the checked output"},
604  {RPCResult::Type::STR_HEX, "spendingtxid", /*optional=*/true, "the transaction id of the mempool transaction spending this output (omitted if unspent)"},
605  }},
606  }
607  },
608  RPCExamples{
609  HelpExampleCli("gettxspendingprevout", "\"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":3}]\"")
610  + HelpExampleRpc("gettxspendingprevout", "\"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":3}]\"")
611  },
612  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
613  {
614  const UniValue& output_params = request.params[0].get_array();
615  if (output_params.empty()) {
616  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, outputs are missing");
617  }
618 
619  std::vector<COutPoint> prevouts;
620  prevouts.reserve(output_params.size());
621 
622  for (unsigned int idx = 0; idx < output_params.size(); idx++) {
623  const UniValue& o = output_params[idx].get_obj();
624 
625  RPCTypeCheckObj(o,
626  {
627  {"txid", UniValueType(UniValue::VSTR)},
628  {"vout", UniValueType(UniValue::VNUM)},
629  }, /*fAllowNull=*/false, /*fStrict=*/true);
630 
631  const Txid txid = Txid::FromUint256(ParseHashO(o, "txid"));
632  const int nOutput{o.find_value("vout").getInt<int>()};
633  if (nOutput < 0) {
634  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
635  }
636 
637  prevouts.emplace_back(txid, nOutput);
638  }
639 
640  const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
641  LOCK(mempool.cs);
642 
643  UniValue result{UniValue::VARR};
644 
645  for (const COutPoint& prevout : prevouts) {
647  o.pushKV("txid", prevout.hash.ToString());
648  o.pushKV("vout", (uint64_t)prevout.n);
649 
650  const CTransaction* spendingTx = mempool.GetConflictTx(prevout);
651  if (spendingTx != nullptr) {
652  o.pushKV("spendingtxid", spendingTx->GetHash().ToString());
653  }
654 
655  result.push_back(o);
656  }
657 
658  return result;
659  },
660  };
661 }
662 
664 {
665  // Make sure this call is atomic in the pool.
666  LOCK(pool.cs);
668  ret.pushKV("loaded", pool.GetLoadTried());
669  ret.pushKV("size", (int64_t)pool.size());
670  ret.pushKV("bytes", (int64_t)pool.GetTotalTxSize());
671  ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
672  ret.pushKV("total_fee", ValueFromAmount(pool.GetTotalFee()));
673  ret.pushKV("maxmempool", pool.m_max_size_bytes);
674  ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(), pool.m_min_relay_feerate).GetFeePerK()));
675  ret.pushKV("minrelaytxfee", ValueFromAmount(pool.m_min_relay_feerate.GetFeePerK()));
676  ret.pushKV("incrementalrelayfee", ValueFromAmount(pool.m_incremental_relay_feerate.GetFeePerK()));
677  ret.pushKV("unbroadcastcount", uint64_t{pool.GetUnbroadcastTxs().size()});
678  ret.pushKV("fullrbf", pool.m_full_rbf);
679  return ret;
680 }
681 
683 {
684  return RPCHelpMan{"getmempoolinfo",
685  "Returns details on the active state of the TX memory pool.",
686  {},
687  RPCResult{
688  RPCResult::Type::OBJ, "", "",
689  {
690  {RPCResult::Type::BOOL, "loaded", "True if the initial load attempt of the persisted mempool finished"},
691  {RPCResult::Type::NUM, "size", "Current tx count"},
692  {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"},
693  {RPCResult::Type::NUM, "usage", "Total memory usage for the mempool"},
694  {RPCResult::Type::STR_AMOUNT, "total_fee", "Total fees for the mempool in " + CURRENCY_UNIT + ", ignoring modified fees through prioritisetransaction"},
695  {RPCResult::Type::NUM, "maxmempool", "Maximum memory usage for the mempool"},
696  {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"},
697  {RPCResult::Type::STR_AMOUNT, "minrelaytxfee", "Current minimum relay fee for transactions"},
698  {RPCResult::Type::NUM, "incrementalrelayfee", "minimum fee rate increment for mempool limiting or replacement in " + CURRENCY_UNIT + "/kvB"},
699  {RPCResult::Type::NUM, "unbroadcastcount", "Current number of transactions that haven't passed initial broadcast yet"},
700  {RPCResult::Type::BOOL, "fullrbf", "True if the mempool accepts RBF without replaceability signaling inspection"},
701  }},
702  RPCExamples{
703  HelpExampleCli("getmempoolinfo", "")
704  + HelpExampleRpc("getmempoolinfo", "")
705  },
706  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
707 {
708  return MempoolInfoToJSON(EnsureAnyMemPool(request.context));
709 },
710  };
711 }
712 
714 {
715  return RPCHelpMan{
716  "importmempool",
717  "Import a mempool.dat file and attempt to add its contents to the mempool.\n"
718  "Warning: Importing untrusted files is dangerous, especially if metadata from the file is taken over.",
719  {
720  {"filepath", RPCArg::Type::STR, RPCArg::Optional::NO, "The mempool file"},
721  {"options",
724  "",
725  {
726  {"use_current_time", RPCArg::Type::BOOL, RPCArg::Default{true},
727  "Whether to use the current system time or use the entry time metadata from the mempool file.\n"
728  "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior."},
729  {"apply_fee_delta_priority", RPCArg::Type::BOOL, RPCArg::Default{false},
730  "Whether to apply the fee delta metadata from the mempool file.\n"
731  "It will be added to any existing fee deltas.\n"
732  "The fee delta can be set by the prioritisetransaction RPC.\n"
733  "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior.\n"
734  "Only set this bool if you understand what it does."},
735  {"apply_unbroadcast_set", RPCArg::Type::BOOL, RPCArg::Default{false},
736  "Whether to apply the unbroadcast set metadata from the mempool file.\n"
737  "Warning: Importing untrusted metadata may lead to unexpected issues and undesirable behavior."},
738  },
739  RPCArgOptions{.oneline_description = "options"}},
740  },
741  RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
742  RPCExamples{HelpExampleCli("importmempool", "/path/to/mempool.dat") + HelpExampleRpc("importmempool", "/path/to/mempool.dat")},
743  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
744  const NodeContext& node{EnsureAnyNodeContext(request.context)};
745 
746  CTxMemPool& mempool{EnsureMemPool(node)};
748  Chainstate& chainstate = chainman.ActiveChainstate();
749 
750  if (chainman.IsInitialBlockDownload()) {
751  throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Can only import the mempool after the block download and sync is done.");
752  }
753 
754  const fs::path load_path{fs::u8path(request.params[0].get_str())};
755  const UniValue& use_current_time{request.params[1]["use_current_time"]};
756  const UniValue& apply_fee_delta{request.params[1]["apply_fee_delta_priority"]};
757  const UniValue& apply_unbroadcast{request.params[1]["apply_unbroadcast_set"]};
759  .use_current_time = use_current_time.isNull() ? true : use_current_time.get_bool(),
760  .apply_fee_delta_priority = apply_fee_delta.isNull() ? false : apply_fee_delta.get_bool(),
761  .apply_unbroadcast_set = apply_unbroadcast.isNull() ? false : apply_unbroadcast.get_bool(),
762  };
763 
764  if (!kernel::LoadMempool(mempool, load_path, chainstate, std::move(opts))) {
765  throw JSONRPCError(RPC_MISC_ERROR, "Unable to import mempool file, see debug.log for details.");
766  }
767 
769  return ret;
770  },
771  };
772 }
773 
775 {
776  return RPCHelpMan{"savemempool",
777  "\nDumps the mempool to disk. It will fail until the previous dump is fully loaded.\n",
778  {},
779  RPCResult{
780  RPCResult::Type::OBJ, "", "",
781  {
782  {RPCResult::Type::STR, "filename", "the directory and file where the mempool was saved"},
783  }},
784  RPCExamples{
785  HelpExampleCli("savemempool", "")
786  + HelpExampleRpc("savemempool", "")
787  },
788  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
789 {
790  const ArgsManager& args{EnsureAnyArgsman(request.context)};
791  const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
792 
793  if (!mempool.GetLoadTried()) {
794  throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
795  }
796 
797  const fs::path& dump_path = MempoolPath(args);
798 
799  if (!DumpMempool(mempool, dump_path)) {
800  throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
801  }
802 
804  ret.pushKV("filename", dump_path.utf8string());
805 
806  return ret;
807 },
808  };
809 }
810 
812 {
813  return RPCHelpMan{"submitpackage",
814  "Submit a package of raw transactions (serialized, hex-encoded) to local node.\n"
815  "The package must consist of a child with its parents, and none of the parents may depend on one another.\n"
816  "The package will be validated according to consensus and mempool policy rules. If any transaction passes, it will be accepted to mempool.\n"
817  "This RPC is experimental and the interface may be unstable. Refer to doc/policy/packages.md for documentation on package policies.\n"
818  "Warning: successful submission does not mean the transactions will propagate throughout the network.\n"
819  ,
820  {
821  {"package", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of raw transactions.",
822  {
824  },
825  },
826  },
827  RPCResult{
828  RPCResult::Type::OBJ, "", "",
829  {
830  {RPCResult::Type::STR, "package_msg", "The transaction package result message. \"success\" indicates all transactions were accepted into or are already in the mempool."},
831  {RPCResult::Type::OBJ_DYN, "tx-results", "transaction results keyed by wtxid",
832  {
833  {RPCResult::Type::OBJ, "wtxid", "transaction wtxid", {
834  {RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
835  {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."},
836  {RPCResult::Type::NUM, "vsize", /*optional=*/true, "Sigops-adjusted virtual transaction size."},
837  {RPCResult::Type::OBJ, "fees", /*optional=*/true, "Transaction fees", {
838  {RPCResult::Type::STR_AMOUNT, "base", "transaction fee in " + CURRENCY_UNIT},
839  {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."},
840  {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.",
841  {{RPCResult::Type::STR_HEX, "", "transaction wtxid in hex"},
842  }},
843  }},
844  {RPCResult::Type::STR, "error", /*optional=*/true, "The transaction error string, if it was rejected by the mempool"},
845  }}
846  }},
847  {RPCResult::Type::ARR, "replaced-transactions", /*optional=*/true, "List of txids of replaced transactions",
848  {
849  {RPCResult::Type::STR_HEX, "", "The transaction id"},
850  }},
851  },
852  },
853  RPCExamples{
854  HelpExampleCli("testmempoolaccept", "[rawtx1, rawtx2]") +
855  HelpExampleCli("submitpackage", "[rawtx1, rawtx2]")
856  },
857  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
858  {
859  const UniValue raw_transactions = request.params[0].get_array();
860  if (raw_transactions.size() < 1 || raw_transactions.size() > MAX_PACKAGE_COUNT) {
862  "Array must contain between 1 and " + ToString(MAX_PACKAGE_COUNT) + " transactions.");
863  }
864 
865  std::vector<CTransactionRef> txns;
866  txns.reserve(raw_transactions.size());
867  for (const auto& rawtx : raw_transactions.getValues()) {
869  if (!DecodeHexTx(mtx, rawtx.get_str())) {
871  "TX decode failed: " + rawtx.get_str() + " Make sure the tx has at least one input.");
872  }
873  txns.emplace_back(MakeTransactionRef(std::move(mtx)));
874  }
875  if (!IsChildWithParentsTree(txns)) {
876  throw JSONRPCTransactionError(TransactionError::INVALID_PACKAGE, "package topology disallowed. not child-with-parents or parents depend on each other.");
877  }
878 
879  NodeContext& node = EnsureAnyNodeContext(request.context);
880  CTxMemPool& mempool = EnsureMemPool(node);
882  const auto package_result = WITH_LOCK(::cs_main, return ProcessNewPackage(chainstate, mempool, txns, /*test_accept=*/ false));
883 
884  std::string package_msg = "success";
885 
886  // First catch package-wide errors, continue if we can
887  switch(package_result.m_state.GetResult()) {
889  {
890  // Belt-and-suspenders check; everything should be successful here
891  CHECK_NONFATAL(package_result.m_tx_results.size() == txns.size());
892  for (const auto& tx : txns) {
893  CHECK_NONFATAL(mempool.exists(GenTxid::Txid(tx->GetHash())));
894  }
895  break;
896  }
898  {
899  // This only happens with internal bug; user should stop and report
901  package_result.m_state.GetRejectReason());
902  }
905  {
906  // Package-wide error we want to return, but we also want to return individual responses
907  package_msg = package_result.m_state.ToString();
908  CHECK_NONFATAL(package_result.m_tx_results.size() == txns.size() ||
909  package_result.m_tx_results.empty());
910  break;
911  }
912  }
913 
914  size_t num_broadcast{0};
915  for (const auto& tx : txns) {
916  // We don't want to re-submit the txn for validation in BroadcastTransaction
917  if (!mempool.exists(GenTxid::Txid(tx->GetHash()))) {
918  continue;
919  }
920 
921  // We do not expect an error here; we are only broadcasting things already/still in mempool
922  std::string err_string;
923  const auto err = BroadcastTransaction(node, tx, err_string, /*max_tx_fee=*/0, /*relay=*/true, /*wait_callback=*/true);
924  if (err != TransactionError::OK) {
925  throw JSONRPCTransactionError(err,
926  strprintf("transaction broadcast failed: %s (%d transactions were broadcast successfully)",
927  err_string, num_broadcast));
928  }
929  num_broadcast++;
930  }
931 
932  UniValue rpc_result{UniValue::VOBJ};
933  rpc_result.pushKV("package_msg", package_msg);
934  UniValue tx_result_map{UniValue::VOBJ};
935  std::set<uint256> replaced_txids;
936  for (const auto& tx : txns) {
937  UniValue result_inner{UniValue::VOBJ};
938  result_inner.pushKV("txid", tx->GetHash().GetHex());
939  auto it = package_result.m_tx_results.find(tx->GetWitnessHash());
940  if (it == package_result.m_tx_results.end()) {
941  // No results, report error and continue
942  result_inner.pushKV("error", "unevaluated");
943  continue;
944  }
945  const auto& tx_result = it->second;
946  switch(it->second.m_result_type) {
948  result_inner.pushKV("other-wtxid", it->second.m_other_wtxid.value().GetHex());
949  break;
951  result_inner.pushKV("error", it->second.m_state.ToString());
952  break;
955  result_inner.pushKV("vsize", int64_t{it->second.m_vsize.value()});
956  UniValue fees(UniValue::VOBJ);
957  fees.pushKV("base", ValueFromAmount(it->second.m_base_fees.value()));
958  if (tx_result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
959  // Effective feerate is not provided for MEMPOOL_ENTRY transactions even
960  // though modified fees is known, because it is unknown whether package
961  // feerate was used when it was originally submitted.
962  fees.pushKV("effective-feerate", ValueFromAmount(tx_result.m_effective_feerate.value().GetFeePerK()));
963  UniValue effective_includes_res(UniValue::VARR);
964  for (const auto& wtxid : tx_result.m_wtxids_fee_calculations.value()) {
965  effective_includes_res.push_back(wtxid.ToString());
966  }
967  fees.pushKV("effective-includes", effective_includes_res);
968  }
969  result_inner.pushKV("fees", fees);
970  if (it->second.m_replaced_transactions.has_value()) {
971  for (const auto& ptx : it->second.m_replaced_transactions.value()) {
972  replaced_txids.insert(ptx->GetHash());
973  }
974  }
975  break;
976  }
977  tx_result_map.pushKV(tx->GetWitnessHash().GetHex(), result_inner);
978  }
979  rpc_result.pushKV("tx-results", tx_result_map);
980  UniValue replaced_list(UniValue::VARR);
981  for (const uint256& hash : replaced_txids) replaced_list.push_back(hash.ToString());
982  rpc_result.pushKV("replaced-transactions", replaced_list);
983  return rpc_result;
984  },
985  };
986 }
987 
989 {
990  static const CRPCCommand commands[]{
991  {"rawtransactions", &sendrawtransaction},
992  {"rawtransactions", &testmempoolaccept},
993  {"blockchain", &getmempoolancestors},
994  {"blockchain", &getmempooldescendants},
995  {"blockchain", &getmempoolentry},
996  {"blockchain", &gettxspendingprevout},
997  {"blockchain", &getmempoolinfo},
998  {"blockchain", &getrawmempool},
999  {"blockchain", &importmempool},
1000  {"blockchain", &savemempool},
1001  {"rawtransactions", &submitpackage},
1002  };
1003  for (const auto& c : commands) {
1004  t.appendCommand(c.name, &c);
1005  }
1006 }
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int ret
static CAmount AmountFromValue(const UniValue &value)
Definition: bitcoin-tx.cpp:555
ArgsManager & args
Definition: bitcoind.cpp:268
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:73
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition: feerate.h:65
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Txid hash
Definition: transaction.h:31
RPC command dispatcher.
Definition: server.h:133
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:343
const std::vector< CTxIn > vin
Definition: transaction.h:306
An input of a transaction.
Definition: transaction.h:67
COutPoint prevout
Definition: transaction.h:69
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
const CTransaction & GetTx() const
const Children & GetMemPoolChildrenConst() const
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:300
setEntries AssumeCalculateMemPoolAncestors(std::string_view calling_fn_name, const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries.
Definition: txmempool.cpp:269
bool GetLoadTried() const
Definition: txmempool.cpp:1200
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:388
CFeeRate GetMinFee(size_t sizelimit) const
Definition: txmempool.cpp:1091
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:946
const bool m_full_rbf
Definition: txmempool.h:445
const int64_t m_max_size_bytes
Definition: txmempool.h:437
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1023
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:394
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:714
const CFeeRate m_min_relay_feerate
Definition: txmempool.h:440
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:732
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:391
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:674
const CFeeRate m_incremental_relay_feerate
Definition: txmempool.h:439
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
Definition: txmempool.cpp:940
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:535
unsigned long size() const
Definition: txmempool.h:656
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:810
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:668
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:662
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:836
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:486
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:844
SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
Definition: validation.h:1062
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:434
void push_back(UniValue val)
Definition: univalue.cpp:104
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:233
@ VOBJ
Definition: univalue.h:23
@ VSTR
Definition: univalue.h:23
@ VARR
Definition: univalue.h:23
@ VNUM
Definition: univalue.h:23
bool isNull() const
Definition: univalue.h:78
const UniValue & get_obj() const
size_t size() const
Definition: univalue.h:70
const std::vector< UniValue > & getValues() const
bool empty() const
Definition: univalue.h:68
void pushKVEnd(std::string key, UniValue val)
Definition: univalue.cpp:118
Int getInt() const
Definition: univalue.h:137
const UniValue & get_array() const
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:126
bool get_bool() const
std::string GetRejectReason() const
Definition: validation.h:126
Result GetResult() const
Definition: validation.h:125
std::string ToString() const
Definition: validation.h:128
std::string ToString() const
Definition: uint256.cpp:55
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
std::string utf8string() const
Return a UTF-8 representation of the path as a std::string, for compatibility with code using std::st...
Definition: fs.h:63
std::string ToString() const
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition: uint256.h:106
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
UniValue ValueFromAmount(const CAmount amount)
Definition: core_write.cpp:26
bool DecodeHexTx(CMutableTransaction &tx, const std::string &hex_tx, bool try_no_witness=false, bool try_witness=true)
Definition: core_read.cpp:194
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
TransactionError
Definition: error.h:22
const std::string CURRENCY_UNIT
Definition: feerate.h:17
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
Definition: moneystr.cpp:16
static path u8path(const std::string &utf8_str)
Definition: fs.h:75
bool LoadMempool(CTxMemPool &pool, const fs::path &load_path, Chainstate &active_chainstate, ImportMempoolOptions &&opts)
Import the file and attempt to add its contents to the mempool.
bool DumpMempool(const CTxMemPool &pool, const fs::path &dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
Definition: init.h:25
TransactionError BroadcastTransaction(NodeContext &node, const CTransactionRef tx, std::string &err_string, const CAmount &max_tx_fee, bool relay, bool wait_callback)
Submit a transaction to the mempool and (optionally) relay it to all P2P peers.
Definition: transaction.cpp:33
fs::path MempoolPath(const ArgsManager &argsman)
static const CFeeRate DEFAULT_MAX_RAW_TX_FEE_RATE
Maximum fee rate for sendrawtransaction and testmempoolaccept RPC calls.
Definition: transaction.h:27
bool IsChildWithParentsTree(const Package &package)
Context-free check that a package IsChildWithParents() and none of the parents depend on each other (...
Definition: packages.cpp:136
static constexpr uint32_t MAX_PACKAGE_COUNT
Default maximum number of transactions in a package.
Definition: packages.h:19
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
@ PCKG_RESULT_UNSET
Initial value. The package has not yet been rejected.
@ PCKG_MEMPOOL_ERROR
Mempool logic error.
@ PCKG_TX
At least one tx is invalid.
RBFTransactionState IsRBFOptIn(const CTransaction &tx, const CTxMemPool &pool)
Determine whether an unconfirmed transaction is signaling opt-in to RBF according to BIP 125 This inv...
Definition: rbf.cpp:22
RBFTransactionState
The rbf state of unconfirmed transactions.
Definition: rbf.h:27
@ UNKNOWN
Unconfirmed tx that does not signal rbf and is not in the mempool.
@ REPLACEABLE_BIP125
Either this tx or a mempool ancestor signals rbf.
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:295
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:58
static RPCHelpMan getmempoolinfo()
Definition: mempool.cpp:682
static RPCHelpMan sendrawtransaction()
Definition: mempool.cpp:35
static void entryToJSON(const CTxMemPool &pool, UniValue &info, const CTxMemPoolEntry &e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
Definition: mempool.cpp:278
static RPCHelpMan importmempool()
Definition: mempool.cpp:713
static std::vector< RPCResult > MempoolEntryDescription()
Definition: mempool.cpp:250
void RegisterMempoolRPCCommands(CRPCTable &t)
Definition: mempool.cpp:988
static RPCHelpMan getrawmempool()
Definition: mempool.cpp:373
static RPCHelpMan getmempoolentry()
Definition: mempool.cpp:549
UniValue MempoolInfoToJSON(const CTxMemPool &pool)
Mempool information to JSON.
Definition: mempool.cpp:663
static RPCHelpMan gettxspendingprevout()
Definition: mempool.cpp:581
static RPCHelpMan submitpackage()
Definition: mempool.cpp:811
UniValue MempoolToJSON(const CTxMemPool &pool, bool verbose, bool include_mempool_sequence)
Mempool to JSON.
Definition: mempool.cpp:335
static RPCHelpMan testmempoolaccept()
Definition: mempool.cpp:102
static RPCHelpMan getmempooldescendants()
Definition: mempool.cpp:484
static RPCHelpMan getmempoolancestors()
Definition: mempool.cpp:423
static RPCHelpMan savemempool()
Definition: mempool.cpp:774
@ RPC_MISC_ERROR
General application defined errors.
Definition: protocol.h:39
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
Definition: protocol.h:43
@ RPC_CLIENT_IN_INITIAL_DOWNLOAD
Still downloading initial blocks.
Definition: protocol.h:59
@ RPC_DESERIALIZATION_ERROR
Error parsing or validating structure in raw format.
Definition: protocol.h:45
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition: protocol.h:41
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:155
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
Definition: util.cpp:380
CFeeRate ParseFeeRate(const UniValue &json)
Parse a json number or string, denoting BTC/kvB, into a CFeeRate (sat/kvB).
Definition: util.cpp:82
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:173
uint256 ParseHashO(const UniValue &o, std::string_view strKey)
Definition: util.cpp:98
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
Definition: util.cpp:43
uint256 ParseHashV(const UniValue &v, std::string_view name)
Utilities: convert hex-encoded Values (throws error if not hex).
Definition: util.cpp:89
ArgsManager & EnsureAnyArgsman(const std::any &context)
Definition: server_util.cpp:65
CTxMemPool & EnsureAnyMemPool(const std::any &context)
Definition: server_util.cpp:38
NodeContext & EnsureAnyNodeContext(const std::any &context)
Definition: server_util.cpp:21
CTxMemPool & EnsureMemPool(const NodeContext &node)
Definition: server_util.cpp:30
ChainstateManager & EnsureChainman(const NodeContext &node)
Definition: server_util.cpp:70
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:109
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
@ DIFFERENT_WITNESS
Valid, transaction was already in the mempool.
@ INVALID
Fully validated, valid.
Validation result for package mempool acceptance.
Definition: validation.h:233
PackageValidationState m_state
Definition: validation.h:234
std::map< uint256, MempoolAcceptResult > m_tx_results
Map from wtxid to finished MempoolAcceptResults.
Definition: validation.h:241
@ STR_HEX
Special type that is a STR with only hex chars.
@ AMOUNT
Special type representing a floating point amount (can be either NUM or STR)
@ OBJ_NAMED_PARAMS
Special type that behaves almost exactly like OBJ, defining an options object with a list of pre-defi...
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
@ NO
Required arg.
std::string oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
Definition: util.h:151
@ NUM_TIME
Special numeric to denote unix epoch time.
@ OBJ_DYN
Special dictionary with keys that are not literals.
@ STR_HEX
Special string with only hex chars.
@ STR_AMOUNT
Special string to represent a floating point amount.
Wrapper for UniValue::VType, which includes typeAny: Used to denote don't care type.
Definition: util.h:74
static constexpr MemPoolLimits NoLimits()
NodeContext struct containing references to chain state and connection state.
Definition: context.h:48
#define AssertLockNotHeld(cs)
Definition: sync.h:147
#define LOCK(cs)
Definition: sync.h:257
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:301
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:54
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept)
Validate (and maybe submit) a package to the mempool.
AssertLockHeld(pool.cs)