Skip to content

Commit b368c9b

Browse files
committed
[consensus] Require large block at SegWit+BIP102HeightDelta fork point
Implements issue #29 Supercedes #35
1 parent 005189d commit b368c9b

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

src/chainparams.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class CMainParams : public CChainParams {
7575
consensus.BIP34Hash = uint256S("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8");
7676
consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
7777
consensus.BIP66Height = 363725; // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
78+
consensus.BIP102HeightDelta = 144 * 90;
7879
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
7980
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
8081
consensus.nPowTargetSpacing = 10 * 60;
@@ -186,6 +187,7 @@ class CTestNetParams : public CChainParams {
186187
consensus.BIP34Hash = uint256S("0x00000000fb7c0a2aeb5f1244e81921b84b7ac770004543144e10c2284f89bfd8");
187188
consensus.BIP65Height = 10001; // 00000000fb7c0a2aeb5f1244e81921b84b7ac770004543144e10c2284f89bfd8
188189
consensus.BIP66Height = 10001; // 00000000fb7c0a2aeb5f1244e81921b84b7ac770004543144e10c2284f89bfd8
190+
consensus.BIP102HeightDelta = 144 * 90; // TODO: update re test plan
189191
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
190192
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
191193
consensus.nPowTargetSpacing = 10 * 60;
@@ -276,6 +278,7 @@ class CRegTestParams : public CChainParams {
276278
consensus.BIP34Hash = uint256();
277279
consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in rpc activation tests)
278280
consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in rpc activation tests)
281+
consensus.BIP102HeightDelta = 144 * 90; // TODO: update
279282
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
280283
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
281284
consensus.nPowTargetSpacing = 10 * 60;

src/consensus/consensus.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,13 @@
88

99
#include <stdint.h>
1010

11-
/** BIP102 block size increase - time until HF activated */
12-
static const unsigned int BIP102_FORK_BUFFER = (144 * 90);
13-
1411
/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
1512
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = (8 * 1000 * 1000);
1613

1714
/** The maximum allowed size for a block excluding witness data, in bytes (network rule) */
1815
static inline bool BIP102active(bool fSegwitSeasoned)
1916
{
20-
if (!fSegwitSeasoned)
21-
return false;
22-
23-
return true;
17+
return fSegwitSeasoned;
2418
}
2519

2620
static const unsigned int MAX_LEGACY_BLOCK_SIZE = (1 * 1000 * 1000);

src/consensus/params.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ struct Params {
5151
int BIP65Height;
5252
/** Block height at which BIP66 becomes active */
5353
int BIP66Height;
54+
/** Block height delta at which BIP102 becomes active */
55+
int BIP102HeightDelta;
56+
5457
/**
5558
* Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
5659
* (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.

src/validation.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)