Skip to content

Commit fa23308

Browse files
author
MarcoFalke
committed
Remove gArgs global from CreateChainParams to aid testing
1 parent e36aa35 commit fa23308

File tree

9 files changed

+34
-33
lines changed

9 files changed

+34
-33
lines changed

src/bench/checkblock.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
3434
char a = '\0';
3535
stream.write(&a, 1); // Prevent compaction
3636

37-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
37+
ArgsManager bench_args;
38+
const auto chainParams = CreateChainParams(bench_args, CBaseChainParams::MAIN);
3839

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

src/chainparams.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,22 +485,22 @@ const CChainParams &Params() {
485485
return *globalChainParams;
486486
}
487487

488-
std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain)
488+
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain)
489489
{
490490
if (chain == CBaseChainParams::MAIN) {
491491
return std::unique_ptr<CChainParams>(new CMainParams());
492492
} else if (chain == CBaseChainParams::TESTNET) {
493493
return std::unique_ptr<CChainParams>(new CTestNetParams());
494494
} else if (chain == CBaseChainParams::SIGNET) {
495-
return std::unique_ptr<CChainParams>(new SigNetParams(gArgs));
495+
return std::unique_ptr<CChainParams>(new SigNetParams(args));
496496
} else if (chain == CBaseChainParams::REGTEST) {
497-
return std::unique_ptr<CChainParams>(new CRegTestParams(gArgs));
497+
return std::unique_ptr<CChainParams>(new CRegTestParams(args));
498498
}
499499
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
500500
}
501501

502502
void SelectParams(const std::string& network)
503503
{
504504
SelectBaseParams(network);
505-
globalChainParams = CreateChainParams(network);
505+
globalChainParams = CreateChainParams(gArgs, network);
506506
}

src/chainparams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class CChainParams
119119
* @returns a CChainParams* of the chosen chain.
120120
* @throws a std::runtime_error if the chain is not supported.
121121
*/
122-
std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain);
122+
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain);
123123

124124
/**
125125
* Return the currently selected parameters. This won't change after app

src/init.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ void SetupServerArgs(NodeContext& node)
380380
const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
381381
const auto signetBaseParams = CreateBaseChainParams(CBaseChainParams::SIGNET);
382382
const auto regtestBaseParams = CreateBaseChainParams(CBaseChainParams::REGTEST);
383-
const auto defaultChainParams = CreateChainParams(CBaseChainParams::MAIN);
384-
const auto testnetChainParams = CreateChainParams(CBaseChainParams::TESTNET);
385-
const auto signetChainParams = CreateChainParams(CBaseChainParams::SIGNET);
386-
const auto regtestChainParams = CreateChainParams(CBaseChainParams::REGTEST);
383+
const auto defaultChainParams = CreateChainParams(argsman, CBaseChainParams::MAIN);
384+
const auto testnetChainParams = CreateChainParams(argsman, CBaseChainParams::TESTNET);
385+
const auto signetChainParams = CreateChainParams(argsman, CBaseChainParams::SIGNET);
386+
const auto regtestChainParams = CreateChainParams(argsman, CBaseChainParams::REGTEST);
387387

388388
// Hidden Options
389389
std::vector<std::string> hidden_args = {

src/qt/paymentserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ void PaymentServer::ipcParseCommandLine(int argc, char* argv[])
9494
SendCoinsRecipient r;
9595
if (GUIUtil::parseBitcoinURI(arg, &r) && !r.address.isEmpty())
9696
{
97-
auto tempChainParams = CreateChainParams(CBaseChainParams::MAIN);
97+
auto tempChainParams = CreateChainParams(gArgs, CBaseChainParams::MAIN);
9898

9999
if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) {
100100
SelectParams(CBaseChainParams::MAIN);
101101
} else {
102-
tempChainParams = CreateChainParams(CBaseChainParams::TESTNET);
102+
tempChainParams = CreateChainParams(gArgs, CBaseChainParams::TESTNET);
103103
if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) {
104104
SelectParams(CBaseChainParams::TESTNET);
105105
}

src/test/miner_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
198198
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
199199
{
200200
// Note that by default, these tests run with size accounting enabled.
201-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
201+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
202202
const CChainParams& chainparams = *chainParams;
203203
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
204204
std::unique_ptr<CBlockTemplate> pblocktemplate;

src/test/pow_tests.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
1414
/* Test calculation of next difficulty target with no constraints applying */
1515
BOOST_AUTO_TEST_CASE(get_next_work)
1616
{
17-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
17+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
1818
int64_t nLastRetargetTime = 1261130161; // Block #30240
1919
CBlockIndex pindexLast;
2020
pindexLast.nHeight = 32255;
@@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(get_next_work)
2626
/* Test the constraint on the upper bound for next work */
2727
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
2828
{
29-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
29+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
3030
int64_t nLastRetargetTime = 1231006505; // Block #0
3131
CBlockIndex pindexLast;
3232
pindexLast.nHeight = 2015;
@@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
3838
/* Test the constraint on the lower bound for actual time taken */
3939
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
4040
{
41-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
41+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
4242
int64_t nLastRetargetTime = 1279008237; // Block #66528
4343
CBlockIndex pindexLast;
4444
pindexLast.nHeight = 68543;
@@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
5050
/* Test the constraint on the upper bound for actual time taken */
5151
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
5252
{
53-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
53+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
5454
int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
5555
CBlockIndex pindexLast;
5656
pindexLast.nHeight = 46367;
@@ -61,7 +61,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
6161

6262
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
6363
{
64-
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
64+
const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
6565
uint256 hash;
6666
unsigned int nBits;
6767
nBits = UintToArith256(consensus.powLimit).GetCompact(true);
@@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
7171

7272
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
7373
{
74-
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
74+
const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
7575
uint256 hash;
7676
unsigned int nBits = ~0x00800000;
7777
hash.SetHex("0x1");
@@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
8080

8181
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
8282
{
83-
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
83+
const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
8484
uint256 hash;
8585
unsigned int nBits;
8686
arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
@@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
9292

9393
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
9494
{
95-
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
95+
const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
9696
uint256 hash;
9797
unsigned int nBits;
9898
arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
@@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
104104

105105
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
106106
{
107-
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
107+
const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
108108
uint256 hash;
109109
unsigned int nBits;
110110
arith_uint256 hash_arith{0};
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
115115

116116
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
117117
{
118-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
118+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
119119
std::vector<CBlockIndex> blocks(10000);
120120
for (int i = 0; i < 10000; i++) {
121121
blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
@@ -135,9 +135,9 @@ BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
135135
}
136136
}
137137

138-
void sanity_check_chainparams(std::string chainName)
138+
void sanity_check_chainparams(const ArgsManager& args, std::string chainName)
139139
{
140-
const auto chainParams = CreateChainParams(chainName);
140+
const auto chainParams = CreateChainParams(args, chainName);
141141
const auto consensus = chainParams->GetConsensus();
142142

143143
// hash genesis is correct
@@ -164,22 +164,22 @@ void sanity_check_chainparams(std::string chainName)
164164

165165
BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity)
166166
{
167-
sanity_check_chainparams(CBaseChainParams::MAIN);
167+
sanity_check_chainparams(*m_node.args, CBaseChainParams::MAIN);
168168
}
169169

170170
BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity)
171171
{
172-
sanity_check_chainparams(CBaseChainParams::REGTEST);
172+
sanity_check_chainparams(*m_node.args, CBaseChainParams::REGTEST);
173173
}
174174

175175
BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity)
176176
{
177-
sanity_check_chainparams(CBaseChainParams::TESTNET);
177+
sanity_check_chainparams(*m_node.args, CBaseChainParams::TESTNET);
178178
}
179179

180180
BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity)
181181
{
182-
sanity_check_chainparams(CBaseChainParams::SIGNET);
182+
sanity_check_chainparams(*m_node.args, CBaseChainParams::SIGNET);
183183
}
184184

185185
BOOST_AUTO_TEST_SUITE_END()

src/test/validation_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval)
3939

4040
BOOST_AUTO_TEST_CASE(block_subsidy_test)
4141
{
42-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
42+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
4343
TestBlockSubsidyHalvings(chainParams->GetConsensus()); // As in main
4444
TestBlockSubsidyHalvings(150); // As in regtest
4545
TestBlockSubsidyHalvings(1000); // Just another interval
4646
}
4747

4848
BOOST_AUTO_TEST_CASE(subsidy_limit_test)
4949
{
50-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
50+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
5151
CAmount nSum = 0;
5252
for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) {
5353
CAmount nSubsidy = GetBlockSubsidy(nHeight, chainParams->GetConsensus());

src/test/versionbits_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
223223
}
224224

225225
// Sanity checks of version bit deployments
226-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
226+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
227227
const Consensus::Params &mainnetParams = chainParams->GetConsensus();
228228
for (int i=0; i<(int) Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
229229
uint32_t bitmask = VersionBitsMask(mainnetParams, static_cast<Consensus::DeploymentPos>(i));
@@ -250,7 +250,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
250250
{
251251
// Check that ComputeBlockVersion will set the appropriate bit correctly
252252
// on mainnet.
253-
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
253+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
254254
const Consensus::Params &mainnetParams = chainParams->GetConsensus();
255255

256256
// Use the TESTDUMMY deployment for testing purposes.

0 commit comments

Comments
 (0)