mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
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.
This commit is contained in:
parent
76cd4e7c96
commit
84b85786f0
4 changed files with 89 additions and 30 deletions
|
@ -399,7 +399,8 @@ void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& option
|
||||||
class CRegTestParams : public CChainParams
|
class CRegTestParams : public CChainParams
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit CRegTestParams(const ArgsManager& args) {
|
explicit CRegTestParams(const RegTestOptions& opts)
|
||||||
|
{
|
||||||
strNetworkID = CBaseChainParams::REGTEST;
|
strNetworkID = CBaseChainParams::REGTEST;
|
||||||
consensus.signet_blocks = false;
|
consensus.signet_blocks = false;
|
||||||
consensus.signet_challenge.clear();
|
consensus.signet_challenge.clear();
|
||||||
|
@ -437,11 +438,33 @@ public:
|
||||||
pchMessageStart[2] = 0xb5;
|
pchMessageStart[2] = 0xb5;
|
||||||
pchMessageStart[3] = 0xda;
|
pchMessageStart[3] = 0xda;
|
||||||
nDefaultPort = 18444;
|
nDefaultPort = 18444;
|
||||||
nPruneAfterHeight = args.GetBoolArg("-fastprune", false) ? 100 : 1000;
|
nPruneAfterHeight = opts.fastprune ? 100 : 1000;
|
||||||
m_assumed_blockchain_size = 0;
|
m_assumed_blockchain_size = 0;
|
||||||
m_assumed_chain_state_size = 0;
|
m_assumed_chain_state_size = 0;
|
||||||
|
|
||||||
UpdateActivationParametersFromArgs(args);
|
for (const auto& [dep, height] : opts.activation_heights) {
|
||||||
|
switch (dep) {
|
||||||
|
case Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT:
|
||||||
|
consensus.SegwitHeight = int{height};
|
||||||
|
break;
|
||||||
|
case Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB:
|
||||||
|
consensus.BIP34Height = int{height};
|
||||||
|
break;
|
||||||
|
case Consensus::BuriedDeployment::DEPLOYMENT_DERSIG:
|
||||||
|
consensus.BIP66Height = int{height};
|
||||||
|
break;
|
||||||
|
case Consensus::BuriedDeployment::DEPLOYMENT_CLTV:
|
||||||
|
consensus.BIP65Height = int{height};
|
||||||
|
break;
|
||||||
|
case Consensus::BuriedDeployment::DEPLOYMENT_CSV:
|
||||||
|
consensus.CSVHeight = int{height};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& [deployment_pos, version_bits_params] : opts.version_bits_parameters) {
|
||||||
|
UpdateVersionBitsParameters(deployment_pos, version_bits_params.start_time, version_bits_params.timeout, version_bits_params.min_activation_height);
|
||||||
|
}
|
||||||
|
|
||||||
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
|
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
|
||||||
consensus.hashGenesisBlock = genesis.GetHash();
|
consensus.hashGenesisBlock = genesis.GetHash();
|
||||||
|
@ -498,41 +521,31 @@ public:
|
||||||
consensus.vDeployments[d].nTimeout = nTimeout;
|
consensus.vDeployments[d].nTimeout = nTimeout;
|
||||||
consensus.vDeployments[d].min_activation_height = min_activation_height;
|
consensus.vDeployments[d].min_activation_height = min_activation_height;
|
||||||
}
|
}
|
||||||
void UpdateActivationParametersFromArgs(const ArgsManager& args);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void MaybeUpdateHeights(const ArgsManager& args, Consensus::Params& consensus)
|
void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options)
|
||||||
{
|
{
|
||||||
|
if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
|
||||||
|
|
||||||
for (const std::string& arg : args.GetArgs("-testactivationheight")) {
|
for (const std::string& arg : args.GetArgs("-testactivationheight")) {
|
||||||
const auto found{arg.find('@')};
|
const auto found{arg.find('@')};
|
||||||
if (found == std::string::npos) {
|
if (found == std::string::npos) {
|
||||||
throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
|
throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
|
||||||
}
|
}
|
||||||
const auto name{arg.substr(0, found)};
|
|
||||||
const auto value{arg.substr(found + 1)};
|
const auto value{arg.substr(found + 1)};
|
||||||
int32_t height;
|
int32_t height;
|
||||||
if (!ParseInt32(value, &height) || height < 0 || height >= std::numeric_limits<int>::max()) {
|
if (!ParseInt32(value, &height) || height < 0 || height >= std::numeric_limits<int>::max()) {
|
||||||
throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
|
throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
|
||||||
}
|
}
|
||||||
if (name == "segwit") {
|
|
||||||
consensus.SegwitHeight = int{height};
|
const auto deployment_name{arg.substr(0, found)};
|
||||||
} else if (name == "bip34") {
|
if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
|
||||||
consensus.BIP34Height = int{height};
|
options.activation_heights[*buried_deployment] = height;
|
||||||
} else if (name == "dersig") {
|
|
||||||
consensus.BIP66Height = int{height};
|
|
||||||
} else if (name == "cltv") {
|
|
||||||
consensus.BIP65Height = int{height};
|
|
||||||
} else if (name == "csv") {
|
|
||||||
consensus.CSVHeight = int{height};
|
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
|
throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
|
|
||||||
{
|
|
||||||
MaybeUpdateHeights(args, consensus);
|
|
||||||
|
|
||||||
if (!args.IsArgSet("-vbparams")) return;
|
if (!args.IsArgSet("-vbparams")) return;
|
||||||
|
|
||||||
|
@ -541,23 +554,26 @@ void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
|
||||||
if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
|
if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
|
||||||
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
|
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
|
||||||
}
|
}
|
||||||
int64_t nStartTime, nTimeout;
|
CChainParams::VersionBitsParameters vbparams{};
|
||||||
int min_activation_height = 0;
|
if (!ParseInt64(vDeploymentParams[1], &vbparams.start_time)) {
|
||||||
if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
|
|
||||||
throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
|
throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
|
||||||
}
|
}
|
||||||
if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
|
if (!ParseInt64(vDeploymentParams[2], &vbparams.timeout)) {
|
||||||
throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
|
throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
|
||||||
}
|
}
|
||||||
if (vDeploymentParams.size() >= 4 && !ParseInt32(vDeploymentParams[3], &min_activation_height)) {
|
if (vDeploymentParams.size() >= 4) {
|
||||||
throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
|
if (!ParseInt32(vDeploymentParams[3], &vbparams.min_activation_height)) {
|
||||||
|
throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
vbparams.min_activation_height = 0;
|
||||||
}
|
}
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
|
for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
|
||||||
if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
|
if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
|
||||||
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout, min_activation_height);
|
options.version_bits_parameters[Consensus::DeploymentPos(j)] = vbparams;
|
||||||
found = true;
|
found = true;
|
||||||
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);
|
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);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -585,7 +601,9 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
|
||||||
ReadSigNetArgs(args, opts);
|
ReadSigNetArgs(args, opts);
|
||||||
return std::make_unique<const SigNetParams>(opts);
|
return std::make_unique<const SigNetParams>(opts);
|
||||||
} else if (chain == CBaseChainParams::REGTEST) {
|
} else if (chain == CBaseChainParams::REGTEST) {
|
||||||
return std::unique_ptr<CChainParams>(new CRegTestParams(args));
|
auto opts = CChainParams::RegTestOptions{};
|
||||||
|
ReadRegTestArgs(args, opts);
|
||||||
|
return std::make_unique<const CRegTestParams>(opts);
|
||||||
}
|
}
|
||||||
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
|
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,8 +13,10 @@
|
||||||
#include <protocol.h>
|
#include <protocol.h>
|
||||||
#include <util/hash_type.h>
|
#include <util/hash_type.h>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
typedef std::map<int, uint256> MapCheckpoints;
|
typedef std::map<int, uint256> MapCheckpoints;
|
||||||
|
@ -131,6 +133,24 @@ public:
|
||||||
std::optional<std::vector<std::string>> seeds{};
|
std::optional<std::vector<std::string>> seeds{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VersionBitsParameters holds activation parameters
|
||||||
|
*/
|
||||||
|
struct VersionBitsParameters {
|
||||||
|
int64_t start_time;
|
||||||
|
int64_t timeout;
|
||||||
|
int min_activation_height;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RegTestOptions holds configurations for creating a regtest CChainParams.
|
||||||
|
*/
|
||||||
|
struct RegTestOptions {
|
||||||
|
std::unordered_map<Consensus::DeploymentPos, VersionBitsParameters> version_bits_parameters{};
|
||||||
|
std::unordered_map<Consensus::BuriedDeployment, int> activation_heights{};
|
||||||
|
bool fastprune{false};
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
CChainParams() {}
|
CChainParams() {}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
#include <consensus/params.h>
|
#include <consensus/params.h>
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
|
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
|
||||||
{
|
{
|
||||||
/*.name =*/ "testdummy",
|
/*.name =*/ "testdummy",
|
||||||
|
@ -34,3 +36,19 @@ std::string DeploymentName(Consensus::BuriedDeployment dep)
|
||||||
} // no default case, so the compiler can warn about missing cases
|
} // no default case, so the compiler can warn about missing cases
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<Consensus::BuriedDeployment> GetBuriedDeployment(const std::string_view name)
|
||||||
|
{
|
||||||
|
if (name == "segwit") {
|
||||||
|
return Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT;
|
||||||
|
} else if (name == "bip34") {
|
||||||
|
return Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB;
|
||||||
|
} else if (name == "dersig") {
|
||||||
|
return Consensus::BuriedDeployment::DEPLOYMENT_DERSIG;
|
||||||
|
} else if (name == "cltv") {
|
||||||
|
return Consensus::BuriedDeployment::DEPLOYMENT_CLTV;
|
||||||
|
} else if (name == "csv") {
|
||||||
|
return Consensus::BuriedDeployment::DEPLOYMENT_CSV;
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <consensus/params.h>
|
#include <consensus/params.h>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct VBDeploymentInfo {
|
struct VBDeploymentInfo {
|
||||||
|
@ -26,4 +27,6 @@ inline std::string DeploymentName(Consensus::DeploymentPos pos)
|
||||||
return VersionBitsDeploymentInfo[pos].name;
|
return VersionBitsDeploymentInfo[pos].name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<Consensus::BuriedDeployment> GetBuriedDeployment(const std::string_view deployment_name);
|
||||||
|
|
||||||
#endif // BITCOIN_DEPLOYMENTINFO_H
|
#endif // BITCOIN_DEPLOYMENTINFO_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue