src/validation.cpp
/** Context-dependent validity checks.
 *  By "context", we mean only the previous block headers, but not the UTXO
 *  set; UTXO-related validity checks are done in ConnectBlock().
 *  NOTE: This function is not currently invoked by ConnectBlock(), so we
 *  should consider upgrade issues if we change which consensus rules are
 *  enforced in this function (eg by adding a new consensus rule). See comment
 *  in ConnectBlock().
 *  Note that -reindex-chainstate skips the validation that happens here!
 */
Context-dependent validity checks.
By "context", we mean only the previous block headers, but not the UTXO
set; UTXO-related validity checks are done in ConnectBlock().
NOTE: This function is not currently invoked by ConnectBlock(), so we
should consider upgrade issues if we change which consensus rules are
enforced in this function . See comment
in ConnectBlock().
Note that -reindex-chainstate skips the validation that happens here!
Context - dependent validity checks . By " context " we mean only the previous block headers but not the UTXO set ; UTXO - related validity checks are done in ConnectBlock () . NOTE : This function is not currently invoked by ConnectBlock () so we should consider upgrade issues if we change which consensus rules are enforced in this function . See comment in ConnectBlock () . Note that - reindex - chainstate skips the validation that happens here!
static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
{
    assert(pindexPrev != nullptr);
    const int nHeight = pindexPrev->nHeight + 1;
    const Consensus::Params& consensusParams = params.GetConsensus();
    if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
        return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
    if (fCheckpointsEnabled) {
        CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
        if (pcheckpoint && nHeight < pcheckpoint->nHeight)
            return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
    }
    if (!IsInitialBlockDownload() && !CheckSyncCheckpoint(block.GetHash(), nHeight, pindexPrev)) {
        return state.DoS(0, error("%s: Block rejected by synchronized checkpoint", __func__),
                             REJECT_CHECKPOINT, "bad-block-checkpoint-sync");
    }
    if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
        return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
    if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
        return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
    if((block.nVersion < 2 && nHeight >= consensusParams.BIP34v2Height) ||
       (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
       (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
            return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
                                 strprintf("rejected nVersion=0x%08x block", block.nVersion));
    if (block.GetBlockTime() > GetAdjustedTime() + 15 * 60)
        return state.Invalid(false, REJECT_INVALID, strprintf("%s: block's timestamp too far in the future", __func__),
                             "time-too-new");
     it should not be more then 15 minutes outside blockchain time
    if ((nHeight >= Params().GetConsensus().nHardForkTwo && block.GetBlockTime() <= pindexPrev->GetBlockTime() - 15 * 60))
        return state.Invalid(false, REJECT_INVALID, strprintf("%s: block's timestamp is too early compare to last block", __func__),
                             "wrong-time-between-blocks");
    return true;
}
static bool ContextualCheckBlockHeader ( const CBlockHeader & block , CValidationState & state , const CChainParams & params , const CBlockIndex * pindexPrev , int64_t nAdjustedTime ) { assert ( pindexPrev != nullptr ) ; const int nHeight = pindexPrev -> nHeight + 1 ; const Consensus :: Params & consensusParams = params . GetConsensus ( ) ; if ( block . nBits != GetNextWorkRequired ( pindexPrev , & block , consensusParams ) ) return state . DoS ( 100 , false , REJECT_INVALID , " " , false , " " ) ; if ( fCheckpointsEnabled ) { CBlockIndex * pcheckpoint = Checkpoints :: GetLastCheckpoint ( params . Checkpoints ( ) ) ; if ( pcheckpoint && nHeight < pcheckpoint -> nHeight ) return state . DoS ( 100 , error ( " " , __func__ , nHeight ) , REJECT_CHECKPOINT , " " ) ; } if ( ! IsInitialBlockDownload ( ) && ! CheckSyncCheckpoint ( block . GetHash ( ) , nHeight , pindexPrev ) ) { return state . DoS ( 0 , error ( " " , __func__ ) , REJECT_CHECKPOINT , " " ) ; } if ( block . GetBlockTime ( ) <= pindexPrev -> GetMedianTimePast ( ) ) return state . Invalid ( false , REJECT_INVALID , " " , " " ) ; if ( block . GetBlockTime ( ) > nAdjustedTime + MAX_FUTURE_BLOCK_TIME ) return state . Invalid ( false , REJECT_INVALID , " " , " " ) ; if ( ( block . nVersion < 2 && nHeight >= consensusParams . BIP34v2Height ) || ( block . nVersion < 3 && nHeight >= consensusParams . BIP66Height ) || ( block . nVersion < 4 && nHeight >= consensusParams . BIP65Height ) ) return state . Invalid ( false , REJECT_OBSOLETE , strprintf ( " " , block . nVersion ) , strprintf ( " " , block . nVersion ) ) ; if ( block . GetBlockTime ( ) > GetAdjustedTime ( ) + 15 * 60 ) return state . Invalid ( false , REJECT_INVALID , strprintf ( " " , __func__ ) , " " ) ; if ( ( nHeight >= Params ( ) . GetConsensus ( ) . nHardForkTwo && block . GetBlockTime ( ) <= pindexPrev -> GetBlockTime ( ) - 15 * 60 ) ) return state . Invalid ( false , REJECT_INVALID , strprintf ( " " , __func__ ) , " " ) ; return true ; }
Context-dependent validity checks.
Context - dependent validity checks .
// Check proof of work // Check against checkpoints // Don't accept any forks from the main chain prior to last checkpoint. // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our // MapBlockIndex. // Check that the block satisfies synchronized checkpoint // Check timestamp against prev // Check timestamp // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded: // check for version 2, 3 and 4 upgrades // BIP34 is always active after block 146557 though there are still version 1 blocks after // that height, the last version 1 block is at height 160053 making BIP34v2Height block height 160054. // Limit block in future accepted in chain to only a time window of 15 min // Check timestamp against prev it should not be more then 15 minutes outside blockchain time