diff options
-rw-r--r-- | src/common/args.cpp | 36 | ||||
-rw-r--r-- | src/common/args.h | 20 |
2 files changed, 45 insertions, 11 deletions
diff --git a/src/common/args.cpp b/src/common/args.cpp index d29b8648bf..266ec5943e 100644 --- a/src/common/args.cpp +++ b/src/common/args.cpp @@ -10,6 +10,7 @@ #include <sync.h> #include <tinyformat.h> #include <univalue.h> +#include <util/chaintype.h> #include <util/check.h> #include <util/fs.h> #include <util/fs_helpers.h> @@ -33,6 +34,7 @@ #include <stdexcept> #include <string> #include <utility> +#include <variant> const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf"; const char * const BITCOIN_SETTINGS_FILENAME = "settings.json"; @@ -717,8 +719,22 @@ fs::path ArgsManager::GetConfigFilePath() const return GetConfigFile(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME)); } +ChainType ArgsManager::GetChainType() const +{ + std::variant<ChainType, std::string> arg = GetChainArg(); + if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed; + throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg))); +} + std::string ArgsManager::GetChainName() const { + auto arg = GetChainArg(); + if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed); + return std::get<std::string>(arg); +} + +std::variant<ChainType, std::string> ArgsManager::GetChainArg() const +{ auto get_net = [&](const std::string& arg) { LOCK(cs_args); util::SettingsValue value = util::GetSetting(m_settings, /* section= */ "", SettingName(arg), @@ -731,20 +747,20 @@ std::string ArgsManager::GetChainName() const const bool fRegTest = get_net("-regtest"); const bool fSigNet = get_net("-signet"); const bool fTestNet = get_net("-testnet"); - const bool is_chain_arg_set = IsArgSet("-chain"); + const auto chain_arg = GetArg("-chain"); - if ((int)is_chain_arg_set + (int)fRegTest + (int)fSigNet + (int)fTestNet > 1) { + if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet > 1) { throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet and -chain. Can use at most one."); } - if (fRegTest) - return CBaseChainParams::REGTEST; - if (fSigNet) { - return CBaseChainParams::SIGNET; + if (chain_arg) { + if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed; + // Not a known string, so return original string + return *chain_arg; } - if (fTestNet) - return CBaseChainParams::TESTNET; - - return GetArg("-chain", CBaseChainParams::MAIN); + if (fRegTest) return ChainType::REGTEST; + if (fSigNet) return ChainType::SIGNET; + if (fTestNet) return ChainType::TESTNET; + return ChainType::MAIN; } bool ArgsManager::UseDefaultSection(const std::string& arg) const diff --git a/src/common/args.h b/src/common/args.h index 430c392e2b..66cd83ed4e 100644 --- a/src/common/args.h +++ b/src/common/args.h @@ -7,6 +7,7 @@ #include <compat/compat.h> #include <sync.h> +#include <util/chaintype.h> #include <util/fs.h> #include <util/settings.h> @@ -17,6 +18,7 @@ #include <set> #include <stdint.h> #include <string> +#include <variant> #include <vector> class ArgsManager; @@ -324,7 +326,15 @@ protected: /** * Returns the appropriate chain name from the program arguments. - * @return CBaseChainParams::MAIN by default; raises runtime error if an invalid combination is given. + * @return ChainType::MAIN by default; raises runtime error if an invalid + * combination, or unknown chain is given. + */ + ChainType GetChainType() const; + + /** + * Returns the appropriate chain name string from the program arguments. + * @return ChainType::MAIN string by default; raises runtime error if an + * invalid combination is given. */ std::string GetChainName() const; @@ -411,6 +421,14 @@ private: */ const fs::path& GetDataDir(bool net_specific) const; + /** + * Return -regtest/-signet/-testnet/-chain= setting as a ChainType enum if a + * recognized chain name was set, or as a string if an unrecognized chain + * name was set. Raise an exception if an invalid combination of flags was + * provided. + */ + std::variant<ChainType, std::string> GetChainArg() const; + // Helper function for LogArgs(). void logArgsPrefix( const std::string& prefix, |