Skip to content

Commit 18e5ba7

Browse files
committed
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.
1 parent 02a0899 commit 18e5ba7

File tree

7 files changed

+22
-11
lines changed

7 files changed

+22
-11
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ int main(int argc, char* argv[])
8989
};
9090
const node::BlockManager::Options blockman_opts{
9191
.chainparams = chainman_opts.chainparams,
92+
.blocks_dir = gArgs.GetBlocksDirPath(),
9293
};
9394
ChainstateManager chainman{chainman_opts, blockman_opts};
9495

src/init.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,19 @@ using kernel::DumpMempool;
116116
using kernel::ValidationCacheSizes;
117117

118118
using node::ApplyArgsManOptions;
119+
using node::BlockManager;
119120
using node::CacheSizes;
120121
using node::CalculateCacheSizes;
121122
using node::DEFAULT_PERSIST_MEMPOOL;
122123
using node::DEFAULT_PRINTPRIORITY;
123124
using node::DEFAULT_STOPAFTERBLOCKIMPORT;
125+
using node::fReindex;
124126
using node::LoadChainstate;
125127
using node::MempoolPath;
126-
using node::ShouldPersistMempool;
127128
using node::NodeContext;
129+
using node::ShouldPersistMempool;
128130
using node::ThreadImport;
129131
using node::VerifyLoadedChainstate;
130-
using node::fReindex;
131132

132133
static constexpr bool DEFAULT_PROXYRANDOMIZE{true};
133134
static constexpr bool DEFAULT_REST_ENABLE{false};
@@ -1037,8 +1038,9 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
10371038
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
10381039
return InitError(*error);
10391040
}
1040-
node::BlockManager::Options blockman_opts_dummy{
1041+
BlockManager::Options blockman_opts_dummy{
10411042
.chainparams = chainman_opts_dummy.chainparams,
1043+
.blocks_dir = args.GetBlocksDirPath(),
10421044
};
10431045
if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) {
10441046
return InitError(*error);
@@ -1446,8 +1448,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14461448
};
14471449
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14481450

1449-
node::BlockManager::Options blockman_opts{
1451+
BlockManager::Options blockman_opts{
14501452
.chainparams = chainman_opts.chainparams,
1453+
.blocks_dir = args.GetBlocksDirPath(),
14511454
};
14521455
Assert(!ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14531456

src/kernel/blockmanager_opts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
66
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
77

8+
#include <util/fs.h>
9+
810
#include <cstdint>
911

1012
class CChainParams;
@@ -19,6 +21,7 @@ struct BlockManagerOpts {
1921
const CChainParams& chainparams;
2022
uint64_t prune_target{0};
2123
bool fast_prune{false};
24+
const fs::path blocks_dir;
2225
};
2326

2427
} // namespace kernel

src/node/blockstorage.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ void BlockManager::CleanupBlockRevFiles() const
427427
// Remove the rev files immediately and insert the blk file paths into an
428428
// ordered map keyed by block file index.
429429
LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
430-
const fs::path& blocksdir = gArgs.GetBlocksDirPath();
431-
for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
430+
for (fs::directory_iterator it(m_opts.blocks_dir); it != fs::directory_iterator(); it++) {
432431
const std::string path = fs::PathToString(it->path().filename());
433432
if (fs::is_regular_file(*it) &&
434433
path.length() == 12 &&
@@ -581,12 +580,12 @@ void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
581580

582581
FlatFileSeq BlockManager::BlockFileSeq() const
583582
{
584-
return FlatFileSeq(gArgs.GetBlocksDirPath(), "blk", m_opts.fast_prune ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
583+
return FlatFileSeq(m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
585584
}
586585

587586
FlatFileSeq BlockManager::UndoFileSeq() const
588587
{
589-
return FlatFileSeq(gArgs.GetBlocksDirPath(), "rev", UNDOFILE_CHUNK_SIZE);
588+
return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
590589
}
591590

592591
FILE* BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const

src/test/blockmanager_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
2121
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
2222
{
2323
const auto params {CreateChainParams(ArgsManager{}, ChainType::MAIN)};
24-
node::BlockManager::Options blockman_opts{
24+
const BlockManager::Options blockman_opts{
2525
.chainparams = *params,
26+
.blocks_dir = m_args.GetBlocksDirPath(),
2627
};
2728
BlockManager blockman{blockman_opts};
2829
CChain chain {};

src/test/util/setup_common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
using kernel::ValidationCacheSizes;
6363
using node::ApplyArgsManOptions;
6464
using node::BlockAssembler;
65+
using node::BlockManager;
6566
using node::CalculateCacheSizes;
6667
using node::LoadChainstate;
6768
using node::RegenerateCommitments;
@@ -186,8 +187,9 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
186187
.adjusted_time_callback = GetAdjustedTime,
187188
.check_block_index = true,
188189
};
189-
node::BlockManager::Options blockman_opts{
190+
const BlockManager::Options blockman_opts{
190191
.chainparams = chainman_opts.chainparams,
192+
.blocks_dir = m_args.GetBlocksDirPath(),
191193
};
192194
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
193195
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <boost/test/unit_test.hpp>
2424

25+
using node::BlockManager;
2526
using node::SnapshotMetadata;
2627

2728
BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, ChainTestingSetup)
@@ -381,8 +382,9 @@ struct SnapshotTestSetup : TestChain100Setup {
381382
.datadir = m_args.GetDataDirNet(),
382383
.adjusted_time_callback = GetAdjustedTime,
383384
};
384-
node::BlockManager::Options blockman_opts{
385+
const BlockManager::Options blockman_opts{
385386
.chainparams = chainman_opts.chainparams,
387+
.blocks_dir = m_args.GetBlocksDirPath(),
386388
};
387389
// For robustness, ensure the old manager is destroyed before creating a
388390
// new one.

0 commit comments

Comments
 (0)