Skip to content

Commit 6bdf4b3

Browse files
committed
Merge #11028: Avoid masking of difficulty adjustment errors by checkpoints
85c82b5 Avoid masking of difficulty adjustment errors by checkpoints (Pieter Wuille) Pull request description: 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. Tree-SHA512: 33666f2c3459151b28c42041a463779e6df18f61d3dd5b1879a0af4e5b199ef74d1e33e06af68bebfdfb211569ad5fb56556bfebe9d63b5688d910ea211b839a
2 parents 653a46d + 85c82b5 commit 6bdf4b3

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

src/validation.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,22 +2835,6 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
28352835
return true;
28362836
}
28372837

2838-
static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
2839-
{
2840-
if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
2841-
return true;
2842-
2843-
int nHeight = pindexPrev->nHeight+1;
2844-
// Don't accept any forks from the main chain prior to last checkpoint.
2845-
// GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
2846-
// MapBlockIndex.
2847-
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
2848-
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2849-
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
2850-
2851-
return true;
2852-
}
2853-
28542838
bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
28552839
{
28562840
LOCK(cs_main);
@@ -2916,14 +2900,26 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
29162900
/** Context-dependent validity checks.
29172901
* By "context", we mean only the previous block headers, but not the UTXO
29182902
* set; UTXO-related validity checks are done in ConnectBlock(). */
2919-
static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
2903+
static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
29202904
{
29212905
assert(pindexPrev != NULL);
29222906
const int nHeight = pindexPrev->nHeight + 1;
2907+
29232908
// Check proof of work
2909+
const Consensus::Params& consensusParams = params.GetConsensus();
29242910
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
29252911
return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
29262912

2913+
// Check against checkpoints
2914+
if (fCheckpointsEnabled) {
2915+
// Don't accept any forks from the main chain prior to last checkpoint.
2916+
// GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
2917+
// MapBlockIndex.
2918+
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
2919+
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2920+
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
2921+
}
2922+
29272923
// Check timestamp against prev
29282924
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
29292925
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
@@ -3054,12 +3050,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
30543050
pindexPrev = (*mi).second;
30553051
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
30563052
return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
3057-
3058-
assert(pindexPrev);
3059-
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
3060-
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3061-
3062-
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3053+
if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
30633054
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
30643055
}
30653056
if (pindex == NULL)
@@ -3208,16 +3199,13 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
32083199
{
32093200
AssertLockHeld(cs_main);
32103201
assert(pindexPrev && pindexPrev == chainActive.Tip());
3211-
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
3212-
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3213-
32143202
CCoinsViewCache viewNew(pcoinsTip);
32153203
CBlockIndex indexDummy(block);
32163204
indexDummy.pprev = pindexPrev;
32173205
indexDummy.nHeight = pindexPrev->nHeight + 1;
32183206

32193207
// NOTE: CheckBlockHeader is called by CheckBlock
3220-
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3208+
if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
32213209
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
32223210
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
32233211
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));

0 commit comments

Comments
 (0)