Skip to content

Commit

Permalink
feat(dcutr): add ConnectionId to upgrade success/failed events
Browse files Browse the repository at this point in the history
Resolves #4519.

Pull-Request: #4558.
  • Loading branch information
dariusc93 authored Oct 20, 2023
1 parent fafa6bc commit 3c00ae8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
8 changes: 5 additions & 3 deletions misc/metrics/src/dcutr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ impl From<&libp2p_dcutr::Event> for EventType {
remote_peer_id: _,
remote_relayed_addr: _,
} => EventType::RemoteInitiatedDirectConnectionUpgrade,
libp2p_dcutr::Event::DirectConnectionUpgradeSucceeded { remote_peer_id: _ } => {
EventType::DirectConnectionUpgradeSucceeded
}
libp2p_dcutr::Event::DirectConnectionUpgradeSucceeded {
remote_peer_id: _,
connection_id: _,
} => EventType::DirectConnectionUpgradeSucceeded,
libp2p_dcutr::Event::DirectConnectionUpgradeFailed {
remote_peer_id: _,
connection_id: _,
error: _,
} => EventType::DirectConnectionUpgradeFailed,
}
Expand Down
4 changes: 4 additions & 0 deletions protocols/dcutr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.11.0 - unreleased

- Add `ConnectionId` to `Event::DirectConnectionUpgradeSucceeded` and `Event::DirectConnectionUpgradeFailed`.
See [PR 4558].

[PR 4558]: https://github.com/libp2p/rust-libp2p/pull/4558

## 0.10.0

Expand Down
6 changes: 6 additions & 0 deletions protocols/dcutr/src/behaviour_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ pub enum Event {
},
DirectConnectionUpgradeSucceeded {
remote_peer_id: PeerId,
connection_id: ConnectionId,
},
DirectConnectionUpgradeFailed {
remote_peer_id: PeerId,
connection_id: ConnectionId,
error: Error,
},
}
Expand Down Expand Up @@ -151,6 +153,7 @@ impl Behaviour {
self.queued_events.extend([ToSwarm::GenerateEvent(
Event::DirectConnectionUpgradeFailed {
remote_peer_id: peer_id,
connection_id: failed_direct_connection,
error: Error::Dial,
},
)]);
Expand Down Expand Up @@ -263,6 +266,7 @@ impl NetworkBehaviour for Behaviour {
self.queued_events.extend([ToSwarm::GenerateEvent(
Event::DirectConnectionUpgradeSucceeded {
remote_peer_id: peer,
connection_id,
},
)]);
}
Expand Down Expand Up @@ -300,6 +304,7 @@ impl NetworkBehaviour for Behaviour {
self.queued_events.push_back(ToSwarm::GenerateEvent(
Event::DirectConnectionUpgradeFailed {
remote_peer_id: event_source,
connection_id,
error: Error::Handler(error),
},
));
Expand All @@ -320,6 +325,7 @@ impl NetworkBehaviour for Behaviour {
self.queued_events.push_back(ToSwarm::GenerateEvent(
Event::DirectConnectionUpgradeFailed {
remote_peer_id: event_source,
connection_id: relayed_connection_id,
error: Error::Handler(error),
},
));
Expand Down
28 changes: 21 additions & 7 deletions protocols/dcutr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,27 @@ async fn connect() {

let dst_addr = dst_addr.with(Protocol::P2p(dst_peer_id));

src.wait(move |e| match e {
SwarmEvent::ConnectionEstablished { endpoint, .. } => {
(*endpoint.get_remote_address() == dst_addr).then_some(())
}
_ => None,
})
.await;
let established_conn_id = src
.wait(move |e| match e {
SwarmEvent::ConnectionEstablished {
endpoint,
connection_id,
..
} => (*endpoint.get_remote_address() == dst_addr).then_some(connection_id),
_ => None,
})
.await;

let reported_conn_id = src
.wait(move |e| match e {
SwarmEvent::Behaviour(ClientEvent::Dcutr(
dcutr::Event::DirectConnectionUpgradeSucceeded { connection_id, .. },
)) => Some(connection_id),
_ => None,
})
.await;

assert_eq!(established_conn_id, reported_conn_id);
}

fn build_relay() -> Swarm<relay::Behaviour> {
Expand Down

0 comments on commit 3c00ae8

Please sign in to comment.