Skip to content

Commit 84b8578

Browse files
dongcarlTheCharlatan
authored andcommitted
Decouple RegTestChainParams from ArgsManager
RegTest chain params can now be initialized by configuring a RegTestOptions struct, or with ArgsManager. This offers an interface for creating RegTestChainParams without a gArgs object.
1 parent 76cd4e7 commit 84b8578

File tree

4 files changed

+89
-30
lines changed

4 files changed

+89
-30
lines changed

src/chainparams.cpp

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& option
399399
class CRegTestParams : public CChainParams
400400
{
401401
public:
402-
explicit CRegTestParams(const ArgsManager& args) {
402+
explicit CRegTestParams(const RegTestOptions& opts)
403+
{
403404
strNetworkID = CBaseChainParams::REGTEST;
404405
consensus.signet_blocks = false;
405406
consensus.signet_challenge.clear();
@@ -437,11 +438,33 @@ class CRegTestParams : public CChainParams
437438
pchMessageStart[2] = 0xb5;
438439
pchMessageStart[3] = 0xda;
439440
nDefaultPort = 18444;
440-
nPruneAfterHeight = args.GetBoolArg("-fastprune", false) ? 100 : 1000;
441+
nPruneAfterHeight = opts.fastprune ? 100 : 1000;
441442
m_assumed_blockchain_size = 0;
442443
m_assumed_chain_state_size = 0;
443444

444-
UpdateActivationParametersFromArgs(args);
445+
for (const auto& [dep, height] : opts.activation_heights) {
446+
switch (dep) {
447+
case Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT:
448+
consensus.SegwitHeight = int{height};
449+
break;
450+
case Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB:
451+
consensus.BIP34Height = int{height};
452+
break;
453+
case Consensus::BuriedDeployment::DEPLOYMENT_DERSIG:
454+
consensus.BIP66Height = int{height};
455+
break;
456+
case Consensus::BuriedDeployment::DEPLOYMENT_CLTV:
457+
consensus.BIP65Height = int{height};
458+
break;
459+
case Consensus::BuriedDeployment::DEPLOYMENT_CSV:
460+
consensus.CSVHeight = int{height};
461+
break;
462+
}
463+
}
464+
465+
for (const auto& [deployment_pos, version_bits_params] : opts.version_bits_parameters) {
466+
UpdateVersionBitsParameters(deployment_pos, version_bits_params.start_time, version_bits_params.timeout, version_bits_params.min_activation_height);
467+
}
445468

446469
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
447470
consensus.hashGenesisBlock = genesis.GetHash();
@@ -498,41 +521,31 @@ class CRegTestParams : public CChainParams
498521
consensus.vDeployments[d].nTimeout = nTimeout;
499522
consensus.vDeployments[d].min_activation_height = min_activation_height;
500523
}
501-
void UpdateActivationParametersFromArgs(const ArgsManager& args);
502524
};
503525

504-
static void MaybeUpdateHeights(const ArgsManager& args, Consensus::Params& consensus)
526+
void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options)
505527
{
528+
if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
529+
506530
for (const std::string& arg : args.GetArgs("-testactivationheight")) {
507531
const auto found{arg.find('@')};
508532
if (found == std::string::npos) {
509533
throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
510534
}
511-
const auto name{arg.substr(0, found)};
535+
512536
const auto value{arg.substr(found + 1)};
513537
int32_t height;
514538
if (!ParseInt32(value, &height) || height < 0 || height >= std::numeric_limits<int>::max()) {
515539
throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
516540
}
517-
if (name == "segwit") {
518-
consensus.SegwitHeight = int{height};
519-
} else if (name == "bip34") {
520-
consensus.BIP34Height = int{height};
521-
} else if (name == "dersig") {
522-
consensus.BIP66Height = int{height};
523-
} else if (name == "cltv") {
524-
consensus.BIP65Height = int{height};
525-
} else if (name == "csv") {
526-
consensus.CSVHeight = int{height};
541+
542+
const auto deployment_name{arg.substr(0, found)};
543+
if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
544+
options.activation_heights[*buried_deployment] = height;
527545
} else {
528546
throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
529547
}
530548
}
531-
}
532-
533-
void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
534-
{
535-
MaybeUpdateHeights(args, consensus);
536549

537550
if (!args.IsArgSet("-vbparams")) return;
538551

@@ -541,23 +554,26 @@ void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
541554
if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
542555
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
543556
}
544-
int64_t nStartTime, nTimeout;
545-
int min_activation_height = 0;
546-
if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
557+
CChainParams::VersionBitsParameters vbparams{};
558+
if (!ParseInt64(vDeploymentParams[1], &vbparams.start_time)) {
547559
throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
548560
}
549-
if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
561+
if (!ParseInt64(vDeploymentParams[2], &vbparams.timeout)) {
550562
throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
551563
}
552-
if (vDeploymentParams.size() >= 4 && !ParseInt32(vDeploymentParams[3], &min_activation_height)) {
553-
throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
564+
if (vDeploymentParams.size() >= 4) {
565+
if (!ParseInt32(vDeploymentParams[3], &vbparams.min_activation_height)) {
566+
throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
567+
}
568+
} else {
569+
vbparams.min_activation_height = 0;
554570
}
555571
bool found = false;
556572
for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
557573
if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
558-
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout, min_activation_height);
574+
options.version_bits_parameters[Consensus::DeploymentPos(j)] = vbparams;
559575
found = true;
560-
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld, min_activation_height=%d\n", vDeploymentParams[0], nStartTime, nTimeout, min_activation_height);
576+
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld, min_activation_height=%d\n", vDeploymentParams[0], vbparams.start_time, vbparams.timeout, vbparams.min_activation_height);
561577
break;
562578
}
563579
}
@@ -585,7 +601,9 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
585601
ReadSigNetArgs(args, opts);
586602
return std::make_unique<const SigNetParams>(opts);
587603
} else if (chain == CBaseChainParams::REGTEST) {
588-
return std::unique_ptr<CChainParams>(new CRegTestParams(args));
604+
auto opts = CChainParams::RegTestOptions{};
605+
ReadRegTestArgs(args, opts);
606+
return std::make_unique<const CRegTestParams>(opts);
589607
}
590608
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
591609
}

src/chainparams.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#include <protocol.h>
1414
#include <util/hash_type.h>
1515

16+
#include <cstdint>
1617
#include <memory>
1718
#include <string>
19+
#include <unordered_map>
1820
#include <vector>
1921

2022
typedef std::map<int, uint256> MapCheckpoints;
@@ -131,6 +133,24 @@ class CChainParams
131133
std::optional<std::vector<std::string>> seeds{};
132134
};
133135

136+
/**
137+
* VersionBitsParameters holds activation parameters
138+
*/
139+
struct VersionBitsParameters {
140+
int64_t start_time;
141+
int64_t timeout;
142+
int min_activation_height;
143+
};
144+
145+
/**
146+
* RegTestOptions holds configurations for creating a regtest CChainParams.
147+
*/
148+
struct RegTestOptions {
149+
std::unordered_map<Consensus::DeploymentPos, VersionBitsParameters> version_bits_parameters{};
150+
std::unordered_map<Consensus::BuriedDeployment, int> activation_heights{};
151+
bool fastprune{false};
152+
};
153+
134154
protected:
135155
CChainParams() {}
136156

src/deploymentinfo.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <consensus/params.h>
88

9+
#include <string_view>
10+
911
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
1012
{
1113
/*.name =*/ "testdummy",
@@ -34,3 +36,19 @@ std::string DeploymentName(Consensus::BuriedDeployment dep)
3436
} // no default case, so the compiler can warn about missing cases
3537
return "";
3638
}
39+
40+
std::optional<Consensus::BuriedDeployment> GetBuriedDeployment(const std::string_view name)
41+
{
42+
if (name == "segwit") {
43+
return Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT;
44+
} else if (name == "bip34") {
45+
return Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB;
46+
} else if (name == "dersig") {
47+
return Consensus::BuriedDeployment::DEPLOYMENT_DERSIG;
48+
} else if (name == "cltv") {
49+
return Consensus::BuriedDeployment::DEPLOYMENT_CLTV;
50+
} else if (name == "csv") {
51+
return Consensus::BuriedDeployment::DEPLOYMENT_CSV;
52+
}
53+
return std::nullopt;
54+
}

src/deploymentinfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <consensus/params.h>
99

10+
#include <optional>
1011
#include <string>
1112

1213
struct VBDeploymentInfo {
@@ -26,4 +27,6 @@ inline std::string DeploymentName(Consensus::DeploymentPos pos)
2627
return VersionBitsDeploymentInfo[pos].name;
2728
}
2829

30+
std::optional<Consensus::BuriedDeployment> GetBuriedDeployment(const std::string_view deployment_name);
31+
2932
#endif // BITCOIN_DEPLOYMENTINFO_H

0 commit comments

Comments
 (0)