Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_TXMEMPOOL_H
7 : : #define BITCOIN_TXMEMPOOL_H
8 : :
9 : : #include <coins.h>
10 : : #include <consensus/amount.h>
11 : : #include <indirectmap.h>
12 : : #include <kernel/cs_main.h>
13 : : #include <kernel/mempool_entry.h> // IWYU pragma: export
14 : : #include <kernel/mempool_limits.h> // IWYU pragma: export
15 : : #include <kernel/mempool_options.h> // IWYU pragma: export
16 : : #include <kernel/mempool_removal_reason.h> // IWYU pragma: export
17 : : #include <policy/feerate.h>
18 : : #include <policy/packages.h>
19 : : #include <primitives/transaction.h>
20 : : #include <sync.h>
21 : : #include <util/epochguard.h>
22 : : #include <util/hasher.h>
23 : : #include <util/result.h>
24 : :
25 : : #include <boost/multi_index/hashed_index.hpp>
26 : : #include <boost/multi_index/identity.hpp>
27 : : #include <boost/multi_index/indexed_by.hpp>
28 : : #include <boost/multi_index/ordered_index.hpp>
29 : : #include <boost/multi_index/sequenced_index.hpp>
30 : : #include <boost/multi_index/tag.hpp>
31 : : #include <boost/multi_index_container.hpp>
32 : :
33 : : #include <atomic>
34 : : #include <map>
35 : : #include <optional>
36 : : #include <set>
37 : : #include <string>
38 : : #include <string_view>
39 : : #include <utility>
40 : : #include <vector>
41 : :
42 : : class CChain;
43 : :
44 : : /** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */
45 : : static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
46 : :
47 : : /**
48 : : * Test whether the LockPoints height and time are still valid on the current chain
49 : : */
50 : : bool TestLockPointValidity(CChain& active_chain, const LockPoints& lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
51 : :
52 : : // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
53 : : struct mempoolentry_txid
54 : : {
55 : : typedef uint256 result_type;
56 : 0 : result_type operator() (const CTxMemPoolEntry &entry) const
57 : : {
58 : 0 : return entry.GetTx().GetHash();
59 : : }
60 : :
61 : : result_type operator() (const CTransactionRef& tx) const
62 : : {
63 : : return tx->GetHash();
64 : : }
65 : : };
66 : :
67 : : // extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
68 : : struct mempoolentry_wtxid
69 : : {
70 : : typedef uint256 result_type;
71 : 0 : result_type operator() (const CTxMemPoolEntry &entry) const
72 : : {
73 : 0 : return entry.GetTx().GetWitnessHash();
74 : : }
75 : :
76 : : result_type operator() (const CTransactionRef& tx) const
77 : : {
78 : : return tx->GetWitnessHash();
79 : : }
80 : : };
81 : :
82 : :
83 : : /** \class CompareTxMemPoolEntryByDescendantScore
84 : : *
85 : : * Sort an entry by max(score/size of entry's tx, score/size with all descendants).
86 : : */
87 : : class CompareTxMemPoolEntryByDescendantScore
88 : : {
89 : : public:
90 : 0 : bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
91 : : {
92 : : double a_mod_fee, a_size, b_mod_fee, b_size;
93 : :
94 : 0 : GetModFeeAndSize(a, a_mod_fee, a_size);
95 : 0 : GetModFeeAndSize(b, b_mod_fee, b_size);
96 : :
97 : : // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
98 : 0 : double f1 = a_mod_fee * b_size;
99 : 0 : double f2 = a_size * b_mod_fee;
100 : :
101 [ # # ]: 0 : if (f1 == f2) {
102 : 0 : return a.GetTime() >= b.GetTime();
103 : : }
104 : 0 : return f1 < f2;
105 : 0 : }
106 : :
107 : : // Return the fee/size we're using for sorting this entry.
108 : 0 : void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
109 : : {
110 : : // Compare feerate with descendants to feerate of the transaction, and
111 : : // return the fee/size for the max.
112 : 0 : double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
113 : 0 : double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
114 : :
115 [ # # ]: 0 : if (f2 > f1) {
116 : 0 : mod_fee = a.GetModFeesWithDescendants();
117 : 0 : size = a.GetSizeWithDescendants();
118 : 0 : } else {
119 : 0 : mod_fee = a.GetModifiedFee();
120 : 0 : size = a.GetTxSize();
121 : : }
122 : 0 : }
123 : : };
124 : :
125 : : /** \class CompareTxMemPoolEntryByScore
126 : : *
127 : : * Sort by feerate of entry (fee/size) in descending order
128 : : * This is only used for transaction relay, so we use GetFee()
129 : : * instead of GetModifiedFee() to avoid leaking prioritization
130 : : * information via the sort order.
131 : : */
132 : : class CompareTxMemPoolEntryByScore
133 : : {
134 : : public:
135 : 0 : bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
136 : : {
137 : 0 : double f1 = (double)a.GetFee() * b.GetTxSize();
138 : 0 : double f2 = (double)b.GetFee() * a.GetTxSize();
139 [ # # ]: 0 : if (f1 == f2) {
140 : 0 : return b.GetTx().GetHash() < a.GetTx().GetHash();
141 : : }
142 : 0 : return f1 > f2;
143 : 0 : }
144 : : };
145 : :
146 : : class CompareTxMemPoolEntryByEntryTime
147 : : {
148 : : public:
149 : 0 : bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
150 : : {
151 : 0 : return a.GetTime() < b.GetTime();
152 : : }
153 : : };
154 : :
155 : : /** \class CompareTxMemPoolEntryByAncestorScore
156 : : *
157 : : * Sort an entry by min(score/size of entry's tx, score/size with all ancestors).
158 : : */
159 : : class CompareTxMemPoolEntryByAncestorFee
160 : : {
161 : : public:
162 : : template<typename T>
163 : 0 : bool operator()(const T& a, const T& b) const
164 : : {
165 : : double a_mod_fee, a_size, b_mod_fee, b_size;
166 : :
167 : 0 : GetModFeeAndSize(a, a_mod_fee, a_size);
168 : 0 : GetModFeeAndSize(b, b_mod_fee, b_size);
169 : :
170 : : // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
171 : 0 : double f1 = a_mod_fee * b_size;
172 : 0 : double f2 = a_size * b_mod_fee;
173 : :
174 [ # # ]: 0 : if (f1 == f2) {
175 : 0 : return a.GetTx().GetHash() < b.GetTx().GetHash();
176 : : }
177 : 0 : return f1 > f2;
178 : 0 : }
179 : :
180 : : // Return the fee/size we're using for sorting this entry.
181 : : template <typename T>
182 : 0 : void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
183 : : {
184 : : // Compare feerate with ancestors to feerate of the transaction, and
185 : : // return the fee/size for the min.
186 : 0 : double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
187 : 0 : double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
188 : :
189 [ # # ]: 0 : if (f1 > f2) {
190 : 0 : mod_fee = a.GetModFeesWithAncestors();
191 : 0 : size = a.GetSizeWithAncestors();
192 : 0 : } else {
193 : 0 : mod_fee = a.GetModifiedFee();
194 : 0 : size = a.GetTxSize();
195 : : }
196 : 0 : }
197 : : };
198 : :
199 : : // Multi_index tag names
200 : : struct descendant_score {};
201 : : struct entry_time {};
202 : : struct ancestor_score {};
203 : : struct index_by_wtxid {};
204 : :
205 : : class CBlockPolicyEstimator;
206 : :
207 : : /**
208 : : * Information about a mempool transaction.
209 : : */
210 : : struct TxMempoolInfo
211 : : {
212 : : /** The transaction itself */
213 : : CTransactionRef tx;
214 : :
215 : : /** Time the transaction entered the mempool. */
216 : : std::chrono::seconds m_time;
217 : :
218 : : /** Fee of the transaction. */
219 : : CAmount fee;
220 : :
221 : : /** Virtual size of the transaction. */
222 : : int32_t vsize;
223 : :
224 : : /** The fee delta. */
225 : : int64_t nFeeDelta;
226 : : };
227 : :
228 : : /**
229 : : * CTxMemPool stores valid-according-to-the-current-best-chain transactions
230 : : * that may be included in the next block.
231 : : *
232 : : * Transactions are added when they are seen on the network (or created by the
233 : : * local node), but not all transactions seen are added to the pool. For
234 : : * example, the following new transactions will not be added to the mempool:
235 : : * - a transaction which doesn't meet the minimum fee requirements.
236 : : * - a new transaction that double-spends an input of a transaction already in
237 : : * the pool where the new transaction does not meet the Replace-By-Fee
238 : : * requirements as defined in doc/policy/mempool-replacements.md.
239 : : * - a non-standard transaction.
240 : : *
241 : : * CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
242 : : *
243 : : * mapTx is a boost::multi_index that sorts the mempool on 5 criteria:
244 : : * - transaction hash (txid)
245 : : * - witness-transaction hash (wtxid)
246 : : * - descendant feerate [we use max(feerate of tx, feerate of tx with all descendants)]
247 : : * - time in mempool
248 : : * - ancestor feerate [we use min(feerate of tx, feerate of tx with all unconfirmed ancestors)]
249 : : *
250 : : * Note: the term "descendant" refers to in-mempool transactions that depend on
251 : : * this one, while "ancestor" refers to in-mempool transactions that a given
252 : : * transaction depends on.
253 : : *
254 : : * In order for the feerate sort to remain correct, we must update transactions
255 : : * in the mempool when new descendants arrive. To facilitate this, we track
256 : : * the set of in-mempool direct parents and direct children in mapLinks. Within
257 : : * each CTxMemPoolEntry, we track the size and fees of all descendants.
258 : : *
259 : : * Usually when a new transaction is added to the mempool, it has no in-mempool
260 : : * children (because any such children would be an orphan). So in
261 : : * addUnchecked(), we:
262 : : * - update a new entry's setMemPoolParents to include all in-mempool parents
263 : : * - update the new entry's direct parents to include the new tx as a child
264 : : * - update all ancestors of the transaction to include the new tx's size/fee
265 : : *
266 : : * When a transaction is removed from the mempool, we must:
267 : : * - update all in-mempool parents to not track the tx in setMemPoolChildren
268 : : * - update all ancestors to not include the tx's size/fees in descendant state
269 : : * - update all in-mempool children to not include it as a parent
270 : : *
271 : : * These happen in UpdateForRemoveFromMempool(). (Note that when removing a
272 : : * transaction along with its descendants, we must calculate that set of
273 : : * transactions to be removed before doing the removal, or else the mempool can
274 : : * be in an inconsistent state where it's impossible to walk the ancestors of
275 : : * a transaction.)
276 : : *
277 : : * In the event of a reorg, the assumption that a newly added tx has no
278 : : * in-mempool children is false. In particular, the mempool is in an
279 : : * inconsistent state while new transactions are being added, because there may
280 : : * be descendant transactions of a tx coming from a disconnected block that are
281 : : * unreachable from just looking at transactions in the mempool (the linking
282 : : * transactions may also be in the disconnected block, waiting to be added).
283 : : * Because of this, there's not much benefit in trying to search for in-mempool
284 : : * children in addUnchecked(). Instead, in the special case of transactions
285 : : * being added from a disconnected block, we require the caller to clean up the
286 : : * state, to account for in-mempool, out-of-block descendants for all the
287 : : * in-block transactions by calling UpdateTransactionsFromBlock(). Note that
288 : : * until this is called, the mempool state is not consistent, and in particular
289 : : * mapLinks may not be correct (and therefore functions like
290 : : * CalculateMemPoolAncestors() and CalculateDescendants() that rely
291 : : * on them to walk the mempool are not generally safe to use).
292 : : *
293 : : * Computational limits:
294 : : *
295 : : * Updating all in-mempool ancestors of a newly added transaction can be slow,
296 : : * if no bound exists on how many in-mempool ancestors there may be.
297 : : * CalculateMemPoolAncestors() takes configurable limits that are designed to
298 : : * prevent these calculations from being too CPU intensive.
299 : : *
300 : : */
301 : : class CTxMemPool
302 : : {
303 : : protected:
304 : : const int m_check_ratio; //!< Value n means that 1 times in n we check.
305 : : std::atomic<unsigned int> nTransactionsUpdated{0}; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
306 : : CBlockPolicyEstimator* const minerPolicyEstimator;
307 : :
308 : : uint64_t totalTxSize GUARDED_BY(cs){0}; //!< sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141.
309 : : CAmount m_total_fee GUARDED_BY(cs){0}; //!< sum of all mempool tx's fees (NOT modified fee)
310 : : uint64_t cachedInnerUsage GUARDED_BY(cs){0}; //!< sum of dynamic memory usage of all the map elements (NOT the maps themselves)
311 : :
312 : : mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
313 : : mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
314 : : mutable double rollingMinimumFeeRate GUARDED_BY(cs){0}; //!< minimum fee to get into the pool, decreases exponentially
315 : : mutable Epoch m_epoch GUARDED_BY(cs){};
316 : :
317 : : // In-memory counter for external mempool tracking purposes.
318 : : // This number is incremented once every time a transaction
319 : : // is added or removed from the mempool for any reason.
320 : : mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
321 : :
322 : : void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs);
323 : :
324 : : bool m_load_tried GUARDED_BY(cs){false};
325 : :
326 : : CFeeRate GetMinFee(size_t sizelimit) const;
327 : :
328 : : public:
329 : :
330 : : static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
331 : :
332 : : typedef boost::multi_index_container<
333 : : CTxMemPoolEntry,
334 : : boost::multi_index::indexed_by<
335 : : // sorted by txid
336 : : boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
337 : : // sorted by wtxid
338 : : boost::multi_index::hashed_unique<
339 : : boost::multi_index::tag<index_by_wtxid>,
340 : : mempoolentry_wtxid,
341 : : SaltedTxidHasher
342 : : >,
343 : : // sorted by fee rate
344 : : boost::multi_index::ordered_non_unique<
345 : : boost::multi_index::tag<descendant_score>,
346 : : boost::multi_index::identity<CTxMemPoolEntry>,
347 : : CompareTxMemPoolEntryByDescendantScore
348 : : >,
349 : : // sorted by entry time
350 : : boost::multi_index::ordered_non_unique<
351 : : boost::multi_index::tag<entry_time>,
352 : : boost::multi_index::identity<CTxMemPoolEntry>,
353 : : CompareTxMemPoolEntryByEntryTime
354 : : >,
355 : : // sorted by fee rate with ancestors
356 : : boost::multi_index::ordered_non_unique<
357 : : boost::multi_index::tag<ancestor_score>,
358 : : boost::multi_index::identity<CTxMemPoolEntry>,
359 : : CompareTxMemPoolEntryByAncestorFee
360 : : >
361 : : >
362 : : > indexed_transaction_set;
363 : :
364 : : /**
365 : : * This mutex needs to be locked when accessing `mapTx` or other members
366 : : * that are guarded by it.
367 : : *
368 : : * @par Consistency guarantees
369 : : *
370 : : * By design, it is guaranteed that:
371 : : *
372 : : * 1. Locking both `cs_main` and `mempool.cs` will give a view of mempool
373 : : * that is consistent with current chain tip (`ActiveChain()` and
374 : : * `CoinsTip()`) and is fully populated. Fully populated means that if the
375 : : * current active chain is missing transactions that were present in a
376 : : * previously active chain, all the missing transactions will have been
377 : : * re-added to the mempool and should be present if they meet size and
378 : : * consistency constraints.
379 : : *
380 : : * 2. Locking `mempool.cs` without `cs_main` will give a view of a mempool
381 : : * consistent with some chain that was active since `cs_main` was last
382 : : * locked, and that is fully populated as described above. It is ok for
383 : : * code that only needs to query or remove transactions from the mempool
384 : : * to lock just `mempool.cs` without `cs_main`.
385 : : *
386 : : * To provide these guarantees, it is necessary to lock both `cs_main` and
387 : : * `mempool.cs` whenever adding transactions to the mempool and whenever
388 : : * changing the chain tip. It's necessary to keep both mutexes locked until
389 : : * the mempool is consistent with the new chain tip and fully populated.
390 : : */
391 : : mutable RecursiveMutex cs;
392 : : indexed_transaction_set mapTx GUARDED_BY(cs);
393 : :
394 : : using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
395 : : std::vector<std::pair<uint256, txiter>> vTxHashes GUARDED_BY(cs); //!< All tx witness hashes/entries in mapTx, in random order
396 : :
397 : : typedef std::set<txiter, CompareIteratorByHash> setEntries;
398 : :
399 : : using Limits = kernel::MemPoolLimits;
400 : :
401 : : uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
402 : : private:
403 : : typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
404 : :
405 : :
406 : : void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
407 : : void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
408 : :
409 : : std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
410 : :
411 : : /**
412 : : * Track locally submitted transactions to periodically retry initial broadcast.
413 : : */
414 : : std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
415 : :
416 : :
417 : : /**
418 : : * Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor
419 : : * and descendant limits (including staged_ancestors themselves, entry_size and entry_count).
420 : : *
421 : : * @param[in] entry_size Virtual size to include in the limits.
422 : : * @param[in] entry_count How many entries to include in the limits.
423 : : * @param[in] staged_ancestors Should contain entries in the mempool.
424 : : * @param[in] limits Maximum number and size of ancestors and descendants
425 : : *
426 : : * @return all in-mempool ancestors, or an error if any ancestor or descendant limits were hit
427 : : */
428 : : util::Result<setEntries> CalculateAncestorsAndCheckLimits(int64_t entry_size,
429 : : size_t entry_count,
430 : : CTxMemPoolEntry::Parents &staged_ancestors,
431 : : const Limits& limits
432 : : ) const EXCLUSIVE_LOCKS_REQUIRED(cs);
433 : :
434 : : public:
435 : : indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
436 : : std::map<uint256, CAmount> mapDeltas GUARDED_BY(cs);
437 : :
438 : : using Options = kernel::MemPoolOptions;
439 : :
440 : : const int64_t m_max_size_bytes;
441 : : const std::chrono::seconds m_expiry;
442 : : const CFeeRate m_incremental_relay_feerate;
443 : : const CFeeRate m_min_relay_feerate;
444 : : const CFeeRate m_dust_relay_feerate;
445 : : const bool m_permit_bare_multisig;
446 : : const std::optional<unsigned> m_max_datacarrier_bytes;
447 : : const bool m_require_standard;
448 : : const bool m_full_rbf;
449 : :
450 : : const Limits m_limits;
451 : :
452 : : /** Create a new CTxMemPool.
453 : : * Sanity checks will be off by default for performance, because otherwise
454 : : * accepting transactions becomes O(N^2) where N is the number of transactions
455 : : * in the pool.
456 : : */
457 : : explicit CTxMemPool(const Options& opts);
458 : :
459 : : /**
460 : : * If sanity-checking is turned on, check makes sure the pool is
461 : : * consistent (does not contain two transactions that spend the same inputs,
462 : : * all inputs are in the mapNextTx array). If sanity-checking is turned off,
463 : : * check does nothing.
464 : : */
465 : : void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
466 : :
467 : : // addUnchecked must updated state for all ancestors of a given transaction,
468 : : // to track size/count of descendant transactions. First version of
469 : : // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
470 : : // then invoke the second version.
471 : : // Note that addUnchecked is ONLY called from ATMP outside of tests
472 : : // and any other callers may break wallet's in-mempool tracking (due to
473 : : // lack of CValidationInterface::TransactionAddedToMempool callbacks).
474 : : void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
475 : : void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
476 : :
477 : : void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
478 : : /** After reorg, filter the entries that would no longer be valid in the next block, and update
479 : : * the entries' cached LockPoints if needed. The mempool does not have any knowledge of
480 : : * consensus rules. It just appplies the callable function and removes the ones for which it
481 : : * returns true.
482 : : * @param[in] filter_final_and_mature Predicate that checks the relevant validation rules
483 : : * and updates an entry's LockPoints.
484 : : * */
485 : : void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
486 : : void removeConflicts(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
487 : : void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
488 : :
489 : : bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
490 : : void queryHashes(std::vector<uint256>& vtxid) const;
491 : : bool isSpent(const COutPoint& outpoint) const;
492 : : unsigned int GetTransactionsUpdated() const;
493 : : void AddTransactionsUpdated(unsigned int n);
494 : : /**
495 : : * Check that none of this transactions inputs are in the mempool, and thus
496 : : * the tx is not dependent on other mempool transactions to be included in a block.
497 : : */
498 : : bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
499 : :
500 : : /** Affect CreateNewBlock prioritisation of transactions */
501 : : void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
502 : : void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
503 : : void ClearPrioritisation(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs);
504 : :
505 : : struct delta_info {
506 : : /** Whether this transaction is in the mempool. */
507 : : const bool in_mempool;
508 : : /** The fee delta added using PrioritiseTransaction(). */
509 : : const CAmount delta;
510 : : /** The modified fee (base fee + delta) of this entry. Only present if in_mempool=true. */
511 : : std::optional<CAmount> modified_fee;
512 : : /** The prioritised transaction's txid. */
513 : : const uint256 txid;
514 : : };
515 : : /** Return a vector of all entries in mapDeltas with their corresponding delta_info. */
516 : : std::vector<delta_info> GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
517 : :
518 : : /** Get the transaction in the pool that spends the same prevout */
519 : : const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
520 : :
521 : : /** Returns an iterator to the given hash, if found */
522 : : std::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
523 : :
524 : : /** Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
525 : : * Does not require that all of the hashes correspond to actual transactions in the mempool,
526 : : * only returns the ones that exist. */
527 : : setEntries GetIterSet(const std::set<uint256>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
528 : :
529 : : /** Translate a list of hashes into a list of mempool iterators to avoid repeated lookups.
530 : : * The nth element in txids becomes the nth element in the returned vector. If any of the txids
531 : : * don't actually exist in the mempool, returns an empty vector. */
532 : : std::vector<txiter> GetIterVec(const std::vector<uint256>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
533 : :
534 : : /** Remove a set of transactions from the mempool.
535 : : * If a transaction is in this set, then all in-mempool descendants must
536 : : * also be in the set, unless this transaction is being removed for being
537 : : * in a block.
538 : : * Set updateDescendants to true when removing a tx that was in a block, so
539 : : * that any in-mempool descendants have their ancestor state updated.
540 : : */
541 : : void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
542 : :
543 : : /** UpdateTransactionsFromBlock is called when adding transactions from a
544 : : * disconnected block back to the mempool, new mempool entries may have
545 : : * children in the mempool (which is generally not the case when otherwise
546 : : * adding transactions).
547 : : * @post updated descendant state for descendants of each transaction in
548 : : * vHashesToUpdate (excluding any child transactions present in
549 : : * vHashesToUpdate, which are already accounted for). Updated state
550 : : * includes add fee/size information for such descendants to the
551 : : * parent and updated ancestor state to include the parent.
552 : : *
553 : : * @param[in] vHashesToUpdate The set of txids from the
554 : : * disconnected block that have been accepted back into the mempool.
555 : : */
556 : : void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main) LOCKS_EXCLUDED(m_epoch);
557 : :
558 : : /**
559 : : * Try to calculate all in-mempool ancestors of entry.
560 : : * (these are all calculated including the tx itself)
561 : : *
562 : : * @param[in] entry CTxMemPoolEntry of which all in-mempool ancestors are calculated
563 : : * @param[in] limits Maximum number and size of ancestors and descendants
564 : : * @param[in] fSearchForParents Whether to search a tx's vin for in-mempool parents, or look
565 : : * up parents from mapLinks. Must be true for entries not in
566 : : * the mempool
567 : : *
568 : : * @return all in-mempool ancestors, or an error if any ancestor or descendant limits were hit
569 : : */
570 : : util::Result<setEntries> CalculateMemPoolAncestors(const CTxMemPoolEntry& entry,
571 : : const Limits& limits,
572 : : bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
573 : :
574 : : /**
575 : : * Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries.
576 : : * Should only be used when it is assumed CalculateMemPoolAncestors would not fail. If
577 : : * CalculateMemPoolAncestors does unexpectedly fail, an empty setEntries is returned and the
578 : : * error is logged to BCLog::MEMPOOL with level BCLog::Level::Error. In debug builds, failure
579 : : * of CalculateMemPoolAncestors will lead to shutdown due to assertion failure.
580 : : *
581 : : * @param[in] calling_fn_name Name of calling function so we can properly log the call site
582 : : *
583 : : * @return a setEntries corresponding to the result of CalculateMemPoolAncestors or an empty
584 : : * setEntries if it failed
585 : : *
586 : : * @see CTXMemPool::CalculateMemPoolAncestors()
587 : : */
588 : : setEntries AssumeCalculateMemPoolAncestors(
589 : : std::string_view calling_fn_name,
590 : : const CTxMemPoolEntry &entry,
591 : : const Limits& limits,
592 : : bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
593 : :
594 : : /** Collect the entire cluster of connected transactions for each transaction in txids.
595 : : * All txids must correspond to transaction entries in the mempool, otherwise this returns an
596 : : * empty vector. This call will also exit early and return an empty vector if it collects 500 or
597 : : * more transactions as a DoS protection. */
598 : : std::vector<txiter> GatherClusters(const std::vector<uint256>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
599 : :
600 : : /** Calculate all in-mempool ancestors of a set of transactions not already in the mempool and
601 : : * check ancestor and descendant limits. Heuristics are used to estimate the ancestor and
602 : : * descendant count of all entries if the package were to be added to the mempool. The limits
603 : : * are applied to the union of all package transactions. For example, if the package has 3
604 : : * transactions and limits.ancestor_count = 25, the union of all 3 sets of ancestors (including the
605 : : * transactions themselves) must be <= 22.
606 : : * @param[in] package Transaction package being evaluated for acceptance
607 : : * to mempool. The transactions need not be direct
608 : : * ancestors/descendants of each other.
609 : : * @param[in] total_vsize Sum of virtual sizes for all transactions in package.
610 : : * @param[out] errString Populated with error reason if a limit is hit.
611 : : */
612 : : bool CheckPackageLimits(const Package& package,
613 : : int64_t total_vsize,
614 : : std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs);
615 : :
616 : : /** Populate setDescendants with all in-mempool descendants of hash.
617 : : * Assumes that setDescendants includes all in-mempool descendants of anything
618 : : * already in it. */
619 : : void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
620 : :
621 : : /** The minimum fee to get into the mempool, which may itself not be enough
622 : : * for larger-sized transactions.
623 : : * The m_incremental_relay_feerate policy variable is used to bound the time it
624 : : * takes the fee rate to go back down all the way to 0. When the feerate
625 : : * would otherwise be half of this, it is set to 0 instead.
626 : : */
627 : 0 : CFeeRate GetMinFee() const {
628 : 0 : return GetMinFee(m_max_size_bytes);
629 : : }
630 : :
631 : : /** Remove transactions from the mempool until its dynamic size is <= sizelimit.
632 : : * pvNoSpendsRemaining, if set, will be populated with the list of outpoints
633 : : * which are not in mempool which no longer have any spends in this mempool.
634 : : */
635 : : void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
636 : :
637 : : /** Expire all transaction (and their dependencies) in the mempool older than time. Return the number of removed transactions. */
638 : : int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
639 : :
640 : : /**
641 : : * Calculate the ancestor and descendant count for the given transaction.
642 : : * The counts include the transaction itself.
643 : : * When ancestors is non-zero (ie, the transaction itself is in the mempool),
644 : : * ancestorsize and ancestorfees will also be set to the appropriate values.
645 : : */
646 : : void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
647 : :
648 : : /**
649 : : * @returns true if an initial attempt to load the persisted mempool was made, regardless of
650 : : * whether the attempt was successful or not
651 : : */
652 : : bool GetLoadTried() const;
653 : :
654 : : /**
655 : : * Set whether or not an initial attempt to load the persisted mempool was made (regardless
656 : : * of whether the attempt was successful or not)
657 : : */
658 : : void SetLoadTried(bool load_tried);
659 : :
660 : 0 : unsigned long size() const
661 : : {
662 : 0 : LOCK(cs);
663 : 0 : return mapTx.size();
664 : 0 : }
665 : :
666 : 0 : uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
667 : : {
668 : 0 : AssertLockHeld(cs);
669 : 0 : return totalTxSize;
670 : : }
671 : :
672 : 0 : CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
673 : : {
674 : 0 : AssertLockHeld(cs);
675 : 0 : return m_total_fee;
676 : : }
677 : :
678 : 0 : bool exists(const GenTxid& gtxid) const
679 : : {
680 : 0 : LOCK(cs);
681 [ # # # # ]: 0 : if (gtxid.IsWtxid()) {
682 [ # # # # ]: 0 : return (mapTx.get<index_by_wtxid>().count(gtxid.GetHash()) != 0);
683 : : }
684 [ # # # # ]: 0 : return (mapTx.count(gtxid.GetHash()) != 0);
685 : 0 : }
686 : :
687 : : CTransactionRef get(const uint256& hash) const;
688 : 0 : txiter get_iter_from_wtxid(const uint256& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
689 : : {
690 : 0 : AssertLockHeld(cs);
691 : 0 : return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
692 : : }
693 : : TxMempoolInfo info(const GenTxid& gtxid) const;
694 : :
695 : : /** Returns info for a transaction if its entry_sequence < last_sequence */
696 : : TxMempoolInfo info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const;
697 : :
698 : : std::vector<TxMempoolInfo> infoAll() const;
699 : :
700 : : size_t DynamicMemoryUsage() const;
701 : :
702 : : /** Adds a transaction to the unbroadcast set */
703 : 0 : void AddUnbroadcastTx(const uint256& txid)
704 : : {
705 : 0 : LOCK(cs);
706 : : // Sanity check the transaction is in the mempool & insert into
707 : : // unbroadcast set.
708 [ # # # # : 0 : if (exists(GenTxid::Txid(txid))) m_unbroadcast_txids.insert(txid);
# # # # ]
709 : 0 : };
710 : :
711 : : /** Removes a transaction from the unbroadcast set */
712 : : void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
713 : :
714 : : /** Returns transactions in unbroadcast set */
715 : 0 : std::set<uint256> GetUnbroadcastTxs() const
716 : : {
717 : 0 : LOCK(cs);
718 [ # # ]: 0 : return m_unbroadcast_txids;
719 : 0 : }
720 : :
721 : : /** Returns whether a txid is in the unbroadcast set */
722 : 0 : bool IsUnbroadcastTx(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
723 : : {
724 : 0 : AssertLockHeld(cs);
725 : 0 : return m_unbroadcast_txids.count(txid) != 0;
726 : : }
727 : :
728 : : /** Guards this internal counter for external reporting */
729 : 0 : uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs) {
730 : 0 : return m_sequence_number++;
731 : : }
732 : :
733 : 0 : uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs) {
734 : 0 : return m_sequence_number;
735 : : }
736 : :
737 : : private:
738 : : /** UpdateForDescendants is used by UpdateTransactionsFromBlock to update
739 : : * the descendants for a single transaction that has been added to the
740 : : * mempool but may have child transactions in the mempool, eg during a
741 : : * chain reorg.
742 : : *
743 : : * @pre CTxMemPoolEntry::m_children is correct for the given tx and all
744 : : * descendants.
745 : : * @pre cachedDescendants is an accurate cache where each entry has all
746 : : * descendants of the corresponding key, including those that should
747 : : * be removed for violation of ancestor limits.
748 : : * @post if updateIt has any non-excluded descendants, cachedDescendants has
749 : : * a new cache line for updateIt.
750 : : * @post descendants_to_remove has a new entry for any descendant which exceeded
751 : : * ancestor limits relative to updateIt.
752 : : *
753 : : * @param[in] updateIt the entry to update for its descendants
754 : : * @param[in,out] cachedDescendants a cache where each line corresponds to all
755 : : * descendants. It will be updated with the descendants of the transaction
756 : : * being updated, so that future invocations don't need to walk the same
757 : : * transaction again, if encountered in another transaction chain.
758 : : * @param[in] setExclude the set of descendant transactions in the mempool
759 : : * that must not be accounted for (because any descendants in setExclude
760 : : * were added to the mempool after the transaction being updated and hence
761 : : * their state is already reflected in the parent state).
762 : : * @param[out] descendants_to_remove Populated with the txids of entries that
763 : : * exceed ancestor limits. It's the responsibility of the caller to
764 : : * removeRecursive them.
765 : : */
766 : : void UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
767 : : const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs);
768 : : /** Update ancestors of hash to add/remove it as a descendant transaction. */
769 : : void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
770 : : /** Set ancestor state for an entry */
771 : : void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
772 : : /** For each transaction being removed, update ancestors and any direct children.
773 : : * If updateDescendants is true, then also update in-mempool descendants'
774 : : * ancestor state. */
775 : : void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
776 : : /** Sever link between specified transaction and direct children. */
777 : : void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs);
778 : :
779 : : /** Before calling removeUnchecked for a given transaction,
780 : : * UpdateForRemoveFromMempool must be called on the entire (dependent) set
781 : : * of transactions being removed at the same time. We use each
782 : : * CTxMemPoolEntry's setMemPoolParents in order to walk ancestors of a
783 : : * given transaction that is removed, so we can't remove intermediate
784 : : * transactions in a chain before we've updated all the state for the
785 : : * removal.
786 : : */
787 : : void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
788 : : public:
789 : : /** visited marks a CTxMemPoolEntry as having been traversed
790 : : * during the lifetime of the most recently created Epoch::Guard
791 : : * and returns false if we are the first visitor, true otherwise.
792 : : *
793 : : * An Epoch::Guard must be held when visited is called or an assert will be
794 : : * triggered.
795 : : *
796 : : */
797 : 0 : bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
798 : : {
799 : 0 : return m_epoch.visited(it->m_epoch_marker);
800 : : }
801 : :
802 : : bool visited(std::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
803 : : {
804 : : assert(m_epoch.guarded()); // verify guard even when it==nullopt
805 : : return !it || visited(*it);
806 : : }
807 : : };
808 : :
809 : : /**
810 : : * CCoinsView that brings transactions from a mempool into view.
811 : : * It does not check for spendings by memory pool transactions.
812 : : * Instead, it provides access to all Coins which are either unspent in the
813 : : * base CCoinsView, are outputs from any mempool transaction, or are
814 : : * tracked temporarily to allow transaction dependencies in package validation.
815 : : * This allows transaction replacement to work as expected, as you want to
816 : : * have all inputs "available" to check signatures, and any cycles in the
817 : : * dependency graph are checked directly in AcceptToMemoryPool.
818 : : * It also allows you to sign a double-spend directly in
819 : : * signrawtransactionwithkey and signrawtransactionwithwallet,
820 : : * as long as the conflicting transaction is not yet confirmed.
821 : : */
822 : : class CCoinsViewMemPool : public CCoinsViewBacked
823 : : {
824 : : /**
825 : : * Coins made available by transactions being validated. Tracking these allows for package
826 : : * validation, since we can access transaction outputs without submitting them to mempool.
827 : : */
828 : : std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
829 : :
830 : : /**
831 : : * Set of all coins that have been fetched from mempool or created using PackageAddTransaction
832 : : * (not base). Used to track the origin of a coin, see GetNonBaseCoins().
833 : : */
834 : : mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
835 : : protected:
836 : : const CTxMemPool& mempool;
837 : :
838 : : public:
839 : : CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
840 : : /** GetCoin, returning whether it exists and is not spent. Also updates m_non_base_coins if the
841 : : * coin is not fetched from base. */
842 : : bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
843 : : /** Add the coins created by this transaction. These coins are only temporarily stored in
844 : : * m_temp_added and cannot be flushed to the back end. Only used for package validation. */
845 : : void PackageAddTransaction(const CTransactionRef& tx);
846 : : /** Get all coins in m_non_base_coins. */
847 : 0 : std::unordered_set<COutPoint, SaltedOutpointHasher> GetNonBaseCoins() const { return m_non_base_coins; }
848 : : /** Clear m_temp_added and m_non_base_coins. */
849 : : void Reset();
850 : : };
851 : : #endif // BITCOIN_TXMEMPOOL_H
|