Bitcoin Core  26.1.0
P2P Digital Currency
disconnected_transactions.h
Go to the documentation of this file.
1 // Copyright (c) 2023 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_KERNEL_DISCONNECTED_TRANSACTIONS_H
6 #define BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H
7 
8 #include <core_memusage.h>
9 #include <memusage.h>
10 #include <primitives/transaction.h>
11 #include <util/hasher.h>
12 
13 #include <list>
14 #include <unordered_map>
15 #include <vector>
16 
18 static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20'000;
39 class DisconnectedBlockTransactions {
40 private:
43  uint64_t cachedInnerUsage = 0;
44  const size_t m_max_mem_usage;
45  std::list<CTransactionRef> queuedTx;
46  using TxList = decltype(queuedTx);
47  std::unordered_map<uint256, TxList::iterator, SaltedTxidHasher> iters_by_txid;
48 
50  std::vector<CTransactionRef> LimitMemoryUsage()
51  {
52  std::vector<CTransactionRef> evicted;
53 
54  while (!queuedTx.empty() && DynamicMemoryUsage() > m_max_mem_usage) {
55  evicted.emplace_back(queuedTx.front());
56  cachedInnerUsage -= RecursiveDynamicUsage(*queuedTx.front());
57  iters_by_txid.erase(queuedTx.front()->GetHash());
58  queuedTx.pop_front();
59  }
60  return evicted;
61  }
62 
63 public:
64  DisconnectedBlockTransactions(size_t max_mem_usage) : m_max_mem_usage{max_mem_usage} {}
65 
66  // It's almost certainly a logic bug if we don't clear out queuedTx before
67  // destruction, as we add to it while disconnecting blocks, and then we
68  // need to re-process remaining transactions to ensure mempool consistency.
69  // For now, assert() that we've emptied out this object on destruction.
70  // This assert() can always be removed if the reorg-processing code were
71  // to be refactored such that this assumption is no longer true (for
72  // instance if there was some other way we cleaned up the mempool after a
73  // reorg, besides draining this object).
75  assert(queuedTx.empty());
76  assert(iters_by_txid.empty());
77  assert(cachedInnerUsage == 0);
78  }
79 
80  size_t DynamicMemoryUsage() const {
81  return cachedInnerUsage + memusage::DynamicUsage(iters_by_txid) + memusage::DynamicUsage(queuedTx);
82  }
83 
91  [[nodiscard]] std::vector<CTransactionRef> AddTransactionsFromBlock(const std::vector<CTransactionRef>& vtx)
92  {
93  iters_by_txid.reserve(iters_by_txid.size() + vtx.size());
94  for (auto block_it = vtx.rbegin(); block_it != vtx.rend(); ++block_it) {
95  auto it = queuedTx.insert(queuedTx.end(), *block_it);
96  iters_by_txid.emplace((*block_it)->GetHash(), it);
97  cachedInnerUsage += RecursiveDynamicUsage(**block_it);
98  }
99  return LimitMemoryUsage();
100  }
101 
103  void removeForBlock(const std::vector<CTransactionRef>& vtx)
104  {
105  // Short-circuit in the common case of a block being added to the tip
106  if (queuedTx.empty()) {
107  return;
108  }
109  for (const auto& tx : vtx) {
110  auto iter = iters_by_txid.find(tx->GetHash());
111  if (iter != iters_by_txid.end()) {
112  auto list_iter = iter->second;
113  iters_by_txid.erase(iter);
114  cachedInnerUsage -= RecursiveDynamicUsage(**list_iter);
115  queuedTx.erase(list_iter);
116  }
117  }
118  }
119 
120  size_t size() const { return queuedTx.size(); }
121 
122  void clear()
123  {
124  cachedInnerUsage = 0;
125  iters_by_txid.clear();
126  queuedTx.clear();
127  }
128 
130  std::list<CTransactionRef> take()
131  {
132  std::list<CTransactionRef> ret = std::move(queuedTx);
133  clear();
134  return ret;
135  }
136 };
137 #endif // BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H
int ret
assert(!tx.IsCoinBase())
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:30
std::list< CTransactionRef > take()
Clear all data structures and return the list of transactions.
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Remove any entries that are in this block.
std::vector< CTransactionRef > AddTransactionsFromBlock(const std::vector< CTransactionRef > &vtx)
Add transactions from the block, iterating through vtx in reverse order.
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE
Maximum kilobytes for transactions to store for processing during reorg.