Skip to content

Commit 06f1b42

Browse files
committed
Don't edit Chainparams after initialization
1 parent 875a47c commit 06f1b42

File tree

5 files changed

+58
-61
lines changed

5 files changed

+58
-61
lines changed

src/chainparams.cpp

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
#include <chainparams.h>
77
#include <consensus/merkle.h>
88

9+
#include <chainparamsseeds.h>
910
#include <tinyformat.h>
1011
#include <util.h>
1112
#include <utilstrencodings.h>
13+
#include <versionbitsinfo.h>
1214

1315
#include <assert.h>
1416

15-
#include <chainparamsseeds.h>
17+
#include <boost/algorithm/string/classification.hpp>
18+
#include <boost/algorithm/string/split.hpp>
1619

1720
static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
1821
{
@@ -53,12 +56,6 @@ static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits
5356
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
5457
}
5558

56-
void CChainParams::UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
57-
{
58-
consensus.vDeployments[d].nStartTime = nStartTime;
59-
consensus.vDeployments[d].nTimeout = nTimeout;
60-
}
61-
6259
/**
6360
* Main network
6461
*/
@@ -279,7 +276,7 @@ class CTestNetParams : public CChainParams {
279276
*/
280277
class CRegTestParams : public CChainParams {
281278
public:
282-
CRegTestParams() {
279+
explicit CRegTestParams(const ArgsManager& args) {
283280
strNetworkID = "regtest";
284281
consensus.nSubsidyHalvingInterval = 150;
285282
consensus.BIP16Exception = uint256();
@@ -317,6 +314,8 @@ class CRegTestParams : public CChainParams {
317314
nDefaultPort = 18444;
318315
nPruneAfterHeight = 1000;
319316

317+
UpdateVersionBitsParametersFromArgs(args);
318+
320319
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
321320
consensus.hashGenesisBlock = genesis.GetHash();
322321
assert(consensus.hashGenesisBlock == uint256S("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
@@ -352,23 +351,65 @@ class CRegTestParams : public CChainParams {
352351
/* enable fallback fee on regtest */
353352
m_fallback_fee_enabled = true;
354353
}
354+
355+
/**
356+
* Allows modifying the Version Bits regtest parameters.
357+
*/
358+
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
359+
{
360+
consensus.vDeployments[d].nStartTime = nStartTime;
361+
consensus.vDeployments[d].nTimeout = nTimeout;
362+
}
363+
void UpdateVersionBitsParametersFromArgs(const ArgsManager& args);
355364
};
356365

357-
static std::unique_ptr<CChainParams> globalChainParams;
366+
void CRegTestParams::UpdateVersionBitsParametersFromArgs(const ArgsManager& args)
367+
{
368+
if (!args.IsArgSet("-vbparams")) return;
369+
370+
for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
371+
std::vector<std::string> vDeploymentParams;
372+
boost::split(vDeploymentParams, strDeployment, boost::is_any_of(":"));
373+
if (vDeploymentParams.size() != 3) {
374+
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end");
375+
}
376+
int64_t nStartTime, nTimeout;
377+
if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
378+
throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
379+
}
380+
if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
381+
throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
382+
}
383+
bool found = false;
384+
for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
385+
if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
386+
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
387+
found = true;
388+
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
389+
break;
390+
}
391+
}
392+
if (!found) {
393+
throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
394+
}
395+
}
396+
}
397+
398+
static std::unique_ptr<const CChainParams> globalChainParams;
358399

359400
const CChainParams &Params() {
360401
assert(globalChainParams);
361402
return *globalChainParams;
362403
}
363404

364-
std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain)
405+
std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain)
365406
{
366407
if (chain == CBaseChainParams::MAIN)
367408
return std::unique_ptr<CChainParams>(new CMainParams());
368409
else if (chain == CBaseChainParams::TESTNET)
369410
return std::unique_ptr<CChainParams>(new CTestNetParams());
370411
else if (chain == CBaseChainParams::REGTEST)
371-
return std::unique_ptr<CChainParams>(new CRegTestParams());
412+
return std::unique_ptr<CChainParams>(new CRegTestParams(gArgs));
372413
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
373414
}
374415

@@ -377,8 +418,3 @@ void SelectParams(const std::string& network)
377418
SelectBaseParams(network);
378419
globalChainParams = CreateChainParams(network);
379420
}
380-
381-
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
382-
{
383-
globalChainParams->UpdateVersionBitsParameters(d, nStartTime, nTimeout);
384-
}

src/chainparams.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class CChainParams
8080
const std::vector<SeedSpec6>& FixedSeeds() const { return vFixedSeeds; }
8181
const CCheckpointData& Checkpoints() const { return checkpointData; }
8282
const ChainTxData& TxData() const { return chainTxData; }
83-
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout);
8483
protected:
8584
CChainParams() {}
8685

@@ -107,7 +106,7 @@ class CChainParams
107106
* @returns a CChainParams* of the chosen chain.
108107
* @throws a std::runtime_error if the chain is not supported.
109108
*/
110-
std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain);
109+
std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain);
111110

112111
/**
113112
* Return the currently selected parameters. This won't change after app
@@ -121,9 +120,4 @@ const CChainParams &Params();
121120
*/
122121
void SelectParams(const std::string& chain);
123122

124-
/**
125-
* Allows modifying the Version Bits regtest parameters.
126-
*/
127-
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout);
128-
129123
#endif // BITCOIN_CHAINPARAMS_H

src/chainparamsbase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void SetupChainParamsBaseOptions()
2020
gArgs.AddArg("-regtest", "Enter regression test mode, which uses a special chain in which blocks can be solved instantly. "
2121
"This is intended for regression testing tools and app development.", true, OptionsCategory::CHAINPARAMS);
2222
gArgs.AddArg("-testnet", "Use the test chain", false, OptionsCategory::CHAINPARAMS);
23+
gArgs.AddArg("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)", true, OptionsCategory::CHAINPARAMS);
2324
}
2425

2526
static std::unique_ptr<CBaseChainParams> globalChainBaseParams;

src/init.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include <util.h>
4545
#include <utilmoneystr.h>
4646
#include <validationinterface.h>
47-
#include <versionbitsinfo.h>
4847
#include <warnings.h>
4948
#include <walletinitinterface.h>
5049
#include <stdint.h>
@@ -464,7 +463,6 @@ void SetupServerArgs()
464463
gArgs.AddArg("-limitancestorsize=<n>", strprintf("Do not accept transactions whose size with all in-mempool ancestors exceeds <n> kilobytes (default: %u)", DEFAULT_ANCESTOR_SIZE_LIMIT), true, OptionsCategory::DEBUG_TEST);
465464
gArgs.AddArg("-limitdescendantcount=<n>", strprintf("Do not accept transactions if any ancestor would have <n> or more in-mempool descendants (default: %u)", DEFAULT_DESCENDANT_LIMIT), true, OptionsCategory::DEBUG_TEST);
466465
gArgs.AddArg("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT), true, OptionsCategory::DEBUG_TEST);
467-
gArgs.AddArg("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)", true, OptionsCategory::DEBUG_TEST);
468466
gArgs.AddArg("-addrmantest", "Allows to test address relay on localhost", true, OptionsCategory::DEBUG_TEST);
469467
gArgs.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
470468
"If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + ListLogCategories() + ".", false, OptionsCategory::DEBUG_TEST);
@@ -1140,39 +1138,6 @@ bool AppInitParameterInteraction()
11401138
fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end());
11411139
}
11421140

1143-
if (gArgs.IsArgSet("-vbparams")) {
1144-
// Allow overriding version bits parameters for testing
1145-
if (!chainparams.MineBlocksOnDemand()) {
1146-
return InitError("Version bits parameters may only be overridden on regtest.");
1147-
}
1148-
for (const std::string& strDeployment : gArgs.GetArgs("-vbparams")) {
1149-
std::vector<std::string> vDeploymentParams;
1150-
boost::split(vDeploymentParams, strDeployment, boost::is_any_of(":"));
1151-
if (vDeploymentParams.size() != 3) {
1152-
return InitError("Version bits parameters malformed, expecting deployment:start:end");
1153-
}
1154-
int64_t nStartTime, nTimeout;
1155-
if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
1156-
return InitError(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
1157-
}
1158-
if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
1159-
return InitError(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
1160-
}
1161-
bool found = false;
1162-
for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j)
1163-
{
1164-
if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) {
1165-
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
1166-
found = true;
1167-
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
1168-
break;
1169-
}
1170-
}
1171-
if (!found) {
1172-
return InitError(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
1173-
}
1174-
}
1175-
}
11761141
return true;
11771142
}
11781143

src/test/test_bitcoin.cpp

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

77
#include <chainparams.h>
88
#include <consensus/consensus.h>
9+
#include <consensus/params.h>
910
#include <consensus/validation.h>
1011
#include <crypto/sha256.h>
1112
#include <validation.h>
@@ -56,6 +57,9 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
5657
InitSignatureCache();
5758
InitScriptExecutionCache();
5859
fCheckBlockIndex = true;
60+
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
61+
// TODO: fix the code to support SegWit blocks.
62+
gArgs.ForceSetArg("-vbparams", strprintf("segwit:0:%d", (int64_t)Consensus::BIP9Deployment::NO_TIMEOUT));
5963
SelectParams(chainName);
6064
noui_connect();
6165
}
@@ -126,9 +130,6 @@ TestingSetup::~TestingSetup()
126130

127131
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
128132
{
129-
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
130-
// TODO: fix the code to support SegWit blocks.
131-
UpdateVersionBitsParameters(Consensus::DEPLOYMENT_SEGWIT, 0, Consensus::BIP9Deployment::NO_TIMEOUT);
132133
// Generate a 100-block chain:
133134
coinbaseKey.MakeNewKey(true);
134135
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;

0 commit comments

Comments
 (0)