Skip to content

Commit

Permalink
Add first_see field to LivePeerInfo (#2724)
Browse files Browse the repository at this point in the history
Totally nice to have, I personally found it useful. Also could be used
to support FIFO peer list.
  • Loading branch information
hashmap authored Apr 4, 2019
1 parent 4f52209 commit 1b7d710
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
27 changes: 7 additions & 20 deletions p2p/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
};

Expand Down
18 changes: 18 additions & 0 deletions p2p/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ pub struct PeerLiveInfo {
pub height: u64,
pub last_seen: DateTime<Utc>,
pub stuck_detector: DateTime<Utc>,
pub first_seen: DateTime<Utc>,
}

/// General information about a connected peer that's useful to other modules.
Expand All @@ -371,6 +372,18 @@ pub struct PeerInfo {
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 {
/// The current total_difficulty of the peer.
pub fn total_difficulty(&self) -> Difficulty {
Expand All @@ -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<Utc> {
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) {
Expand Down

0 comments on commit 1b7d710

Please sign in to comment.