Exemplo n.º 1
0
    def test_pong_message_timestamp(self):
        t0 = nonce_generator.get_nonce()
        time.sleep(0)
        t1 = nonce_generator.get_nonce()
        msg = PongMessage(t0, t1)
        self.assertEqual(msg.nonce(), t0)
        self.assertEqual(msg.timestamp(), t1)

        new_msg = PongMessage(buf=msg.buf)
        self.assertEqual(msg.nonce(), new_msg.nonce())
    def msg_pong(self, msg: PongMessage):
        super(InternalNodeConnection, self).msg_pong(msg)

        nonce = msg.nonce()
        timestamp = msg.timestamp()
        if timestamp:
            self.inbound_peer_latency = time.time(
            ) - nonce_generator.get_timestamp_from_nonce(timestamp)
        if nonce in self.ping_message_timestamps.contents:
            request_msg_timestamp = self.ping_message_timestamps.contents[
                nonce]
            request_response_time = time.time() - request_msg_timestamp

            if nonce in self._nonce_to_network_num:
                self.sync_ping_latencies[
                    self._nonce_to_network_num[nonce]] = request_response_time

            if request_response_time > constants.PING_PONG_TRESHOLD:
                self.log_debug(
                    "Ping/pong exchange nonce {} took {:.2f} seconds to complete.",
                    msg.nonce(), request_response_time)
            else:
                self.log_trace(
                    "Ping/pong exchange nonce {} took {:.2f} seconds to complete.",
                    msg.nonce(), request_response_time)

            hooks.add_measurement(self.peer_desc, MeasurementType.PING,
                                  request_response_time, self.peer_id)
            if timestamp:
                assumed_peer_response_time = nonce_generator.get_timestamp_from_nonce(
                    timestamp) - request_msg_timestamp
                hooks.add_measurement(self.peer_desc,
                                      MeasurementType.PING_OUTGOING,
                                      assumed_peer_response_time, self.peer_id)

        elif nonce is not None:
            self.log_debug(
                "Pong message had no matching ping request. Nonce: {}", nonce)