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

@@ -7,14 +7,10 @@
#include <common/args.h>
#include <tinyformat.h>
#include <util/chaintype.h>
#include <assert.h>
const std::string CBaseChainParams::MAIN = "main";
const std::string CBaseChainParams::TESTNET = "test";
const std::string CBaseChainParams::SIGNET = "signet";
const std::string CBaseChainParams::REGTEST = "regtest";
void SetupChainParamsBaseOptions(ArgsManager& argsman)
{
argsman.AddArg("-chain=<chain>", "Use the chain <chain> (default: main). Allowed values: main, test, signet, regtest", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
@@ -40,22 +36,23 @@ const CBaseChainParams& BaseParams()
* Port numbers for incoming Tor connections (8334, 18334, 38334, 18445) have
* been chosen arbitrarily to keep ranges of used ports tight.
*/
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain)
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainType chain)
{
if (chain == CBaseChainParams::MAIN) {
switch (chain) {
case ChainType::MAIN:
return std::make_unique<CBaseChainParams>("", 8332, 8334);
} else if (chain == CBaseChainParams::TESTNET) {
case ChainType::TESTNET:
return std::make_unique<CBaseChainParams>("testnet3", 18332, 18334);
} else if (chain == CBaseChainParams::SIGNET) {
case ChainType::SIGNET:
return std::make_unique<CBaseChainParams>("signet", 38332, 38334);
} else if (chain == CBaseChainParams::REGTEST) {
case ChainType::REGTEST:
return std::make_unique<CBaseChainParams>("regtest", 18443, 18445);
}
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__));
}
void SelectBaseParams(const std::string& chain)
void SelectBaseParams(const ChainType chain)
{
globalChainBaseParams = CreateBaseChainParams(chain);
gArgs.SelectConfigNetwork(chain);
gArgs.SelectConfigNetwork(ChainTypeToString(chain));
}