MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ContextualCheckBlock

Function ContextualCheckBlock

src/validation.cpp:4139–4194  ·  view source on GitHub ↗

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 comment * in ConnectBlock(). * Note that -reindex-chainstate skips the validation that happens here! */

Source from the content-addressed store, hash-verified

4137 * Note that -reindex-chainstate skips the validation that happens here!
4138 */
4139static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev)
4140{
4141 const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
4142
4143 // Enforce BIP113 (Median Time Past).
4144 bool enforce_locktime_median_time_past{false};
4145 if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_CSV)) {
4146 assert(pindexPrev != nullptr);
4147 enforce_locktime_median_time_past = true;
4148 }
4149
4150 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past ?
4151 pindexPrev->GetMedianTimePast() :
4152 block.GetBlockTime()};
4153
4154 // Check that all transactions are finalized
4155 for (const auto& tx : block.vtx) {
4156 if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
4157 return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal", "non-final transaction");
4158 }
4159 }
4160
4161 // Enforce rule that the coinbase starts with serialized block height
4162 if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB))
4163 {
4164 CScript expect = CScript() << nHeight;
4165 if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
4166 !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
4167 return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-height", "block height mismatch in coinbase");
4168 }
4169 }
4170
4171 // Validation for witness commitments.
4172 // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
4173 // coinbase (where 0x0000....0000 is used instead).
4174 // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness reserved value (unconstrained).
4175 // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
4176 // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
4177 // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness reserved value). In case there are
4178 // multiple, the last one is used.
4179 if (!CheckWitnessMalleation(block, DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT), state)) {
4180 return false;
4181 }
4182
4183 // After the coinbase witness reserved value and commitment are verified,
4184 // we can check if the block weight passes (before we've checked the
4185 // coinbase witness, it would be possible for the weight to be too
4186 // large by filling up the coinbase witness, which doesn't change
4187 // the block hash, so we couldn't mark the block as permanently
4188 // failed).
4189 if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
4190 return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-weight", strprintf("%s : weight limit failed", __func__));
4191 }
4192
4193 return true;
4194}
4195
4196bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationState& state, CBlockIndex** ppindex, bool min_pow_checked)

Callers 2

AcceptBlockMethod · 0.85
TestBlockValidityFunction · 0.85

Calls 11

IsFinalTxFunction · 0.85
CheckWitnessMalleationFunction · 0.85
GetBlockWeightFunction · 0.85
GetMedianTimePastMethod · 0.80
InvalidMethod · 0.80
DeploymentActiveAfterFunction · 0.70
CScriptClass · 0.70
GetBlockTimeMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by 1

TestBlockValidityFunction · 0.68