Bitcoin Core  0.20.99
P2P Digital Currency
txmempool.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <set>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 #include <amount.h>
17 #include <coins.h>
18 #include <crypto/siphash.h>
19 #include <indirectmap.h>
20 #include <optional.h>
21 #include <policy/feerate.h>
22 #include <primitives/transaction.h>
23 #include <sync.h>
24 #include <random.h>
25 
26 #include <boost/multi_index_container.hpp>
27 #include <boost/multi_index/hashed_index.hpp>
28 #include <boost/multi_index/ordered_index.hpp>
29 #include <boost/multi_index/sequenced_index.hpp>
30 
31 class CBlockIndex;
32 extern RecursiveMutex cs_main;
33 
35 static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
36 
37 struct LockPoints
38 {
39  // Will be set to the blockchain height and median time past
40  // values that would be necessary to satisfy all relative locktime
41  // constraints (BIP68) of this tx given our view of block chain history
42  int height;
43  int64_t time;
44  // As long as the current chain descends from the highest height block
45  // containing one of the inputs used in the calculation, then the cached
46  // values are still valid even after a reorg.
48 
49  LockPoints() : height(0), time(0), maxInputBlock(nullptr) { }
50 };
51 
65 {
66 private:
68  const CAmount nFee;
69  const size_t nTxWeight;
70  const size_t nUsageSize;
71  const int64_t nTime;
72  const unsigned int entryHeight;
73  const bool spendsCoinbase;
74  const int64_t sigOpCost;
75  int64_t feeDelta;
77 
78  // Information about descendants of this transaction that are in the
79  // mempool; if we remove this transaction we must remove all of these
80  // descendants as well.
84 
85  // Analogous statistics for ancestor transactions
90 
91 public:
92  CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
93  int64_t _nTime, unsigned int _entryHeight,
94  bool spendsCoinbase,
95  int64_t nSigOpsCost, LockPoints lp);
96 
97  const CTransaction& GetTx() const { return *this->tx; }
98  CTransactionRef GetSharedTx() const { return this->tx; }
99  const CAmount& GetFee() const { return nFee; }
100  size_t GetTxSize() const;
101  size_t GetTxWeight() const { return nTxWeight; }
102  std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
103  unsigned int GetHeight() const { return entryHeight; }
104  int64_t GetSigOpCost() const { return sigOpCost; }
105  int64_t GetModifiedFee() const { return nFee + feeDelta; }
106  size_t DynamicMemoryUsage() const { return nUsageSize; }
107  const LockPoints& GetLockPoints() const { return lockPoints; }
108 
109  // Adjusts the descendant state.
110  void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
111  // Adjusts the ancestor state
112  void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
113  // Updates the fee delta used for mining priority score, and the
114  // modified fees with descendants.
115  void UpdateFeeDelta(int64_t feeDelta);
116  // Update the LockPoints after a reorg
117  void UpdateLockPoints(const LockPoints& lp);
118 
119  uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
120  uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
121  CAmount GetModFeesWithDescendants() const { return nModFeesWithDescendants; }
122 
123  bool GetSpendsCoinbase() const { return spendsCoinbase; }
124 
125  uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
126  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
127  CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
128  int64_t GetSigOpCostWithAncestors() const { return nSigOpCostWithAncestors; }
129 
130  mutable size_t vTxHashesIdx;
131  mutable uint64_t m_epoch;
132 };
133 
134 // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
136 {
137  update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
138  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
139  {}
140 
141  void operator() (CTxMemPoolEntry &e)
142  { e.UpdateDescendantState(modifySize, modifyFee, modifyCount); }
143 
144  private:
145  int64_t modifySize;
147  int64_t modifyCount;
148 };
149 
151 {
152  update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost) :
153  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOpsCost(_modifySigOpsCost)
154  {}
155 
156  void operator() (CTxMemPoolEntry &e)
157  { e.UpdateAncestorState(modifySize, modifyFee, modifyCount, modifySigOpsCost); }
158 
159  private:
160  int64_t modifySize;
162  int64_t modifyCount;
164 };
165 
167 {
168  explicit update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
169 
170  void operator() (CTxMemPoolEntry &e) { e.UpdateFeeDelta(feeDelta); }
171 
172 private:
173  int64_t feeDelta;
174 };
175 
177 {
178  explicit update_lock_points(const LockPoints& _lp) : lp(_lp) { }
179 
180  void operator() (CTxMemPoolEntry &e) { e.UpdateLockPoints(lp); }
181 
182 private:
183  const LockPoints& lp;
184 };
185 
186 // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
188 {
190  result_type operator() (const CTxMemPoolEntry &entry) const
191  {
192  return entry.GetTx().GetHash();
193  }
194 
195  result_type operator() (const CTransactionRef& tx) const
196  {
197  return tx->GetHash();
198  }
199 };
200 
201 // extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
203 {
205  result_type operator() (const CTxMemPoolEntry &entry) const
206  {
207  return entry.GetTx().GetWitnessHash();
208  }
209 
210  result_type operator() (const CTransactionRef& tx) const
211  {
212  return tx->GetWitnessHash();
213  }
214 };
215 
216 
222 {
223 public:
224  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
225  {
226  double a_mod_fee, a_size, b_mod_fee, b_size;
227 
228  GetModFeeAndSize(a, a_mod_fee, a_size);
229  GetModFeeAndSize(b, b_mod_fee, b_size);
230 
231  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
232  double f1 = a_mod_fee * b_size;
233  double f2 = a_size * b_mod_fee;
234 
235  if (f1 == f2) {
236  return a.GetTime() >= b.GetTime();
237  }
238  return f1 < f2;
239  }
240 
241  // Return the fee/size we're using for sorting this entry.
242  void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
243  {
244  // Compare feerate with descendants to feerate of the transaction, and
245  // return the fee/size for the max.
246  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
247  double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
248 
249  if (f2 > f1) {
250  mod_fee = a.GetModFeesWithDescendants();
251  size = a.GetSizeWithDescendants();
252  } else {
253  mod_fee = a.GetModifiedFee();
254  size = a.GetTxSize();
255  }
256  }
257 };
258 
267 {
268 public:
269  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
270  {
271  double f1 = (double)a.GetFee() * b.GetTxSize();
272  double f2 = (double)b.GetFee() * a.GetTxSize();
273  if (f1 == f2) {
274  return b.GetTx().GetHash() < a.GetTx().GetHash();
275  }
276  return f1 > f2;
277  }
278 };
279 
281 {
282 public:
283  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
284  {
285  return a.GetTime() < b.GetTime();
286  }
287 };
288 
294 {
295 public:
296  template<typename T>
297  bool operator()(const T& a, const T& b) const
298  {
299  double a_mod_fee, a_size, b_mod_fee, b_size;
300 
301  GetModFeeAndSize(a, a_mod_fee, a_size);
302  GetModFeeAndSize(b, b_mod_fee, b_size);
303 
304  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
305  double f1 = a_mod_fee * b_size;
306  double f2 = a_size * b_mod_fee;
307 
308  if (f1 == f2) {
309  return a.GetTx().GetHash() < b.GetTx().GetHash();
310  }
311  return f1 > f2;
312  }
313 
314  // Return the fee/size we're using for sorting this entry.
315  template <typename T>
316  void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
317  {
318  // Compare feerate with ancestors to feerate of the transaction, and
319  // return the fee/size for the min.
320  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
321  double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
322 
323  if (f1 > f2) {
324  mod_fee = a.GetModFeesWithAncestors();
325  size = a.GetSizeWithAncestors();
326  } else {
327  mod_fee = a.GetModifiedFee();
328  size = a.GetTxSize();
329  }
330  }
331 };
332 
333 // Multi_index tag names
335 struct entry_time {};
336 struct ancestor_score {};
337 struct index_by_wtxid {};
338 
340 
345 {
348 
350  std::chrono::seconds m_time;
351 
354 
356  size_t vsize;
357 
359  int64_t nFeeDelta;
360 };
361 
366  EXPIRY,
367  SIZELIMIT,
368  REORG,
369  BLOCK,
370  CONFLICT,
371  REPLACED,
372 };
373 
375 {
376 private:
378  const uint64_t k0, k1;
379 
380 public:
382 
383  size_t operator()(const uint256& txid) const {
384  return SipHashUint256(k0, k1, txid);
385  }
386 };
387 
462 {
463 private:
464  uint32_t nCheckFrequency GUARDED_BY(cs);
465  std::atomic<unsigned int> nTransactionsUpdated;
467 
468  uint64_t totalTxSize;
469  uint64_t cachedInnerUsage;
470 
471  mutable int64_t lastRollingFeeUpdate;
473  mutable double rollingMinimumFeeRate;
474  mutable uint64_t m_epoch;
475  mutable bool m_has_epoch_guard;
476 
477  void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs);
478 
479  bool m_is_loaded GUARDED_BY(cs){false};
480 
481 public:
482 
483  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
484 
485  typedef boost::multi_index_container<
487  boost::multi_index::indexed_by<
488  // sorted by txid
489  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
490  // sorted by wtxid
491  boost::multi_index::hashed_unique<
492  boost::multi_index::tag<index_by_wtxid>,
495  >,
496  // sorted by fee rate
497  boost::multi_index::ordered_non_unique<
498  boost::multi_index::tag<descendant_score>,
499  boost::multi_index::identity<CTxMemPoolEntry>,
501  >,
502  // sorted by entry time
503  boost::multi_index::ordered_non_unique<
504  boost::multi_index::tag<entry_time>,
505  boost::multi_index::identity<CTxMemPoolEntry>,
507  >,
508  // sorted by fee rate with ancestors
509  boost::multi_index::ordered_non_unique<
510  boost::multi_index::tag<ancestor_score>,
511  boost::multi_index::identity<CTxMemPoolEntry>,
513  >
514  >
516 
546 
547  using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
548  std::vector<std::pair<uint256, txiter>> vTxHashes GUARDED_BY(cs);
549 
551  bool operator()(const txiter &a, const txiter &b) const {
552  return a->GetTx().GetHash() < b->GetTx().GetHash();
553  }
554  };
555  typedef std::set<txiter, CompareIteratorByHash> setEntries;
556 
557  const setEntries & GetMemPoolParents(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
558  const setEntries & GetMemPoolChildren(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
559  uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
560 private:
561  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
562 
563  struct TxLinks {
564  setEntries parents;
565  setEntries children;
566  };
567 
568  typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
569  txlinksMap mapLinks;
570 
571  void UpdateParent(txiter entry, txiter parent, bool add);
572  void UpdateChild(txiter entry, txiter child, bool add);
573 
574  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
575 
580  std::map<uint256, uint256> m_unbroadcast_txids GUARDED_BY(cs);
581 
582 public:
583  indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
584  std::map<uint256, CAmount> mapDeltas;
585 
588  explicit CTxMemPool(CBlockPolicyEstimator* estimator = nullptr);
589 
596  void check(const CCoinsViewCache *pcoins) const;
597  void setSanityCheck(double dFrequency = 1.0) { LOCK(cs); nCheckFrequency = static_cast<uint32_t>(dFrequency * 4294967295.0); }
598 
599  // addUnchecked must updated state for all ancestors of a given transaction,
600  // to track size/count of descendant transactions. First version of
601  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
602  // then invoke the second version.
603  // Note that addUnchecked is ONLY called from ATMP outside of tests
604  // and any other callers may break wallet's in-mempool tracking (due to
605  // lack of CValidationInterface::TransactionAddedToMempool callbacks).
606  void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
607  void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
608 
609  void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
610  void removeForReorg(const CCoinsViewCache* pcoins, unsigned int nMemPoolHeight, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
611  void removeConflicts(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
612  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
613 
614  void clear();
615  void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
616  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
617  void queryHashes(std::vector<uint256>& vtxid) const;
618  bool isSpent(const COutPoint& outpoint) const;
619  unsigned int GetTransactionsUpdated() const;
620  void AddTransactionsUpdated(unsigned int n);
625  bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
626 
628  void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
629  void ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const;
630  void ClearPrioritisation(const uint256 hash);
631 
633  const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
634 
636  Optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
637 
639  setEntries GetIterSet(const std::set<uint256>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
640 
648  void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
649 
659  void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
660 
671  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);
672 
676  void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
677 
684  CFeeRate GetMinFee(size_t sizelimit) const;
685 
690  void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
691 
693  int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
694 
699  void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) const;
700 
702  bool IsLoaded() const;
703 
705  void SetIsLoaded(bool loaded);
706 
707  unsigned long size() const
708  {
709  LOCK(cs);
710  return mapTx.size();
711  }
712 
713  uint64_t GetTotalTxSize() const
714  {
715  LOCK(cs);
716  return totalTxSize;
717  }
718 
719  bool exists(const GenTxid& gtxid) const
720  {
721  LOCK(cs);
722  if (gtxid.IsWtxid()) {
723  return (mapTx.get<index_by_wtxid>().count(gtxid.GetHash()) != 0);
724  }
725  return (mapTx.count(gtxid.GetHash()) != 0);
726  }
727  bool exists(const uint256& txid) const { return exists(GenTxid{false, txid}); }
728 
729  CTransactionRef get(const uint256& hash) const;
731  {
732  AssertLockHeld(cs);
733  return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
734  }
735  TxMempoolInfo info(const uint256& hash) const;
736  TxMempoolInfo info(const GenTxid& gtxid) const;
737  std::vector<TxMempoolInfo> infoAll() const;
738 
739  size_t DynamicMemoryUsage() const;
740 
742  void AddUnbroadcastTx(const uint256& txid, const uint256& wtxid) {
743  LOCK(cs);
744  // Sanity Check: the transaction should also be in the mempool
745  if (exists(txid)) {
746  m_unbroadcast_txids[txid] = wtxid;
747  }
748  }
749 
751  void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
752 
754  std::map<uint256, uint256> GetUnbroadcastTxs() const {
755  LOCK(cs);
756  return m_unbroadcast_txids;
757  }
758 
760  bool IsUnbroadcastTx(const uint256& txid) const {
761  LOCK(cs);
762  return (m_unbroadcast_txids.count(txid) != 0);
763  }
764 
765 private:
779  void UpdateForDescendants(txiter updateIt,
780  cacheMap &cachedDescendants,
781  const std::set<uint256> &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs);
783  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
785  void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
789  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
791  void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs);
792 
801  void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
802 public:
822  class EpochGuard {
823  const CTxMemPool& pool;
824  public:
825  EpochGuard(const CTxMemPool& in);
826  ~EpochGuard();
827  };
828  // N.B. GetFreshEpoch modifies mutable state via the EpochGuard construction
829  // (and later destruction)
830  EpochGuard GetFreshEpoch() const EXCLUSIVE_LOCKS_REQUIRED(cs);
831 
840  bool visited(txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
841  assert(m_has_epoch_guard);
842  bool ret = it->m_epoch >= m_epoch;
843  it->m_epoch = std::max(it->m_epoch, m_epoch);
844  return ret;
845  }
846 
848  assert(m_has_epoch_guard);
849  return !it || visited(*it);
850  }
851 };
852 
866 {
867 protected:
869 
870 public:
871  CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
872  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
873 };
874 
890 // multi_index tag names
891 struct txid_index {};
892 struct insertion_order {};
893 
895  typedef boost::multi_index_container<
897  boost::multi_index::indexed_by<
898  // sorted by txid
899  boost::multi_index::hashed_unique<
900  boost::multi_index::tag<txid_index>,
903  >,
904  // sorted by order in the blockchain
905  boost::multi_index::sequenced<
906  boost::multi_index::tag<insertion_order>
907  >
908  >
910 
911  // It's almost certainly a logic bug if we don't clear out queuedTx before
912  // destruction, as we add to it while disconnecting blocks, and then we
913  // need to re-process remaining transactions to ensure mempool consistency.
914  // For now, assert() that we've emptied out this object on destruction.
915  // This assert() can always be removed if the reorg-processing code were
916  // to be refactored such that this assumption is no longer true (for
917  // instance if there was some other way we cleaned up the mempool after a
918  // reorg, besides draining this object).
919  ~DisconnectedBlockTransactions() { assert(queuedTx.empty()); }
920 
922  uint64_t cachedInnerUsage = 0;
923 
924  // Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
925  // no exact formula for boost::multi_index_contained is implemented.
926  size_t DynamicMemoryUsage() const {
927  return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
928  }
929 
930  void addTransaction(const CTransactionRef& tx)
931  {
932  queuedTx.insert(tx);
933  cachedInnerUsage += RecursiveDynamicUsage(tx);
934  }
935 
936  // Remove entries based on txid_index, and update memory usage.
937  void removeForBlock(const std::vector<CTransactionRef>& vtx)
938  {
939  // Short-circuit in the common case of a block being added to the tip
940  if (queuedTx.empty()) {
941  return;
942  }
943  for (auto const &tx : vtx) {
944  auto it = queuedTx.find(tx->GetHash());
945  if (it != queuedTx.end()) {
946  cachedInnerUsage -= RecursiveDynamicUsage(*it);
947  queuedTx.erase(it);
948  }
949  }
950  }
951 
952  // Remove an entry by insertion_order index, and update memory usage.
953  void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
954  {
955  cachedInnerUsage -= RecursiveDynamicUsage(*entry);
956  queuedTx.get<insertion_order>().erase(entry);
957  }
958 
959  void clear()
960  {
961  cachedInnerUsage = 0;
962  queuedTx.clear();
963  }
964 };
965 
966 #endif // BITCOIN_TXMEMPOOL_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:389
CAmount GetModFeesWithAncestors() const
Definition: txmempool.h:127
size_t vTxHashesIdx
Index in mempool&#39;s vTxHashes.
Definition: txmempool.h:130
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: txmempool.h:73
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:94
Information about a mempool transaction.
Definition: txmempool.h:344
update_fee_delta(int64_t _feeDelta)
Definition: txmempool.h:168
const CTransactionRef tx
Definition: txmempool.h:67
uint64_t GetSizeWithAncestors() const
Definition: txmempool.h:126
bool m_has_epoch_guard
Definition: txmempool.h:475
CAmount nModFeesWithDescendants
... and total fees (all including us)
Definition: txmempool.h:83
void UpdateLockPoints(const LockPoints &lp)
Definition: txmempool.cpp:47
std::map< txiter, TxLinks, CompareIteratorByHash > txlinksMap
Definition: txmempool.h:568
uint64_t m_epoch
Definition: txmempool.h:474
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:547
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:128
A UTXO entry.
Definition: coins.h:30
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:719
size_t GetTxSize() const
Definition: txmempool.cpp:52
size_t GetTxWeight() const
Definition: txmempool.h:101
static void pool cs
LockPoints()
Definition: txmempool.h:49
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:283
int height
Definition: txmempool.h:42
Removed in size limiting.
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: txmempool.h:72
CAmount GetModFeesWithDescendants() const
Definition: txmempool.h:121
const uint256 & GetHash() const
Definition: transaction.h:401
int64_t feeDelta
Definition: txmempool.h:173
uint64_t GetTotalTxSize() const
Definition: txmempool.h:713
bool operator()(const txiter &a, const txiter &b) const
Definition: txmempool.h:551
uint64_t m_epoch
epoch when last touched, useful for graph algorithms
Definition: txmempool.h:131
uint64_t GetSizeWithDescendants() const
Definition: txmempool.h:120
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:555
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:930
size_t vsize
Virtual size of the transaction.
Definition: txmempool.h:356
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:365
const uint64_t k1
Definition: txmempool.h:378
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:224
bool GetSpendsCoinbase() const
Definition: txmempool.h:123
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:465
const int64_t nTime
Local time when entering the mempool.
Definition: txmempool.h:71
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:318
uint64_t nCountWithDescendants
number of descendant transactions
Definition: txmempool.h:81
int64_t lastRollingFeeUpdate
Definition: txmempool.h:471
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:359
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
Definition: txmempool.h:137
pool TrimToSize(limit, &vNoSpendsRemaining)
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:347
disconnectpool queuedTx clear()
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:64
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
Definition: txmempool.h:316
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:909
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:297
bool blockSinceLastRollingFeeBump
Definition: txmempool.h:472
Removed for reorganization.
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:921
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:35
int64_t nSigOpCostWithAncestors
Definition: txmempool.h:89
void AddUnbroadcastTx(const uint256 &txid, const uint256 &wtxid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:742
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: txmempool.h:69
void UpdateFeeDelta(int64_t feeDelta)
Definition: txmempool.cpp:40
Definition: txmempool.h:293
uint64_t nSizeWithAncestors
Definition: txmempool.h:87
bool exists(const uint256 &txid) const
Definition: txmempool.h:727
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:75
Abstract view on the open txout dataset.
Definition: coins.h:180
size_t DynamicMemoryUsage() const
Definition: txmempool.h:106
unsigned int GetHeight() const
Definition: txmempool.h:103
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Definition: txmempool.h:937
const uint256 & GetWitnessHash() const
Definition: transaction.h:306
#define LOCK(cs)
Definition: sync.h:228
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:124
const uint256 & GetHash() const
Definition: transaction.h:305
const CAmount & GetFee() const
Definition: txmempool.h:99
Removed for conflict with in-block transaction.
DisconnectedBlockTransactions.
Definition: txmempool.h:891
CTransactionRef GetSharedTx() const
Definition: txmempool.h:98
Removed for block.
std::map< uint256, uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:754
mempool UpdateTransactionsFromBlock(vHashUpdate)
bool IsUnbroadcastTx(const uint256 &txid) const
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:760
uint64_t cachedInnerUsage
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:469
Sort an entry by max(score/size of entry&#39;s tx, score/size with all descendants).
Definition: txmempool.h:221
CAmount nModFeesWithAncestors
Definition: txmempool.h:88
Expired from mempool.
std::chrono::seconds GetTime() const
Definition: txmempool.h:102
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
uint64_t nSizeWithDescendants
... and size
Definition: txmempool.h:82
uint256 result_type
Definition: txmempool.h:189
uint64_t GetCountWithDescendants() const
Definition: txmempool.h:119
const size_t nUsageSize
... and total memory usage
Definition: txmempool.h:70
uint64_t totalTxSize
sum of all mempool tx&#39;s virtual sizes. Differs from serialized tx size since witness data is discount...
Definition: txmempool.h:468
bool IsWtxid() const
Definition: transaction.h:400
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:50
int64_t GetSigOpCostWithAncestors() const
Definition: txmempool.h:128
const int64_t sigOpCost
Total sigop cost.
Definition: txmempool.h:74
int flags
Definition: bitcoin-tx.cpp:509
bool visited(Optional< txiter > it) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:847
uint64_t GetCountWithAncestors() const
Definition: txmempool.h:125
256-bit opaque blob.
Definition: uint256.h:123
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:53
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:461
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:515
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:561
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: txmempool.h:76
pool addUnchecked(CTxMemPoolEntry(tx, nFee, nTime, nHeight, spendsCoinbase, sigOpCost, lp))
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:309
const CTransaction & GetTx() const
Definition: txmempool.h:97
const LockPoints & lp
Definition: txmempool.h:183
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost)
Definition: txmempool.h:152
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:730
int64_t GetModifiedFee() const
Definition: txmempool.h:105
unsigned int sigOpCost
uint64_t nCountWithAncestors
Definition: txmempool.h:86
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
int64_t GetSigOpCost() const
Definition: txmempool.h:104
#define GUARDED_BY(x)
Definition: threadsafety.h:40
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: txmempool.h:68
bool spendsCoinbase
size_t operator()(const uint256 &txid) const
Definition: txmempool.h:383
CAmount fee
Fee of the transaction.
Definition: txmempool.h:353
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
update_lock_points(const LockPoints &_lp)
Definition: txmempool.h:178
int64_t time
Definition: txmempool.h:43
size_t DynamicMemoryUsage() const
Definition: txmempool.h:926
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:253
CCoinsView backed by another CCoinsView.
Definition: coins.h:217
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
const LockPoints & GetLockPoints() const
Definition: txmempool.h:107
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:236
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay...
Definition: txmempool.h:266
AssertLockHeld(g_cs_orphans)
const CTxMemPool & mempool
Definition: txmempool.h:868
auto it
Definition: validation.cpp:383
txlinksMap mapLinks
Definition: txmempool.h:569
Removed for replacement.
CBlockPolicyEstimator * minerPolicyEstimator
Definition: txmempool.h:466
uint256 result_type
Definition: txmempool.h:204
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:350
CBlockIndex * maxInputBlock
Definition: txmempool.h:47
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
Definition: txmempool.h:242
double rollingMinimumFeeRate
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:473
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:865
A generic txid reference (txid or wtxid).
Definition: transaction.h:394
EpochGuard: RAII-style guard for using epoch-based graph traversal algorithms.
Definition: txmempool.h:822
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:953
int64_t modifySigOpsCost
Definition: txmempool.h:163
bool m_is_loaded GUARDED_BY(cs)
Definition: txmempool.h:479
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:269
const CTxMemPool & pool
Definition: txmempool.h:823
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:544
Definition: txmempool.h:280
LockPoints lp