BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
txmempool.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <memory>
10 #include <set>
11 #include <map>
12 #include <vector>
13 #include <utility>
14 #include <string>
15 
16 #include <amount.h>
17 #include <coins.h>
18 #include <indirectmap.h>
19 #include <policy/feerate.h>
20 #include <primitives/transaction.h>
21 #include <sync.h>
22 #include <random.h>
23 
24 #include <boost/multi_index_container.hpp>
25 #include <boost/multi_index/hashed_index.hpp>
26 #include <boost/multi_index/ordered_index.hpp>
27 #include <boost/multi_index/sequenced_index.hpp>
28 #include <boost/signals2/signal.hpp>
29 
30 class CBlockIndex;
32 
34 static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
35 
36 struct LockPoints
37 {
38  // Will be set to the blockchain height and median time past
39  // values that would be necessary to satisfy all relative locktime
40  // constraints (BIP68) of this tx given our view of block chain history
41  int height;
42  int64_t time;
43  // As long as the current chain descends from the highest height block
44  // containing one of the inputs used in the calculation, then the cached
45  // values are still valid even after a reorg.
47 
48  LockPoints() : height(0), time(0), maxInputBlock(nullptr) { }
49 };
50 
51 class CTxMemPool;
52 
66 {
67 private:
69  const CAmount nFee;
70  const size_t nTxWeight;
71  const size_t nUsageSize;
72  const int64_t nTime;
73  const unsigned int entryHeight;
74  const bool spendsCoinbase;
75  const int64_t sigOpCost;
76  int64_t feeDelta;
78 
79  // Information about descendants of this transaction that are in the
80  // mempool; if we remove this transaction we must remove all of these
81  // descendants as well.
85 
86  // Analogous statistics for ancestor transactions
91 
92 public:
93  CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
94  int64_t _nTime, unsigned int _entryHeight,
95  bool spendsCoinbase,
96  int64_t nSigOpsCost, LockPoints lp);
97 
98  const CTransaction& GetTx() const { return *this->tx; }
99  CTransactionRef GetSharedTx() const { return this->tx; }
100  const CAmount& GetFee() const { return nFee; }
101  size_t GetTxSize() const;
102  size_t GetTxWeight() const { return nTxWeight; }
103  int64_t GetTime() const { return nTime; }
104  unsigned int GetHeight() const { return entryHeight; }
105  int64_t GetSigOpCost() const { return sigOpCost; }
106  int64_t GetModifiedFee() const { return nFee + feeDelta; }
107  size_t DynamicMemoryUsage() const { return nUsageSize; }
108  const LockPoints& GetLockPoints() const { return lockPoints; }
109 
110  // Adjusts the descendant state.
111  void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
112  // Adjusts the ancestor state
113  void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
114  // Updates the fee delta used for mining priority score, and the
115  // modified fees with descendants.
116  void UpdateFeeDelta(int64_t feeDelta);
117  // Update the LockPoints after a reorg
118  void UpdateLockPoints(const LockPoints& lp);
119 
120  uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
121  uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
123 
124  bool GetSpendsCoinbase() const { return spendsCoinbase; }
125 
126  uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
127  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
130 
131  mutable size_t vTxHashesIdx;
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 
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 
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 
171 
172 private:
173  int64_t feeDelta;
174 };
175 
177 {
178  explicit update_lock_points(const LockPoints& _lp) : lp(_lp) { }
179 
181 
182 private:
183  const LockPoints& lp;
184 };
185 
186 // extracts a transaction hash from CTxMempoolEntry or CTransactionRef
188 {
191  {
192  return entry.GetTx().GetHash();
193  }
194 
196  {
197  return tx->GetHash();
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 
323 
328 {
331 
333  int64_t nTime;
334 
337 
339  int64_t nFeeDelta;
340 };
341 
346  UNKNOWN = 0,
347  EXPIRY,
348  SIZELIMIT,
349  REORG,
350  BLOCK,
351  CONFLICT,
352  REPLACED,
353 };
354 
356 {
357 private:
359  const uint64_t k0, k1;
360 
361 public:
363 
364  size_t operator()(const uint256& txid) const {
365  return SipHashUint256(k0, k1, txid);
366  }
367 };
368 
442 {
443 private:
444  uint32_t nCheckFrequency GUARDED_BY(cs);
445  unsigned int nTransactionsUpdated;
447 
448  uint64_t totalTxSize;
449  uint64_t cachedInnerUsage;
450 
451  mutable int64_t lastRollingFeeUpdate;
453  mutable double rollingMinimumFeeRate;
454 
456 
457 public:
458 
459  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
460 
461  typedef boost::multi_index_container<
463  boost::multi_index::indexed_by<
464  // sorted by txid
465  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
466  // sorted by fee rate
467  boost::multi_index::ordered_non_unique<
468  boost::multi_index::tag<descendant_score>,
469  boost::multi_index::identity<CTxMemPoolEntry>,
471  >,
472  // sorted by entry time
473  boost::multi_index::ordered_non_unique<
474  boost::multi_index::tag<entry_time>,
475  boost::multi_index::identity<CTxMemPoolEntry>,
477  >,
478  // sorted by fee rate with ancestors
479  boost::multi_index::ordered_non_unique<
480  boost::multi_index::tag<ancestor_score>,
481  boost::multi_index::identity<CTxMemPoolEntry>,
483  >
484  >
486 
489 
490  using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
491  std::vector<std::pair<uint256, txiter> > vTxHashes;
492 
494  bool operator()(const txiter &a, const txiter &b) const {
495  return a->GetTx().GetHash() < b->GetTx().GetHash();
496  }
497  };
498  typedef std::set<txiter, CompareIteratorByHash> setEntries;
499 
503 private:
504  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
505 
506  struct TxLinks {
509  };
510 
511  typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
513 
514  void UpdateParent(txiter entry, txiter parent, bool add);
515  void UpdateChild(txiter entry, txiter child, bool add);
516 
517  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
518 
519 public:
520  indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
522 
525  explicit CTxMemPool(CBlockPolicyEstimator* estimator = nullptr);
526 
533  void check(const CCoinsViewCache *pcoins) const;
534  void setSanityCheck(double dFrequency = 1.0) { LOCK(cs); nCheckFrequency = static_cast<uint32_t>(dFrequency * 4294967295.0); }
535 
536  // addUnchecked must updated state for all ancestors of a given transaction,
537  // to track size/count of descendant transactions. First version of
538  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
539  // then invoke the second version.
540  // Note that addUnchecked is ONLY called from ATMP outside of tests
541  // and any other callers may break wallet's in-mempool tracking (due to
542  // lack of CValidationInterface::TransactionAddedToMempool callbacks).
543  void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs);
544  void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs);
545 
547  void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
549  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight);
550 
551  void clear();
552  void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
553  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
554  void queryHashes(std::vector<uint256>& vtxid);
555  bool isSpent(const COutPoint& outpoint) const;
556  unsigned int GetTransactionsUpdated() const;
557  void AddTransactionsUpdated(unsigned int n);
562  bool HasNoInputsOf(const CTransaction& tx) const;
563 
565  void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
566  void ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const;
567  void ClearPrioritisation(const uint256 hash);
568 
570  const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
571 
573  boost::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
574 
576  setEntries GetIterSet(const std::set<uint256>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
577 
586 
596  void UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate);
597 
608  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);
609 
613  void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
614 
621  CFeeRate GetMinFee(size_t sizelimit) const;
622 
627  void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining=nullptr);
628 
630  int Expire(int64_t time);
631 
636  void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) const;
637 
638  unsigned long size()
639  {
640  LOCK(cs);
641  return mapTx.size();
642  }
643 
644  uint64_t GetTotalTxSize() const
645  {
646  LOCK(cs);
647  return totalTxSize;
648  }
649 
650  bool exists(const uint256& hash) const
651  {
652  LOCK(cs);
653  return (mapTx.count(hash) != 0);
654  }
655 
656  CTransactionRef get(const uint256& hash) const;
657  TxMempoolInfo info(const uint256& hash) const;
658  std::vector<TxMempoolInfo> infoAll() const;
659 
660  size_t DynamicMemoryUsage() const;
661 
662  boost::signals2::signal<void (CTransactionRef)> NotifyEntryAdded;
663  boost::signals2::signal<void (CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved;
664 
665 private:
679  void UpdateForDescendants(txiter updateIt,
680  cacheMap &cachedDescendants,
681  const std::set<uint256> &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs);
683  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
689  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
692 
702 };
703 
716 {
717 protected:
719 
720 public:
721  CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
722  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
723 };
724 
740 // multi_index tag names
741 struct txid_index {};
742 struct insertion_order {};
743 
745  typedef boost::multi_index_container<
747  boost::multi_index::indexed_by<
748  // sorted by txid
749  boost::multi_index::hashed_unique<
750  boost::multi_index::tag<txid_index>,
753  >,
754  // sorted by order in the blockchain
755  boost::multi_index::sequenced<
756  boost::multi_index::tag<insertion_order>
757  >
758  >
760 
761  // It's almost certainly a logic bug if we don't clear out queuedTx before
762  // destruction, as we add to it while disconnecting blocks, and then we
763  // need to re-process remaining transactions to ensure mempool consistency.
764  // For now, assert() that we've emptied out this object on destruction.
765  // This assert() can always be removed if the reorg-processing code were
766  // to be refactored such that this assumption is no longer true (for
767  // instance if there was some other way we cleaned up the mempool after a
768  // reorg, besides draining this object).
770 
772  uint64_t cachedInnerUsage = 0;
773 
774  // Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
775  // no exact formula for boost::multi_index_contained is implemented.
776  size_t DynamicMemoryUsage() const {
777  return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
778  }
779 
781  {
782  queuedTx.insert(tx);
783  cachedInnerUsage += RecursiveDynamicUsage(tx);
784  }
785 
786  // Remove entries based on txid_index, and update memory usage.
787  void removeForBlock(const std::vector<CTransactionRef>& vtx)
788  {
789  // Short-circuit in the common case of a block being added to the tip
790  if (queuedTx.empty()) {
791  return;
792  }
793  for (auto const &tx : vtx) {
794  auto it = queuedTx.find(tx->GetHash());
795  if (it != queuedTx.end()) {
796  cachedInnerUsage -= RecursiveDynamicUsage(*it);
797  queuedTx.erase(it);
798  }
799  }
800  }
801 
802  // Remove an entry by insertion_order index, and update memory usage.
803  void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
804  {
805  cachedInnerUsage -= RecursiveDynamicUsage(*entry);
806  queuedTx.get<insertion_order>().erase(entry);
807  }
808 
809  void clear()
810  {
811  cachedInnerUsage = 0;
812  queuedTx.clear();
813  }
814 };
815 
816 #endif // BITCOIN_TXMEMPOOL_H
CAmount GetModFeesWithAncestors() const
Definition: txmempool.h:128
size_t vTxHashesIdx
Index in mempool&#39;s vTxHashes.
Definition: txmempool.h:131
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: txmempool.h:74
Information about a mempool transaction.
Definition: txmempool.h:327
int Expire(int64_t time)
Expire all transaction (and their dependencies) in the mempool older than time.
Definition: txmempool.cpp:925
update_fee_delta(int64_t _feeDelta)
Definition: txmempool.h:168
const CTransactionRef tx
Definition: txmempool.h:68
uint64_t GetSizeWithAncestors() const
Definition: txmempool.h:127
CAmount nModFeesWithDescendants
... and total fees (all including us)
Definition: txmempool.h:84
void UpdateLockPoints(const LockPoints &lp)
Definition: txmempool.cpp:46
std::map< txiter, TxLinks, CompareIteratorByHash > txlinksMap
Definition: txmempool.h:511
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:490
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:784
Definition: init.h:16
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Definition: txmempool.cpp:460
uint32_t nCheckFrequency GUARDED_BY(cs)
Value n means that n times in 2^32 we check.
A UTXO entry.
Definition: coins.h:29
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: txmempool.cpp:492
size_t GetTxSize() const
Definition: txmempool.cpp:51
size_t GetTxWeight() const
Definition: txmempool.h:102
boost::signals2::signal< void(CTransactionRef)> NotifyEntryAdded
Definition: txmempool.h:662
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:437
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:911
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:895
LockPoints()
Definition: txmempool.h:48
Manually removed or unknown reason.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:267
int height
Definition: txmempool.h:41
TxMempoolInfo info(const uint256 &hash) const
Definition: txmempool.cpp:807
Removed in size limiting.
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:529
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: txmempool.h:73
CAmount GetModFeesWithDescendants() const
Definition: txmempool.h:122
void clear()
Definition: txmempool.cpp:591
int64_t feeDelta
Definition: txmempool.h:173
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:168
uint64_t GetTotalTxSize() const
Definition: txmempool.h:644
bool operator()(const txiter &a, const txiter &b) const
Definition: txmempool.h:494
bool HasNoInputsOf(const CTransaction &tx) const
Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on o...
Definition: txmempool.cpp:885
uint64_t GetSizeWithDescendants() const
Definition: txmempool.h:121
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:498
void queryHashes(std::vector< uint256 > &vtxid)
Definition: txmempool.cpp:767
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:780
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:141
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:345
const uint64_t k1
Definition: txmempool.h:359
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:208
bool GetSpendsCoinbase() const
Definition: txmempool.h:124
const int64_t nTime
Local time when entering the mempool.
Definition: txmempool.h:72
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
Definition: txmempool.cpp:1018
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:315
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:402
uint64_t nCountWithDescendants
number of descendant transactions
Definition: txmempool.h:82
int64_t lastRollingFeeUpdate
Definition: txmempool.h:451
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:180
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:339
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:337
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
Definition: txmempool.h:137
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate)
When adding transactions from a disconnected block back to the mempool, new mempool entries may have ...
Definition: txmempool.cpp:106
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:330
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:917
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:65
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:606
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:759
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:211
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:281
bool blockSinceLastRollingFeeBump
Definition: txmempool.h:452
Removed for reorganization.
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:771
int64_t nSigOpCostWithAncestors
Definition: txmempool.h:90
void setSanityCheck(double dFrequency=1.0)
Definition: txmempool.h:534
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: txmempool.h:70
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:190
std::vector< std::pair< uint256, txiter > > vTxHashes
All tx witness hashes/entries in mapTx, in random order.
Definition: txmempool.h:491
void UpdateFeeDelta(int64_t feeDelta)
Definition: txmempool.cpp:39
void removeUnchecked(txiter entry, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
Definition: txmempool.cpp:406
Definition: txmempool.h:277
unsigned long size()
Definition: txmempool.h:638
uint64_t nSizeWithAncestors
Definition: txmempool.h:88
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:76
Abstract view on the open txout dataset.
Definition: coins.h:145
size_t DynamicMemoryUsage() const
Definition: txmempool.h:107
unsigned int GetHeight() const
Definition: txmempool.h:104
void ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const
Definition: txmempool.cpp:846
bool exists(const uint256 &hash) const
Definition: txmempool.h:650
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Definition: txmempool.h:787
#define LOCK(cs)
Definition: sync.h:181
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:136
const uint256 & GetHash() const
Definition: transaction.h:316
const CAmount & GetFee() const
Definition: txmempool.h:100
Removed for conflict with in-block transaction.
DisconnectedBlockTransactions.
Definition: txmempool.h:741
CTransactionRef GetSharedTx() const
Definition: txmempool.h:99
Removed for block.
void UpdateParent(txiter entry, txiter parent, bool add)
Definition: txmempool.cpp:960
std::map< uint256, CAmount > mapDeltas
Definition: txmempool.h:521
const uint64_t k0
Salt.
Definition: txmempool.h:359
CCriticalSection cs_main
Definition: validation.cpp:216
uint64_t cachedInnerUsage
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:449
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:459
CAmount nModFeesWithAncestors
Definition: txmempool.h:89
const setEntries & GetMemPoolChildren(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:978
unsigned int nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:445
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:862
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:149
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
uint64_t nSizeWithDescendants
... and size
Definition: txmempool.h:83
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:349
uint256 result_type
Definition: txmempool.h:189
uint64_t GetCountWithDescendants() const
Definition: txmempool.h:120
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:986
const size_t nUsageSize
... and total memory usage
Definition: txmempool.h:71
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:448
CCriticalSection cs
Definition: txmempool.h:487
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1061
int64_t GetSigOpCostWithAncestors() const
Definition: txmempool.h:129
const int64_t sigOpCost
Total sigop cost.
Definition: txmempool.h:75
uint64_t GetCountWithAncestors() const
Definition: txmempool.h:126
int64_t nTime
Time the transaction entered the mempool.
Definition: txmempool.h:333
256-bit opaque blob.
Definition: uint256.h:122
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:753
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:441
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb)
Definition: txmempool.cpp:722
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:156
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:504
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
int64_t GetTime() const
Definition: txmempool.h:103
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: txmempool.h:77
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:306
const CTransaction & GetTx() const
Definition: txmempool.h:98
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:578
const LockPoints & lp
Definition: txmempool.h:183
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:226
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost)
Definition: txmempool.h:152
int64_t GetModifiedFee() const
Definition: txmempool.h:106
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:875
boost::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:868
boost::multi_index_container< CTxMemPoolEntry, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< mempoolentry_txid, 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:485
uint64_t nCountWithAncestors
Definition: txmempool.h:87
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
int64_t GetSigOpCost() const
Definition: txmempool.h:105
void UpdateChild(txiter entry, txiter child, bool add)
Definition: txmempool.cpp:950
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:343
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: txmempool.h:69
size_t operator()(const uint256 &txid) const
Definition: txmempool.h:364
const setEntries & GetMemPoolParents(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:970
update_lock_points(const LockPoints &_lp)
Definition: txmempool.h:178
void ClearPrioritisation(const uint256 hash)
Definition: txmempool.cpp:856
int64_t time
Definition: txmempool.h:42
boost::signals2::signal< void(CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved
Definition: txmempool.h:663
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1010
int flags
Definition: bsha3-tx.cpp:509
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight)
Called when a block is connected.
Definition: txmempool.cpp:549
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:893
size_t DynamicMemoryUsage() const
Definition: txmempool.h:776
CFeeRate feeRate
Feerate of the transaction.
Definition: txmempool.h:336
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:264
CCoinsView backed by another CCoinsView.
Definition: coins.h:182
const LockPoints & GetLockPoints() const
Definition: txmempool.h:108
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:201
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay...
Definition: txmempool.h:250
const CTxMemPool & mempool
Definition: txmempool.h:718
txlinksMap mapLinks
Definition: txmempool.h:512
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:240
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:59
CBlockPolicyEstimator * minerPolicyEstimator
Definition: txmempool.h:446
CBlockIndex * maxInputBlock
Definition: txmempool.h:46
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
Definition: txmempool.h:226
double rollingMinimumFeeRate
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:453
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:715
CTxMemPoolEntry(const CTransactionRef &_tx, const CAmount &_nFee, int64_t _nTime, unsigned int _entryHeight, bool spendsCoinbase, int64_t nSigOpsCost, LockPoints lp)
Definition: txmempool.cpp:21
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:816
void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:941
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:803
int64_t modifySigOpsCost
Definition: txmempool.h:163
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:248
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:170
Definition: txmempool.h:264
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:1083