Skip to content

Commit fc06881

Browse files
committed
Merge bitcoin/bitcoin#27491: refactor: Move chain constants to the util library
d168458 scripted-diff: Remove unused chainparamsbase includes (TheCharlatan) e9ee8aa Add missing definitions in prep for scripted diff (TheCharlatan) ba8fc7d refactor: Replace string chain name constants with ChainTypes (TheCharlatan) 401453d refactor: Introduce ChainType getters for ArgsManager (TheCharlatan) bfc21c3 refactor: Create chaintype files (TheCharlatan) Pull request description: This pull request is part of the `libbitcoinkernel` project bitcoin/bitcoin#24303 https://github.com/bitcoin/bitcoin/projects/18 and more specifically its "Step 2: Decouple most non-consensus code from libbitcoinkernel". It is also a follow up to #26177. It replaces pull request bitcoin/bitcoin#27294, which just moved the constants to a new file, but did not re-declare them as enums. The code move of the chain name constants out of the `chainparamsbase` to their own separate header allows the kernel `chainparams` to no longer include `chainparamsbase`. The `chainparamsbase` contain references to the `ArgsManager` and networking related options that should not belong to the kernel library. Besides this move, the constants are re-declared as enums with helper functions facilitating string conversions. ACKs for top commit: ryanofsky: Code review ACK d168458. Just suggested changes since last review. Tree-SHA512: ac2fbe5cbbab4f52eae1e30af1f16700b6589eb4764c328a151a712adfc37f326cc94a65c385534c57d4bc92cc1a13bf1777d92bc924a20dbb30440e7380b316
2 parents d5ff96f + d168458 commit fc06881

Some content is hidden

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

86 files changed

+402
-254
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/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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
6-
#include <chainparamsbase.h>
76
#include <kernel/cs_main.h>
87
#include <kernel/mempool_entry.h>
98
#include <rpc/mempool.h>
109
#include <test/util/setup_common.h>
1110
#include <txmempool.h>
11+
#include <util/chaintype.h>
1212

1313
#include <univalue.h>
1414

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

2222
static void RpcMempool(benchmark::Bench& bench)
2323
{
24-
const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(CBaseChainParams::MAIN);
24+
const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
2525
CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
2626
LOCK2(cs_main, pool.cs);
2727

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <config/bitcoin-config.h>
77
#endif
88

9+
#include <chainparamsbase.h>
910
#include <clientversion.h>
1011
#include <coins.h>
1112
#include <common/args.h>
@@ -91,7 +92,7 @@ static int AppInitRawTx(int argc, char* argv[])
9192

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

0 commit comments

Comments
 (0)