Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/net/banlist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ impl BanList {
/// Bans the IP until the timestamp.
///
/// This does not ban non-global IPs.
/// If the IP is already banned, the timeout will be updated to the new value.
pub fn ban_ip_until(&mut self, ip: IpAddr, until: Instant) {
self.ban_ip_with(ip, Some(until));
}

/// Bans the peer until the timestamp
/// Bans the peer until the timestamp.
///
/// If the peer is already banned, the timeout will be updated to the new value.
pub fn ban_peer_until(&mut self, node_id: PeerId, until: Instant) {
self.ban_peer_with(node_id, Some(until));
}
Expand All @@ -147,13 +150,16 @@ impl BanList {
}

/// Bans the peer indefinitely or until the given timeout.
///
/// If the peer is already banned, the timeout will be updated to the new value.
pub fn ban_peer_with(&mut self, node_id: PeerId, until: Option<Instant>) {
self.banned_peers.insert(node_id, until);
}

/// Bans the ip indefinitely or until the given timeout.
///
/// This does not ban non-global IPs.
/// If the IP is already banned, the timeout will be updated to the new value.
pub fn ban_ip_with(&mut self, ip: IpAddr, until: Option<Instant>) {
if is_global(&ip) {
self.banned_ips.insert(ip, until);
Expand All @@ -167,7 +173,7 @@ mod tests {

#[test]
fn can_ban_unban_peer() {
let peer = PeerId::random();
let peer = PeerId::new([1; 64]);
let mut banlist = BanList::default();
banlist.ban_peer(peer);
assert!(banlist.is_banned_peer(&peer));
Expand Down