From 1b7d7103171f1073230a157887b84949d8dcc07a Mon Sep 17 00:00:00 2001 From: hashmap Date: Thu, 4 Apr 2019 11:59:44 +0200 Subject: [PATCH] Add first_see field to LivePeerInfo (#2724) Totally nice to have, I personally found it useful. Also could be used to support FIFO peer list. --- p2p/src/handshake.rs | 27 +++++++-------------------- p2p/src/types.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/p2p/src/handshake.rs b/p2p/src/handshake.rs index dbb1b14da..6c5ac4694 100644 --- a/p2p/src/handshake.rs +++ b/p2p/src/handshake.rs @@ -12,19 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::util::RwLock; -use std::collections::VecDeque; -use std::net::{SocketAddr, TcpStream}; -use std::sync::Arc; - -use chrono::prelude::*; -use rand::{thread_rng, Rng}; - use crate::core::core::hash::Hash; use crate::core::pow::Difficulty; use crate::msg::{read_message, write_message, Hand, Shake, Type, PROTOCOL_VERSION, USER_AGENT}; use crate::peer::Peer; use crate::types::{Capabilities, Direction, Error, P2PConfig, PeerAddr, PeerInfo, PeerLiveInfo}; +use crate::util::RwLock; +use rand::{thread_rng, Rng}; +use std::collections::VecDeque; +use std::net::{SocketAddr, TcpStream}; +use std::sync::Arc; /// Local generated nonce for peer connecting. /// Used for self-connecting detection (on receiver side), @@ -105,12 +102,7 @@ impl Handshake { user_agent: shake.user_agent, addr: peer_addr, version: shake.version, - live_info: Arc::new(RwLock::new(PeerLiveInfo { - total_difficulty: shake.total_difficulty, - height: 0, - last_seen: Utc::now(), - stuck_detector: Utc::now(), - })), + live_info: Arc::new(RwLock::new(PeerLiveInfo::new(shake.total_difficulty))), direction: Direction::Outbound, }; @@ -171,12 +163,7 @@ impl Handshake { user_agent: hand.user_agent, addr: resolve_peer_addr(hand.sender_addr, &conn), version: hand.version, - live_info: Arc::new(RwLock::new(PeerLiveInfo { - total_difficulty: hand.total_difficulty, - height: 0, - last_seen: Utc::now(), - stuck_detector: Utc::now(), - })), + live_info: Arc::new(RwLock::new(PeerLiveInfo::new(hand.total_difficulty))), direction: Direction::Inbound, }; diff --git a/p2p/src/types.rs b/p2p/src/types.rs index 48f308dc9..9481c357a 100644 --- a/p2p/src/types.rs +++ b/p2p/src/types.rs @@ -358,6 +358,7 @@ pub struct PeerLiveInfo { pub height: u64, pub last_seen: DateTime, pub stuck_detector: DateTime, + pub first_seen: DateTime, } /// General information about a connected peer that's useful to other modules. @@ -371,6 +372,18 @@ pub struct PeerInfo { pub live_info: Arc>, } +impl PeerLiveInfo { + pub fn new(difficulty: Difficulty) -> PeerLiveInfo { + PeerLiveInfo { + total_difficulty: difficulty, + height: 0, + first_seen: Utc::now(), + last_seen: Utc::now(), + stuck_detector: Utc::now(), + } + } +} + impl PeerInfo { /// The current total_difficulty of the peer. pub fn total_difficulty(&self) -> Difficulty { @@ -391,6 +404,11 @@ impl PeerInfo { self.live_info.read().last_seen } + /// Time of first_seen for this peer. + pub fn first_seen(&self) -> DateTime { + self.live_info.read().first_seen + } + /// Update the total_difficulty, height and last_seen of the peer. /// Takes a write lock on the live_info. pub fn update(&self, height: u64, total_difficulty: Difficulty) {