Branch data Line data Source code
1 : : // Copyright (c) 2009-present 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 <txgraph.h>
15 : : #include <util/overflow.h>
16 : :
17 : : #include <chrono>
18 : : #include <cstddef>
19 : : #include <cstdint>
20 : : #include <functional>
21 : : #include <memory>
22 : : #include <set>
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.
35 : : CBlockIndex* maxInputBlock{nullptr};
36 : : };
37 : :
38 : : struct CompareIteratorByHash {
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 : 619322 : bool operator()(const std::reference_wrapper<T>& a, const std::reference_wrapper<T>& b) const
43 : : {
44 : 619322 : return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();
45 : : }
46 : : template <typename T>
47 : 1387859 : bool operator()(const T& a, const T& b) const
48 : : {
49 : 1387859 : return a->GetTx().GetHash() < b->GetTx().GetHash();
50 : : }
51 : : };
52 : :
53 : : /** \class CTxMemPoolEntry
54 : : *
55 : : * CTxMemPoolEntry stores data about the corresponding transaction, as well
56 : : * as data about all in-mempool transactions that depend on the transaction
57 : : * ("descendant" transactions).
58 : : *
59 : : * When a new entry is added to the mempool, we update the descendant state
60 : : * (m_count_with_descendants, nSizeWithDescendants, and nModFeesWithDescendants) for
61 : : * all ancestors of the newly added transaction.
62 : : *
63 : : */
64 : :
65 : : class CTxMemPoolEntry : public TxGraph::Ref
66 : : {
67 : : public:
68 : : typedef std::reference_wrapper<const CTxMemPoolEntry> CTxMemPoolEntryRef;
69 : :
70 : : private:
71 : : CTxMemPoolEntry(const CTxMemPoolEntry&) = delete;
72 : :
73 : : const CTransactionRef tx;
74 : : const CAmount nFee; //!< Cached to avoid expensive parent-transaction lookups
75 : : const int32_t nTxWeight; //!< ... and avoid recomputing tx weight (also used for GetTxSize())
76 : : const size_t nUsageSize; //!< ... and total memory usage
77 : : const int64_t nTime; //!< Local time when entering the mempool
78 : : const uint64_t entry_sequence; //!< Sequence number used to determine whether this transaction is too recent for relay
79 : : const unsigned int entryHeight; //!< Chain height when entering the mempool
80 : : const bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
81 : : const int64_t sigOpCost; //!< Total sigop cost
82 : : mutable CAmount m_modified_fee; //!< Used for determining the priority of the transaction for mining in a block
83 : : mutable LockPoints lockPoints; //!< Track the height and time at which tx was final
84 : :
85 : : public:
86 [ + - ]: 177744 : virtual ~CTxMemPoolEntry() = default;
87 : 88872 : CTxMemPoolEntry(TxGraph::Ref&& ref, const CTransactionRef& tx, CAmount fee,
88 : : int64_t time, unsigned int entry_height, uint64_t entry_sequence,
89 : : bool spends_coinbase,
90 : : int64_t sigops_cost, LockPoints lp)
91 : 88872 : : TxGraph::Ref(std::move(ref)),
92 : 177744 : tx{tx},
93 : 88872 : nFee{fee},
94 : 88872 : nTxWeight{GetTransactionWeight(*tx)},
95 : 88872 : nUsageSize{RecursiveDynamicUsage(tx)},
96 : 88872 : nTime{time},
97 : 88872 : entry_sequence{entry_sequence},
98 : 88872 : entryHeight{entry_height},
99 : 88872 : spendsCoinbase{spends_coinbase},
100 : 88872 : sigOpCost{sigops_cost},
101 : 88872 : m_modified_fee{nFee},
102 [ + - ]: 88872 : lockPoints{lp} {}
103 : :
104 : : CTxMemPoolEntry& operator=(const CTxMemPoolEntry&) = delete;
105 : : CTxMemPoolEntry(CTxMemPoolEntry&&) = default;
106 : : CTxMemPoolEntry& operator=(CTxMemPoolEntry&&) = delete;
107 : :
108 [ - + - + : 116389868 : const CTransaction& GetTx() const { return *this->tx; }
+ + + + -
- + - + -
+ + - + -
+ + + + -
+ - ][ + +
+ + + + ]
[ + + + -
# # ][ + +
# # # # #
# # # ][ -
- - - + -
+ - - + #
# # # # #
# # # # #
# # # #
# ][ - - +
- + - + -
+ - + - +
- + - +
- ]
109 [ + - + - : 700867 : CTransactionRef GetSharedTx() const { return this->tx; }
+ - + - +
- + - - -
- - ][ + -
+ + # # #
# ][ + - +
- + - + -
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
[ + - ][ + -
+ - + - +
- + - + -
+ - ][ + -
+ - - - -
- - - -
- ]
110 [ + - + - : 57495 : const CAmount& GetFee() const { return nFee; }
+ - + - +
- + - + -
+ - ]
111 : 11148602 : int32_t GetTxSize() const
112 : : {
113 : 11148602 : return GetVirtualTransactionSize(nTxWeight, sigOpCost, ::nBytesPerSigOp);
114 : : }
115 : 10435737 : int32_t GetAdjustedWeight() const { return GetSigOpsAdjustedWeight(nTxWeight, sigOpCost, ::nBytesPerSigOp); }
116 [ + + ]: 18965 : int32_t GetTxWeight() const { return nTxWeight; }
117 [ + + - - ]: 91549 : std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
[ + - # # ]
118 [ + - ]: 80780 : unsigned int GetHeight() const { return entryHeight; }
119 [ + + + - ]: 39094 : uint64_t GetSequence() const { return entry_sequence; }
120 [ + + ]: 88922 : int64_t GetSigOpCost() const { return sigOpCost; }
121 [ + + ][ + - : 11002938 : CAmount GetModifiedFee() const { return m_modified_fee; }
+ - + - ]
[ + - + - ]
122 [ + + ]: 10482392 : size_t DynamicMemoryUsage() const { return nUsageSize; }
123 [ + - ]: 31106 : const LockPoints& GetLockPoints() const { return lockPoints; }
124 : :
125 : : // Updates the modified fees with descendants/ancestors.
126 : 303 : void UpdateModifiedFee(CAmount fee_diff) const
127 : : {
128 : 303 : m_modified_fee = SaturatingAdd(m_modified_fee, fee_diff);
129 : : }
130 : :
131 : : // Update the LockPoints after a reorg
132 : 227 : void UpdateLockPoints(const LockPoints& lp) const
133 : : {
134 : 227 : lockPoints = lp;
135 : : }
136 : :
137 [ + + ]: 28855 : bool GetSpendsCoinbase() const { return spendsCoinbase; }
138 : :
139 : : mutable size_t idx_randomized; //!< Index in mempool's txns_randomized
140 : : };
141 : :
142 : : using CTxMemPoolEntryRef = CTxMemPoolEntry::CTxMemPoolEntryRef;
143 : :
144 : 741932 : struct TransactionInfo {
145 : : const CTransactionRef m_tx;
146 : : /* The fee the transaction paid */
147 : : const CAmount m_fee;
148 : : /**
149 : : * The virtual transaction size.
150 : : *
151 : : * This is a policy field which considers the sigop cost of the
152 : : * transaction as well as its weight, and reinterprets it as bytes.
153 : : *
154 : : * It is the primary metric by which the mining algorithm selects
155 : : * transactions.
156 : : */
157 : : const int64_t m_virtual_transaction_size;
158 : : /* The block height the transaction entered the mempool */
159 : : const unsigned int txHeight;
160 : :
161 : 94507 : TransactionInfo(const CTransactionRef& tx, const CAmount& fee, const int64_t vsize, const unsigned int height)
162 : 189014 : : m_tx{tx},
163 : 94507 : m_fee{fee},
164 : 94507 : m_virtual_transaction_size{vsize},
165 : 94507 : txHeight{height} {}
166 : : };
167 : :
168 [ - - + - ]: 773296 : struct RemovedMempoolTransactionInfo {
[ - - ]
169 : : TransactionInfo info;
170 : 45488 : explicit RemovedMempoolTransactionInfo(const CTxMemPoolEntry& entry)
171 [ + - + - : 181952 : : info{entry.GetSharedTx(), entry.GetFee(), entry.GetTxSize(), entry.GetHeight()} {}
+ - + - ]
172 : : };
173 : :
174 [ + - - - : 196076 : struct NewMempoolTransactionInfo {
+ - - - ]
[ + - - - ]
[ + - + -
+ - + - +
- + - - -
- - - - ]
175 : : TransactionInfo info;
176 : : /*
177 : : * This boolean indicates whether the transaction was added
178 : : * without enforcing mempool fee limits.
179 : : */
180 : : const bool m_mempool_limit_bypassed;
181 : : /* This boolean indicates whether the transaction is part of a package. */
182 : : const bool m_submitted_in_package;
183 : : /*
184 : : * This boolean indicates whether the blockchain is up to date when the
185 : : * transaction is added to the mempool.
186 : : */
187 : : const bool m_chainstate_is_current;
188 : : /* Indicates whether the transaction has unconfirmed parents. */
189 : : const bool m_has_no_mempool_parents;
190 : :
191 : 49019 : explicit NewMempoolTransactionInfo(const CTransactionRef& tx, const CAmount& fee,
192 : : const int64_t vsize, const unsigned int height,
193 : : const bool mempool_limit_bypassed, const bool submitted_in_package,
194 : : const bool chainstate_is_current,
195 : : const bool has_no_mempool_parents)
196 [ + - + - ]: 49019 : : info{tx, fee, vsize, height},
[ + - + -
+ - ]
197 : 49019 : m_mempool_limit_bypassed{mempool_limit_bypassed},
198 : 49019 : m_submitted_in_package{submitted_in_package},
199 : 49019 : m_chainstate_is_current{chainstate_is_current},
200 [ + - + - ]: 49019 : m_has_no_mempool_parents{has_no_mempool_parents} {}
[ + - + -
+ - ]
201 : : };
202 : :
203 : : #endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H
|