Skip to content

Commit b54ab7f

Browse files
fanquakevijaydasmp
authored andcommitted
Merge 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#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 bitcoin#26177. It replaces pull request 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
1 parent 73b0cee commit b54ab7f

47 files changed

Lines changed: 208 additions & 64 deletions

Some content is hidden

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

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ BITCOIN_CORE_H = \
404404
util/hash_type.h \
405405
util/helpers.h \
406406
util/asmap.h \
407+
util/chaintype.h \
407408
util/getuniquepath.h \
408409
util/macros.h \
409410
util/message.h \
@@ -993,6 +994,7 @@ libbitcoin_util_a_SOURCES = \
993994
util/bip32.cpp \
994995
util/bytevectorhash.cpp \
995996
util/check.cpp \
997+
util/chaintype.cpp \
996998
util/edge.cpp \
997999
util/error.cpp \
9981000
util/fees.cpp \

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
@@ -6,6 +6,7 @@
66
#include <policy/policy.h>
77
#include <test/util/setup_common.h>
88
#include <txmempool.h>
9+
#include <util/chaintype.h>
910
#include <validation.h>
1011

1112
#include <vector>
@@ -85,7 +86,7 @@ static void ComplexMemPool(benchmark::Bench& bench)
8586
childTxs = static_cast<int>(bench.complexityN());
8687
}
8788
std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /*min_ancestors=*/1);
88-
const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN);
89+
const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
8990
CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
9091
LOCK2(cs_main, pool.cs);
9192
bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
@@ -100,7 +101,7 @@ static void ComplexMemPool(benchmark::Bench& bench)
100101
static void MempoolCheck(benchmark::Bench& bench)
101102
{
102103
FastRandomContext det_rand{true};
103-
auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(CBaseChainParams::REGTEST, {"-checkmempool=1"});
104+
auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(ChainType::REGTEST, {"-checkmempool=1"});
104105
CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
105106
LOCK2(cs_main, pool.cs);
106107
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
@@ -12,14 +12,15 @@
1212
#include <rpc/blockchain.h>
1313
#include <streams.h>
1414
#include <test/util/setup_common.h>
15+
#include <util/chaintype.h>
1516
#include <validation.h>
1617

1718
#include <univalue.h>
1819

1920
namespace {
2021

2122
struct TestBlockAndIndex {
22-
const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN)};
23+
const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
2324
CBlock block{};
2425
uint256 blockHash{};
2526
CBlockIndex blockindex{};

src/bitcoin-util.cpp

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

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

src/bitcoin-wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[
9090
return EXIT_FAILURE;
9191
}
9292
// Check for chain settings (Params() calls are only valid after this clause)
93-
SelectParams(args.GetChainName());
93+
SelectParams(args.GetChainType());
9494

9595
return std::nullopt;
9696
}

src/chainparams.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,24 +1349,42 @@ const CChainParams &Params() {
13491349
return *globalChainParams;
13501350
}
13511351

1352-
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain)
1352+
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
13531353
{
1354-
if (chain == CBaseChainParams::MAIN) {
1354+
if (chain == ChainType::MAIN) {
13551355
return std::unique_ptr<CChainParams>(new CMainParams());
1356-
} else if (chain == CBaseChainParams::TESTNET) {
1356+
} else if (chain == ChainType::TESTNET) {
13571357
return std::unique_ptr<CChainParams>(new CTestNetParams());
1358-
} else if (chain == CBaseChainParams::DEVNET) {
1358+
} else if (chain == ChainType::DEVNET) {
13591359
return std::unique_ptr<CChainParams>(new CDevNetParams(args));
1360-
} else if (chain == CBaseChainParams::REGTEST) {
1360+
} else if (chain == ChainType::REGTEST) {
13611361
return std::unique_ptr<CChainParams>(new CRegTestParams(args));
13621362
}
1363-
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
1363+
throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__));
1364+
}
1365+
1366+
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain)
1367+
{
1368+
const auto chain_type{ChainTypeFromString(chain)};
1369+
if (!chain_type) {
1370+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
1371+
}
1372+
return CreateChainParams(args, *chain_type);
13641373
}
13651374

1366-
void SelectParams(const std::string& network)
1375+
void SelectParams(const ChainType chain)
13671376
{
1368-
SelectBaseParams(network);
1369-
globalChainParams = CreateChainParams(gArgs, network);
1377+
SelectBaseParams(chain);
1378+
globalChainParams = CreateChainParams(gArgs, chain);
1379+
}
1380+
1381+
void SelectParams(const std::string& chain)
1382+
{
1383+
const auto chain_type{ChainTypeFromString(chain)};
1384+
if (!chain_type) {
1385+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
1386+
}
1387+
SelectParams(*chain_type);
13701388
}
13711389

13721390
void SetupChainParamsOptions(ArgsManager& argsman)

src/chainparams.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <netaddress.h>
1313
#include <primitives/block.h>
1414
#include <protocol.h>
15+
#include <util/chaintype.h>
1516
#include <util/hash_type.h>
1617

1718
#include <memory>
@@ -121,6 +122,8 @@ class CChainParams
121122
int LLMQConnectionRetryTimeout() const { return nLLMQConnectionRetryTimeout; }
122123
/** Return the network string */
123124
std::string NetworkIDString() const { return strNetworkID; }
125+
ChainType GetChainType() const { return ChainTypeFromString(strNetworkID).value(); }
126+
std::string GetChainTypeString() const { return strNetworkID; }
124127
/** Return the list of hostnames to look up for DNS seeds */
125128
const std::vector<std::string>& DNSSeeds() const { return vSeeds; }
126129
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
@@ -197,6 +200,7 @@ class CChainParams
197200
* @returns a CChainParams* of the chosen chain.
198201
* @throws a std::runtime_error if the chain is not supported.
199202
*/
203+
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain);
200204
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain);
201205

202206
/**
@@ -209,6 +213,7 @@ const CChainParams &Params();
209213
* Sets the params returned by Params() to those for the given chain name.
210214
* @throws std::runtime_error when the chain is not supported.
211215
*/
216+
void SelectParams(const ChainType chain);
212217
void SelectParams(const std::string& chain);
213218

214219
/**

src/chainparamsbase.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,40 @@ const CBaseChainParams& BaseParams()
3636
* Port numbers for incoming Tor connections (9996, 19996, 19796, 19896) have
3737
* been chosen arbitrarily to keep ranges of used ports tight.
3838
*/
39-
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain)
39+
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainType chain)
4040
{
41-
if (chain == CBaseChainParams::MAIN)
41+
if (chain == ChainType::MAIN)
4242
return std::make_unique<CBaseChainParams>("", 9998, 9996);
43-
else if (chain == CBaseChainParams::TESTNET)
43+
else if (chain == ChainType::TESTNET)
4444
return std::make_unique<CBaseChainParams>("testnet3", 19998, 19996);
45-
else if (chain == CBaseChainParams::DEVNET)
45+
else if (chain == ChainType::DEVNET)
4646
return std::make_unique<CBaseChainParams>(gArgs.GetDevNetName(), 19798, 19796);
47-
else if (chain == CBaseChainParams::REGTEST)
47+
else if (chain == ChainType::REGTEST)
4848
return std::make_unique<CBaseChainParams>("regtest", 19898, 19896);
4949
else
50+
throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__));
51+
}
52+
53+
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain)
54+
{
55+
const auto chain_type{ChainTypeFromString(chain)};
56+
if (!chain_type) {
5057
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
58+
}
59+
return CreateBaseChainParams(*chain_type);
5160
}
5261

53-
void SelectBaseParams(const std::string& chain)
62+
void SelectBaseParams(const ChainType chain)
5463
{
5564
globalChainBaseParams = CreateBaseChainParams(chain);
56-
gArgs.SelectConfigNetwork(chain);
65+
gArgs.SelectConfigNetwork(ChainTypeToString(chain));
66+
}
67+
68+
void SelectBaseParams(const std::string& chain)
69+
{
70+
const auto chain_type{ChainTypeFromString(chain)};
71+
if (!chain_type) {
72+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
73+
}
74+
SelectBaseParams(*chain_type);
5775
}

0 commit comments

Comments
 (0)