| 3568 | } |
| 3569 | |
| 3570 | bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot) |
| 3571 | { |
| 3572 | // These are checks that are independent of context. |
| 3573 | |
| 3574 | if (block.fChecked) |
| 3575 | return true; |
| 3576 | |
| 3577 | // Check that the header is valid (particularly PoW). This is mostly |
| 3578 | // redundant with the call in AcceptBlockHeader. |
| 3579 | if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW)) |
| 3580 | return false; |
| 3581 | |
| 3582 | // Signet only: check block solution |
| 3583 | if (consensusParams.signet_blocks && fCheckPOW && !CheckSignetBlockSolution(block, consensusParams)) { |
| 3584 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-signet-blksig", "signet block signature validation failure"); |
| 3585 | } |
| 3586 | |
| 3587 | // Check the merkle root. |
| 3588 | if (fCheckMerkleRoot) { |
| 3589 | bool mutated; |
| 3590 | uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated); |
| 3591 | if (block.hashMerkleRoot != hashMerkleRoot2) |
| 3592 | return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-txnmrklroot", "hashMerkleRoot mismatch"); |
| 3593 | |
| 3594 | // Check for merkle tree malleability (CVE-2012-2459): repeating sequences |
| 3595 | // of transactions in a block without affecting the merkle root of a block, |
| 3596 | // while still invalidating it. |
| 3597 | if (mutated) |
| 3598 | return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-txns-duplicate", "duplicate transaction"); |
| 3599 | } |
| 3600 | |
| 3601 | // All potential-corruption validation must be done before we do any |
| 3602 | // transaction validation, as otherwise we may mark the header as invalid |
| 3603 | // because we receive the wrong transactions for it. |
| 3604 | // Note that witness malleability is checked in ContextualCheckBlock, so no |
| 3605 | // checks that use witness data may be performed here. |
| 3606 | |
| 3607 | // Size limits |
| 3608 | if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) |
| 3609 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-length", "size limits failed"); |
| 3610 | |
| 3611 | // First transaction must be coinbase, the rest must not be |
| 3612 | if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) |
| 3613 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-missing", "first tx is not coinbase"); |
| 3614 | for (unsigned int i = 1; i < block.vtx.size(); i++) |
| 3615 | if (block.vtx[i]->IsCoinBase()) |
| 3616 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-multiple", "more than one coinbase"); |
| 3617 | |
| 3618 | // Check transactions |
| 3619 | // Must check for duplicate inputs (see CVE-2018-17144) |
| 3620 | for (const auto& tx : block.vtx) { |
| 3621 | TxValidationState tx_state; |
| 3622 | if (!CheckTransaction(*tx, tx_state)) { |
| 3623 | // CheckBlock() does context-free validation checks. The only |
| 3624 | // possible failures are consensus failures. |
| 3625 | assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS); |
| 3626 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(), |
| 3627 | strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), tx_state.GetDebugMessage())); |