Coverage Report

Created: 2024-11-15 12:18

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