Skip to content

Commit eb8fffa

Browse files
authored
feat(identify): include pushed Info in Event::Pushed
Starting in #3980, we make more use of the identify push protocol by actively notifying the peer when our locally supported protocols change. We emit an event (`Pushed`) to the user in that case. This event however does not include **what** we pushed. Users might be interested in the internal changes that we push to remote peers. This patch adds the identify `Info` to the `Pushed` variant. Fixes #4332. Pull-Request: #4527.
1 parent 79f961f commit eb8fffa

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

protocols/identify/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 0.44.0 - unreleased
22

3+
- Add `Info` to the `libp2p-identify::Event::Pushed` to report pushed info.
4+
See [PR 4527](https://github.com/libp2p/rust-libp2p/pull/4527)
35

46
## 0.43.1
57

protocols/identify/src/behaviour.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ impl NetworkBehaviour for Behaviour {
301301
self.events
302302
.push_back(ToSwarm::GenerateEvent(Event::Sent { peer_id }));
303303
}
304-
handler::Event::IdentificationPushed => {
304+
handler::Event::IdentificationPushed(info) => {
305305
self.events
306-
.push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id }));
306+
.push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id, info }));
307307
}
308308
handler::Event::IdentificationError(error) => {
309309
self.events
@@ -431,6 +431,9 @@ pub enum Event {
431431
Pushed {
432432
/// The peer that the information has been sent to.
433433
peer_id: PeerId,
434+
/// The full Info struct we pushed to the remote peer. Clients must
435+
/// do some diff'ing to know what has changed since the last push.
436+
info: Info,
434437
},
435438
/// Error while attempting to identify the remote.
436439
Error {

protocols/identify/src/handler.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub enum Event {
110110
/// We replied to an identification request from the remote.
111111
Identification,
112112
/// We actively pushed our identification information to the remote.
113-
IdentificationPushed,
113+
IdentificationPushed(Info),
114114
/// Failed to identify the remote, or to reply to an identification request.
115115
IdentificationError(StreamUpgradeError<UpgradeError>),
116116
}
@@ -211,7 +211,7 @@ impl Handler {
211211
if self
212212
.active_streams
213213
.try_push(
214-
protocol::send_identify(stream, info).map_ok(|_| Success::SentIdentifyPush),
214+
protocol::send_identify(stream, info).map_ok(Success::SentIdentifyPush),
215215
)
216216
.is_err()
217217
{
@@ -352,9 +352,9 @@ impl ConnectionHandler for Handler {
352352
remote_info,
353353
)));
354354
}
355-
Poll::Ready(Ok(Ok(Success::SentIdentifyPush))) => {
355+
Poll::Ready(Ok(Ok(Success::SentIdentifyPush(info)))) => {
356356
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(
357-
Event::IdentificationPushed,
357+
Event::IdentificationPushed(info),
358358
));
359359
}
360360
Poll::Ready(Ok(Ok(Success::SentIdentify))) => {
@@ -446,6 +446,6 @@ impl ConnectionHandler for Handler {
446446
enum Success {
447447
SentIdentify,
448448
ReceivedIdentify(Info),
449-
SentIdentifyPush,
449+
SentIdentifyPush(Info),
450450
ReceivedIdentifyPush(PushInfo),
451451
}

protocols/identify/src/protocol.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,23 @@ pub struct PushInfo {
9090
pub observed_addr: Option<Multiaddr>,
9191
}
9292

93-
pub(crate) async fn send_identify<T>(io: T, info: Info) -> Result<(), UpgradeError>
93+
pub(crate) async fn send_identify<T>(io: T, info: Info) -> Result<Info, UpgradeError>
9494
where
9595
T: AsyncWrite + Unpin,
9696
{
9797
trace!("Sending: {:?}", info);
9898

99-
let listen_addrs = info
100-
.listen_addrs
101-
.into_iter()
102-
.map(|addr| addr.to_vec())
103-
.collect();
99+
let listen_addrs = info.listen_addrs.iter().map(|addr| addr.to_vec()).collect();
104100

105101
let pubkey_bytes = info.public_key.encode_protobuf();
106102

107103
let message = proto::Identify {
108-
agentVersion: Some(info.agent_version),
109-
protocolVersion: Some(info.protocol_version),
104+
agentVersion: Some(info.agent_version.clone()),
105+
protocolVersion: Some(info.protocol_version.clone()),
110106
publicKey: Some(pubkey_bytes),
111107
listenAddrs: listen_addrs,
112108
observedAddr: Some(info.observed_addr.to_vec()),
113-
protocols: info.protocols.into_iter().map(|p| p.to_string()).collect(),
109+
protocols: info.protocols.iter().map(|p| p.to_string()).collect(),
114110
};
115111

116112
let mut framed_io = FramedWrite::new(
@@ -121,7 +117,7 @@ where
121117
framed_io.send(message).await?;
122118
framed_io.close().await?;
123119

124-
Ok(())
120+
Ok(info)
125121
}
126122

127123
pub(crate) async fn recv_push<T>(socket: T) -> Result<PushInfo, UpgradeError>

0 commit comments

Comments
 (0)