MCPcopy
hub / github.com/petertodd/python-bitcoinlib / CheckBlock

Function CheckBlock

bitcoin/core/__init__.py:872–948  ·  view source on GitHub ↗

Context independent CBlock checks. CheckBlockHeader() is called first, which may raise a CheckBlockHeader exception, followed the block tests. CheckTransaction() is called for every transaction. fCheckPoW - Check proof-of-work. fCheckMerkleRoot - Check merkle root and w

(block, fCheckPoW = True, fCheckMerkleRoot = True, cur_time=None)

Source from the content-addressed store, hash-verified

870
871
872def CheckBlock(block, fCheckPoW = True, fCheckMerkleRoot = True, cur_time=None):
873 """Context independent CBlock checks.
874
875 CheckBlockHeader() is called first, which may raise a CheckBlockHeader
876 exception, followed the block tests. CheckTransaction() is called for every
877 transaction.
878
879 fCheckPoW - Check proof-of-work.
880
881 fCheckMerkleRoot - Check merkle root and witness merkle root matches transactions.
882 - Check witness commitment in coinbase
883
884 cur_time - Current time. Defaults to time.time()
885 """
886
887 # Block header checks
888 CheckBlockHeader(block.get_header(), fCheckPoW=fCheckPoW, cur_time=cur_time)
889
890 # Size limits
891 if not block.vtx:
892 raise CheckBlockError("CheckBlock() : vtx empty")
893 if len(block.serialize(dict(include_witness=False))) > MAX_BLOCK_SIZE:
894 raise CheckBlockError("CheckBlock() : block larger than MAX_BLOCK_SIZE")
895
896 if block.GetWeight() > MAX_BLOCK_WEIGHT:
897 raise CheckBlockError("CheckBlock() : block larger than MAX_BLOCK_WEIGHT")
898
899 # First transaction must be coinbase
900 if not block.vtx[0].is_coinbase():
901 raise CheckBlockError("CheckBlock() : first tx is not coinbase")
902
903 # Check rest of transactions. Note how we do things "all at once", which
904 # could potentially be a consensus failure if there was some obscure bug.
905
906 # For unique txid uniqueness testing. If coinbase tx is included twice
907 # it'll be caught by the "more than one coinbase" test.
908 unique_txids = set()
909 nSigOps = 0
910 for tx in block.vtx[1:]:
911 if tx.is_coinbase():
912 raise CheckBlockError("CheckBlock() : more than one coinbase")
913
914 CheckTransaction(tx)
915
916 txid = tx.GetTxid()
917 if txid in unique_txids:
918 raise CheckBlockError("CheckBlock() : duplicate transaction")
919 unique_txids.add(txid)
920
921 nSigOps += GetLegacySigOpCount(tx)
922 if nSigOps > MAX_BLOCK_SIGOPS:
923 raise CheckBlockError("CheckBlock() : out-of-bounds SigOpCount")
924
925 # Check merkle root
926 if fCheckMerkleRoot:
927 if block.hashMerkleRoot != block.calc_merkle_root():
928 raise CheckBlockError("CheckBlock() : hashMerkleRoot mismatch")
929 if len(block.vWitnessMerkleTree):

Callers 3

test_checkblockMethod · 0.85
test_checkblock_validMethod · 0.85

Calls 12

CheckBlockHeaderFunction · 0.85
CheckBlockErrorClass · 0.85
CheckTransactionFunction · 0.85
GetLegacySigOpCountFunction · 0.85
HashFunction · 0.85
get_headerMethod · 0.80
GetWeightMethod · 0.80
is_coinbaseMethod · 0.80
GetTxidMethod · 0.80
calc_merkle_rootMethod · 0.80
serializeMethod · 0.45

Tested by 3

test_checkblockMethod · 0.68
test_checkblock_validMethod · 0.68