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.
This commit is contained in:
hashmap 2019-04-04 11:59:44 +02:00 committed by GitHub
parent 4f52209cd0
commit 1b7d710317
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 20 deletions

View file

@ -12,19 +12,16 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // 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::core::hash::Hash;
use crate::core::pow::Difficulty; use crate::core::pow::Difficulty;
use crate::msg::{read_message, write_message, Hand, Shake, Type, PROTOCOL_VERSION, USER_AGENT}; use crate::msg::{read_message, write_message, Hand, Shake, Type, PROTOCOL_VERSION, USER_AGENT};
use crate::peer::Peer; use crate::peer::Peer;
use crate::types::{Capabilities, Direction, Error, P2PConfig, PeerAddr, PeerInfo, PeerLiveInfo}; 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. /// Local generated nonce for peer connecting.
/// Used for self-connecting detection (on receiver side), /// Used for self-connecting detection (on receiver side),
@ -105,12 +102,7 @@ impl Handshake {
user_agent: shake.user_agent, user_agent: shake.user_agent,
addr: peer_addr, addr: peer_addr,
version: shake.version, version: shake.version,
live_info: Arc::new(RwLock::new(PeerLiveInfo { live_info: Arc::new(RwLock::new(PeerLiveInfo::new(shake.total_difficulty))),
total_difficulty: shake.total_difficulty,
height: 0,
last_seen: Utc::now(),
stuck_detector: Utc::now(),
})),
direction: Direction::Outbound, direction: Direction::Outbound,
}; };
@ -171,12 +163,7 @@ impl Handshake {
user_agent: hand.user_agent, user_agent: hand.user_agent,
addr: resolve_peer_addr(hand.sender_addr, &conn), addr: resolve_peer_addr(hand.sender_addr, &conn),
version: hand.version, version: hand.version,
live_info: Arc::new(RwLock::new(PeerLiveInfo { live_info: Arc::new(RwLock::new(PeerLiveInfo::new(hand.total_difficulty))),
total_difficulty: hand.total_difficulty,
height: 0,
last_seen: Utc::now(),
stuck_detector: Utc::now(),
})),
direction: Direction::Inbound, direction: Direction::Inbound,
}; };

View file

@ -358,6 +358,7 @@ pub struct PeerLiveInfo {
pub height: u64, pub height: u64,
pub last_seen: DateTime<Utc>, pub last_seen: DateTime<Utc>,
pub stuck_detector: DateTime<Utc>, pub stuck_detector: DateTime<Utc>,
pub first_seen: DateTime<Utc>,
} }
/// General information about a connected peer that's useful to other modules. /// General information about a connected peer that's useful to other modules.
@ -371,6 +372,18 @@ pub struct PeerInfo {
pub live_info: Arc<RwLock<PeerLiveInfo>>, pub live_info: Arc<RwLock<PeerLiveInfo>>,
} }
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 { impl PeerInfo {
/// The current total_difficulty of the peer. /// The current total_difficulty of the peer.
pub fn total_difficulty(&self) -> Difficulty { pub fn total_difficulty(&self) -> Difficulty {
@ -391,6 +404,11 @@ impl PeerInfo {
self.live_info.read().last_seen self.live_info.read().last_seen
} }
/// Time of first_seen for this peer.
pub fn first_seen(&self) -> DateTime<Utc> {
self.live_info.read().first_seen
}
/// Update the total_difficulty, height and last_seen of the peer. /// Update the total_difficulty, height and last_seen of the peer.
/// Takes a write lock on the live_info. /// Takes a write lock on the live_info.
pub fn update(&self, height: u64, total_difficulty: Difficulty) { pub fn update(&self, height: u64, total_difficulty: Difficulty) {