From a7ae23ccb79d557bdb013d03753bff47254811fb Mon Sep 17 00:00:00 2001 From: Dave Huseby Date: Wed, 16 Aug 2023 20:34:31 -0600 Subject: [PATCH] added behavior event when local protocols change Signed-off-by: Dave Huseby --- protocols/identify/src/behaviour.rs | 13 ++++++++++- protocols/identify/src/handler.rs | 35 ++++++++++++++++++----------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index f572b937d38..a7eb68ae9b0 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -26,7 +26,7 @@ use libp2p_identity::PublicKey; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::{ ConnectionDenied, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, - NotifyHandler, PollParameters, StreamUpgradeError, THandlerInEvent, ToSwarm, + NotifyHandler, PollParameters, StreamProtocol, StreamUpgradeError, THandlerInEvent, ToSwarm, }; use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent}; use lru::LruCache; @@ -309,6 +309,10 @@ impl NetworkBehaviour for Behaviour { self.events .push_back(ToSwarm::GenerateEvent(Event::Error { peer_id, error })); } + handler::Event::LocalProtocolsChanged(protocols) => { + self.events + .push_back(ToSwarm::GenerateEvent(Event::LocalProtocolsChanged { peer_id, protocols })); + } } } @@ -439,6 +443,13 @@ pub enum Event { /// The error that occurred. error: StreamUpgradeError, }, + /// Local protocols have changed. + LocalProtocolsChanged { + /// The peer we are advertising protocols to. + peer_id: PeerId, + /// The list of local supported protocols we are advertising to the peer. + protocols: Vec, + }, } /// If there is a given peer_id in the multiaddr, make sure it is the same as diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 5a1712e8c3d..a6a99194e6f 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -104,6 +104,8 @@ pub enum Event { IdentificationPushed, /// Failed to identify the remote, or to reply to an identification request. IdentificationError(StreamUpgradeError), + /// Local supported protocols changed + LocalProtocolsChanged(Vec), } impl Handler { @@ -370,20 +372,27 @@ impl ConnectionHandler for Handler { .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(); + if protocols_changed { self.events - .push(ConnectionHandlerEvent::OutboundSubstreamRequest { - protocol: SubstreamProtocol::new( - Either::Right(Push::outbound(info)), - (), - ), - }); + .push(ConnectionHandlerEvent::NotifyBehaviour(Event::LocalProtocolsChanged( + Vec::from_iter(self.local_supported_protocols.iter().cloned()) + ))); + + if 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)), + (), + ), + }); + } } } }