Skip to content

Commit 4dac24d

Browse files
author
MarcoFalke
committed
Merge #13311: Don't edit Chainparams after initialization
6fa901f Don't edit Chainparams after initialization (Jorge Timón) 980b38f MOVEONLY: Move versionbits info out of versionbits.o (Jorge Timón) Pull request description: This encapsulates the "-vbparams" option, which is only meant for regtest, directly on CRegTestParams. This is a refactor and doesn't change functionality. Related to bitcoin/bitcoin#8994 Tree-SHA512: 79771d729a63a720e743a9c77d5e2d80369f072d66202a43c1304e83a7d0ef7c6103d4968a03aea9666cc89a7203c618da972124a677b38cfe62ddaeb28f9f5d
2 parents 990fc0d + 6fa901f commit 4dac24d

12 files changed

+101
-83
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ BITCOIN_CORE_H = \
187187
validation.h \
188188
validationinterface.h \
189189
versionbits.h \
190+
versionbitsinfo.h \
190191
walletinitinterface.h \
191192
wallet/coincontrol.h \
192193
wallet/crypter.h \
@@ -400,6 +401,7 @@ libbitcoin_common_a_SOURCES = \
400401
script/ismine.cpp \
401402
script/sign.cpp \
402403
script/standard.cpp \
404+
versionbitsinfo.cpp \
403405
warnings.cpp \
404406
$(BITCOIN_CORE_H)
405407

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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ void SetupServerArgs()
445445
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);
446446
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);
447447
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);
448-
gArgs.AddArg("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)", true, OptionsCategory::DEBUG_TEST);
449448
gArgs.AddArg("-addrmantest", "Allows to test address relay on localhost", true, OptionsCategory::DEBUG_TEST);
450449
gArgs.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
451450
"If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + ListLogCategories() + ".", false, OptionsCategory::DEBUG_TEST);
@@ -1101,39 +1100,6 @@ bool AppInitParameterInteraction()
11011100
fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end());
11021101
}
11031102

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

src/rpc/blockchain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <utilstrencodings.h>
3131
#include <hash.h>
3232
#include <validationinterface.h>
33+
#include <versionbitsinfo.h>
3334
#include <warnings.h>
3435

3536
#include <assert.h>

src/rpc/mining.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <utilstrencodings.h>
2525
#include <validation.h>
2626
#include <validationinterface.h>
27+
#include <versionbitsinfo.h>
2728
#include <warnings.h>
2829

2930
#include <memory>

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;

src/versionbits.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@
55
#include <versionbits.h>
66
#include <consensus/params.h>
77

8-
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
9-
{
10-
/*.name =*/ "testdummy",
11-
/*.gbt_force =*/ true,
12-
},
13-
{
14-
/*.name =*/ "csv",
15-
/*.gbt_force =*/ true,
16-
},
17-
{
18-
/*.name =*/ "segwit",
19-
/*.gbt_force =*/ true,
20-
}
21-
};
22-
238
ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const
249
{
2510
int nPeriod = Period(params);

src/versionbits.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ enum class ThresholdState {
3030
// will either be nullptr or a block with (height + 1) % Period() == 0.
3131
typedef std::map<const CBlockIndex*, ThresholdState> ThresholdConditionCache;
3232

33-
struct VBDeploymentInfo {
34-
/** Deployment name */
35-
const char *name;
36-
/** Whether GBT clients can safely ignore this rule in simplified usage */
37-
bool gbt_force;
38-
};
39-
4033
struct BIP9Stats {
4134
int period;
4235
int threshold;
@@ -45,8 +38,6 @@ struct BIP9Stats {
4538
bool possible;
4639
};
4740

48-
extern const struct VBDeploymentInfo VersionBitsDeploymentInfo[];
49-
5041
/**
5142
* Abstract class that implements BIP9-style threshold logic, and caches results.
5243
*/

0 commit comments

Comments
 (0)