From de5b6868fb4d6bc09b0f5686fd2ee22b57f166da Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Mon, 8 Oct 2018 13:39:52 +0100 Subject: [PATCH] Add last_seen to peer info (#1688) * add last_seen to peer and update on ping/pong expose last_seen in tui * rustfmt * chrono serde features --- p2p/Cargo.toml | 2 +- p2p/src/handshake.rs | 3 +++ p2p/src/peers.rs | 28 +++++----------------------- p2p/src/types.rs | 3 +++ servers/src/common/stats.rs | 5 +++++ src/bin/tui/peers.rs | 11 ++++++++--- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/p2p/Cargo.toml b/p2p/Cargo.toml index a02561d4a..8a7f483b5 100644 --- a/p2p/Cargo.toml +++ b/p2p/Cargo.toml @@ -16,7 +16,7 @@ rand = "0.5" serde = "1" serde_derive = "1" slog = { version = "~2.3", features = ["max_level_trace", "release_max_level_trace"] } -chrono = "0.4.4" +chrono = { version = "0.4.4", features = ["serde"] } grin_core = { path = "../core" } grin_store = { path = "../store" } diff --git a/p2p/src/handshake.rs b/p2p/src/handshake.rs index f0c21389c..ce076e65e 100644 --- a/p2p/src/handshake.rs +++ b/p2p/src/handshake.rs @@ -16,6 +16,7 @@ use std::collections::VecDeque; use std::net::{SocketAddr, TcpStream}; use std::sync::{Arc, RwLock}; +use chrono::prelude::*; use rand::{thread_rng, Rng}; use core::core::hash::Hash; @@ -100,6 +101,7 @@ impl Handshake { total_difficulty: shake.total_difficulty, height: 0, direction: Direction::Outbound, + last_seen: Utc::now(), }; // If denied then we want to close the connection @@ -156,6 +158,7 @@ impl Handshake { total_difficulty: hand.total_difficulty, height: 0, direction: Direction::Inbound, + last_seen: Utc::now(), }; // At this point we know the published ip and port of the peer diff --git a/p2p/src/peers.rs b/p2p/src/peers.rs index a2cac8475..b56976325 100644 --- a/p2p/src/peers.rs +++ b/p2p/src/peers.rs @@ -19,7 +19,7 @@ use std::sync::{Arc, RwLock}; use rand::{thread_rng, Rng}; -use chrono::prelude::Utc; +use chrono::prelude::*; use core::core; use core::core::hash::{Hash, Hashed}; use core::pow::Difficulty; @@ -644,29 +644,11 @@ impl NetAdapter for Peers { } fn peer_difficulty(&self, addr: SocketAddr, diff: Difficulty, height: u64) { - if diff != self.total_difficulty() || height != self.total_height() { - trace!( - LOGGER, - "ping/pong: {}: {} @ {} vs us: {} @ {}", - addr, - diff, - height, - self.total_difficulty(), - self.total_height() - ); - } - if let Some(peer) = self.get_connected_peer(&addr) { - let (prev_diff, prev_height) = { - let peer = peer.read().unwrap(); - (peer.info.total_difficulty, peer.info.height) - }; - - if diff != prev_diff || height != prev_height { - let mut peer = peer.write().unwrap(); - peer.info.total_difficulty = diff; - peer.info.height = height; - } + let mut peer = peer.write().unwrap(); + peer.info.total_difficulty = diff; + peer.info.height = height; + peer.info.last_seen = Utc::now(); } } diff --git a/p2p/src/types.rs b/p2p/src/types.rs index a22b67e7f..0dffd5404 100644 --- a/p2p/src/types.rs +++ b/p2p/src/types.rs @@ -18,6 +18,8 @@ use std::io; use std::net::{IpAddr, SocketAddr}; use std::sync::mpsc; +use chrono::prelude::*; + use core::core::hash::Hash; use core::pow::Difficulty; use core::{core, ser}; @@ -243,6 +245,7 @@ pub struct PeerInfo { pub total_difficulty: Difficulty, pub height: u64, pub direction: Direction, + pub last_seen: DateTime, } /// The full txhashset data along with indexes required for a consumer to diff --git a/servers/src/common/stats.rs b/servers/src/common/stats.rs index ef7d94979..498491ea1 100644 --- a/servers/src/common/stats.rs +++ b/servers/src/common/stats.rs @@ -19,6 +19,8 @@ use std::sync::atomic::AtomicBool; use std::sync::{Arc, RwLock}; use std::time::SystemTime; +use chrono::prelude::*; + use chain; use common::types::SyncStatus; use p2p; @@ -144,6 +146,8 @@ pub struct PeerStats { pub height: u64, /// direction pub direction: String, + /// Last time we saw a ping/pong from this peer. + pub last_seen: DateTime, } impl StratumStats { @@ -176,6 +180,7 @@ impl PeerStats { total_difficulty: peer.info.total_difficulty.to_num(), height: peer.info.height, direction: direction.to_string(), + last_seen: peer.info.last_seen, } } } diff --git a/src/bin/tui/peers.rs b/src/bin/tui/peers.rs index 1f9e019d4..de95f95d0 100644 --- a/src/bin/tui/peers.rs +++ b/src/bin/tui/peers.rs @@ -18,6 +18,8 @@ use std::cmp::Ordering; use servers::{PeerStats, ServerStats}; +use chrono::prelude::*; + use cursive::direction::Orientation; use cursive::traits::{Boxable, Identifiable}; use cursive::view::View; @@ -54,9 +56,12 @@ impl TableViewItem for PeerStats { match column { PeerColumn::Address => self.addr.clone(), PeerColumn::State => self.state.clone(), - PeerColumn::TotalDifficulty => { - format!("{} D @ {} H", self.total_difficulty, self.height).to_string() - } + PeerColumn::TotalDifficulty => format!( + "{} D @ {} H ({}s)", + self.total_difficulty, + self.height, + (Utc::now() - self.last_seen).num_seconds(), + ).to_string(), PeerColumn::Direction => self.direction.clone(), PeerColumn::Version => self.version.to_string(), }