From 1ffc4b9269c379f2a48a7273f51ad262d840e50d Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Fri, 17 Nov 2023 11:37:19 +0200 Subject: [PATCH 1/3] feat(identify): support pushing changes of external addresses --- protocols/identify/CHANGELOG.md | 2 ++ protocols/identify/src/behaviour.rs | 23 ++++++++++++++++++++++- protocols/kad/tests/client_mode.rs | 8 ++++---- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index d2f67bd908c..8bb93414b83 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.44.1 - unreleased +- Support (enabled by default) pushing changes of external addresses. + See [PR 4885](https://github.com/libp2p/rust-libp2p/pull/4885). - Ensure `Multiaddr` handled and returned by `Behaviour` are `/p2p` terminated. See [PR 4596](https://github.com/libp2p/rust-libp2p/pull/4596). diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index e4da898f44c..2c90088b135 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -93,6 +93,16 @@ pub struct Config { /// Disabled by default. pub push_listen_addr_updates: bool, + /// Whether new or changes in external addresses of the local node should + /// trigger an active push of an identify message to all connected peers. + /// + /// Enabling this option can result in connected peers being informed + /// earlier about new or expired external addresses of the local node, + /// i.e. before the next periodic identify request with each peer. + /// + /// Enabled by default. + pub push_external_addr_updates: bool, + /// How many entries of discovered peers to keep before we discard /// the least-recently used one. /// @@ -110,6 +120,7 @@ impl Config { local_public_key, interval: Duration::from_secs(5 * 60), push_listen_addr_updates: false, + push_external_addr_updates: true, cache_size: 100, } } @@ -135,6 +146,14 @@ impl Config { self } + /// Configures whether new or expired external addresses of the local + /// node should trigger an active push of an identify message to all + /// connected peers. + pub fn with_push_external_addr_updates(mut self, b: bool) -> Self { + self.push_external_addr_updates = b; + self + } + /// Configures the size of the LRU cache, caching addresses of discovered peers. pub fn with_cache_size(mut self, cache_size: usize) -> Self { self.cache_size = cache_size; @@ -357,7 +376,9 @@ impl NetworkBehaviour for Behaviour { self.events.extend(change_events) } - if listen_addr_changed && self.config.push_listen_addr_updates { + if (listen_addr_changed && self.config.push_listen_addr_updates) + | (external_addr_changed && self.config.push_external_addr_updates) + { // trigger an identify push for all connected peers let push_events = self.connected.keys().map(|peer| ToSwarm::NotifyHandler { peer_id: *peer, diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index f290a36b727..bdbda24cebc 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -172,10 +172,10 @@ impl MyBehaviour { let local_peer_id = k.public().to_peer_id(); Self { - identify: identify::Behaviour::new(identify::Config::new( - "/test/1.0.0".to_owned(), - k.public(), - )), + identify: identify::Behaviour::new( + identify::Config::new("/test/1.0.0".to_owned(), k.public()) + .with_push_external_addr_updates(false), + ), kad: Behaviour::with_config( local_peer_id, MemoryStore::new(local_peer_id), From 0d7b04d7d108243038c5760fe6a8c31021c0bd79 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 20 Nov 2023 15:54:54 +0200 Subject: [PATCH 2/3] Fix bitwise or instead of logical --- protocols/identify/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 2c90088b135..4f29df007a3 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -377,7 +377,7 @@ impl NetworkBehaviour for Behaviour { } if (listen_addr_changed && self.config.push_listen_addr_updates) - | (external_addr_changed && self.config.push_external_addr_updates) + || (external_addr_changed && self.config.push_external_addr_updates) { // trigger an identify push for all connected peers let push_events = self.connected.keys().map(|peer| ToSwarm::NotifyHandler { From 96ea6edb2ed220c9259e208038812f0caadcbcb9 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 20 Nov 2023 23:41:52 +0200 Subject: [PATCH 3/3] Update documentation on `push_external_addr_updates` --- protocols/identify/src/behaviour.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 4f29df007a3..a7a1a68d404 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -96,9 +96,9 @@ pub struct Config { /// Whether new or changes in external addresses of the local node should /// trigger an active push of an identify message to all connected peers. /// - /// Enabling this option can result in connected peers being informed - /// earlier about new or expired external addresses of the local node, - /// i.e. before the next periodic identify request with each peer. + /// Disabling this option means connected peers are only informed + /// about new or expired external addresses of the local node with + /// the next periodic identify request with each peer. /// /// Enabled by default. pub push_external_addr_updates: bool,