Skip to content

Commit 1fea667

Browse files
committed
Merge pull request #5975
425c3a8 Consensus: Separate CheckIndexAgainstCheckpoint() from ContextualCheckBlockHeader (Jorge Timón)
2 parents ac5476e + 425c3a8 commit 1fea667

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/main.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,18 +2719,23 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
27192719
return true;
27202720
}
27212721

2722-
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
2722+
static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
27232723
{
2724-
const CChainParams& chainParams = Params();
2725-
const Consensus::Params& consensusParams = chainParams.GetConsensus();
2726-
uint256 hash = block.GetHash();
2727-
if (hash == consensusParams.hashGenesisBlock)
2724+
if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
27282725
return true;
27292726

2730-
assert(pindexPrev);
2731-
27322727
int nHeight = pindexPrev->nHeight+1;
2728+
// Don't accept any forks from the main chain prior to last checkpoint
2729+
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
2730+
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2731+
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
2732+
2733+
return true;
2734+
}
27332735

2736+
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
2737+
{
2738+
const Consensus::Params& consensusParams = Params().GetConsensus();
27342739
// Check proof of work
27352740
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
27362741
return state.DoS(100, error("%s: incorrect proof of work", __func__),
@@ -2741,14 +2746,6 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
27412746
return state.Invalid(error("%s: block's timestamp is too early", __func__),
27422747
REJECT_INVALID, "time-too-old");
27432748

2744-
if (fCheckpointsEnabled)
2745-
{
2746-
// Don't accept any forks from the main chain prior to last checkpoint
2747-
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainParams.Checkpoints());
2748-
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2749-
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
2750-
}
2751-
27522749
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
27532750
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
27542751
return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
@@ -2818,6 +2815,9 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
28182815
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
28192816
return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
28202817
}
2818+
assert(pindexPrev);
2819+
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
2820+
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
28212821

28222822
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
28232823
return false;
@@ -2933,8 +2933,11 @@ bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, bool
29332933

29342934
bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex * const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
29352935
{
2936+
const CChainParams& chainparams = Params();
29362937
AssertLockHeld(cs_main);
2937-
assert(pindexPrev == chainActive.Tip());
2938+
assert(pindexPrev && pindexPrev == chainActive.Tip());
2939+
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
2940+
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
29382941

29392942
CCoinsViewCache viewNew(pcoinsTip);
29402943
CBlockIndex indexDummy(block);

0 commit comments

Comments
 (0)