15
15
#include <consensus/tx_verify.h>
16
16
#include <consensus/validation.h>
17
17
#include <cuckoocache.h>
18
+ #include <deploymentstatus.h>
18
19
#include <flatfile.h>
19
20
#include <hash.h>
20
21
#include <index/blockfilterindex.h>
@@ -1649,15 +1650,6 @@ class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
1649
1650
1650
1651
static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS] GUARDED_BY(cs_main);
1651
1652
1652
- // 0.13.0 was shipped with a segwit deployment defined for testnet, but not for
1653
- // mainnet. We no longer need to support disabling the segwit deployment
1654
- // except for testing purposes, due to limitations of the functional test
1655
- // environment. See test/functional/p2p-segwit.py.
1656
- static bool IsScriptWitnessEnabled(const Consensus::Params& params)
1657
- {
1658
- return params.SegwitHeight != std::numeric_limits<int>::max();
1659
- }
1660
-
1661
1653
static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& consensusparams)
1662
1654
{
1663
1655
unsigned int flags = SCRIPT_VERIFY_NONE;
@@ -1676,22 +1668,22 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consens
1676
1668
1677
1669
// Enforce WITNESS rules whenever P2SH is in effect (and the segwit
1678
1670
// deployment is defined).
1679
- if (flags & SCRIPT_VERIFY_P2SH && IsScriptWitnessEnabled (consensusparams)) {
1671
+ if (flags & SCRIPT_VERIFY_P2SH && DeploymentEnabled (consensusparams, Consensus::DEPLOYMENT_SEGWIT )) {
1680
1672
flags |= SCRIPT_VERIFY_WITNESS;
1681
1673
}
1682
1674
1683
- // Start enforcing the DERSIG (BIP66) rule
1684
- if (pindex->nHeight >= consensusparams.BIP66Height ) {
1675
+ // Enforce the DERSIG (BIP66) rule
1676
+ if (DeploymentActiveAt(* pindex, consensusparams, Consensus::DEPLOYMENT_DERSIG) ) {
1685
1677
flags |= SCRIPT_VERIFY_DERSIG;
1686
1678
}
1687
1679
1688
- // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
1689
- if (pindex->nHeight >= consensusparams.BIP65Height ) {
1680
+ // Enforce CHECKLOCKTIMEVERIFY (BIP65)
1681
+ if (DeploymentActiveAt(* pindex, consensusparams, Consensus::DEPLOYMENT_CLTV) ) {
1690
1682
flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
1691
1683
}
1692
1684
1693
- // Start enforcing BIP112 (CHECKSEQUENCEVERIFY )
1694
- if (pindex->nHeight >= consensusparams.CSVHeight ) {
1685
+ // Enforce CHECKSEQUENCEVERIFY (BIP112 )
1686
+ if (DeploymentActiveAt(* pindex, consensusparams, Consensus::DEPLOYMENT_CSV) ) {
1695
1687
flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
1696
1688
}
1697
1689
@@ -1700,8 +1692,8 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consens
1700
1692
flags |= SCRIPT_VERIFY_TAPROOT;
1701
1693
}
1702
1694
1703
- // Start enforcing BIP147 NULLDUMMY (activated simultaneously with segwit)
1704
- if (IsWitnessEnabled( pindex->pprev , consensusparams)) {
1695
+ // Enforce BIP147 NULLDUMMY (activated simultaneously with segwit)
1696
+ if (DeploymentActiveAt(* pindex, consensusparams, Consensus::DEPLOYMENT_SEGWIT )) {
1705
1697
flags |= SCRIPT_VERIFY_NULLDUMMY;
1706
1698
}
1707
1699
@@ -1891,9 +1883,9 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
1891
1883
}
1892
1884
}
1893
1885
1894
- // Start enforcing BIP68 (sequence locks)
1886
+ // Enforce BIP68 (sequence locks)
1895
1887
int nLockTimeFlags = 0;
1896
- if (pindex->nHeight >= m_params.GetConsensus().CSVHeight ) {
1888
+ if (DeploymentActiveAt(* pindex, m_params.GetConsensus(), Consensus::DEPLOYMENT_CSV) ) {
1897
1889
nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
1898
1890
}
1899
1891
@@ -2986,7 +2978,7 @@ void CChainState::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pi
2986
2978
pindexNew->nDataPos = pos.nPos;
2987
2979
pindexNew->nUndoPos = 0;
2988
2980
pindexNew->nStatus |= BLOCK_HAVE_DATA;
2989
- if (IsWitnessEnabled( pindexNew->pprev , m_params.GetConsensus())) {
2981
+ if (DeploymentActiveAt(* pindexNew, m_params.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT )) {
2990
2982
pindexNew->nStatus |= BLOCK_OPT_WITNESS;
2991
2983
}
2992
2984
pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
@@ -3107,17 +3099,11 @@ bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensu
3107
3099
return true;
3108
3100
}
3109
3101
3110
- bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
3111
- {
3112
- int height = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
3113
- return (height >= params.SegwitHeight);
3114
- }
3115
-
3116
3102
void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
3117
3103
{
3118
3104
int commitpos = GetWitnessCommitmentIndex(block);
3119
3105
static const std::vector<unsigned char> nonce(32, 0x00);
3120
- if (commitpos != NO_WITNESS_COMMITMENT && IsWitnessEnabled (pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
3106
+ if (commitpos != NO_WITNESS_COMMITMENT && DeploymentActiveAfter (pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT ) && !block.vtx[0]->HasWitness()) {
3121
3107
CMutableTransaction tx(*block.vtx[0]);
3122
3108
tx.vin[0].scriptWitness.stack.resize(1);
3123
3109
tx.vin[0].scriptWitness.stack[0] = nonce;
@@ -3130,7 +3116,7 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
3130
3116
std::vector<unsigned char> commitment;
3131
3117
int commitpos = GetWitnessCommitmentIndex(block);
3132
3118
std::vector<unsigned char> ret(32, 0x00);
3133
- if (consensusParams.SegwitHeight != std::numeric_limits<int>::max( )) {
3119
+ if (DeploymentEnabled( consensusParams, Consensus::DEPLOYMENT_SEGWIT )) {
3134
3120
if (commitpos == NO_WITNESS_COMMITMENT) {
3135
3121
uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
3136
3122
CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
@@ -3208,13 +3194,13 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
3208
3194
if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
3209
3195
return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future");
3210
3196
3211
- // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
3212
- // check for version 2, 3 and 4 upgrades
3213
- if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
3214
- (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
3215
- (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
3197
+ // Reject blocks with outdated version
3198
+ if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_HEIGHTINCB)) ||
3199
+ (block.nVersion < 3 && DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DERSIG)) ||
3200
+ (block.nVersion < 4 && DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CLTV))) {
3216
3201
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, strprintf("bad-version(0x%08x)", block.nVersion),
3217
3202
strprintf("rejected nVersion=0x%08x block", block.nVersion));
3203
+ }
3218
3204
3219
3205
return true;
3220
3206
}
@@ -3229,9 +3215,9 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
3229
3215
{
3230
3216
const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
3231
3217
3232
- // Start enforcing BIP113 (Median Time Past).
3218
+ // Enforce BIP113 (Median Time Past).
3233
3219
int nLockTimeFlags = 0;
3234
- if (nHeight >= consensusParams.CSVHeight ) {
3220
+ if (DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV) ) {
3235
3221
assert(pindexPrev != nullptr);
3236
3222
nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
3237
3223
}
@@ -3248,7 +3234,7 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
3248
3234
}
3249
3235
3250
3236
// Enforce rule that the coinbase starts with serialized block height
3251
- if (nHeight >= consensusParams.BIP34Height )
3237
+ if (DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_HEIGHTINCB) )
3252
3238
{
3253
3239
CScript expect = CScript() << nHeight;
3254
3240
if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
@@ -3266,7 +3252,7 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
3266
3252
// {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness reserved value). In case there are
3267
3253
// multiple, the last one is used.
3268
3254
bool fHaveWitness = false;
3269
- if (nHeight >= consensusParams.SegwitHeight ) {
3255
+ if (DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT) ) {
3270
3256
int commitpos = GetWitnessCommitmentIndex(block);
3271
3257
if (commitpos != NO_WITNESS_COMMITMENT) {
3272
3258
bool malleated = false;
@@ -4096,9 +4082,8 @@ bool CChainState::NeedsRedownload() const
4096
4082
4097
4083
// At and above m_params.SegwitHeight, segwit consensus rules must be validated
4098
4084
CBlockIndex* block{m_chain.Tip()};
4099
- const int segwit_height{m_params.GetConsensus().SegwitHeight};
4100
4085
4101
- while (block != nullptr && block->nHeight >= segwit_height ) {
4086
+ while (block != nullptr && DeploymentActiveAt(* block, m_params.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT) ) {
4102
4087
if (!(block->nStatus & BLOCK_OPT_WITNESS)) {
4103
4088
// block is insufficiently validated for a segwit client
4104
4089
return true;
@@ -5000,7 +4985,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5000
4985
5001
4986
// Fake BLOCK_OPT_WITNESS so that CChainState::NeedsRedownload()
5002
4987
// won't ask to rewind the entire assumed-valid chain on startup.
5003
- if (index->pprev && ::IsWitnessEnabled( index->pprev , ::Params().GetConsensus())) {
4988
+ if (index->pprev && DeploymentActiveAt(* index, ::Params().GetConsensus(), Consensus::DEPLOYMENT_SEGWIT )) {
5004
4989
index->nStatus |= BLOCK_OPT_WITNESS;
5005
4990
}
5006
4991
}
0 commit comments