Skip to content

Commit bfc21c3

Browse files
committed
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.
1 parent 322ec63 commit bfc21c3

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ BITCOIN_CORE_H = \
280280
util/bip32.h \
281281
util/bitdeque.h \
282282
util/bytevectorhash.h \
283+
util/chaintype.h \
283284
util/check.h \
284285
util/epochguard.h \
285286
util/error.h \
@@ -707,6 +708,7 @@ libbitcoin_util_a_SOURCES = \
707708
util/asmap.cpp \
708709
util/bip32.cpp \
709710
util/bytevectorhash.cpp \
711+
util/chaintype.cpp \
710712
util/check.cpp \
711713
util/error.cpp \
712714
util/exception.cpp \
@@ -956,6 +958,7 @@ libbitcoinkernel_la_SOURCES = \
956958
txdb.cpp \
957959
txmempool.cpp \
958960
uint256.cpp \
961+
util/chaintype.cpp \
959962
util/check.cpp \
960963
util/exception.cpp \
961964
util/fs.cpp \

src/util/chaintype.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <util/chaintype.h>
6+
7+
#include <cassert>
8+
#include <optional>
9+
#include <string>
10+
11+
std::string ChainTypeToString(ChainType chain)
12+
{
13+
switch (chain) {
14+
case ChainType::MAIN:
15+
return "main";
16+
case ChainType::TESTNET:
17+
return "test";
18+
case ChainType::SIGNET:
19+
return "signet";
20+
case ChainType::REGTEST:
21+
return "regtest";
22+
}
23+
assert(false);
24+
}
25+
26+
std::optional<ChainType> ChainTypeFromString(std::string_view chain)
27+
{
28+
if (chain == "main") {
29+
return ChainType::MAIN;
30+
} else if (chain == "test") {
31+
return ChainType::TESTNET;
32+
} else if (chain == "signet") {
33+
return ChainType::SIGNET;
34+
} else if (chain == "regtest") {
35+
return ChainType::REGTEST;
36+
} else {
37+
return std::nullopt;
38+
}
39+
}

src/util/chaintype.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_CHAINTYPE_H
6+
#define BITCOIN_UTIL_CHAINTYPE_H
7+
8+
#include <optional>
9+
#include <string>
10+
11+
enum class ChainType {
12+
MAIN,
13+
TESTNET,
14+
SIGNET,
15+
REGTEST,
16+
};
17+
18+
std::string ChainTypeToString(ChainType chain);
19+
20+
std::optional<ChainType> ChainTypeFromString(std::string_view chain);
21+
22+
#endif // BITCOIN_UTIL_CHAINTYPE_H

0 commit comments

Comments
 (0)