Skip to content

Commit

Permalink
Merge #14409: utils and libraries: Make 'blocksdir' always net specific
Browse files Browse the repository at this point in the history
e4a0c35 Improve blocksdir functional test. (Hennadii Stepanov)
c3f1821 Make blockdir always net specific (Hennadii Stepanov)

Pull request description:

  The blocks directory is net specific by definition.

  Also this prevents the side effect of calling `GetBlocksDir(false)` in the non-mainnet environment.
  Currently a new node creates an unused `blocks\` directory in the root of the data directory when `-testnet` or `-regtest` is specified.

  Refs:
  - #12653
  - #12653 (comment) by @laanwj
  - #14595 (comment)

Tree-SHA512: c9957a68a4a200ebd2010823a56db7e61563afedcb7c9828e86b13f3af2990e07854b622c1f3374756f94574acb3ea32de7d2a399eef6c0623f0e11265155627
  • Loading branch information
laanwj committed Jan 16, 2019
2 parents 3ae3748 + e4a0c35 commit 64ee943
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Expand Up @@ -930,7 +930,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()));
}

Expand Down
9 changes: 3 additions & 6 deletions src/util/system.cpp
Expand Up @@ -749,18 +749,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.
Expand All @@ -776,9 +775,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;
Expand Down Expand Up @@ -822,7 +820,6 @@ void ClearDatadirCache()

pathCached = fs::path();
pathCachedNetSpecific = fs::path();
g_blocks_path_cached = fs::path();
g_blocks_path_cache_net_specific = fs::path();
}

Expand Down
3 changes: 2 additions & 1 deletion src/util/system.h
Expand Up @@ -79,7 +79,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);
Expand Down
2 changes: 2 additions & 0 deletions test/functional/feature_blocksdir.py
Expand Up @@ -18,6 +18,8 @@ def set_test_params(self):

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 ...")
Expand Down

0 comments on commit 64ee943

Please sign in to comment.