MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / CheckBlock

Function CheckBlock

src/validation.cpp:3928–3993  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3926}
3927
3928bool 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 }

Callers 11

ConnectBlockMethod · 0.85
AcceptBlockMethod · 0.85
ProcessNewBlockMethod · 0.85
TestBlockValidityFunction · 0.85
VerifyDBMethod · 0.85
BOOST_FIXTURE_TEST_CASEFunction · 0.85
BOOST_FIXTURE_TEST_CASEFunction · 0.85
block.cppFile · 0.85
CheckBlockTestFunction · 0.85
DuplicateInputsFunction · 0.85
btck_block_checkFunction · 0.85

Calls 15

CheckBlockHeaderFunction · 0.85
CheckSignetBlockSolutionFunction · 0.85
CheckMerkleRootFunction · 0.85
GetSerializeSizeFunction · 0.85
GetLegacySigOpCountFunction · 0.85
InvalidMethod · 0.80
GetResultMethod · 0.80
GetRejectReasonMethod · 0.80
GetDebugMessageMethod · 0.80
CheckTransactionFunction · 0.50
emptyMethod · 0.45
sizeMethod · 0.45

Tested by 3

TestBlockValidityFunction · 0.68
BOOST_FIXTURE_TEST_CASEFunction · 0.68
BOOST_FIXTURE_TEST_CASEFunction · 0.68