Bitcoin Core  0.21.1
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules
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 
53  // SFINAE for T where T is either a pointer type (e.g., a txiter) or a reference_wrapper<T>
54  // (e.g. a wrapped CTxMemPoolEntry&)
55  template <typename T>
56  bool operator()(const std::reference_wrapper<T>& a, const std::reference_wrapper<T>& b) const
57  {
58  return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();
59  }
60  template <typename T>
61  bool operator()(const T& a, const T& b) const
62  {
63  return a->GetTx().GetHash() < b->GetTx().GetHash();
64  }
65 };
79 {
80 public:
81  typedef std::reference_wrapper<const CTxMemPoolEntry> CTxMemPoolEntryRef;
82  // two aliases, should the types ever diverge
83  typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Parents;
84  typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Children;
85 
86 private:
88  mutable Parents m_parents;
89  mutable Children m_children;
90  const CAmount nFee;
91  const size_t nTxWeight;
92  const size_t nUsageSize;
93  const int64_t nTime;
94  const unsigned int entryHeight;
95  const bool spendsCoinbase;
96  const int64_t sigOpCost;
97  int64_t feeDelta;
99 
100  // Information about descendants of this transaction that are in the
101  // mempool; if we remove this transaction we must remove all of these
102  // descendants as well.
106 
107  // Analogous statistics for ancestor transactions
112 
113 public:
114  CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
115  int64_t _nTime, unsigned int _entryHeight,
116  bool spendsCoinbase,
117  int64_t nSigOpsCost, LockPoints lp);
118 
119  const CTransaction& GetTx() const { return *this->tx; }
120  CTransactionRef GetSharedTx() const { return this->tx; }
121  const CAmount& GetFee() const { return nFee; }
122  size_t GetTxSize() const;
123  size_t GetTxWeight() const { return nTxWeight; }
124  std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
125  unsigned int GetHeight() const { return entryHeight; }
126  int64_t GetSigOpCost() const { return sigOpCost; }
127  int64_t GetModifiedFee() const { return nFee + feeDelta; }
128  size_t DynamicMemoryUsage() const { return nUsageSize; }
129  const LockPoints& GetLockPoints() const { return lockPoints; }
130 
131  // Adjusts the descendant state.
132  void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
133  // Adjusts the ancestor state
134  void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
135  // Updates the fee delta used for mining priority score, and the
136  // modified fees with descendants.
137  void UpdateFeeDelta(int64_t feeDelta);
138  // Update the LockPoints after a reorg
139  void UpdateLockPoints(const LockPoints& lp);
140 
141  uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
142  uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
144 
145  bool GetSpendsCoinbase() const { return spendsCoinbase; }
146 
147  uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
148  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
151 
152  const Parents& GetMemPoolParentsConst() const { return m_parents; }
153  const Children& GetMemPoolChildrenConst() const { return m_children; }
154  Parents& GetMemPoolParents() const { return m_parents; }
155  Children& GetMemPoolChildren() const { return m_children; }
156 
157  mutable size_t vTxHashesIdx;
158  mutable uint64_t m_epoch;
159 };
160 
161 // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
163 {
164  update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
165  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
166  {}
167 
170 
171  private:
172  int64_t modifySize;
174  int64_t modifyCount;
175 };
176 
178 {
179  update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost) :
180  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOpsCost(_modifySigOpsCost)
181  {}
182 
185 
186  private:
187  int64_t modifySize;
189  int64_t modifyCount;
191 };
192 
194 {
195  explicit update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
196 
198 
199 private:
200  int64_t feeDelta;
201 };
202 
204 {
205  explicit update_lock_points(const LockPoints& _lp) : lp(_lp) { }
206 
208 
209 private:
210  const LockPoints& lp;
211 };
212 
213 // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
215 {
217  result_type operator() (const CTxMemPoolEntry &entry) const
218  {
219  return entry.GetTx().GetHash();
220  }
221 
222  result_type operator() (const CTransactionRef& tx) const
223  {
224  return tx->GetHash();
225  }
226 };
227 
228 // extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
230 {
232  result_type operator() (const CTxMemPoolEntry &entry) const
233  {
234  return entry.GetTx().GetWitnessHash();
235  }
236 
237  result_type operator() (const CTransactionRef& tx) const
238  {
239  return tx->GetWitnessHash();
240  }
241 };
242 
243 
249 {
250 public:
251  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
252  {
253  double a_mod_fee, a_size, b_mod_fee, b_size;
254 
255  GetModFeeAndSize(a, a_mod_fee, a_size);
256  GetModFeeAndSize(b, b_mod_fee, b_size);
257 
258  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
259  double f1 = a_mod_fee * b_size;
260  double f2 = a_size * b_mod_fee;
261 
262  if (f1 == f2) {
263  return a.GetTime() >= b.GetTime();
264  }
265  return f1 < f2;
266  }
267 
268  // Return the fee/size we're using for sorting this entry.
269  void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
270  {
271  // Compare feerate with descendants to feerate of the transaction, and
272  // return the fee/size for the max.
273  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
274  double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
275 
276  if (f2 > f1) {
277  mod_fee = a.GetModFeesWithDescendants();
278  size = a.GetSizeWithDescendants();
279  } else {
280  mod_fee = a.GetModifiedFee();
281  size = a.GetTxSize();
282  }
283  }
284 };
285 
294 {
295 public:
296  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
297  {
298  double f1 = (double)a.GetFee() * b.GetTxSize();
299  double f2 = (double)b.GetFee() * a.GetTxSize();
300  if (f1 == f2) {
301  return b.GetTx().GetHash() < a.GetTx().GetHash();
302  }
303  return f1 > f2;
304  }
305 };
306 
308 {
309 public:
310  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
311  {
312  return a.GetTime() < b.GetTime();
313  }
314 };
315 
321 {
322 public:
323  template<typename T>
324  bool operator()(const T& a, const T& b) const
325  {
326  double a_mod_fee, a_size, b_mod_fee, b_size;
327 
328  GetModFeeAndSize(a, a_mod_fee, a_size);
329  GetModFeeAndSize(b, b_mod_fee, b_size);
330 
331  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
332  double f1 = a_mod_fee * b_size;
333  double f2 = a_size * b_mod_fee;
334 
335  if (f1 == f2) {
336  return a.GetTx().GetHash() < b.GetTx().GetHash();
337  }
338  return f1 > f2;
339  }
340 
341  // Return the fee/size we're using for sorting this entry.
342  template <typename T>
343  void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
344  {
345  // Compare feerate with ancestors to feerate of the transaction, and
346  // return the fee/size for the min.
347  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
348  double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
349 
350  if (f1 > f2) {
351  mod_fee = a.GetModFeesWithAncestors();
352  size = a.GetSizeWithAncestors();
353  } else {
354  mod_fee = a.GetModifiedFee();
355  size = a.GetTxSize();
356  }
357  }
358 };
359 
360 // Multi_index tag names
362 struct entry_time {};
363 struct ancestor_score {};
364 struct index_by_wtxid {};
365 
367 
372 {
375 
377  std::chrono::seconds m_time;
378 
381 
383  size_t vsize;
384 
386  int64_t nFeeDelta;
387 };
388 
393  EXPIRY,
394  SIZELIMIT,
395  REORG,
396  BLOCK,
397  CONFLICT,
398  REPLACED,
399 };
400 
402 {
403 private:
405  const uint64_t k0, k1;
406 
407 public:
409 
410  size_t operator()(const uint256& txid) const {
411  return SipHashUint256(k0, k1, txid);
412  }
413 };
414 
489 {
490 private:
491  uint32_t nCheckFrequency GUARDED_BY(cs);
492  std::atomic<unsigned int> nTransactionsUpdated;
494 
495  uint64_t totalTxSize;
496  uint64_t cachedInnerUsage;
497 
498  mutable int64_t lastRollingFeeUpdate;
500  mutable double rollingMinimumFeeRate;
501  mutable uint64_t m_epoch;
502  mutable bool m_has_epoch_guard;
503 
504  // In-memory counter for external mempool tracking purposes.
505  // This number is incremented once every time a transaction
506  // is added or removed from the mempool for any reason.
507  mutable uint64_t m_sequence_number{1};
508 
510 
511  bool m_is_loaded GUARDED_BY(cs){false};
512 
513 public:
514 
515  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
516 
517  typedef boost::multi_index_container<
519  boost::multi_index::indexed_by<
520  // sorted by txid
521  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
522  // sorted by wtxid
523  boost::multi_index::hashed_unique<
524  boost::multi_index::tag<index_by_wtxid>,
527  >,
528  // sorted by fee rate
529  boost::multi_index::ordered_non_unique<
530  boost::multi_index::tag<descendant_score>,
531  boost::multi_index::identity<CTxMemPoolEntry>,
533  >,
534  // sorted by entry time
535  boost::multi_index::ordered_non_unique<
536  boost::multi_index::tag<entry_time>,
537  boost::multi_index::identity<CTxMemPoolEntry>,
539  >,
540  // sorted by fee rate with ancestors
541  boost::multi_index::ordered_non_unique<
542  boost::multi_index::tag<ancestor_score>,
543  boost::multi_index::identity<CTxMemPoolEntry>,
545  >
546  >
548 
578 
579  using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
580  std::vector<std::pair<uint256, txiter>> vTxHashes GUARDED_BY(cs);
581 
582  typedef std::set<txiter, CompareIteratorByHash> setEntries;
583 
585 private:
586  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
587 
588 
589  void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
590  void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
591 
592  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
593 
597  std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
598 
599 public:
600  indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
602 
605  explicit CTxMemPool(CBlockPolicyEstimator* estimator = nullptr);
606 
613  void check(const CCoinsViewCache *pcoins) const;
614  void setSanityCheck(double dFrequency = 1.0) { LOCK(cs); nCheckFrequency = static_cast<uint32_t>(dFrequency * 4294967295.0); }
615 
616  // addUnchecked must updated state for all ancestors of a given transaction,
617  // to track size/count of descendant transactions. First version of
618  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
619  // then invoke the second version.
620  // Note that addUnchecked is ONLY called from ATMP outside of tests
621  // and any other callers may break wallet's in-mempool tracking (due to
622  // lack of CValidationInterface::TransactionAddedToMempool callbacks).
623  void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
624  void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
625 
627  void removeForReorg(const CCoinsViewCache* pcoins, unsigned int nMemPoolHeight, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
629  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
630 
631  void clear();
632  void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
633  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
634  void queryHashes(std::vector<uint256>& vtxid) const;
635  bool isSpent(const COutPoint& outpoint) const;
636  unsigned int GetTransactionsUpdated() const;
637  void AddTransactionsUpdated(unsigned int n);
642  bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
643 
645  void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
646  void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
648 
650  const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
651 
653  Optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
654 
656  setEntries GetIterSet(const std::set<uint256>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
657 
665  void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
666 
676  void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
677 
688  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);
689 
693  void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
694 
701  CFeeRate GetMinFee(size_t sizelimit) const;
702 
707  void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
708 
710  int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
711 
716  void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) const;
717 
719  bool IsLoaded() const;
720 
722  void SetIsLoaded(bool loaded);
723 
724  unsigned long size() const
725  {
726  LOCK(cs);
727  return mapTx.size();
728  }
729 
731  {
732  AssertLockHeld(cs);
733  return totalTxSize;
734  }
735 
736  bool exists(const GenTxid& gtxid) const
737  {
738  LOCK(cs);
739  if (gtxid.IsWtxid()) {
740  return (mapTx.get<index_by_wtxid>().count(gtxid.GetHash()) != 0);
741  }
742  return (mapTx.count(gtxid.GetHash()) != 0);
743  }
744  bool exists(const uint256& txid) const { return exists(GenTxid{false, txid}); }
745 
746  CTransactionRef get(const uint256& hash) const;
748  {
749  AssertLockHeld(cs);
750  return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
751  }
752  TxMempoolInfo info(const uint256& hash) const;
753  TxMempoolInfo info(const GenTxid& gtxid) const;
754  std::vector<TxMempoolInfo> infoAll() const;
755 
756  size_t DynamicMemoryUsage() const;
757 
759  void AddUnbroadcastTx(const uint256& txid)
760  {
761  LOCK(cs);
762  // Sanity check the transaction is in the mempool & insert into
763  // unbroadcast set.
764  if (exists(txid)) m_unbroadcast_txids.insert(txid);
765  };
766 
768  void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
769 
771  std::set<uint256> GetUnbroadcastTxs() const
772  {
773  LOCK(cs);
774  return m_unbroadcast_txids;
775  }
776 
779  {
780  AssertLockHeld(cs);
781  return m_unbroadcast_txids.count(txid) != 0;
782  }
783 
786  return m_sequence_number++;
787  }
788 
789  uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs) {
790  return m_sequence_number;
791  }
792 
793 private:
807  void UpdateForDescendants(txiter updateIt,
808  cacheMap &cachedDescendants,
809  const std::set<uint256> &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs);
811  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
813  void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
817  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
820 
830 public:
850  class EpochGuard {
851  const CTxMemPool& pool;
852  public:
853  EpochGuard(const CTxMemPool& in);
854  ~EpochGuard();
855  };
856  // N.B. GetFreshEpoch modifies mutable state via the EpochGuard construction
857  // (and later destruction)
859 
869  assert(m_has_epoch_guard);
870  bool ret = it->m_epoch >= m_epoch;
871  it->m_epoch = std::max(it->m_epoch, m_epoch);
872  return ret;
873  }
874 
876  assert(m_has_epoch_guard);
877  return !it || visited(*it);
878  }
879 };
880 
894 {
895 protected:
897 
898 public:
899  CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
900  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
901 };
902 
918 // multi_index tag names
919 struct txid_index {};
920 struct insertion_order {};
921 
923  typedef boost::multi_index_container<
925  boost::multi_index::indexed_by<
926  // sorted by txid
927  boost::multi_index::hashed_unique<
928  boost::multi_index::tag<txid_index>,
931  >,
932  // sorted by order in the blockchain
933  boost::multi_index::sequenced<
934  boost::multi_index::tag<insertion_order>
935  >
936  >
938 
939  // It's almost certainly a logic bug if we don't clear out queuedTx before
940  // destruction, as we add to it while disconnecting blocks, and then we
941  // need to re-process remaining transactions to ensure mempool consistency.
942  // For now, assert() that we've emptied out this object on destruction.
943  // This assert() can always be removed if the reorg-processing code were
944  // to be refactored such that this assumption is no longer true (for
945  // instance if there was some other way we cleaned up the mempool after a
946  // reorg, besides draining this object).
948 
950  uint64_t cachedInnerUsage = 0;
951 
952  // Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
953  // no exact formula for boost::multi_index_contained is implemented.
954  size_t DynamicMemoryUsage() const {
955  return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
956  }
957 
958  void addTransaction(const CTransactionRef& tx)
959  {
960  queuedTx.insert(tx);
961  cachedInnerUsage += RecursiveDynamicUsage(tx);
962  }
963 
964  // Remove entries based on txid_index, and update memory usage.
965  void removeForBlock(const std::vector<CTransactionRef>& vtx)
966  {
967  // Short-circuit in the common case of a block being added to the tip
968  if (queuedTx.empty()) {
969  return;
970  }
971  for (auto const &tx : vtx) {
972  auto it = queuedTx.find(tx->GetHash());
973  if (it != queuedTx.end()) {
974  cachedInnerUsage -= RecursiveDynamicUsage(*it);
975  queuedTx.erase(it);
976  }
977  }
978  }
979 
980  // Remove an entry by insertion_order index, and update memory usage.
981  void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
982  {
983  cachedInnerUsage -= RecursiveDynamicUsage(*entry);
984  queuedTx.get<insertion_order>().erase(entry);
985  }
986 
987  void clear()
988  {
989  cachedInnerUsage = 0;
990  queuedTx.clear();
991  }
992 };
993 
994 #endif // BITCOIN_TXMEMPOOL_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:395
size_t vTxHashesIdx
Index in mempool's vTxHashes.
Definition: txmempool.h:157
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: txmempool.h:95
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.cpp:1000
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:371
CAmount GetModFeesWithAncestors() const
Definition: txmempool.h:149
update_fee_delta(int64_t _feeDelta)
Definition: txmempool.h:195
const CTransactionRef tx
Definition: txmempool.h:87
bool m_has_epoch_guard
Definition: txmempool.h:502
CAmount nModFeesWithDescendants
... and total fees (all including us)
Definition: txmempool.h:105
void UpdateLockPoints(const LockPoints &lp)
Definition: txmempool.cpp:47
uint64_t m_sequence_number
Definition: txmempool.h:507
Optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:886
unsigned int GetHeight() const
Definition: txmempool.h:125
uint64_t m_epoch
Definition: txmempool.h:501
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:579
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:129
uint64_t GetCountWithAncestors() const
Definition: txmempool.h:147
uint32_t nCheckFrequency GUARDED_BY(cs)
Value n means that n times in 2^32 we check.
uint64_t GetCountWithDescendants() const
Definition: txmempool.h:141
A UTXO entry.
Definition: coins.h:30
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
When adding transactions from a disconnected block back to the mempool, new mempool entries may have ...
Definition: txmempool.cpp:107
size_t GetTxWeight() const
Definition: txmempool.h:123
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:453
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:913
LockPoints()
Definition: txmempool.h:49
int height
Definition: txmempool.h:42
Removed in size limiting.
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:544
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:736
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: txmempool.h:94
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:251
std::chrono::seconds GetTime() const
Definition: txmempool.h:124
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:944
void clear()
Definition: txmempool.cpp:605
int64_t feeDelta
Definition: txmempool.h:200
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:789
uint64_t m_epoch
epoch when last touched, useful for graph algorithms
Definition: txmempool.h:158
size_t DynamicMemoryUsage() const
Definition: txmempool.h:128
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
Definition: txmempool.cpp:738
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:582
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:958
size_t vsize
Virtual size of the transaction.
Definition: txmempool.h:383
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:168
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:392
const uint64_t k1
Definition: txmempool.h:405
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:929
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:492
const int64_t nTime
Local time when entering the mempool.
Definition: txmempool.h:93
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:477
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs
Definition: txmempool.cpp:507
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:323
uint64_t nCountWithDescendants
number of descendant transactions
Definition: txmempool.h:103
int64_t lastRollingFeeUpdate
Definition: txmempool.h:498
const LockPoints & GetLockPoints() const
Definition: txmempool.h:129
const Children & GetMemPoolChildrenConst() const
Definition: txmempool.h:153
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:207
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:310
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:386
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
Definition: txmempool.h:164
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:800
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:374
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:78
bool visited(txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs)
visited marks a CTxMemPoolEntry as having been traversed during the lifetime of the most recently cre...
Definition: txmempool.h:868
void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) EXCLUSIVE_LOCKS_REQUIRED(cs
Definition: txmempool.cpp:969
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:351
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:937
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:217
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint64_t GetSizeWithDescendants() const
Definition: txmempool.h:142
bool blockSinceLastRollingFeeBump
Definition: txmempool.h:499
Removed for reorganization.
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:949
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
const CAmount & GetFee() const
Definition: txmempool.h:121
int64_t nSigOpCostWithAncestors
Definition: txmempool.h:111
void setSanityCheck(double dFrequency=1.0)
Definition: txmempool.h:614
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: txmempool.h:91
bool exists(const uint256 &txid) const
Definition: txmempool.h:744
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
Definition: txmempool.h:83
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:759
void UpdateFeeDelta(int64_t feeDelta)
Definition: txmempool.cpp:40
void queryHashes(std::vector< uint256 > &vtxid) const
Definition: txmempool.cpp:783
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) const
Definition: txmempool.h:56
Parents m_parents
Definition: txmempool.h:88
CTransactionRef GetSharedTx() const
Definition: txmempool.h:120
Definition: txmempool.h:320
uint64_t nSizeWithAncestors
Definition: txmempool.h:109
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:97
Abstract view on the open txout dataset.
Definition: coins.h:180
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:952
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Definition: txmempool.h:965
TxMempoolInfo info(const uint256 &hash) const
Definition: txmempool.cpp:832
#define LOCK(cs)
Definition: sync.h:230
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:124
Removed for conflict with in-block transaction.
DisconnectedBlockTransactions.
Definition: txmempool.h:919
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
Definition: txmempool.h:269
size_t DynamicMemoryUsage() const
Definition: txmempool.h:954
CAmount GetModFeesWithDescendants() const
Definition: txmempool.h:143
AssertLockHeld(mempool.cs)
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
Definition: txmempool.h:343
Removed for block.
Parents & GetMemPoolParents() const
Definition: txmempool.h:154
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:410
std::map< uint256, CAmount > mapDeltas
Definition: txmempool.h:601
const uint64_t k0
Salt.
Definition: txmempool.h:405
Children & GetMemPoolChildren() const
Definition: txmempool.h:155
int64_t GetSigOpCost() const
Definition: txmempool.h:126
bool GetSpendsCoinbase() const
Definition: txmempool.h:145
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:345
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:61
uint64_t cachedInnerUsage
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:496
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
Definition: txmempool.h:248
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:515
bool IsWtxid() const
Definition: transaction.h:406
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:730
CAmount nModFeesWithAncestors
Definition: txmempool.h:110
Expired from mempool.
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:880
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:152
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
uint64_t nSizeWithDescendants
... and size
Definition: txmempool.h:104
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:356
uint256 result_type
Definition: txmempool.h:216
const uint256 & GetWitnessHash() const
Definition: transaction.h:312
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:864
uint64_t GetSizeWithAncestors() const
Definition: txmempool.h:148
const size_t nUsageSize
... and total memory usage
Definition: txmempool.h:92
uint64_t totalTxSize
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
Definition: txmempool.h:495
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1075
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:296
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:50
const uint256 & GetHash() const
Definition: transaction.h:407
const int64_t sigOpCost
Total sigop cost.
Definition: txmempool.h:96
void GetTransactionAncestry(const uint256 &txid, size_t &ancestors, size_t &descendants) const
Calculate the ancestor and descendant count for the given transaction.
Definition: txmempool.cpp:1097
int flags
Definition: bitcoin-tx.cpp:506
EpochGuard(const CTxMemPool &in)
Definition: txmempool.cpp:1124
bool visited(Optional< txiter > it) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:875
bool IsLoaded() const
Definition: txmempool.cpp:1107
256-bit opaque blob.
Definition: uint256.h:124
const CTransaction & GetTx() const
Definition: txmempool.h:119
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:785
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:769
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:488
int64_t GetModifiedFee() const
Definition: txmempool.h:127
void check(const CCoinsViewCache *pcoins) const
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.cpp:620
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:903
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:547
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:183
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:586
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
EpochGuard GetFreshEpoch() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1120
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: txmempool.h:98
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:314
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:593
const LockPoints & lp
Definition: txmempool.h:210
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:232
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost)
Definition: txmempool.h:179
std::reference_wrapper< const CTxMemPoolEntry > CTxMemPoolEntryRef
Definition: txmempool.h:81
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:771
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:747
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:978
void ClearPrioritisation(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:874
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:893
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Children
Definition: txmempool.h:84
uint64_t nCountWithAncestors
Definition: txmempool.h:108
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: txmempool.h:90
CAmount fee
Fee of the transaction.
Definition: txmempool.h:380
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
update_lock_points(const LockPoints &_lp)
Definition: txmempool.h:205
int64_t time
Definition: txmempool.h:43
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:1032
const uint256 & GetHash() const
Definition: transaction.h:311
int64_t GetSigOpCostWithAncestors() const
Definition: txmempool.h:150
void RemoveUnbroadcastTx(const uint256 &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:935
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1024
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:911
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:232
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:324
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
CCoinsView backed by another CCoinsView.
Definition: coins.h:217
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
bool IsUnbroadcastTx(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:778
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:293
size_t operator()(const uint256 &txid) const
Definition: txmempool.h:410
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:989
const CTxMemPool & mempool
Definition: txmempool.h:896
auto it
Definition: validation.cpp:381
void SetIsLoaded(bool loaded)
Sets the current loaded state.
Definition: txmempool.cpp:1113
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:246
Removed for replacement.
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
Definition: txmempool.cpp:60
CBlockPolicyEstimator * minerPolicyEstimator
Definition: txmempool.h:493
Children m_children
Definition: txmempool.h:89
uint256 result_type
Definition: txmempool.h:231
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:377
CBlockIndex * maxInputBlock
Definition: txmempool.h:47
double rollingMinimumFeeRate
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:500
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:893
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:564
CTxMemPoolEntry(const CTransactionRef &_tx, const CAmount &_nFee, int64_t _nTime, unsigned int _entryHeight, bool spendsCoinbase, int64_t nSigOpsCost, LockPoints lp)
Definition: txmempool.cpp:22
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:834
A generic txid reference (txid or wtxid).
Definition: transaction.h:400
EpochGuard: RAII-style guard for using epoch-based graph traversal algorithms.
Definition: txmempool.h:850
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:981
int64_t modifySigOpsCost
Definition: txmempool.h:190
const Parents & GetMemPoolParentsConst() const
Definition: txmempool.h:152
bool m_is_loaded GUARDED_BY(cs)
Definition: txmempool.h:511
const CTxMemPool & pool
Definition: txmempool.h:851
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:254
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:576
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:197
Definition: txmempool.h:307
size_t GetTxSize() const
Definition: txmempool.cpp:52
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:217
unsigned long size() const
Definition: txmempool.h:724
LockPoints lp