refactor: Create chaintype files

This is the first of a number of commits with the goal of moving the
chain type definitions out of chainparamsbase to their own file and
implementing them as enums instead of constant strings. The goal is to
allow the kernel chainparams to no longer include chainparamsbase.

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 21:55:17 +02:00
parent 322ec63b01
commit bfc21c31b2
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
3 changed files with 64 additions and 0 deletions

View file

@ -280,6 +280,7 @@ BITCOIN_CORE_H = \
util/bip32.h \
util/bitdeque.h \
util/bytevectorhash.h \
util/chaintype.h \
util/check.h \
util/epochguard.h \
util/error.h \
@ -707,6 +708,7 @@ libbitcoin_util_a_SOURCES = \
util/asmap.cpp \
util/bip32.cpp \
util/bytevectorhash.cpp \
util/chaintype.cpp \
util/check.cpp \
util/error.cpp \
util/exception.cpp \
@ -956,6 +958,7 @@ libbitcoinkernel_la_SOURCES = \
txdb.cpp \
txmempool.cpp \
uint256.cpp \
util/chaintype.cpp \
util/check.cpp \
util/exception.cpp \
util/fs.cpp \

39
src/util/chaintype.cpp Normal file
View file

@ -0,0 +1,39 @@
// Copyright (c) 2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <util/chaintype.h>
#include <cassert>
#include <optional>
#include <string>
std::string ChainTypeToString(ChainType chain)
{
switch (chain) {
case ChainType::MAIN:
return "main";
case ChainType::TESTNET:
return "test";
case ChainType::SIGNET:
return "signet";
case ChainType::REGTEST:
return "regtest";
}
assert(false);
}
std::optional<ChainType> ChainTypeFromString(std::string_view chain)
{
if (chain == "main") {
return ChainType::MAIN;
} else if (chain == "test") {
return ChainType::TESTNET;
} else if (chain == "signet") {
return ChainType::SIGNET;
} else if (chain == "regtest") {
return ChainType::REGTEST;
} else {
return std::nullopt;
}
}

22
src/util/chaintype.h Normal file
View file

@ -0,0 +1,22 @@
// Copyright (c) 2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_UTIL_CHAINTYPE_H
#define BITCOIN_UTIL_CHAINTYPE_H
#include <optional>
#include <string>
enum class ChainType {
MAIN,
TESTNET,
SIGNET,
REGTEST,
};
std::string ChainTypeToString(ChainType chain);
std::optional<ChainType> ChainTypeFromString(std::string_view chain);
#endif // BITCOIN_UTIL_CHAINTYPE_H