Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
| Show First 20 Lines • Show All 3,569 Lines • ▼ Show 20 Lines | |||||
| * By "context", we mean only the previous block headers, but not the UTXO | * By "context", we mean only the previous block headers, but not the UTXO | ||||
| * set; UTXO-related validity checks are done in ConnectBlock(). | * set; UTXO-related validity checks are done in ConnectBlock(). | ||||
| * NOTE: This function is not currently invoked by ConnectBlock(), so we | * NOTE: This function is not currently invoked by ConnectBlock(), so we | ||||
| * should consider upgrade issues if we change which consensus rules are | * should consider upgrade issues if we change which consensus rules are | ||||
| * enforced in this function (eg by adding a new consensus rule). See comment | * enforced in this function (eg by adding a new consensus rule). See comment | ||||
| * in ConnectBlock(). | * in ConnectBlock(). | ||||
| * Note that -reindex-chainstate skips the validation that happens here! | * Note that -reindex-chainstate skips the validation that happens here! | ||||
| */ | */ | ||||
| static bool ContextualCheckBlockHeader(const Config &config, | static bool ContextualCheckBlockHeader(const CChainParams ¶ms, | ||||
| const CBlockHeader &block, | const CBlockHeader &block, | ||||
| CValidationState &state, | CValidationState &state, | ||||
| const CBlockIndex *pindexPrev, | const CBlockIndex *pindexPrev, | ||||
| int64_t nAdjustedTime) { | int64_t nAdjustedTime) { | ||||
| assert(pindexPrev != nullptr); | assert(pindexPrev != nullptr); | ||||
| const int nHeight = pindexPrev->nHeight + 1; | const int nHeight = pindexPrev->nHeight + 1; | ||||
| // Check proof of work | // Check proof of work | ||||
| const Consensus::Params &consensusParams = | const Consensus::Params &consensusParams = params.GetConsensus(); | ||||
| config.GetChainParams().GetConsensus(); | |||||
| if (block.nBits != | if (block.nBits != | ||||
| GetNextWorkRequired(pindexPrev, &block, consensusParams)) { | GetNextWorkRequired(pindexPrev, &block, consensusParams)) { | ||||
| LogPrintf("bad bits after height: %d\n", pindexPrev->nHeight); | LogPrintf("bad bits after height: %d\n", pindexPrev->nHeight); | ||||
| return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, | return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, | ||||
| "incorrect proof of work"); | "incorrect proof of work"); | ||||
| } | } | ||||
| // Check against checkpoints | // Check against checkpoints | ||||
| if (fCheckpointsEnabled) { | if (fCheckpointsEnabled) { | ||||
| const CCheckpointData &checkpoints = | const CCheckpointData &checkpoints = params.Checkpoints(); | ||||
| config.GetChainParams().Checkpoints(); | |||||
| // Check that the block chain matches the known block chain up to a | // Check that the block chain matches the known block chain up to a | ||||
| // checkpoint. | // checkpoint. | ||||
| if (!Checkpoints::CheckBlock(checkpoints, nHeight, block.GetHash())) { | if (!Checkpoints::CheckBlock(checkpoints, nHeight, block.GetHash())) { | ||||
| return state.DoS(100, | return state.DoS(100, | ||||
| error("%s: rejected by checkpoint lock-in at %d", | error("%s: rejected by checkpoint lock-in at %d", | ||||
| __func__, nHeight), | __func__, nHeight), | ||||
| REJECT_CHECKPOINT, "checkpoint mismatch"); | REJECT_CHECKPOINT, "checkpoint mismatch"); | ||||
| ▲ Show 20 Lines • Show All 199 Lines • ▼ Show 20 Lines | if (hash != chainparams.GetConsensus().hashGenesisBlock) { | ||||
| CBlockIndex *pindexPrev = (*mi).second; | CBlockIndex *pindexPrev = (*mi).second; | ||||
| assert(pindexPrev); | assert(pindexPrev); | ||||
| if (pindexPrev->nStatus.isInvalid()) { | if (pindexPrev->nStatus.isInvalid()) { | ||||
| return state.DoS(100, error("%s: prev block invalid", __func__), | return state.DoS(100, error("%s: prev block invalid", __func__), | ||||
| REJECT_INVALID, "bad-prevblk"); | REJECT_INVALID, "bad-prevblk"); | ||||
| } | } | ||||
| if (!ContextualCheckBlockHeader(config, block, state, pindexPrev, | if (!ContextualCheckBlockHeader(chainparams, block, state, pindexPrev, | ||||
| GetAdjustedTime())) { | GetAdjustedTime())) { | ||||
| return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", | return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", | ||||
| __func__, hash.ToString(), FormatStateMessage(state)); | __func__, hash.ToString(), FormatStateMessage(state)); | ||||
| } | } | ||||
| if (!pindexPrev->IsValid(BlockValidity::SCRIPTS)) { | if (!pindexPrev->IsValid(BlockValidity::SCRIPTS)) { | ||||
| for (const CBlockIndex *failedit : m_failed_blocks) { | for (const CBlockIndex *failedit : m_failed_blocks) { | ||||
| if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) { | if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) { | ||||
| ▲ Show 20 Lines • Show All 303 Lines • ▼ Show 20 Lines | bool TestBlockValidity(const Config &config, CValidationState &state, | ||||
| CCoinsViewCache viewNew(pcoinsTip.get()); | CCoinsViewCache viewNew(pcoinsTip.get()); | ||||
| uint256 block_hash(block.GetHash()); | uint256 block_hash(block.GetHash()); | ||||
| CBlockIndex indexDummy(block); | CBlockIndex indexDummy(block); | ||||
| indexDummy.pprev = pindexPrev; | indexDummy.pprev = pindexPrev; | ||||
| indexDummy.nHeight = pindexPrev->nHeight + 1; | indexDummy.nHeight = pindexPrev->nHeight + 1; | ||||
| indexDummy.phashBlock = &block_hash; | indexDummy.phashBlock = &block_hash; | ||||
| // NOTE: CheckBlockHeader is called by CheckBlock | // NOTE: CheckBlockHeader is called by CheckBlock | ||||
| if (!ContextualCheckBlockHeader(config, block, state, pindexPrev, | if (!ContextualCheckBlockHeader(config.GetChainParams(), block, state, | ||||
| GetAdjustedTime())) { | pindexPrev, GetAdjustedTime())) { | ||||
| return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, | return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, | ||||
| FormatStateMessage(state)); | FormatStateMessage(state)); | ||||
| } | } | ||||
| if (!CheckBlock(config, block, state, validationOptions)) { | if (!CheckBlock(config, block, state, validationOptions)) { | ||||
| return error("%s: Consensus::CheckBlock: %s", __func__, | return error("%s: Consensus::CheckBlock: %s", __func__, | ||||
| FormatStateMessage(state)); | FormatStateMessage(state)); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 1,567 Lines • Show Last 20 Lines | |||||