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