Skip to content

Commit

Permalink
feat(identify): include pushed Info in Event::Pushed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dhuseby authored Oct 20, 2023
1 parent 79f961f commit eb8fffa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 2 additions & 0 deletions protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 5 additions & 2 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UpgradeError>),
}
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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))) => {
Expand Down Expand Up @@ -446,6 +446,6 @@ impl ConnectionHandler for Handler {
enum Success {
SentIdentify,
ReceivedIdentify(Info),
SentIdentifyPush,
SentIdentifyPush(Info),
ReceivedIdentifyPush(PushInfo),
}
16 changes: 6 additions & 10 deletions protocols/identify/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,23 @@ pub struct PushInfo {
pub observed_addr: Option<Multiaddr>,
}

pub(crate) async fn send_identify<T>(io: T, info: Info) -> Result<(), UpgradeError>
pub(crate) async fn send_identify<T>(io: T, info: Info) -> Result<Info, UpgradeError>
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(
Expand All @@ -121,7 +117,7 @@ where
framed_io.send(message).await?;
framed_io.close().await?;

Ok(())
Ok(info)
}

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

0 comments on commit eb8fffa

Please sign in to comment.