Skip to content

Commit

Permalink
feat(identify): add connection_id in event
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-frb committed Dec 5, 2023
1 parent 0f98c98 commit dfca077
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions misc/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
protocols,
..
},
..
} = e
{
if protocols.iter().any(|p| *p == kad::PROTOCOL_NAME) {
Expand Down
2 changes: 2 additions & 0 deletions protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Ensure `Multiaddr` handled and returned by `Behaviour` are `/p2p` terminated.
See [PR 4596](https://github.com/libp2p/rust-libp2p/pull/4596).
- Add `ConnectionId` in `Event`.
See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981)

## 0.44.0

Expand Down
51 changes: 41 additions & 10 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,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 @@ -274,9 +274,13 @@ impl NetworkBehaviour for Behaviour {

let observed = info.observed_addr.clone();
self.events
.push_back(ToSwarm::GenerateEvent(Event::Received { peer_id, info }));
.push_back(ToSwarm::GenerateEvent(Event::Received {
connection_id,
peer_id,
info,
}));

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 @@ -289,7 +293,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 @@ -299,16 +303,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 @@ -406,6 +418,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 @@ -414,12 +428,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 @@ -428,13 +446,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 dfca077

Please sign in to comment.