Skip to content

Commit ee9f4a5

Browse files
committed
Consensus: Decouple from chainparams.o and timedata.o
Do it for the consensus-critical functions: - CheckBlockHeader - CheckBlock - ContextualCheckBlockHeader
1 parent 1b87e5b commit ee9f4a5

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/main.cpp

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

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

22292229
// verify that the view's current state corresponds to the previous block
@@ -3234,20 +3234,20 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne
32343234
return true;
32353235
}
32363236

3237-
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW)
3237+
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW)
32383238
{
32393239
// Check proof of work matches claimed amount
3240-
if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
3240+
if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
32413241
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
32423242

32433243
// Check timestamp
3244-
if (block.GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
3244+
if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
32453245
return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
32463246

32473247
return true;
32483248
}
32493249

3250-
bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
3250+
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW, bool fCheckMerkleRoot)
32513251
{
32523252
// These are checks that are independent of context.
32533253

@@ -3256,7 +3256,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
32563256

32573257
// Check that the header is valid (particularly PoW). This is mostly
32583258
// redundant with the call in AcceptBlockHeader.
3259-
if (!CheckBlockHeader(block, state, fCheckPOW))
3259+
if (!CheckBlockHeader(block, state, consensusParams, nAdjustedTime, fCheckPOW))
32603260
return false;
32613261

32623262
// Check the merkle root.
@@ -3322,9 +3322,8 @@ static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidati
33223322
return true;
33233323
}
33243324

3325-
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
3325+
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex * const pindexPrev)
33263326
{
3327-
const Consensus::Params& consensusParams = Params().GetConsensus();
33283327
// Check proof of work
33293328
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
33303329
return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
@@ -3397,7 +3396,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
33973396
return true;
33983397
}
33993398

3400-
if (!CheckBlockHeader(block, state))
3399+
if (!CheckBlockHeader(block, state, chainparams.GetConsensus(), GetAdjustedTime()))
34013400
return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
34023401

34033402
// Get prev block index
@@ -3413,7 +3412,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
34133412
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
34143413
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
34153414

3416-
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
3415+
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev))
34173416
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
34183417
}
34193418
if (pindex == NULL)
@@ -3457,7 +3456,7 @@ static bool AcceptBlock(const CBlock& block, CValidationState& state, const CCha
34573456
if (fTooFarAhead) return true; // Block height is too high
34583457
}
34593458

3460-
if ((!CheckBlock(block, state)) || !ContextualCheckBlock(block, state, pindex->pprev)) {
3459+
if ((!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime())) || !ContextualCheckBlock(block, state, pindex->pprev)) {
34613460
if (state.IsInvalid() && !state.CorruptionPossible()) {
34623461
pindex->nStatus |= BLOCK_FAILED_VALID;
34633462
setDirtyBlockIndex.insert(pindex);
@@ -3542,9 +3541,9 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
35423541
indexDummy.nHeight = pindexPrev->nHeight + 1;
35433542

35443543
// NOTE: CheckBlockHeader is called by CheckBlock
3545-
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
3544+
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev))
35463545
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
3547-
if (!CheckBlock(block, state, fCheckPOW, fCheckMerkleRoot))
3546+
if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime(), fCheckPOW, fCheckMerkleRoot))
35483547
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
35493548
if (!ContextualCheckBlock(block, state, pindexPrev))
35503549
return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
@@ -3876,7 +3875,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
38763875
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
38773876
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
38783877
// check level 1: verify block validity
3879-
if (nCheckLevel >= 1 && !CheckBlock(block, state))
3878+
if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime()))
38803879
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
38813880
pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
38823881
// 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, bool fCheckPOW = true);
429-
bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
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);
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, CBlockIndex *pindexPrev);
434+
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex* pindexPrev);
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)