Skip to content

Commit e536dea

Browse files
Consume supported protocols in identify
1 parent 9a28cd2 commit e536dea

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

protocols/identify/src/behaviour.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl NetworkBehaviour for Behaviour {
323323
fn poll(
324324
&mut self,
325325
_cx: &mut Context<'_>,
326-
params: &mut impl PollParameters,
326+
_: &mut impl PollParameters,
327327
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
328328
if let Some(event) = self.events.pop_front() {
329329
return Poll::Ready(event);
@@ -344,7 +344,6 @@ impl NetworkBehaviour for Behaviour {
344344
.chain(self.external_addresses.iter())
345345
.cloned()
346346
.collect(),
347-
supported_protocols: supported_protocols(params),
348347
protocol: Protocol::Push,
349348
},
350349
}),
@@ -361,7 +360,6 @@ impl NetworkBehaviour for Behaviour {
361360
.chain(self.external_addresses.iter())
362361
.cloned()
363362
.collect(),
364-
supported_protocols: supported_protocols(params),
365363
protocol: Protocol::Identify(connection_id),
366364
},
367365
}),
@@ -488,15 +486,6 @@ pub enum Event {
488486
},
489487
}
490488

491-
fn supported_protocols(params: &impl PollParameters) -> Vec<String> {
492-
// The protocol names can be bytes, but the identify protocol except UTF-8 strings.
493-
// There's not much we can do to solve this conflict except strip non-UTF-8 characters.
494-
params
495-
.supported_protocols()
496-
.map(|p| String::from_utf8_lossy(&p).to_string())
497-
.collect()
498-
}
499-
500489
/// If there is a given peer_id in the multiaddr, make sure it is the same as
501490
/// the given peer_id. If there is no peer_id for the peer in the mutiaddr, this returns true.
502491
fn multiaddr_matches_peer_id(addr: &Multiaddr, peer_id: &PeerId) -> bool {

protocols/identify/src/handler.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use libp2p_identity::PeerId;
3232
use libp2p_identity::PublicKey;
3333
use libp2p_swarm::handler::{
3434
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
35+
ProtocolsChange,
3536
};
3637
use libp2p_swarm::{
3738
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
@@ -83,6 +84,8 @@ pub struct Handler {
8384

8485
/// Address observed by or for the remote.
8586
observed_addr: Multiaddr,
87+
88+
local_supported_protocols: Vec<String>,
8689
}
8790

8891
/// An event from `Behaviour` with the information requested by the `Handler`.
@@ -91,9 +94,6 @@ pub struct InEvent {
9194
/// The addresses that the peer is listening on.
9295
pub listen_addrs: Vec<Multiaddr>,
9396

94-
/// The list of protocols supported by the peer, e.g. `/ipfs/ping/1.0.0`.
95-
pub supported_protocols: Vec<String>,
96-
9797
/// The protocol w.r.t. the information requested.
9898
pub protocol: Protocol,
9999
}
@@ -138,6 +138,7 @@ impl Handler {
138138
protocol_version,
139139
agent_version,
140140
observed_addr,
141+
local_supported_protocols: vec![],
141142
}
142143
}
143144

@@ -238,7 +239,6 @@ impl ConnectionHandler for Handler {
238239
&mut self,
239240
InEvent {
240241
listen_addrs,
241-
supported_protocols,
242242
protocol,
243243
}: Self::InEvent,
244244
) {
@@ -247,7 +247,7 @@ impl ConnectionHandler for Handler {
247247
protocol_version: self.protocol_version.clone(),
248248
agent_version: self.agent_version.clone(),
249249
listen_addrs,
250-
protocols: supported_protocols,
250+
protocols: self.local_supported_protocols.clone(),
251251
observed_addr: self.observed_addr.clone(),
252252
};
253253

@@ -344,9 +344,10 @@ impl ConnectionHandler for Handler {
344344
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
345345
self.on_dial_upgrade_error(dial_upgrade_error)
346346
}
347-
ConnectionEvent::AddressChange(_)
348-
| ConnectionEvent::ListenUpgradeError(_)
349-
| ConnectionEvent::LocalProtocolsChange(_) => {}
347+
ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) => {}
348+
ConnectionEvent::LocalProtocolsChange(ProtocolsChange { protocols }) => {
349+
self.local_supported_protocols = protocols.to_vec();
350+
}
350351
ConnectionEvent::RemoteProtocolsChange(_) => {}
351352
}
352353
}

0 commit comments

Comments
 (0)