Bitcoin Core  25.99.0
P2P Digital Currency
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
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>
17 #include <chrono>
18 #include <functional>
19 #include <memory>
20 #include <set>
21 #include <stddef.h>
22 #include <stdint.h>
24 class CBlockIndex;
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 };
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 };
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;
73 private:
75  mutable Parents m_parents;
77  const CAmount nFee;
78  const int32_t nTxWeight;
79  const size_t nUsageSize;
80  const int64_t nTime;
81  const uint64_t entry_sequence;
82  const unsigned int entryHeight;
83  const bool spendsCoinbase;
84  const int64_t sigOpCost;
88  // Information about descendants of this transaction that are in the
89  // mempool; if we remove this transaction we must remove all of these
90  // descendants as well.
92  // Using int64_t instead of int32_t to avoid signed integer overflow issues.
96  // Analogous statistics for ancestor transactions
98  // Using int64_t instead of int32_t to avoid signed integer overflow issues.
103 public:
105  int64_t time, unsigned int entry_height, uint64_t entry_sequence,
106  bool spends_coinbase,
107  int64_t sigops_cost, LockPoints lp)
108  : tx{tx},
109  nFee{fee},
112  nTime{time},
114  entryHeight{entry_height},
115  spendsCoinbase{spends_coinbase},
116  sigOpCost{sigops_cost},
118  lockPoints{lp},
125  const CTransaction& GetTx() const { return *this->tx; }
126  CTransactionRef GetSharedTx() const { return this->tx; }
127  const CAmount& GetFee() const { return nFee; }
128  int32_t GetTxSize() const
129  {
131  }
132  int32_t GetTxWeight() const { return nTxWeight; }
133  std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
134  unsigned int GetHeight() const { return entryHeight; }
135  uint64_t GetSequence() const { return entry_sequence; }
136  int64_t GetSigOpCost() const { return sigOpCost; }
138  size_t DynamicMemoryUsage() const { return nUsageSize; }
139  const LockPoints& GetLockPoints() const { return lockPoints; }
141  // Adjusts the descendant state.
142  void UpdateDescendantState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount);
143  // Adjusts the ancestor state
144  void UpdateAncestorState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
145  // Updates the modified fees with descendants/ancestors.
146  void UpdateModifiedFee(CAmount fee_diff)
147  {
151  }
153  // Update the LockPoints after a reorg
155  {
156  lockPoints = lp;
157  }
160  int64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
163  bool GetSpendsCoinbase() const { return spendsCoinbase; }
165  uint64_t GetCountWithAncestors() const { return m_count_with_ancestors; }
166  int64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
170  const Parents& GetMemPoolParentsConst() const { return m_parents; }
171  const Children& GetMemPoolChildrenConst() const { return m_children; }
172  Parents& GetMemPoolParents() const { return m_parents; }
175  mutable size_t vTxHashesIdx;
177 };
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:159
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
CAmount m_modified_fee
Used for determining the priority of the transaction for mining in a block.
Definition: mempool_entry.h:85
const int64_t sigOpCost
Total sigop cost.
Definition: mempool_entry.h:84
const CTransactionRef tx
Definition: mempool_entry.h:74
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
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Definition: mempool_entry.h:83
uint64_t GetSequence() const
int64_t nSigOpCostWithAncestors
int64_t m_count_with_descendants
number of descendant transactions
Definition: mempool_entry.h:91
void UpdateAncestorState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:392
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:80
uint64_t GetCountWithDescendants() const
const size_t nUsageSize
... and total memory usage
Definition: mempool_entry.h:79
const uint64_t entry_sequence
Sequence number used to determine whether this transaction is too recent for relay.
Definition: mempool_entry.h:81
void UpdateLockPoints(const LockPoints &lp)
CAmount nModFeesWithAncestors
const LockPoints & GetLockPoints() const
CAmount GetModFeesWithDescendants() const
Parents m_parents
Definition: mempool_entry.h:75
int64_t GetSigOpCost() const
CTransactionRef GetSharedTx() const
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:94
int32_t GetTxSize() const
int64_t nSizeWithDescendants
... and size
Definition: mempool_entry.h:93
const int32_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: mempool_entry.h:78
int64_t m_count_with_ancestors
Definition: mempool_entry.h:97
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: mempool_entry.h:77
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:86
void UpdateDescendantState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:383
CAmount GetModifiedFee() const
Children m_children
Definition: mempool_entry.h:76
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)
const CAmount & GetFee() const
int64_t nSizeWithAncestors
Definition: mempool_entry.h:99
const Children & GetMemPoolChildrenConst() const
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: mempool_entry.h:82
Children & GetMemPoolChildren() const
size_t vTxHashesIdx
Index in mempool's vTxHashes.
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
Definition: mempool_entry.h:70
static int32_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