Bitcoin ABC  0.29.2
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 <core_memusage.h>
10 #include <policy/policy.h>
11 #include <policy/settings.h>
12 #include <primitives/transaction.h>
13 #include <rcu.h>
14 
15 #include <chrono>
16 #include <cstddef>
17 #include <cstdint>
18 #include <functional>
19 #include <memory>
20 #include <set>
21 
22 struct LockPoints {
23  // Will be set to the blockchain height and median time past values that
24  // would be necessary to satisfy all relative locktime constraints (BIP68)
25  // of this tx given our view of block chain history
26  int height{0};
27  int64_t time{0};
28 };
29 
31  // SFINAE for T where T is either a std::reference_wrapper<T> (e.g. a
32  // CTxMemPoolEntryRef) or an iterator to a pointer type (e.g., a txiter)
33  template <typename T>
34  bool operator()(const std::reference_wrapper<T> &a,
35  const std::reference_wrapper<T> &b) const {
36  return a.get()->GetTx().GetId() < b.get()->GetTx().GetId();
37  }
38  template <typename T> bool operator()(const T &a, const T &b) const {
39  return (*a)->GetTx().GetId() < (*b)->GetTx().GetId();
40  }
41 };
44  template <typename T>
45  bool operator()(const std::reference_wrapper<T> &a,
46  const std::reference_wrapper<T> &b) const {
47  return a.get()->GetEntryId() > b.get()->GetEntryId();
48  }
49 
50  template <typename T> bool operator()(const T &a, const T &b) const {
51  return (*a)->GetEntryId() > (*b)->GetEntryId();
52  }
53 };
54 
55 class CTxMemPoolEntry;
57 
66 public:
67  // two aliases, should the types ever diverge
68  typedef std::set<std::reference_wrapper<const CTxMemPoolEntryRef>,
71  typedef std::set<std::reference_wrapper<const CTxMemPoolEntryRef>,
74 
75 private:
77  uint64_t entryId = 0;
78 
80  mutable Parents m_parents;
83  const Amount nFee;
85  const size_t nTxSize;
87  const size_t nUsageSize;
89  const int64_t nTime;
91  const unsigned int entryHeight;
93  const int64_t sigChecks;
99 
101 
102 public:
103  CTxMemPoolEntry(const CTransactionRef &_tx, const Amount fee, int64_t time,
104  unsigned int entry_height, int64_t sigchecks, LockPoints lp)
105  : tx{_tx}, nFee{fee},
106  nTxSize(tx->GetTotalSize()), nUsageSize{RecursiveDynamicUsage(tx)},
107  nTime(time), entryHeight{entry_height}, sigChecks(sigchecks),
108  lockPoints(lp) {}
109 
110  CTxMemPoolEntry(const CTxMemPoolEntry &other) = delete;
112  : entryId(other.entryId), tx(std::move(other.tx)),
113  m_parents(std::move(other.m_parents)),
114  m_children(std::move(other.m_children)), nFee(other.nFee),
115  nTxSize(other.nTxSize), nUsageSize(other.nUsageSize),
116  nTime(other.nTime), entryHeight(other.entryHeight),
117  sigChecks(other.sigChecks), feeDelta(other.feeDelta),
118  lockPoints(std::move(other.lockPoints)),
119  refcount(other.refcount.load()){};
120 
121  uint64_t GetEntryId() const { return entryId; }
124  void SetEntryId(uint64_t eid) { entryId = eid; }
125 
126  const CTransaction &GetTx() const { return *this->tx; }
127  CTransactionRef GetSharedTx() const { return this->tx; }
128  Amount GetFee() const { return nFee; }
129  size_t GetTxSize() const { return nTxSize; }
130  size_t GetTxVirtualSize() const {
133  }
134 
135  std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
136  unsigned int GetHeight() const { return entryHeight; }
137  int64_t GetSigChecks() const { return sigChecks; }
138  Amount GetModifiedFee() const { return nFee + feeDelta; }
141  }
142  size_t DynamicMemoryUsage() const { return nUsageSize; }
143  const LockPoints &GetLockPoints() const { return lockPoints; }
144 
145  // Updates the fee delta used for mining priority score
146  void UpdateFeeDelta(Amount newFeeDelta) { feeDelta = newFeeDelta; }
147 
148  const Parents &GetMemPoolParentsConst() const { return m_parents; }
149  const Children &GetMemPoolChildrenConst() const { return m_children; }
150  Parents &GetMemPoolParents() const { return m_parents; }
152 };
153 
154 #endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:65
const CTransactionRef tx
Definition: mempool_entry.h:79
const CTransaction & GetTx() const
unsigned int GetHeight() const
const Parents & GetMemPoolParentsConst() const
std::chrono::seconds GetTime() const
IMPLEMENT_RCU_REFCOUNT(uint64_t)
uint64_t GetEntryId() const
const int64_t nTime
Local time when entering the mempool.
Definition: mempool_entry.h:89
const Amount nFee
Cached to avoid expensive parent-transaction lookups.
Definition: mempool_entry.h:83
const size_t nTxSize
... and avoid recomputing tx size
Definition: mempool_entry.h:85
const size_t nUsageSize
... and total memory usage
Definition: mempool_entry.h:87
Amount GetFee() const
CTxMemPoolEntry(const CTransactionRef &_tx, const Amount fee, int64_t time, unsigned int entry_height, int64_t sigchecks, LockPoints lp)
const LockPoints & GetLockPoints() const
int64_t GetSigChecks() const
void SetEntryId(uint64_t eid)
This should only be set by addUnchecked() before entry insertion into mempool.
std::set< std::reference_wrapper< const CTxMemPoolEntryRef >, CompareIteratorById > Children
Definition: mempool_entry.h:73
Parents m_parents
Definition: mempool_entry.h:80
size_t GetTxSize() const
CTxMemPoolEntry(const CTxMemPoolEntry &other)=delete
Amount feeDelta
Used for determining the priority of the transaction for mining in a block.
Definition: mempool_entry.h:96
CTxMemPoolEntry(CTxMemPoolEntry &&other)
CTransactionRef GetSharedTx() const
Amount GetModifiedFee() const
CFeeRate GetModifiedFeeRate() const
size_t DynamicMemoryUsage() const
void UpdateFeeDelta(Amount newFeeDelta)
size_t GetTxVirtualSize() const
uint64_t entryId
Unique identifier – used for topological sorting.
Definition: mempool_entry.h:77
Parents & GetMemPoolParents() const
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: mempool_entry.h:98
Children m_children
Definition: mempool_entry.h:81
const Children & GetMemPoolChildrenConst() const
const unsigned int entryHeight
Chain height when entering the mempool.
Definition: mempool_entry.h:91
Children & GetMemPoolChildren() const
std::set< std::reference_wrapper< const CTxMemPoolEntryRef >, CompareIteratorById > Parents
Definition: mempool_entry.h:70
const int64_t sigChecks
Total sigChecks.
Definition: mempool_entry.h:93
Definition: rcu.h:85
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
LockPoints lp
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257
uint32_t nBytesPerSigCheck
Definition: settings.cpp:10
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigChecks, unsigned int bytes_per_sigCheck)
Compute the virtual transaction size (size, or more if sigChecks are too dense).
Definition: policy.cpp:165
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) const
Definition: mempool_entry.h:34
bool operator()(const T &a, const T &b) const
Definition: mempool_entry.h:38
Iterate txs in reverse-topological order.
Definition: mempool_entry.h:43
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) const
Definition: mempool_entry.h:45
bool operator()(const T &a, const T &b) const
Definition: mempool_entry.h:50
int64_t time
Definition: mempool_entry.h:27