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)
        )
Exemplo n.º 2
0
    def test_pack_unpack_ipv4(self):
        ipv4_ip = "127.0.0.1"
        buf = bytearray((IP_ADDR_SIZE_IN_BYTES + UL_SHORT_SIZE_IN_BYTES) * 2)
        memview = memoryview(buf)

        pack_ip_port(buf, 0, ipv4_ip, 8000)
        ip, port = unpack_ip_port(memview[:IP_ADDR_SIZE_IN_BYTES +
                                          UL_SHORT_SIZE_IN_BYTES].tobytes())
        self.assertEqual(ipv4_ip, ip)
        self.assertEqual(8000, port)

        pack_ip_port(buf, IP_ADDR_SIZE_IN_BYTES + UL_SHORT_SIZE_IN_BYTES,
                     ipv4_ip, 9000)
        ip, port = unpack_ip_port(memview[IP_ADDR_SIZE_IN_BYTES +
                                          UL_SHORT_SIZE_IN_BYTES:].tobytes())
        self.assertEqual(ipv4_ip, ip)
        self.assertEqual(9000, port)
    def __init__(self, protocol_version=None, network_num=None, ip=None, port=None, ordering=None, node_id=None, buf=None):
        if buf is None:
            buf = bytearray(self.HEADER_LENGTH + self.PAYLOAD_LENGTH)

            off = VersionMessage.BASE_LENGTH

            message_utils.pack_ip_port(buf, off, ip, port)
            off += constants.IP_ADDR_SIZE_IN_BYTES + constants.UL_SHORT_SIZE_IN_BYTES

            struct.pack_into("<L", buf, off, ordering)
            off += constants.UL_INT_SIZE_IN_BYTES
            struct.pack_into("%ss" % constants.NODE_ID_SIZE_IN_BYTES, buf, off, uuid_pack.to_bytes(node_id))
        self.buf = buf
        self._ip = None
        self._port = None
        self._ordering = None
        self._node_id = None
        super(GatewayHelloMessage, self).__init__(self.MESSAGE_TYPE, self.PAYLOAD_LENGTH, protocol_version,
                                                  network_num, buf)
    def _serialize(self, start_time: datetime, end_time: datetime,
                   memory_utilization_mb: int,
                   node_stats: Dict[IpEndpoint, BdnPerformanceStatsDataV18]):
        stats_serialized_length = constants.UL_SHORT_SIZE_IN_BYTES + len(
            node_stats) * (constants.IP_ADDR_SIZE_IN_BYTES +
                           constants.UL_SHORT_SIZE_IN_BYTES +
                           (2 * constants.UL_SHORT_SIZE_IN_BYTES) +
                           (5 * constants.UL_INT_SIZE_IN_BYTES))
        msg_size = (self.node_stats_offset + stats_serialized_length +
                    constants.CONTROL_FLAGS_LEN)
        buf = bytearray(msg_size)
        off = AbstractBloxrouteMessage.HEADER_LENGTH

        struct.pack_into("<d", buf, off, start_time.timestamp())
        off += constants.DOUBLE_SIZE_IN_BYTES

        struct.pack_into("<d", buf, off, end_time.timestamp())
        off += constants.DOUBLE_SIZE_IN_BYTES

        memory_utilization_mb = min(memory_utilization_mb,
                                    constants.UNSIGNED_SHORT_MAX_VALUE)
        struct.pack_into("<H", buf, off, memory_utilization_mb)
        off += constants.UL_SHORT_SIZE_IN_BYTES

        struct.pack_into("<H", buf, off, len(node_stats))
        off += constants.UL_SHORT_SIZE_IN_BYTES

        for endpoint, node_stat in node_stats.items():
            message_utils.pack_ip_port(buf, off, endpoint.ip_address,
                                       endpoint.port)
            off += constants.IP_ADDR_SIZE_IN_BYTES + constants.UL_SHORT_SIZE_IN_BYTES

            struct.pack_into(
                "<H", buf, off,
                node_stat.new_blocks_received_from_blockchain_node)
            off += constants.UL_SHORT_SIZE_IN_BYTES

            struct.pack_into("<H", buf, off,
                             node_stat.new_blocks_received_from_bdn)
            off += constants.UL_SHORT_SIZE_IN_BYTES

            struct.pack_into("<I", buf, off,
                             node_stat.new_tx_received_from_blockchain_node)
            off += constants.UL_INT_SIZE_IN_BYTES

            struct.pack_into("<I", buf, off,
                             node_stat.new_tx_received_from_bdn)
            off += constants.UL_INT_SIZE_IN_BYTES

            struct.pack_into("<I", buf, off, node_stat.new_blocks_seen)
            off += constants.UL_INT_SIZE_IN_BYTES

            struct.pack_into("<I", buf, off,
                             node_stat.new_block_messages_from_blockchain_node)
            off += constants.UL_INT_SIZE_IN_BYTES

            struct.pack_into(
                "<I", buf, off,
                node_stat.new_block_announcements_from_blockchain_node)
            off += constants.UL_INT_SIZE_IN_BYTES

        return buf