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 __init__(self, message_hash: Optional[Sha256Hash] = None, network_num: Optional[int] = None, source_id: Optional[str] = None, buf: Optional[Union[bytearray, memoryview]] = None): if buf is None: self.buf = bytearray(self.HEADER_LENGTH + self.PAYLOAD_LENGTH) else: self.buf = buf payload_length = len(self.buf) - self.HEADER_LENGTH super().__init__(self.MESSAGE_TYPE, payload_length, self.buf) if buf is None: assert message_hash is not None and network_num is not None and source_id is not None off = AbstractBloxrouteMessage.HEADER_LENGTH self.buf[off:off + crypto.SHA256_HASH_LEN] = message_hash.binary off += crypto.SHA256_HASH_LEN struct.pack_into("<L", self.buf, off, network_num) off += constants.NETWORK_NUM_LEN struct.pack_into("<16s", self.buf, off, uuid_pack.to_bytes(source_id)) off += constants.NODE_ID_SIZE_IN_BYTES
def __init__(self, message_hash: Optional[Sha256Hash] = None, source_id: str = "", origin_node_id: Optional[str] = None, forwarding_node_id: Optional[str] = None, routing_update_id: Optional[Sha256Hash] = None, routing_update: Optional[List[str]] = None, buf: Optional[Union[bytearray, memoryview]] = None) -> None: if routing_update is not None: # pylint: disable=invalid-name self.PAYLOAD_LENGTH += len( routing_update) * constants.NODE_ID_SIZE_IN_BYTES super().__init__(message_hash, constants.ALL_NETWORK_NUM, source_id, buf) if buf is None: assert origin_node_id is not None assert routing_update is not None assert forwarding_node_id is not None assert routing_update_id is not None off = (self.HEADER_LENGTH + AbstractBroadcastMessage.PAYLOAD_LENGTH - constants.CONTROL_FLAGS_LEN) struct.pack_into("<16s", self.buf, off, uuid_pack.to_bytes(origin_node_id)) off += constants.NODE_ID_SIZE_IN_BYTES struct.pack_into("<16s", self.buf, off, uuid_pack.to_bytes(forwarding_node_id)) off += constants.NODE_ID_SIZE_IN_BYTES self.buf[off:off + crypto.SHA256_HASH_LEN] = routing_update_id.binary off += crypto.SHA256_HASH_LEN struct.pack_into("<I", self.buf, off, len(routing_update)) off += constants.UL_INT_SIZE_IN_BYTES for node_id in routing_update: struct.pack_into("<16s", self.buf, off, uuid_pack.to_bytes(node_id)) off += constants.NODE_ID_SIZE_IN_BYTES
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)
class HelloMessage(VersionMessage): """ BloXroute relay hello message type. node_id: the id of the node """ MESSAGE_TYPE = BloxrouteMessageType.HELLO HELLO_MESSAGE_BLOCK = PayloadBlock( VersionMessage.BASE_LENGTH, "HelloMessage", PROTOCOL_VERSION, PayloadElement(name="node_id", structure="%ss" % constants.NODE_ID_SIZE_IN_BYTES, encode=lambda x: uuid_pack.to_bytes(x), decode=lambda x: uuid_pack.from_bytes(x))) HELLO_MESSAGE_LENGTH = VersionMessage.VERSION_MESSAGE_BLOCK.size + HELLO_MESSAGE_BLOCK.size + constants.CONTROL_FLAGS_LEN def __init__(self, protocol_version: Optional[int] = None, network_num: Optional[int] = None, node_id: str = None, buf: bytearray = None): if buf is None: buf = bytearray(self.HEADER_LENGTH + self.HELLO_MESSAGE_LENGTH) buf = self.HELLO_MESSAGE_BLOCK.build(buf, node_id=node_id) self.buf = buf self._node_id = None self._network_num = None self._memoryview = memoryview(buf) super(HelloMessage, self).__init__(self.MESSAGE_TYPE, self.HELLO_MESSAGE_LENGTH, protocol_version, network_num, buf) def __unpack(self): contents = self.HELLO_MESSAGE_BLOCK.read(self._memoryview) self._node_id = contents.get("node_id") def node_id(self): if self._node_id is None: self.__unpack() return self._node_id
def setUp(self): self.MESSAGE_BLOCK_HDR = PayloadBlock(0, "HDR", 0, PayloadElement(name="msg_type", structure="<12s",), PayloadElement(name="payload_len", structure="<L",) ) self.MESSAGE_BLOCK_VERSION = PayloadBlock(0, "VersionMessage", 0, self.MESSAGE_BLOCK_HDR, PayloadElement(structure="<L", name="protocol_version"), PayloadElement(structure="<L", name="network_num") ) self.MESSAGE_BLOCK_HELLO = PayloadBlock(0, "HelloMessage", 3, self.MESSAGE_BLOCK_VERSION, PayloadElement(name="node_id", structure="<16s", encode=lambda x: uuid_pack.to_bytes(x), decode=lambda x: uuid_pack.from_bytes(x)) ) self.kwargs = { "node_id": "31f93bcc-ad56-431f-9c14-28ffb0e8e41a", "msg_type": b"HelloMessage", "protocol_version": 3, "network_num": 12345 }
def set_source_id(self, source_id: str) -> None: self._source_id = source_id off = self.HEADER_LENGTH + crypto.SHA256_HASH_LEN + constants.NETWORK_NUM_LEN struct.pack_into("<16s", self.buf, off, uuid_pack.to_bytes(source_id))