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 changed listen protocols to remote #3980

Merged
merged 4 commits into from
May 25, 2023
Merged
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
6 changes: 5 additions & 1 deletion protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
- Fix aborting the answering of an identify request in rare situations.
See [PR 3876].

- Actively push changes in listen protocols to remote.
See [PR 3980].

[PR 3545]: https://github.com/libp2p/rust-libp2p/pull/3545
[PR 3698]: https://github.com/libp2p/rust-libp2p/pull/3698
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
[PR 3545]: https://github.com/libp2p/rust-libp2p/pull/3545
[PR 3876]: https://github.com/libp2p/rust-libp2p/pull/3876
[PR 3980]: https://github.com/libp2p/rust-libp2p/pull/3980

## 0.42.2

Expand Down
39 changes: 37 additions & 2 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamProtocol, StreamUpgradeError,
SubstreamProtocol, SupportedProtocols,
};
use log::warn;
use log::{warn, Level};
use smallvec::SmallVec;
use std::collections::HashSet;
use std::{io, task::Context, task::Poll, time::Duration};
Expand All @@ -60,6 +60,9 @@ pub struct Handler {
/// Future that fires when we need to identify the node again.
trigger_next_identify: Delay,

/// Whether we have exchanged at least one periodic identify.
exchanged_one_periodic_identify: bool,

/// The interval of `trigger_next_identify`, i.e. the recurrent delay.
interval: Duration,

Expand Down Expand Up @@ -122,6 +125,7 @@ impl Handler {
events: SmallVec::new(),
pending_replies: FuturesUnordered::new(),
trigger_next_identify: Delay::new(initial_delay),
exchanged_one_periodic_identify: false,
interval,
public_key,
protocol_version,
Expand Down Expand Up @@ -238,6 +242,14 @@ impl Handler {

self.remote_supported_protocols = new_remote_protocols;
}

fn local_protocols_to_string(&mut self) -> String {
self.local_supported_protocols
.iter()
.map(|p| p.to_string())
.collect::<Vec<_>>()
.join(", ")
}
}

impl ConnectionHandler for Handler {
Expand Down Expand Up @@ -319,6 +331,7 @@ impl ConnectionHandler for Handler {
let event = result
.map(|()| Event::Identification)
.unwrap_or_else(|err| Event::IdentificationError(StreamUpgradeError::Apply(err)));
self.exchanged_one_periodic_identify = true;

return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event));
}
Expand Down Expand Up @@ -349,7 +362,29 @@ impl ConnectionHandler for Handler {
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::RemoteProtocolsChange(_) => {}
ConnectionEvent::LocalProtocolsChange(change) => {
self.local_supported_protocols.on_protocols_change(change);
let before = log::log_enabled!(Level::Debug)
.then(|| self.local_protocols_to_string())
.unwrap_or_default();
let protocols_changed = self.local_supported_protocols.on_protocols_change(change);
let after = log::log_enabled!(Level::Debug)
.then(|| self.local_protocols_to_string())
.unwrap_or_default();

if protocols_changed && self.exchanged_one_periodic_identify {
log::debug!(
"Supported listen protocols changed from [{before}] to [{after}], pushing to {}",
self.remote_peer_id
);

let info = self.build_info();
self.events
.push(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(
Either::Right(Push::outbound(info)),
(),
),
});
}
}
}
}
Expand Down