mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
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:
parent
4f52209cd0
commit
1b7d710317
2 changed files with 25 additions and 20 deletions
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
@ -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 {
|
||||
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue