use reth_network_peers::PeerId;
use std::{collections::HashMap, net::IpAddr, time::Instant};
#[derive(Debug, Clone, Default)]
pub(crate) struct PongTable {
nodes: HashMap<NodeKey, Instant>,
}
impl PongTable {
pub(crate) fn on_pong(&mut self, remote_id: PeerId, remote_ip: IpAddr) {
let key = NodeKey { remote_id, remote_ip };
self.nodes.insert(key, Instant::now());
}
pub(crate) fn last_pong(&self, remote_id: PeerId, remote_ip: IpAddr) -> Option<Instant> {
self.nodes.get(&NodeKey { remote_id, remote_ip }).copied()
}
pub(crate) fn evict_expired(&mut self, now: Instant, timeout: std::time::Duration) {
self.nodes.retain(|_, last_pong| now - *last_pong < timeout);
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub(crate) struct NodeKey {
pub(crate) remote_id: PeerId,
pub(crate) remote_ip: IpAddr,
}