Skip to content

Commit 401453d

Browse files
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>
1 parent bfc21c3 commit 401453d

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/common/args.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sync.h>
1111
#include <tinyformat.h>
1212
#include <univalue.h>
13+
#include <util/chaintype.h>
1314
#include <util/check.h>
1415
#include <util/fs.h>
1516
#include <util/fs_helpers.h>
@@ -33,6 +34,7 @@
3334
#include <stdexcept>
3435
#include <string>
3536
#include <utility>
37+
#include <variant>
3638

3739
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
3840
const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
@@ -717,7 +719,21 @@ fs::path ArgsManager::GetConfigFilePath() const
717719
return GetConfigFile(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME));
718720
}
719721

722+
ChainType ArgsManager::GetChainType() const
723+
{
724+
std::variant<ChainType, std::string> arg = GetChainArg();
725+
if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed;
726+
throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
727+
}
728+
720729
std::string ArgsManager::GetChainName() const
730+
{
731+
auto arg = GetChainArg();
732+
if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
733+
return std::get<std::string>(arg);
734+
}
735+
736+
std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
721737
{
722738
auto get_net = [&](const std::string& arg) {
723739
LOCK(cs_args);
@@ -731,20 +747,20 @@ std::string ArgsManager::GetChainName() const
731747
const bool fRegTest = get_net("-regtest");
732748
const bool fSigNet = get_net("-signet");
733749
const bool fTestNet = get_net("-testnet");
734-
const bool is_chain_arg_set = IsArgSet("-chain");
750+
const auto chain_arg = GetArg("-chain");
735751

736-
if ((int)is_chain_arg_set + (int)fRegTest + (int)fSigNet + (int)fTestNet > 1) {
752+
if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet > 1) {
737753
throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet and -chain. Can use at most one.");
738754
}
739-
if (fRegTest)
740-
return CBaseChainParams::REGTEST;
741-
if (fSigNet) {
742-
return CBaseChainParams::SIGNET;
755+
if (chain_arg) {
756+
if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
757+
// Not a known string, so return original string
758+
return *chain_arg;
743759
}
744-
if (fTestNet)
745-
return CBaseChainParams::TESTNET;
746-
747-
return GetArg("-chain", CBaseChainParams::MAIN);
760+
if (fRegTest) return ChainType::REGTEST;
761+
if (fSigNet) return ChainType::SIGNET;
762+
if (fTestNet) return ChainType::TESTNET;
763+
return ChainType::MAIN;
748764
}
749765

750766
bool ArgsManager::UseDefaultSection(const std::string& arg) const

src/common/args.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <compat/compat.h>
99
#include <sync.h>
10+
#include <util/chaintype.h>
1011
#include <util/fs.h>
1112
#include <util/settings.h>
1213

@@ -17,6 +18,7 @@
1718
#include <set>
1819
#include <stdint.h>
1920
#include <string>
21+
#include <variant>
2022
#include <vector>
2123

2224
class ArgsManager;
@@ -324,7 +326,15 @@ class ArgsManager
324326

325327
/**
326328
* Returns the appropriate chain name from the program arguments.
327-
* @return CBaseChainParams::MAIN by default; raises runtime error if an invalid combination is given.
329+
* @return ChainType::MAIN by default; raises runtime error if an invalid
330+
* combination, or unknown chain is given.
331+
*/
332+
ChainType GetChainType() const;
333+
334+
/**
335+
* Returns the appropriate chain name string from the program arguments.
336+
* @return ChainType::MAIN string by default; raises runtime error if an
337+
* invalid combination is given.
328338
*/
329339
std::string GetChainName() const;
330340

@@ -411,6 +421,14 @@ class ArgsManager
411421
*/
412422
const fs::path& GetDataDir(bool net_specific) const;
413423

424+
/**
425+
* Return -regtest/-signet/-testnet/-chain= setting as a ChainType enum if a
426+
* recognized chain name was set, or as a string if an unrecognized chain
427+
* name was set. Raise an exception if an invalid combination of flags was
428+
* provided.
429+
*/
430+
std::variant<ChainType, std::string> GetChainArg() const;
431+
414432
// Helper function for LogArgs().
415433
void logArgsPrefix(
416434
const std::string& prefix,

0 commit comments

Comments
 (0)