Skip to content

Commit e63975d

Browse files
feat(allow-block-list): add getters and return results (#5572)
## Description Small changes to improve usability of the `allow-block-list` Behaviour. When trying to use it, we found ourselves wanting to known: - which were the current allowed or blocked peers: hence the new methods `allowed_peers` and `blocked_peers` - if the peer was already present in the set when adding or removing it from the set: that is why `allow/disallow_peer` and `block/unblock_peer` methods now return a boolean, allowing the end user the know if there was a change or not (in our case, we needed it in order to log something). ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR which don't need to go into the final commit message. --> ## Change checklist <!-- Please add a Changelog entry in the appropriate crates and bump the crate versions if needed. See <https://github.com/libp2p/rust-libp2p/blob/master/docs/release.md#development-between-releases>--> - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates
1 parent 64c6eb2 commit e63975d

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ asynchronous-codec = { version = "0.7.0" }
7676
futures-bounded = { version = "0.2.4" }
7777
futures-rustls = { version = "0.26.0", default-features = false }
7878
libp2p = { version = "0.54.1", path = "libp2p" }
79-
libp2p-allow-block-list = { version = "0.4.0", path = "misc/allow-block-list" }
79+
libp2p-allow-block-list = { version = "0.4.1", path = "misc/allow-block-list" }
8080
libp2p-autonat = { version = "0.13.0", path = "protocols/autonat" }
8181
libp2p-connection-limits = { version = "0.4.0", path = "misc/connection-limits" }
8282
libp2p-core = { version = "0.42.0", path = "core" }

misc/allow-block-list/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.4.1
2+
3+
- Add getters & setters for the allowed/blocked peers.
4+
Return a `bool` for every "insert/remove" function, informing if a change was performed.
5+
See [PR 5572](https://github.com/libp2p/rust-libp2p/pull/5572).
6+
17
## 0.4.0
28

39
<!-- Update to libp2p-swarm v0.45.0 -->

misc/allow-block-list/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-allow-block-list"
33
edition = "2021"
44
rust-version = { workspace = true }
55
description = "Allow/block list connection management for libp2p."
6-
version = "0.4.0"
6+
version = "0.4.1"
77
license = "MIT"
88
repository = "https://github.com/libp2p/rust-libp2p"
99
keywords = ["peer-to-peer", "libp2p", "networking"]

misc/allow-block-list/src/lib.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,44 +94,74 @@ pub struct BlockedPeers {
9494
}
9595

9696
impl Behaviour<AllowedPeers> {
97+
/// Peers that are currently allowed.
98+
pub fn allowed_peers(&self) -> &HashSet<PeerId> {
99+
&self.state.peers
100+
}
101+
97102
/// Allow connections to the given peer.
98-
pub fn allow_peer(&mut self, peer: PeerId) {
99-
self.state.peers.insert(peer);
100-
if let Some(waker) = self.waker.take() {
101-
waker.wake()
103+
///
104+
/// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set.
105+
pub fn allow_peer(&mut self, peer: PeerId) -> bool {
106+
let inserted = self.state.peers.insert(peer);
107+
if inserted {
108+
if let Some(waker) = self.waker.take() {
109+
waker.wake()
110+
}
102111
}
112+
inserted
103113
}
104114

105115
/// Disallow connections to the given peer.
106116
///
107117
/// All active connections to this peer will be closed immediately.
108-
pub fn disallow_peer(&mut self, peer: PeerId) {
109-
self.state.peers.remove(&peer);
110-
self.close_connections.push_back(peer);
111-
if let Some(waker) = self.waker.take() {
112-
waker.wake()
118+
///
119+
/// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set.
120+
pub fn disallow_peer(&mut self, peer: PeerId) -> bool {
121+
let removed = self.state.peers.remove(&peer);
122+
if removed {
123+
self.close_connections.push_back(peer);
124+
if let Some(waker) = self.waker.take() {
125+
waker.wake()
126+
}
113127
}
128+
removed
114129
}
115130
}
116131

117132
impl Behaviour<BlockedPeers> {
133+
/// Peers that are currently blocked.
134+
pub fn blocked_peers(&self) -> &HashSet<PeerId> {
135+
&self.state.peers
136+
}
137+
118138
/// Block connections to a given peer.
119139
///
120140
/// All active connections to this peer will be closed immediately.
121-
pub fn block_peer(&mut self, peer: PeerId) {
122-
self.state.peers.insert(peer);
123-
self.close_connections.push_back(peer);
124-
if let Some(waker) = self.waker.take() {
125-
waker.wake()
141+
///
142+
/// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set.
143+
pub fn block_peer(&mut self, peer: PeerId) -> bool {
144+
let inserted = self.state.peers.insert(peer);
145+
if inserted {
146+
self.close_connections.push_back(peer);
147+
if let Some(waker) = self.waker.take() {
148+
waker.wake()
149+
}
126150
}
151+
inserted
127152
}
128153

129154
/// Unblock connections to a given peer.
130-
pub fn unblock_peer(&mut self, peer: PeerId) {
131-
self.state.peers.remove(&peer);
132-
if let Some(waker) = self.waker.take() {
133-
waker.wake()
155+
///
156+
/// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set.
157+
pub fn unblock_peer(&mut self, peer: PeerId) -> bool {
158+
let removed = self.state.peers.remove(&peer);
159+
if removed {
160+
if let Some(waker) = self.waker.take() {
161+
waker.wake()
162+
}
134163
}
164+
removed
135165
}
136166
}
137167

0 commit comments

Comments
 (0)