Bitcoin Core  27.99.0
P2P Digital Currency
txmempool.h
Go to the documentation of this file.
1 // Copyright (c) 2009-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 #ifndef BITCOIN_TXMEMPOOL_H
7 #define BITCOIN_TXMEMPOOL_H
8 
9 #include <coins.h>
10 #include <consensus/amount.h>
11 #include <indirectmap.h>
12 #include <kernel/cs_main.h>
13 #include <kernel/mempool_entry.h> // IWYU pragma: export
14 #include <kernel/mempool_limits.h> // IWYU pragma: export
15 #include <kernel/mempool_options.h> // IWYU pragma: export
16 #include <kernel/mempool_removal_reason.h> // IWYU pragma: export
17 #include <policy/feerate.h>
18 #include <policy/packages.h>
19 #include <primitives/transaction.h>
20 #include <sync.h>
21 #include <util/epochguard.h>
22 #include <util/hasher.h>
23 #include <util/result.h>
24 
25 #include <boost/multi_index/hashed_index.hpp>
26 #include <boost/multi_index/identity.hpp>
27 #include <boost/multi_index/indexed_by.hpp>
28 #include <boost/multi_index/ordered_index.hpp>
29 #include <boost/multi_index/sequenced_index.hpp>
30 #include <boost/multi_index/tag.hpp>
31 #include <boost/multi_index_container.hpp>
32 
33 #include <atomic>
34 #include <map>
35 #include <optional>
36 #include <set>
37 #include <string>
38 #include <string_view>
39 #include <utility>
40 #include <vector>
41 
42 class CChain;
43 class ValidationSignals;
44 
46 static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
47 
52 
53 // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
55 {
58  {
59  return entry.GetTx().GetHash();
60  }
61 
63  {
64  return tx->GetHash();
65  }
66 };
67 
68 // extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
70 {
73  {
74  return entry.GetTx().GetWitnessHash();
75  }
76 
78  {
79  return tx->GetWitnessHash();
80  }
81 };
82 
83 
89 {
90 public:
91  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
92  {
93  double a_mod_fee, a_size, b_mod_fee, b_size;
94 
95  GetModFeeAndSize(a, a_mod_fee, a_size);
96  GetModFeeAndSize(b, b_mod_fee, b_size);
97 
98  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
99  double f1 = a_mod_fee * b_size;
100  double f2 = a_size * b_mod_fee;
101 
102  if (f1 == f2) {
103  return a.GetTime() >= b.GetTime();
104  }
105  return f1 < f2;
106  }
107 
108  // Return the fee/size we're using for sorting this entry.
109  void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
110  {
111  // Compare feerate with descendants to feerate of the transaction, and
112  // return the fee/size for the max.
113  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
114  double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
115 
116  if (f2 > f1) {
117  mod_fee = a.GetModFeesWithDescendants();
118  size = a.GetSizeWithDescendants();
119  } else {
120  mod_fee = a.GetModifiedFee();
121  size = a.GetTxSize();
122  }
123  }
124 };
125 
134 {
135 public:
136  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
137  {
138  double f1 = (double)a.GetFee() * b.GetTxSize();
139  double f2 = (double)b.GetFee() * a.GetTxSize();
140  if (f1 == f2) {
141  return b.GetTx().GetHash() < a.GetTx().GetHash();
142  }
143  return f1 > f2;
144  }
145 };
146 
148 {
149 public:
150  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
151  {
152  return a.GetTime() < b.GetTime();
153  }
154 };
155 
161 {
162 public:
163  template<typename T>
164  bool operator()(const T& a, const T& b) const
165  {
166  double a_mod_fee, a_size, b_mod_fee, b_size;
167 
168  GetModFeeAndSize(a, a_mod_fee, a_size);
169  GetModFeeAndSize(b, b_mod_fee, b_size);
170 
171  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
172  double f1 = a_mod_fee * b_size;
173  double f2 = a_size * b_mod_fee;
174 
175  if (f1 == f2) {
176  return a.GetTx().GetHash() < b.GetTx().GetHash();
177  }
178  return f1 > f2;
179  }
180 
181  // Return the fee/size we're using for sorting this entry.
182  template <typename T>
183  void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
184  {
185  // Compare feerate with ancestors to feerate of the transaction, and
186  // return the fee/size for the min.
187  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
188  double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
189 
190  if (f1 > f2) {
191  mod_fee = a.GetModFeesWithAncestors();
192  size = a.GetSizeWithAncestors();
193  } else {
194  mod_fee = a.GetModifiedFee();
195  size = a.GetTxSize();
196  }
197  }
198 };
199 
200 // Multi_index tag names
202 struct entry_time {};
203 struct ancestor_score {};
204 struct index_by_wtxid {};
205 
210 {
213 
215  std::chrono::seconds m_time;
216 
219 
221  int32_t vsize;
222 
224  int64_t nFeeDelta;
225 };
226 
301 {
302 protected:
303  const int m_check_ratio;
304  std::atomic<unsigned int> nTransactionsUpdated{0};
305 
306  uint64_t totalTxSize GUARDED_BY(cs){0};
307  CAmount m_total_fee GUARDED_BY(cs){0};
308  uint64_t cachedInnerUsage GUARDED_BY(cs){0};
309 
310  mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
311  mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
312  mutable double rollingMinimumFeeRate GUARDED_BY(cs){0};
314 
315  // In-memory counter for external mempool tracking purposes.
316  // This number is incremented once every time a transaction
317  // is added or removed from the mempool for any reason.
318  mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
319 
321 
322  bool m_load_tried GUARDED_BY(cs){false};
323 
324  CFeeRate GetMinFee(size_t sizelimit) const;
325 
326 public:
327 
328  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
329 
330  typedef boost::multi_index_container<
332  boost::multi_index::indexed_by<
333  // sorted by txid
334  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
335  // sorted by wtxid
336  boost::multi_index::hashed_unique<
337  boost::multi_index::tag<index_by_wtxid>,
340  >,
341  // sorted by fee rate
342  boost::multi_index::ordered_non_unique<
343  boost::multi_index::tag<descendant_score>,
344  boost::multi_index::identity<CTxMemPoolEntry>,
346  >,
347  // sorted by entry time
348  boost::multi_index::ordered_non_unique<
349  boost::multi_index::tag<entry_time>,
350  boost::multi_index::identity<CTxMemPoolEntry>,
352  >,
353  // sorted by fee rate with ancestors
354  boost::multi_index::ordered_non_unique<
355  boost::multi_index::tag<ancestor_score>,
356  boost::multi_index::identity<CTxMemPoolEntry>,
358  >
359  >
361 
391 
392  using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
393  std::vector<CTransactionRef> txns_randomized GUARDED_BY(cs);
394 
395  typedef std::set<txiter, CompareIteratorByHash> setEntries;
396 
398 
400 private:
401  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
402 
403 
404  void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
405  void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
406 
407  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
408 
412  std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
413 
414 
427  size_t entry_count,
428  CTxMemPoolEntry::Parents &staged_ancestors,
429  const Limits& limits
430  ) const EXCLUSIVE_LOCKS_REQUIRED(cs);
431 
432 public:
434  std::map<uint256, CAmount> mapDeltas GUARDED_BY(cs);
435 
436  using Options = kernel::MemPoolOptions;
437 
438  const int64_t m_max_size_bytes;
439  const std::chrono::seconds m_expiry;
444  const std::optional<unsigned> m_max_datacarrier_bytes;
445  const bool m_require_standard;
446  const bool m_full_rbf;
447  const bool m_persist_v1_dat;
448 
450 
452 
458  explicit CTxMemPool(const Options& opts);
459 
466  void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
467 
468  // addUnchecked must updated state for all ancestors of a given transaction,
469  // to track size/count of descendant transactions. First version of
470  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
471  // then invoke the second version.
472  // Note that addUnchecked is ONLY called from ATMP outside of tests
473  // and any other callers may break wallet's in-mempool tracking (due to
474  // lack of CValidationInterface::TransactionAddedToMempool callbacks).
477 
486  void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
488  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
489 
490  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
491  bool isSpent(const COutPoint& outpoint) const;
492  unsigned int GetTransactionsUpdated() const;
493  void AddTransactionsUpdated(unsigned int n);
498  bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
499 
501  void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
502  void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
504 
505  struct delta_info {
507  const bool in_mempool;
509  const CAmount delta;
511  std::optional<CAmount> modified_fee;
513  const uint256 txid;
514  };
516  std::vector<delta_info> GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
517 
519  const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
520 
522  std::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
523 
527  setEntries GetIterSet(const std::set<Txid>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
528 
532  std::vector<txiter> GetIterVec(const std::vector<uint256>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
533 
541  void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
542 
557 
571  const Limits& limits,
572  bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
573 
589  std::string_view calling_fn_name,
590  const CTxMemPoolEntry &entry,
591  const Limits& limits,
592  bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
593 
598  std::vector<txiter> GatherClusters(const std::vector<uint256>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
599 
612  util::Result<void> CheckPackageLimits(const Package& package,
613  int64_t total_vsize) const EXCLUSIVE_LOCKS_REQUIRED(cs);
614 
618  void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
619 
626  CFeeRate GetMinFee() const {
627  return GetMinFee(m_max_size_bytes);
628  }
629 
634  void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
635 
637  int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
638 
645  void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
646 
651  bool GetLoadTried() const;
652 
657  void SetLoadTried(bool load_tried);
658 
659  unsigned long size() const
660  {
661  LOCK(cs);
662  return mapTx.size();
663  }
664 
666  {
668  return totalTxSize;
669  }
670 
672  {
674  return m_total_fee;
675  }
676 
677  bool exists(const GenTxid& gtxid) const
678  {
679  LOCK(cs);
680  if (gtxid.IsWtxid()) {
681  return (mapTx.get<index_by_wtxid>().count(gtxid.GetHash()) != 0);
682  }
683  return (mapTx.count(gtxid.GetHash()) != 0);
684  }
685 
687 
688  CTransactionRef get(const uint256& hash) const;
690  {
692  return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
693  }
694  TxMempoolInfo info(const GenTxid& gtxid) const;
695 
697  TxMempoolInfo info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const;
698 
699  std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs);
700  std::vector<TxMempoolInfo> infoAll() const;
701 
702  size_t DynamicMemoryUsage() const;
703 
705  void AddUnbroadcastTx(const uint256& txid)
706  {
707  LOCK(cs);
708  // Sanity check the transaction is in the mempool & insert into
709  // unbroadcast set.
710  if (exists(GenTxid::Txid(txid))) m_unbroadcast_txids.insert(txid);
711  };
712 
714  void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
715 
717  std::set<uint256> GetUnbroadcastTxs() const
718  {
719  LOCK(cs);
720  return m_unbroadcast_txids;
721  }
722 
725  {
727  return m_unbroadcast_txids.count(txid) != 0;
728  }
729 
732  return m_sequence_number++;
733  }
734 
736  return m_sequence_number;
737  }
738 
739 private:
768  void UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
769  const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs);
771  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
777  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
780 
790 public:
800  {
801  return m_epoch.visited(it->m_epoch_marker);
802  }
803 
804  bool visited(std::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
805  {
806  assert(m_epoch.guarded()); // verify guard even when it==nullopt
807  return !it || visited(*it);
808  }
809 };
810 
825 {
830  std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
831 
836  mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
837 protected:
839 
840 public:
841  CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
844  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
847  void PackageAddTransaction(const CTransactionRef& tx);
849  std::unordered_set<COutPoint, SaltedOutpointHasher> GetNonBaseCoins() const { return m_non_base_coins; }
851  void Reset();
852 };
853 #endif // BITCOIN_TXMEMPOOL_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define LIFETIMEBOUND
Definition: attributes.h:16
An in-memory indexed chain of blocks.
Definition: chain.h:447
CCoinsView backed by another CCoinsView.
Definition: coins.h:210
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:229
Abstract view on the open txout dataset.
Definition: coins.h:173
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:825
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:989
std::unordered_set< COutPoint, SaltedOutpointHasher > GetNonBaseCoins() const
Get all coins in m_non_base_coins.
Definition: txmempool.h:849
void Reset()
Clear m_temp_added and m_non_base_coins.
Definition: txmempool.cpp:1020
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:830
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:987
std::unordered_set< COutPoint, SaltedOutpointHasher > m_non_base_coins
Set of all coins that have been fetched from mempool or created using PackageAddTransaction (not base...
Definition: txmempool.h:836
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:1013
const CTxMemPool & mempool
Definition: txmempool.h:838
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
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 Wtxid & GetWitnessHash() const LIFETIMEBOUND
Definition: transaction.h:344
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
int64_t GetSizeWithDescendants() const
const CTransaction & GetTx() const
std::chrono::seconds GetTime() const
CAmount GetModFeesWithDescendants() const
int32_t GetTxSize() const
CAmount GetModifiedFee() const
const CAmount & GetFee() const
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:301
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:610
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:304
void RemoveUnbroadcastTx(const uint256 &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:1032
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:875
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 HasNoInputsOf(const CTransaction &tx) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on o...
Definition: txmempool.cpp:979
const bool m_require_standard
Definition: txmempool.h:445
setEntries GetIterSet(const std::set< Txid > &hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
Definition: txmempool.cpp:956
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:705
std::vector< txiter > GetIterVec(const std::vector< uint256 > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a list of hashes into a list of mempool iterators to avoid repeated lookups.
Definition: txmempool.cpp:966
ValidationSignals *const m_signals
Definition: txmempool.h:451
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
Definition: txmempool.h:308
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:298
bool GetLoadTried() const
Definition: txmempool.cpp:1203
bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs
visited marks a CTxMemPoolEntry as having been traversed during the lifetime of the most recently cre...
bool visited(std::optional< txiter > it) const EXCLUSIVE_LOCKS_REQUIRED(cs
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.h:626
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:389
void ClearPrioritisation(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:921
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1118
util::Result< setEntries > CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
Definition: txmempool.cpp:237
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:560
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
Definition: txmempool.h:311
const int m_check_ratio
Value n means that 1 times in n we check.
Definition: txmempool.h:303
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
Definition: txmempool.cpp:1126
const std::chrono::seconds m_expiry
Definition: txmempool.h:439
const std::optional< unsigned > m_max_datacarrier_bytes
Definition: txmempool.h:444
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
Definition: txmempool.cpp:102
return !it visited * it
Definition: txmempool.h:807
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:425
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:312
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:949
util::Result< setEntries > CalculateAncestorsAndCheckLimits(int64_t entry_size, size_t entry_count, CTxMemPoolEntry::Parents &staged_ancestors, const Limits &limits) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor and desc...
Definition: txmempool.cpp:157
const bool m_full_rbf
Definition: txmempool.h:446
const int64_t m_max_size_bytes
Definition: txmempool.h:438
void cs_main LOCKS_EXCLUDED(m_epoch)
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:846
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1026
Epoch m_epoch GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:313
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:825
void GetTransactionAncestry(const uint256 &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, CAmount *ancestorfees=nullptr) const
Calculate the ancestor and descendant count for the given transaction.
Definition: txmempool.cpp:1191
indexed_transaction_set mapTx GUARDED_BY(cs)
bool IsUnbroadcastTx(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:724
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1083
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
Definition: txmempool.cpp:485
uint64_t totalTxSize GUARDED_BY(cs)
Definition: txmempool.h:306
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
Definition: txmempool.cpp:1049
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:689
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update ancestors of hash to add/remove it as a descendant transaction.
Definition: txmempool.cpp:283
void removeForReorg(CChain &chain, std::function< bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs
After reorg, filter the entries that would no longer be valid in the next block, and update the entri...
Definition: txmempool.cpp:590
util::Result< void > CheckPackageLimits(const Package &package, int64_t total_vsize) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate all in-mempool ancestors of a set of transactions not already in the mempool and check ance...
Definition: txmempool.cpp:199
TxMempoolInfo info(const GenTxid &gtxid) const
Definition: txmempool.cpp:855
std::vector< txiter > GatherClusters(const std::vector< uint256 > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Collect the entire cluster of connected transactions for each transaction in txids.
Definition: txmempool.cpp:1215
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:911
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:310
const bool m_permit_bare_multisig
Definition: txmempool.h:443
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude, std::set< uint256 > &descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
Definition: txmempool.cpp:51
bool m_load_tried GUARDED_BY(cs)
Definition: txmempool.h:322
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:328
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:395
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:795
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1041
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:630
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:717
std::vector< delta_info > GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return a vector of all entries in mapDeltas with their corresponding delta_info.
Definition: txmempool.cpp:927
const CFeeRate m_min_relay_feerate
Definition: txmempool.h:441
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:735
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:392
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:731
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1072
TxMempoolInfo info_for_relay(const GenTxid &gtxid, uint64_t last_sequence) const
Returns info for a transaction if its entry_sequence < last_sequence.
Definition: txmempool.cpp:864
double rollingMinimumFeeRate GUARDED_BY(cs)
Definition: txmempool.h:312
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:677
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:401
bool m_epoch
Definition: txmempool.h:800
const CFeeRate m_incremental_relay_feerate
Definition: txmempool.h:440
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:943
boost::multi_index_container< CTxMemPoolEntry, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< mempoolentry_txid, SaltedTxidHasher >, boost::multi_index::hashed_unique< boost::multi_index::tag< index_by_wtxid >, mempoolentry_wtxid, SaltedTxidHasher >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< descendant_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByDescendantScore >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< entry_time >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByEntryTime >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByAncestorFee > > > indexed_transaction_set
Definition: txmempool.h:360
const bool m_persist_v1_dat
Definition: txmempool.h:447
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
Definition: txmempool.cpp:759
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:536
unsigned long size() const
Definition: txmempool.h:659
const Limits m_limits
Definition: txmempool.h:449
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
Definition: txmempool.h:475
uint64_t m_sequence_number GUARDED_BY(cs)
Definition: txmempool.h:318
void SetLoadTried(bool load_tried)
Set whether or not an initial attempt to load the persisted mempool was made (regardless of whether t...
Definition: txmempool.cpp:1209
const CFeeRate m_dust_relay_feerate
Definition: txmempool.h:442
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
Definition: txmempool.cpp:320
std::vector< CTransactionRef > txns_randomized GUARDED_BY(cs)
All transactions in mapTx, in random order.
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:813
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:671
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1169
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:665
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:414
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:839
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:420
CAmount m_total_fee GUARDED_BY(cs)
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
Definition: txmempool.h:307
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.h:475
A UTXO entry.
Definition: coins.h:32
Definition: txmempool.h:161
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:164
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
Definition: txmempool.h:183
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
Definition: txmempool.h:89
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:91
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
Definition: txmempool.h:109
Definition: txmempool.h:148
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:150
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay,...
Definition: txmempool.h:134
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:136
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
Definition: epochguard.h:35
A generic txid reference (txid or wtxid).
Definition: transaction.h:428
bool IsWtxid() const
Definition: transaction.h:436
const uint256 & GetHash() const LIFETIMEBOUND
Definition: transaction.h:437
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:434
256-bit opaque blob.
Definition: uint256.h:106
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
LockPoints lp
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:50
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
const CAmount delta
The fee delta added using PrioritiseTransaction().
Definition: txmempool.h:509
const bool in_mempool
Whether this transaction is in the mempool.
Definition: txmempool.h:507
std::optional< CAmount > modified_fee
The modified fee (base fee + delta) of this entry.
Definition: txmempool.h:511
const uint256 txid
The prioritised transaction's txid.
Definition: txmempool.h:513
Information about a mempool transaction.
Definition: txmempool.h:210
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:224
int32_t vsize
Virtual size of the transaction.
Definition: txmempool.h:221
CAmount fee
Fee of the transaction.
Definition: txmempool.h:218
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:212
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:215
Options struct containing limit options for a CTxMemPool.
Options struct containing options for constructing a CTxMemPool.
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:57
uint256 result_type
Definition: txmempool.h:56
uint256 result_type
Definition: txmempool.h:71
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:72
#define LOCK(cs)
Definition: sync.h:257
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
int64_t GetTime()
Definition: time.cpp:97
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0....
Definition: txmempool.h:46
bool TestLockPointValidity(CChain &active_chain, const LockPoints &lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Test whether the LockPoints height and time are still valid on the current chain.
Definition: txmempool.cpp:34
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())