Bitcoin Core  25.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 <txmempool.h>
10 #include <memory>
11 #include <optional>
12 #include <stdint.h>
14 namespace node {
16 // Container for tracking updates to ancestor feerate as we include ancestors in the "block"
18 {
21  const int64_t vsize_individual;
25 // This class must be constructed while holding mempool.cs. After construction, the object's
26 // methods can be called without holding that lock.
28 public:
31  tx{entry->GetSharedTx()},
32  vsize_individual(entry->GetTxSize()),
35  { }
39  int64_t GetTxSize() const { return vsize_individual; }
40  int64_t GetSizeWithAncestors() const { return vsize_with_ancestors; }
41  const CTransaction& GetTx() const LIFETIMEBOUND { return *tx; }
42  void UpdateAncestorState(int64_t vsize_change, CAmount fee_change) {
43  vsize_with_ancestors += vsize_change;
44  fee_with_ancestors += fee_change;
45  }
46 };
48 // Comparator needed for std::set<MockEntryMap::iterator>
50 {
51  template<typename I>
52  bool operator()(const I& a, const I& b) const
53  {
54  return &(*a) < &(*b);
55  }
56 };
60 class MiniMiner
61 {
62  // When true, a caller may use CalculateBumpFees(). Becomes false if we failed to retrieve
63  // mempool entries (i.e. cluster size too large) or bump fees have already been calculated.
66  // Set once per lifetime, fill in during initialization.
67  // txids of to-be-replaced transactions
68  std::set<uint256> m_to_be_replaced;
70  // If multiple argument outpoints correspond to the same transaction, cache them together in
71  // a single entry indexed by txid. Then we can just work with txids since all outpoints from
72  // the same tx will have the same bumpfee. Excludes non-mempool transactions.
73  std::map<uint256, std::vector<COutPoint>> m_requested_outpoints_by_txid;
75  // What we're trying to calculate.
76  std::map<COutPoint, CAmount> m_bump_fees;
78  // The constructed block template
79  std::set<uint256> m_in_block;
81  // Information on the current status of the block
83  int32_t m_total_vsize{0};
86  std::map<uint256, MiniMinerMempoolEntry> m_entries_by_txid;
87  using MockEntryMap = decltype(m_entries_by_txid);
90  std::vector<MockEntryMap::iterator> m_entries;
93  std::map<uint256, std::vector<MockEntryMap::iterator>> m_descendant_set_by_txid;
96  void DeleteAncestorPackage(const std::set<MockEntryMap::iterator, IteratorComparator>& ancestors);
99  void SanityCheck() const;
101 public:
103  bool IsReadyToCalculate() const { return m_ready_to_calculate; }
106  void BuildMockTemplate(const CFeeRate& target_feerate);
109  std::set<uint256> GetMockTemplateTxids() const { return m_in_block; }
111  MiniMiner(const CTxMemPool& mempool, const std::vector<COutPoint>& outpoints);
117  std::map<COutPoint, CAmount> CalculateBumpFees(const CFeeRate& target_feerate);
122  std::optional<CAmount> CalculateTotalBumpFees(const CFeeRate& target_feerate);
123 };
124 } // 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:295
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:302
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:394
A minimal version of BlockAssembler.
Definition: mini_miner.h:61
std::map< uint256, std::vector< MockEntryMap::iterator > > m_descendant_set_by_txid
Map of txid to its descendants.
Definition: mini_miner.h:93
decltype(m_entries_by_txid) MockEntryMap
Definition: mini_miner.h:87
int32_t m_total_vsize
Definition: mini_miner.h:83
void BuildMockTemplate(const CFeeRate &target_feerate)
Build a block template until the target feerate is hit.
Definition: mini_miner.cpp:204
CAmount m_total_fees
Definition: mini_miner.h:82
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:248
std::map< uint256, std::vector< COutPoint > > m_requested_outpoints_by_txid
Definition: mini_miner.h:73
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:328
std::set< uint256 > m_in_block
Definition: mini_miner.h:79
bool IsReadyToCalculate() const
Returns true if CalculateBumpFees may be called, false if not.
Definition: mini_miner.h:103
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:157
std::set< uint256 > GetMockTemplateTxids() const
Returns set of txids in the block template if one has been constructed.
Definition: mini_miner.h:109
std::map< uint256, MiniMinerMempoolEntry > m_entries_by_txid
Main data structure holding the entries, can be indexed by txid.
Definition: mini_miner.h:86
void SanityCheck() const
Perform some checks.
Definition: mini_miner.cpp:190
std::vector< MockEntryMap::iterator > m_entries
Vector of entries, can be sorted by ancestor feerate.
Definition: mini_miner.h:90
MiniMiner(const CTxMemPool &mempool, const std::vector< COutPoint > &outpoints)
Definition: mini_miner.cpp:18
std::set< uint256 > m_to_be_replaced
Definition: mini_miner.h:68
std::map< COutPoint, CAmount > m_bump_fees
Definition: mini_miner.h:76
bool m_ready_to_calculate
Definition: mini_miner.h:64
Definition: mini_miner.h:18
MiniMinerMempoolEntry(CTxMemPool::txiter entry)
Definition: mini_miner.h:29
CAmount fee_with_ancestors
Definition: mini_miner.h:22
int64_t GetSizeWithAncestors() const
Definition: mini_miner.h:40
const int64_t vsize_individual
Definition: mini_miner.h:21
const CTransaction & GetTx() const LIFETIMEBOUND
Definition: mini_miner.h:41
void UpdateAncestorState(int64_t vsize_change, CAmount fee_change)
Definition: mini_miner.h:42
CAmount GetModFeesWithAncestors() const
Definition: mini_miner.h:38
int64_t GetTxSize() const
Definition: mini_miner.h:39
const CTransactionRef tx
Definition: mini_miner.h:20
int64_t vsize_with_ancestors
Definition: mini_miner.h:23
CAmount GetModifiedFee() const
Definition: mini_miner.h:37
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:52