aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorTheCharlatan <seb.kung@gmail.com>2023-04-17 22:20:59 +0200
committerTheCharlatan <seb.kung@gmail.com>2023-05-09 15:49:14 +0200
commitba8fc7d788932b25864fb260ca14983aa2398c23 (patch)
tree3049d9b5282c243faf7e0ab1e7d2a7ee17937a36 /src/common
parent401453df419af35957ec711423ac3d93ad512fe8 (diff)
downloadbitcoin-ba8fc7d788932b25864fb260ca14983aa2398c23.tar.xz
refactor: Replace string chain name constants with ChainTypes
This commit effectively moves the definition of these constants out of the chainparamsbase to their own file. Using the ChainType enums provides better type safety compared to passing around strings. The commit is part of an ongoing effort to decouple the libbitcoinkernel library from the ArgsManager and other functionality that should not be part of the kernel library.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/args.cpp20
-rw-r--r--src/common/args.h2
-rw-r--r--src/common/config.cpp5
-rw-r--r--src/common/init.cpp2
4 files changed, 15 insertions, 14 deletions
diff --git a/src/common/args.cpp b/src/common/args.cpp
index 266ec5943e..93f2005931 100644
--- a/src/common/args.cpp
+++ b/src/common/args.cpp
@@ -143,7 +143,7 @@ std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
if (m_network.empty()) return std::set<std::string> {};
// if it's okay to use the default section for this network, don't worry
- if (m_network == CBaseChainParams::MAIN) return std::set<std::string> {};
+ if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {};
for (const auto& arg : m_network_only_args) {
if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) {
@@ -157,10 +157,10 @@ std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
{
// Section names to be recognized in the config file.
static const std::set<std::string> available_sections{
- CBaseChainParams::REGTEST,
- CBaseChainParams::SIGNET,
- CBaseChainParams::TESTNET,
- CBaseChainParams::MAIN
+ ChainTypeToString(ChainType::REGTEST),
+ ChainTypeToString(ChainType::SIGNET),
+ ChainTypeToString(ChainType::TESTNET),
+ ChainTypeToString(ChainType::MAIN),
};
LOCK(cs_args);
@@ -445,7 +445,7 @@ util::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) c
{
LOCK(cs_args);
return util::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
- /*ignore_nonpersistent=*/true, /*get_chain_name=*/false);
+ /*ignore_nonpersistent=*/true, /*get_chain_type=*/false);
}
bool ArgsManager::IsArgNegated(const std::string& strArg) const
@@ -726,7 +726,7 @@ ChainType ArgsManager::GetChainType() const
throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
}
-std::string ArgsManager::GetChainName() const
+std::string ArgsManager::GetChainTypeString() const
{
auto arg = GetChainArg();
if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
@@ -740,7 +740,7 @@ std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
util::SettingsValue value = util::GetSetting(m_settings, /* section= */ "", SettingName(arg),
/* ignore_default_section_config= */ false,
/*ignore_nonpersistent=*/false,
- /* get_chain_name= */ true);
+ /* get_chain_type= */ true);
return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
};
@@ -765,7 +765,7 @@ std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
bool ArgsManager::UseDefaultSection(const std::string& arg) const
{
- return m_network == CBaseChainParams::MAIN || m_network_only_args.count(arg) == 0;
+ return m_network == ChainTypeToString(ChainType::MAIN) || m_network_only_args.count(arg) == 0;
}
util::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
@@ -773,7 +773,7 @@ util::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
LOCK(cs_args);
return util::GetSetting(
m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
- /*ignore_nonpersistent=*/false, /*get_chain_name=*/false);
+ /*ignore_nonpersistent=*/false, /*get_chain_type=*/false);
}
std::vector<util::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
diff --git a/src/common/args.h b/src/common/args.h
index 66cd83ed4e..5335633911 100644
--- a/src/common/args.h
+++ b/src/common/args.h
@@ -336,7 +336,7 @@ protected:
* @return ChainType::MAIN string by default; raises runtime error if an
* invalid combination is given.
*/
- std::string GetChainName() const;
+ std::string GetChainTypeString() const;
/**
* Add argument
diff --git a/src/common/config.cpp b/src/common/config.cpp
index 747503ad2a..5efb5efb67 100644
--- a/src/common/config.cpp
+++ b/src/common/config.cpp
@@ -8,6 +8,7 @@
#include <sync.h>
#include <tinyformat.h>
#include <univalue.h>
+#include <util/chaintype.h>
#include <util/fs.h>
#include <util/settings.h>
#include <util/string.h>
@@ -152,7 +153,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
}
}
if (use_conf_file) {
- std::string chain_id = GetChainName();
+ std::string chain_id = GetChainTypeString();
std::vector<std::string> conf_file_names;
auto add_includes = [&](const std::string& network, size_t skip = 0) {
@@ -191,7 +192,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
conf_file_names.clear();
add_includes(chain_id, /* skip= */ chain_includes);
add_includes({}, /* skip= */ default_includes);
- std::string chain_id_final = GetChainName();
+ std::string chain_id_final = GetChainTypeString();
if (chain_id_final != chain_id) {
// Also warn about recursive includeconf for the chain that was specified in one of the includeconfs
add_includes(chain_id_final);
diff --git a/src/common/init.cpp b/src/common/init.cpp
index 6ffa44847a..8933d59c27 100644
--- a/src/common/init.cpp
+++ b/src/common/init.cpp
@@ -26,7 +26,7 @@ std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn setting
}
// Check for chain settings (Params() calls are only valid after this clause)
- SelectParams(args.GetChainName());
+ SelectParams(args.GetChainType());
// Create datadir if it does not exist.
const auto base_path{args.GetDataDirBase()};