Skip to content

Commit ba8fc7d

Browse files
committed
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.
1 parent 401453d commit ba8fc7d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+288
-229
lines changed

src/bench/checkblock.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <common/args.h>
1010
#include <consensus/validation.h>
1111
#include <streams.h>
12+
#include <util/chaintype.h>
1213
#include <validation.h>
1314

1415
// These are the two major time-sinks which happen after we have fully received
@@ -36,7 +37,7 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
3637
stream.write({&a, 1}); // Prevent compaction
3738

3839
ArgsManager bench_args;
39-
const auto chainParams = CreateChainParams(bench_args, CBaseChainParams::MAIN);
40+
const auto chainParams = CreateChainParams(bench_args, ChainType::MAIN);
4041

4142
bench.unit("block").run([&] {
4243
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here

src/bench/load_external.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <bench/data.h>
77
#include <chainparams.h>
88
#include <test/util/setup_common.h>
9+
#include <util/chaintype.h>
910
#include <validation.h>
1011

1112
/**
@@ -22,7 +23,7 @@
2223
*/
2324
static void LoadExternalBlockFile(benchmark::Bench& bench)
2425
{
25-
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN)};
26+
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
2627

2728
// Create a single block as in the blocks files (magic bytes, block size,
2829
// block data) as a stream object.

src/bench/logging.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <bench/bench.h>
66
#include <logging.h>
77
#include <test/util/setup_common.h>
8+
#include <util/chaintype.h>
89

910
// All but 2 of the benchmarks should have roughly similar performance:
1011
//
@@ -18,7 +19,7 @@ static void Logging(benchmark::Bench& bench, const std::vector<const char*>& ext
1819
LogInstance().DisableCategory(BCLog::LogFlags::ALL);
1920

2021
TestingSetup test_setup{
21-
CBaseChainParams::REGTEST,
22+
ChainType::REGTEST,
2223
extra_args,
2324
};
2425

src/bench/mempool_stress.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <policy/policy.h>
88
#include <test/util/setup_common.h>
99
#include <txmempool.h>
10+
#include <util/chaintype.h>
1011
#include <validation.h>
1112

1213
#include <vector>
@@ -88,7 +89,7 @@ static void ComplexMemPool(benchmark::Bench& bench)
8889
childTxs = static_cast<int>(bench.complexityN());
8990
}
9091
std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /*min_ancestors=*/1);
91-
const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN);
92+
const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
9293
CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
9394
LOCK2(cs_main, pool.cs);
9495
bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
@@ -103,7 +104,7 @@ static void ComplexMemPool(benchmark::Bench& bench)
103104
static void MempoolCheck(benchmark::Bench& bench)
104105
{
105106
FastRandomContext det_rand{true};
106-
auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(CBaseChainParams::REGTEST, {"-checkmempool=1"});
107+
auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(ChainType::REGTEST, {"-checkmempool=1"});
107108
CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
108109
LOCK2(cs_main, pool.cs);
109110
testing_setup->PopulateMempool(det_rand, 400, true);

src/bench/rpc_blockchain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include <rpc/blockchain.h>
99
#include <streams.h>
1010
#include <test/util/setup_common.h>
11+
#include <util/chaintype.h>
1112
#include <validation.h>
1213

1314
#include <univalue.h>
1415

1516
namespace {
1617

1718
struct TestBlockAndIndex {
18-
const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN)};
19+
const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
1920
CBlock block{};
2021
uint256 blockHash{};
2122
CBlockIndex blockindex{};

src/bench/rpc_mempool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rpc/mempool.h>
1010
#include <test/util/setup_common.h>
1111
#include <txmempool.h>
12+
#include <util/chaintype.h>
1213

1314
#include <univalue.h>
1415

@@ -21,7 +22,7 @@ static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& poo
2122

2223
static void RpcMempool(benchmark::Bench& bench)
2324
{
24-
const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(CBaseChainParams::MAIN);
25+
const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
2526
CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
2627
LOCK2(cs_main, pool.cs);
2728

src/bitcoin-chainstate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <node/chainstate.h>
2626
#include <scheduler.h>
2727
#include <script/sigcache.h>
28+
#include <util/chaintype.h>
2829
#include <util/thread.h>
2930
#include <validation.h>
3031
#include <validationinterface.h>
@@ -52,7 +53,7 @@ int main(int argc, char* argv[])
5253

5354

5455
// SETUP: Misc Globals
55-
SelectParams(CBaseChainParams::MAIN);
56+
SelectParams(ChainType::MAIN);
5657
auto chainparams = CChainParams::Main();
5758

5859
kernel::Context kernel_context{};

src/bitcoin-cli.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <rpc/request.h>
2121
#include <tinyformat.h>
2222
#include <univalue.h>
23+
#include <util/chaintype.h>
2324
#include <util/exception.h>
2425
#include <util/strencodings.h>
2526
#include <util/system.h>
@@ -73,10 +74,10 @@ static void SetupCliArgs(ArgsManager& argsman)
7374
{
7475
SetupHelpOptions(argsman);
7576

76-
const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
77-
const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
78-
const auto signetBaseParams = CreateBaseChainParams(CBaseChainParams::SIGNET);
79-
const auto regtestBaseParams = CreateBaseChainParams(CBaseChainParams::REGTEST);
77+
const auto defaultBaseParams = CreateBaseChainParams(ChainType::MAIN);
78+
const auto testnetBaseParams = CreateBaseChainParams(ChainType::TESTNET);
79+
const auto signetBaseParams = CreateBaseChainParams(ChainType::SIGNET);
80+
const auto regtestBaseParams = CreateBaseChainParams(ChainType::REGTEST);
8081

8182
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
8283
argsman.AddArg("-conf=<file>", strprintf("Specify configuration file. Relative paths will be prefixed by datadir location. (default: %s)", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -174,7 +175,7 @@ static int AppInitRPC(int argc, char* argv[])
174175
}
175176
// Check for chain settings (BaseParams() calls are only valid after this clause)
176177
try {
177-
SelectBaseParams(gArgs.GetChainName());
178+
SelectBaseParams(gArgs.GetChainType());
178179
} catch (const std::exception& e) {
179180
tfm::format(std::cerr, "Error: %s\n", e.what());
180181
return EXIT_FAILURE;
@@ -426,10 +427,16 @@ class NetinfoRequestHandler : public BaseRequestHandler
426427
std::vector<Peer> m_peers;
427428
std::string ChainToString() const
428429
{
429-
if (gArgs.GetChainName() == CBaseChainParams::TESTNET) return " testnet";
430-
if (gArgs.GetChainName() == CBaseChainParams::SIGNET) return " signet";
431-
if (gArgs.GetChainName() == CBaseChainParams::REGTEST) return " regtest";
432-
return "";
430+
switch (gArgs.GetChainType()) {
431+
case ChainType::TESTNET:
432+
return " testnet";
433+
case ChainType::SIGNET:
434+
return " signet";
435+
case ChainType::REGTEST:
436+
return " regtest";
437+
default:
438+
return "";
439+
}
433440
}
434441
std::string PingTimeToString(double seconds) const
435442
{

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static int AppInitRawTx(int argc, char* argv[])
9191

9292
// Check for chain settings (Params() calls are only valid after this clause)
9393
try {
94-
SelectParams(gArgs.GetChainName());
94+
SelectParams(gArgs.GetChainType());
9595
} catch (const std::exception& e) {
9696
tfm::format(std::cerr, "Error: %s\n", e.what());
9797
return EXIT_FAILURE;

src/bitcoin-util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
7575

7676
// Check for chain settings (Params() calls are only valid after this clause)
7777
try {
78-
SelectParams(args.GetChainName());
78+
SelectParams(args.GetChainType());
7979
} catch (const std::exception& e) {
8080
tfm::format(std::cerr, "Error: %s\n", e.what());
8181
return EXIT_FAILURE;

0 commit comments

Comments
 (0)