Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(identify): push changes of external addresses #4885

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
23 changes: 22 additions & 1 deletion protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
/// 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,

/// How many entries of discovered peers to keep before we discard
/// the least-recently used one.
///
Expand All @@ -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,
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions protocols/kad/tests/client_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
kad: Behaviour::with_config(
local_peer_id,
MemoryStore::new(local_peer_id),
Expand Down