PIVX Core  5.6.99
P2P Digital Currency
txmempool.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2016-2021 The PIVX Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_TXMEMPOOL_H
8 #define PIVX_TXMEMPOOL_H
9 
10 #include <list>
11 #include <memory>
12 #include <set>
13 
14 #include "amount.h"
15 #include "coins.h"
16 #include "crypto/siphash.h"
17 #include "indirectmap.h"
18 #include "policy/feerate.h"
19 #include "primitives/transaction.h"
20 #include "sync.h"
21 #include "random.h"
22 #include "netaddress.h"
23 
24 #include "boost/multi_index_container.hpp"
25 #include "boost/multi_index/ordered_index.hpp"
26 #include "boost/multi_index/hashed_index.hpp"
27 #include <boost/multi_index/sequenced_index.hpp>
28 
29 class CAutoFile;
30 class CBLSPublicKey;
31 
32 
34 static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
35 
36 class CTxMemPool;
37 
55 {
56 private:
59  size_t nTxSize;
60  size_t nUsageSize;
62  bool hasZerocoins{false};
63  bool m_isShielded{false};
64  int64_t nTime;
65  unsigned int entryHeight;
67  unsigned int sigOpCount;
68  int64_t feeDelta;
69 
70  // Information about descendants of this transaction that are in the
71  // mempool; if we remove this transaction we must remove all of these
72  // descendants as well. if nCountWithDescendants is 0, treat this entry as
73  // dirty, and nSizeWithDescendants and nModFeesWithDescendants will not be
74  // correct.
78 
79  // Analogous statistics for ancestor transactions
84 
85 public:
86  CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
87  int64_t _nTime, unsigned int _entryHeight,
88  bool _spendsCoinbaseOrCoinstake, unsigned int nSigOps);
89 
90  const CTransaction& GetTx() const { return *this->tx; }
91  std::shared_ptr<const CTransaction> GetSharedTx() const { return this->tx; }
92  const CAmount& GetFee() const { return nFee; }
93  size_t GetTxSize() const { return nTxSize; }
94  int64_t GetTime() const { return nTime; }
95  unsigned int GetHeight() const { return entryHeight; }
96  bool HasZerocoins() const { return hasZerocoins; }
97  bool IsShielded() const { return m_isShielded; }
98  unsigned int GetSigOpCount() const { return sigOpCount; }
99  int64_t GetModifiedFee() const { return nFee + feeDelta; }
100  size_t DynamicMemoryUsage() const { return nUsageSize; }
101 
102  // Adjusts the descendant state, if this entry is not dirty.
103  void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
104  // Adjusts the ancestor state
105  void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int modifySigOps);
106  // Updates the fee delta used for mining priority score, and the
107  // modified fees with descendants.
108  void UpdateFeeDelta(int64_t feeDelta);
109 
110  uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
111  uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
113 
115 
116  uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
117  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
119  unsigned int GetSigOpCountWithAncestors() const { return nSigOpCountWithAncestors; }
120 };
121 
122 // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
124 {
125  update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
126  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
127  {}
128 
131 
132  private:
133  int64_t modifySize;
135  int64_t modifyCount;
136 };
137 
139 {
140  update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int _modifySigOps) :
141  modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOps(_modifySigOps)
142  {}
143 
146 
147 private:
148  int64_t modifySize;
150  int64_t modifyCount;
152 };
153 
155 {
156  explicit update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
157 
159 
160 private:
161  int64_t feeDelta;
162 };
163 
164 // extracts a transaction hash from CTxMempoolEntry or CTransactionRef
166 {
169  {
170  return entry.GetTx().GetHash();
171  }
172 
174  {
175  return tx->GetHash();
176  }
177 };
178 
184 {
185 public:
186  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
187  {
188  bool fUseADescendants = UseDescendantScore(a);
189  bool fUseBDescendants = UseDescendantScore(b);
190 
191  double aModFee = fUseADescendants ? a.GetModFeesWithDescendants() : a.GetModifiedFee();
192  double aSize = fUseADescendants ? a.GetSizeWithDescendants() : a.GetTxSize();
193 
194  double bModFee = fUseBDescendants ? b.GetModFeesWithDescendants() : b.GetModifiedFee();
195  double bSize = fUseBDescendants ? b.GetSizeWithDescendants() : b.GetTxSize();
196 
197  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
198  double f1 = aModFee * bSize;
199  double f2 = aSize * bModFee;
200 
201  if (f1 == f2) {
202  return a.GetTime() >= b.GetTime();
203  }
204  return f1 < f2;
205  }
206 
207  // Calculate which score to use for an entry (avoiding division).
208  bool UseDescendantScore(const CTxMemPoolEntry &a) const
209  {
210  double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
211  double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
212  return f2 > f1;
213  }
214 };
215 
221 {
222 public:
223  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
224  {
225  double f1 = (double)a.GetModifiedFee() * b.GetTxSize();
226  double f2 = (double)b.GetModifiedFee() * a.GetTxSize();
227  if (f1 == f2) {
228  return b.GetTx().GetHash() < a.GetTx().GetHash();
229  }
230  return f1 > f2;
231  }
232 };
233 
235 {
236 public:
237  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
238  {
239  return a.GetTime() < b.GetTime();
240  }
241 };
242 
244 {
245 public:
246  bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
247  {
248  double aFees = a.GetModFeesWithAncestors();
249  double aSize = a.GetSizeWithAncestors();
250 
251  double bFees = b.GetModFeesWithAncestors();
252  double bSize = b.GetSizeWithAncestors();
253 
254  // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
255  double f1 = aFees * bSize;
256  double f2 = aSize * bFees;
257 
258  if (f1 == f2) {
259  return a.GetTx().GetHash() < b.GetTx().GetHash();
260  }
261 
262  return f1 > f2;
263  }
264 };
265 
266 // Multi_index tag names
268 struct entry_time {};
269 struct mining_score {};
270 struct ancestor_score {};
271 
273 
278 {
281 
283  int64_t nTime;
284 
287 
289  int64_t nFeeDelta;
290 };
291 
296  UNKNOWN = 0,
297  EXPIRY,
298  SIZELIMIT,
299  REORG,
300  BLOCK,
301  CONFLICT,
302  REPLACED
303 };
304 
384 {
385 private:
386  uint32_t nCheckFrequency;
387  unsigned int nTransactionsUpdated;
389 
391  uint64_t totalTxSize;
392  uint64_t cachedInnerUsage;
393 
395 
396  mutable int64_t lastRollingFeeUpdate;
398  mutable double rollingMinimumFeeRate;
399 
400  void trackPackageRemoved(const CFeeRate& rate);
401 
402  // Shielded txes
403  std::map<uint256, CTransactionRef> mapSaplingNullifiers;
404  void checkNullifiers() const;
405 
406  bool m_is_loaded GUARDED_BY(cs){false};
407 
408 public:
409 
410  static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
411 
412  typedef boost::multi_index_container<
414  boost::multi_index::indexed_by<
415  // sorted by txid
416  boost::multi_index::hashed_unique<mempoolentry_txid, SaltedIdHasher>,
417  // sorted by fee rate
418  boost::multi_index::ordered_non_unique<
419  boost::multi_index::tag<descendant_score>,
420  boost::multi_index::identity<CTxMemPoolEntry>,
422  >,
423  // sorted by entry time
424  boost::multi_index::ordered_non_unique<
425  boost::multi_index::tag<entry_time>,
426  boost::multi_index::identity<CTxMemPoolEntry>,
428  >,
429  // sorted by score (for mining prioritization)
430  boost::multi_index::ordered_unique<
431  boost::multi_index::tag<mining_score>,
432  boost::multi_index::identity<CTxMemPoolEntry>,
434  >,
435  // sorted by fee rate with ancestors
436  boost::multi_index::ordered_non_unique<
437  boost::multi_index::tag<ancestor_score>,
438  boost::multi_index::identity<CTxMemPoolEntry>,
440  >
441  >
443 
473 
474  typedef indexed_transaction_set::nth_index<0>::type::iterator txiter;
475 
477  bool operator()(const txiter &a, const txiter &b) const {
478  return a->GetTx().GetHash() < b->GetTx().GetHash();
479  }
480  };
481  typedef std::set<txiter, CompareIteratorByHash> setEntries;
482 
483  const setEntries & GetMemPoolParents(txiter entry) const;
484  const setEntries & GetMemPoolChildren(txiter entry) const;
485 
486 private:
487  typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
488 
489  struct TxLinks {
492  };
493 
494  typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
496 
497  std::multimap<uint256, uint256> mapProTxRefs; // proTxHash -> transaction (all TXs that refer to an existing proTx)
498  std::map<CService, uint256> mapProTxAddresses;
499  std::map<CKeyID, uint256> mapProTxPubKeyIDs;
500  std::map<uint256, uint256> mapProTxBlsPubKeyHashes;
501  std::map<COutPoint, uint256> mapProTxCollaterals;
502 
503  void UpdateParent(txiter entry, txiter parent, bool add);
504  void UpdateChild(txiter entry, txiter child, bool add);
505 
506  std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const;
507 
508 public:
510  std::map<uint256, CAmount> mapDeltas;
511 
517  explicit CTxMemPool(const CFeeRate& _minReasonableRelayFee);
518  ~CTxMemPool();
519 
526  void check(const CCoinsViewCache *pcoins) const;
527  void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = dFrequency * 4294967295.0; }
528 
529  // addUnchecked must updated state for all ancestors of a given transaction,
530  // to track size/count of descendant transactions. First version of
531  // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
532  // then invoke the second version.
533  // Note that addUnchecked is ONLY called from ATMP outside of tests
534  // and any other callers may break wallet's in-mempool tracking (due to
535  // lack of CValidationInterface::TransactionAddedToMempool callbacks).
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 removeWithAnchor(const uint256& invalidRoot);
542  void removeConflicts(const CTransaction& tx);
543  void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight);
544 
545  void clear();
546  void _clear(); // lock-free
547  bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
548  void queryHashes(std::vector<uint256>& vtxid);
549  void getTransactions(std::set<uint256>& setTxid);
550  bool isSpent(const COutPoint& outpoint);
551  unsigned int GetTransactionsUpdated() const;
552  void AddTransactionsUpdated(unsigned int n);
557  bool HasNoInputsOf(const CTransaction& tx) const;
558 
560  void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
561  void ApplyDelta(const uint256& hash, CAmount& nFeeDelta) const;
562  void ClearPrioritisation(const uint256 hash);
563 
564  bool nullifierExists(const uint256& nullifier) const;
565 
573  void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN);
574 
584  void UpdateTransactionsFromBlock(const std::vector<uint256> &hashesToUpdate);
585 
596  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;
597 
601  void CalculateDescendants(txiter it, setEntries &setDescendants);
602 
609  CFeeRate GetMinFee(size_t sizelimit) const;
610 
615  void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr);
616 
618  int Expire(int64_t time);
619 
621  bool TransactionWithinChainLimit(const uint256& txid, size_t chainLimit) const;
622 
624  bool IsLoaded() const;
625 
627  void SetIsLoaded(bool loaded);
628 
629  unsigned long size() const
630  {
631  LOCK(cs);
632  return mapTx.size();
633  }
634  uint64_t GetTotalTxSize()
635  {
636  LOCK(cs);
637  return totalTxSize;
638  }
639 
640  bool exists(uint256 hash) const
641  {
642  LOCK(cs);
643  return (mapTx.count(hash) != 0);
644  }
645 
646  bool exists(const COutPoint& outpoint) const
647  {
648  LOCK(cs);
649  auto it = mapTx.find(outpoint.hash);
650  return (it != mapTx.end() && outpoint.n < it->GetTx().vout.size());
651  }
652 
653  CTransactionRef get(const uint256& hash) const;
654  TxMempoolInfo info(const uint256& hash) const;
655  std::vector<TxMempoolInfo> infoAll() const;
656 
657  bool existsProviderTxConflict(const CTransaction &tx) const;
659 
664  CFeeRate estimateSmartFee(int nBlocks, int *answerFoundAtBlocks = nullptr) const;
665 
667  CFeeRate estimateFee(int nBlocks) const;
668 
670  bool WriteFeeEstimates(CAutoFile& fileout) const;
671  bool ReadFeeEstimates(CAutoFile& filein);
672 
673  size_t DynamicMemoryUsage() const;
674 
675 private:
689  void UpdateForDescendants(txiter updateIt,
690  cacheMap &cachedDescendants,
691  const std::set<uint256> &setExclude);
693  void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors);
695  void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors);
699  void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants);
701  void UpdateChildrenForRemoval(txiter entry);
702 
712 
714  void addUncheckedSpecialTx(const CTransaction& tx);
715  void removeUncheckedSpecialTx(const CTransaction& tx);
716  void removeProTxPubKeyConflicts(const CTransaction &tx, const CKeyID &keyId);
717  void removeProTxPubKeyConflicts(const CTransaction& tx, const CBLSPublicKey& pubKey);
718  void removeProTxCollateralConflicts(const CTransaction &tx, const COutPoint &collateralOutpoint);
720  void removeProTxConflicts(const CTransaction &tx);
721 
722 };
723 
729 {
730 protected:
732 
733 public:
734  CCoinsViewMemPool(CCoinsView* baseIn, CTxMemPool& mempoolIn);
735  bool GetCoin(const COutPoint& outpoint, Coin& coin) const;
736  bool HaveCoin(const COutPoint& outpoint) const;
737  bool GetNullifier(const uint256& nullifier) const;
738 };
739 
754 // multi_index tag names
755 struct txid_index {};
756 struct insertion_order {};
757 
759  typedef boost::multi_index_container<
761  boost::multi_index::indexed_by<
762  // sorted by txid
763  boost::multi_index::hashed_unique<
764  boost::multi_index::tag<txid_index>,
767  >,
768  // sorted by order in the blockchain
769  boost::multi_index::sequenced<
770  boost::multi_index::tag<insertion_order>
771  >
772  >
774 
775  // It's almost certainly a logic bug if we don't clear out queuedTx before
776  // destruction, as we add to it while disconnecting blocks, and then we
777  // need to re-process remaining transactions to ensure mempool consistency.
778  // For now, assert() that we've emptied out this object on destruction.
779  // This assert() can always be removed if the reorg-processing code were
780  // to be refactored such that this assumption is no longer true (for
781  // instance if there was some other way we cleaned up the mempool after a
782  // reorg, besides draining this object).
784 
786  uint64_t cachedInnerUsage = 0;
787 
788  // Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
789  // no exact formula for boost::multi_index_contained is implemented.
790  size_t DynamicMemoryUsage() const {
791  return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
792  }
793 
795  {
796  queuedTx.insert(tx);
797  cachedInnerUsage += memusage::RecursiveDynamicUsage(tx);
798  }
799 
800  // Remove entries based on txid_index, and update memory usage.
801  void removeForBlock(const std::vector<CTransactionRef>& vtx)
802  {
803  // Short-circuit in the common case of a block being added to the tip
804  if (queuedTx.empty()) {
805  return;
806  }
807  for (auto const &tx : vtx) {
808  auto it = queuedTx.find(tx->GetHash());
809  if (it != queuedTx.end()) {
810  cachedInnerUsage -= memusage::RecursiveDynamicUsage(*it);
811  queuedTx.erase(it);
812  }
813  }
814  }
815 
816  // Remove an entry by insertion_order index, and update memory usage.
817  void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
818  {
819  cachedInnerUsage -= memusage::RecursiveDynamicUsage(*entry);
820  queuedTx.get<insertion_order>().erase(entry);
821  }
822 
823  void clear()
824  {
825  cachedInnerUsage = 0;
826  queuedTx.clear();
827  }
828 };
829 
830 #endif // PIVX_TXMEMPOOL_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
uint256 hash
Definition: transaction.h:35
uint32_t n
Definition: transaction.h:36
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:452
We want to be able to estimate feerates or priorities that are needed on tx's to be included in a cer...
Definition: fees.h:201
CCoinsView backed by another CCoinsView.
Definition: coins.h:250
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:283
Abstract view on the open txout dataset.
Definition: coins.h:201
CCoinsView that brings transactions from a memorypool into view.
Definition: txmempool.h:729
bool HaveCoin(const COutPoint &outpoint) const
Just check whether we have data for a given outpoint.
Definition: txmempool.cpp:1317
bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:1300
CTxMemPool & mempool
Definition: txmempool.h:731
bool GetNullifier(const uint256 &nullifier) const
Determine whether a nullifier is spent or not.
Definition: txmempool.cpp:1322
CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn)
Definition: txmempool.cpp:1298
Fee rate in PIV per kilobyte: CAmount / kB.
Definition: feerate.h:20
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:72
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
const uint256 & GetHash() const
Definition: transaction.h:301
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:55
bool IsShielded() const
Definition: txmempool.h:97
void UpdateFeeDelta(int64_t feeDelta)
Definition: txmempool.cpp:49
size_t nTxSize
Cached to avoid expensive parent-transaction lookups.
Definition: txmempool.h:59
unsigned int entryHeight
Local time when entering the mempool.
Definition: txmempool.h:65
const CTransaction & GetTx() const
Definition: txmempool.h:90
int64_t feeDelta
Legacy sig ops plus P2SH sig op count.
Definition: txmempool.h:68
unsigned int GetHeight() const
Definition: txmempool.h:95
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:306
size_t nUsageSize
... and avoid recomputing tx size
Definition: txmempool.h:60
bool GetSpendsCoinbaseOrCoinstake() const
Definition: txmempool.h:114
int64_t nTime
... and checking if it contains shielded spends/outputs
Definition: txmempool.h:64
uint64_t GetCountWithDescendants() const
Definition: txmempool.h:110
bool HasZerocoins() const
Definition: txmempool.h:96
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int modifySigOps)
Definition: txmempool.cpp:315
CAmount nModFeesWithAncestors
Definition: txmempool.h:82
unsigned int nSigOpCountWithAncestors
Definition: txmempool.h:83
CAmount GetModFeesWithDescendants() const
Definition: txmempool.h:112
bool hasZerocoins
... and fee per kB
Definition: txmempool.h:62
uint64_t nCountWithDescendants
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:75
size_t GetTxSize() const
Definition: txmempool.h:93
unsigned int sigOpCount
keep track of transactions that spend a coinbase or a coinstake
Definition: txmempool.h:67
unsigned int GetSigOpCount() const
Definition: txmempool.h:98
bool m_isShielded
... and checking if it contains zPIV (mints/spends)
Definition: txmempool.h:63
std::shared_ptr< const CTransaction > GetSharedTx() const
Definition: txmempool.h:91
uint64_t GetSizeWithAncestors() const
Definition: txmempool.h:117
int64_t GetTime() const
Definition: txmempool.h:94
uint64_t nSizeWithDescendants
number of descendant transactions
Definition: txmempool.h:76
CTxMemPoolEntry(const CTransactionRef &_tx, const CAmount &_nFee, int64_t _nTime, unsigned int _entryHeight, bool _spendsCoinbaseOrCoinstake, unsigned int nSigOps)
Definition: txmempool.cpp:26
size_t DynamicMemoryUsage() const
Definition: txmempool.h:100
CAmount nModFeesWithDescendants
... and size
Definition: txmempool.h:77
uint64_t nCountWithAncestors
... and total fees (all including us)
Definition: txmempool.h:80
unsigned int GetSigOpCountWithAncestors() const
Definition: txmempool.h:119
CAmount GetModFeesWithAncestors() const
Definition: txmempool.h:118
uint64_t GetSizeWithDescendants() const
Definition: txmempool.h:111
uint64_t GetCountWithAncestors() const
Definition: txmempool.h:116
CFeeRate feeRate
... and total memory usage
Definition: txmempool.h:61
uint64_t nSizeWithAncestors
Definition: txmempool.h:81
int64_t GetModifiedFee() const
Definition: txmempool.h:99
const CAmount & GetFee() const
Definition: txmempool.h:92
bool spendsCoinbaseOrCoinstake
Chain height when entering the mempool.
Definition: txmempool.h:66
CTransactionRef tx
Definition: txmempool.h:57
CAmount nFee
Definition: txmempool.h:58
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:384
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:1243
txlinksMap mapLinks
Definition: txmempool.h:495
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:1490
bool ReadFeeEstimates(CAutoFile &filein)
Definition: txmempool.cpp:1226
const setEntries & GetMemPoolChildren(txiter entry) const
Definition: txmempool.cpp:1404
void ClearPrioritisation(const uint256 hash)
Definition: txmempool.cpp:1275
void removeWithAnchor(const uint256 &invalidRoot)
Definition: txmempool.cpp:664
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Definition: txmempool.cpp:601
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors)
Update ancestors of hash to add/remove it as a descendant transaction.
Definition: txmempool.cpp:211
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:471
unsigned int nTransactionsUpdated
Value n means that n times in 2^32 we check.
Definition: txmempool.h:387
std::map< uint256, CTransactionRef > mapSaplingNullifiers
Definition: txmempool.h:403
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:1412
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:1128
bool WriteFeeEstimates(CAutoFile &fileout) const
Write/Read estimates to disk.
Definition: txmempool.cpp:1212
std::map< CKeyID, uint256 > mapProTxPubKeyIDs
Definition: txmempool.h:499
int Expire(int64_t time)
Expire all transaction (and their dependencies) in the mempool older than time.
Definition: txmempool.cpp:1349
uint32_t nCheckFrequency
Definition: txmempool.h:386
void getTransactions(std::set< uint256 > &setTxid)
Definition: txmempool.cpp:1119
void _clear()
Definition: txmempool.cpp:872
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:357
CFeeRate estimateFee(int nBlocks) const
Estimate fee rate needed to get into the next nBlocks.
Definition: txmempool.cpp:1200
indirectmap< COutPoint, CTransactionRef > mapNextTx
Definition: txmempool.h:509
void removeProTxReferences(const uint256 &proTxHash, MemPoolRemovalReason reason)
Definition: txmempool.cpp:748
void setSanityCheck(double dFrequency=1.0)
Definition: txmempool.h:527
CFeeRate estimateSmartFee(int nBlocks, int *answerFoundAtBlocks=nullptr) const
Estimate fee rate needed to get into the next nBlocks If no answer can be given at nBlocks,...
Definition: txmempool.cpp:1206
double rollingMinimumFeeRate
Definition: txmempool.h:398
void queryHashes(std::vector< uint256 > &vtxid)
Definition: txmempool.cpp:1088
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const
Definition: txmempool.cpp:1074
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:59
void removeUnchecked(txiter entry, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
Definition: txmempool.cpp:540
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, bool validFeeEstimate=true)
Definition: txmempool.cpp:1366
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1327
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:1105
void SetIsLoaded(bool loaded)
Sets the current loaded state.
Definition: txmempool.cpp:1503
bool exists(const COutPoint &outpoint) const
Definition: txmempool.h:646
int64_t lastRollingFeeUpdate
Definition: txmempool.h:396
boost::multi_index_container< CTxMemPoolEntry, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< mempoolentry_txid, SaltedIdHasher >, 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:442
std::map< uint256, CAmount > mapDeltas
Definition: txmempool.h:510
void trackPackageRemoved(const CFeeRate &rate)
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.cpp:1437
void checkNullifiers() const
Definition: txmempool.cpp:1032
void addUncheckedSpecialTx(const CTransaction &tx)
Special txes.
Definition: txmempool.cpp:363
void clear()
Definition: txmempool.cpp:887
void removeProTxPubKeyConflicts(const CTransaction &tx, const CKeyID &keyId)
Definition: txmempool.cpp:718
CFeeRate minReasonableRelayFee
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:394
void removeProTxCollateralConflicts(const CTransaction &tx, const COutPoint &collateralOutpoint)
Definition: txmempool.cpp:738
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:1287
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants)
For each transaction being removed, update ancestors and any direct children.
Definition: txmempool.cpp:248
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:106
bool existsProviderTxConflict(const CTransaction &tx) const
Definition: txmempool.cpp:1146
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb)
Definition: txmempool.cpp:1043
void UpdateChildrenForRemoval(txiter entry)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:240
void removeProTxConflicts(const CTransaction &tx)
Definition: txmempool.cpp:785
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:149
bool exists(uint256 hash) const
Definition: txmempool.h:640
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:410
CFeeRate minRelayFee
Definition: txmempool.h:390
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:481
void removeConflicts(const CTransaction &tx)
Definition: txmempool.cpp:688
void UpdateChild(txiter entry, txiter child, bool add)
Definition: txmempool.cpp:1376
uint64_t cachedInnerUsage
sum of all mempool tx' byte sizes
Definition: txmempool.h:392
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:474
void removeUncheckedSpecialTx(const CTransaction &tx)
Definition: txmempool.cpp:480
std::map< CService, uint256 > mapProTxAddresses
Definition: txmempool.h:498
bool IsLoaded() const
Definition: txmempool.cpp:1497
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:893
std::multimap< uint256, uint256 > mapProTxRefs
Definition: txmempool.h:497
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:487
CTxMemPool(const CFeeRate &_minReasonableRelayFee)
Create a new CTxMemPool.
Definition: txmempool.cpp:326
const setEntries & GetMemPoolParents(txiter entry) const
Definition: txmempool.cpp:1396
uint64_t totalTxSize
Passed to constructor to avoid dependency on main.
Definition: txmempool.h:391
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1340
indexed_transaction_set mapTx
Definition: txmempool.h:472
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
Definition: txmempool.cpp:633
TxMempoolInfo info(const uint256 &hash) const
Definition: txmempool.cpp:1137
uint64_t GetTotalTxSize()
Definition: txmempool.h:634
unsigned long size() const
Definition: txmempool.h:629
std::map< uint256, uint256 > mapProTxBlsPubKeyHashes
Definition: txmempool.h:500
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
Definition: txmempool.cpp:1446
bool m_is_loaded GUARDED_BY(cs)
Definition: txmempool.h:406
bool isSpent(const COutPoint &outpoint)
Definition: txmempool.cpp:345
void UpdateParent(txiter entry, txiter parent, bool add)
Definition: txmempool.cpp:1386
void CalculateDescendants(txiter it, setEntries &setDescendants)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:578
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors)
Set ancestor state for an entry.
Definition: txmempool.cpp:226
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight)
Called when a block is connected.
Definition: txmempool.cpp:844
std::map< COutPoint, uint256 > mapProTxCollaterals
Definition: txmempool.h:501
bool blockSinceLastRollingFeeBump
Definition: txmempool.h:397
void removeProTxSpentCollateralConflicts(const CTransaction &tx)
Definition: txmempool.cpp:768
CBlockPolicyEstimator * minerPolicyEstimator
Definition: txmempool.h:388
bool nullifierExists(const uint256 &nullifier) const
Definition: txmempool.cpp:1281
std::map< txiter, TxLinks, CompareIteratorByHash > txlinksMap
Definition: txmempool.h:494
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const
Definition: txmempool.cpp:1265
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:351
A UTXO entry.
Definition: coins.h:32
Definition: txmempool.h:244
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:246
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
Definition: txmempool.h:184
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:186
bool UseDescendantScore(const CTxMemPoolEntry &a) const
Definition: txmempool.h:208
Definition: txmempool.h:235
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:237
Sort by score of entry ((fee+delta)/size) in descending order.
Definition: txmempool.h:221
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:223
256-bit opaque blob.
Definition: uint256.h:138
@ LOCK
Definition: lockunlock.h:16
int flags
Definition: pivx-tx.cpp:400
@ proTxHash
Definition: rpcevo.cpp:50
bool operator()(const txiter &a, const txiter &b) const
Definition: txmempool.h:477
boost::multi_index_container< CTransactionRef, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::tag< txid_index >, mempoolentry_txid, SaltedIdHasher >, boost::multi_index::sequenced< boost::multi_index::tag< insertion_order > > > > indexed_disconnected_transactions
Definition: txmempool.h:773
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:817
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:785
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Definition: txmempool.h:801
size_t DynamicMemoryUsage() const
Definition: txmempool.h:790
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:794
Information about a mempool transaction.
Definition: txmempool.h:278
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:289
int64_t nTime
Time the transaction entered the mempool.
Definition: txmempool.h:283
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:280
CFeeRate feeRate
Feerate of the transaction.
Definition: txmempool.h:286
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:168
uint256 result_type
Definition: txmempool.h:167
DisconnectedBlockTransactions During the reorg, it's desirable to re-add previously confirmed transac...
Definition: txmempool.h:755
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:144
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int _modifySigOps)
Definition: txmempool.h:140
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:129
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
Definition: txmempool.h:125
int64_t feeDelta
Definition: txmempool.h:161
update_fee_delta(int64_t _feeDelta)
Definition: txmempool.h:156
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.h:158
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:456
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:295
@ 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.