| 4442 | } |
| 4443 | |
| 4444 | bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) |
| 4445 | { |
| 4446 | const char * const s = block.IsProofOfStake() ? "pos" : "pow"; |
| 4447 | |
| 4448 | // These are checks that are independent of context. |
| 4449 | |
| 4450 | // Check that the header is valid (particularly PoW). This is mostly |
| 4451 | // redundant with the call in AcceptBlockHeader. |
| 4452 | if (fCheckPOW && !CheckBlockHeader(block, state, consensusParams, fCheckPOW && block.IsProofOfWork())) |
| 4453 | return state.DoS(100, error("%s: invalid (%s) block header", __func__, s), |
| 4454 | REJECT_INVALID, "bad-header", true); |
| 4455 | |
| 4456 | // 3 minute future drift for PoS |
| 4457 | auto const nBlockTimeLimit = GetAdjustedTime() + (block.IsProofOfStake() ? 180 : 7200); |
| 4458 | |
| 4459 | LogPrint("debug", "%s: block=%s (%s %d %d)\n", __func__, block.GetHash().GetHex(), s, |
| 4460 | block.GetBlockTime(), nBlockTimeLimit); |
| 4461 | |
| 4462 | // Check block time, reject far future blocks. |
| 4463 | if (block.GetBlockTime() > nBlockTimeLimit) |
| 4464 | return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future"); |
| 4465 | |
| 4466 | // Check the merkle root. |
| 4467 | if (fCheckMerkleRoot) { |
| 4468 | bool mutated; |
| 4469 | uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated); |
| 4470 | if (block.hashMerkleRoot != hashMerkleRoot2) |
| 4471 | return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch"); |
| 4472 | |
| 4473 | // Check for merkle tree malleability (CVE-2012-2459): repeating sequences |
| 4474 | // of transactions in a block without affecting the merkle root of a block, |
| 4475 | // while still invalidating it. |
| 4476 | if (mutated) |
| 4477 | return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction"); |
| 4478 | } |
| 4479 | |
| 4480 | // All potential-corruption validation must be done before we do any |
| 4481 | // transaction validation, as otherwise we may mark the header as invalid |
| 4482 | // because we receive the wrong transactions for it. |
| 4483 | // Note that witness malleability is checked in ContextualCheckBlock, so no |
| 4484 | // checks that use witness data may be performed here. |
| 4485 | |
| 4486 | // Size limits |
| 4487 | if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_BASE_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_BASE_SIZE) |
| 4488 | return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed"); |
| 4489 | |
| 4490 | // First transaction must be coinbase, the rest must not be |
| 4491 | if (block.vtx.empty() || !block.vtx[0].IsCoinBase()) |
| 4492 | return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase"); |
| 4493 | |
| 4494 | for (unsigned int i = 1; i < block.vtx.size(); i++) |
| 4495 | if (block.vtx[i].IsCoinBase()) |
| 4496 | return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase"); |
| 4497 | |
| 4498 | if (block.IsProofOfStake()) { |
| 4499 | // Coinbase output should be empty if proof-of-stake block |
| 4500 | int commitpos = GetWitnessCommitmentIndex(block); |
| 4501 | if (block.vtx[0].vout.size() != (commitpos == -1 ? 1 : 2) || !block.vtx[0].vout[0].IsEmpty()) |