Bitcoin Core 31.99.0
P2P Digital Currency
mini_miner.h
Go to the documentation of this file.
1// Copyright (c) 2022-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_NODE_MINI_MINER_H
6#define BITCOIN_NODE_MINI_MINER_H
7
8#include <attributes.h>
9#include <consensus/amount.h>
11
12#include <cstdint>
13#include <map>
14#include <optional>
15#include <set>
16#include <vector>
17
18class CFeeRate;
19class CTxMemPool;
20
21namespace node {
22
23// Container for tracking updates to ancestor feerate as we include ancestors in the "block"
25{
27 const int64_t vsize_individual;
31
32// This class must be constructed while holding mempool.cs. After construction, the object's
33// methods can be called without holding that lock.
34
35public:
37 int64_t vsize_self,
38 int64_t vsize_ancestor,
39 CAmount fee_self,
40 CAmount fee_ancestor):
41 tx{tx_in},
42 vsize_individual{vsize_self},
43 vsize_with_ancestors{vsize_ancestor},
44 fee_individual{fee_self},
45 fee_with_ancestors{fee_ancestor}
46 { }
47
50 int64_t GetTxSize() const { return vsize_individual; }
51 int64_t GetSizeWithAncestors() const { return vsize_with_ancestors; }
52 const CTransaction& GetTx() const LIFETIMEBOUND { return *tx; }
53 void UpdateAncestorState(int64_t vsize_change, CAmount fee_change) {
54 vsize_with_ancestors += vsize_change;
55 fee_with_ancestors += fee_change;
56 }
57};
58
59// Comparator needed for std::set<MockEntryMap::iterator>
61{
62 template<typename I>
63 bool operator()(const I& a, const I& b) const
64 {
65 return a->first < b->first;
66 }
67};
68
78{
79 // When true, a caller may use CalculateBumpFees(). Becomes false if we failed to retrieve
80 // mempool entries (i.e. cluster size too large) or bump fees have already been calculated.
82
83 // Set once per lifetime, fill in during initialization.
84 // txids of to-be-replaced transactions
85 std::set<Txid> m_to_be_replaced;
86
87 // If multiple argument outpoints correspond to the same transaction, cache them together in
88 // a single entry indexed by txid. Then we can just work with txids since all outpoints from
89 // the same tx will have the same bumpfee. Excludes non-mempool transactions.
90 std::map<Txid, std::vector<COutPoint>> m_requested_outpoints_by_txid;
91
92 // Txid to a number representing the order in which this transaction was included (smaller
93 // number = included earlier). Transactions included in an ancestor set together have the same
94 // sequence number.
95 std::map<Txid, uint32_t> m_inclusion_order;
96 // What we're trying to calculate. Outpoint to the fee needed to bring the transaction to the target feerate.
97 std::map<COutPoint, CAmount> m_bump_fees;
98
99 // The constructed block template
100 std::set<Txid> m_in_block;
101
102 // Information on the current status of the block
104 int32_t m_total_vsize{0};
105
107 std::map<Txid, MiniMinerMempoolEntry> m_entries_by_txid;
109
111 std::vector<MockEntryMap::iterator> m_entries;
112
114 std::map<Txid, std::vector<MockEntryMap::iterator>> m_descendant_set_by_txid;
115
117 void DeleteAncestorPackage(const std::set<MockEntryMap::iterator, IteratorComparator>& ancestors);
118
120 void SanityCheck() const;
121
122public:
125
128 void BuildMockTemplate(std::optional<CFeeRate> target_feerate);
129
131 std::set<Txid> GetMockTemplateTxids() const { return m_in_block; }
132
137 MiniMiner(const CTxMemPool& mempool, const std::vector<COutPoint>& outpoints);
138
149 MiniMiner(const std::vector<MiniMinerMempoolEntry>& manual_entries,
150 const std::map<Txid, std::set<Txid>>& descendant_caches);
151
156 std::map<COutPoint, CAmount> CalculateBumpFees(const CFeeRate& target_feerate);
157
161 std::optional<CAmount> CalculateTotalBumpFees(const CFeeRate& target_feerate);
162
167 std::map<Txid, uint32_t> Linearize();
168};
169} // namespace node
170
171#endif // BITCOIN_NODE_MINI_MINER_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define LIFETIMEBOUND
Definition: attributes.h:16
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.
Definition: transaction.h:281
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:187
A minimal version of BlockAssembler, using the same ancestor set scoring algorithm.
Definition: mini_miner.h:78
decltype(m_entries_by_txid) MockEntryMap
Definition: mini_miner.h:108
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...
Definition: mini_miner.cpp:303
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...
Definition: mini_miner.cpp:309
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 ...
Definition: mini_miner.cpp:389
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::set< Txid > GetMockTemplateTxids() const
Returns set of txids in the block template if one has been constructed.
Definition: mini_miner.h:131
std::map< Txid, uint32_t > m_inclusion_order
Definition: mini_miner.h:95
bool IsReadyToCalculate() const
Returns true if CalculateBumpFees may be called, false if not.
Definition: mini_miner.h:124
void DeleteAncestorPackage(const std::set< MockEntryMap::iterator, IteratorComparator > &ancestors)
Consider this ancestor package "mined" so remove all these entries from our data structures.
Definition: mini_miner.cpp:199
void SanityCheck() const
Perform some checks.
Definition: mini_miner.cpp:231
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.
Definition: mini_miner.cpp:24
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.cpp:244
Definition: mini_miner.h:25
CAmount fee_with_ancestors
Definition: mini_miner.h:30
int64_t GetSizeWithAncestors() const
Definition: mini_miner.h:51
const int64_t vsize_individual
Definition: mini_miner.h:27
const CTransaction & GetTx() const LIFETIMEBOUND
Definition: mini_miner.h:52
MiniMinerMempoolEntry(const CTransactionRef &tx_in, int64_t vsize_self, int64_t vsize_ancestor, CAmount fee_self, CAmount fee_ancestor)
Definition: mini_miner.h:36
void UpdateAncestorState(int64_t vsize_change, CAmount fee_change)
Definition: mini_miner.h:53
CAmount GetModFeesWithAncestors() const
Definition: mini_miner.h:49
int64_t GetTxSize() const
Definition: mini_miner.h:50
const CTransactionRef tx
Definition: mini_miner.h:26
int64_t vsize_with_ancestors
Definition: mini_miner.h:28
CAmount GetModifiedFee() const
Definition: mini_miner.h:48
const CAmount fee_individual
Definition: mini_miner.h:29
Definition: messages.h:21
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
bool operator()(const I &a, const I &b) const
Definition: mini_miner.h:63