Example #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 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))
Example #2
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 v10: {msg_type}"
            )

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

        old_version_msg_bytes = bytearray(self._OLD_MESSAGE_LEN)
        old_version_msg_bytes[:self._BREAKPOINT] = msg.rawbytes()[:self.
                                                                  _BREAKPOINT]
        old_version_msg_bytes[self._BREAKPOINT:] = msg.rawbytes(
        )[self._BREAKPOINT + self._LENGTH_DIFFERENCE:]

        return AbstractBloxrouteMessage.initialize_class(
            old_version_msg_class,
            old_version_msg_bytes,
            (msg_type, old_version_payload_len),
        )
Example #3
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.TRANSACTION_FLAG_LEN +
                           constants.DOUBLE_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(
            "<H",
            new_msg_bytes,
            self._LEFT_BREAKPOINT,
            0,
        )
        struct.pack_into(
            "<L",
            new_msg_bytes,
            self._LEFT_BREAKPOINT + constants.TRANSACTION_FLAG_LEN,
            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"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))
    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),
        )