From 0490040dd430ec83dce747ff50d5c081de31e97e Mon Sep 17 00:00:00 2001 From: Tiram <18632023+tiram88@users.noreply.github.com> Date: Tue, 25 Apr 2023 15:47:46 +0000 Subject: [PATCH] Simplify the router last ping state --- protocol/flows/src/v5/ping.rs | 6 +++--- protocol/p2p/src/core/router.rs | 22 +++------------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/protocol/flows/src/v5/ping.rs b/protocol/flows/src/v5/ping.rs index 904fa6f06..a5d2c58f3 100644 --- a/protocol/flows/src/v5/ping.rs +++ b/protocol/flows/src/v5/ping.rs @@ -1,5 +1,5 @@ use crate::{flow_context::FlowContext, flow_trait::Flow}; -use kaspa_core::debug; +use kaspa_core::{debug, time::unix_now}; use kaspa_p2p_lib::{ common::ProtocolError, dequeue, dequeue_with_timeout, make_message, @@ -93,8 +93,8 @@ impl SendPingsFlow { // Create a fresh random nonce for each ping let nonce = rand::thread_rng().gen::(); let ping = make_message!(Payload::Ping, PingMessage { nonce }); + let ping_time = unix_now(); if let Some(router) = self.router.upgrade() { - router.set_ping_pending(nonce); router.enqueue(ping).await?; } else { return Err(ProtocolError::ConnectionClosed); @@ -106,7 +106,7 @@ impl SendPingsFlow { debug!("Successful ping with peer {} (nonce: {})", self.peer, pong.nonce); } if let Some(router) = self.router.upgrade() { - router.set_ping_idle(); + router.set_last_ping_duration(unix_now() - ping_time); } else { return Err(ProtocolError::ConnectionClosed); } diff --git a/protocol/p2p/src/core/router.rs b/protocol/p2p/src/core/router.rs index c67163b9b..e05e6aa6d 100644 --- a/protocol/p2p/src/core/router.rs +++ b/protocol/p2p/src/core/router.rs @@ -2,7 +2,6 @@ use crate::core::hub::HubEvent; use crate::pb::KaspadMessage; use crate::Peer; use crate::{common::ProtocolError, KaspadMessagePayloadType}; -use kaspa_core::time::unix_now; use kaspa_core::{debug, error, info, trace}; use kaspa_utils::networking::PeerId; use parking_lot::{Mutex, RwLock}; @@ -51,12 +50,6 @@ struct RouterMutableState { /// Properties of the peer properties: Arc, - /// The nonce of the last ping we sent - last_ping_nonce: u64, - - /// Time last ping was sent - last_ping_time: u64, - /// Duration of the last ping to this peer last_ping_duration: u64, } @@ -231,18 +224,9 @@ impl Router { self.mutable_state.lock().properties = properties; } - /// Sets the ping state of the peer to 'pending' - pub fn set_ping_pending(&self, nonce: u64) { - let mut state = self.mutable_state.lock(); - state.last_ping_nonce = nonce; - state.last_ping_time = unix_now(); - } - - /// sets the ping state of the peer to 'idle' - pub fn set_ping_idle(&self) { - let mut state = self.mutable_state.lock(); - state.last_ping_nonce = 0; - state.last_ping_duration = unix_now() - state.last_ping_time; + /// Sets the duration of the last ping + pub fn set_last_ping_duration(&self, last_ping_duration: u64) { + self.mutable_state.lock().last_ping_duration = last_ping_duration; } pub fn last_ping_duration(&self) -> u64 {