Skip to content

Commit 73d4a70

Browse files
committed
versionbits: Add support for delayed activation
1 parent 9e6b65f commit 73d4a70

File tree

8 files changed

+41
-10
lines changed

8 files changed

+41
-10
lines changed

src/chainparams.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ class CMainParams : public CChainParams {
8383
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
8484
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
8585
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
86+
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].min_activation_height = 0; // No activation delay
8687

8788
// Deployment of Taproot (BIPs 340-342)
8889
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
8990
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = 1199145601; // January 1, 2008
9091
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = 1230767999; // December 31, 2008
92+
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay
9193

9294
consensus.nMinimumChainWork = uint256S("0x00000000000000000000000000000000000000001533efd8d716a517fe2c5008");
9395
consensus.defaultAssumeValid = uint256S("0x0000000000000000000b9d2ec5a352ecba0592946514a92f14319dc2b367fc72"); // 654683
@@ -200,11 +202,13 @@ class CTestNetParams : public CChainParams {
200202
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
201203
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
202204
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
205+
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].min_activation_height = 0; // No activation delay
203206

204207
// Deployment of Taproot (BIPs 340-342)
205208
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
206209
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = 1199145601; // January 1, 2008
207210
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = 1230767999; // December 31, 2008
211+
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay
208212

209213
consensus.nMinimumChainWork = uint256S("0x0000000000000000000000000000000000000000000001db6ec4ac88cf2272c6");
210214
consensus.defaultAssumeValid = uint256S("0x000000000000006433d1efec504c53ca332b64963c425395515b01977bd7b3b0"); // 1864000
@@ -335,11 +339,13 @@ class SigNetParams : public CChainParams {
335339
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
336340
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
337341
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
342+
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].min_activation_height = 0; // No activation delay
338343

339344
// Activation of Taproot (BIPs 340-342)
340345
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
341346
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
342347
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
348+
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay
343349

344350
// message start is defined as the first 4 bytes of the sha256d of the block script
345351
CHashWriter h(SER_DISK, 0);
@@ -397,12 +403,16 @@ class CRegTestParams : public CChainParams {
397403
consensus.fPowNoRetargeting = true;
398404
consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
399405
consensus.nMinerConfirmationWindow = 144; // Faster than normal for regtest (144 instead of 2016)
406+
400407
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
401408
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 0;
402409
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
410+
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].min_activation_height = 0; // No activation delay
411+
403412
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
404413
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
405414
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
415+
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay
406416

407417
consensus.nMinimumChainWork = uint256{};
408418
consensus.defaultAssumeValid = uint256{};
@@ -466,10 +476,11 @@ class CRegTestParams : public CChainParams {
466476
/**
467477
* Allows modifying the Version Bits regtest parameters.
468478
*/
469-
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
479+
void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout, int min_activation_height)
470480
{
471481
consensus.vDeployments[d].nStartTime = nStartTime;
472482
consensus.vDeployments[d].nTimeout = nTimeout;
483+
consensus.vDeployments[d].min_activation_height = min_activation_height;
473484
}
474485
void UpdateActivationParametersFromArgs(const ArgsManager& args);
475486
};
@@ -492,22 +503,26 @@ void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
492503
for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
493504
std::vector<std::string> vDeploymentParams;
494505
boost::split(vDeploymentParams, strDeployment, boost::is_any_of(":"));
495-
if (vDeploymentParams.size() != 3) {
496-
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end");
506+
if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
507+
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
497508
}
498509
int64_t nStartTime, nTimeout;
510+
int min_activation_height = 0;
499511
if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
500512
throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
501513
}
502514
if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
503515
throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
504516
}
517+
if (vDeploymentParams.size() >= 4 && !ParseInt32(vDeploymentParams[3], &min_activation_height)) {
518+
throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
519+
}
505520
bool found = false;
506521
for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
507522
if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
508-
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
523+
UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout, min_activation_height);
509524
found = true;
510-
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
525+
LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld, min_activation_height=%d\n", vDeploymentParams[0], nStartTime, nTimeout, min_activation_height);
511526
break;
512527
}
513528
}

src/chainparamsbase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void SetupChainParamsBaseOptions(ArgsManager& argsman)
2222
"This is intended for regression testing tools and app development. Equivalent to -chain=regtest.", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
2323
argsman.AddArg("-segwitheight=<n>", "Set the activation height of segwit. -1 to disable. (regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
2424
argsman.AddArg("-testnet", "Use the test chain. Equivalent to -chain=test.", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
25-
argsman.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+
argsman.AddArg("-vbparams=deployment:start:end[:min_activation_height]", "Use given start/end times and min_activation_height for specified version bits deployment (regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
2626
argsman.AddArg("-signet", "Use the signet chain. Equivalent to -chain=signet. Note that the network is defined by the -signetchallenge parameter", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
2727
argsman.AddArg("-signetchallenge", "Blocks must satisfy the given script to be considered valid (only for signet networks; defaults to the global default signet test network challenge)", ArgsManager::ALLOW_STRING, OptionsCategory::CHAINPARAMS);
2828
argsman.AddArg("-signetseednode", "Specify a seed node for the signet network, in the hostname[:port] format, e.g. sig.net:1234 (may be used multiple times to specify multiple seed nodes; defaults to the global default signet test network seed node(s))", ArgsManager::ALLOW_STRING, OptionsCategory::CHAINPARAMS);

src/consensus/params.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ struct BIP9Deployment {
2929
int64_t nStartTime;
3030
/** Timeout/expiry MedianTime for the deployment attempt. */
3131
int64_t nTimeout;
32+
/** If lock in occurs, delay activation until at least this block
33+
* height. Note that activation will only occur on a retarget
34+
* boundary.
35+
*/
36+
int min_activation_height{0};
3237

3338
/** Constant for nTimeout very far in the future. */
3439
static constexpr int64_t NO_TIMEOUT = std::numeric_limits<int64_t>::max();

src/rpc/blockchain.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ static void BIP9SoftForkDescPushBack(UniValue& softforks, const std::string &nam
12581258
statsUV.pushKV("possible", statsStruct.possible);
12591259
bip9.pushKV("statistics", statsUV);
12601260
}
1261+
bip9.pushKV("min_activation_height", consensusParams.vDeployments[id].min_activation_height);
12611262

12621263
UniValue rv(UniValue::VOBJ);
12631264
rv.pushKV("type", "bip9");
@@ -1304,6 +1305,7 @@ RPCHelpMan getblockchaininfo()
13041305
{RPCResult::Type::NUM_TIME, "start_time", "the minimum median time past of a block at which the bit gains its meaning"},
13051306
{RPCResult::Type::NUM_TIME, "timeout", "the median time past of a block at which the deployment is considered failed if not yet locked in"},
13061307
{RPCResult::Type::NUM, "since", "height of the first block to which the status applies"},
1308+
{RPCResult::Type::NUM, "min_activation_height", "minimum height of blocks for which the rules may be enforced"},
13071309
{RPCResult::Type::OBJ, "statistics", "numeric statistics about BIP9 signalling for a softfork (only for \"started\" status)",
13081310
{
13091311
{RPCResult::Type::NUM, "period", "the length in blocks of the BIP9 signalling period"},

src/test/versionbits_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
255255
int64_t bit = params.vDeployments[dep].bit;
256256
int64_t nStartTime = params.vDeployments[dep].nStartTime;
257257
int64_t nTimeout = params.vDeployments[dep].nTimeout;
258+
int min_activation_height = params.vDeployments[dep].min_activation_height;
258259

259260
// should not be any signalling for first block
260261
BOOST_CHECK_EQUAL(ComputeBlockVersion(nullptr, params), VERSIONBITS_TOP_BITS);
@@ -267,6 +268,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
267268
BOOST_REQUIRE(nTimeout <= std::numeric_limits<uint32_t>::max() || nTimeout == Consensus::BIP9Deployment::NO_TIMEOUT);
268269
BOOST_REQUIRE(0 <= bit && bit < 32);
269270
BOOST_REQUIRE(((1 << bit) & VERSIONBITS_TOP_MASK) == 0);
271+
BOOST_REQUIRE(min_activation_height == 0);
270272

271273
// In the first chain, test that the bit is set by CBV until it has failed.
272274
// In the second chain, test the bit is set by CBV while STARTED and

src/versionbits.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex*
99
{
1010
int nPeriod = Period(params);
1111
int nThreshold = Threshold(params);
12+
int min_activation_height = MinActivationHeight(params);
1213
int64_t nTimeStart = BeginTime(params);
1314
int64_t nTimeTimeout = EndTime(params);
1415

@@ -78,8 +79,10 @@ ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex*
7879
break;
7980
}
8081
case ThresholdState::LOCKED_IN: {
81-
// Always progresses into ACTIVE.
82-
stateNext = ThresholdState::ACTIVE;
82+
// Progresses into ACTIVE provided activation height will have been reached.
83+
if (pindexPrev->nHeight + 1 >= min_activation_height) {
84+
stateNext = ThresholdState::ACTIVE;
85+
}
8386
break;
8487
}
8588
case ThresholdState::FAILED:
@@ -170,6 +173,7 @@ class VersionBitsConditionChecker : public AbstractThresholdConditionChecker {
170173
protected:
171174
int64_t BeginTime(const Consensus::Params& params) const override { return params.vDeployments[id].nStartTime; }
172175
int64_t EndTime(const Consensus::Params& params) const override { return params.vDeployments[id].nTimeout; }
176+
int MinActivationHeight(const Consensus::Params& params) const override { return params.vDeployments[id].min_activation_height; }
173177
int Period(const Consensus::Params& params) const override { return params.nMinerConfirmationWindow; }
174178
int Threshold(const Consensus::Params& params) const override { return params.nRuleChangeActivationThreshold; }
175179

src/versionbits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static const int32_t VERSIONBITS_NUM_BITS = 29;
2525
enum class ThresholdState {
2626
DEFINED, // First state that each softfork starts out as. The genesis block is by definition in this state for each deployment.
2727
STARTED, // For blocks past the starttime.
28-
LOCKED_IN, // For one retarget period after the first retarget period with STARTED blocks of which at least threshold have the associated bit set in nVersion.
28+
LOCKED_IN, // For at least one retarget period after the first retarget period with STARTED blocks of which at least threshold have the associated bit set in nVersion, until min_activation_height is reached.
2929
ACTIVE, // For all blocks after the LOCKED_IN retarget period (final state)
3030
FAILED, // For all blocks once the first retarget period after the timeout time is hit, if LOCKED_IN wasn't already reached (final state)
3131
};
@@ -57,6 +57,7 @@ class AbstractThresholdConditionChecker {
5757
virtual bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const =0;
5858
virtual int64_t BeginTime(const Consensus::Params& params) const =0;
5959
virtual int64_t EndTime(const Consensus::Params& params) const =0;
60+
virtual int MinActivationHeight(const Consensus::Params& params) const { return 0; }
6061
virtual int Period(const Consensus::Params& params) const =0;
6162
virtual int Threshold(const Consensus::Params& params) const =0;
6263

test/functional/rpc_blockchain.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def _test_getblockchaininfo(self):
149149
'count': 57,
150150
'possible': True,
151151
},
152+
'min_activation_height': 0,
152153
},
153154
'active': False
154155
},
@@ -158,7 +159,8 @@ def _test_getblockchaininfo(self):
158159
'status': 'active',
159160
'start_time': -1,
160161
'timeout': 9223372036854775807,
161-
'since': 0
162+
'since': 0,
163+
'min_activation_height': 0,
162164
},
163165
'height': 0,
164166
'active': True

0 commit comments

Comments
 (0)