1//! Additional support for tracking nodes.
23use reth_network_peers::PeerId;
4use std::{collections::HashMap, net::IpAddr, time::Instant};
56/// Keeps track of nodes from which we have received a `Pong` message.
7#[derive(Debug, Clone, Default)]
8pub(crate) struct PongTable {
9/// The nodes we have received a `Pong` from.
10nodes: HashMap<NodeKey, Instant>,
11}
1213impl PongTable {
14/// Updates the timestamp we received a `Pong` from the given node.
15pub(crate) fn on_pong(&mut self, remote_id: PeerId, remote_ip: IpAddr) {
16let key = NodeKey { remote_id, remote_ip };
17self.nodes.insert(key, Instant::now());
18 }
1920/// Returns the timestamp we received a `Pong` from the given node.
21pub(crate) fn last_pong(&self, remote_id: PeerId, remote_ip: IpAddr) -> Option<Instant> {
22self.nodes.get(&NodeKey { remote_id, remote_ip }).copied()
23 }
2425/// Removes all nodes from the table that have not sent a `Pong` for at least `timeout`.
26pub(crate) fn evict_expired(&mut self, now: Instant, timeout: std::time::Duration) {
27self.nodes.retain(|_, last_pong| now - *last_pong < timeout);
28 }
29}
3031#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
32pub(crate) struct NodeKey {
33pub(crate) remote_id: PeerId,
34pub(crate) remote_ip: IpAddr,
35}