Bitcoin Core  25.99.0
P2P Digital Currency
mini_miner.h
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 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 <txmempool.h>
9 
10 #include <memory>
11 #include <optional>
12 #include <stdint.h>
13 
14 namespace node {
15 
16 // Container for tracking updates to ancestor feerate as we include ancestors in the "block"
18 {
21  const int64_t vsize_individual;
22 
23 // This class must be constructed while holding mempool.cs. After construction, the object's
24 // methods can be called without holding that lock.
25 public:
30  tx{entry->GetSharedTx()},
31  vsize_individual(entry->GetTxSize()),
34  { }
35 
38  int64_t GetTxSize() const { return vsize_individual; }
39  int64_t GetSizeWithAncestors() const { return vsize_with_ancestors; }
40  const CTransaction& GetTx() const LIFETIMEBOUND { return *tx; }
41 };
42 
43 // Comparator needed for std::set<MockEntryMap::iterator>
45 {
46  template<typename I>
47  bool operator()(const I& a, const I& b) const
48  {
49  return &(*a) < &(*b);
50  }
51 };
52 
55 class MiniMiner
56 {
57  // When true, a caller may use CalculateBumpFees(). Becomes false if we failed to retrieve
58  // mempool entries (i.e. cluster size too large) or bump fees have already been calculated.
60 
61  // Set once per lifetime, fill in during initialization.
62  // txids of to-be-replaced transactions
63  std::set<uint256> m_to_be_replaced;
64 
65  // If multiple argument outpoints correspond to the same transaction, cache them together in
66  // a single entry indexed by txid. Then we can just work with txids since all outpoints from
67  // the same tx will have the same bumpfee. Excludes non-mempool transactions.
68  std::map<uint256, std::vector<COutPoint>> m_requested_outpoints_by_txid;
69 
70  // What we're trying to calculate.
71  std::map<COutPoint, CAmount> m_bump_fees;
72 
73  // The constructed block template
74  std::set<uint256> m_in_block;
75 
76  // Information on the current status of the block
78  int32_t m_total_vsize{0};
79 
81  std::map<uint256, MiniMinerMempoolEntry> m_entries_by_txid;
82  using MockEntryMap = decltype(m_entries_by_txid);
83 
85  std::vector<MockEntryMap::iterator> m_entries;
86 
88  std::map<uint256, std::vector<MockEntryMap::iterator>> m_descendant_set_by_txid;
89 
91  void DeleteAncestorPackage(const std::set<MockEntryMap::iterator, IteratorComparator>& ancestors);
92 
94  void SanityCheck() const;
95 
96 public:
98  bool IsReadyToCalculate() const { return m_ready_to_calculate; }
99 
101  void BuildMockTemplate(const CFeeRate& target_feerate);
102 
104  std::set<uint256> GetMockTemplateTxids() const { return m_in_block; }
105 
106  MiniMiner(const CTxMemPool& mempool, const std::vector<COutPoint>& outpoints);
107 
112  std::map<COutPoint, CAmount> CalculateBumpFees(const CFeeRate& target_feerate);
113 
117  std::optional<CAmount> CalculateTotalBumpFees(const CFeeRate& target_feerate);
118 };
119 } // namespace node
120 
121 #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 kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:316
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:408
A minimal version of BlockAssembler.
Definition: mini_miner.h:56
std::map< uint256, std::vector< MockEntryMap::iterator > > m_descendant_set_by_txid
Map of txid to its descendants.
Definition: mini_miner.h:88
decltype(m_entries_by_txid) MockEntryMap
Definition: mini_miner.h:82
int32_t m_total_vsize
Definition: mini_miner.h:78
void BuildMockTemplate(const CFeeRate &target_feerate)
Build a block template until the target feerate is hit.
Definition: mini_miner.cpp:207
CAmount m_total_fees
Definition: mini_miner.h:77
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:251
std::map< uint256, std::vector< COutPoint > > m_requested_outpoints_by_txid
Definition: mini_miner.h:68
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:331
std::set< uint256 > m_in_block
Definition: mini_miner.h:74
bool IsReadyToCalculate() const
Returns true if CalculateBumpFees may be called, false if not.
Definition: mini_miner.h:98
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:159
std::set< uint256 > GetMockTemplateTxids() const
Returns set of txids in the block template if one has been constructed.
Definition: mini_miner.h:104
std::map< uint256, MiniMinerMempoolEntry > m_entries_by_txid
Main data structure holding the entries, can be indexed by txid.
Definition: mini_miner.h:81
void SanityCheck() const
Perform some checks.
Definition: mini_miner.cpp:193
std::vector< MockEntryMap::iterator > m_entries
Vector of entries, can be sorted by ancestor feerate.
Definition: mini_miner.h:85
MiniMiner(const CTxMemPool &mempool, const std::vector< COutPoint > &outpoints)
Definition: mini_miner.cpp:20
std::set< uint256 > m_to_be_replaced
Definition: mini_miner.h:63
std::map< COutPoint, CAmount > m_bump_fees
Definition: mini_miner.h:71
bool m_ready_to_calculate
Definition: mini_miner.h:59
Definition: mini_miner.h:18
MiniMinerMempoolEntry(CTxMemPool::txiter entry)
Definition: mini_miner.h:28
CAmount fee_with_ancestors
Definition: mini_miner.h:26
int64_t GetSizeWithAncestors() const
Definition: mini_miner.h:39
const int64_t vsize_individual
Definition: mini_miner.h:21
const CTransaction & GetTx() const LIFETIMEBOUND
Definition: mini_miner.h:40
CAmount GetModFeesWithAncestors() const
Definition: mini_miner.h:37
int64_t GetTxSize() const
Definition: mini_miner.h:38
const CTransactionRef tx
Definition: mini_miner.h:20
int64_t vsize_with_ancestors
Definition: mini_miner.h:27
CAmount GetModifiedFee() const
Definition: mini_miner.h:36
const CAmount fee_individual
Definition: mini_miner.h:19
Definition: init.h:25
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
bool operator()(const I &a, const I &b) const
Definition: mini_miner.h:47