MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / ContextualCheckBlockHeader

Function ContextualCheckBlockHeader

src/validation.cpp:3447–3489  ·  view source on GitHub ↗

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 comm

Source from the content-addressed store, hash-verified

3445 * Note that -reindex-chainstate skips the validation that happens here!
3446 */
3447static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
3448{
3449 assert(pindexPrev != nullptr);
3450 const int nHeight = pindexPrev->nHeight + 1;
3451
3452 // Check proof of work
3453 const Consensus::Params& consensusParams = params.GetConsensus();
3454 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
3455 return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
3456
3457 // Check against checkpoints
3458 if (fCheckpointsEnabled) {
3459 // Don't accept any forks from the main chain prior to last checkpoint.
3460 // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
3461 // MapBlockIndex.
3462 CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
3463 if (pcheckpoint && nHeight < pcheckpoint->nHeight)
3464 return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
3465 }
3466
3467 // Check block height for blocks after BTG fork.
3468 if (nHeight >= consensusParams.BTGHeight && block.nHeight != (uint32_t)nHeight)
3469 return state.Invalid(false, REJECT_INVALID, "bad-height", "incorrect block height");
3470
3471 // Check timestamp against prev
3472 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
3473 return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
3474
3475 // Check timestamp
3476 if (block.GetBlockTime() > nAdjustedTime + std::min(consensusParams.BTGMaxFutureBlockTime,
3477 BITCOIN_MAX_FUTURE_BLOCK_TIME))
3478 return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
3479
3480 // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
3481 // check for version 2, 3 and 4 upgrades
3482 if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
3483 (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
3484 (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
3485 return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
3486 strprintf("rejected nVersion=0x%08x block", block.nVersion));
3487
3488 return true;
3489}
3490
3491/** NOTE: This function is not currently invoked by ConnectBlock(), so we
3492 * should consider upgrade issues if we change which consensus rules are

Callers 2

AcceptBlockHeaderMethod · 0.85
TestBlockValidityFunction · 0.85

Calls 7

GetNextWorkRequiredFunction · 0.85
GetLastCheckpointFunction · 0.85
errorFunction · 0.85
DoSMethod · 0.80
InvalidMethod · 0.80
GetMedianTimePastMethod · 0.80
GetBlockTimeMethod · 0.45

Tested by 1

TestBlockValidityFunction · 0.68