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! */
| 3495 | * Note that -reindex-chainstate skips the validation that happens here! |
| 3496 | */ |
| 3497 | static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) |
| 3498 | { |
| 3499 | const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1; |
| 3500 | |
| 3501 | // Start enforcing BIP113 (Median Time Past) using versionbits logic. |
| 3502 | int nLockTimeFlags = 0; |
| 3503 | if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV, versionbitscache) == ThresholdState::ACTIVE) { |
| 3504 | nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST; |
| 3505 | } |
| 3506 | |
| 3507 | int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST) |
| 3508 | ? pindexPrev->GetMedianTimePast() |
| 3509 | : block.GetBlockTime(); |
| 3510 | |
| 3511 | // Check that all transactions are finalized |
| 3512 | for (const auto& tx : block.vtx) { |
| 3513 | if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) { |
| 3514 | return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction"); |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | // Enforce rule that the coinbase starts with serialized block height |
| 3519 | if (nHeight >= consensusParams.BIP34Height) |
| 3520 | { |
| 3521 | CScript expect = CScript() << nHeight; |
| 3522 | if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() || |
| 3523 | !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) { |
| 3524 | return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase"); |
| 3525 | } |
| 3526 | } |
| 3527 | |
| 3528 | if (nHeight >= consensusParams.BTGHeight && |
| 3529 | nHeight < consensusParams.BTGHeight + consensusParams.BTGPremineWindow && |
| 3530 | consensusParams.BTGPremineEnforceWhitelist) |
| 3531 | { |
| 3532 | if (block.vtx[0]->vout.size() != 1) { |
| 3533 | return state.DoS( |
| 3534 | 100, error("%s: only one coinbase output is allowed",__func__), |
| 3535 | REJECT_INVALID, "bad-premine-coinbase-output"); |
| 3536 | } |
| 3537 | const CTxOut& output = block.vtx[0]->vout[0]; |
| 3538 | bool valid = Params().IsPremineAddressScript(output.scriptPubKey, (uint32_t)nHeight); |
| 3539 | if (!valid) { |
| 3540 | return state.DoS( |
| 3541 | 100, error("%s: not in premine whitelist", __func__), |
| 3542 | REJECT_INVALID, "bad-premine-coinbase-scriptpubkey"); |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | |
| 3547 | // Validation for witness commitments. |
| 3548 | // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the |
| 3549 | // coinbase (where 0x0000....0000 is used instead). |
| 3550 | // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness reserved value (unconstrained). |
| 3551 | // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header). |
| 3552 | // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are |
| 3553 | // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness reserved value). In case there are |
| 3554 | // multiple, the last one is used. |