Skip to content

Commit

Permalink
feat(allow-block-list): add getters and return results
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-frb committed Aug 28, 2024
1 parent 64c6eb2 commit fead401
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ asynchronous-codec = { version = "0.7.0" }
futures-bounded = { version = "0.2.4" }
futures-rustls = { version = "0.26.0", default-features = false }
libp2p = { version = "0.54.1", path = "libp2p" }
libp2p-allow-block-list = { version = "0.4.0", path = "misc/allow-block-list" }
libp2p-allow-block-list = { version = "0.4.1", path = "misc/allow-block-list" }
libp2p-autonat = { version = "0.13.0", path = "protocols/autonat" }
libp2p-connection-limits = { version = "0.4.0", path = "misc/connection-limits" }
libp2p-core = { version = "0.42.0", path = "core" }
Expand Down
6 changes: 6 additions & 0 deletions misc/allow-block-list/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.1

- Add getters & setters for the allowed/blocked peers.
Return a `bool` for every "insert/remove" function, informing if a change was performed.
See [PR 5572](https://github.com/libp2p/rust-libp2p/pull/5572)

## 0.4.0

<!-- Update to libp2p-swarm v0.45.0 -->
Expand Down
2 changes: 1 addition & 1 deletion misc/allow-block-list/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-allow-block-list"
edition = "2021"
rust-version = { workspace = true }
description = "Allow/block list connection management for libp2p."
version = "0.4.0"
version = "0.4.1"
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
Expand Down
66 changes: 48 additions & 18 deletions misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,44 +94,74 @@ pub struct BlockedPeers {
}

impl Behaviour<AllowedPeers> {
/// Peers that are currently allowed.
pub fn allowed_peers(&self) -> &HashSet<PeerId> {
&self.state.peers
}

/// Allow connections to the given peer.
pub fn allow_peer(&mut self, peer: PeerId) {
self.state.peers.insert(peer);
if let Some(waker) = self.waker.take() {
waker.wake()
///
/// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set.
pub fn allow_peer(&mut self, peer: PeerId) -> bool {
let inserted = self.state.peers.insert(peer);
if inserted {
if let Some(waker) = self.waker.take() {
waker.wake()
}
}
inserted
}

/// Disallow connections to the given peer.
///
/// All active connections to this peer will be closed immediately.
pub fn disallow_peer(&mut self, peer: PeerId) {
self.state.peers.remove(&peer);
self.close_connections.push_back(peer);
if let Some(waker) = self.waker.take() {
waker.wake()
///
/// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set.
pub fn disallow_peer(&mut self, peer: PeerId) -> bool {
let removed = self.state.peers.remove(&peer);
if removed {
self.close_connections.push_back(peer);
if let Some(waker) = self.waker.take() {
waker.wake()
}
}
removed
}
}

impl Behaviour<BlockedPeers> {
/// Peers that are currently blocked.
pub fn blocked_peers(&self) -> &HashSet<PeerId> {
&self.state.peers
}

/// Block connections to a given peer.
///
/// All active connections to this peer will be closed immediately.
pub fn block_peer(&mut self, peer: PeerId) {
self.state.peers.insert(peer);
self.close_connections.push_back(peer);
if let Some(waker) = self.waker.take() {
waker.wake()
///
/// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set.
pub fn block_peer(&mut self, peer: PeerId) -> bool {
let inserted = self.state.peers.insert(peer);
if inserted {
self.close_connections.push_back(peer);
if let Some(waker) = self.waker.take() {
waker.wake()
}
}
inserted
}

/// Unblock connections to a given peer.
pub fn unblock_peer(&mut self, peer: PeerId) {
self.state.peers.remove(&peer);
if let Some(waker) = self.waker.take() {
waker.wake()
///
/// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set.
pub fn unblock_peer(&mut self, peer: PeerId) -> bool {
let removed = self.state.peers.remove(&peer);
if removed {
if let Some(waker) = self.waker.take() {
waker.wake()
}
}
removed
}
}

Expand Down

0 comments on commit fead401

Please sign in to comment.