@@ -2835,22 +2835,6 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
2835
2835
return true ;
2836
2836
}
2837
2837
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
-
2854
2838
bool IsWitnessEnabled (const CBlockIndex* pindexPrev, const Consensus::Params& params)
2855
2839
{
2856
2840
LOCK (cs_main);
@@ -2916,14 +2900,26 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
2916
2900
/* * Context-dependent validity checks.
2917
2901
* By "context", we mean only the previous block headers, but not the UTXO
2918
2902
* 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)
2920
2904
{
2921
2905
assert (pindexPrev != NULL );
2922
2906
const int nHeight = pindexPrev->nHeight + 1 ;
2907
+
2923
2908
// Check proof of work
2909
+ const Consensus::Params& consensusParams = params.GetConsensus ();
2924
2910
if (block.nBits != GetNextWorkRequired (pindexPrev, &block, consensusParams))
2925
2911
return state.DoS (100 , false , REJECT_INVALID, " bad-diffbits" , false , " incorrect proof of work" );
2926
2912
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
+
2927
2923
// Check timestamp against prev
2928
2924
if (block.GetBlockTime () <= pindexPrev->GetMedianTimePast ())
2929
2925
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
3054
3050
pindexPrev = (*mi).second ;
3055
3051
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
3056
3052
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 ()))
3063
3054
return error (" %s: Consensus::ContextualCheckBlockHeader: %s, %s" , __func__, hash.ToString (), FormatStateMessage (state));
3064
3055
}
3065
3056
if (pindex == NULL )
@@ -3208,16 +3199,13 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
3208
3199
{
3209
3200
AssertLockHeld (cs_main);
3210
3201
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
-
3214
3202
CCoinsViewCache viewNew (pcoinsTip);
3215
3203
CBlockIndex indexDummy (block);
3216
3204
indexDummy.pprev = pindexPrev;
3217
3205
indexDummy.nHeight = pindexPrev->nHeight + 1 ;
3218
3206
3219
3207
// NOTE: CheckBlockHeader is called by CheckBlock
3220
- if (!ContextualCheckBlockHeader (block, state, chainparams. GetConsensus () , pindexPrev, GetAdjustedTime ()))
3208
+ if (!ContextualCheckBlockHeader (block, state, chainparams, pindexPrev, GetAdjustedTime ()))
3221
3209
return error (" %s: Consensus::ContextualCheckBlockHeader: %s" , __func__, FormatStateMessage (state));
3222
3210
if (!CheckBlock (block, state, chainparams.GetConsensus (), fCheckPOW , fCheckMerkleRoot ))
3223
3211
return error (" %s: Consensus::CheckBlock: %s" , __func__, FormatStateMessage (state));
0 commit comments