refactor: Introduce ChainType getters for ArgsManager

These are introduced for the next commit where the usage of the
ChainType is adopted throughout the code.

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
Co-authored-by: TheCharlatan <seb.kung@gmail.com>
This commit is contained in:
TheCharlatanandRussell Yanofsky committed 2023-05-09 15:38:37 +02:00
1 parent bfc21c31b2
commit 401453df41
2 files changed
+45 -11

No files matched your search

+26 -10
View File
@@ -10,6 +10,7 @@
#include <sync.h> #include <sync.h>
#include <tinyformat.h> #include <tinyformat.h>
#include <univalue.h> #include <univalue.h>
#include <util/chaintype.h>
#include <util/check.h> #include <util/check.h>
#include <util/fs.h> #include <util/fs.h>
#include <util/fs_helpers.h> #include <util/fs_helpers.h>
@@ -33,6 +34,7 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <utility> #include <utility>
#include <variant>
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf"; const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
const char * const BITCOIN_SETTINGS_FILENAME = "settings.json"; const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
@@ -717,7 +719,21 @@ fs::path ArgsManager::GetConfigFilePath() const
return GetConfigFile(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME)); 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 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) { auto get_net = [&](const std::string& arg) {
LOCK(cs_args); LOCK(cs_args);
@@ -731,20 +747,20 @@ std::string ArgsManager::GetChainName() const
const bool fRegTest = get_net("-regtest"); const bool fRegTest = get_net("-regtest");
const bool fSigNet = get_net("-signet"); const bool fSigNet = get_net("-signet");
const bool fTestNet = get_net("-testnet"); 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."); throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet and -chain. Can use at most one.");
} }
if (fRegTest) if (chain_arg) {
return CBaseChainParams::REGTEST; if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
if (fSigNet) { // Not a known string, so return original string
return CBaseChainParams::SIGNET; return *chain_arg;
} }
if (fTestNet) if (fRegTest) return ChainType::REGTEST;
return CBaseChainParams::TESTNET; if (fSigNet) return ChainType::SIGNET;
if (fTestNet) return ChainType::TESTNET;
return GetArg("-chain", CBaseChainParams::MAIN); return ChainType::MAIN;
} }
bool ArgsManager::UseDefaultSection(const std::string& arg) const bool ArgsManager::UseDefaultSection(const std::string& arg) const
+19 -1
View File
@@ -7,6 +7,7 @@
#include <compat/compat.h> #include <compat/compat.h>
#include <sync.h> #include <sync.h>
#include <util/chaintype.h>
#include <util/fs.h> #include <util/fs.h>
#include <util/settings.h> #include <util/settings.h>
@@ -17,6 +18,7 @@
#include <set> #include <set>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <variant>
#include <vector> #include <vector>
class ArgsManager; class ArgsManager;
@@ -324,7 +326,15 @@ protected:
/** /**
* Returns the appropriate chain name from the program arguments. * 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; std::string GetChainName() const;
@@ -411,6 +421,14 @@ private:
*/ */
const fs::path& GetDataDir(bool net_specific) const; 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(). // Helper function for LogArgs().
void logArgsPrefix( void logArgsPrefix(
const std::string& prefix, const std::string& prefix,