diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index 9010c7ae8be..0b47163b51a 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.44.0 - unreleased +- Add `Info` to the `libp2p-identify::Event::Pushed` to report pushed info. + See [PR 4527](https://github.com/libp2p/rust-libp2p/pull/4527) ## 0.43.1 diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index f572b937d38..c97456826fe 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -301,9 +301,9 @@ impl NetworkBehaviour for Behaviour { self.events .push_back(ToSwarm::GenerateEvent(Event::Sent { peer_id })); } - handler::Event::IdentificationPushed => { + handler::Event::IdentificationPushed(info) => { self.events - .push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id })); + .push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id, info })); } handler::Event::IdentificationError(error) => { self.events @@ -431,6 +431,9 @@ pub enum Event { Pushed { /// The peer that the information has been sent to. peer_id: PeerId, + /// The full Info struct we pushed to the remote peer. Clients must + /// do some diff'ing to know what has changed since the last push. + info: Info, }, /// Error while attempting to identify the remote. Error { diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 50b9882f2c5..b71f98e8509 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -110,7 +110,7 @@ pub enum Event { /// We replied to an identification request from the remote. Identification, /// We actively pushed our identification information to the remote. - IdentificationPushed, + IdentificationPushed(Info), /// Failed to identify the remote, or to reply to an identification request. IdentificationError(StreamUpgradeError), } @@ -211,7 +211,7 @@ impl Handler { if self .active_streams .try_push( - protocol::send_identify(stream, info).map_ok(|_| Success::SentIdentifyPush), + protocol::send_identify(stream, info).map_ok(Success::SentIdentifyPush), ) .is_err() { @@ -352,9 +352,9 @@ impl ConnectionHandler for Handler { remote_info, ))); } - Poll::Ready(Ok(Ok(Success::SentIdentifyPush))) => { + Poll::Ready(Ok(Ok(Success::SentIdentifyPush(info)))) => { return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - Event::IdentificationPushed, + Event::IdentificationPushed(info), )); } Poll::Ready(Ok(Ok(Success::SentIdentify))) => { @@ -446,6 +446,6 @@ impl ConnectionHandler for Handler { enum Success { SentIdentify, ReceivedIdentify(Info), - SentIdentifyPush, + SentIdentifyPush(Info), ReceivedIdentifyPush(PushInfo), } diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 5e2891e04e4..803b79bf79c 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -90,27 +90,23 @@ pub struct PushInfo { pub observed_addr: Option, } -pub(crate) async fn send_identify(io: T, info: Info) -> Result<(), UpgradeError> +pub(crate) async fn send_identify(io: T, info: Info) -> Result where T: AsyncWrite + Unpin, { trace!("Sending: {:?}", info); - let listen_addrs = info - .listen_addrs - .into_iter() - .map(|addr| addr.to_vec()) - .collect(); + let listen_addrs = info.listen_addrs.iter().map(|addr| addr.to_vec()).collect(); let pubkey_bytes = info.public_key.encode_protobuf(); let message = proto::Identify { - agentVersion: Some(info.agent_version), - protocolVersion: Some(info.protocol_version), + agentVersion: Some(info.agent_version.clone()), + protocolVersion: Some(info.protocol_version.clone()), publicKey: Some(pubkey_bytes), listenAddrs: listen_addrs, observedAddr: Some(info.observed_addr.to_vec()), - protocols: info.protocols.into_iter().map(|p| p.to_string()).collect(), + protocols: info.protocols.iter().map(|p| p.to_string()).collect(), }; let mut framed_io = FramedWrite::new( @@ -121,7 +117,7 @@ where framed_io.send(message).await?; framed_io.close().await?; - Ok(()) + Ok(info) } pub(crate) async fn recv_push(socket: T) -> Result