Dogecoin Core  1.14.2
P2P Digital Currency
txmempool.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 "primitives/transaction.h"
20 #include "sync.h"
21 #include "random.h"
22 
23 #undef foreach
24 #include "boost/multi_index_container.hpp"
25 #include "boost/multi_index/ordered_index.hpp"
26 #include "boost/multi_index/hashed_index.hpp"
27 
28 #include <boost/signals2/signal.hpp>
29 
30 class CAutoFile;
31 class CBlockIndex;
32 
33 inline double AllowFreeThreshold()
34 {
35  return COIN * 144 / 250;
36 }
37 
38 inline bool AllowFree(double dPriority)
39 {
40  // Large (in bytes) low-priority (new, small-coin) transactions
41  // need a fee.
42  return dPriority > AllowFreeThreshold();
43 }
44 
46 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
47 
48 struct LockPoints
49 {
50  // Will be set to the blockchain height and median time past
51  // values that would be necessary to satisfy all relative locktime
52  // constraints (BIP68) of this tx given our view of block chain history
53  int height;
54  int64_t time;
55  // As long as the current chain descends from the highest height block
56  // containing one of the inputs used in the calculation, then the cached
57  // values are still valid even after a reorg.
59 
60  LockPoints() : height(0), time(0), maxInputBlock(NULL) { }
61 };
62 
63 class CTxMemPool;
64 
83 {
84 private:
87  size_t nTxWeight;
88  size_t nModSize;
89  size_t nUsageSize;
90  int64_t nTime;
91  double entryPriority;
92  unsigned int entryHeight;
95  int64_t sigOpCost;
96  int64_t feeDelta;
98 
99  // Information about descendants of this transaction that are in the
100  // mempool; if we remove this transaction we must remove all of these
101  // descendants as well. if nCountWithDescendants is 0, treat this entry as
102  // dirty, and nSizeWithDescendants and nModFeesWithDescendants will not be
103  // correct.
107 
108  // Analogous statistics for ancestor transactions
113 
114 public:
115  CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
116  int64_t _nTime, double _entryPriority, unsigned int _entryHeight,
117  CAmount _inChainInputValue, bool spendsCoinbase,
118  int64_t nSigOpsCost, LockPoints lp);
119 
120  CTxMemPoolEntry(const CTxMemPoolEntry& other);
121 
122  const CTransaction& GetTx() const { return *this->tx; }
123  CTransactionRef GetSharedTx() const { return this->tx; }
128  double GetPriority(unsigned int currentHeight) const;
129  const CAmount& GetFee() const { return nFee; }
130  size_t GetTxSize() const;
131  size_t GetTxWeight() const { return nTxWeight; }
132  int64_t GetTime() const { return nTime; }
133  unsigned int GetHeight() const { return entryHeight; }
134  int64_t GetSigOpCost() const { return sigOpCost; }
135  int64_t GetModifiedFee() const { return nFee + feeDelta; }
136  size_t DynamicMemoryUsage() const { return nUsageSize; }
137  const LockPoints& GetLockPoints() const { return lockPoints; }
138 
139  // Adjusts the descendant state, if this entry is not dirty.
140  void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
141  // Adjusts the ancestor state
142  void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int modifySigOps);
143  // Updates the fee delta used for mining priority score, and the
144  // modified fees with descendants.
145  void UpdateFeeDelta(int64_t feeDelta);
146  // Update the LockPoints after a reorg
147  void UpdateLockPoints(const LockPoints& lp);
148 
149  uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
150  uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
152 
153  bool GetSpendsCoinbase() const { return spendsCoinbase; }
154 
155  uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
156  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
159 
160  mutable size_t vTxHashesIdx;
161 };
162 
163 // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
165 {
166  update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
167  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
168  {}
169 
172 
173  private:
174  int64_t modifySize;
176  int64_t modifyCount;
177 };
178 
180 {
181  update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost) :
182  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOpsCost(_modifySigOpsCost)
183  {}
184 
187 
188  private:
189  int64_t modifySize;
191  int64_t modifyCount;
193 };
194 
196 {
197  update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
198 
200 
201 private:
202  int64_t feeDelta;
203 };
204 
206 {
207  update_lock_points(const LockPoints& _lp) : lp(_lp) { }
208 
210 
211 private:
212  const LockPoints& lp;
213 };
214 
215 // extracts a TxMemPoolEntry's transaction hash
217 {
220  {
221  return entry.GetTx().GetHash();
222  }
223 };
224 
230 {
231 public:
232  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
233  {
234  bool fUseADescendants = UseDescendantScore(a);
235  bool fUseBDescendants = UseDescendantScore(b);
236 
237  double aModFee = fUseADescendants ? a.GetModFeesWithDescendants() : a.GetModifiedFee();
238  double aSize = fUseADescendants ? a.GetSizeWithDescendants() : a.GetTxSize();
239 
240  double bModFee = fUseBDescendants ? b.GetModFeesWithDescendants() : b.GetModifiedFee();
241  double bSize = fUseBDescendants ? b.GetSizeWithDescendants() : b.GetTxSize();
242 
243  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
244  double f1 = aModFee * bSize;
245  double f2 = aSize * bModFee;
246 
247  if (f1 == f2) {
248  return a.GetTime() >= b.GetTime();
249  }
250  return f1 < f2;
251  }
252 
253  // Calculate which score to use for an entry (avoiding division).
254  bool UseDescendantScore(const CTxMemPoolEntry &a) const
255  {
256  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
257  double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
258  return f2 > f1;
259  }
260 };
261 
267 {
268 public:
269  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
270  {
271  double f1 = (double)a.GetModifiedFee() * b.GetTxSize();
272  double f2 = (double)b.GetModifiedFee() * 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 
290 {
291 public:
292  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
293  {
294  double aFees = a.GetModFeesWithAncestors();
295  double aSize = a.GetSizeWithAncestors();
296 
297  double bFees = b.GetModFeesWithAncestors();
298  double bSize = b.GetSizeWithAncestors();
299 
300  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
301  double f1 = aFees * bSize;
302  double f2 = aSize * bFees;
303 
304  if (f1 == f2) {
305  return a.GetTx().GetHash() < b.GetTx().GetHash();
306  }
307 
308  return f1 > f2;
309  }
310 };
311 
312 // Multi_index tag names
314 struct entry_time {};
315 struct mining_score {};
316 struct ancestor_score {};
317 
319 
324 {
327 
329  int64_t nTime;
330 
333 
335  int64_t nFeeDelta;
336 };
337 
342  UNKNOWN = 0,
343  EXPIRY,
344  SIZELIMIT,
345  REORG,
346  BLOCK,
347  CONFLICT,
348  REPLACED
349 };
350 
432 {
433 private:
434  uint32_t nCheckFrequency;
435  unsigned int nTransactionsUpdated;
437 
438  uint64_t totalTxSize;
439  uint64_t cachedInnerUsage;
440 
441  mutable int64_t lastRollingFeeUpdate;
443  mutable double rollingMinimumFeeRate;
444 
445  void trackPackageRemoved(const CFeeRate& rate);
446 
447 public:
448 
449  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
450 
451  typedef boost::multi_index_container<
453  boost::multi_index::indexed_by<
454  // sorted by txid
455  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
456  // sorted by fee rate
457  boost::multi_index::ordered_non_unique<
458  boost::multi_index::tag<descendant_score>,
459  boost::multi_index::identity<CTxMemPoolEntry>,
461  >,
462  // sorted by entry time
463  boost::multi_index::ordered_non_unique<
464  boost::multi_index::tag<entry_time>,
465  boost::multi_index::identity<CTxMemPoolEntry>,
467  >,
468  // sorted by score (for mining prioritization)
469  boost::multi_index::ordered_unique<
470  boost::multi_index::tag<mining_score>,
471  boost::multi_index::identity<CTxMemPoolEntry>,
473  >,
474  // sorted by fee rate with ancestors
475  boost::multi_index::ordered_non_unique<
476  boost::multi_index::tag<ancestor_score>,
477  boost::multi_index::identity<CTxMemPoolEntry>,
479  >
480  >
482 
485 
486  typedef indexed_transaction_set::nth_index<0>::type::iterator txiter;
487  std::vector<std::pair<uint256, txiter> > vTxHashes;
488 
490  bool operator()(const txiter &a, const txiter &b) const {
491  return a->GetTx().GetHash() < b->GetTx().GetHash();
492  }
493  };
494  typedef std::set<txiter, CompareIteratorByHash> setEntries;
495 
496  const setEntries & GetMemPoolParents(txiter entry) const;
497  const setEntries & GetMemPoolChildren(txiter entry) const;
498 private:
499  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
500 
501  struct TxLinks {
504  };
505 
506  typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
508 
509  void UpdateParent(txiter entry, txiter parent, bool add);
510  void UpdateChild(txiter entry, txiter child, bool add);
511 
512  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const;
513 
514 public:
516  std::map<uint256, std::pair<double, CAmount> > mapDeltas;
517 
520  CTxMemPool(const CFeeRate& _minReasonableRelayFee);
521  ~CTxMemPool();
522 
529  void check(const CCoinsViewCache *pcoins) const;
530  void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = dFrequency * 4294967295.0; }
531 
532  // addUnchecked must updated state for all ancestors of a given transaction,
533  // to track size/count of descendant transactions. First version of
534  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
535  // then invoke the second version.
536  bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool validFeeEstimate = true);
537  bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate = true);
538 
540  void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags);
541  void removeConflicts(const CTransaction &tx);
542  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight);
543 
544  void clear();
545  void _clear(); //lock free
546  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
547  void queryHashes(std::vector<uint256>& vtxid);
548  void pruneSpent(const uint256& hash, CCoins &coins);
549  unsigned int GetTransactionsUpdated() const;
550  void AddTransactionsUpdated(unsigned int n);
555  bool HasNoInputsOf(const CTransaction& tx) const;
556 
558  void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
559  void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const;
560  void ClearPrioritisation(const uint256 hash);
561 
562 public:
570  void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN);
571 
581  void UpdateTransactionsFromBlock(const std::vector<uint256> &hashesToUpdate);
582 
593  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;
594 
598  void CalculateDescendants(txiter it, setEntries &setDescendants);
599 
606  CFeeRate GetMinFee(size_t sizelimit) const;
607 
612  void TrimToSize(size_t sizelimit, std::vector<uint256>* pvNoSpendsRemaining=NULL);
613 
615  int Expire(int64_t time);
616 
618  bool TransactionWithinChainLimit(const uint256& txid, size_t chainLimit) const;
619 
620  unsigned long size()
621  {
622  LOCK(cs);
623  return mapTx.size();
624  }
625 
626  uint64_t GetTotalTxSize()
627  {
628  LOCK(cs);
629  return totalTxSize;
630  }
631 
632  bool exists(uint256 hash) const
633  {
634  LOCK(cs);
635  return (mapTx.count(hash) != 0);
636  }
637 
638  CTransactionRef get(const uint256& hash) const;
639  TxMempoolInfo info(const uint256& hash) const;
640  std::vector<TxMempoolInfo> infoAll() const;
641 
646  CFeeRate estimateSmartFee(int nBlocks, int *answerFoundAtBlocks = NULL) const;
647 
649  CFeeRate estimateFee(int nBlocks) const;
650 
655  double estimateSmartPriority(int nBlocks, int *answerFoundAtBlocks = NULL) const;
656 
658  double estimatePriority(int nBlocks) const;
659 
661  bool WriteFeeEstimates(CAutoFile& fileout) const;
662  bool ReadFeeEstimates(CAutoFile& filein);
663 
664  size_t DynamicMemoryUsage() const;
665 
666  boost::signals2::signal<void (CTransactionRef)> NotifyEntryAdded;
667  boost::signals2::signal<void (CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved;
668 
669 private:
683  void UpdateForDescendants(txiter updateIt,
684  cacheMap &cachedDescendants,
685  const std::set<uint256> &setExclude);
687  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors);
689  void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors);
693  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants);
695  void UpdateChildrenForRemoval(txiter entry);
696 
706 };
707 
713 {
714 protected:
716 
717 public:
718  CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
719  bool GetCoins(const uint256 &txid, CCoins &coins) const;
720  bool HaveCoins(const uint256 &txid) const;
721 };
722 
723 // We want to sort transactions by coin age priority
724 typedef std::pair<double, CTxMemPool::txiter> TxCoinAgePriority;
725 
727 {
729  {
730  if (a.first == b.first)
731  return CompareTxMemPoolEntryByScore()(*(b.second), *(a.second)); //Reverse order to make sort less than
732  return a.first < b.first;
733  }
734 };
735 
736 #endif // BITCOIN_TXMEMPOOL_H
737 
738 
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:15
int flags
Definition: bitcoin-tx.cpp:468
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:456
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
We want to be able to estimate feerates that are needed on tx's to be included in a certain number of...
Definition: fees.h:199
Pruned version of CTransaction: only retains metadata and unspent transaction outputs.
Definition: coins.h:75
CCoinsView backed by another CCoinsView.
Definition: coins.h:333
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:372
Abstract view on the open txout dataset.
Definition: coins.h:307
CCoinsView that brings transactions from a memorypool into view.
Definition: txmempool.h:713
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
Definition: txmempool.cpp:984
bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
Definition: txmempool.cpp:996
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:982
const CTxMemPool & mempool
Definition: txmempool.h:715
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:93
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: amount.h:38
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:308
const uint256 & GetHash() const
Definition: transaction.h:358
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:83
size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: txmempool.h:87
int64_t GetSigOpCostWithAncestors() const
Definition: txmempool.h:158
double GetPriority(unsigned int currentHeight) const
Fast calculation of lower bound of current priority as update from entry priority.
Definition: txmempool.cpp:54
void UpdateFeeDelta(int64_t feeDelta)
Definition: txmempool.cpp:63
int64_t nSigOpCostWithAncestors
Definition: txmempool.h:112
unsigned int entryHeight
Chain height when entering the mempool.
Definition: txmempool.h:92
const CTransaction & GetTx() const
Definition: txmempool.h:122
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:96
unsigned int GetHeight() const
Definition: txmempool.h:133
bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: txmempool.h:94
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:332
size_t nUsageSize
... and total memory usage
Definition: txmempool.h:89
bool GetSpendsCoinbase() const
Definition: txmempool.h:153
int64_t nTime
Local time when entering the mempool.
Definition: txmempool.h:90
uint64_t GetCountWithDescendants() const
Definition: txmempool.h:149
void UpdateLockPoints(const LockPoints &lp)
Definition: txmempool.cpp:70
int64_t sigOpCost
Total sigop cost.
Definition: txmempool.h:95
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int modifySigOps)
Definition: txmempool.cpp:341
CAmount nModFeesWithAncestors
Definition: txmempool.h:111
const LockPoints & GetLockPoints() const
Definition: txmempool.h:137
CAmount GetModFeesWithDescendants() const
Definition: txmempool.h:151
int64_t GetSigOpCost() const
Definition: txmempool.h:134
uint64_t nCountWithDescendants
number of descendant transactions
Definition: txmempool.h:104
double entryPriority
Priority when entering the mempool.
Definition: txmempool.h:91
size_t GetTxSize() const
Definition: txmempool.cpp:75
CTxMemPoolEntry(const CTransactionRef &_tx, const CAmount &_nFee, int64_t _nTime, double _entryPriority, unsigned int _entryHeight, CAmount _inChainInputValue, bool spendsCoinbase, int64_t nSigOpsCost, LockPoints lp)
Definition: txmempool.cpp:22
uint64_t GetSizeWithAncestors() const
Definition: txmempool.h:156
int64_t GetTime() const
Definition: txmempool.h:132
CTransactionRef GetSharedTx() const
Definition: txmempool.h:123
uint64_t nSizeWithDescendants
... and size
Definition: txmempool.h:105
size_t DynamicMemoryUsage() const
Definition: txmempool.h:136
CAmount nModFeesWithDescendants
... and total fees (all including us)
Definition: txmempool.h:106
uint64_t nCountWithAncestors
Definition: txmempool.h:109
CAmount GetModFeesWithAncestors() const
Definition: txmempool.h:157
uint64_t GetSizeWithDescendants() const
Definition: txmempool.h:150
uint64_t GetCountWithAncestors() const
Definition: txmempool.h:155
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: txmempool.h:97
uint64_t nSizeWithAncestors
Definition: txmempool.h:110
int64_t GetModifiedFee() const
Definition: txmempool.h:135
const CAmount & GetFee() const
Definition: txmempool.h:129
CAmount inChainInputValue
Sum of all txin values that are already in blockchain.
Definition: txmempool.h:93
CTransactionRef tx
Definition: txmempool.h:85
size_t nModSize
... and modified size for priority
Definition: txmempool.h:88
size_t vTxHashesIdx
Index in mempool's vTxHashes.
Definition: txmempool.h:160
size_t GetTxWeight() const
Definition: txmempool.h:131
CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: txmempool.h:86
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:432
txlinksMap mapLinks
Definition: txmempool.h:507
bool TransactionWithinChainLimit(const uint256 &txid, size_t chainLimit) const
Returns false if the transaction is in the mempool and not within the chain limit specified.
Definition: txmempool.cpp:1153
bool ReadFeeEstimates(CAutoFile &filein)
Definition: txmempool.cpp:909
const setEntries & GetMemPoolChildren(txiter entry) const
Definition: txmempool.cpp:1068
void ClearPrioritisation(const uint256 hash)
Definition: txmempool.cpp:968
void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:926
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Definition: txmempool.cpp:508
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors)
Update ancestors of hash to add/remove it as a descendant transaction.
Definition: txmempool.cpp:237
double estimatePriority(int nBlocks) const
Estimate priority needed to get into the next nBlocks.
Definition: txmempool.cpp:881
unsigned int nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:435
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:1076
bool WriteFeeEstimates(CAutoFile &fileout) const
Write/Read estimates to disk.
Definition: txmempool.cpp:893
int Expire(int64_t time)
Expire all transaction (and their dependencies) in the mempool older than time.
Definition: txmempool.cpp:1014
boost::signals2::signal< void(CTransactionRef)> NotifyEntryAdded
Definition: txmempool.h:666
CFeeRate estimateSmartFee(int nBlocks, int *answerFoundAtBlocks=NULL) const
Estimate fee rate needed to get into the next nBlocks If no answer can be given at nBlocks,...
Definition: txmempool.cpp:876
uint32_t nCheckFrequency
Value n means that n times in 2^32 we check.
Definition: txmempool.h:434
void _clear()
Definition: txmempool.cpp:627
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:389
CFeeRate estimateFee(int nBlocks) const
Estimate fee rate needed to get into the next nBlocks.
Definition: txmempool.cpp:871
std::map< uint256, std::pair< double, CAmount > > mapDeltas
Definition: txmempool.h:516
void setSanityCheck(double dFrequency=1.0)
Definition: txmempool.h:530
double rollingMinimumFeeRate
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:443
void queryHashes(std::vector< uint256 > &vtxid)
Definition: txmempool.cpp:822
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const
Definition: txmempool.cpp:808
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
Definition: txmempool.cpp:83
void removeUnchecked(txiter entry, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
Definition: txmempool.cpp:454
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:853
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, bool validFeeEstimate=true)
Definition: txmempool.cpp:1030
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1000
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:839
std::vector< std::pair< uint256, txiter > > vTxHashes
All tx witness hashes/entries in mapTx, in random order.
Definition: txmempool.h:487
indirectmap< COutPoint, const CTransaction * > mapNextTx
Definition: txmempool.h:515
int64_t lastRollingFeeUpdate
Definition: txmempool.h:441
void trackPackageRemoved(const CFeeRate &rate)
Definition: txmempool.cpp:1100
void clear()
Definition: txmempool.cpp:640
void TrimToSize(size_t sizelimit, std::vector< uint256 > *pvNoSpendsRemaining=NULL)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
Definition: txmempool.cpp:1108
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:974
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants)
For each transaction being removed, update ancestors and any direct children.
Definition: txmempool.cpp:274
void UpdateTransactionsFromBlock(const std::vector< uint256 > &hashesToUpdate)
When adding transactions from a disconnected block back to the mempool, new mempool entries may have ...
Definition: txmempool.cpp:130
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb)
Definition: txmempool.cpp:777
unsigned long size()
Definition: txmempool.h:620
void UpdateChildrenForRemoval(txiter entry)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:266
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
Try to calculate all in-mempool ancestors of entry.
Definition: txmempool.cpp:173
bool exists(uint256 hash) const
Definition: txmempool.h:632
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:449
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const
Definition: txmempool.cpp:957
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:494
void removeConflicts(const CTransaction &tx)
Definition: txmempool.cpp:578
void UpdateChild(txiter entry, txiter child, bool add)
Definition: txmempool.cpp:1040
uint64_t cachedInnerUsage
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:439
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:486
double estimateSmartPriority(int nBlocks, int *answerFoundAtBlocks=NULL) const
Estimate priority needed to get into the next nBlocks If no answer can be given at nBlocks,...
Definition: txmempool.cpp:886
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:646
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:499
CTxMemPool(const CFeeRate &_minReasonableRelayFee)
Create a new CTxMemPool.
Definition: txmempool.cpp:352
const setEntries & GetMemPoolParents(txiter entry) const
Definition: txmempool.cpp:1060
uint64_t totalTxSize
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
Definition: txmempool.h:438
CCriticalSection cs
Definition: txmempool.h:483
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1006
indexed_transaction_set mapTx
Definition: txmempool.h:484
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
Definition: txmempool.cpp:540
TxMempoolInfo info(const uint256 &hash) const
Definition: txmempool.cpp:862
boost::signals2::signal< void(CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved
Definition: txmempool.h:667
uint64_t GetTotalTxSize()
Definition: txmempool.h:626
void pruneSpent(const uint256 &hash, CCoins &coins)
Definition: txmempool.cpp:370
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_unique< boost::multi_index::tag< mining_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByScore >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByAncestorFee > > > indexed_transaction_set
Definition: txmempool.h:481
void UpdateParent(txiter entry, txiter parent, bool add)
Definition: txmempool.cpp:1050
void CalculateDescendants(txiter it, setEntries &setDescendants)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:485
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors)
Set ancestor state for an entry.
Definition: txmempool.cpp:252
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight)
Called when a block is connected.
Definition: txmempool.cpp:598
bool blockSinceLastRollingFeeBump
Definition: txmempool.h:442
CBlockPolicyEstimator * minerPolicyEstimator
Definition: txmempool.h:436
std::map< txiter, TxLinks, CompareIteratorByHash > txlinksMap
Definition: txmempool.h:506
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:383
Definition: txmempool.h:290
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:292
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
Definition: txmempool.h:230
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:232
bool UseDescendantScore(const CTxMemPoolEntry &a) const
Definition: txmempool.h:254
Definition: txmempool.h:281
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:283
Sort by score of entry ((fee+delta)/size) in descending order.
Definition: txmempool.h:267
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:269
256-bit opaque blob.
Definition: uint256.h:123
bool operator()(const txiter &a, const txiter &b) const
Definition: txmempool.h:490
CBlockIndex * maxInputBlock
Definition: txmempool.h:58
LockPoints()
Definition: txmempool.h:60
int height
Definition: txmempool.h:53
int64_t time
Definition: txmempool.h:54
bool operator()(const TxCoinAgePriority &a, const TxCoinAgePriority &b)
Definition: txmempool.h:728
Information about a mempool transaction.
Definition: txmempool.h:324
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:335
int64_t nTime
Time the transaction entered the mempool.
Definition: txmempool.h:329
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:326
CFeeRate feeRate
Feerate of the transaction.
Definition: txmempool.h:332
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:219
uint256 result_type
Definition: txmempool.h:218
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:185
int64_t modifySigOpsCost
Definition: txmempool.h:192
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost)
Definition: txmempool.h:181
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:170
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
Definition: txmempool.h:166
int64_t feeDelta
Definition: txmempool.h:202
update_fee_delta(int64_t _feeDelta)
Definition: txmempool.h:197
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:199
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:209
update_lock_points(const LockPoints &_lp)
Definition: txmempool.h:207
const LockPoints & lp
Definition: txmempool.h:212
#define LOCK(cs)
Definition: sync.h:177
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:459
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:341
@ SIZELIMIT
Expired from mempool.
@ BLOCK
Removed for reorganization.
@ EXPIRY
Manually removed or unknown reason.
@ REPLACED
Removed for conflict with in-block transaction.
@ CONFLICT
Removed for block.
@ REORG
Removed in size limiting.
double AllowFreeThreshold()
Definition: txmempool.h:33
bool AllowFree(double dPriority)
Definition: txmempool.h:38
std::pair< double, CTxMemPool::txiter > TxCoinAgePriority
Definition: txmempool.h:724