Bitcoin Core  24.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 <atomic>
10 #include <map>
11 #include <optional>
12 #include <set>
13 #include <string>
14 #include <string_view>
15 #include <utility>
16 #include <vector>
17 
18 #include <kernel/mempool_limits.h>
19 #include <kernel/mempool_options.h>
20 
21 #include <coins.h>
22 #include <consensus/amount.h>
23 #include <indirectmap.h>
24 #include <kernel/cs_main.h>
25 #include <kernel/mempool_entry.h>
26 #include <policy/feerate.h>
27 #include <policy/packages.h>
28 #include <primitives/transaction.h>
29 #include <random.h>
30 #include <sync.h>
31 #include <util/epochguard.h>
32 #include <util/hasher.h>
33 #include <util/result.h>
34 
35 #include <boost/multi_index/hashed_index.hpp>
36 #include <boost/multi_index/ordered_index.hpp>
37 #include <boost/multi_index/sequenced_index.hpp>
38 #include <boost/multi_index_container.hpp>
39 
40 class CBlockIndex;
41 class CChain;
42 class Chainstate;
43 
45 static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
46 
51 
52 // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
54 {
57  {
58  return entry.GetTx().GetHash();
59  }
60 
62  {
63  return tx->GetHash();
64  }
65 };
66 
67 // extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
69 {
72  {
73  return entry.GetTx().GetWitnessHash();
74  }
75 
77  {
78  return tx->GetWitnessHash();
79  }
80 };
81 
82 
88 {
89 public:
90  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
91  {
92  double a_mod_fee, a_size, b_mod_fee, b_size;
93 
94  GetModFeeAndSize(a, a_mod_fee, a_size);
95  GetModFeeAndSize(b, b_mod_fee, b_size);
96 
97  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
98  double f1 = a_mod_fee * b_size;
99  double f2 = a_size * b_mod_fee;
100 
101  if (f1 == f2) {
102  return a.GetTime() >= b.GetTime();
103  }
104  return f1 < f2;
105  }
106 
107  // Return the fee/size we're using for sorting this entry.
108  void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
109  {
110  // Compare feerate with descendants to feerate of the transaction, and
111  // return the fee/size for the max.
112  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
113  double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
114 
115  if (f2 > f1) {
116  mod_fee = a.GetModFeesWithDescendants();
117  size = a.GetSizeWithDescendants();
118  } else {
119  mod_fee = a.GetModifiedFee();
120  size = a.GetTxSize();
121  }
122  }
123 };
124 
133 {
134 public:
135  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
136  {
137  double f1 = (double)a.GetFee() * b.GetTxSize();
138  double f2 = (double)b.GetFee() * a.GetTxSize();
139  if (f1 == f2) {
140  return b.GetTx().GetHash() < a.GetTx().GetHash();
141  }
142  return f1 > f2;
143  }
144 };
145 
147 {
148 public:
149  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
150  {
151  return a.GetTime() < b.GetTime();
152  }
153 };
154 
160 {
161 public:
162  template<typename T>
163  bool operator()(const T& a, const T& b) const
164  {
165  double a_mod_fee, a_size, b_mod_fee, b_size;
166 
167  GetModFeeAndSize(a, a_mod_fee, a_size);
168  GetModFeeAndSize(b, b_mod_fee, b_size);
169 
170  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
171  double f1 = a_mod_fee * b_size;
172  double f2 = a_size * b_mod_fee;
173 
174  if (f1 == f2) {
175  return a.GetTx().GetHash() < b.GetTx().GetHash();
176  }
177  return f1 > f2;
178  }
179 
180  // Return the fee/size we're using for sorting this entry.
181  template <typename T>
182  void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
183  {
184  // Compare feerate with ancestors to feerate of the transaction, and
185  // return the fee/size for the min.
186  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
187  double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
188 
189  if (f1 > f2) {
190  mod_fee = a.GetModFeesWithAncestors();
191  size = a.GetSizeWithAncestors();
192  } else {
193  mod_fee = a.GetModifiedFee();
194  size = a.GetTxSize();
195  }
196  }
197 };
198 
199 // Multi_index tag names
201 struct entry_time {};
202 struct ancestor_score {};
203 struct index_by_wtxid {};
204 
206 
211 {
214 
216  std::chrono::seconds m_time;
217 
220 
222  size_t vsize;
223 
225  int64_t nFeeDelta;
226 };
227 
232  EXPIRY,
233  SIZELIMIT,
234  REORG,
235  BLOCK,
236  CONFLICT,
237  REPLACED,
238 };
239 
240 std::string RemovalReasonToString(const MemPoolRemovalReason& r) noexcept;
241 
316 {
317 protected:
318  const int m_check_ratio;
319  std::atomic<unsigned int> nTransactionsUpdated{0};
321 
322  uint64_t totalTxSize GUARDED_BY(cs){0};
323  CAmount m_total_fee GUARDED_BY(cs){0};
324  uint64_t cachedInnerUsage GUARDED_BY(cs){0};
325 
326  mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
327  mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
328  mutable double rollingMinimumFeeRate GUARDED_BY(cs){0};
330 
331  // In-memory counter for external mempool tracking purposes.
332  // This number is incremented once every time a transaction
333  // is added or removed from the mempool for any reason.
334  mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
335 
337 
338  bool m_load_tried GUARDED_BY(cs){false};
339 
340  CFeeRate GetMinFee(size_t sizelimit) const;
341 
342 public:
343 
344  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
345 
346  typedef boost::multi_index_container<
348  boost::multi_index::indexed_by<
349  // sorted by txid
350  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
351  // sorted by wtxid
352  boost::multi_index::hashed_unique<
353  boost::multi_index::tag<index_by_wtxid>,
356  >,
357  // sorted by fee rate
358  boost::multi_index::ordered_non_unique<
359  boost::multi_index::tag<descendant_score>,
360  boost::multi_index::identity<CTxMemPoolEntry>,
362  >,
363  // sorted by entry time
364  boost::multi_index::ordered_non_unique<
365  boost::multi_index::tag<entry_time>,
366  boost::multi_index::identity<CTxMemPoolEntry>,
368  >,
369  // sorted by fee rate with ancestors
370  boost::multi_index::ordered_non_unique<
371  boost::multi_index::tag<ancestor_score>,
372  boost::multi_index::identity<CTxMemPoolEntry>,
374  >
375  >
377 
407 
408  using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
409  std::vector<std::pair<uint256, txiter>> vTxHashes GUARDED_BY(cs);
410 
411  typedef std::set<txiter, CompareIteratorByHash> setEntries;
412 
414 
416 private:
417  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
418 
419 
420  void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
421  void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
422 
423  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
424 
428  std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
429 
430 
443  size_t entry_count,
444  CTxMemPoolEntry::Parents &staged_ancestors,
445  const Limits& limits
446  ) const EXCLUSIVE_LOCKS_REQUIRED(cs);
447 
448 public:
450  std::map<uint256, CAmount> mapDeltas GUARDED_BY(cs);
451 
452  using Options = kernel::MemPoolOptions;
453 
454  const int64_t m_max_size_bytes;
455  const std::chrono::seconds m_expiry;
460  const std::optional<unsigned> m_max_datacarrier_bytes;
461  const bool m_require_standard;
462  const bool m_full_rbf;
463 
465 
471  explicit CTxMemPool(const Options& opts);
472 
479  void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
480 
481  // addUnchecked must updated state for all ancestors of a given transaction,
482  // to track size/count of descendant transactions. First version of
483  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
484  // then invoke the second version.
485  // Note that addUnchecked is ONLY called from ATMP outside of tests
486  // and any other callers may break wallet's in-mempool tracking (due to
487  // lack of CValidationInterface::TransactionAddedToMempool callbacks).
488  void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
489  void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
490 
499  void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
501  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
502 
503  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
504  void queryHashes(std::vector<uint256>& vtxid) const;
505  bool isSpent(const COutPoint& outpoint) const;
506  unsigned int GetTransactionsUpdated() const;
507  void AddTransactionsUpdated(unsigned int n);
512  bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
513 
515  void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
516  void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
518 
520  const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
521 
523  std::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
524 
526  setEntries GetIterSet(const std::set<uint256>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
527 
535  void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
536 
551 
565  const Limits& limits,
566  bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
567 
583  std::string_view calling_fn_name,
584  const CTxMemPoolEntry &entry,
585  const Limits& limits,
586  bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
587 
600  bool CheckPackageLimits(const Package& package,
601  const Limits& limits,
602  std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs);
603 
607  void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
608 
615  CFeeRate GetMinFee() const {
616  return GetMinFee(m_max_size_bytes);
617  }
618 
623  void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
624 
626  int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
627 
634  void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
635 
640  bool GetLoadTried() const;
641 
646  void SetLoadTried(bool load_tried);
647 
648  unsigned long size() const
649  {
650  LOCK(cs);
651  return mapTx.size();
652  }
653 
655  {
657  return totalTxSize;
658  }
659 
661  {
663  return m_total_fee;
664  }
665 
666  bool exists(const GenTxid& gtxid) const
667  {
668  LOCK(cs);
669  if (gtxid.IsWtxid()) {
670  return (mapTx.get<index_by_wtxid>().count(gtxid.GetHash()) != 0);
671  }
672  return (mapTx.count(gtxid.GetHash()) != 0);
673  }
674 
675  CTransactionRef get(const uint256& hash) const;
677  {
679  return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
680  }
681  TxMempoolInfo info(const GenTxid& gtxid) const;
682  std::vector<TxMempoolInfo> infoAll() const;
683 
684  size_t DynamicMemoryUsage() const;
685 
687  void AddUnbroadcastTx(const uint256& txid)
688  {
689  LOCK(cs);
690  // Sanity check the transaction is in the mempool & insert into
691  // unbroadcast set.
692  if (exists(GenTxid::Txid(txid))) m_unbroadcast_txids.insert(txid);
693  };
694 
696  void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
697 
699  std::set<uint256> GetUnbroadcastTxs() const
700  {
701  LOCK(cs);
702  return m_unbroadcast_txids;
703  }
704 
707  {
709  return m_unbroadcast_txids.count(txid) != 0;
710  }
711 
714  return m_sequence_number++;
715  }
716 
718  return m_sequence_number;
719  }
720 
721 private:
750  void UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
751  const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs);
753  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
759  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
762 
772 public:
782  {
783  return m_epoch.visited(it->m_epoch_marker);
784  }
785 
786  bool visited(std::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
787  {
788  assert(m_epoch.guarded()); // verify guard even when it==nullopt
789  return !it || visited(*it);
790  }
791 };
792 
807 {
812  std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
813 protected:
815 
816 public:
817  CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
818  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
821  void PackageAddTransaction(const CTransactionRef& tx);
822 };
823 
839 // multi_index tag names
840 struct txid_index {};
841 struct insertion_order {};
842 
844  typedef boost::multi_index_container<
846  boost::multi_index::indexed_by<
847  // sorted by txid
848  boost::multi_index::hashed_unique<
849  boost::multi_index::tag<txid_index>,
852  >,
853  // sorted by order in the blockchain
854  boost::multi_index::sequenced<
855  boost::multi_index::tag<insertion_order>
856  >
857  >
859 
860  // It's almost certainly a logic bug if we don't clear out queuedTx before
861  // destruction, as we add to it while disconnecting blocks, and then we
862  // need to re-process remaining transactions to ensure mempool consistency.
863  // For now, assert() that we've emptied out this object on destruction.
864  // This assert() can always be removed if the reorg-processing code were
865  // to be refactored such that this assumption is no longer true (for
866  // instance if there was some other way we cleaned up the mempool after a
867  // reorg, besides draining this object).
869 
871  uint64_t cachedInnerUsage = 0;
872 
873  // Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
874  // no exact formula for boost::multi_index_contained is implemented.
875  size_t DynamicMemoryUsage() const {
876  return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
877  }
878 
880  {
881  queuedTx.insert(tx);
883  }
884 
885  // Remove entries based on txid_index, and update memory usage.
886  void removeForBlock(const std::vector<CTransactionRef>& vtx)
887  {
888  // Short-circuit in the common case of a block being added to the tip
889  if (queuedTx.empty()) {
890  return;
891  }
892  for (auto const &tx : vtx) {
893  auto it = queuedTx.find(tx->GetHash());
894  if (it != queuedTx.end()) {
896  queuedTx.erase(it);
897  }
898  }
899  }
900 
901  // Remove an entry by insertion_order index, and update memory usage.
902  void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
903  {
905  queuedTx.get<insertion_order>().erase(entry);
906  }
907 
908  void clear()
909  {
910  cachedInnerUsage = 0;
911  queuedTx.clear();
912  }
913 };
914 
915 #endif // BITCOIN_TXMEMPOOL_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:133
An in-memory indexed chain of blocks.
Definition: chain.h:436
CCoinsView backed by another CCoinsView.
Definition: coins.h:194
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
Abstract view on the open txout dataset.
Definition: coins.h:157
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:807
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:911
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:812
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:909
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:934
const CTxMemPool & mempool
Definition: txmempool.h:814
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:36
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
const uint256 & GetHash() const
Definition: transaction.h:337
const uint256 & GetWitnessHash() const
Definition: transaction.h:338
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
const CTransaction & GetTx() const
std::chrono::seconds GetTime() const
CAmount GetModFeesWithDescendants() const
size_t GetTxSize() const
uint64_t GetSizeWithDescendants() 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:316
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:587
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:319
void RemoveUnbroadcastTx(const uint256 &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:947
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:835
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:257
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:901
const bool m_require_standard
Definition: txmempool.h:461
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:687
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
Definition: txmempool.h:324
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:286
bool GetLoadTried() const
Definition: txmempool.cpp:1118
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:615
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:405
void ClearPrioritisation(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:872
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1033
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:225
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:537
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) 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:488
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
Definition: txmempool.h:327
const int m_check_ratio
Value n means that 1 times in n we check.
Definition: txmempool.h:318
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:1041
const std::chrono::seconds m_expiry
Definition: txmempool.h:455
const std::optional< unsigned > m_max_datacarrier_bytes
Definition: txmempool.h:460
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:99
return !it visited * it
Definition: txmempool.h:789
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:412
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:300
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:884
const bool m_full_rbf
Definition: txmempool.h:462
const int64_t m_max_size_bytes
Definition: txmempool.h:454
void cs_main LOCKS_EXCLUDED(m_epoch)
util::Result< setEntries > CalculateAncestorsAndCheckLimits(size_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:154
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:817
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:941
Epoch m_epoch GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:329
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:803
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:1106
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:706
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:998
bool CheckPackageLimits(const Package &package, const Limits &limits, std::string &errString) 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:196
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:469
uint64_t totalTxSize GUARDED_BY(cs)
Definition: txmempool.h:322
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:964
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:676
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:271
CBlockPolicyEstimator *const minerPolicyEstimator
Definition: txmempool.h:320
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:567
TxMempoolInfo info(const GenTxid &gtxid) const
Definition: txmempool.cpp:826
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:862
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:326
const bool m_permit_bare_multisig
Definition: txmempool.h:459
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:48
bool m_load_tried GUARDED_BY(cs)
Definition: txmempool.h:338
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:344
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:411
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:772
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:956
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:607
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:699
void queryHashes(std::vector< uint256 > &vtxid) const
Definition: txmempool.cpp:786
const CFeeRate m_min_relay_feerate
Definition: txmempool.h:457
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:717
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:408
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:713
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:987
double rollingMinimumFeeRate GUARDED_BY(cs)
Definition: txmempool.h:328
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:666
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:417
bool m_epoch
Definition: txmempool.h:782
const CFeeRate m_incremental_relay_feerate
Definition: txmempool.h:456
setEntries GetIterSet(const std::set< uint256 > &hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
Definition: txmempool.cpp:891
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:878
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:376
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
Definition: txmempool.cpp:741
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:513
unsigned long size() const
Definition: txmempool.h:648
const Limits m_limits
Definition: txmempool.h:464
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
Definition: txmempool.h:488
uint64_t m_sequence_number GUARDED_BY(cs)
Definition: txmempool.h:334
void SetLoadTried(bool load_tried)
Set whether or not we've made an attempt to load the mempool (regardless of whether the attempt was s...
Definition: txmempool.cpp:1124
const CFeeRate m_dust_relay_feerate
Definition: txmempool.h:458
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:308
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:660
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1084
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:654
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:401
std::vector< std::pair< uint256, txiter > > vTxHashes GUARDED_BY(cs)
All tx witness hashes/entries in mapTx, in random order.
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:407
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:323
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:441
A UTXO entry.
Definition: coins.h:31
Definition: txmempool.h:160
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:163
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
Definition: txmempool.h:182
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
Definition: txmempool.h:88
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:90
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
Definition: txmempool.h:108
Definition: txmempool.h:147
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:149
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay,...
Definition: txmempool.h:133
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:135
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:426
bool IsWtxid() const
Definition: transaction.h:434
const uint256 & GetHash() const
Definition: transaction.h:435
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:432
256-bit opaque blob.
Definition: uint256.h:105
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
LockPoints lp
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:49
Definition: overloaded.h:8
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:44
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
boost::multi_index_container< CTransactionRef, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::tag< txid_index >, mempoolentry_txid, SaltedTxidHasher >, boost::multi_index::sequenced< boost::multi_index::tag< insertion_order > > > > indexed_disconnected_transactions
Definition: txmempool.h:858
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:902
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:870
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Definition: txmempool.h:886
size_t DynamicMemoryUsage() const
Definition: txmempool.h:875
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:879
Information about a mempool transaction.
Definition: txmempool.h:211
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:225
CAmount fee
Fee of the transaction.
Definition: txmempool.h:219
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:213
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:216
size_t vsize
Virtual size of the transaction.
Definition: txmempool.h:222
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:56
uint256 result_type
Definition: txmempool.h:55
uint256 result_type
Definition: txmempool.h:70
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:71
DisconnectedBlockTransactions.
Definition: txmempool.h:840
#define LOCK(cs)
Definition: sync.h:258
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
int64_t GetTime()
Definition: time.cpp:115
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:231
@ SIZELIMIT
Removed in size limiting.
@ BLOCK
Removed for block.
@ EXPIRY
Expired from mempool.
@ REPLACED
Removed for replacement.
@ CONFLICT
Removed for conflict with in-block transaction.
@ REORG
Removed for reorganization.
std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
Definition: txmempool.cpp:1131
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:45
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:31
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())