def deserialize(self, reader: serialization.BinaryReader) -> None: self.magic = reader.read_uint32() compiler_with_version = reader.read_bytes(64).decode('utf-8') self.compiler, version = compiler_with_version.rsplit('-', maxsplit=1) self.version = Version.deserialize_from_bytes( version[:32].encode('utf-8')) assert reader.read_uint16() == 0 # 2 reserved bytes reader.read_var_int(128) # TODO: method tokens assert reader.read_uint16() == 0 # 2 reserved bytes self.script = reader.read_var_bytes() self.checksum = reader.read_bytes(4) if self.checksum != self.compute_checksum(): raise ValueError("Deserialization error - invalid checksum")
def deserialize(self, reader: serialization.BinaryReader) -> None: """ Deserialize the object from a binary stream. Args: reader: instance. Raises: ValueError: if the content count of the block is zero, or if there is a duplicate transaction in the list, or if the merkle root does not included the calculated root. """ super(Block, self).deserialize(reader) content_count = reader.read_var_int(max=self.MAX_CONTENTS_PER_BLOCK) if content_count == 0: raise ValueError("Deserialization error - no contents") self.consensus_data = reader.read_serializable(payloads.ConsensusData) tx_count = content_count - 1 for _ in range(tx_count): self.transactions.append( reader.read_serializable(payloads.Transaction)) if len(set(self.transactions)) != tx_count: raise ValueError( "Deserialization error - block contains duplicate transaction") hashes = [t.hash() for t in self.transactions] if Block.calculate_merkle_root(self.consensus_data.hash(), hashes) != self.merkle_root: raise ValueError("Deserialization error - merkle root mismatch")
def deserialize_binary(reader: BinaryReader) -> Any: deserialized_items = [] underserialized = 1 while underserialized > 0: stack_type = reader.read_byte() if stack_type == StackItemType.Any: deserialized_items.append(None) elif stack_type == StackItemType.Boolean: deserialized_items.append(reader.read_bool()) elif stack_type == StackItemType.Integer: value_in_bytes = reader.read_var_bytes() deserialized_items.append(Integer.from_bytes(value_in_bytes)) elif stack_type in (StackItemType.ByteString, StackItemType.Buffer): deserialized_items.append(reader.read_var_bytes()) elif stack_type in (StackItemType.Array, StackItemType.Struct): count = reader.read_var_int() deserialized_items.append(list(range(count))) underserialized += count elif stack_type == StackItemType.Map: count = reader.read_var_int() deserialized_items.append({key: None for key in range(count)}) underserialized += count * 2 else: raise ValueError underserialized -= 1 stack_temp = [] while len(deserialized_items) > 0: item = deserialized_items.pop() if isinstance(item, list): new_item = [] for _ in range(len(item)): new_item.append(stack_temp.pop()) item = new_item elif isinstance(item, dict): new_item = {} for _ in range(0, len(item), 2): key = stack_temp.pop() value = stack_temp.pop() new_item[key] = value item = new_item stack_temp.append(item) return stack_temp.pop()
def deserialize(self, reader: serialization.BinaryReader) -> None: """ Deserialize the object from a binary stream. Args: reader: instance. """ self.primary_index = reader.read_var_int(max=1024) # comes from C#'s Clockchain.MaxValidators self.nonce = reader.read_uint64()
def deserialize(self, reader: serialization.BinaryReader) -> None: """ Deserialize the object from a binary stream. Args: reader: instance. """ super(MerkleBlockPayload, self).deserialize(reader) self.content_count = reader.read_var_int() self.hashes = reader.read_serializable_list(types.UInt256) self.flags = reader.read_var_bytes()
def deserialize(self, reader: serialization.BinaryReader) -> None: """ Deserialize the object from a binary stream. Args: reader: instance. """ addr_list_len = reader.read_var_int() for i in range(0, addr_list_len): nawt = NetworkAddress() nawt.deserialize(reader) self.addresses.append(nawt)
def deserialize(self, reader: serialization.BinaryReader) -> None: """ Deserialize the object from a binary stream. Args: reader: instance. """ self.magic = reader.read_uint32() self.version = reader.read_uint32() self.timestamp = reader.read_uint32() self.nonce = reader.read_uint32() self.user_agent = reader.read_var_string(max=1024) capabilities_cnt = reader.read_var_int(self.MAX_CAPABILITIES) capabilities_list = [] for _ in range(capabilities_cnt): capabilities_list.append(capabilities.NodeCapability.deserialize_from(reader)) self.capabilities = capabilities_list
def deserialize(self, reader: serialization.BinaryReader) -> None: """ Deserialize the object from a binary stream. Args: reader: instance. """ self.timestamp = reader.read_uint32() full_address_bytes = bytearray(reader.read_bytes(16)) ip_bytes = full_address_bytes[-4:] host = '.'.join(map(lambda b: str(b), ip_bytes)) port = 0 capabilities_cnt = reader.read_var_int(VersionPayload.MAX_CAPABILITIES) capabilities_list = [] for _ in range(capabilities_cnt): capa = capabilities.NodeCapability.deserialize_from(reader) if isinstance(capa, capabilities.ServerCapability): port = capa.port capabilities_list.append(capa) self.capabilities = capabilities_list self.address = f"{host}:{port}"