Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
node/context.cpp
node/database_args.cpp
node/eviction.cpp
node/extrapool_persist.cpp
node/interface_ui.cpp
node/interfaces.cpp
node/kernel_notifications.cpp
Expand Down
25 changes: 25 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <node/chainstate.h>
#include <node/chainstatemanager_args.h>
#include <node/context.h>
#include <node/extrapool_persist.h>
#include <node/interface_ui.h>
#include <node/kernel_notifications.h>
#include <node/mempool_args.h>
Expand Down Expand Up @@ -135,16 +136,21 @@ using node::BlockManager;
using node::CalculateCacheSizes;
using node::ChainstateLoadResult;
using node::ChainstateLoadStatus;
using node::DEFAULT_PERSIST_EXTRA_POOL;
using node::DEFAULT_PERSIST_MEMPOOL;
using node::DEFAULT_PRINT_MODIFIED_FEE;
using node::DEFAULT_STOPATHEIGHT;
using node::DumpExtraPool;
using node::DumpMempool;
using node::ExtraPoolPath;
using node::ImportBlocks;
using node::KernelNotifications;
using node::LoadChainstate;
using node::LoadExtraPool;
using node::LoadMempool;
using node::MempoolPath;
using node::NodeContext;
using node::ShouldPersistExtraPool;
using node::ShouldPersistMempool;
using node::VerifyLoadedChainstate;
using util::Join;
Expand Down Expand Up @@ -339,6 +345,12 @@ void Shutdown(NodeContext& node)
// as this would prevent the shutdown from completing.
if (node.scheduler) node.scheduler->stop();

// Dump extra pool to disk for compact block reconstruction on next startup.
if (node.peerman && ShouldPersistExtraPool(*node.args)) {
auto pool = node.peerman->GetExtraPoolForDump();
DumpExtraPool(pool, ExtraPoolPath(*node.args));
}

// After the threads that potentially access these pointers have been stopped,
// destruct and reset all to nullptr.
node.peerman.reset();
Expand Down Expand Up @@ -509,6 +521,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
strprintf("Upper limit of memory usage (in megabytes) for keeping extra transactions in memory for compact block reconstructions (default: %s)",
DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN_SIZE / 1000000),
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-persistextrapool", strprintf("Persist extra transactions to disk for compact block reconstruction (default: %u)", DEFAULT_PERSIST_EXTRA_POOL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-blocksonly", strprintf("Whether to reject transactions from network peers. Disables automatic broadcast and rebroadcast of transactions, unless the source peer has the 'forcerelay' permission. RPC transactions are not affected. (default: %u)", DEFAULT_BLOCKSONLY), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-coinstatsindex", strprintf("Maintain coinstats index used by the gettxoutsetinfo RPC (default: %u)", DEFAULT_COINSTATSINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-conf=<file>", strprintf("Specify path to read-only configuration file. Relative paths will be prefixed by datadir location (only useable from command line, not configuration file) (default: %s)", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
Expand Down Expand Up @@ -2159,6 +2172,18 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
peerman_opts);
validation_signals.RegisterValidationInterface(node.peerman.get());

// Load extra pool from disk for compact block reconstruction.
if (ShouldPersistExtraPool(args)) {
std::vector<CTransactionRef> extra_pool;
size_t extra_pool_pos = 0, extra_pool_memusage = 0;
LoadExtraPool(extra_pool, extra_pool_pos, extra_pool_memusage,
peerman_opts.max_extra_txs, peerman_opts.max_extra_txs_size,
ExtraPoolPath(args));
if (!extra_pool.empty()) {
node.peerman->SetExtraPool(std::move(extra_pool), extra_pool_memusage);
}
}

// ********************************************************* Step 8: start indexers

if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
Expand Down
21 changes: 21 additions & 0 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ class PeerManagerImpl final : public PeerManager
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const override;
int GetNumberOfPeersWithValidatedDownloads() const override EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
std::vector<CTransactionRef> GetExtraPoolForDump() const override EXCLUSIVE_LOCKS_REQUIRED(!g_msgproc_mutex);
void SetExtraPool(std::vector<CTransactionRef> pool, size_t memusage) override EXCLUSIVE_LOCKS_REQUIRED(!g_msgproc_mutex);

private:
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
Expand Down Expand Up @@ -1699,6 +1701,25 @@ int PeerManagerImpl::GetNumberOfPeersWithValidatedDownloads() const
return m_peers_downloading_from;
}

std::vector<CTransactionRef> PeerManagerImpl::GetExtraPoolForDump() const
{
LOCK(g_msgproc_mutex);
return vExtraTxnForCompact;
}

void PeerManagerImpl::SetExtraPool(std::vector<CTransactionRef> pool, size_t memusage)
{
LOCK(g_msgproc_mutex);
vExtraTxnForCompact.assign(m_opts.max_extra_txs, nullptr);
Comment thread
pdath marked this conversation as resolved.
for (size_t i = 0; i < pool.size() && i < m_opts.max_extra_txs; ++i) {
vExtraTxnForCompact[i] = std::move(pool[i]);
}
// Derive position from pool size (next insertion position is at the end of loaded data)
vExtraTxnForCompactIt = std::min(pool.size(), static_cast<size_t>(m_opts.max_extra_txs));
if (vExtraTxnForCompactIt >= m_opts.max_extra_txs) vExtraTxnForCompactIt = 0;
blockreconstructionextratxn_memusage = memusage;
}

bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const
{
{
Expand Down
8 changes: 8 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <validationinterface.h>

#include <chrono>
#include <utility>

class AddrMan;
class CChainParams;
Expand Down Expand Up @@ -165,6 +166,13 @@ class PeerManager : public CValidationInterface, public NetEventsInterface

/** Get number of peers from which we're downloading blocks */
virtual int GetNumberOfPeersWithValidatedDownloads() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main) = 0;

/** Get a copy of the extra pool (vExtraTxnForCompact) for persistence. */
virtual std::vector<CTransactionRef> GetExtraPoolForDump() const = 0;

/** Set the extra pool contents and memory usage (used on startup to restore from disk).
* Position is derived from pool size. */
virtual void SetExtraPool(std::vector<CTransactionRef> pool, size_t memusage) = 0;
};

#endif // BITCOIN_NET_PROCESSING_H
238 changes: 238 additions & 0 deletions src/node/extrapool_persist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
// Copyright (c) 2026 The Bitcoin Knots developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <node/extrapool_persist.h>

#include <common/args.h>
#include <core_memusage.h>
#include <logging.h>
#include <net_processing.h>
#include <primitives/transaction.h>
#include <serialize.h>
#include <streams.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/syserror.h>
#include <util/time.h>

#include <algorithm>
#include <cstdint>
#include <exception>
#include <stdexcept>
#include <vector>

using fsbridge::FopenFn;

namespace node {

/**
* Returns true if extra pool persistence is enabled.
* Extra pool is persisted when the -persistextrapool option is set.
*
* @param argsman The ArgsManager instance containing command line arguments
* @return true if -persistextrapool=1, false otherwise
*/
bool ShouldPersistExtraPool(const ArgsManager& argsman)
{
return argsman.GetBoolArg("-persistextrapool", DEFAULT_PERSIST_EXTRA_POOL);
}

/**
* Returns the path to the extra pool persistence file.
* The file is stored in the data directory as "extrapool.dat".
*
* @param argsman The ArgsManager instance containing command line arguments
* @return The filesystem path to the extra pool file
*/
fs::path ExtraPoolPath(const ArgsManager& argsman)
{
return argsman.GetDataDirNet() / "extrapool.dat";
}

/**
* Serialize extra pool transactions to disk.
* Writes version, count, and all non-null transactions to the file.
* Uses atomic write (write to .new file, then rename) for crash safety.
*
* @param pool The vector of transaction references to serialize
* @param dump_path The filesystem path to write the extra pool file to
* @param mockable_fopen_function Function pointer for opening files (for testing)
* @return true on success, false on failure (non-fatal, logged)
*/
bool DumpExtraPool(const std::vector<CTransactionRef>& pool,
const fs::path& dump_path,
FopenFn mockable_fopen_function)
{
auto start = SteadyClock::now();

// Count non-null entries
uint64_t count = 0;
for (const auto& tx : pool) {
if (tx != nullptr) ++count;
}

AutoFile file{mockable_fopen_function(dump_path + ".new", "wb")};
if (file.IsNull()) {
LogInfo("Failed to open extra pool file for writing: %s. Continuing anyway.\n", fs::PathToString(dump_path));
return false;
}

try {
// Write header: version, count
// pool_pos is not persisted; on load it will be derived from count
const uint64_t version{1};
file << version;
file << count;

// Serialize each non-null transaction
for (const auto& tx : pool) {
if (tx != nullptr) {
file << TX_WITH_WITNESS(*tx);
}
}

if (!file.Commit()) {
throw std::runtime_error("Commit failed");
}
if (file.fclose() != 0) {
throw std::runtime_error(
strprintf("Error closing %s: %s", fs::PathToString(dump_path + ".new"), SysErrorString(errno)));
}
if (!RenameOver(dump_path + ".new", dump_path)) {
throw std::runtime_error("Rename failed");
}

auto last = SteadyClock::now();
LogInfo("Dumped extra pool: %d transactions written in %.3fs\n",
count, Ticks<SecondsDouble>(last - start));
} catch (const std::exception& e) {
LogInfo("Failed to dump extra pool: %s. Continuing anyway.\n", e.what());
(void)file.fclose();
return false;
}
return true;
}

/**
* Deserialize extra pool transactions from disk.
* Reads version and count from file header, then deserializes each transaction.
* Enforces per-TX size limit (BLOCK_RECONSTRUCTION_EXTRA_TXN_PER_TXN_SIZE_LIMIT)
* and cumulative memory limit (max_mem_bytes) during loading.
* Derives pool_pos from count to maintain ring buffer invariant.
*
* @param[out] pool The vector to populate with deserialized transaction references
* @param[out] pool_pos The ring buffer position (derived from count, clamped to max_count)
* @param[out] memusage The total memory usage of loaded transactions
* @param max_count Maximum number of transactions to load
* @param max_mem_bytes Maximum memory usage allowed for loaded transactions
* @param load_path The filesystem path to read the extra pool file from
* @param mockable_fopen_function Function pointer for opening files (for testing)
* @return true on success (even if file doesn't exist - returns empty pool),
* false only on unrecoverable errors (currently always returns true)
*/
bool LoadExtraPool(std::vector<CTransactionRef>& pool,
size_t& pool_pos,
size_t& memusage,
size_t max_count,
size_t max_mem_bytes,
const fs::path& load_path,
FopenFn mockable_fopen_function)
{
auto start = SteadyClock::now();

AutoFile file{mockable_fopen_function(load_path, "rb")};
if (file.IsNull()) {
LogInfo("No extra pool file found at %s. Starting with empty pool.\n", fs::PathToString(load_path));
pool.clear();
pool_pos = 0;
memusage = 0;
return true;
}

try {
uint64_t version;
file >> version;
if (version != 1) {
LogWarning("Extra pool file has unrecognized version %d. Starting with empty pool.\n", version);
pool.clear();
pool_pos = 0;
memusage = 0;
return true;
}

uint64_t count;
file >> count;
if (count > std::max(static_cast<uint64_t>(max_count), uint64_t{1000000})) {
LogWarning("Extra pool file claims %d transactions (likely corrupt). Starting with empty pool.\n", count);
pool.clear();
pool_pos = 0;
memusage = 0;
return true;
}

size_t to_load = std::min(static_cast<size_t>(count), max_count);
pool.clear();
memusage = 0;

for (size_t i = 0; i < to_load; ++i) {
try {
CTransactionRef tx;
file >> TX_WITH_WITNESS(tx);

// Check per-TX size limit (match live insert behavior)
size_t tx_usage = RecursiveDynamicUsage(*tx);
if (tx_usage > BLOCK_RECONSTRUCTION_EXTRA_TXN_PER_TXN_SIZE_LIMIT) {
LogWarning("Skipping oversized extra pool transaction (%d bytes) at index %d\n", tx_usage, i);
continue;
}

// Check cumulative memory before adding
if (memusage + tx_usage > max_mem_bytes && !pool.empty()) {
LogDebug(BCLog::NET, "Extra pool memory limit reached, stopping load at %d transactions\n", i);
break;
}

pool.push_back(std::move(tx));
memusage += tx_usage;
} catch (const std::exception&) {
LogWarning("Extra pool deserialization failed at transaction %d. Keeping %d already loaded.\n", i, i);
break;
}
}

// Derive pool_pos from count (next insertion position is at the end of loaded data)
// This maintains ring buffer invariant: TXs at [0, pool.size()), next write at pool.size() % max_count
// Guard against zero-capacity case (max_count == 0) to prevent division by zero
pool_pos = max_count > 0 ? pool.size() % max_count : 0;

// Memory eviction: if over limit, evict from position forward using ring buffer pattern
if (memusage > max_mem_bytes && !pool.empty()) {
size_t safety_counter = 0;
const size_t pool_size = pool.size();
while (memusage > max_mem_bytes && safety_counter < pool_size) {
size_t evict_pos = pool_pos % pool_size;
if (pool[evict_pos] != nullptr) {
memusage -= RecursiveDynamicUsage(*pool[evict_pos]);
pool[evict_pos].reset();
}
pool_pos = (pool_pos + 1) % pool_size;
++safety_counter;
}
}

auto last = SteadyClock::now();
LogInfo("Imported %d extra pool transactions from file in %.3fs\n",
pool.size(), Ticks<SecondsDouble>(last - start));
} catch (const std::exception& e) {
LogInfo("Failed to deserialize extra pool: %s. Starting with empty pool.\n", e.what());
pool.clear();
pool_pos = 0;
memusage = 0;
return true;
}

return true;
}

} // namespace node
Loading