Example #1
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)
        )
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 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))
Example #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 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),
        )
Example #5
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))
    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 v16: {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._INTERVAL_TIMES_BREAKPOINT] = msg.rawbytes()[:self._INTERVAL_TIMES_BREAKPOINT]
        off = self._BASE_LENGTH + self._INTERVAL_TIMES_LENGTH

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

        # 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

        new_msg_bytes[off:off + self._SECOND_STATS_SET_LENGTH] = \
            msg.rawbytes()[
                self._MEMORY_UTILIZATION_BREAKPOINT:self._MEMORY_UTILIZATION_BREAKPOINT + self._SECOND_STATS_SET_LENGTH
            ]
        off += self._SECOND_STATS_SET_LENGTH

        default_new_stats = 0
        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._SECOND_STATS_SET_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 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)
        )
Example #8
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))
 def test_broadcast_message_cut_through_from_old(self):
     block_hash = Sha256Hash(helpers.generate_bytes(crypto.SHA256_HASH_LEN))
     blob = helpers.generate_bytes(250)
     network_num = 1234
     old_message = BroadcastMessageV5(network_num=network_num,
                                      msg_hash=block_hash,
                                      blob=blob)
     old_message_bytes = old_message.rawbytes()
     new_message_bytes = bloxroute_version_manager.convert_message_first_bytes_from_older_version(
         5, BroadcastMessage.MESSAGE_TYPE, old_message_bytes)
     new_message_bytes = bloxroute_version_manager.convert_message_last_bytes_from_older_version(
         5, BroadcastMessage.MESSAGE_TYPE, new_message_bytes)
     msg_type, payload_length = AbstractBloxrouteMessage.unpack(
         new_message_bytes)
     new_msg = AbstractBloxrouteMessage.initialize_class(
         BroadcastMessage, bytearray(new_message_bytes.tobytes()),
         (msg_type, payload_length))
     self._validate_messages_match(old_message, new_msg)
     self.assertEqual(EMPTY_SOURCE_ID_STR, new_msg.source_id())
Example #10
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))
Example #11
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))
    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),
        )