mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
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:
parent
bfc21c31b2
commit
401453df41
2 changed files with 45 additions and 11 deletions
|
@ -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,7 +719,21 @@ 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);
|
||||
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue