| 3301 | } |
| 3302 | |
| 3303 | bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot) |
| 3304 | { |
| 3305 | // These are checks that are independent of context. |
| 3306 | |
| 3307 | if (block.fChecked) |
| 3308 | return true; |
| 3309 | |
| 3310 | // Check that the header is valid (particularly PoW). This is mostly |
| 3311 | // redundant with the call in AcceptBlockHeader. |
| 3312 | if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW)) |
| 3313 | return false; |
| 3314 | |
| 3315 | // Check the merkle root. |
| 3316 | if (fCheckMerkleRoot) { |
| 3317 | bool mutated; |
| 3318 | uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated); |
| 3319 | if (block.hashMerkleRoot != hashMerkleRoot2) |
| 3320 | return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch"); |
| 3321 | |
| 3322 | // Check for merkle tree malleability (CVE-2012-2459): repeating sequences |
| 3323 | // of transactions in a block without affecting the merkle root of a block, |
| 3324 | // while still invalidating it. |
| 3325 | if (mutated) |
| 3326 | return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction"); |
| 3327 | } |
| 3328 | |
| 3329 | // All potential-corruption validation must be done before we do any |
| 3330 | // transaction validation, as otherwise we may mark the header as invalid |
| 3331 | // because we receive the wrong transactions for it. |
| 3332 | // Note that witness malleability is checked in ContextualCheckBlock, so no |
| 3333 | // checks that use witness data may be performed here. |
| 3334 | |
| 3335 | // Size limits |
| 3336 | int serialization_flags = SERIALIZE_TRANSACTION_NO_WITNESS; |
| 3337 | if (block.nHeight < (uint32_t)consensusParams.BTGHeight) { |
| 3338 | serialization_flags |= SERIALIZE_BLOCK_LEGACY; |
| 3339 | } |
| 3340 | if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | serialization_flags) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) |
| 3341 | return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed"); |
| 3342 | |
| 3343 | // First transaction must be coinbase, the rest must not be |
| 3344 | if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) |
| 3345 | return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase"); |
| 3346 | for (unsigned int i = 1; i < block.vtx.size(); i++) |
| 3347 | if (block.vtx[i]->IsCoinBase()) |
| 3348 | return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase"); |
| 3349 | |
| 3350 | // Check transactions |
| 3351 | for (const auto& tx : block.vtx) |
| 3352 | if (!CheckTransaction(*tx, state, true)) |
| 3353 | return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(), |
| 3354 | strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage())); |
| 3355 | |
| 3356 | unsigned int nSigOps = 0; |
| 3357 | for (const auto& tx : block.vtx) |
| 3358 | { |
| 3359 | nSigOps += GetLegacySigOpCount(*tx); |
| 3360 | } |