Skip to content

Commit a31ab0e

Browse files
committed
Merge #427: [0.17] Don't edit Chainparams after initialization
06f1b42 Don't edit Chainparams after initialization (Jorge Timón) 875a47c MOVEONLY: Move versionbits info out of versionbits.o (Jorge Timón) Pull request description: This is a backport of bitcoin/bitcoin#13311 since it's already merged, merging this in elements-0.17 should at least make the next rebase slightly easier. And since we're heavily touching chainparams in elements, I think it's a good thing. Tree-SHA512: 177f6396a34ecf87b609b39a108cc2b1e5b49bd8d12b705a4ae0241c7a5007db754ff01d6d045ee1e6a219ec6c6767663cd98558752c83961c4aff3378aef5a2
2 parents e1ed37e + 06f1b42 commit a31ab0e

File tree

12 files changed

+101
-84
lines changed

12 files changed

+101
-84
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ BITCOIN_CORE_H = \
186186
validation.h \
187187
validationinterface.h \
188188
versionbits.h \
189+
versionbitsinfo.h \
189190
walletinitinterface.h \
190191
wallet/coincontrol.h \
191192
wallet/crypter.h \
@@ -392,6 +393,7 @@ libbitcoin_common_a_SOURCES = \
392393
script/ismine.cpp \
393394
script/sign.cpp \
394395
script/standard.cpp \
396+
versionbitsinfo.cpp \
395397
warnings.cpp \
396398
$(BITCOIN_CORE_H)
397399

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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,6 @@ void SetupServerArgs()
463463
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);
464464
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);
465465
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);
466-
gArgs.AddArg("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)", true, OptionsCategory::DEBUG_TEST);
467466
gArgs.AddArg("-addrmantest", "Allows to test address relay on localhost", true, OptionsCategory::DEBUG_TEST);
468467
gArgs.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
469468
"If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + ListLogCategories() + ".", false, OptionsCategory::DEBUG_TEST);
@@ -1139,39 +1138,6 @@ bool AppInitParameterInteraction()
11391138
fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end());
11401139
}
11411140

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

src/rpc/blockchain.cpp

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

3435
#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 <util.h>
2525
#include <utilstrencodings.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 <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;

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)