Exemplo n.º 1
0
    def get_broadcast_message_preview(
            self, input_buffer: InputBuffer) -> BroadcastMessagePreview:
        """
        Peeks the hash and network number from hashed messages.
        Currently, only Broadcast messages are supported here.
        :param input_buffer
        :return: is full header, message hash, network number, source id, payload length
        """
        # -1 for control flag length
        broadcast_header_length = self.base_message_type.HEADER_LENGTH + AbstractBroadcastMessage.PAYLOAD_LENGTH - \
                                  constants.CONTROL_FLAGS_LEN
        is_full_header = input_buffer.length >= broadcast_header_length
        if not is_full_header:
            return BroadcastMessagePreview(False, None, None, None, None, None,
                                           None)
        else:
            _is_full_message, _command, payload_length = self.get_message_header_preview_from_input_buffer(
                input_buffer)

            broadcast_header = input_buffer.peek_message(
                broadcast_header_length)

            offset = self.base_message_type.HEADER_LENGTH

            block_hash = broadcast_header[offset:offset +
                                          crypto.SHA256_HASH_LEN]
            block_hash_with_network_num = broadcast_header[
                offset:offset + crypto.SHA256_HASH_LEN +
                constants.NETWORK_NUM_LEN]
            offset += crypto.SHA256_HASH_LEN

            network_num, = struct.unpack_from(
                "<L",
                broadcast_header[offset:offset + constants.NETWORK_NUM_LEN])
            offset += constants.NETWORK_NUM_LEN

            source_id = uuid_pack.from_bytes(
                struct.unpack_from(
                    "<16s",
                    broadcast_header[offset:offset +
                                     constants.NODE_ID_SIZE_IN_BYTES])[0])
            offset += constants.NODE_ID_SIZE_IN_BYTES

            broadcast_type_bytearray = broadcast_header[offset:offset +
                                                        constants.
                                                        BROADCAST_TYPE_LEN]
            broadcast_type_in_str = struct.unpack_from(
                "<4s", broadcast_type_bytearray)[0].decode(
                    constants.DEFAULT_TEXT_ENCODING)
            broadcast_type = BroadcastMessageType(broadcast_type_in_str)
            message_id = ConcatHash(
                bytearray(block_hash_with_network_num) +
                broadcast_type_bytearray, 0)

            return BroadcastMessagePreview(is_full_header,
                                           Sha256Hash(block_hash),
                                           broadcast_type, message_id,
                                           network_num, source_id,
                                           payload_length)
Exemplo n.º 2
0
    def broadcast_type(self) -> BroadcastMessageType:
        if self._broadcast_type is None:
            off = self.HEADER_LENGTH + AbstractBroadcastMessage.PAYLOAD_LENGTH - constants.CONTROL_FLAGS_LEN
            broadcast_type_in_str = struct.unpack_from(
                "<4s", self.buf,
                off)[0].decode(constants.DEFAULT_TEXT_ENCODING)
            self._broadcast_type = BroadcastMessageType(broadcast_type_in_str)

        broadcast_type = self._broadcast_type
        assert broadcast_type is not None
        return broadcast_type