| 3926 | } |
| 3927 | |
| 3928 | bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot) |
| 3929 | { |
| 3930 | // These are checks that are independent of context. |
| 3931 | |
| 3932 | if (block.fChecked) |
| 3933 | return true; |
| 3934 | |
| 3935 | // Check that the header is valid (particularly PoW). This is mostly |
| 3936 | // redundant with the call in AcceptBlockHeader. |
| 3937 | if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW)) |
| 3938 | return false; |
| 3939 | |
| 3940 | // Signet only: check block solution |
| 3941 | if (consensusParams.signet_blocks && fCheckPOW && !CheckSignetBlockSolution(block, consensusParams)) { |
| 3942 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-signet-blksig", "signet block signature validation failure"); |
| 3943 | } |
| 3944 | |
| 3945 | // Check the merkle root. |
| 3946 | if (fCheckMerkleRoot && !CheckMerkleRoot(block, state)) { |
| 3947 | return false; |
| 3948 | } |
| 3949 | |
| 3950 | // All potential-corruption validation must be done before we do any |
| 3951 | // transaction validation, as otherwise we may mark the header as invalid |
| 3952 | // because we receive the wrong transactions for it. |
| 3953 | // Note that witness malleability is checked in ContextualCheckBlock, so no |
| 3954 | // checks that use witness data may be performed here. |
| 3955 | |
| 3956 | // Size limits |
| 3957 | if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(TX_NO_WITNESS(block)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) |
| 3958 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-length", "size limits failed"); |
| 3959 | |
| 3960 | // First transaction must be coinbase, the rest must not be |
| 3961 | if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) |
| 3962 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-missing", "first tx is not coinbase"); |
| 3963 | for (unsigned int i = 1; i < block.vtx.size(); i++) |
| 3964 | if (block.vtx[i]->IsCoinBase()) |
| 3965 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-multiple", "more than one coinbase"); |
| 3966 | |
| 3967 | // Check transactions |
| 3968 | // Must check for duplicate inputs (see CVE-2018-17144) |
| 3969 | for (const auto& tx : block.vtx) { |
| 3970 | TxValidationState tx_state; |
| 3971 | if (!CheckTransaction(*tx, tx_state)) { |
| 3972 | // CheckBlock() does context-free validation checks. The only |
| 3973 | // possible failures are consensus failures. |
| 3974 | assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS); |
| 3975 | return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(), |
| 3976 | strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), tx_state.GetDebugMessage())); |
| 3977 | } |
| 3978 | } |
| 3979 | // This underestimates the number of sigops, because unlike ConnectBlock it |
| 3980 | // does not count witness and p2sh sigops. |
| 3981 | unsigned int nSigOps = 0; |
| 3982 | for (const auto& tx : block.vtx) |
| 3983 | { |
| 3984 | nSigOps += GetLegacySigOpCount(*tx); |
| 3985 | } |