예제 #1
0
    async def test_processing_messages(self):
        settings.network.magic = 769
        socket_mock = NeoNodeSocketMock(self.loop, '127.0.0.1', 1111)

        m_addr = message.Message(msg_type=message.MessageType.ADDR, payload=payloads.AddrPayload([]))
        m_block = message.Message(msg_type=message.MessageType.BLOCK, payload=payloads.EmptyPayload())
        m_inv1 = message.Message(msg_type=message.MessageType.INV, payload=payloads.InventoryPayload(
            payloads.InventoryType.BLOCK,
            hashes=[types.UInt256.from_string("65793a030c0dcd4fff4da8a6a6d5daa8b570750da4fdeea1bbc43bdf124aedc9")]
        ))
        m_inv2 = message.Message(msg_type=message.MessageType.INV,
                                 payload=payloads.InventoryPayload(payloads.InventoryType.TX, []))
        m_getaddr = message.Message(msg_type=message.MessageType.GETADDR, payload=payloads.EmptyPayload())
        m_mempool = message.Message(msg_type=message.MessageType.MEMPOOL, payload=payloads.EmptyPayload())

        # taken from the Headers testcase in `test_payloads`
        raw_headers_payload = binascii.unhexlify(b'0000000001FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00A402FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00A400000000000000007B00000000F7B4D00143932F3B6243CFC06CB4A68F22C739E201020102020304')
        m_headers = message.Message(msg_type=message.MessageType.HEADERS,
                                    payload=payloads.HeadersPayload.deserialize_from_bytes(raw_headers_payload))
        m_ping = message.Message(msg_type=message.MessageType.PING, payload=payloads.PingPayload(0))
        m_pong = message.Message(msg_type=message.MessageType.PONG, payload=payloads.PingPayload(0))
        m_reject = message.Message(msg_type=message.MessageType.REJECT, payload=payloads.EmptyPayload())

        def _recv_data2(self):
            # first do handshake
            yield self.m_send_version.to_array()
            yield self.m_verack.to_array()
            # next send all types of messages we handle
            yield m_addr.to_array()
            yield m_block.to_array()
            yield m_inv1.to_array()
            yield m_inv2.to_array()
            yield m_getaddr.to_array()
            yield m_mempool.to_array()
            yield m_headers.to_array()
            yield m_ping.to_array()
            yield m_pong.to_array()
            yield m_reject.to_array()

        socket_mock.recv_data = _recv_data2(socket_mock)
        with self.assertLogs(network_logger, 'DEBUG') as log_context:
            try:
                n, _ = await node.NeoNode.connect_to(socket=socket_mock)
            except Exception as e:
                print(f"GVD {e}")

            await asyncio.sleep(0.5)
            await n.disconnect(payloads.DisconnectReason.SHUTTING_DOWN)
예제 #2
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream.

        Args:
            reader: instance.
        """
        self.config = MessageConfig(reader.read_uint8())
        x = reader.read_uint8()
        self.type = MessageType(x)
        # self.type = MessageType(reader.read_uint8())

        payload_data = reader.read_var_bytes(self.PAYLOAD_MAX_SIZE)
        if len(payload_data) > 0:
            if MessageConfig.COMPRESSED in self.config:
                # From the lz4 documentation:
                # "The uncompressed_size argument specifies an upper bound on the size of the uncompressed data size
                # rather than an absolute value"
                try:
                    size = int.from_bytes(payload_data[:4], 'little')
                    payload_data = lz4.block.decompress(payload_data[4:],
                                                        uncompressed_size=size)
                except lz4.block.LZ4BlockError:
                    raise ValueError(
                        "Invalid payload data - decompress failed")

            self.payload = self._payload_from_data(self.type, payload_data)

        if self.payload is None:
            self.payload = payloads.EmptyPayload()
예제 #3
0
    def __init__(self, msg_type: MessageType = None, payload: serialization.ISerializable_T = None):
        """

        Args:
            msg_type: message object configuration.
            payload: an identifier specifying the purpose of the message.
        """
        self.config = MessageConfig.NONE  #: MessageConfig: message object configuration.
        # something strange is going on if the check does not explicitly include "is not None", then it will
        # use the 'else' result even if a msg_type is clearly specified and present in the debugger
        #: MessageType: an identifier specifying the purpose of the message.
        self.type: MessageType = msg_type if msg_type is not None else MessageType.DEFAULT
        self.payload: serialization.ISerializable_T = payloads.EmptyPayload()  # type: ignore
        # mypy doesn't get EmptyPayload is an ISerializable

        if payload:
            self.payload = payload
예제 #4
0
    def __init__(self,
                 msg_type: MessageType,
                 payload: serialization.ISerializable_T = None):
        """

        Args:
            msg_type: message object configuration.
            payload: an identifier specifying the purpose of the message.
        """
        self.config = MessageConfig.NONE  #: MessageConfig: message object configuration.
        #: MessageType: an identifier specifying the purpose of the message.
        self.type: MessageType = msg_type
        self.payload: serialization.ISerializable_T = payloads.EmptyPayload(
        )  # type: ignore
        # mypy doesn't get EmptyPayload is an ISerializable

        if payload:
            self.payload = payload