| 3297 | } |
| 3298 | |
| 3299 | bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot) |
| 3300 | { |
| 3301 | // These are checks that are independent of context. |
| 3302 | |
| 3303 | // Check that the header is valid (particularly PoW). This is mostly |
| 3304 | // redundant with the call in AcceptBlockHeader. |
| 3305 | if (!CheckBlockHeader(block, state, fCheckPOW)) |
| 3306 | return false; |
| 3307 | |
| 3308 | // Check the merkle root. |
| 3309 | if (fCheckMerkleRoot) { |
| 3310 | bool mutated; |
| 3311 | uint256 hashMerkleRoot2 = block.BuildMerkleTree(&mutated); |
| 3312 | if (block.hashMerkleRoot != hashMerkleRoot2) |
| 3313 | return state.DoS(100, error("CheckBlock(): hashMerkleRoot mismatch"), |
| 3314 | REJECT_INVALID, "bad-txnmrklroot", true); |
| 3315 | |
| 3316 | // Check for merkle tree malleability (CVE-2012-2459): repeating sequences |
| 3317 | // of transactions in a block without affecting the merkle root of a block, |
| 3318 | // while still invalidating it. |
| 3319 | if (mutated) |
| 3320 | return state.DoS(100, error("CheckBlock(): duplicate transaction"), |
| 3321 | REJECT_INVALID, "bad-txns-duplicate", true); |
| 3322 | } |
| 3323 | |
| 3324 | // All potential-corruption validation must be done before we do any |
| 3325 | // transaction validation, as otherwise we may mark the header as invalid |
| 3326 | // because we receive the wrong transactions for it. |
| 3327 | |
| 3328 | // Size limits |
| 3329 | if (block.vtx.empty()) |
| 3330 | return state.DoS(100, error("CheckBlock(): no transactions"), REJECT_INVALID, "bad-blk-length"); |
| 3331 | |
| 3332 | // First transaction must be coinbase, the rest must not be |
| 3333 | if (block.vtx.empty() || !block.vtx[0].IsCoinBase()) |
| 3334 | return state.DoS(100, error("CheckBlock(): first tx is not coinbase"), |
| 3335 | REJECT_INVALID, "bad-cb-missing"); |
| 3336 | for (unsigned int i = 1; i < block.vtx.size(); i++) |
| 3337 | if (block.vtx[i].IsCoinBase()) |
| 3338 | return state.DoS(100, error("CheckBlock(): more than one coinbase"), |
| 3339 | REJECT_INVALID, "bad-cb-multiple"); |
| 3340 | |
| 3341 | // Check transactions |
| 3342 | BOOST_FOREACH(const CTransaction& tx, block.vtx) |
| 3343 | if (!CheckTransaction(tx, state)) |
| 3344 | return error("CheckBlock(): CheckTransaction failed"); |
| 3345 | |
| 3346 | unsigned int nSigOps = 0; |
| 3347 | BOOST_FOREACH(const CTransaction& tx, block.vtx) |
| 3348 | { |
| 3349 | nSigOps += GetLegacySigOpCount(tx); |
| 3350 | } |
| 3351 | if (nSigOps > MaxBlockSigops(::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION))) |
| 3352 | return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"), REJECT_INVALID, "bad-blk-sigops", true); |
| 3353 | |
| 3354 | return true; |
| 3355 | } |
| 3356 | |