Skip to content

Commit

Permalink
Use distinct data dirs for each signet
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BrandonOdiwuor committed May 15, 2024
1 parent 0f0e36d commit 9f8b438
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/common/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <common/args.h>

#include <hash.h>
#include <chainparamsbase.h>
#include <common/settings.h>
#include <logging.h>
Expand Down Expand Up @@ -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<uint8_t> 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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/common/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ class ArgsManager
*/
std::optional<const Command> 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
*
Expand Down

0 comments on commit 9f8b438

Please sign in to comment.