Bitcoin Core  25.99.0
P2P Digital Currency
mempool_entry.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_KERNEL_MEMPOOL_ENTRY_H
6 #define BITCOIN_KERNEL_MEMPOOL_ENTRY_H
7 
8 #include <consensus/amount.h>
9 #include <consensus/validation.h>
10 #include <core_memusage.h>
11 #include <policy/policy.h>
12 #include <policy/settings.h>
13 #include <primitives/transaction.h>
14 #include <util/epochguard.h>
15 #include <util/overflow.h>
16 
17 #include <chrono>
18 #include <functional>
19 #include <memory>
20 #include <set>
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 class CBlockIndex;
25 
26 struct LockPoints {
27  // Will be set to the blockchain height and median time past
28  // values that would be necessary to satisfy all relative locktime
29  // constraints (BIP68) of this tx given our view of block chain history
30  int height{0};
31  int64_t time{0};
32  // As long as the current chain descends from the highest height block
33  // containing one of the inputs used in the calculation, then the cached
34  // values are still valid even after a reorg.
36 };
37 
39  // SFINAE for T where T is either a pointer type (e.g., a txiter) or a reference_wrapper<T>
40  // (e.g. a wrapped CTxMemPoolEntry&)
41  template <typename T>
42  bool operator()(const std::reference_wrapper<T>& a, const std::reference_wrapper<T>& b) const
43  {
44  return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();
45  }
46  template <typename T>
47  bool operator()(const T& a, const T& b) const
48  {
49  return a->GetTx().GetHash() < b->GetTx().GetHash();
50  }
51 };
52 
66 {
67 public:
68  typedef std::reference_wrapper<const CTxMemPoolEntry> CTxMemPoolEntryRef;
69  // two aliases, should the types ever diverge
70  typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Parents;
71  typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Children;
72 
73 private:
75  mutable Parents m_parents;
77  const CAmount nFee;
78  const size_t nTxWeight;
79  const size_t nUsageSize;
80  const int64_t nTime;
81  const unsigned int entryHeight;
82  const bool spendsCoinbase;
83  const int64_t sigOpCost;
86 
87  // Information about descendants of this transaction that are in the
88  // mempool; if we remove this transaction we must remove all of these
89  // descendants as well.
90  uint64_t nCountWithDescendants{1};
93 
94  // Analogous statistics for ancestor transactions
95  uint64_t nCountWithAncestors{1};
99 
100 public:
102  int64_t time, unsigned int entry_height,
103  bool spends_coinbase,
104  int64_t sigops_cost, LockPoints lp)
105  : tx{tx},
106  nFee{fee},
109  nTime{time},
110  entryHeight{entry_height},
111  spendsCoinbase{spends_coinbase},
112  sigOpCost{sigops_cost},
114  lockPoints{lp},
120 
121  const CTransaction& GetTx() const { return *this->tx; }
122  CTransactionRef GetSharedTx() const { return this->tx; }
123  const CAmount& GetFee() const { return nFee; }
124  size_t GetTxSize() const
125  {
127  }
128  size_t GetTxWeight() const { return nTxWeight; }
129  std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
130  unsigned int GetHeight() const { return entryHeight; }
131  int64_t GetSigOpCost() const { return sigOpCost; }
133  size_t DynamicMemoryUsage() const { return nUsageSize; }
134  const LockPoints& GetLockPoints() const { return lockPoints; }
135 
136  // Adjusts the descendant state.
137  void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
138  // Adjusts the ancestor state
139  void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
140  // Updates the modified fees with descendants/ancestors.
141  void UpdateModifiedFee(CAmount fee_diff)
142  {
146  }
147 
148  // Update the LockPoints after a reorg
150  {
151  lockPoints = lp;
152  }
153 
154  uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
155  uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
157 
158  bool GetSpendsCoinbase() const { return spendsCoinbase; }
159 
160  uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
161  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
164 
165  const Parents& GetMemPoolParentsConst() const { return m_parents; }
166  const Children& GetMemPoolChildrenConst() const { return m_children; }
167  Parents& GetMemPoolParents() const { return m_parents; }
169 
170  mutable size_t vTxHashesIdx;
172 };
173 
174 #endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
CTxMemPoolEntry(const CTransactionRef &tx, CAmount fee, int64_t time, unsigned int entry_height, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
CAmount m_modified_fee
Used for determining the priority of the transaction for mining in a block.
Definition: mempool_entry.h:84
const int64_t sigOpCost
Total sigop cost.
Definition: mempool_entry.h:83
const CTransactionRef tx
Definition: mempool_entry.h:74
Epoch::Marker m_epoch_marker
epoch when last touched, useful for graph algorithms
int64_t GetSigOpCostWithAncestors() const
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: mempool_entry.h:82
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: mempool_entry.h:78
int64_t nSigOpCostWithAncestors
Definition: mempool_entry.h:98
const CTransaction & GetTx() const
unsigned int GetHeight() const
const Parents & GetMemPoolParentsConst() const
std::chrono::seconds GetTime() const
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:368
std::reference_wrapper< const CTxMemPoolEntry > CTxMemPoolEntryRef
Definition: mempool_entry.h:68
bool GetSpendsCoinbase() const
const int64_t nTime
Local time when entering the mempool.
Definition: mempool_entry.h:80
uint64_t GetCountWithDescendants() const
const size_t nUsageSize
... and total memory usage
Definition: mempool_entry.h:79
void UpdateLockPoints(const LockPoints &lp)
CAmount nModFeesWithAncestors
Definition: mempool_entry.h:97
const LockPoints & GetLockPoints() const
CAmount GetModFeesWithDescendants() const
Parents m_parents
Definition: mempool_entry.h:75
int64_t GetSigOpCost() const
uint64_t nCountWithDescendants
number of descendant transactions
Definition: mempool_entry.h:90
size_t GetTxSize() const
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:377
uint64_t GetSizeWithAncestors() const
CTransactionRef GetSharedTx() const
uint64_t nSizeWithDescendants
... and size
Definition: mempool_entry.h:91
void UpdateModifiedFee(CAmount fee_diff)
size_t DynamicMemoryUsage() const
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Children
Definition: mempool_entry.h:71
CAmount nModFeesWithDescendants
... and total fees (all including us)
Definition: mempool_entry.h:92
uint64_t nCountWithAncestors
Definition: mempool_entry.h:95
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: mempool_entry.h:77
Parents & GetMemPoolParents() const
CAmount GetModFeesWithAncestors() const
uint64_t GetSizeWithDescendants() const
uint64_t GetCountWithAncestors() const
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: mempool_entry.h:85
CAmount GetModifiedFee() const
uint64_t nSizeWithAncestors
Definition: mempool_entry.h:96
Children m_children
Definition: mempool_entry.h:76
const CAmount & GetFee() const
const Children & GetMemPoolChildrenConst() const
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: mempool_entry.h:81
Children & GetMemPoolChildren() const
size_t vTxHashesIdx
Index in mempool's vTxHashes.
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
Definition: mempool_entry.h:70
size_t GetTxWeight() const
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
LockPoints lp
T SaturatingAdd(const T i, const T j) noexcept
Definition: overflow.h:33
unsigned int nBytesPerSigOp
Definition: settings.cpp:10
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:295
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
bool operator()(const T &a, const T &b) const
Definition: mempool_entry.h:47
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) const
Definition: mempool_entry.h:42
CBlockIndex * maxInputBlock
Definition: mempool_entry.h:35
int64_t time
Definition: mempool_entry.h:31