Bitcoin Core  24.1.0
P2P Digital Currency
txmempool.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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 <utility>
15 #include <vector>
16 
17 #include <kernel/mempool_limits.h>
18 #include <kernel/mempool_options.h>
19 
20 #include <coins.h>
21 #include <consensus/amount.h>
22 #include <indirectmap.h>
23 #include <policy/feerate.h>
24 #include <policy/packages.h>
25 #include <primitives/transaction.h>
26 #include <random.h>
27 #include <sync.h>
28 #include <util/epochguard.h>
29 #include <util/hasher.h>
30 
31 #include <boost/multi_index/hashed_index.hpp>
32 #include <boost/multi_index/ordered_index.hpp>
33 #include <boost/multi_index/sequenced_index.hpp>
34 #include <boost/multi_index_container.hpp>
35 
36 class CBlockIndex;
37 class CChain;
38 class Chainstate;
39 extern RecursiveMutex cs_main;
40 
42 static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
43 
44 struct LockPoints {
45  // Will be set to the blockchain height and median time past
46  // values that would be necessary to satisfy all relative locktime
47  // constraints (BIP68) of this tx given our view of block chain history
48  int height{0};
49  int64_t time{0};
50  // As long as the current chain descends from the highest height block
51  // containing one of the inputs used in the calculation, then the cached
52  // values are still valid even after a reorg.
54 };
55 
60 
62  // SFINAE for T where T is either a pointer type (e.g., a txiter) or a reference_wrapper<T>
63  // (e.g. a wrapped CTxMemPoolEntry&)
64  template <typename T>
65  bool operator()(const std::reference_wrapper<T>& a, const std::reference_wrapper<T>& b) const
66  {
67  return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();
68  }
69  template <typename T>
70  bool operator()(const T& a, const T& b) const
71  {
72  return a->GetTx().GetHash() < b->GetTx().GetHash();
73  }
74 };
75 
89 {
90 public:
91  typedef std::reference_wrapper<const CTxMemPoolEntry> CTxMemPoolEntryRef;
92  // two aliases, should the types ever diverge
93  typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Parents;
94  typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Children;
95 
96 private:
98  mutable Parents m_parents;
100  const CAmount nFee;
101  const size_t nTxWeight;
102  const size_t nUsageSize;
103  const int64_t nTime;
104  const unsigned int entryHeight;
105  const bool spendsCoinbase;
106  const int64_t sigOpCost;
109 
110  // Information about descendants of this transaction that are in the
111  // mempool; if we remove this transaction we must remove all of these
112  // descendants as well.
113  uint64_t nCountWithDescendants{1};
116 
117  // Analogous statistics for ancestor transactions
118  uint64_t nCountWithAncestors{1};
122 
123 public:
125  int64_t time, unsigned int entry_height,
126  bool spends_coinbase,
127  int64_t sigops_cost, LockPoints lp);
128 
129  const CTransaction& GetTx() const { return *this->tx; }
130  CTransactionRef GetSharedTx() const { return this->tx; }
131  const CAmount& GetFee() const { return nFee; }
132  size_t GetTxSize() const;
133  size_t GetTxWeight() const { return nTxWeight; }
134  std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
135  unsigned int GetHeight() const { return entryHeight; }
136  int64_t GetSigOpCost() const { return sigOpCost; }
138  size_t DynamicMemoryUsage() const { return nUsageSize; }
139  const LockPoints& GetLockPoints() const { return lockPoints; }
140 
141  // Adjusts the descendant state.
142  void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
143  // Adjusts the ancestor state
144  void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
145  // Updates the modified fees with descendants/ancestors.
146  void UpdateModifiedFee(CAmount fee_diff);
147  // Update the LockPoints after a reorg
148  void UpdateLockPoints(const LockPoints& lp);
149 
150  uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
151  uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
153 
154  bool GetSpendsCoinbase() const { return spendsCoinbase; }
155 
156  uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
157  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
160 
161  const Parents& GetMemPoolParentsConst() const { return m_parents; }
162  const Children& GetMemPoolChildrenConst() const { return m_children; }
163  Parents& GetMemPoolParents() const { return m_parents; }
165 
166  mutable size_t vTxHashesIdx;
168 };
169 
170 // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
172 {
175  {
176  return entry.GetTx().GetHash();
177  }
178 
180  {
181  return tx->GetHash();
182  }
183 };
184 
185 // extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
187 {
190  {
191  return entry.GetTx().GetWitnessHash();
192  }
193 
195  {
196  return tx->GetWitnessHash();
197  }
198 };
199 
200 
206 {
207 public:
208  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
209  {
210  double a_mod_fee, a_size, b_mod_fee, b_size;
211 
212  GetModFeeAndSize(a, a_mod_fee, a_size);
213  GetModFeeAndSize(b, b_mod_fee, b_size);
214 
215  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
216  double f1 = a_mod_fee * b_size;
217  double f2 = a_size * b_mod_fee;
218 
219  if (f1 == f2) {
220  return a.GetTime() >= b.GetTime();
221  }
222  return f1 < f2;
223  }
224 
225  // Return the fee/size we're using for sorting this entry.
226  void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
227  {
228  // Compare feerate with descendants to feerate of the transaction, and
229  // return the fee/size for the max.
230  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
231  double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
232 
233  if (f2 > f1) {
234  mod_fee = a.GetModFeesWithDescendants();
235  size = a.GetSizeWithDescendants();
236  } else {
237  mod_fee = a.GetModifiedFee();
238  size = a.GetTxSize();
239  }
240  }
241 };
242 
251 {
252 public:
253  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
254  {
255  double f1 = (double)a.GetFee() * b.GetTxSize();
256  double f2 = (double)b.GetFee() * a.GetTxSize();
257  if (f1 == f2) {
258  return b.GetTx().GetHash() < a.GetTx().GetHash();
259  }
260  return f1 > f2;
261  }
262 };
263 
265 {
266 public:
267  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
268  {
269  return a.GetTime() < b.GetTime();
270  }
271 };
272 
278 {
279 public:
280  template<typename T>
281  bool operator()(const T& a, const T& b) const
282  {
283  double a_mod_fee, a_size, b_mod_fee, b_size;
284 
285  GetModFeeAndSize(a, a_mod_fee, a_size);
286  GetModFeeAndSize(b, b_mod_fee, b_size);
287 
288  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
289  double f1 = a_mod_fee * b_size;
290  double f2 = a_size * b_mod_fee;
291 
292  if (f1 == f2) {
293  return a.GetTx().GetHash() < b.GetTx().GetHash();
294  }
295  return f1 > f2;
296  }
297 
298  // Return the fee/size we're using for sorting this entry.
299  template <typename T>
300  void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
301  {
302  // Compare feerate with ancestors to feerate of the transaction, and
303  // return the fee/size for the min.
304  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
305  double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
306 
307  if (f1 > f2) {
308  mod_fee = a.GetModFeesWithAncestors();
309  size = a.GetSizeWithAncestors();
310  } else {
311  mod_fee = a.GetModifiedFee();
312  size = a.GetTxSize();
313  }
314  }
315 };
316 
317 // Multi_index tag names
319 struct entry_time {};
320 struct ancestor_score {};
321 struct index_by_wtxid {};
322 
324 
329 {
332 
334  std::chrono::seconds m_time;
335 
338 
340  size_t vsize;
341 
343  int64_t nFeeDelta;
344 };
345 
350  EXPIRY,
351  SIZELIMIT,
352  REORG,
353  BLOCK,
354  CONFLICT,
355  REPLACED,
356 };
357 
432 {
433 protected:
434  const int m_check_ratio;
435  std::atomic<unsigned int> nTransactionsUpdated{0};
437 
438  uint64_t totalTxSize GUARDED_BY(cs);
439  CAmount m_total_fee GUARDED_BY(cs);
440  uint64_t cachedInnerUsage GUARDED_BY(cs);
441 
442  mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs);
443  mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs);
444  mutable double rollingMinimumFeeRate GUARDED_BY(cs);
445  mutable Epoch m_epoch GUARDED_BY(cs);
446 
447  // In-memory counter for external mempool tracking purposes.
448  // This number is incremented once every time a transaction
449  // is added or removed from the mempool for any reason.
450  mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
451 
453 
454  bool m_load_tried GUARDED_BY(cs){false};
455 
456  CFeeRate GetMinFee(size_t sizelimit) const;
457 
458 public:
459 
460  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
461 
462  typedef boost::multi_index_container<
464  boost::multi_index::indexed_by<
465  // sorted by txid
466  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
467  // sorted by wtxid
468  boost::multi_index::hashed_unique<
469  boost::multi_index::tag<index_by_wtxid>,
472  >,
473  // sorted by fee rate
474  boost::multi_index::ordered_non_unique<
475  boost::multi_index::tag<descendant_score>,
476  boost::multi_index::identity<CTxMemPoolEntry>,
478  >,
479  // sorted by entry time
480  boost::multi_index::ordered_non_unique<
481  boost::multi_index::tag<entry_time>,
482  boost::multi_index::identity<CTxMemPoolEntry>,
484  >,
485  // sorted by fee rate with ancestors
486  boost::multi_index::ordered_non_unique<
487  boost::multi_index::tag<ancestor_score>,
488  boost::multi_index::identity<CTxMemPoolEntry>,
490  >
491  >
493 
523 
524  using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
525  std::vector<std::pair<uint256, txiter>> vTxHashes GUARDED_BY(cs);
526 
527  typedef std::set<txiter, CompareIteratorByHash> setEntries;
528 
530 private:
531  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
532 
533 
534  void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
535  void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
536 
537  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
538 
542  std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
543 
544 
553  bool CalculateAncestorsAndCheckLimits(size_t entry_size,
554  size_t entry_count,
555  setEntries& setAncestors,
556  CTxMemPoolEntry::Parents &staged_ancestors,
557  uint64_t limitAncestorCount,
558  uint64_t limitAncestorSize,
559  uint64_t limitDescendantCount,
560  uint64_t limitDescendantSize,
561  std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs);
562 
563 public:
564  indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
565  std::map<uint256, CAmount> mapDeltas GUARDED_BY(cs);
566 
567  using Options = kernel::MemPoolOptions;
568 
569  const int64_t m_max_size_bytes;
570  const std::chrono::seconds m_expiry;
575  const std::optional<unsigned> m_max_datacarrier_bytes;
576  const bool m_require_standard;
577  const bool m_full_rbf;
578 
579  using Limits = kernel::MemPoolLimits;
580 
582 
588  explicit CTxMemPool(const Options& opts);
589 
596  void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
597 
598  // addUnchecked must updated state for all ancestors of a given transaction,
599  // to track size/count of descendant transactions. First version of
600  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
601  // then invoke the second version.
602  // Note that addUnchecked is ONLY called from ATMP outside of tests
603  // and any other callers may break wallet's in-mempool tracking (due to
604  // lack of CValidationInterface::TransactionAddedToMempool callbacks).
605  void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
606  void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
607 
616  void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
618  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
619 
620  void clear();
621  void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
622  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
623  void queryHashes(std::vector<uint256>& vtxid) const;
624  bool isSpent(const COutPoint& outpoint) const;
625  unsigned int GetTransactionsUpdated() const;
626  void AddTransactionsUpdated(unsigned int n);
631  bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
632 
634  void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
635  void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
637 
639  const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
640 
642  std::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
643 
645  setEntries GetIterSet(const std::set<uint256>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
646 
654  void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
655 
670 
681  bool CalculateMemPoolAncestors(const CTxMemPoolEntry& entry, setEntries& setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string& errString, bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
682 
698  bool CheckPackageLimits(const Package& package,
699  uint64_t limitAncestorCount,
700  uint64_t limitAncestorSize,
701  uint64_t limitDescendantCount,
702  uint64_t limitDescendantSize,
703  std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs);
704 
708  void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
709 
716  CFeeRate GetMinFee() const {
717  return GetMinFee(m_max_size_bytes);
718  }
719 
724  void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
725 
727  int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
728 
735  void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
736 
741  bool GetLoadTried() const;
742 
747  void SetLoadTried(bool load_tried);
748 
749  unsigned long size() const
750  {
751  LOCK(cs);
752  return mapTx.size();
753  }
754 
756  {
758  return totalTxSize;
759  }
760 
762  {
764  return m_total_fee;
765  }
766 
767  bool exists(const GenTxid& gtxid) const
768  {
769  LOCK(cs);
770  if (gtxid.IsWtxid()) {
771  return (mapTx.get<index_by_wtxid>().count(gtxid.GetHash()) != 0);
772  }
773  return (mapTx.count(gtxid.GetHash()) != 0);
774  }
775 
776  CTransactionRef get(const uint256& hash) const;
778  {
780  return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
781  }
782  TxMempoolInfo info(const GenTxid& gtxid) const;
783  std::vector<TxMempoolInfo> infoAll() const;
784 
785  size_t DynamicMemoryUsage() const;
786 
788  void AddUnbroadcastTx(const uint256& txid)
789  {
790  LOCK(cs);
791  // Sanity check the transaction is in the mempool & insert into
792  // unbroadcast set.
793  if (exists(GenTxid::Txid(txid))) m_unbroadcast_txids.insert(txid);
794  };
795 
797  void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
798 
800  std::set<uint256> GetUnbroadcastTxs() const
801  {
802  LOCK(cs);
803  return m_unbroadcast_txids;
804  }
805 
808  {
810  return m_unbroadcast_txids.count(txid) != 0;
811  }
812 
815  return m_sequence_number++;
816  }
817 
819  return m_sequence_number;
820  }
821 
822 private:
851  void UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
852  const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs);
854  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
860  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
863 
873 public:
882  bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
883  {
884  return m_epoch.visited(it->m_epoch_marker);
885  }
886 
887  bool visited(std::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
888  {
889  assert(m_epoch.guarded()); // verify guard even when it==nullopt
890  return !it || visited(*it);
891  }
892 };
893 
908 {
913  std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
914 protected:
916 
917 public:
918  CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
919  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
922  void PackageAddTransaction(const CTransactionRef& tx);
923 };
924 
940 // multi_index tag names
941 struct txid_index {};
942 struct insertion_order {};
943 
945  typedef boost::multi_index_container<
947  boost::multi_index::indexed_by<
948  // sorted by txid
949  boost::multi_index::hashed_unique<
950  boost::multi_index::tag<txid_index>,
953  >,
954  // sorted by order in the blockchain
955  boost::multi_index::sequenced<
956  boost::multi_index::tag<insertion_order>
957  >
958  >
960 
961  // It's almost certainly a logic bug if we don't clear out queuedTx before
962  // destruction, as we add to it while disconnecting blocks, and then we
963  // need to re-process remaining transactions to ensure mempool consistency.
964  // For now, assert() that we've emptied out this object on destruction.
965  // This assert() can always be removed if the reorg-processing code were
966  // to be refactored such that this assumption is no longer true (for
967  // instance if there was some other way we cleaned up the mempool after a
968  // reorg, besides draining this object).
970 
972  uint64_t cachedInnerUsage = 0;
973 
974  // Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
975  // no exact formula for boost::multi_index_contained is implemented.
976  size_t DynamicMemoryUsage() const {
977  return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
978  }
979 
981  {
982  queuedTx.insert(tx);
984  }
985 
986  // Remove entries based on txid_index, and update memory usage.
987  void removeForBlock(const std::vector<CTransactionRef>& vtx)
988  {
989  // Short-circuit in the common case of a block being added to the tip
990  if (queuedTx.empty()) {
991  return;
992  }
993  for (auto const &tx : vtx) {
994  auto it = queuedTx.find(tx->GetHash());
995  if (it != queuedTx.end()) {
997  queuedTx.erase(it);
998  }
999  }
1000  }
1001 
1002  // Remove an entry by insertion_order index, and update memory usage.
1003  void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
1004  {
1006  queuedTx.get<insertion_order>().erase(entry);
1007  }
1008 
1009  void clear()
1010  {
1011  cachedInnerUsage = 0;
1012  queuedTx.clear();
1013  }
1014 };
1015 
1016 #endif // BITCOIN_TXMEMPOOL_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:414
CAmount GetModFeesWithAncestors() const
Definition: txmempool.h:158
size_t vTxHashesIdx
Index in mempool&#39;s vTxHashes.
Definition: txmempool.h:166
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: txmempool.h:105
void queryHashes(std::vector< uint256 > &vtxid) const
Definition: txmempool.cpp:856
Information about a mempool transaction.
Definition: txmempool.h:328
const CTransactionRef tx
Definition: txmempool.h:97
uint64_t GetSizeWithAncestors() const
Definition: txmempool.h:157
CAmount nModFeesWithDescendants
... and total fees (all including us)
Definition: txmempool.h:115
void UpdateLockPoints(const LockPoints &lp)
Definition: txmempool.cpp:70
void UpdateModifiedFee(CAmount fee_diff)
Definition: txmempool.cpp:63
AssertLockHeld(pool.cs)
CAmount m_modified_fee
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:107
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:524
assert(!tx.IsCoinBase())
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:873
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:121
A UTXO entry.
Definition: coins.h:30
Children & GetMemPoolChildren() const
Definition: txmempool.h:164
CTxMemPoolEntry(const CTransactionRef &tx, CAmount fee, int64_t time, unsigned int entry_height, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
Definition: txmempool.cpp:44
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:767
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:131
size_t GetTxSize() const
Definition: txmempool.cpp:75
size_t GetTxWeight() const
Definition: txmempool.h:133
Options struct containing limit options for a CTxMemPool.
An in-memory indexed chain of blocks.
Definition: chain.h:422
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:555
const int64_t m_max_size_bytes
Definition: txmempool.h:569
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1014
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:984
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:44
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:267
int height
Definition: txmempool.h:48
Removed in size limiting.
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:629
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: txmempool.h:104
CAmount GetModFeesWithDescendants() const
Definition: txmempool.h:152
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1029
const uint256 & GetHash() const
Definition: transaction.h:428
unsigned long size() const
Definition: txmempool.h:749
void clear()
Definition: txmempool.cpp:692
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:818
uint64_t GetSizeWithDescendants() const
Definition: txmempool.h:151
const std::optional< unsigned > m_max_datacarrier_bytes
Definition: txmempool.h:575
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
Definition: txmempool.cpp:806
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:527
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:980
const Children & GetMemPoolChildrenConst() const
Definition: txmempool.h:162
const bool m_full_rbf
Definition: txmempool.h:577
size_t vsize
Virtual size of the transaction.
Definition: txmempool.h:340
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:349
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:208
bool GetSpendsCoinbase() const
Definition: txmempool.h:154
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:435
const int64_t nTime
Local time when entering the mempool.
Definition: txmempool.h:103
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:579
void SetLoadTried(bool load_tried)
Set whether or not we&#39;ve made an attempt to load the mempool (regardless of whether the attempt was s...
Definition: txmempool.cpp:1200
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.h:716
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:415
uint64_t nCountWithDescendants
number of descendant transactions
Definition: txmempool.h:113
CAmount GetModifiedFee() const
Definition: txmempool.h:137
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) const
Definition: txmempool.h:65
bool CheckPackageLimits(const Package &package, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, 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:235
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:343
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:443
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:609
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:70
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:331
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:88
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:605
const int m_check_ratio
Value n means that 1 times in n we check.
Definition: txmempool.h:434
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
Definition: txmempool.h:300
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:959
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:309
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Parents & GetMemPoolParents() const
Definition: txmempool.h:163
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:80
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:281
const CFeeRate m_dust_relay_feerate
Definition: txmempool.h:573
Removed for reorganization.
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:971
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:42
int64_t nSigOpCostWithAncestors
Definition: txmempool.h:121
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: txmempool.h:101
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
Definition: txmempool.h:93
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:174
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:788
Parents m_parents
Definition: txmempool.h:98
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:437
const std::chrono::seconds m_expiry
Definition: txmempool.h:570
Definition: txmempool.h:277
uint64_t nSizeWithAncestors
Definition: txmempool.h:119
Abstract view on the open txout dataset.
Definition: coins.h:156
size_t DynamicMemoryUsage() const
Definition: txmempool.h:138
unsigned int GetHeight() const
Definition: txmempool.h:135
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:1037
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Definition: txmempool.h:987
const uint256 & GetWitnessHash() const
Definition: transaction.h:331
#define LOCK(cs)
Definition: sync.h:261
bool m_load_tried GUARDED_BY(cs)
Definition: txmempool.h:454
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:132
const uint256 & GetHash() const
Definition: transaction.h:330
const CAmount & GetFee() const
Definition: txmempool.h:131
Removed for conflict with in-block transaction.
DisconnectedBlockTransactions.
Definition: txmempool.h:941
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:189
CTransactionRef GetSharedTx() const
Definition: txmempool.h:130
Removed for block.
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:511
const Limits m_limits
Definition: txmempool.h:581
const CFeeRate m_min_relay_feerate
Definition: txmempool.h:572
Sort an entry by max(score/size of entry&#39;s tx, score/size with all descendants).
Definition: txmempool.h:205
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:460
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:755
CAmount nModFeesWithAncestors
Definition: txmempool.h:120
uint64_t m_sequence_number GUARDED_BY(cs)
Definition: txmempool.h:450
bool CalculateAncestorsAndCheckLimits(size_t entry_size, size_t entry_count, setEntries &setAncestors, CTxMemPoolEntry::Parents &staged_ancestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor and desc...
Definition: txmempool.cpp:186
Expired from mempool.
std::chrono::seconds GetTime() const
Definition: txmempool.h:134
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:951
bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
Definition: txmempool.cpp:270
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:34
uint64_t nSizeWithDescendants
... and size
Definition: txmempool.h:114
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:454
uint256 result_type
Definition: txmempool.h:173
uint64_t GetCountWithDescendants() const
Definition: txmempool.h:150
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:935
const size_t nUsageSize
... and total memory usage
Definition: txmempool.h:102
const CFeeRate m_incremental_relay_feerate
Definition: txmempool.h:571
bool IsWtxid() const
Definition: transaction.h:427
const bool m_require_standard
Definition: txmempool.h:576
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1160
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:50
bool GetLoadTried() const
Definition: txmempool.cpp:1194
int64_t GetSigOpCostWithAncestors() const
Definition: txmempool.h:159
const int64_t sigOpCost
Total sigop cost.
Definition: txmempool.h:106
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:957
uint64_t GetCountWithAncestors() const
Definition: txmempool.h:156
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
Definition: txmempool.h:605
256-bit opaque blob.
Definition: uint256.h:119
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:814
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:842
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...
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:431
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:974
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:492
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:531
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:800
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:151
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: txmempool.h:108
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:406
const CTransaction & GetTx() const
Definition: txmempool.h:129
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:678
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:324
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:761
bool m_epoch
Definition: txmempool.h:883
std::reference_wrapper< const CTxMemPoolEntry > CTxMemPoolEntryRef
Definition: txmempool.h:91
TxMempoolInfo info(const GenTxid &gtxid) const
Definition: txmempool.cpp:896
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:777
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1063
void ClearPrioritisation(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:945
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:964
CBlockPolicyEstimator *const minerPolicyEstimator
Definition: txmempool.h:436
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:1182
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Children
Definition: txmempool.h:94
uint64_t nCountWithAncestors
Definition: txmempool.h:118
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
int64_t GetSigOpCost() const
Definition: txmempool.h:136
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:449
Epoch::Marker m_epoch_marker
epoch when last touched, useful for graph algorithms
Definition: txmempool.h:167
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: txmempool.h:100
CAmount fee
Fee of the transaction.
Definition: txmempool.h:337
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:1007
int64_t time
Definition: txmempool.h:49
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:1117
void RemoveUnbroadcastTx(const uint256 &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:1020
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1109
const bool m_permit_bare_multisig
Definition: txmempool.h:574
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:27
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:982
Options struct containing options for constructing a CTxMemPool.
size_t DynamicMemoryUsage() const
Definition: txmempool.h:976
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:287
CCoinsView backed by another CCoinsView.
Definition: coins.h:193
const LockPoints & GetLockPoints() const
Definition: txmempool.h:139
bool IsUnbroadcastTx(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:807
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:212
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay...
Definition: txmempool.h:250
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1074
const CTxMemPool & mempool
Definition: txmempool.h:915
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:338
Removed for replacement.
const Parents & GetMemPoolParentsConst() const
Definition: txmempool.h:161
Children m_children
Definition: txmempool.h:99
uint256 result_type
Definition: txmempool.h:188
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:334
CBlockIndex * maxInputBlock
Definition: txmempool.h:53
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
Definition: txmempool.h:226
return !it visited * it
Definition: txmempool.h:890
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:907
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:649
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:905
A generic txid reference (txid or wtxid).
Definition: transaction.h:418
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:1003
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:913
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:425
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:253
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:346
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:521
void cs_main LOCKS_EXCLUDED(m_epoch)
Definition: txmempool.h:264
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
Definition: epochguard.h:34
LockPoints lp
uint64_t totalTxSize GUARDED_BY(cs)
sum of all mempool tx&#39;s virtual sizes. Differs from serialized tx size since witness data is discount...