Skip to content

Commit

Permalink
feat(identify): add connection_id in event
Browse files Browse the repository at this point in the history
Fixes: libp2p#4980.

Pull-Request: libp2p#4981.
  • Loading branch information
stormshield-frb authored Jun 20, 2024
1 parent 32e917f commit 605f208
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions libp2p/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions misc/server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions misc/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
protocols,
..
},
..
} = e
{
if protocols.iter().any(|p| *p == kad::PROTOCOL_NAME) {
Expand Down
5 changes: 5 additions & 0 deletions protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
46 changes: 37 additions & 9 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>,
) {
match event {
Expand All @@ -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(),
}));
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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,
}));
}
}
}
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -437,13 +452,26 @@ 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.
error: StreamUpgradeError<UpgradeError>,
},
}

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 {
Expand Down

0 comments on commit 605f208

Please sign in to comment.