Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/node/mini_miner.cpp
Line
Count
Source
1
// Copyright (c) 2023-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
#include <node/mini_miner.h>
6
7
#include <boost/multi_index/detail/hash_index_iterator.hpp>
8
#include <boost/operators.hpp>
9
#include <consensus/amount.h>
10
#include <policy/feerate.h>
11
#include <primitives/transaction.h>
12
#include <sync.h>
13
#include <txmempool.h>
14
#include <uint256.h>
15
#include <util/check.h>
16
17
#include <algorithm>
18
#include <numeric>
19
#include <ranges>
20
#include <utility>
21
22
namespace node {
23
24
MiniMiner::MiniMiner(const CTxMemPool& mempool, const std::vector<COutPoint>& outpoints)
25
13.5k
{
26
13.5k
    LOCK(mempool.cs);
27
    // Find which outpoints to calculate bump fees for.
28
    // Anything that's spent by the mempool is to-be-replaced
29
    // Anything otherwise unavailable just has a bump fee of 0
30
156k
    for (const auto& outpoint : outpoints) {
31
156k
        if (!mempool.exists(outpoint.hash)) {
32
            // This UTXO is either confirmed or not yet submitted to mempool.
33
            // If it's confirmed, no bump fee is required.
34
            // If it's not yet submitted, we have no information, so return 0.
35
106k
            m_bump_fees.emplace(outpoint, 0);
36
106k
            continue;
37
106k
        }
38
39
        // UXTO is created by transaction in mempool, add to map.
40
        // Note: This will either create a missing entry or add the outpoint to an existing entry
41
49.8k
        m_requested_outpoints_by_txid[outpoint.hash].push_back(outpoint);
42
43
49.8k
        if (const auto ptx{mempool.GetConflictTx(outpoint)}) {
44
            // This outpoint is already being spent by another transaction in the mempool. We
45
            // assume that the caller wants to replace this transaction and its descendants. It
46
            // would be unusual for the transaction to have descendants as the wallet won’t normally
47
            // attempt to replace transactions with descendants. If the outpoint is from a mempool
48
            // transaction, we still need to calculate its ancestors bump fees (added to
49
            // m_requested_outpoints_by_txid below), but after removing the to-be-replaced entries.
50
            //
51
            // Note that the descendants of a transaction include the transaction itself. Also note,
52
            // that this is only calculating bump fees. RBF fee rules should be handled separately.
53
112
            CTxMemPool::setEntries descendants;
54
112
            mempool.CalculateDescendants(mempool.GetIter(ptx->GetHash()).value(), descendants);
55
112
            for (const auto& desc_txiter : descendants) {
56
112
                m_to_be_replaced.insert(desc_txiter->GetTx().GetHash());
57
112
            }
58
112
        }
59
49.8k
    }
60
61
    // No unconfirmed UTXOs, so nothing mempool-related needs to be calculated.
62
13.5k
    if (m_requested_outpoints_by_txid.empty()) return;
63
64
    // Calculate the cluster and construct the entry map.
65
3.08k
    auto txids_needed{m_requested_outpoints_by_txid | std::views::keys};
66
3.08k
    const auto cluster = mempool.GatherClusters({txids_needed.begin(), txids_needed.end()});
67
3.08k
    if (cluster.empty()) {
68
        // An empty cluster means that at least one of the transactions is missing from the mempool
69
        // (should not be possible given processing above) or DoS limit was hit.
70
0
        m_ready_to_calculate = false;
71
0
        return;
72
0
    }
73
74
    // Add every entry to m_entries_by_txid and m_entries, except the ones that will be replaced.
75
68.3k
    for (const auto& txiter : cluster) {
76
68.3k
        if (!m_to_be_replaced.contains(txiter->GetTx().GetHash())) {
77
68.2k
            auto [ancestor_count, ancestor_size, ancestor_fee] = mempool.CalculateAncestorData(*txiter);
78
68.2k
            auto [mapiter, success] = m_entries_by_txid.emplace(txiter->GetTx().GetHash(),
79
68.2k
                MiniMinerMempoolEntry{/*tx_in=*/txiter->GetSharedTx(),
80
68.2k
                                      /*vsize_self=*/txiter->GetTxSize(),
81
68.2k
                                      /*vsize_ancestor=*/int64_t(ancestor_size),
82
68.2k
                                      /*fee_self=*/txiter->GetModifiedFee(),
83
68.2k
                                      /*fee_ancestor=*/ancestor_fee});
84
68.2k
            m_entries.push_back(mapiter);
85
68.2k
        } else {
86
112
            auto outpoints_it = m_requested_outpoints_by_txid.find(txiter->GetTx().GetHash());
87
112
            if (outpoints_it != m_requested_outpoints_by_txid.end()) {
88
                // This UTXO is the output of a to-be-replaced transaction. Bump fee is 0; spending
89
                // this UTXO is impossible as it will no longer exist after the replacement.
90
0
                for (const auto& outpoint : outpoints_it->second) {
91
0
                    m_bump_fees.emplace(outpoint, 0);
92
0
                }
93
0
                m_requested_outpoints_by_txid.erase(outpoints_it);
94
0
            }
95
112
        }
96
68.3k
    }
97
98
    // Build the m_descendant_set_by_txid cache.
99
68.3k
    for (const auto& txiter : cluster) {
100
68.3k
        const auto& txid = txiter->GetTx().GetHash();
101
        // Cache descendants for future use. Unlike the real mempool, a descendant MiniMinerMempoolEntry
102
        // will not exist without its ancestor MiniMinerMempoolEntry, so these sets won't be invalidated.
103
68.3k
        std::vector<MockEntryMap::iterator> cached_descendants;
104
68.3k
        const bool remove{m_to_be_replaced.contains(txid)};
105
68.3k
        CTxMemPool::setEntries descendants;
106
68.3k
        mempool.CalculateDescendants(txiter, descendants);
107
68.3k
        Assume(descendants.contains(txiter));
108
100k
        for (const auto& desc_txiter : descendants) {
109
100k
            const auto txid_desc = desc_txiter->GetTx().GetHash();
110
100k
            const bool remove_desc{m_to_be_replaced.contains(txid_desc)};
111
100k
            auto desc_it{m_entries_by_txid.find(txid_desc)};
112
100k
            Assume((desc_it == m_entries_by_txid.end()) == remove_desc);
113
100k
            if (remove) Assume(remove_desc);
114
            // It's possible that remove=false but remove_desc=true.
115
100k
            if (!remove && !remove_desc) {
116
100k
                cached_descendants.push_back(desc_it);
117
100k
            }
118
100k
        }
119
68.3k
        if (remove) {
120
112
            Assume(cached_descendants.empty());
121
68.2k
        } else {
122
68.2k
            m_descendant_set_by_txid.emplace(txid, cached_descendants);
123
68.2k
        }
124
68.3k
    }
125
126
    // Release the mempool lock; we now have all the information we need for a subset of the entries
127
    // we care about. We will solely operate on the MiniMinerMempoolEntry map from now on.
128
3.08k
    Assume(m_in_block.empty());
129
3.08k
    Assume(m_requested_outpoints_by_txid.size() <= outpoints.size());
130
3.08k
    SanityCheck();
131
3.08k
}
132
133
MiniMiner::MiniMiner(const std::vector<MiniMinerMempoolEntry>& manual_entries,
134
                     const std::map<Txid, std::set<Txid>>& descendant_caches)
135
3
{
136
23
    for (const auto& entry : manual_entries) {
137
23
        const auto& txid = entry.GetTx().GetHash();
138
        // We need to know the descendant set of every transaction.
139
23
        if (!Assume(descendant_caches.contains(txid))) {
140
0
            m_ready_to_calculate = false;
141
0
            return;
142
0
        }
143
        // Just forward these args onto MiniMinerMempoolEntry
144
23
        auto [mapiter, success] = m_entries_by_txid.emplace(txid, entry);
145
        // Txids must be unique; this txid shouldn't already be an entry in m_entries_by_txid
146
23
        if (Assume(success)) m_entries.push_back(mapiter);
147
23
    }
148
    // Descendant cache is already built, but we need to translate them to m_entries_by_txid iters.
149
23
    for (const auto& [txid, desc_txids] : descendant_caches) {
150
        // Descendant cache should include at least the tx itself.
151
23
        if (!Assume(!desc_txids.empty())) {
152
0
            m_ready_to_calculate = false;
153
0
            return;
154
0
        }
155
23
        std::vector<MockEntryMap::iterator> descendants;
156
44
        for (const auto& desc_txid : desc_txids) {
157
44
            auto desc_it{m_entries_by_txid.find(desc_txid)};
158
            // Descendants should only include transactions with corresponding entries.
159
44
            if (!Assume(desc_it != m_entries_by_txid.end())) {
160
0
                m_ready_to_calculate = false;
161
0
                return;
162
44
            } else {
163
44
                descendants.emplace_back(desc_it);
164
44
            }
165
44
        }
166
23
        m_descendant_set_by_txid.emplace(txid, descendants);
167
23
    }
168
3
    Assume(m_to_be_replaced.empty());
169
3
    Assume(m_requested_outpoints_by_txid.empty());
170
3
    Assume(m_bump_fees.empty());
171
3
    Assume(m_inclusion_order.empty());
172
3
    SanityCheck();
173
3
}
174
175
// Compare by min(ancestor feerate, individual feerate), then txid
176
//
177
// Under the ancestor-based mining approach, high-feerate children can pay for parents, but high-feerate
178
// parents do not incentive inclusion of their children. Therefore the mining algorithm only considers
179
// transactions for inclusion on basis of the minimum of their own feerate or their ancestor feerate.
180
struct AncestorFeerateComparator
181
{
182
    template<typename I>
183
19.4M
    bool operator()(const I& a, const I& b) const {
184
38.9M
        auto min_feerate = [](const MiniMinerMempoolEntry& e) -> FeeFrac {
185
38.9M
            FeeFrac self_feerate(e.GetModifiedFee(), e.GetTxSize());
186
38.9M
            FeeFrac ancestor_feerate(e.GetModFeesWithAncestors(), e.GetSizeWithAncestors());
187
38.9M
            return std::min(ancestor_feerate, self_feerate);
188
38.9M
        };
189
19.4M
        FeeFrac a_feerate{min_feerate(a->second)};
190
19.4M
        FeeFrac b_feerate{min_feerate(b->second)};
191
19.4M
        if (a_feerate != b_feerate) {
192
3.57M
            return a_feerate > b_feerate;
193
3.57M
        }
194
        // Use txid as tiebreaker for stable sorting
195
15.8M
        return a->first < b->first;
196
19.4M
    }
197
};
198
199
void MiniMiner::DeleteAncestorPackage(const std::set<MockEntryMap::iterator, IteratorComparator>& ancestors)
200
67.3k
{
201
67.3k
    Assume(ancestors.size() >= 1);
202
    // "Mine" all transactions in this ancestor set.
203
67.9k
    for (auto& anc : ancestors) {
204
67.9k
        Assume(!m_in_block.contains(anc->first));
205
67.9k
        m_in_block.insert(anc->first);
206
67.9k
        m_total_fees += anc->second.GetModifiedFee();
207
67.9k
        m_total_vsize += anc->second.GetTxSize();
208
67.9k
        auto it = m_descendant_set_by_txid.find(anc->first);
209
        // Each entry’s descendant set includes itself
210
67.9k
        Assume(it != m_descendant_set_by_txid.end());
211
100k
        for (auto& descendant : it->second) {
212
            // If this fails, we must be double-deducting. Don't check fees because negative is possible.
213
100k
            Assume(descendant->second.GetSizeWithAncestors() >= anc->second.GetTxSize());
214
100k
            descendant->second.UpdateAncestorState(-anc->second.GetTxSize(), -anc->second.GetModifiedFee());
215
100k
        }
216
67.9k
    }
217
    // Delete these entries.
218
67.9k
    for (const auto& anc : ancestors) {
219
67.9k
        m_descendant_set_by_txid.erase(anc->first);
220
        // The above loop should have deducted each ancestor's size and fees from each of their
221
        // respective descendants exactly once.
222
67.9k
        Assume(anc->second.GetModFeesWithAncestors() == 0);
223
67.9k
        Assume(anc->second.GetSizeWithAncestors() == 0);
224
67.9k
        auto vec_it = std::find(m_entries.begin(), m_entries.end(), anc);
225
67.9k
        Assume(vec_it != m_entries.end());
226
67.9k
        m_entries.erase(vec_it);
227
67.9k
        m_entries_by_txid.erase(anc);
228
67.9k
    }
229
67.3k
}
230
231
void MiniMiner::SanityCheck() const
232
70.4k
{
233
    // m_entries, m_entries_by_txid, and m_descendant_set_by_txid all same size
234
70.4k
    Assume(m_entries.size() == m_entries_by_txid.size());
235
70.4k
    Assume(m_entries.size() == m_descendant_set_by_txid.size());
236
    // Cached ancestor values should be at least as large as the transaction's own size
237
70.4k
    Assume(std::all_of(m_entries.begin(), m_entries.end(), [](const auto& entry) {
238
70.4k
        return entry->second.GetSizeWithAncestors() >= entry->second.GetTxSize();}));
239
    // None of the entries should be to-be-replaced transactions
240
70.4k
    Assume(std::all_of(m_to_be_replaced.begin(), m_to_be_replaced.end(),
241
70.4k
        [&](const auto& txid){ return !m_entries_by_txid.contains(txid); }));
242
70.4k
}
243
244
void MiniMiner::BuildMockTemplate(std::optional<CFeeRate> target_feerate)
245
13.5k
{
246
13.5k
    const auto num_txns{m_entries_by_txid.size()};
247
13.5k
    uint32_t sequence_num{0};
248
80.8k
    while (!m_entries_by_txid.empty()) {
249
        // Sort again, since transaction removal may change some m_entries' ancestor feerates.
250
67.5k
        std::sort(m_entries.begin(), m_entries.end(), AncestorFeerateComparator());
251
252
        // Pick highest ancestor feerate entry.
253
67.5k
        auto best_iter = m_entries.begin();
254
67.5k
        Assume(best_iter != m_entries.end());
255
67.5k
        const auto ancestor_package_size = (*best_iter)->second.GetSizeWithAncestors();
256
67.5k
        const auto ancestor_package_fee = (*best_iter)->second.GetModFeesWithAncestors();
257
        // Stop here. Everything that didn't "make it into the block" has bumpfee.
258
67.5k
        if (target_feerate.has_value() &&
259
67.5k
            ancestor_package_fee < target_feerate->GetFee(ancestor_package_size)) {
260
209
            break;
261
209
        }
262
263
        // Calculate ancestors on the fly. This lookup should be fairly cheap, and ancestor sets
264
        // change at every iteration, so this is more efficient than maintaining a cache.
265
67.3k
        std::set<MockEntryMap::iterator, IteratorComparator> ancestors;
266
67.3k
        {
267
67.3k
            std::set<MockEntryMap::iterator, IteratorComparator> to_process;
268
67.3k
            to_process.insert(*best_iter);
269
135k
            while (!to_process.empty()) {
270
67.9k
                auto iter = to_process.begin();
271
67.9k
                Assume(iter != to_process.end());
272
67.9k
                ancestors.insert(*iter);
273
72.8k
                for (const auto& input : (*iter)->second.GetTx().vin) {
274
72.8k
                    if (auto parent_it{m_entries_by_txid.find(input.prevout.hash)}; parent_it != m_entries_by_txid.end()) {
275
590
                        if (!ancestors.contains(parent_it)) {
276
590
                            to_process.insert(parent_it);
277
590
                        }
278
590
                    }
279
72.8k
                }
280
67.9k
                to_process.erase(iter);
281
67.9k
            }
282
67.3k
        }
283
        // Track the order in which transactions were selected.
284
67.9k
        for (const auto& ancestor : ancestors) {
285
67.9k
            m_inclusion_order.emplace(ancestor->first, sequence_num);
286
67.9k
        }
287
67.3k
        DeleteAncestorPackage(ancestors);
288
67.3k
        SanityCheck();
289
67.3k
        ++sequence_num;
290
67.3k
    }
291
13.5k
    if (!target_feerate.has_value()) {
292
6
        Assume(m_in_block.size() == num_txns);
293
13.5k
    } else {
294
13.5k
        Assume(m_in_block.empty() || m_total_fees >= target_feerate->GetFee(m_total_vsize));
295
13.5k
    }
296
13.5k
    Assume(m_in_block.empty() || sequence_num > 0);
297
13.5k
    Assume(m_in_block.size() == m_inclusion_order.size());
298
    // Do not try to continue building the block template with a different feerate.
299
13.5k
    m_ready_to_calculate = false;
300
13.5k
}
301
302
303
std::map<Txid, uint32_t> MiniMiner::Linearize()
304
5
{
305
5
    BuildMockTemplate(std::nullopt);
306
5
    return m_inclusion_order;
307
5
}
308
309
std::map<COutPoint, CAmount> MiniMiner::CalculateBumpFees(const CFeeRate& target_feerate)
310
4.17k
{
311
4.17k
    if (!m_ready_to_calculate) return {};
312
    // Build a block template until the target feerate is hit.
313
4.17k
    BuildMockTemplate(target_feerate);
314
315
    // Each transaction that "made it into the block" has a bumpfee of 0, i.e. they are part of an
316
    // ancestor package with at least the target feerate and don't need to be bumped.
317
65.0k
    for (const auto& txid : m_in_block) {
318
        // Not all of the block transactions were necessarily requested.
319
65.0k
        auto it = m_requested_outpoints_by_txid.find(txid);
320
65.0k
        if (it != m_requested_outpoints_by_txid.end()) {
321
47.8k
            for (const auto& outpoint : it->second) {
322
47.8k
                m_bump_fees.emplace(outpoint, 0);
323
47.8k
            }
324
47.6k
            m_requested_outpoints_by_txid.erase(it);
325
47.6k
        }
326
65.0k
    }
327
328
    // A transactions and its ancestors will only be picked into a block when
329
    // both the ancestor set feerate and the individual feerate meet the target
330
    // feerate.
331
    //
332
    // We had to convince ourselves that after running the mini miner and
333
    // picking all eligible transactions into our MockBlockTemplate, there
334
    // could still be transactions remaining that have a lower individual
335
    // feerate than their ancestor feerate. So here is an example:
336
    //
337
    //               ┌─────────────────┐
338
    //               │                 │
339
    //               │   Grandparent   │
340
    //               │    1700 vB      │
341
    //               │    1700 sats    │                    Target feerate: 10    s/vB
342
    //               │       1 s/vB    │    GP Ancestor Set Feerate (ASFR):  1    s/vB
343
    //               │                 │                           P1_ASFR:  9.84 s/vB
344
    //               └──────▲───▲──────┘                           P2_ASFR:  2.47 s/vB
345
    //                      │   │                                   C_ASFR: 10.27 s/vB
346
    // ┌───────────────┐    │   │    ┌──────────────┐
347
    // │               ├────┘   └────┤              │             ⇒ C_FR < TFR < C_ASFR
348
    // │   Parent 1    │             │   Parent 2   │
349
    // │    200 vB     │             │    200 vB    │
350
    // │  17000 sats   │             │   3000 sats  │
351
    // │     85 s/vB   │             │     15 s/vB  │
352
    // │               │             │              │
353
    // └───────────▲───┘             └───▲──────────┘
354
    //             │                     │
355
    //             │    ┌───────────┐    │
356
    //             └────┤           ├────┘
357
    //                  │   Child   │
358
    //                  │  100 vB   │
359
    //                  │  900 sats │
360
    //                  │    9 s/vB │
361
    //                  │           │
362
    //                  └───────────┘
363
    //
364
    // We therefore calculate both the bump fee that is necessary to elevate
365
    // the individual transaction to the target feerate:
366
    //         target_feerate × tx_size - tx_fees
367
    // and the bump fee that is necessary to bump the entire ancestor set to
368
    // the target feerate:
369
    //         target_feerate × ancestor_set_size - ancestor_set_fees
370
    // By picking the maximum from the two, we ensure that a transaction meets
371
    // both criteria.
372
4.17k
    for (const auto& [txid, outpoints] : m_requested_outpoints_by_txid) {
373
165
        auto it = m_entries_by_txid.find(txid);
374
165
        Assume(it != m_entries_by_txid.end());
375
165
        if (it != m_entries_by_txid.end()) {
376
165
            Assume(target_feerate.GetFee(it->second.GetSizeWithAncestors()) > std::min(it->second.GetModifiedFee(), it->second.GetModFeesWithAncestors()));
377
165
            CAmount bump_fee_with_ancestors = target_feerate.GetFee(it->second.GetSizeWithAncestors()) - it->second.GetModFeesWithAncestors();
378
165
            CAmount bump_fee_individual = target_feerate.GetFee(it->second.GetTxSize()) - it->second.GetModifiedFee();
379
165
            const CAmount bump_fee{std::max(bump_fee_with_ancestors, bump_fee_individual)};
380
165
            Assume(bump_fee >= 0);
381
239
            for (const auto& outpoint : outpoints) {
382
239
                m_bump_fees.emplace(outpoint, bump_fee);
383
239
            }
384
165
        }
385
165
    }
386
4.17k
    return m_bump_fees;
387
4.17k
}
388
389
std::optional<CAmount> MiniMiner::CalculateTotalBumpFees(const CFeeRate& target_feerate)
390
9.35k
{
391
9.35k
    if (!m_ready_to_calculate) return std::nullopt;
392
    // Build a block template until the target feerate is hit.
393
9.35k
    BuildMockTemplate(target_feerate);
394
395
    // All remaining ancestors that are not part of m_in_block must be bumped, but no other relatives
396
9.35k
    std::set<MockEntryMap::iterator, IteratorComparator> ancestors;
397
9.35k
    std::set<MockEntryMap::iterator, IteratorComparator> to_process;
398
9.35k
    for (const auto& [txid, outpoints] : m_requested_outpoints_by_txid) {
399
        // Skip any ancestors that already have a miner score higher than the target feerate
400
        // (already "made it" into the block)
401
1.75k
        if (m_in_block.contains(txid)) continue;
402
98
        auto iter = m_entries_by_txid.find(txid);
403
98
        if (iter == m_entries_by_txid.end()) continue;
404
98
        to_process.insert(iter);
405
98
        ancestors.insert(iter);
406
98
    }
407
408
9.35k
    std::set<Txid> has_been_processed;
409
9.46k
    while (!to_process.empty()) {
410
116
        auto iter = to_process.begin();
411
116
        const CTransaction& tx = (*iter)->second.GetTx();
412
131
        for (const auto& input : tx.vin) {
413
131
            if (auto parent_it{m_entries_by_txid.find(input.prevout.hash)}; parent_it != m_entries_by_txid.end()) {
414
30
                if (!has_been_processed.contains(input.prevout.hash)) {
415
24
                    to_process.insert(parent_it);
416
24
                }
417
30
                ancestors.insert(parent_it);
418
30
            }
419
131
        }
420
116
        has_been_processed.insert(tx.GetHash());
421
116
        to_process.erase(iter);
422
116
    }
423
9.35k
    const auto ancestor_package_size = std::accumulate(ancestors.cbegin(), ancestors.cend(), int64_t{0},
424
9.35k
        [](int64_t sum, const auto it) {return sum + it->second.GetTxSize();});
425
9.35k
    const auto ancestor_package_fee = std::accumulate(ancestors.cbegin(), ancestors.cend(), CAmount{0},
426
9.35k
        [](CAmount sum, const auto it) {return sum + it->second.GetModifiedFee();});
427
9.35k
    return target_feerate.GetFee(ancestor_package_size) - ancestor_package_fee;
428
9.35k
}
429
} // namespace node