def compare_ping_old_to_current(
     self,
     converted_current_message: PingMessage,
     original_current_message: PingMessage,
 ):
     self.assertEqual(
         original_current_message.rawbytes(),
         converted_current_message.rawbytes(),
     )
    def test_queuing_messages_cleared_after_timeout(self):
        node = self._initialize_gateway(True, True, True)
        remote_blockchain_conn = next(iter(node.connection_pool.get_by_connection_types([ConnectionType.REMOTE_BLOCKCHAIN_NODE])))
        remote_blockchain_conn.mark_for_close()

        queued_message = PingMessage(12345)
        node.send_msg_to_remote_node(queued_message)
        self.assertEqual(1, len(node.remote_node_msg_queue._queue))
        self.assertEqual(queued_message, node.remote_node_msg_queue._queue[0])

        # queue has been cleared
        time.time = MagicMock(return_value=time.time() + node.opts.remote_blockchain_message_ttl + 0.1)
        node.alarm_queue.fire_alarms()

        node.on_connection_added(MockSocketConnection(3, node, ip_address=LOCALHOST, port=8003))
        next_conn = next(iter(node.connection_pool.get_by_connection_types([ConnectionType.REMOTE_BLOCKCHAIN_NODE])))
        next_conn.outputbuf = OutputBuffer()  # clear buffer

        node.on_remote_blockchain_connection_ready(next_conn)
        self.assertEqual(0, next_conn.outputbuf.length)

        next_conn.mark_for_close()

        queued_message = PingMessage(12345)
        node.send_msg_to_remote_node(queued_message)
        self.assertEqual(1, len(node.remote_node_msg_queue._queue))
        self.assertEqual(queued_message, node.remote_node_msg_queue._queue[0])

        node.on_connection_added(MockSocketConnection(4, node, ip_address=LOCALHOST, port=8003))
        reestablished_conn = next(iter(node.connection_pool.get_by_connection_types([ConnectionType.REMOTE_BLOCKCHAIN_NODE])))
        reestablished_conn.outputbuf = OutputBuffer()  # clear buffer

        node.on_remote_blockchain_connection_ready(reestablished_conn)
        self.assertEqual(queued_message.rawbytes().tobytes(), reestablished_conn.outputbuf.get_buffer().tobytes())
Beispiel #3
0
    def test_ping_pong(self):
        hello_msg = HelloMessage(protocol_version=protocol_version.PROTOCOL_VERSION, network_num=1)
        self.connection.add_received_bytes(hello_msg.rawbytes())
        self.connection.process_message()

        hello_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(hello_msg_bytes) > 0)
        self.connection.advance_sent_bytes(len(hello_msg_bytes))

        ack_msg = AckMessage()
        self.connection.add_received_bytes(ack_msg.rawbytes())
        self.connection.process_message()

        ack_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(ack_msg_bytes) > 0)
        self.connection.advance_sent_bytes(len(ack_msg_bytes))

        ping_msg = PingMessage(nonce=12345)
        self.connection.add_received_bytes(ping_msg.rawbytes())
        self.connection.process_message()

        pong_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(pong_msg_bytes) > 0)

        msg_type, payload_len = AbstractBloxrouteMessage.unpack(pong_msg_bytes[:AbstractBloxrouteMessage.HEADER_LENGTH])
        self.assertEqual(BloxrouteMessageType.PONG, msg_type)
        self.connection.advance_sent_bytes(len(pong_msg_bytes))

        time.time = MagicMock(return_value=time.time() + constants.PING_INTERVAL_S)
        self.node.alarm_queue.fire_alarms()

        ping_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(ping_msg_bytes) > 0)
        msg_type, payload_len = AbstractBloxrouteMessage.unpack(ping_msg_bytes[:AbstractBloxrouteMessage.HEADER_LENGTH])
        self.assertEqual(BloxrouteMessageType.PING, msg_type)
Beispiel #4
0
    async def test_queuing_messages_no_blockchain_connection(self):
        node = self._initialize_gateway(True, True)
        blockchain_conn = next(
            iter(
                node.connection_pool.get_by_connection_types(
                    [ConnectionType.BLOCKCHAIN_NODE])))
        blockchain_conn.mark_for_close()

        self.assertIsNone(node.node_conn)

        queued_message = PingMessage(12345)
        node.send_msg_to_node(queued_message)
        self.assertEqual(1, len(node.node_msg_queue._queue))
        self.assertEqual(queued_message, node.node_msg_queue._queue[0])

        node.on_connection_added(
            MockSocketConnection(ip_address=LOCALHOST, port=8001))
        next_conn = next(
            iter(
                node.connection_pool.get_by_connection_types(
                    [ConnectionType.BLOCKCHAIN_NODE])))
        next_conn.outputbuf = OutputBuffer()  # clear buffer

        node.on_blockchain_connection_ready(next_conn)
        self.assertEqual(queued_message.rawbytes().tobytes(),
                         next_conn.outputbuf.get_buffer().tobytes())