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

Class CBlock

test/functional/test_framework/messages.py:793–867  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

791assert_equal(BLOCK_HEADER_SIZE, 80)
792
793class CBlock(CBlockHeader):
794 __slots__ = ("vtx",)
795
796 def __init__(self, header=None):
797 super().__init__(header)
798 self.vtx = []
799
800 def deserialize(self, f):
801 super().deserialize(f)
802 self.vtx = deser_vector(f, CTransaction)
803
804 def serialize(self, with_witness=True):
805 r = b""
806 r += super().serialize()
807 if with_witness:
808 r += ser_vector(self.vtx, "serialize_with_witness")
809 else:
810 r += ser_vector(self.vtx, "serialize_without_witness")
811 return r
812
813 # Calculate the merkle root given a vector of transaction hashes
814 @classmethod
815 def get_merkle_root(cls, hashes):
816 while len(hashes) > 1:
817 newhashes = []
818 for i in range(0, len(hashes), 2):
819 i2 = min(i+1, len(hashes)-1)
820 newhashes.append(hash256(hashes[i] + hashes[i2]))
821 hashes = newhashes
822 return uint256_from_str(hashes[0])
823
824 def calc_merkle_root(self):
825 hashes = []
826 for tx in self.vtx:
827 hashes.append(ser_uint256(tx.txid_int))
828 return self.get_merkle_root(hashes)
829
830 def calc_witness_merkle_root(self):
831 # For witness root purposes, the hash of the
832 # coinbase, with witness, is defined to be 0...0
833 hashes = [ser_uint256(0)]
834
835 for tx in self.vtx[1:]:
836 # Calculate the hashes with witness data
837 hashes.append(ser_uint256(tx.wtxid_int))
838
839 return self.get_merkle_root(hashes)
840
841 def is_valid(self):
842 target = uint256_from_compact(self.nBits)
843 if self.hash_int > target:
844 return False
845 for tx in self.vtx:
846 if not tx.is_valid():
847 return False
848 if self.calc_merkle_root() != self.hashMerkleRoot:
849 return False
850 return True

Callers 14

build_block_on_tipMethod · 0.90
test_timewarpMethod · 0.90
run_testMethod · 0.90
mineMethod · 0.90
run_testMethod · 0.90
run_testMethod · 0.90
test_basicMethod · 0.90
mining_get_blockFunction · 0.90
create_blockFunction · 0.70

Calls

no outgoing calls

Tested by 5

test_timewarpMethod · 0.72
test_basicMethod · 0.72