Skip to content

Commit 6fa901f

Browse files
committed
Don't edit Chainparams after initialization
1 parent 980b38f commit 6fa901f

File tree

5 files changed

+58
-60
lines changed

5 files changed

+58
-60
lines changed

src/chainparams.cpp

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
#include <tinyformat.h>
1111
#include <util.h>
1212
#include <utilstrencodings.h>
13+
#include <versionbitsinfo.h>
1314

1415
#include <assert.h>
1516

17+
#include <boost/algorithm/string/classification.hpp>
18+
#include <boost/algorithm/string/split.hpp>
19+
1620
static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
1721
{
1822
CMutableTransaction txNew;
@@ -52,12 +56,6 @@ static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits
5256
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
5357
}
5458

55-
void CChainParams::UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
56-
{
57-
consensus.vDeployments[d].nStartTime = nStartTime;
58-
consensus.vDeployments[d].nTimeout = nTimeout;
59-
}
60-
6159
/**
6260
* Main network
6361
*/
@@ -270,7 +268,7 @@ class CTestNetParams : public CChainParams {
270268
*/
271269
class CRegTestParams : public CChainParams {
272270
public:
273-
CRegTestParams() {
271+
explicit CRegTestParams(const ArgsManager& args) {
274272
strNetworkID = "regtest";
275273
consensus.nSubsidyHalvingInterval = 150;
276274
consensus.BIP16Exception = uint256();
@@ -308,6 +306,8 @@ class CRegTestParams : public CChainParams {
308306
nDefaultPort = 18444;
309307
nPruneAfterHeight = 1000;
310308

309+
UpdateVersionBitsParametersFromArgs(args);
310+
311311
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
312312
consensus.hashGenesisBlock = genesis.GetHash();
313313
assert(consensus.hashGenesisBlock == uint256S("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
@@ -343,23 +343,65 @@ class CRegTestParams : public CChainParams {
343343
/* enable fallback fee on regtest */
344344
m_fallback_fee_enabled = true;
345345
}
346+
347+
/**
348+
* Allows modifying the Version Bits regtest parameters.
349+
*/
350+
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
351+
{
352+
consensus.vDeployments[d].nStartTime = nStartTime;
353+
consensus.vDeployments[d].nTimeout = nTimeout;
354+
}
355+
void UpdateVersionBitsParametersFromArgs(const ArgsManager& args);
346356
};
347357

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

350392
const CChainParams &Params() {
351393
assert(globalChainParams);
352394
return *globalChainParams;
353395
}
354396

355-
std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain)
397+
std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain)
356398
{
357399
if (chain == CBaseChainParams::MAIN)
358400
return std::unique_ptr<CChainParams>(new CMainParams());
359401
else if (chain == CBaseChainParams::TESTNET)
360402
return std::unique_ptr<CChainParams>(new CTestNetParams());
361403
else if (chain == CBaseChainParams::REGTEST)
362-
return std::unique_ptr<CChainParams>(new CRegTestParams());
404+
return std::unique_ptr<CChainParams>(new CRegTestParams(gArgs));
363405
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
364406
}
365407

@@ -368,8 +410,3 @@ void SelectParams(const std::string& network)
368410
SelectBaseParams(network);
369411
globalChainParams = CreateChainParams(network);
370412
}
371-
372-
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
373-
{
374-
globalChainParams->UpdateVersionBitsParameters(d, nStartTime, nTimeout);
375-
}

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>
@@ -446,7 +445,6 @@ void SetupServerArgs()
446445
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);
447446
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);
448447
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);
449-
gArgs.AddArg("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)", true, OptionsCategory::DEBUG_TEST);
450448
gArgs.AddArg("-addrmantest", "Allows to test address relay on localhost", true, OptionsCategory::DEBUG_TEST);
451449
gArgs.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
452450
"If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + ListLogCategories() + ".", false, OptionsCategory::DEBUG_TEST);
@@ -1102,39 +1100,6 @@ bool AppInitParameterInteraction()
11021100
fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end());
11031101
}
11041102

1105-
if (gArgs.IsArgSet("-vbparams")) {
1106-
// Allow overriding version bits parameters for testing
1107-
if (!chainparams.MineBlocksOnDemand()) {
1108-
return InitError("Version bits parameters may only be overridden on regtest.");
1109-
}
1110-
for (const std::string& strDeployment : gArgs.GetArgs("-vbparams")) {
1111-
std::vector<std::string> vDeploymentParams;
1112-
boost::split(vDeploymentParams, strDeployment, boost::is_any_of(":"));
1113-
if (vDeploymentParams.size() != 3) {
1114-
return InitError("Version bits parameters malformed, expecting deployment:start:end");
1115-
}
1116-
int64_t nStartTime, nTimeout;
1117-
if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
1118-
return InitError(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
1119-
}
1120-
if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
1121-
return InitError(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
1122-
}
1123-
bool found = false;
1124-
for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j)
1125-
{
1126-
if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) {
1127-
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
1128-
found = true;
1129-
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
1130-
break;
1131-
}
1132-
}
1133-
if (!found) {
1134-
return InitError(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
1135-
}
1136-
}
1137-
}
11381103
return true;
11391104
}
11401105

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 <miner.h>
@@ -58,6 +59,9 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
5859
InitSignatureCache();
5960
InitScriptExecutionCache();
6061
fCheckBlockIndex = true;
62+
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
63+
// TODO: fix the code to support SegWit blocks.
64+
gArgs.ForceSetArg("-vbparams", strprintf("segwit:0:%d", (int64_t)Consensus::BIP9Deployment::NO_TIMEOUT));
6165
SelectParams(chainName);
6266
noui_connect();
6367
}
@@ -128,9 +132,6 @@ TestingSetup::~TestingSetup()
128132

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

0 commit comments

Comments
 (0)