Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H6#define BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H8#include <primitives/transaction.h>9#include <util/hasher.h>11#include <cstddef>12#include <cstdint>13#include <list>14#include <unordered_map>15#include <vector>17/** Maximum bytes for transactions to store for processing during reorg */18inline constexpr unsigned int MAX_DISCONNECTED_TX_POOL_BYTES{20'000'000};19/**20* DisconnectedBlockTransactions22* During the reorg, it's desirable to re-add previously confirmed transactions23* to the mempool, so that anything not re-confirmed in the new chain is24* available to be mined. However, it's more efficient to wait until the reorg25* is complete and process all still-unconfirmed transactions at that time,26* since we expect most confirmed transactions to (typically) still be27* confirmed in the new chain, and re-accepting to the memory pool is expensive28* (and therefore better to not do in the middle of reorg-processing).29* Instead, store the disconnected transactions (in order!) as we go, remove any30* that are included in blocks in the new chain, and then process the remaining31* still-unconfirmed transactions at the end.32*33* Order of queuedTx:34* The front of the list should be the most recently-confirmed transactions (transactions at the35* end of vtx of blocks closer to the tip). If memory usage grows too large, we trim from the front36* of the list. After trimming, transactions can be re-added to the mempool from the back of the37* list to the front without running into missing inputs.38*/39class DisconnectedBlockTransactions {40private:41/** Cached dynamic memory usage for the `CTransactionRef`s */42uint64_t cachedInnerUsage = 0;43const size_t m_max_mem_usage;44std::list<CTransactionRef> queuedTx;45using TxList = decltype(queuedTx);46std::unordered_map<Txid, TxList::iterator, SaltedTxidHasher> iters_by_txid;48/** Trim the earliest-added entries until we are within memory bounds. */49std::vector<CTransactionRef> LimitMemoryUsage();51public:52DisconnectedBlockTransactions(size_t max_mem_usage)53: m_max_mem_usage{max_mem_usage} {}55~DisconnectedBlockTransactions();57size_t DynamicMemoryUsage() const;59/** Add transactions from the block, iterating through vtx in reverse order. Callers should call60* this function for blocks in descending order by block height.61* We assume that callers never pass multiple transactions with the same txid, otherwise things62* can go very wrong in removeForBlock due to queuedTx containing an item without a63* corresponding entry in iters_by_txid.64* @returns vector of transactions that were evicted for size-limiting.65*/66[[nodiscard]] std::vector<CTransactionRef> AddTransactionsFromBlock(const std::vector<CTransactionRef>& vtx);68/** Remove any entries that are in this block. */69void removeForBlock(const std::vector<CTransactionRef>& vtx);71size_t size() const { return queuedTx.size(); }73void clear();75/** Clear all data structures and return the list of transactions. */76std::list<CTransactionRef> take();77};78#endif // BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H