Skip to content

Commit

Permalink
allow for custom block index path
Browse files Browse the repository at this point in the history
useful for when you have a blocks dir and you want to benchmark rebuilding
the block index / chainstate with different db implementations.

can specify -blocksdir=<blocks directory> -blockindexdir=<my custom index dir>
and reuse the same blocksdir to test multiple chainstate / block index implementations
  • Loading branch information
josibake committed Oct 1, 2024
1 parent fc642c3 commit 1469952
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/common/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ fs::path ArgsManager::GetBlocksDirPath() const
return path;
}

fs::path ArgsManager::GetIndexDir() const
{
LOCK(cs_args);
const fs::path indexdir{GetPathArg("-blockindexdir")};
if (!indexdir.empty()) {
return fs::absolute(indexdir);
} else {
return GetDataDirNet() / "blocks" / "index";
}
}
fs::path ArgsManager::GetDataDir(bool net_specific) const
{
LOCK(cs_args);
Expand Down
1 change: 1 addition & 0 deletions src/common/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class ArgsManager
* @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
*/
fs::path GetDataDirNet() const { return GetDataDir(true); }
fs::path GetIndexDir() const;

/**
* Clear cached directory paths
Expand Down
3 changes: 3 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
#endif
argsman.AddArg("-assumevalid=<hex>", strprintf("If this block is in the chain assume that it and its ancestors are valid and potentially skip their script verification (0 to verify all, default: %s, testnet3: %s, testnet4: %s, signet: %s)", defaultChainParams->GetConsensus().defaultAssumeValid.GetHex(), testnetChainParams->GetConsensus().defaultAssumeValid.GetHex(), testnet4ChainParams->GetConsensus().defaultAssumeValid.GetHex(), signetChainParams->GetConsensus().defaultAssumeValid.GetHex()), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-blocksdir=<dir>", "Specify directory to hold blocks subdirectory for *.dat files (default: <datadir>)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-blockindexdir=<dir>", "Specify directory to hold blocks index subdirectory (default: <datadir>/blocks/)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-blocksxor",
strprintf("Whether an XOR-key applies to blocksdir *.dat files. "
"The created XOR-key will be zeros for an existing blocksdir or when `-blocksxor=0` is "
Expand Down Expand Up @@ -1055,6 +1056,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
ChainstateManager::Options chainman_opts_dummy{
.chainparams = chainparams,
.datadir = args.GetDataDirNet(),
.indexdir = args.GetIndexDir(),
.notifications = notifications,
};
auto chainman_result{ApplyArgsManOptions(args, chainman_opts_dummy)};
Expand Down Expand Up @@ -1205,6 +1207,7 @@ static ChainstateLoadResult InitAndLoadChainstate(
ChainstateManager::Options chainman_opts{
.chainparams = chainparams,
.datadir = args.GetDataDirNet(),
.indexdir = args.GetIndexDir(),
.notifications = *node.notifications,
.signals = node.validation_signals.get(),
};
Expand Down
1 change: 1 addition & 0 deletions src/kernel/chainstatemanager_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace kernel {
struct ChainstateManagerOpts {
const CChainParams& chainparams;
fs::path datadir;
fs::path indexdir;
std::optional<int32_t> check_block_index{};
bool checkpoints_enabled{DEFAULT_CHECKPOINTS_ENABLED};
//! If set, it will override the minimum work we will assume exists on some valid chain.
Expand Down
2 changes: 1 addition & 1 deletion src/node/chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static ChainstateLoadResult CompleteChainstateInitialization(
// fails if it's still open from the previous loop. Close it first:
pblocktree.reset();
pblocktree = std::make_unique<BlockTreeDB>(DBParams{
.path = chainman.m_options.datadir / "blocks" / "index",
.path = chainman.m_options.indexdir,
.cache_bytes = static_cast<size_t>(cache_sizes.block_tree_db),
.memory_only = options.block_tree_db_in_memory,
.wipe_data = options.wipe_block_tree_db,
Expand Down
2 changes: 2 additions & 0 deletions src/test/validation_chainstatemanager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ struct SnapshotTestSetup : TestChain100Setup {
const ChainstateManager::Options chainman_opts{
.chainparams = ::Params(),
.datadir = chainman.m_options.datadir,
.indexdir = chainman.m_options.indexdir,
.notifications = *m_node.notifications,
.signals = m_node.validation_signals.get(),
};
Expand Down Expand Up @@ -795,6 +796,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_args, BasicTestingSetup)
static const ChainstateManager::Options options{
.chainparams = ::Params(),
.datadir = {},
.indexdir = {},
.notifications = notifications};
return SetOptsFromArgs(*this->m_node.args, options, args);
};
Expand Down

0 comments on commit 1469952

Please sign in to comment.