Skip to main content

zebra_network/
address_book_peers.rs

1//! A AddressBookPeers trait for getting the [`MetaAddr`] of recently live peers.
2
3use chrono::Utc;
4
5use crate::{meta_addr::MetaAddr, PeerSocketAddr};
6
7#[cfg(any(test, feature = "proptest-impl"))]
8pub mod mock;
9
10#[cfg(any(test, feature = "proptest-impl"))]
11pub use mock::MockAddressBookPeers;
12
13/// Method signatures for getting [`MetaAddr`]s of recently live peers.
14pub trait AddressBookPeers {
15    /// Return an Vec of peers we've seen recently, in reconnection attempt order.
16    fn recently_live_peers(&self, now: chrono::DateTime<Utc>) -> Vec<MetaAddr>;
17
18    /// Add a peer to the address book.
19    fn add_peer(&mut self, peer: PeerSocketAddr) -> bool;
20
21    /// Returns the misbehavior score for the peer group containing `addr`:
22    /// [`MAX_PEER_MISBEHAVIOR_SCORE`](crate::constants::MAX_PEER_MISBEHAVIOR_SCORE)
23    /// if the group is banned, and `0` otherwise.
24    ///
25    /// Bans apply per peer group — one IPv4 address, or one IPv6 `/64` subnet —
26    /// so every address in a group reports the same score.
27    ///
28    /// Defaults to `0` for implementers that don't track misbehavior.
29    fn misbehavior_score(&self, _addr: PeerSocketAddr) -> u32 {
30        0
31    }
32}