def test_only_79_bytes() -> None: fname = "block_1.bin" filename = path.join(path.dirname(__file__), "test_data", fname) header_bytes = open(filename, "rb").read() header_bytes = header_bytes[:70] with pytest.raises(IndexError): BlockHeader.deserialize(header_bytes)
def deserialize(cls, data): stream = bytesio_from_binarydata(data) header = BlockHeader.deserialize(stream) index = varint.decode(stream) status = BlockStatus.from_bytes(stream.read(1), "little") downloaded = bool(int.from_bytes(stream.read(1), "little")) return cls(header, index, status, downloaded)
def deserialize(cls, data): stream = bytesio_from_binarydata(data) headers_num = varint.decode(stream) headers = [] for x in range(headers_num): header = BlockHeader.deserialize(stream) stream.read(1) headers.append(header) return cls(headers)
def test_dataclasses_json_dict() -> None: fname = "block_481824.bin" filename = path.join(path.dirname(__file__), "test_data", fname) block = open(filename, "rb").read() # dataclass block_data = Block.deserialize(block) assert isinstance(block_data, Block) # str block_json_str = block_data.to_json() assert isinstance(block_json_str, str) assert block_data == Block.from_json(block_json_str) # dict block_dict = block_data.to_dict() assert isinstance(block_dict, dict) assert block_data == Block.from_dict(block_dict) import json datadir = path.join(path.dirname(__file__), "generated_files") filename = path.join(datadir, "block_481824.json") with open(filename, "w") as f: json.dump(block_dict, f, indent=True) block_header = block_data.header.serialize() # dataclass block_header_data = BlockHeader.deserialize(block_header) assert isinstance(block_header_data, BlockHeader) # str block_header_s = block_header_data.to_json() assert isinstance(block_header_s, str) assert block_header_data == BlockHeader.from_json(block_header_s) # dict block_header_d = block_header_data.to_dict() assert isinstance(block_header_d, dict) assert block_header_data == BlockHeader.from_dict(block_header_d) filename = path.join(datadir, "block_header_481824.json") with open(filename, "w") as f: json.dump(block_header_d, f, indent=True)
def deserialize(cls, data): stream = bytesio_from_binarydata(data) header = BlockHeader.deserialize(stream) nonce = int.from_bytes(stream.read(8), "little") short_ids = [] short_ids_length = varint.decode(stream) for x in range(short_ids_length): short_ids.append(stream.read(6)[::-1].hex()) prefilled_tx_list = [] prefilled_tx_num = varint.decode(stream) for x in range(prefilled_tx_num): tx_index = varint.decode(stream) tx = Tx.deserialize(stream) prefilled_tx_list.append((tx_index, tx)) return cls( header=header, nonce=nonce, short_ids=short_ids, prefilled_tx_list=prefilled_tx_list, )