Ejemplo n.º 1
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to v6: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + constants.NODE_ID_SIZE_IN_BYTES

        new_msg_bytes = bytearray(AbstractBloxrouteMessage.HEADER_LENGTH +
                                  new_payload_len)
        new_msg_bytes[:self._LEFT_BREAKPOINT] = msg.rawbytes(
        )[:self._LEFT_BREAKPOINT]
        new_msg_bytes[self._RIGHT_BREAKPOINT:] = msg.rawbytes(
        )[self._LEFT_BREAKPOINT:]

        # pack empty source id for old gateways, since message converters are singletons
        # and source ids for gateway don't matter anyway
        struct.pack_into("<16s", new_msg_bytes, self._LEFT_BREAKPOINT,
                         uuid_pack.to_bytes(""))

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
Ejemplo n.º 2
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to v9: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + constants.BROADCAST_TYPE_LEN

        new_msg_bytes = bytearray(AbstractBloxrouteMessage.HEADER_LENGTH +
                                  new_payload_len)
        new_msg_bytes[:self._BASE_LENGTH] = msg.rawbytes()[:self._BASE_LENGTH]
        struct.pack_into(
            "<4s",
            new_msg_bytes,
            self._BASE_LENGTH,
            # pylint: disable=no-member
            BroadcastMessageType.BLOCK.value.encode(
                constants.DEFAULT_TEXT_ENCODING))
        new_msg_bytes[self._BREAKPOINT:] = msg.rawbytes()[self._BASE_LENGTH:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
Ejemplo n.º 3
0
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected new message type to v8: {msg_type}"
            )

        old_version_msg_class = self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING[
            msg_type]
        old_version_payload_len = msg.payload_len(
        ) - constants.BROADCAST_TYPE_LEN

        old_version_msg_bytes = bytearray(
            self._BASE_LENGTH + len(msg.rawbytes()[self._BREAKPOINT:]))
        old_version_msg_bytes[:self._BASE_LENGTH] = msg.rawbytes(
        )[:self._BASE_LENGTH]
        old_version_msg_bytes[self._BASE_LENGTH:] = msg.rawbytes(
        )[self._BREAKPOINT:]

        return AbstractBloxrouteMessage.initialize_class(
            old_version_msg_class,
            old_version_msg_bytes,
            (msg_type, old_version_payload_len),
        )
Ejemplo n.º 4
0
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected new message type to v6: {msg_type}"
            )

        old_version_msg_class = self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING[
            msg_type]
        old_version_payload_len = (msg.payload_len() -
                                   constants.TRANSACTION_FLAG_LEN -
                                   constants.DOUBLE_SIZE_IN_BYTES)

        old_version_msg_bytes = bytearray(
            self._LEFT_BREAKPOINT +
            len(msg.rawbytes()[self._RIGHT_BREAKPOINT:]))
        old_version_msg_bytes[:self._LEFT_BREAKPOINT] = msg.rawbytes(
        )[:self._LEFT_BREAKPOINT]
        old_version_msg_bytes[self._LEFT_BREAKPOINT:] = msg.rawbytes(
        )[self._RIGHT_BREAKPOINT:]

        struct.pack_into(
            "<12sL",
            old_version_msg_bytes,
            AbstractBloxrouteMessage.STARTING_BYTES_LEN,
            msg_type,
            old_version_payload_len,
        )
        return AbstractBloxrouteMessage.initialize_class(
            old_version_msg_class,
            old_version_msg_bytes,
            (msg_type, old_version_payload_len),
        )
    def convert_from_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to "
                f"v7: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = (
            msg.payload_len() +
            constants.QUOTA_FLAG_LEN +
            constants.UL_INT_SIZE_IN_BYTES
        )

        new_msg_bytes = bytearray(
            AbstractBloxrouteMessage.HEADER_LENGTH + new_payload_len
        )
        new_msg_bytes[: self._LEFT_BREAKPOINT] = msg.rawbytes()[
            : self._LEFT_BREAKPOINT
        ]

        struct.pack_into("<B", new_msg_bytes, self._LEFT_BREAKPOINT, 0)
        struct.pack_into("<L", new_msg_bytes, self._LEFT_BREAKPOINT, 0)
        new_msg_bytes[self._RIGHT_BREAKPOINT :] = msg.rawbytes()[
            self._LEFT_BREAKPOINT :
        ]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len)
        )
    def convert_from_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type from v10: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + self._LENGTH_DIFFERENCE

        default_new_stats = 0
        new_msg_bytes = bytearray(self._NEW_MESSAGE_LEN)
        new_msg_bytes[:self._INTERVAL_TIMES_BREAKPOINT] = msg.rawbytes()[:self._INTERVAL_TIMES_BREAKPOINT]
        off = self._BASE_LENGTH + self._INTERVAL_TIMES_LENGTH

        # memory
        struct.pack_into("<H", new_msg_bytes, off, default_new_stats)
        off += constants.UL_SHORT_SIZE_IN_BYTES

        # single blockchain peer
        struct.pack_into("<H", new_msg_bytes, off, 1)
        off += constants.UL_SHORT_SIZE_IN_BYTES

        # placeholder ip/port
        message_utils.pack_ip_port(new_msg_bytes, off, "0.0.0.0", 0)
        off += constants.IP_ADDR_SIZE_IN_BYTES + constants.UL_SHORT_SIZE_IN_BYTES

        new_msg_bytes[off:off + self._FIRST_STATS_SETS_LENGTH] = \
            msg.rawbytes()[
            self._INTERVAL_TIMES_BREAKPOINT:self._INTERVAL_TIMES_BREAKPOINT + self._FIRST_STATS_SETS_LENGTH
            ]
        off += self._FIRST_STATS_SETS_LENGTH

        struct.pack_into("<I", new_msg_bytes, off, default_new_stats)
        off += constants.UL_INT_SIZE_IN_BYTES
        struct.pack_into("<I", new_msg_bytes, off, default_new_stats)
        off += constants.UL_INT_SIZE_IN_BYTES
        struct.pack_into("<I", new_msg_bytes, off, default_new_stats)
        off += constants.UL_INT_SIZE_IN_BYTES
        struct.pack_into("<I", new_msg_bytes, off, default_new_stats)
        off += constants.UL_INT_SIZE_IN_BYTES
        struct.pack_into("<I", new_msg_bytes, off, default_new_stats)
        off += constants.UL_INT_SIZE_IN_BYTES

        new_msg_bytes[off:] = msg.rawbytes()[self._FIRST_STATS_SETS_BREAKPOINT:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class,
            new_msg_bytes,
            (msg_type, new_payload_len)
        )
Ejemplo n.º 7
0
 def _serialize_deserialize_message(self,
                                    tx_message: TxMessage) -> TxMessage:
     tx_message_bytes = tx_message.rawbytes()
     reserialized_tx_message = AbstractInternalMessage.initialize_class(
         TxMessage, tx_message_bytes,
         (TxMessage.MESSAGE_TYPE, tx_message.payload_len()))
     self.assertEqual(tx_message_bytes, reserialized_tx_message.rawbytes())
     return reserialized_tx_message
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE
        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected new message type to v6: {msg_type}"
            )
        msg = cast(TxServiceSyncTxsMessage, msg)

        txs_content_short_ids = msg.txs_content_short_ids()
        txs_content_short_ids_v6 = [
            TxContentShortIdsV6(item.tx_hash, item.tx_content, item.short_ids)
            for item in txs_content_short_ids
        ]
        network_num = msg.network_num()

        return TxServiceSyncTxsMessageV6(network_num, txs_content_short_ids_v6)
Ejemplo n.º 9
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(f"Tried to convert unexpected new "
                             f"message type to v17: {msg_type}")

        msg = cast(TxMessageV17, msg)

        tx_hash = msg.tx_hash()
        network_num = msg.network_num()
        source_id = msg.source_id()
        short_id = msg.short_id()
        tx_val = msg.tx_val()
        transaction_flag = msg.transaction_flag()
        ts = msg.timestamp()

        return TxMessage(message_hash=tx_hash,
                         network_num=network_num,
                         source_id=source_id,
                         short_id=short_id,
                         tx_val=tx_val,
                         transaction_flag=transaction_flag,
                         timestamp=ts)
Ejemplo n.º 10
0
    def convert_from_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to "
                f"v15: {msg_type}"
            )

        msg = cast(TxMessageV15, msg)

        tx_hash = msg.tx_hash()
        network_num = msg.network_num()
        source_id = msg.source_id()
        short_id = msg.short_id()
        tx_val = msg.tx_val()
        quota_type = msg.quota_type()
        ts = msg.timestamp()

        if QuotaType.FREE_DAILY_QUOTA in quota_type:
            tx_flag = TransactionFlag.NO_FLAGS
        else:
            tx_flag = TransactionFlag.PAID_TX

        return TxMessage(
            message_hash=tx_hash,
            network_num=network_num,
            source_id=source_id,
            short_id=short_id,
            tx_val=tx_val,
            transaction_flag=tx_flag,
            timestamp=ts
        )
Ejemplo n.º 11
0
    def convert_to_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type != TxMessage.MESSAGE_TYPE:
            raise ValueError(
                f"Tried to convert unexpected new "
                f"message type to v17: {msg_type}"
            )

        msg = cast(TxMessage, msg)
        transaction_flag = msg.transaction_flag()

        if self.UNKNOWN_TRANSACTION_FLAGS & transaction_flag:
            transaction_flag &= self.INVERTED_UNKNOWN_TRANSACTION_FLAGS

        return TxMessageV17(
            msg.message_hash(),
            msg.network_num(),
            msg.source_id(),
            msg.short_id(),
            msg.tx_val(),
            transaction_flag,
            int(msg.timestamp())
        )
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to v6: {msg_type}"
            )
        msg = cast(TxServiceSyncTxsMessageV6, msg)

        txs_content_short_ids_v6 = msg.txs_content_short_ids()
        txs_content_short_ids = [
            TxContentShortIds(
                item.tx_hash, item.tx_content, item.short_ids,
                [TransactionFlag.NO_FLAGS for _ in item.short_ids])
            for item in txs_content_short_ids_v6
        ]
        network_num = msg.network_num()

        return TxServiceSyncTxsMessage(network_num, txs_content_short_ids)
Ejemplo n.º 13
0
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Unexpected got message of type {msg_type}. Could not convert to version 4."
            )

        old_version_msg_class = self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING[
            msg_type]
        old_version_payload_len = msg.payload_len(
        ) - constants.CONTROL_FLAGS_LEN
        old_version_msg_bytes = msg.rawbytes(
        )[constants.STARTING_SEQUENCE_BYTES_LEN:-constants.CONTROL_FLAGS_LEN]
        struct.pack_into("<12sL", old_version_msg_bytes, 0, msg_type,
                         old_version_payload_len)

        return MessageV4.initialize_class(old_version_msg_class,
                                          old_version_msg_bytes,
                                          (msg_type, old_version_payload_len))
Ejemplo n.º 14
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type from v10: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + self._LENGTH_DIFFERENCE

        new_msg_bytes = bytearray(self._NEW_MESSAGE_LEN)

        new_msg_bytes[:self._BREAKPOINT] = msg.rawbytes()[:self._BREAKPOINT]

        new_msg_bytes[self._BREAKPOINT +
                      self._LENGTH_DIFFERENCE:] = msg.rawbytes()[self.
                                                                 _BREAKPOINT:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
Ejemplo n.º 15
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Unexpected got message of type {msg_type}. Could not convert to current version."
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + constants.CONTROL_FLAGS_LEN
        new_msg_bytes = bytearray(AbstractBloxrouteMessage.HEADER_LENGTH +
                                  new_payload_len)
        new_msg_bytes[:constants.
                      STARTING_SEQUENCE_BYTES_LEN] = constants.STARTING_SEQUENCE_BYTES
        struct.pack_into("<12sL", new_msg_bytes,
                         constants.STARTING_SEQUENCE_BYTES_LEN, msg_type,
                         new_payload_len)
        new_msg_bytes[constants.STARTING_SEQUENCE_BYTES_LEN:-constants.
                      CONTROL_FLAGS_LEN] = msg.rawbytes()
        new_msg_bytes[-1] = BloxrouteMessageControlFlags.VALID

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
Ejemplo n.º 16
0
    def convert_to_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected new "
                f"message type to v7: {msg_type}"
            )

        old_version_msg_class = self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING[
            msg_type
        ]
        old_version_payload_len = (
            msg.payload_len()
            - constants.QUOTA_FLAG_LEN
            - constants.UL_INT_SIZE_IN_BYTES
        )

        old_version_msg_bytes = bytearray(
            self._LEFT_BREAKPOINT
            + len(msg.rawbytes()[self._RIGHT_BREAKPOINT :])
        )
        old_version_msg_bytes[: self._LEFT_BREAKPOINT] = msg.rawbytes()[
            : self._LEFT_BREAKPOINT
        ]
        old_version_msg_bytes[self._LEFT_BREAKPOINT :] = msg.rawbytes()[
            self._RIGHT_BREAKPOINT :
        ]

        return AbstractBloxrouteMessage.initialize_class(
            old_version_msg_class,
            old_version_msg_bytes,
            (msg_type, old_version_payload_len),
        )
Ejemplo n.º 17
0
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:

        if not isinstance(msg, GatewayHelloMessageV2):
            raise TypeError("GatewayHelloMessage is expected")

        msg_bytes = msg.rawbytes()
        mem_view = memoryview(msg_bytes)

        result_bytes = bytearray(
            len(msg_bytes) - GatewayHelloMessageV2.ADDITIONAL_LENGTH)
        payload_len = GatewayHelloMessageV1.PAYLOAD_LENGTH

        result_bytes[:] = mem_view[:len(msg_bytes) -
                                   GatewayHelloMessageV2.ADDITIONAL_LENGTH]

        return AbstractBloxrouteMessage.initialize_class(
            GatewayHelloMessageV1, result_bytes,
            (GatewayMessageType.HELLO, payload_len))
Ejemplo n.º 18
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:

        if not isinstance(msg, GatewayHelloMessageV1):
            raise TypeError("GatewayHelloMessageV1 is expected")

        msg_bytes = msg.rawbytes()
        mem_view = memoryview(msg_bytes)

        result_bytes = bytearray(
            len(msg_bytes) + constants.NODE_ID_SIZE_IN_BYTES)
        payload_len = GatewayHelloMessageV2.PAYLOAD_LENGTH
        off = 0
        result_bytes[off:len(msg_bytes)] = mem_view[:]
        off = len(msg_bytes)
        struct.pack_into("%ss" % constants.NODE_ID_SIZE_IN_BYTES, result_bytes,
                         off, b'')

        return AbstractBloxrouteMessage.initialize_class(
            GatewayHelloMessageV2, result_bytes,
            (GatewayMessageType.HELLO, payload_len))
    def convert_from_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type from v18: {msg_type}"
            )
        if len(msg.rawbytes()) == self._MESSAGE_LEN_WITHOUT_NODE_STATS:
            # per Endpoint Stats is not available
            length_difference = 0
            new_message_len = self._MESSAGE_LEN_WITHOUT_NODE_STATS
            num_blockchain_peers = 0
        else:
            num_blockchain_peers, = struct.unpack_from("<I", msg.rawbytes(), self._MEMORY_UTILIZATION_BREAKPOINT)
            length_difference = self._NODE_STATS_LENGTH_DIFFERENCE * num_blockchain_peers
            new_message_len = self._MESSAGE_LEN_WITHOUT_NODE_STATS + \
                (num_blockchain_peers * self._NEW_MESSAGE_NODE_STATS_LEN)

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + length_difference

        new_msg_bytes = bytearray(new_message_len)
        new_msg_bytes[:self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT] = msg.rawbytes()[:self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT]
        new_msg_off = self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT
        old_msg_off = self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT
        default_new_stats = 0
        for _ in range(num_blockchain_peers):
            new_msg_bytes[new_msg_off:new_msg_off + self._OLD_MESSAGE_NODE_STATS_LEN] = \
                msg.rawbytes()[old_msg_off:old_msg_off + self._OLD_MESSAGE_NODE_STATS_LEN]
            new_msg_off += self._OLD_MESSAGE_NODE_STATS_LEN
            old_msg_off += self._OLD_MESSAGE_NODE_STATS_LEN

            struct.pack_into("<I", new_msg_bytes, new_msg_off, default_new_stats)
            new_msg_off += constants.UL_INT_SIZE_IN_BYTES
            struct.pack_into("<I", new_msg_bytes, new_msg_off, default_new_stats)
            new_msg_off += constants.UL_INT_SIZE_IN_BYTES

        new_msg_bytes[new_msg_off:] = msg.rawbytes()[old_msg_off:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class,
            new_msg_bytes,
            (msg_type, new_payload_len)
        )