@@ -2719,18 +2719,23 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
2719
2719
return true ;
2720
2720
}
2721
2721
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 )
2723
2723
{
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 )
2728
2725
return true ;
2729
2726
2730
- assert (pindexPrev);
2731
-
2732
2727
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
+ }
2733
2735
2736
+ bool ContextualCheckBlockHeader (const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
2737
+ {
2738
+ const Consensus::Params& consensusParams = Params ().GetConsensus ();
2734
2739
// Check proof of work
2735
2740
if (block.nBits != GetNextWorkRequired (pindexPrev, &block, consensusParams))
2736
2741
return state.DoS (100 , error (" %s: incorrect proof of work" , __func__),
@@ -2741,14 +2746,6 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
2741
2746
return state.Invalid (error (" %s: block's timestamp is too early" , __func__),
2742
2747
REJECT_INVALID, " time-too-old" );
2743
2748
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
-
2752
2749
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2753
2750
if (block.nVersion < 2 && IsSuperMajority (2 , pindexPrev, consensusParams.nMajorityRejectBlockOutdated , consensusParams))
2754
2751
return state.Invalid (error (" %s: rejected nVersion=1 block" , __func__),
@@ -2818,6 +2815,9 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
2818
2815
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
2819
2816
return state.DoS (100 , error (" %s: prev block invalid" , __func__), REJECT_INVALID, " bad-prevblk" );
2820
2817
}
2818
+ assert (pindexPrev);
2819
+ if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint (pindexPrev, state, chainparams, hash))
2820
+ return error (" %s: CheckIndexAgainstCheckpoint(): %s" , __func__, state.GetRejectReason ().c_str ());
2821
2821
2822
2822
if (!ContextualCheckBlockHeader (block, state, pindexPrev))
2823
2823
return false ;
@@ -2933,8 +2933,11 @@ bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, bool
2933
2933
2934
2934
bool TestBlockValidity (CValidationState &state, const CBlock& block, CBlockIndex * const pindexPrev, bool fCheckPOW , bool fCheckMerkleRoot )
2935
2935
{
2936
+ const CChainParams& chainparams = Params ();
2936
2937
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 ());
2938
2941
2939
2942
CCoinsViewCache viewNew (pcoinsTip);
2940
2943
CBlockIndex indexDummy (block);
0 commit comments