Bitcoin Core  27.99.0
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
8 #include <consensus/amount.h>
10 #include <uint256.h>
12 #include <map>
13 #include <memory>
14 #include <optional>
15 #include <set>
16 #include <stdint.h>
17 #include <vector>
19 class CFeeRate;
20 class CTxMemPool;
22 namespace node {
24 // Container for tracking updates to ancestor feerate as we include ancestors in the "block"
26 {
28  const int64_t vsize_individual;
33 // This class must be constructed while holding mempool.cs. After construction, the object's
34 // methods can be called without holding that lock.
36 public:
37  explicit MiniMinerMempoolEntry(const CTransactionRef& tx_in,
38  int64_t vsize_self,
39  int64_t vsize_ancestor,
40  CAmount fee_self,
41  CAmount fee_ancestor):
42  tx{tx_in},
43  vsize_individual{vsize_self},
44  vsize_with_ancestors{vsize_ancestor},
45  fee_individual{fee_self},
46  fee_with_ancestors{fee_ancestor}
47  { }
51  int64_t GetTxSize() const { return vsize_individual; }
52  int64_t GetSizeWithAncestors() const { return vsize_with_ancestors; }
53  const CTransaction& GetTx() const LIFETIMEBOUND { return *tx; }
54  void UpdateAncestorState(int64_t vsize_change, CAmount fee_change) {
55  vsize_with_ancestors += vsize_change;
56  fee_with_ancestors += fee_change;
57  }
58 };
60 // Comparator needed for std::set<MockEntryMap::iterator>
62 {
63  template<typename I>
64  bool operator()(const I& a, const I& b) const
65  {
66  return a->first < b->first;
67  }
68 };
78 class MiniMiner
79 {
80  // When true, a caller may use CalculateBumpFees(). Becomes false if we failed to retrieve
81  // mempool entries (i.e. cluster size too large) or bump fees have already been calculated.
84  // Set once per lifetime, fill in during initialization.
85  // txids of to-be-replaced transactions
86  std::set<uint256> m_to_be_replaced;
88  // If multiple argument outpoints correspond to the same transaction, cache them together in
89  // a single entry indexed by txid. Then we can just work with txids since all outpoints from
90  // the same tx will have the same bumpfee. Excludes non-mempool transactions.
91  std::map<uint256, std::vector<COutPoint>> m_requested_outpoints_by_txid;
93  // Txid to a number representing the order in which this transaction was included (smaller
94  // number = included earlier). Transactions included in an ancestor set together have the same
95  // sequence number.
96  std::map<Txid, uint32_t> m_inclusion_order;
97  // What we're trying to calculate. Outpoint to the fee needed to bring the transaction to the target feerate.
98  std::map<COutPoint, CAmount> m_bump_fees;
100  // The constructed block template
101  std::set<uint256> m_in_block;
103  // Information on the current status of the block
105  int32_t m_total_vsize{0};
108  std::map<uint256, MiniMinerMempoolEntry> m_entries_by_txid;
109  using MockEntryMap = decltype(m_entries_by_txid);
112  std::vector<MockEntryMap::iterator> m_entries;
115  std::map<uint256, std::vector<MockEntryMap::iterator>> m_descendant_set_by_txid;
118  void DeleteAncestorPackage(const std::set<MockEntryMap::iterator, IteratorComparator>& ancestors);
121  void SanityCheck() const;
123 public:
125  bool IsReadyToCalculate() const { return m_ready_to_calculate; }
129  void BuildMockTemplate(std::optional<CFeeRate> target_feerate);
132  std::set<uint256> GetMockTemplateTxids() const { return m_in_block; }
138  MiniMiner(const CTxMemPool& mempool, const std::vector<COutPoint>& outpoints);
150  MiniMiner(const std::vector<MiniMinerMempoolEntry>& manual_entries,
151  const std::map<Txid, std::set<Txid>>& descendant_caches);
157  std::map<COutPoint, CAmount> CalculateBumpFees(const CFeeRate& target_feerate);
162  std::optional<CAmount> CalculateTotalBumpFees(const CFeeRate& target_feerate);
168  std::map<Txid, uint32_t> Linearize();
169 };
170 } // namespace node
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Definition: attributes.h:16
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:304
A minimal version of BlockAssembler, using the same ancestor set scoring algorithm.
Definition: mini_miner.h:79
std::map< uint256, std::vector< MockEntryMap::iterator > > m_descendant_set_by_txid
Map of txid to its descendants.
Definition: mini_miner.h:115
decltype(m_entries_by_txid) MockEntryMap
Definition: mini_miner.h:109
int32_t m_total_vsize
Definition: mini_miner.h:105
CAmount m_total_fees
Definition: mini_miner.h:104
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:315
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:321
std::map< uint256, std::vector< COutPoint > > m_requested_outpoints_by_txid
Definition: mini_miner.h:91
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:401
std::set< uint256 > m_in_block
Definition: mini_miner.h:101
std::map< Txid, uint32_t > m_inclusion_order
Definition: mini_miner.h:96
bool IsReadyToCalculate() const
Returns true if CalculateBumpFees may be called, false if not.
Definition: mini_miner.h:125
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:209
std::set< uint256 > GetMockTemplateTxids() const
Returns set of txids in the block template if one has been constructed.
Definition: mini_miner.h:132
std::map< uint256, MiniMinerMempoolEntry > m_entries_by_txid
Main data structure holding the entries, can be indexed by txid.
Definition: mini_miner.h:108
void SanityCheck() const
Perform some checks.
Definition: mini_miner.cpp:242
std::vector< MockEntryMap::iterator > m_entries
Vector of entries, can be sorted by ancestor feerate.
Definition: mini_miner.h:112
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:23
std::set< uint256 > m_to_be_replaced
Definition: mini_miner.h:86
std::map< COutPoint, CAmount > m_bump_fees
Definition: mini_miner.h:98
bool m_ready_to_calculate
Definition: mini_miner.h:82
void BuildMockTemplate(std::optional< CFeeRate > target_feerate)
Build a block template until the target feerate is hit.
Definition: mini_miner.cpp:256
Definition: mini_miner.h:26
CAmount fee_with_ancestors
Definition: mini_miner.h:31
int64_t GetSizeWithAncestors() const
Definition: mini_miner.h:52
const int64_t vsize_individual
Definition: mini_miner.h:28
const CTransaction & GetTx() const LIFETIMEBOUND
Definition: mini_miner.h:53
MiniMinerMempoolEntry(const CTransactionRef &tx_in, int64_t vsize_self, int64_t vsize_ancestor, CAmount fee_self, CAmount fee_ancestor)
Definition: mini_miner.h:37
void UpdateAncestorState(int64_t vsize_change, CAmount fee_change)
Definition: mini_miner.h:54
CAmount GetModFeesWithAncestors() const
Definition: mini_miner.h:50
int64_t GetTxSize() const
Definition: mini_miner.h:51
const CTransactionRef tx
Definition: mini_miner.h:27
int64_t vsize_with_ancestors
Definition: mini_miner.h:29
CAmount GetModifiedFee() const
Definition: mini_miner.h:49
const CAmount fee_individual
Definition: mini_miner.h:30
Definition: messages.h:20
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
bool operator()(const I &a, const I &b) const
Definition: mini_miner.h:64