From 605f20846280679e26e72e34efabfb3603aed678 Mon Sep 17 00:00:00 2001 From: stormshield-frb <144998884+stormshield-frb@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:41:19 +0200 Subject: [PATCH] feat(identify): add connection_id in event Fixes: #4980. Pull-Request: #4981. --- Cargo.lock | 2 +- Cargo.toml | 2 +- libp2p/CHANGELOG.md | 1 + misc/server/CHANGELOG.md | 2 ++ misc/server/src/main.rs | 1 + protocols/identify/CHANGELOG.md | 5 ++++ protocols/identify/Cargo.toml | 2 +- protocols/identify/src/behaviour.rs | 46 +++++++++++++++++++++++------ 8 files changed, 49 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efa620c9f41..67d0c3fb59b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2837,7 +2837,7 @@ dependencies = [ [[package]] name = "libp2p-identify" -version = "0.44.2" +version = "0.45.0" dependencies = [ "async-std", "asynchronous-codec", diff --git a/Cargo.toml b/Cargo.toml index 3f7d8ab7c0d..76bb4f18b3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,7 @@ libp2p-dcutr = { version = "0.11.1", path = "protocols/dcutr" } libp2p-dns = { version = "0.41.1", path = "transports/dns" } libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.46.2", path = "protocols/gossipsub" } -libp2p-identify = { version = "0.44.2", path = "protocols/identify" } +libp2p-identify = { version = "0.45.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.9" } libp2p-kad = { version = "0.46.0", path = "protocols/kad" } libp2p-mdns = { version = "0.45.1", path = "protocols/mdns" } diff --git a/libp2p/CHANGELOG.md b/libp2p/CHANGELOG.md index 23efbc21fd9..977d9f91924 100644 --- a/libp2p/CHANGELOG.md +++ b/libp2p/CHANGELOG.md @@ -2,6 +2,7 @@ - Update individual crates. - Update to [`libp2p-kad` `v0.46.0`](protocols/kad/CHANGELOG.md#0460). + - Update to [`libp2p-identify` `v0.45.0`](protocols/identify/CHANGELOG.md#0450). - Raise MSRV to 1.73. See [PR 5266](https://github.com/libp2p/rust-libp2p/pull/5266). diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index 254ab1d92be..5369163460c 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -4,6 +4,8 @@ - Use periodic and automatic bootstrap of Kademlia. See [PR 4838](https://github.com/libp2p/rust-libp2p/pull/4838). +- Update to [`libp2p-identify` `v0.45.0`](protocols/identify/CHANGELOG.md#0450). + See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981). ## 0.12.6 diff --git a/misc/server/src/main.rs b/misc/server/src/main.rs index 6dfa035f3b1..eb28b9ad5c2 100644 --- a/misc/server/src/main.rs +++ b/misc/server/src/main.rs @@ -138,6 +138,7 @@ async fn main() -> Result<(), Box> { protocols, .. }, + .. } = e { if protocols.iter().any(|p| *p == kad::PROTOCOL_NAME) { diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index 83984448d07..b8ab3f8df50 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.45.0 + +- Add `ConnectionId` in `Event`. + See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981). + ## 0.44.2 - Emit `ToSwarm::NewExternalAddrOfPeer` for all external addresses of remote peers. diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 86425d580e9..320c8465650 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-identify" edition = "2021" rust-version = { workspace = true } description = "Nodes identification protocol for libp2p" -version = "0.44.2" +version = "0.45.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 43bddb52fe7..92a0dc46103 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -258,7 +258,7 @@ impl NetworkBehaviour for Behaviour { fn on_connection_handler_event( &mut self, peer_id: PeerId, - id: ConnectionId, + connection_id: ConnectionId, event: THandlerOutEvent, ) { match event { @@ -270,6 +270,7 @@ impl NetworkBehaviour for Behaviour { let observed = info.observed_addr.clone(); self.events .push_back(ToSwarm::GenerateEvent(Event::Received { + connection_id, peer_id, info: info.clone(), })); @@ -285,7 +286,7 @@ impl NetworkBehaviour for Behaviour { } } - match self.our_observed_addresses.entry(id) { + match self.our_observed_addresses.entry(connection_id) { Entry::Vacant(not_yet_observed) => { not_yet_observed.insert(observed.clone()); self.events @@ -298,7 +299,7 @@ impl NetworkBehaviour for Behaviour { tracing::info!( old_address=%already_observed.get(), new_address=%observed, - "Our observed address on connection {id} changed", + "Our observed address on connection {connection_id} changed", ); *already_observed.get_mut() = observed.clone(); @@ -308,16 +309,24 @@ impl NetworkBehaviour for Behaviour { } } handler::Event::Identification => { - self.events - .push_back(ToSwarm::GenerateEvent(Event::Sent { peer_id })); + self.events.push_back(ToSwarm::GenerateEvent(Event::Sent { + connection_id, + peer_id, + })); } handler::Event::IdentificationPushed(info) => { - self.events - .push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id, info })); + self.events.push_back(ToSwarm::GenerateEvent(Event::Pushed { + connection_id, + peer_id, + info, + })); } handler::Event::IdentificationError(error) => { - self.events - .push_back(ToSwarm::GenerateEvent(Event::Error { peer_id, error })); + self.events.push_back(ToSwarm::GenerateEvent(Event::Error { + connection_id, + peer_id, + error, + })); } } } @@ -415,6 +424,8 @@ impl NetworkBehaviour for Behaviour { pub enum Event { /// Identification information has been received from a peer. Received { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer that has been identified. peer_id: PeerId, /// The information provided by the peer. @@ -423,12 +434,16 @@ pub enum Event { /// Identification information of the local node has been sent to a peer in /// response to an identification request. Sent { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer that the information has been sent to. peer_id: PeerId, }, /// Identification information of the local node has been actively pushed to /// a peer. Pushed { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer that the information has been sent to. peer_id: PeerId, /// The full Info struct we pushed to the remote peer. Clients must @@ -437,6 +452,8 @@ pub enum Event { }, /// Error while attempting to identify the remote. Error { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer with whom the error originated. peer_id: PeerId, /// The error that occurred. @@ -444,6 +461,17 @@ pub enum Event { }, } +impl Event { + pub fn connection_id(&self) -> ConnectionId { + match self { + Event::Received { connection_id, .. } + | Event::Sent { connection_id, .. } + | Event::Pushed { connection_id, .. } + | Event::Error { connection_id, .. } => *connection_id, + } + } +} + /// If there is a given peer_id in the multiaddr, make sure it is the same as /// the given peer_id. If there is no peer_id for the peer in the mutiaddr, this returns true. fn multiaddr_matches_peer_id(addr: &Multiaddr, peer_id: &PeerId) -> bool {