Skip to content

Commit

Permalink
Merge branch 'uasfsegwit+rewind' of https://github.com/luke-jr/bitcoin
Browse files Browse the repository at this point in the history
…into luke-jr-uasfsegwit+rewind
  • Loading branch information
mkwia committed Jul 2, 2017
2 parents 5c1fdcb + 6048d3e commit 3512f41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,17 +1853,8 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin

if (GetBoolArg("-bip148", DEFAULT_BIP148)) {
// BIP148 mandatory segwit signalling.
int64_t nMedianTimePast = pindex->GetMedianTimePast();
if ( (nMedianTimePast >= 1501545600) && // Tue 01 Aug 2017 00:00:00 UTC
(nMedianTimePast <= 1510704000) && // Wed 15 Nov 2017 00:00:00 UTC
(!IsWitnessLockedIn(pindex->pprev, chainparams.GetConsensus()) && // Segwit is not locked in
!IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus())) ) // and is not active.
{
bool fVersionBits = (pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS;
bool fSegbit = (pindex->nVersion & VersionBitsMask(chainparams.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT)) != 0;
if (!(fVersionBits && fSegbit)) {
return state.DoS(0, error("ConnectBlock(): relayed block must signal for segwit, please upgrade"), REJECT_INVALID, "bad-no-segwit");
}
if (!CheckBIP148(pindex, chainparams.GetConsensus())) {
return state.DoS(0, error("ConnectBlock(): relayed block must signal for segwit, please upgrade"), REJECT_INVALID, "bad-no-segwit");
}
}

Expand Down Expand Up @@ -2940,6 +2931,23 @@ bool IsWitnessLockedIn(const CBlockIndex* pindexPrev, const Consensus::Params& p
return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_LOCKED_IN);
}

bool CheckBIP148(const CBlockIndex* pindex, const Consensus::Params& params)
{
int64_t nMedianTimePast = pindex->GetMedianTimePast();
if ( (nMedianTimePast >= 1501545600) && // Tue 01 Aug 2017 00:00:00 UTC
(nMedianTimePast <= 1510704000) && // Wed 15 Nov 2017 00:00:00 UTC
(!IsWitnessLockedIn(pindex->pprev, params) && // Segwit is not locked in
!IsWitnessEnabled(pindex->pprev, params)) ) // and is not active.
{
bool fVersionBits = (pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS;
bool fSegbit = (pindex->nVersion & VersionBitsMask(params, Consensus::DEPLOYMENT_SEGWIT)) != 0;
if (!(fVersionBits && fSegbit)) {
return false;
}
}
return true;
}

// Compute at which vout of the block's coinbase transaction the witness
// commitment occurs, or -1 if not found.
static int GetWitnessCommitmentIndex(const CBlock& block)
Expand Down Expand Up @@ -3728,10 +3736,17 @@ bool RewindBlockIndex(const CChainParams& params)
LOCK(cs_main);

int nHeight = 1;
bool fCheckBIP148 = GetBoolArg("-bip148", DEFAULT_BIP148);
while (nHeight <= chainActive.Height()) {
if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
break;
}
if (fCheckBIP148 && !CheckBIP148(chainActive[nHeight], params.GetConsensus())) {
if (!(chainActive[nHeight]->nStatus & BLOCK_HAVE_DATA)) {
return error("RewindBlockIndex: invalid block found violating BIP148 at height %i has already been pruned", nHeight);
}
break;
}
nHeight++;
}

Expand Down
3 changes: 3 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& pa
/** Check if Segregated Witness is Locked In */
bool IsWitnessLockedIn(const CBlockIndex* pindexPrev, const Consensus::Params& params);

/** Check if block complies with BIP148 rule; note this accepts pindex for the block, not pindexPrev! */
bool CheckBIP148(const CBlockIndex* pindex, const Consensus::Params&);

/** When there are blocks in the active chain with missing data, rewind the chainstate and remove them from the block index */
bool RewindBlockIndex(const CChainParams& params);

Expand Down

0 comments on commit 3512f41

Please sign in to comment.