0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

refactor: Add path argument to FindSnapshotChainstateDir

Remove access to the global gArgs for getting the directory in
utxo_snapshot.

This is done in the context of the libbitcoinkernel project, wherein
reliance of libbitcoinkernel code on the global gArgs is incrementally
removed.
This commit is contained in:
TheCharlatan 2023-05-04 23:19:58 +02:00
parent ef95be334f
commit 8789b11114
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
4 changed files with 11 additions and 12 deletions

View file

@ -4,7 +4,6 @@
#include <node/utxo_snapshot.h> #include <node/utxo_snapshot.h>
#include <common/args.h>
#include <logging.h> #include <logging.h>
#include <streams.h> #include <streams.h>
#include <sync.h> #include <sync.h>
@ -82,10 +81,10 @@ std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
return base_blockhash; return base_blockhash;
} }
std::optional<fs::path> FindSnapshotChainstateDir() std::optional<fs::path> FindSnapshotChainstateDir(const fs::path& data_dir)
{ {
fs::path possible_dir = fs::path possible_dir =
gArgs.GetDataDirNet() / fs::u8path(strprintf("chainstate%s", SNAPSHOT_CHAINSTATE_SUFFIX)); data_dir / fs::u8path(strprintf("chainstate%s", SNAPSHOT_CHAINSTATE_SUFFIX));
if (fs::exists(possible_dir)) { if (fs::exists(possible_dir)) {
return possible_dir; return possible_dir;

View file

@ -67,7 +67,7 @@ constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";
//! Return a path to the snapshot-based chainstate dir, if one exists. //! Return a path to the snapshot-based chainstate dir, if one exists.
std::optional<fs::path> FindSnapshotChainstateDir(); std::optional<fs::path> FindSnapshotChainstateDir(const fs::path& data_dir);
} // namespace node } // namespace node

View file

@ -184,7 +184,7 @@ struct SnapshotTestSetup : TestChain100Setup {
{ {
LOCK(::cs_main); LOCK(::cs_main);
BOOST_CHECK(!chainman.IsSnapshotValidated()); BOOST_CHECK(!chainman.IsSnapshotValidated());
BOOST_CHECK(!node::FindSnapshotChainstateDir()); BOOST_CHECK(!node::FindSnapshotChainstateDir(m_args.GetDataDirNet()));
} }
size_t initial_size; size_t initial_size;
@ -234,7 +234,7 @@ struct SnapshotTestSetup : TestChain100Setup {
auto_infile >> coin; auto_infile >> coin;
})); }));
BOOST_CHECK(!node::FindSnapshotChainstateDir()); BOOST_CHECK(!node::FindSnapshotChainstateDir(m_args.GetDataDirNet()));
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot( BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) { this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
@ -258,7 +258,7 @@ struct SnapshotTestSetup : TestChain100Setup {
})); }));
BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this)); BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
BOOST_CHECK(fs::exists(*node::FindSnapshotChainstateDir())); BOOST_CHECK(fs::exists(*node::FindSnapshotChainstateDir(m_args.GetDataDirNet())));
// Ensure our active chain is the snapshot chainstate. // Ensure our active chain is the snapshot chainstate.
BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull()); BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
@ -271,7 +271,7 @@ struct SnapshotTestSetup : TestChain100Setup {
{ {
LOCK(::cs_main); LOCK(::cs_main);
fs::path found = *node::FindSnapshotChainstateDir(); fs::path found = *node::FindSnapshotChainstateDir(m_args.GetDataDirNet());
// Note: WriteSnapshotBaseBlockhash() is implicitly tested above. // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
@ -491,7 +491,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
this->SetupSnapshot(); this->SetupSnapshot();
fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(); fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(m_args.GetDataDirNet());
BOOST_CHECK(fs::exists(snapshot_chainstate_dir)); BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot"); BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
@ -565,7 +565,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup
SnapshotCompletionResult res; SnapshotCompletionResult res;
auto mock_shutdown = [](bilingual_str msg) {}; auto mock_shutdown = [](bilingual_str msg) {};
fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(); fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(m_args.GetDataDirNet());
BOOST_CHECK(fs::exists(snapshot_chainstate_dir)); BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot"); BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");

View file

@ -5107,7 +5107,7 @@ bool ChainstateManager::ActivateSnapshot(
// PopulateAndValidateSnapshot can return (in error) before the leveldb datadir // PopulateAndValidateSnapshot can return (in error) before the leveldb datadir
// has been created, so only attempt removal if we got that far. // has been created, so only attempt removal if we got that far.
if (auto snapshot_datadir = node::FindSnapshotChainstateDir()) { if (auto snapshot_datadir = node::FindSnapshotChainstateDir(m_options.datadir)) {
// We have to destruct leveldb::DB in order to release the db lock, otherwise // We have to destruct leveldb::DB in order to release the db lock, otherwise
// DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See `leveldb::~DBImpl()`. // DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See `leveldb::~DBImpl()`.
// Destructing the chainstate (and so resetting the coinsviews object) does this. // Destructing the chainstate (and so resetting the coinsviews object) does this.
@ -5597,7 +5597,7 @@ ChainstateManager::~ChainstateManager()
bool ChainstateManager::DetectSnapshotChainstate(CTxMemPool* mempool) bool ChainstateManager::DetectSnapshotChainstate(CTxMemPool* mempool)
{ {
assert(!m_snapshot_chainstate); assert(!m_snapshot_chainstate);
std::optional<fs::path> path = node::FindSnapshotChainstateDir(); std::optional<fs::path> path = node::FindSnapshotChainstateDir(m_options.datadir);
if (!path) { if (!path) {
return false; return false;
} }