refactor, blockstorage: Replace blocksdir arg

Add a blocks_dir field to the BlockManager options. Move functions
relying on the global gArgs to get the blocks_dir into the BlockManager
class.

This should eventually allow users of the BlockManager to not rely on
the global Args and instead pass in their own options.
This commit is contained in:
TheCharlatan 2023-02-18 18:17:33 +01:00
parent 02a0899527
commit 18e5ba7c80
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
7 changed files with 22 additions and 11 deletions

View file

@ -89,6 +89,7 @@ int main(int argc, char* argv[])
};
const node::BlockManager::Options blockman_opts{
.chainparams = chainman_opts.chainparams,
.blocks_dir = gArgs.GetBlocksDirPath(),
};
ChainstateManager chainman{chainman_opts, blockman_opts};

View file

@ -116,18 +116,19 @@ using kernel::DumpMempool;
using kernel::ValidationCacheSizes;
using node::ApplyArgsManOptions;
using node::BlockManager;
using node::CacheSizes;
using node::CalculateCacheSizes;
using node::DEFAULT_PERSIST_MEMPOOL;
using node::DEFAULT_PRINTPRIORITY;
using node::DEFAULT_STOPAFTERBLOCKIMPORT;
using node::fReindex;
using node::LoadChainstate;
using node::MempoolPath;
using node::ShouldPersistMempool;
using node::NodeContext;
using node::ShouldPersistMempool;
using node::ThreadImport;
using node::VerifyLoadedChainstate;
using node::fReindex;
static constexpr bool DEFAULT_PROXYRANDOMIZE{true};
static constexpr bool DEFAULT_REST_ENABLE{false};
@ -1037,8 +1038,9 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
return InitError(*error);
}
node::BlockManager::Options blockman_opts_dummy{
BlockManager::Options blockman_opts_dummy{
.chainparams = chainman_opts_dummy.chainparams,
.blocks_dir = args.GetBlocksDirPath(),
};
if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) {
return InitError(*error);
@ -1446,8 +1448,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
};
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
node::BlockManager::Options blockman_opts{
BlockManager::Options blockman_opts{
.chainparams = chainman_opts.chainparams,
.blocks_dir = args.GetBlocksDirPath(),
};
Assert(!ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction

View file

@ -5,6 +5,8 @@
#ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
#include <util/fs.h>
#include <cstdint>
class CChainParams;
@ -19,6 +21,7 @@ struct BlockManagerOpts {
const CChainParams& chainparams;
uint64_t prune_target{0};
bool fast_prune{false};
const fs::path blocks_dir;
};
} // namespace kernel

View file

@ -427,8 +427,7 @@ void BlockManager::CleanupBlockRevFiles() const
// Remove the rev files immediately and insert the blk file paths into an
// ordered map keyed by block file index.
LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
const fs::path& blocksdir = gArgs.GetBlocksDirPath();
for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
for (fs::directory_iterator it(m_opts.blocks_dir); it != fs::directory_iterator(); it++) {
const std::string path = fs::PathToString(it->path().filename());
if (fs::is_regular_file(*it) &&
path.length() == 12 &&
@ -581,12 +580,12 @@ void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
FlatFileSeq BlockManager::BlockFileSeq() const
{
return FlatFileSeq(gArgs.GetBlocksDirPath(), "blk", m_opts.fast_prune ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
return FlatFileSeq(m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
}
FlatFileSeq BlockManager::UndoFileSeq() const
{
return FlatFileSeq(gArgs.GetBlocksDirPath(), "rev", UNDOFILE_CHUNK_SIZE);
return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
}
FILE* BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const

View file

@ -21,8 +21,9 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
{
const auto params {CreateChainParams(ArgsManager{}, ChainType::MAIN)};
node::BlockManager::Options blockman_opts{
const BlockManager::Options blockman_opts{
.chainparams = *params,
.blocks_dir = m_args.GetBlocksDirPath(),
};
BlockManager blockman{blockman_opts};
CChain chain {};

View file

@ -62,6 +62,7 @@
using kernel::ValidationCacheSizes;
using node::ApplyArgsManOptions;
using node::BlockAssembler;
using node::BlockManager;
using node::CalculateCacheSizes;
using node::LoadChainstate;
using node::RegenerateCommitments;
@ -186,8 +187,9 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
.adjusted_time_callback = GetAdjustedTime,
.check_block_index = true,
};
node::BlockManager::Options blockman_opts{
const BlockManager::Options blockman_opts{
.chainparams = chainman_opts.chainparams,
.blocks_dir = m_args.GetBlocksDirPath(),
};
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{

View file

@ -22,6 +22,7 @@
#include <boost/test/unit_test.hpp>
using node::BlockManager;
using node::SnapshotMetadata;
BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, ChainTestingSetup)
@ -381,8 +382,9 @@ struct SnapshotTestSetup : TestChain100Setup {
.datadir = m_args.GetDataDirNet(),
.adjusted_time_callback = GetAdjustedTime,
};
node::BlockManager::Options blockman_opts{
const BlockManager::Options blockman_opts{
.chainparams = chainman_opts.chainparams,
.blocks_dir = m_args.GetBlocksDirPath(),
};
// For robustness, ensure the old manager is destroyed before creating a
// new one.