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
23 changes: 23 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 @@ -138,13 +139,17 @@ using node::ChainstateLoadStatus;
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 +344,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, pos] = node.peerman->GetExtraPoolForDump();
DumpExtraPool(pool, pos, 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 @@ -2159,6 +2170,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_pos, extra_pool_memusage);
}
}

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

if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
Expand Down
20 changes: 20 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::pair<std::vector<CTransactionRef>, size_t> GetExtraPoolForDump() const override EXCLUSIVE_LOCKS_REQUIRED(!g_msgproc_mutex);
void SetExtraPool(std::vector<CTransactionRef> pool, size_t pos, 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,24 @@ int PeerManagerImpl::GetNumberOfPeersWithValidatedDownloads() const
return m_peers_downloading_from;
}

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

void PeerManagerImpl::SetExtraPool(std::vector<CTransactionRef> pool, size_t pos, size_t memusage)
{
LOCK(g_msgproc_mutex);
vExtraTxnForCompact.assign(m_opts.max_extra_txs, nullptr);
for (size_t i = 0; i < pool.size() && i < m_opts.max_extra_txs; ++i) {
vExtraTxnForCompact[i] = std::move(pool[i]);
}
vExtraTxnForCompactIt = std::min(pos, 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
7 changes: 7 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,12 @@ 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) and current ring buffer position for persistence. */
virtual std::pair<std::vector<CTransactionRef>, size_t> GetExtraPoolForDump() const = 0;

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

#endif // BITCOIN_NET_PROCESSING_H
185 changes: 185 additions & 0 deletions src/node/extrapool_persist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Copyright (c) 2024 The Bitcoin Core 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 <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 {

bool ShouldPersistExtraPool(const ArgsManager& argsman)
{
return argsman.GetBoolArg("-rejecttokens", false);
}

fs::path ExtraPoolPath(const ArgsManager& argsman)
{
return argsman.GetDataDirNet() / "extrapool.dat";
}

bool DumpExtraPool(const std::vector<CTransactionRef>& pool,
size_t pool_pos,
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, ring buffer position
const uint64_t version{1};
file << version;
file << count;
file << static_cast<uint64_t>(pool_pos);

// 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;
}

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;
}

uint64_t position;
file >> position;

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

for (size_t i = 0; i < to_load; ++i) {
try {
CTransactionRef tx;
file >> TX_WITH_WITNESS(tx);
pool[i] = std::move(tx);
memusage += RecursiveDynamicUsage(*pool[i]);
} catch (const std::exception&) {
LogWarning("Extra pool deserialization failed at transaction %d. Keeping %d already loaded.\n", i, i);
pool.resize(i);
break;
}
}

// Clamp position to loaded count
pool_pos = std::min(static_cast<size_t>(position), pool.size());

// 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
43 changes: 43 additions & 0 deletions src/node/extrapool_persist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2024 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_NODE_EXTRAPOOL_PERSIST_H
#define BITCOIN_NODE_EXTRAPOOL_PERSIST_H

#include <primitives/transaction.h>
#include <util/fs.h>

#include <cstddef>
#include <vector>

class ArgsManager;

namespace node {

// Returns true if extra pool persistence is enabled (rejecttokens=1)
bool ShouldPersistExtraPool(const ArgsManager& argsman);

// Returns path: GetDataDirNet() / "extrapool.dat"
fs::path ExtraPoolPath(const ArgsManager& argsman);

// Serialize vExtraTxnForCompact to disk. Called from Shutdown().
// Returns true on success, false on failure (logged, non-fatal).
bool DumpExtraPool(const std::vector<CTransactionRef>& pool,
size_t pool_pos,
const fs::path& dump_path,
fsbridge::FopenFn mockable_fopen_function = fsbridge::fopen);

// Deserialize extrapool.dat into the provided vector and position.
// Applies count and memory limits. Returns true on success.
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,
fsbridge::FopenFn mockable_fopen_function = fsbridge::fopen);

} // namespace node

#endif // BITCOIN_NODE_EXTRAPOOL_PERSIST_H
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ add_executable(test_bitcoin
denialofservice_tests.cpp
descriptor_tests.cpp
disconnected_transactions.cpp
extrapool_persist_tests.cpp
feefrac_tests.cpp
flatfile_tests.cpp
fs_tests.cpp
Expand Down
Loading