From 9f8b43815a0a0d5dcfcdfc63597e5661c8580a74 Mon Sep 17 00:00:00 2001 From: Brandon Odiwuor Date: Tue, 9 Apr 2024 18:23:10 +0300 Subject: [PATCH] Use distinct data dirs for each signet If the -signetchallenge argument is provided, the hash-160 (first 8 characters) of the challenge is appended to the datadir name, resulting in unique data directories for each signet, i.e., signet_XXXXXXX. --- src/common/args.cpp | 26 ++++++++++++++++++++++++-- src/common/args.h | 8 ++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/common/args.cpp b/src/common/args.cpp index c90eb0c6856d8..6a879fc070c04 100644 --- a/src/common/args.cpp +++ b/src/common/args.cpp @@ -5,6 +5,7 @@ #include +#include #include #include #include @@ -277,6 +278,18 @@ fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) return result.has_filename() ? result : result.parent_path(); } +fs::path ArgsManager::GetSignetDataDir() const +{ + const std::string base_data_dir = BaseParams().DataDir(); + const std::string signet_challenge_str = GetArg("-signetchallenge", ""); + if (!signet_challenge_str.empty()) { + std::vector signet_challenge = ParseHex(signet_challenge_str); + const auto data_dir_suffix = HexStr(Hash160(signet_challenge)).substr(0, 8); + return fs::PathFromString(base_data_dir + "_" + data_dir_suffix); + } + return fs::PathFromString(base_data_dir); +} + fs::path ArgsManager::GetBlocksDirPath() const { LOCK(cs_args); @@ -296,7 +309,12 @@ fs::path ArgsManager::GetBlocksDirPath() const path = GetDataDirBase(); } - path /= fs::PathFromString(BaseParams().DataDir()); + if (GetChainType() == ChainType::SIGNET) { + path /= GetSignetDataDir(); + } else { + path /= fs::PathFromString(BaseParams().DataDir()); + } + path /= "blocks"; fs::create_directories(path); return path; @@ -322,7 +340,11 @@ fs::path ArgsManager::GetDataDir(bool net_specific) const } if (net_specific && !BaseParams().DataDir().empty()) { - path /= fs::PathFromString(BaseParams().DataDir()); + if (GetChainType() == ChainType::SIGNET) { + path /= GetSignetDataDir(); + } else { + path /= fs::PathFromString(BaseParams().DataDir()); + } } return path; diff --git a/src/common/args.h b/src/common/args.h index 78a61313b91df..d7099f956e67a 100644 --- a/src/common/args.h +++ b/src/common/args.h @@ -210,6 +210,14 @@ class ArgsManager */ std::optional GetCommand() const; + /** + * Get signet data directory path. + * If a signet-challange argument is provided, it is used in constructing the directory path. + * + * @return The path to the signet data directory. + */ + fs::path GetSignetDataDir() const; + /** * Get blocks directory path *