Skip to content

Commit c4e76a3

Browse files
committed
validation: avoid masking of difficulty adjustment errors
Currently difficulty adjustment violations are not reported for chains that branch off before the last checkpoint. Change this by moving the checkpoint check after the difficulty check. Original Author: Pieter Wuille <[email protected]> Cherry-picked from: 215fc33 Github Pull Request: dogecoin#3576
1 parent b961bab commit c4e76a3

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

src/validation.cpp

+12-20
Original file line numberDiff line numberDiff line change
@@ -2999,20 +2999,6 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
29992999
return true;
30003000
}
30013001

3002-
static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
3003-
{
3004-
if (*pindexPrev->phashBlock == chainparams.GetConsensus(0).hashGenesisBlock)
3005-
return true;
3006-
3007-
int nHeight = pindexPrev->nHeight+1;
3008-
// Don't accept any forks from the main chain prior to last checkpoint
3009-
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
3010-
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
3011-
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
3012-
3013-
return true;
3014-
}
3015-
30163002
bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
30173003
{
30183004
// Dogecoin: Disable SegWit
@@ -3080,7 +3066,8 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
30803066
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
30813067
{
30823068
const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
3083-
const Consensus::Params& consensusParams = Params().GetConsensus(nHeight);
3069+
const CChainParams& params = Params();
3070+
const Consensus::Params& consensusParams = params.GetConsensus(nHeight);
30843071

30853072
// Disallow legacy blocks after merge-mining start.
30863073
if (!consensusParams.fAllowLegacyBlocks
@@ -3102,6 +3089,16 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
31023089
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
31033090
return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
31043091

3092+
// Check against checkpoints
3093+
if (fCheckpointsEnabled) {
3094+
// Don't accept any forks from the main chain prior to last checkpoint.
3095+
// GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
3096+
// MapBlockIndex.
3097+
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
3098+
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
3099+
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
3100+
}
3101+
31053102
// Check timestamp against prev
31063103
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
31073104
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
@@ -3237,8 +3234,6 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
32373234
return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
32383235

32393236
assert(pindexPrev);
3240-
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
3241-
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
32423237

32433238
if (!ContextualCheckBlockHeader(block, state, pindexPrev, GetAdjustedTime()))
32443239
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
@@ -3418,9 +3413,6 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
34183413
{
34193414
AssertLockHeld(cs_main);
34203415
assert(pindexPrev && pindexPrev == chainActive.Tip());
3421-
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
3422-
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3423-
34243416
CCoinsViewCache viewNew(pcoinsTip);
34253417
CBlockIndex indexDummy(block);
34263418
indexDummy.pprev = pindexPrev;

0 commit comments

Comments
 (0)