Skip to content

Commit 715e9fd

Browse files
committed
Merge #8077: Consensus: Decouple from chainparams.o and timedata.o
ee9f4a5 Consensus: Decouple from chainparams.o and timedata.o (Jorge Timón)
2 parents 2e0a990 + ee9f4a5 commit 715e9fd

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
@@ -2228,7 +2228,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
22282228
int64_t nTimeStart = GetTimeMicros();
22292229

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

22342234
// verify that the view's current state corresponds to the previous block
@@ -3243,20 +3243,20 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne
32433243
return true;
32443244
}
32453245

3246-
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW)
3246+
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW)
32473247
{
32483248
// Check proof of work matches claimed amount
3249-
if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
3249+
if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
32503250
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
32513251

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

32563256
return true;
32573257
}
32583258

3259-
bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
3259+
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, int64_t nAdjustedTime, bool fCheckPOW, bool fCheckMerkleRoot)
32603260
{
32613261
// These are checks that are independent of context.
32623262

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

32663266
// Check that the header is valid (particularly PoW). This is mostly
32673267
// redundant with the call in AcceptBlockHeader.
3268-
if (!CheckBlockHeader(block, state, fCheckPOW))
3268+
if (!CheckBlockHeader(block, state, consensusParams, nAdjustedTime, fCheckPOW))
32693269
return false;
32703270

32713271
// Check the merkle root.
@@ -3331,9 +3331,8 @@ static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidati
33313331
return true;
33323332
}
33333333

3334-
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
3334+
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex * const pindexPrev)
33353335
{
3336-
const Consensus::Params& consensusParams = Params().GetConsensus();
33373336
// Check proof of work
33383337
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
33393338
return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
@@ -3406,7 +3405,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
34063405
return true;
34073406
}
34083407

3409-
if (!CheckBlockHeader(block, state))
3408+
if (!CheckBlockHeader(block, state, chainparams.GetConsensus(), GetAdjustedTime()))
34103409
return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
34113410

34123411
// Get prev block index
@@ -3422,7 +3421,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
34223421
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
34233422
return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
34243423

3425-
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
3424+
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev))
34263425
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
34273426
}
34283427
if (pindex == NULL)
@@ -3466,7 +3465,7 @@ static bool AcceptBlock(const CBlock& block, CValidationState& state, const CCha
34663465
if (fTooFarAhead) return true; // Block height is too high
34673466
}
34683467

3469-
if ((!CheckBlock(block, state)) || !ContextualCheckBlock(block, state, pindex->pprev)) {
3468+
if ((!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime())) || !ContextualCheckBlock(block, state, pindex->pprev)) {
34703469
if (state.IsInvalid() && !state.CorruptionPossible()) {
34713470
pindex->nStatus |= BLOCK_FAILED_VALID;
34723471
setDirtyBlockIndex.insert(pindex);
@@ -3551,9 +3550,9 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
35513550
indexDummy.nHeight = pindexPrev->nHeight + 1;
35523551

35533552
// NOTE: CheckBlockHeader is called by CheckBlock
3554-
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
3553+
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev))
35553554
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
3556-
if (!CheckBlock(block, state, fCheckPOW, fCheckMerkleRoot))
3555+
if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime(), fCheckPOW, fCheckMerkleRoot))
35573556
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
35583557
if (!ContextualCheckBlock(block, state, pindexPrev))
35593558
return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
@@ -3890,7 +3889,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
38903889
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
38913890
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
38923891
// check level 1: verify block validity
3893-
if (nCheckLevel >= 1 && !CheckBlock(block, state))
3892+
if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime()))
38943893
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
38953894
pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
38963895
// 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)