Skip to content

Commit cbda71c

Browse files
committed
Move context-required checks from CheckBlockHeader to Contextual...
1 parent 7c29ec9 commit cbda71c

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/main.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,7 +2241,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
22412241
int64_t nTimeStart = GetTimeMicros();
22422242

22432243
// Check it again in case a previous version let a bad block in
2244-
if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime(), !fJustCheck, !fJustCheck))
2244+
if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck))
22452245
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
22462246

22472247
// verify that the view's current state corresponds to the previous block
@@ -3258,20 +3258,16 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne
32583258
return true;
32593259
}
32603260

3261-
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW)
3261+
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW)
32623262
{
32633263
// Check proof of work matches claimed amount
32643264
if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
32653265
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
32663266

3267-
// Check timestamp
3268-
if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
3269-
return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
3270-
32713267
return true;
32723268
}
32733269

3274-
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW, bool fCheckMerkleRoot)
3270+
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
32753271
{
32763272
// These are checks that are independent of context.
32773273

@@ -3280,7 +3276,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
32803276

32813277
// Check that the header is valid (particularly PoW). This is mostly
32823278
// redundant with the call in AcceptBlockHeader.
3283-
if (!CheckBlockHeader(block, state, consensusParams, nAdjustedTime, fCheckPOW))
3279+
if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
32843280
return false;
32853281

32863282
// Check the merkle root.
@@ -3346,7 +3342,7 @@ static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidati
33463342
return true;
33473343
}
33483344

3349-
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex * const pindexPrev)
3345+
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex * const pindexPrev, int64_t nAdjustedTime)
33503346
{
33513347
// Check proof of work
33523348
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
@@ -3356,6 +3352,10 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
33563352
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
33573353
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
33583354

3355+
// Check timestamp
3356+
if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
3357+
return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
3358+
33593359
// Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
33603360
for (int32_t version = 2; version < 5; ++version) // check for version 2, 3 and 4 upgrades
33613361
if (block.nVersion < version && IsSuperMajority(version, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
@@ -3420,7 +3420,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
34203420
return true;
34213421
}
34223422

3423-
if (!CheckBlockHeader(block, state, chainparams.GetConsensus(), GetAdjustedTime()))
3423+
if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
34243424
return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
34253425

34263426
// Get prev block index
@@ -3436,7 +3436,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
34363436
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
34373437
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
34383438

3439-
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev))
3439+
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
34403440
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
34413441
}
34423442
if (pindex == NULL)
@@ -3569,9 +3569,9 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
35693569
indexDummy.nHeight = pindexPrev->nHeight + 1;
35703570

35713571
// NOTE: CheckBlockHeader is called by CheckBlock
3572-
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev))
3572+
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
35733573
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
3574-
if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime(), fCheckPOW, fCheckMerkleRoot))
3574+
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
35753575
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
35763576
if (!ContextualCheckBlock(block, state, pindexPrev))
35773577
return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
@@ -3916,7 +3916,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
39163916
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
39173917
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
39183918
// check level 1: verify block validity
3919-
if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime()))
3919+
if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
39203920
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
39213921
pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
39223922
// check level 2: verify undo validity

src/main.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,13 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
425425
/** Functions for validating blocks and updating the block tree */
426426

427427
/** Context-independent validity checks */
428-
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW = true);
429-
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
428+
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true);
429+
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
430430

431431
/** Context-dependent validity checks.
432432
* By "context", we mean only the previous block headers, but not the UTXO
433433
* set; UTXO-related validity checks are done in ConnectBlock(). */
434-
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex* pindexPrev);
434+
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex* pindexPrev, int64_t nAdjustedTime);
435435
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex *pindexPrev);
436436

437437
/** Apply the effects of this block (with given index) on the UTXO set represented by coins.

0 commit comments

Comments
 (0)