From aadd7c5b9b43a38beaa954b4cb8c2fff55f2200f Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Tue, 16 Aug 2022 23:32:55 -0400 Subject: [PATCH] refactor, validation: Add ChainstateManagerOpts db options Use ChainstateManagerOpts struct to remove ArgsManager uses from validation.cpp. This commit does not change behavior. --- src/bitcoin-chainstate.cpp | 1 + src/init.cpp | 2 ++ src/kernel/chainstatemanager_opts.h | 6 ++++++ src/node/chainstate.cpp | 6 ++---- src/node/chainstatemanager_args.cpp | 7 +++++++ src/test/util/setup_common.cpp | 1 + src/test/validation_chainstatemanager_tests.cpp | 1 + src/validation.cpp | 8 +++----- 8 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index d972b71a65..423fa79c6f 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -82,6 +82,7 @@ int main(int argc, char* argv[]) // SETUP: Chainstate const ChainstateManager::Options chainman_opts{ .chainparams = chainparams, + .datadir = gArgs.GetDataDirNet(), .adjusted_time_callback = NodeClock::now, }; ChainstateManager chainman{chainman_opts}; diff --git a/src/init.cpp b/src/init.cpp index 73ae36e4f7..49040c6f63 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1046,6 +1046,7 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb { ChainstateManager::Options chainman_opts_dummy{ .chainparams = chainparams, + .datadir = args.GetDataDirNet(), }; if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) { return InitError(*error); @@ -1444,6 +1445,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false); ChainstateManager::Options chainman_opts{ .chainparams = chainparams, + .datadir = args.GetDataDirNet(), .adjusted_time_callback = GetAdjustedTime, }; Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction diff --git a/src/kernel/chainstatemanager_opts.h b/src/kernel/chainstatemanager_opts.h index 226bb6031e..2395f60164 100644 --- a/src/kernel/chainstatemanager_opts.h +++ b/src/kernel/chainstatemanager_opts.h @@ -6,6 +6,8 @@ #define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H #include +#include +#include #include #include @@ -27,6 +29,7 @@ namespace kernel { */ struct ChainstateManagerOpts { const CChainParams& chainparams; + fs::path datadir; const std::function adjusted_time_callback{nullptr}; std::optional check_block_index{}; bool checkpoints_enabled{DEFAULT_CHECKPOINTS_ENABLED}; @@ -36,6 +39,9 @@ struct ChainstateManagerOpts { std::optional assumed_valid_block{}; //! If the tip is older than this, the node is considered to be in initial block download. std::chrono::seconds max_tip_age{DEFAULT_MAX_TIP_AGE}; + DBOptions block_tree_db{}; + DBOptions coins_db{}; + CoinsViewOptions coins_view{}; }; } // namespace kernel diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp index fa521a9e03..4741c4c421 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -10,14 +10,12 @@ #include #include #include -#include #include #include #include #include #include #include -#include #include #include #include @@ -67,11 +65,11 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize // fails if it's still open from the previous loop. Close it first: pblocktree.reset(); pblocktree = std::make_unique(DBParams{ - .path = gArgs.GetDataDirNet() / "blocks" / "index", + .path = chainman.m_options.datadir / "blocks" / "index", .cache_bytes = static_cast(cache_sizes.block_tree_db), .memory_only = options.block_tree_db_in_memory, .wipe_data = options.reindex, - .options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}); + .options = chainman.m_options.block_tree_db}); if (options.reindex) { pblocktree->WriteReindexing(true); diff --git a/src/node/chainstatemanager_args.cpp b/src/node/chainstatemanager_args.cpp index b0d929626b..9801e6e959 100644 --- a/src/node/chainstatemanager_args.cpp +++ b/src/node/chainstatemanager_args.cpp @@ -5,6 +5,9 @@ #include #include +#include +#include +#include #include #include #include @@ -34,6 +37,10 @@ std::optional ApplyArgsManOptions(const ArgsManager& args, Chains if (auto value{args.GetIntArg("-maxtipage")}) opts.max_tip_age = std::chrono::seconds{*value}; + ReadDatabaseArgs(args, opts.block_tree_db); + ReadDatabaseArgs(args, opts.coins_db); + ReadCoinsViewArgs(args, opts.coins_view); + return std::nullopt; } } // namespace node diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 2318380c6e..1fc8a02975 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -180,6 +180,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve const ChainstateManager::Options chainman_opts{ .chainparams = chainparams, + .datadir = m_args.GetDataDirNet(), .adjusted_time_callback = GetAdjustedTime, .check_block_index = true, }; diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp index 56867a584b..63523d3e2e 100644 --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -374,6 +374,7 @@ struct SnapshotTestSetup : TestChain100Setup { BOOST_CHECK_EQUAL(chainman.GetAll().size(), 0); const ChainstateManager::Options chainman_opts{ .chainparams = ::Params(), + .datadir = m_args.GetDataDirNet(), .adjusted_time_callback = GetAdjustedTime, }; // For robustness, ensure the old manager is destroyed before creating a diff --git a/src/validation.cpp b/src/validation.cpp index 971eb346a4..c839647b29 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -28,8 +28,6 @@ #include #include #include -#include -#include #include #include #include @@ -1545,13 +1543,13 @@ void Chainstate::InitCoinsDB( m_coins_views = std::make_unique( DBParams{ - .path = gArgs.GetDataDirNet() / leveldb_name, + .path = m_chainman.m_options.datadir / leveldb_name, .cache_bytes = cache_size_bytes, .memory_only = in_memory, .wipe_data = should_wipe, .obfuscate = true, - .options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}, - [] { CoinsViewOptions options; node::ReadCoinsViewArgs(gArgs, options); return options; }()); + .options = m_chainman.m_options.coins_db}, + m_chainman.m_options.coins_view); } void Chainstate::InitCoinsCache(size_t cache_size_bytes)