@@ -2830,22 +2830,6 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
2830
2830
return true ;
2831
2831
}
2832
2832
2833
- static bool CheckIndexAgainstCheckpoint (const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
2834
- {
2835
- if (*pindexPrev->phashBlock == chainparams.GetConsensus ().hashGenesisBlock )
2836
- return true ;
2837
-
2838
- int nHeight = pindexPrev->nHeight +1 ;
2839
- // Don't accept any forks from the main chain prior to last checkpoint.
2840
- // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
2841
- // MapBlockIndex.
2842
- CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint (chainparams.Checkpoints ());
2843
- if (pcheckpoint && nHeight < pcheckpoint->nHeight )
2844
- return state.DoS (100 , error (" %s: forked chain older than last checkpoint (height %d)" , __func__, nHeight), REJECT_CHECKPOINT, " bad-fork-prior-to-checkpoint" );
2845
-
2846
- return true ;
2847
- }
2848
-
2849
2833
bool IsWitnessEnabled (const CBlockIndex* pindexPrev, const Consensus::Params& params)
2850
2834
{
2851
2835
LOCK (cs_main);
@@ -2911,14 +2895,26 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
2911
2895
/* * Context-dependent validity checks.
2912
2896
* By "context", we mean only the previous block headers, but not the UTXO
2913
2897
* set; UTXO-related validity checks are done in ConnectBlock(). */
2914
- static bool ContextualCheckBlockHeader (const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams , const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
2898
+ static bool ContextualCheckBlockHeader (const CBlockHeader& block, CValidationState& state, const CChainParams& params , const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
2915
2899
{
2916
2900
assert (pindexPrev != NULL );
2917
2901
const int nHeight = pindexPrev->nHeight + 1 ;
2902
+
2918
2903
// Check proof of work
2904
+ const Consensus::Params& consensusParams = params.GetConsensus ();
2919
2905
if (block.nBits != GetNextWorkRequired (pindexPrev, &block, consensusParams))
2920
2906
return state.DoS (100 , false , REJECT_INVALID, " bad-diffbits" , false , " incorrect proof of work" );
2921
2907
2908
+ // Check against checkpoints
2909
+ if (fCheckpointsEnabled ) {
2910
+ // Don't accept any forks from the main chain prior to last checkpoint.
2911
+ // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
2912
+ // MapBlockIndex.
2913
+ CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint (params.Checkpoints ());
2914
+ if (pcheckpoint && nHeight < pcheckpoint->nHeight )
2915
+ return state.DoS (100 , error (" %s: forked chain older than last checkpoint (height %d)" , __func__, nHeight), REJECT_CHECKPOINT, " bad-fork-prior-to-checkpoint" );
2916
+ }
2917
+
2922
2918
// Check timestamp against prev
2923
2919
if (block.GetBlockTime () <= pindexPrev->GetMedianTimePast ())
2924
2920
return state.Invalid (false , REJECT_INVALID, " time-too-old" , " block's timestamp is too early" );
@@ -3049,12 +3045,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
3049
3045
pindexPrev = (*mi).second ;
3050
3046
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
3051
3047
return state.DoS (100 , error (" %s: prev block invalid" , __func__), REJECT_INVALID, " bad-prevblk" );
3052
-
3053
- assert (pindexPrev);
3054
- if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint (pindexPrev, state, chainparams, hash))
3055
- return error (" %s: CheckIndexAgainstCheckpoint(): %s" , __func__, state.GetRejectReason ().c_str ());
3056
-
3057
- if (!ContextualCheckBlockHeader (block, state, chainparams.GetConsensus (), pindexPrev, GetAdjustedTime ()))
3048
+ if (!ContextualCheckBlockHeader (block, state, chainparams, pindexPrev, GetAdjustedTime ()))
3058
3049
return error (" %s: Consensus::ContextualCheckBlockHeader: %s, %s" , __func__, hash.ToString (), FormatStateMessage (state));
3059
3050
}
3060
3051
if (pindex == NULL )
@@ -3203,16 +3194,13 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
3203
3194
{
3204
3195
AssertLockHeld (cs_main);
3205
3196
assert (pindexPrev && pindexPrev == chainActive.Tip ());
3206
- if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint (pindexPrev, state, chainparams, block.GetHash ()))
3207
- return error (" %s: CheckIndexAgainstCheckpoint(): %s" , __func__, state.GetRejectReason ().c_str ());
3208
-
3209
3197
CCoinsViewCache viewNew (pcoinsTip);
3210
3198
CBlockIndex indexDummy (block);
3211
3199
indexDummy.pprev = pindexPrev;
3212
3200
indexDummy.nHeight = pindexPrev->nHeight + 1 ;
3213
3201
3214
3202
// NOTE: CheckBlockHeader is called by CheckBlock
3215
- if (!ContextualCheckBlockHeader (block, state, chainparams. GetConsensus () , pindexPrev, GetAdjustedTime ()))
3203
+ if (!ContextualCheckBlockHeader (block, state, chainparams, pindexPrev, GetAdjustedTime ()))
3216
3204
return error (" %s: Consensus::ContextualCheckBlockHeader: %s" , __func__, FormatStateMessage (state));
3217
3205
if (!CheckBlock (block, state, chainparams.GetConsensus (), fCheckPOW , fCheckMerkleRoot ))
3218
3206
return error (" %s: Consensus::CheckBlock: %s" , __func__, FormatStateMessage (state));
0 commit comments