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.
This commit is contained in:
TheCharlatan
2023-04-17 22:20:59 +02:00
parent 401453df41
commit ba8fc7d788
78 changed files with 288 additions and 229 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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);

View File

@@ -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()};