Bitcoin ABC 0.30.7
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>
13#include <rcu.h>
14
15#include <chrono>
16#include <cstddef>
17#include <cstdint>
18#include <functional>
19#include <memory>
20#include <set>
21
22struct 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
55class CTxMemPoolEntry;
57
66public:
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
75private:
77 uint64_t entryId = 0;
78
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
102public:
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}, nTxSize(tx->GetTotalSize()),
107 entryHeight{entry_height}, sigChecks(sigchecks), lockPoints(lp) {}
108
109 CTxMemPoolEntry(const CTxMemPoolEntry &other) = delete;
111 : entryId(other.entryId), tx(std::move(other.tx)),
112 m_parents(std::move(other.m_parents)),
113 m_children(std::move(other.m_children)), nFee(other.nFee),
114 nTxSize(other.nTxSize), nUsageSize(other.nUsageSize),
115 nTime(other.nTime), entryHeight(other.entryHeight),
116 sigChecks(other.sigChecks), feeDelta(other.feeDelta),
117 lockPoints(std::move(other.lockPoints)),
118 refcount(other.refcount.load()){};
119
120 uint64_t GetEntryId() const { return entryId; }
123 void SetEntryId(uint64_t eid) { entryId = eid; }
124
125 const CTransaction &GetTx() const { return *this->tx; }
126 CTransactionRef GetSharedTx() const { return this->tx; }
127 Amount GetFee() const { return nFee; }
128 size_t GetTxSize() const { return nTxSize; }
129 size_t GetTxVirtualSize() const {
132 }
133
134 std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
135 unsigned int GetHeight() const { return entryHeight; }
136 int64_t GetSigChecks() const { return sigChecks; }
137 Amount GetModifiedFee() const { return nFee + feeDelta; }
140 }
141 size_t DynamicMemoryUsage() const { return nUsageSize; }
142 const LockPoints &GetLockPoints() const { return lockPoints; }
143
144 // Updates the fee delta used for mining priority score
145 void UpdateFeeDelta(Amount newFeeDelta) { feeDelta = newFeeDelta; }
146
147 const Parents &GetMemPoolParentsConst() const { return m_parents; }
148 const Children &GetMemPoolChildrenConst() const { return m_children; }
151};
152
153#endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
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 LockPoints & GetLockPoints() const
unsigned int GetHeight() const
std::chrono::seconds GetTime() const
const CTransaction & GetTx() const
Children & GetMemPoolChildren() 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)
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
const Children & GetMemPoolChildrenConst() const
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
const Parents & GetMemPoolParentsConst() const
CFeeRate GetModifiedFeeRate() const
size_t DynamicMemoryUsage() const
void UpdateFeeDelta(Amount newFeeDelta)
size_t GetTxVirtualSize() const
Parents & GetMemPoolParents() const
uint64_t entryId
Unique identifier – used for topological sorting.
Definition: mempool_entry.h:77
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 unsigned int entryHeight
Chain height when entering the mempool.
Definition: mempool_entry.h:91
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:259
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