Bitcoin Core  27.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:
74  CTxMemPoolEntry(const CTxMemPoolEntry&) = default;
75  struct ExplicitCopyTag {
76  explicit ExplicitCopyTag() = default;
77  };
78 
80  mutable Parents m_parents;
82  const CAmount nFee;
83  const int32_t nTxWeight;
84  const size_t nUsageSize;
85  const int64_t nTime;
86  const uint64_t entry_sequence;
87  const unsigned int entryHeight;
88  const bool spendsCoinbase;
89  const int64_t sigOpCost;
92 
93  // Information about descendants of this transaction that are in the
94  // mempool; if we remove this transaction we must remove all of these
95  // descendants as well.
97  // Using int64_t instead of int32_t to avoid signed integer overflow issues.
100 
101  // Analogous statistics for ancestor transactions
103  // Using int64_t instead of int32_t to avoid signed integer overflow issues.
107 
108 public:
110  int64_t time, unsigned int entry_height, uint64_t entry_sequence,
111  bool spends_coinbase,
112  int64_t sigops_cost, LockPoints lp)
113  : tx{tx},
114  nFee{fee},
117  nTime{time},
119  entryHeight{entry_height},
120  spendsCoinbase{spends_coinbase},
121  sigOpCost{sigops_cost},
123  lockPoints{lp},
129 
134 
135  static constexpr ExplicitCopyTag ExplicitCopy{};
136 
137  const CTransaction& GetTx() const { return *this->tx; }
138  CTransactionRef GetSharedTx() const { return this->tx; }
139  const CAmount& GetFee() const { return nFee; }
140  int32_t GetTxSize() const
141  {
143  }
144  int32_t GetTxWeight() const { return nTxWeight; }
145  std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
146  unsigned int GetHeight() const { return entryHeight; }
147  uint64_t GetSequence() const { return entry_sequence; }
148  int64_t GetSigOpCost() const { return sigOpCost; }
150  size_t DynamicMemoryUsage() const { return nUsageSize; }
151  const LockPoints& GetLockPoints() const { return lockPoints; }
152 
153  // Adjusts the descendant state.
154  void UpdateDescendantState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount);
155  // Adjusts the ancestor state
156  void UpdateAncestorState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
157  // Updates the modified fees with descendants/ancestors.
158  void UpdateModifiedFee(CAmount fee_diff)
159  {
163  }
164 
165  // Update the LockPoints after a reorg
166  void UpdateLockPoints(const LockPoints& lp) const
167  {
168  lockPoints = lp;
169  }
170 
172  int64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
174 
175  bool GetSpendsCoinbase() const { return spendsCoinbase; }
176 
177  uint64_t GetCountWithAncestors() const { return m_count_with_ancestors; }
178  int64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
181 
182  const Parents& GetMemPoolParentsConst() const { return m_parents; }
183  const Children& GetMemPoolChildrenConst() const { return m_children; }
184  Parents& GetMemPoolParents() const { return m_parents; }
186 
187  mutable size_t idx_randomized;
189 };
190 
192 
195  /* The fee the transaction paid */
196  const CAmount m_fee;
207  /* The block height the transaction entered the mempool */
208  const unsigned int txHeight;
209 
210  TransactionInfo(const CTransactionRef& tx, const CAmount& fee, const int64_t vsize, const unsigned int height)
211  : m_tx{tx},
212  m_fee{fee},
214  txHeight{height} {}
215 };
216 
220  : info{entry.GetSharedTx(), entry.GetFee(), entry.GetTxSize(), entry.GetHeight()} {}
221 };
222 
225  /*
226  * This boolean indicates whether the transaction was added
227  * without enforcing mempool fee limits.
228  */
230  /* This boolean indicates whether the transaction is part of a package. */
232  /*
233  * This boolean indicates whether the blockchain is up to date when the
234  * transaction is added to the mempool.
235  */
237  /* Indicates whether the transaction has unconfirmed parents. */
239 
240  explicit NewMempoolTransactionInfo(const CTransactionRef& tx, const CAmount& fee,
241  const int64_t vsize, const unsigned int height,
242  const bool mempool_limit_bypassed, const bool submitted_in_package,
243  const bool chainstate_is_current,
244  const bool has_no_mempool_parents)
245  : info{tx, fee, vsize, height},
246  m_mempool_limit_bypassed{mempool_limit_bypassed},
247  m_submitted_in_package{submitted_in_package},
248  m_chainstate_is_current{chainstate_is_current},
249  m_has_no_mempool_parents{has_no_mempool_parents} {}
250 };
251 
252 #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:141
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
CAmount m_modified_fee
Used for determining the priority of the transaction for mining in a block.
Definition: mempool_entry.h:90
const int64_t sigOpCost
Total sigop cost.
Definition: mempool_entry.h:89
const CTransactionRef tx
Definition: mempool_entry.h:79
Epoch::Marker m_epoch_marker
epoch when last touched, useful for graph algorithms
int64_t GetSizeWithAncestors() const
int64_t GetSigOpCostWithAncestors() const
int64_t GetSizeWithDescendants() const
CTxMemPoolEntry(CTxMemPoolEntry &&)=delete
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: mempool_entry.h:88
uint64_t GetSequence() const
int64_t nSigOpCostWithAncestors
int64_t m_count_with_descendants
number of descendant transactions
Definition: mempool_entry.h:96
void UpdateAncestorState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:387
const CTransaction & GetTx() const
unsigned int GetHeight() const
const Parents & GetMemPoolParentsConst() const
std::chrono::seconds GetTime() const
int32_t GetTxWeight() const
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:85
size_t idx_randomized
Index in mempool's txns_randomized.
uint64_t GetCountWithDescendants() const
const size_t nUsageSize
... and total memory usage
Definition: mempool_entry.h:84
const uint64_t entry_sequence
Sequence number used to determine whether this transaction is too recent for relay.
Definition: mempool_entry.h:86
CTxMemPoolEntry(ExplicitCopyTag, const CTxMemPoolEntry &entry)
CAmount nModFeesWithAncestors
const LockPoints & GetLockPoints() const
CTxMemPoolEntry(const CTxMemPoolEntry &)=default
CAmount GetModFeesWithDescendants() const
Parents m_parents
Definition: mempool_entry.h:80
int64_t GetSigOpCost() const
CTransactionRef GetSharedTx() const
void UpdateModifiedFee(CAmount fee_diff)
CTxMemPoolEntry & operator=(const CTxMemPoolEntry &)=delete
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:99
int32_t GetTxSize() const
int64_t nSizeWithDescendants
... and size
Definition: mempool_entry.h:98
const int32_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: mempool_entry.h:83
int64_t m_count_with_ancestors
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: mempool_entry.h:82
Parents & GetMemPoolParents() const
CAmount GetModFeesWithAncestors() const
uint64_t GetCountWithAncestors() const
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: mempool_entry.h:91
void UpdateDescendantState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:378
CAmount GetModifiedFee() const
Children m_children
Definition: mempool_entry.h:81
CTxMemPoolEntry(const CTransactionRef &tx, CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
CTxMemPoolEntry & operator=(CTxMemPoolEntry &&)=delete
const CAmount & GetFee() const
static constexpr ExplicitCopyTag ExplicitCopy
int64_t nSizeWithAncestors
void UpdateLockPoints(const LockPoints &lp) const
const Children & GetMemPoolChildrenConst() const
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: mempool_entry.h:87
Children & GetMemPoolChildren() const
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
Definition: mempool_entry.h:70
static int32_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:149
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
CTxMemPoolEntry::CTxMemPoolEntryRef CTxMemPoolEntryRef
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:423
Definition: mempool_entry.h:75
ExplicitCopyTag()=default
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
const bool m_has_no_mempool_parents
const bool m_chainstate_is_current
const bool m_mempool_limit_bypassed
NewMempoolTransactionInfo(const CTransactionRef &tx, const CAmount &fee, const int64_t vsize, const unsigned int height, const bool mempool_limit_bypassed, const bool submitted_in_package, const bool chainstate_is_current, const bool has_no_mempool_parents)
RemovedMempoolTransactionInfo(const CTxMemPoolEntry &entry)
TransactionInfo(const CTransactionRef &tx, const CAmount &fee, const int64_t vsize, const unsigned int height)
const CAmount m_fee
const unsigned int txHeight
const CTransactionRef m_tx
const int64_t m_virtual_transaction_size
The virtual transaction size.