Skip to content

Commit b00120a

Browse files
committed
Replace BIP9 with Modern Soft-fork Activation
1 parent 45dde9c commit b00120a

File tree

10 files changed

+608
-590
lines changed

10 files changed

+608
-590
lines changed

src/chainparams.cpp

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <tinyformat.h>
1111
#include <util/system.h>
1212
#include <util/strencodings.h>
13+
#include <versionbits.h>
1314
#include <versionbitsinfo.h>
1415

1516
#include <assert.h>
@@ -56,12 +57,30 @@ static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits
5657
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
5758
}
5859

60+
/**
61+
* Add a UASF-able deployment
62+
*/
63+
template<int bit, int signal_height, uint16_t signal_periods=26, uint16_t quiet_periods=13, uint16_t uasf_periods=52, uint16_t period=2016, uint16_t threshold=1916>
64+
inline void SetDeployment(Consensus::Params& params, Consensus::DeploymentPos pos, const ArgsManager& args, bool force_uasf=false)
65+
{
66+
static_assert(signal_periods >= 0 && quiet_periods >= 0 && uasf_periods >= 0, "Must specify signal/quiet/uasf periods");
67+
68+
bool uasf = force_uasf;
69+
for (const std::string& strDeployment : args.GetArgs("-uasf")) {
70+
if (strDeployment == VersionBitsDeploymentInfo[(int)pos].name) {
71+
uasf = true;
72+
break;
73+
}
74+
}
75+
params.vDeployments[(int)pos] = Deployment<bit,signal_height,signal_periods,quiet_periods,uasf_periods,period,threshold>(uasf);
76+
}
77+
5978
/**
6079
* Main network
6180
*/
6281
class CMainParams : public CChainParams {
6382
public:
64-
CMainParams() {
83+
explicit CMainParams(const ArgsManager& args) {
6584
strNetworkID = CBaseChainParams::MAIN;
6685
consensus.nSubsidyHalvingInterval = 210000;
6786
consensus.BIP16Exception = uint256S("0x00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22");
@@ -71,17 +90,15 @@ class CMainParams : public CChainParams {
7190
consensus.BIP66Height = 363725; // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
7291
consensus.CSVHeight = 419328; // 000000000000000004a1b34462cb8aeebd5799177f7a29cf28f2d1961716b5b5
7392
consensus.SegwitHeight = 481824; // 0000000000000000001c8018d9cb3b742ef25114f27563e3fc4a1902167f9893
74-
consensus.MinBIP9WarningHeight = 483840; // segwit activation height + miner confirmation window
93+
consensus.MinModernDeploymentWarningHeight = 483840; // segwit activation height + miner confirmation window
7594
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
7695
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
7796
consensus.nPowTargetSpacing = 10 * 60;
7897
consensus.fPowAllowMinDifficultyBlocks = false;
7998
consensus.fPowNoRetargeting = false;
8099
consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016
81100
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
82-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
83-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
84-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
101+
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY] = DeploymentDisabled<28>();
85102

86103
// The best chain should have at least this much work.
87104
consensus.nMinimumChainWork = uint256S("0x00000000000000000000000000000000000000000e1ab5ec9348e9f4b8eb8154");
@@ -169,7 +186,7 @@ class CMainParams : public CChainParams {
169186
*/
170187
class CTestNetParams : public CChainParams {
171188
public:
172-
CTestNetParams() {
189+
explicit CTestNetParams(const ArgsManager& args) {
173190
strNetworkID = CBaseChainParams::TESTNET;
174191
consensus.nSubsidyHalvingInterval = 210000;
175192
consensus.BIP16Exception = uint256S("0x00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105");
@@ -179,17 +196,15 @@ class CTestNetParams : public CChainParams {
179196
consensus.BIP66Height = 330776; // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
180197
consensus.CSVHeight = 770112; // 00000000025e930139bac5c6c31a403776da130831ab85be56578f3fa75369bb
181198
consensus.SegwitHeight = 834624; // 00000000002b980fcd729daaa248fd9316a5200e9b367f4ff2c42453e84201ca
182-
consensus.MinBIP9WarningHeight = 836640; // segwit activation height + miner confirmation window
199+
consensus.MinModernDeploymentWarningHeight = 836640; // segwit activation height + miner confirmation window
183200
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
184201
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
185202
consensus.nPowTargetSpacing = 10 * 60;
186203
consensus.fPowAllowMinDifficultyBlocks = true;
187204
consensus.fPowNoRetargeting = false;
188205
consensus.nRuleChangeActivationThreshold = 1512; // 75% for testchains
189206
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
190-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
191-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
192-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
207+
SetDeployment<28, 0>(consensus, Consensus::DEPLOYMENT_TESTDUMMY, args);
193208

194209
// The best chain should have at least this much work.
195210
consensus.nMinimumChainWork = uint256S("0x0000000000000000000000000000000000000000000001495c1d5a01e2af8a23");
@@ -264,17 +279,15 @@ class CRegTestParams : public CChainParams {
264279
consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in functional tests)
265280
consensus.CSVHeight = 432; // CSV activated on regtest (Used in rpc activation tests)
266281
consensus.SegwitHeight = 0; // SEGWIT is always activated on regtest unless overridden
267-
consensus.MinBIP9WarningHeight = 0;
282+
consensus.MinModernDeploymentWarningHeight = 0;
268283
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
269284
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
270285
consensus.nPowTargetSpacing = 10 * 60;
271286
consensus.fPowAllowMinDifficultyBlocks = true;
272287
consensus.fPowNoRetargeting = true;
273288
consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
274289
consensus.nMinerConfirmationWindow = 144; // Faster than normal for regtest (144 instead of 2016)
275-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
276-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 0;
277-
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
290+
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY] = DeploymentAlwaysSignal<28,144,108>();
278291

279292
// The best chain should have at least this much work.
280293
consensus.nMinimumChainWork = uint256S("0x00");
@@ -330,10 +343,14 @@ class CRegTestParams : public CChainParams {
330343
/**
331344
* Allows modifying the Version Bits regtest parameters.
332345
*/
333-
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
346+
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int signal_height, int16_t signal_periods)
334347
{
335-
consensus.vDeployments[d].nStartTime = nStartTime;
336-
consensus.vDeployments[d].nTimeout = nTimeout;
348+
consensus.vDeployments[d].signal_height = signal_height;
349+
consensus.vDeployments[d].signal_periods = signal_periods;
350+
consensus.vDeployments[d].quiet_periods = -1;
351+
consensus.vDeployments[d].uasf_periods = 0;
352+
consensus.vDeployments[d].threshold = consensus.nRuleChangeActivationThreshold;
353+
consensus.vDeployments[d].period = consensus.nMinerConfirmationWindow;
337354
}
338355
void UpdateActivationParametersFromArgs(const ArgsManager& args);
339356
};
@@ -359,19 +376,20 @@ void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
359376
if (vDeploymentParams.size() != 3) {
360377
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end");
361378
}
362-
int64_t nStartTime, nTimeout;
363-
if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
364-
throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
379+
int32_t signal_height;
380+
int32_t signal_periods;
381+
if (!ParseInt32(vDeploymentParams[1], &signal_height) || signal_height < 0) {
382+
throw std::runtime_error(strprintf("Invalid signal_height (%s)", vDeploymentParams[1]));
365383
}
366-
if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
367-
throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
384+
if (!ParseInt32(vDeploymentParams[2], &signal_periods) || signal_periods < 0 || signal_periods > 10000) {
385+
throw std::runtime_error(strprintf("Invalid signal_periods (%s)", vDeploymentParams[2]));
368386
}
369387
bool found = false;
370388
for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
371389
if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
372-
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
390+
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), signal_height, signal_periods);
373391
found = true;
374-
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
392+
LogPrintf("Setting version bits activation parameters for %s to signal_height=%ld, signal_periods=%ld\n", vDeploymentParams[0], signal_height, signal_periods);
375393
break;
376394
}
377395
}
@@ -391,9 +409,9 @@ const CChainParams &Params() {
391409
std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain)
392410
{
393411
if (chain == CBaseChainParams::MAIN)
394-
return std::unique_ptr<CChainParams>(new CMainParams());
412+
return std::unique_ptr<CChainParams>(new CMainParams(gArgs));
395413
else if (chain == CBaseChainParams::TESTNET)
396-
return std::unique_ptr<CChainParams>(new CTestNetParams());
414+
return std::unique_ptr<CChainParams>(new CTestNetParams(gArgs));
397415
else if (chain == CBaseChainParams::REGTEST)
398416
return std::unique_ptr<CChainParams>(new CRegTestParams(gArgs));
399417
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));

src/chainparamsbase.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ void SetupChainParamsBaseOptions()
2222
"This is intended for regression testing tools and app development. Equivalent to -chain=regtest.", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
2323
gArgs.AddArg("-segwitheight=<n>", "Set the activation height of segwit. -1 to disable. (regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
2424
gArgs.AddArg("-testnet", "Use the test chain. Equivalent to -chain=test.", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
25-
gArgs.AddArg("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
25+
gArgs.AddArg("-vbparams=deployment:start:signalblocks", "Use given start time and number of blocks to signal for for specified version bits deployment (regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
26+
gArgs.AddArg("-uasf=deployment", "Ensure mandatory activation of named deployment", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
2627
}
2728

2829
static std::unique_ptr<CBaseChainParams> globalChainBaseParams;

src/consensus/params.h

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,30 @@ enum DeploymentPos
2828
};
2929

3030
/**
31-
* Struct for each individual consensus rule change using BIP9.
31+
* Struct for each individual consensus rule change using a ModernDeployment
32+
* See Deployment<>() in versionbits.h for safe initialisation.
3233
*/
33-
struct BIP9Deployment {
34-
/** Bit position to select the particular bit in nVersion. */
35-
int bit;
36-
/** Start MedianTime for version bits miner confirmation. Can be a date in the past */
37-
int64_t nStartTime;
38-
/** Timeout/expiry MedianTime for the deployment attempt. */
39-
int64_t nTimeout;
34+
struct ModernDeployment {
35+
/** Start height for version bits miner confirmation. */
36+
int signal_height;
37+
38+
/** Negative value means phase does not end */
39+
int16_t signal_periods;
40+
int16_t quiet_periods;
41+
int16_t uasf_periods;
42+
43+
/** Length of each period (normally same as nMinerConfirmationWindow)
44+
*/
45+
uint16_t period;
46+
47+
/** Number of blocks signalling in a period to move to locked in */
48+
uint16_t threshold;
4049

41-
/** Constant for nTimeout very far in the future. */
42-
static constexpr int64_t NO_TIMEOUT = std::numeric_limits<int64_t>::max();
50+
/** Bit position to select the particular bit in nVersion. */
51+
uint8_t bit;
4352

44-
/** Special value for nStartTime indicating that the deployment is always active.
45-
* This is useful for testing, as it means tests don't need to deal with the activation
46-
* process (which takes at least 3 BIP9 intervals). Only tests that specifically test the
47-
* behaviour during activation cannot use this. */
48-
static constexpr int64_t ALWAYS_ACTIVE = -1;
53+
/** UASF phase enabled? */
54+
bool uasf_enabled;
4955
};
5056

5157
/**
@@ -69,17 +75,17 @@ struct Params {
6975
* Note that segwit v0 script rules are enforced on all blocks except the
7076
* BIP 16 exception blocks. */
7177
int SegwitHeight;
72-
/** Don't warn about unknown BIP 9 activations below this height.
78+
/** Don't warn about unknown ModernDeployment activations below this height.
7379
* This prevents us from warning about the CSV and segwit activations. */
74-
int MinBIP9WarningHeight;
80+
int MinModernDeploymentWarningHeight;
7581
/**
7682
* Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
77-
* (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
83+
* (nPowTargetTimespan / nPowTargetSpacing) which is also used for deployments.
7884
* Examples: 1916 for 95%, 1512 for testchains.
7985
*/
80-
uint32_t nRuleChangeActivationThreshold;
81-
uint32_t nMinerConfirmationWindow;
82-
BIP9Deployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS];
86+
uint16_t nRuleChangeActivationThreshold;
87+
uint16_t nMinerConfirmationWindow;
88+
ModernDeployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS];
8389
/** Proof of work parameters */
8490
uint256 powLimit;
8591
bool fPowAllowMinDifficultyBlocks;

0 commit comments

Comments
 (0)