@@ -2938,10 +2938,7 @@ bool IsWitnessSeasoned(const CBlockIndex* pindexPrev, const Consensus::Params& p
29382938
29392939 const int nHeight = pindexPrev->nHeight + 1 ;
29402940
2941- if (nHeight < (int )BIP102_FORK_BUFFER)
2942- return false ;
2943-
2944- const CBlockIndex* pindexForkBuffer = pindexPrev->GetAncestor (nHeight - BIP102_FORK_BUFFER);
2941+ const CBlockIndex* pindexForkBuffer = pindexPrev->GetAncestor (nHeight - params.BIP102HeightDelta );
29452942
29462943 return (VersionBitsState (pindexForkBuffer, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
29472944}
@@ -3069,6 +3066,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
30693066 // multiple, the last one is used.
30703067 bool fHaveWitness = false ;
30713068 bool fSegwitSeasoned = false ;
3069+ bool fBIP102FirstBlock = false ;
30723070 bool fSegWitActive = (VersionBitsState (pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
30733071 if (fSegWitActive ) {
30743072 int commitpos = GetWitnessCommitmentIndex (block);
@@ -3088,12 +3086,28 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
30883086 fHaveWitness = true ;
30893087 }
30903088
3091- fSegwitSeasoned = IsWitnessSeasoned (pindexPrev, consensusParams);
3089+ // Look back to test SegWit activation period
3090+ const CBlockIndex* pindexForkBuffer = pindexPrev ? pindexPrev->GetAncestor (nHeight - consensusParams.BIP102HeightDelta ) : NULL ;
3091+ fSegwitSeasoned = (VersionBitsState (pindexForkBuffer, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
3092+
3093+ // Look back one more block, to detect edge
3094+ if (fSegwitSeasoned ) {
3095+ assert (pindexForkBuffer);
3096+ const CBlockIndex* pindexLastSeason = pindexForkBuffer->pprev ;
3097+ fBIP102FirstBlock = (VersionBitsState (pindexLastSeason, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) != THRESHOLD_ACTIVE);
3098+ }
30923099 }
30933100
3101+ // Max block base size limit
30943102 if (::GetSerializeSize (block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) > MaxBlockBaseSize (fSegwitSeasoned ))
30953103 return state.DoS (100 , false , REJECT_INVALID, " bad-blk-length" , false , " size limits failed" );
30963104
3105+ // First block at fork must be large
3106+ if (fBIP102FirstBlock ) {
3107+ if (::GetSerializeSize (block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) <= MAX_LEGACY_BLOCK_SIZE)
3108+ return state.DoS (100 , false , REJECT_INVALID, " bad-blk-length-toosmall" , false , " size limits failed" );
3109+ }
3110+
30973111 // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
30983112 if (!fHaveWitness ) {
30993113 for (size_t i = 0 ; i < block.vtx .size (); i++) {
0 commit comments