From c3f1821ac788e522e7558e3575150433450dcb8c Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 5 Oct 2018 21:04:08 +0300 Subject: [PATCH 1/2] Make blockdir always net specific The blocks directory is net specific by definition. Also this prevents the side effect of calling GetBlocksDir(false) in the non-mainnet environment. --- src/init.cpp | 2 +- src/util/system.cpp | 9 +++------ src/util/system.h | 3 ++- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 90dfeb360..070f52caf 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -906,7 +906,7 @@ bool AppInitParameterInteraction() // also see: InitParameterInteraction() - if (!fs::is_directory(GetBlocksDir(false))) { + if (!fs::is_directory(GetBlocksDir())) { return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist."), gArgs.GetArg("-blocksdir", "").c_str())); } diff --git a/src/util/system.cpp b/src/util/system.cpp index 4f5dd2d6e..38d8ef534 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -728,18 +728,17 @@ fs::path GetDefaultDataDir() #endif } -static fs::path g_blocks_path_cached; static fs::path g_blocks_path_cache_net_specific; static fs::path pathCached; static fs::path pathCachedNetSpecific; static CCriticalSection csPathCached; -const fs::path &GetBlocksDir(bool fNetSpecific) +const fs::path &GetBlocksDir() { LOCK(csPathCached); - fs::path &path = fNetSpecific ? g_blocks_path_cache_net_specific : g_blocks_path_cached; + fs::path &path = g_blocks_path_cache_net_specific; // This can be called during exceptions by LogPrintf(), so we cache the // value so we don't have to do memory allocations after that. @@ -755,9 +754,8 @@ const fs::path &GetBlocksDir(bool fNetSpecific) } else { path = GetDataDir(false); } - if (fNetSpecific) - path /= BaseParams().DataDir(); + path /= BaseParams().DataDir(); path /= "blocks"; fs::create_directories(path); return path; @@ -801,7 +799,6 @@ void ClearDatadirCache() pathCached = fs::path(); pathCachedNetSpecific = fs::path(); - g_blocks_path_cached = fs::path(); g_blocks_path_cache_net_specific = fs::path(); } diff --git a/src/util/system.h b/src/util/system.h index 5634b8dd6..dd8868d16 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -78,7 +78,8 @@ void ReleaseDirectoryLocks(); bool TryCreateDirectories(const fs::path& p); fs::path GetDefaultDataDir(); -const fs::path &GetBlocksDir(bool fNetSpecific = true); +// The blocks directory is always net specific. +const fs::path &GetBlocksDir(); const fs::path &GetDataDir(bool fNetSpecific = true); void ClearDatadirCache(); fs::path GetConfigFile(const std::string& confPath); From e4a0c3547ed886871f8b3d51c6b4ffdb181a8b9c Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 30 Nov 2018 16:32:23 +0200 Subject: [PATCH 2/2] Improve blocksdir functional test. A new node should not create an unused `blocks` directory in the root of the data directory when `-testnet` or `-regtest` is specified. --- test/functional/feature_blocksdir.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/functional/feature_blocksdir.py b/test/functional/feature_blocksdir.py index c170f510c..3a4889bbe 100755 --- a/test/functional/feature_blocksdir.py +++ b/test/functional/feature_blocksdir.py @@ -18,6 +18,8 @@ class BlocksdirTest(BitcoinTestFramework): def run_test(self): self.stop_node(0) + assert os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest", "blocks")) + assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "blocks")) shutil.rmtree(self.nodes[0].datadir) initialize_datadir(self.options.tmpdir, 0) self.log.info("Starting with nonexistent blocksdir ...")