From 88dbbf47f2f21b52e88eb4f15d4260951a7a50be Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Mon, 6 Jan 2025 09:22:13 +0530 Subject: [PATCH] feat(network): enable dcutr --- Cargo.lock | 25 +++++++++++++++++++++++++ ant-networking/Cargo.toml | 1 + ant-networking/src/driver.rs | 2 ++ ant-networking/src/event/mod.rs | 7 +++++++ ant-networking/src/event/swarm.rs | 12 ++++++++++++ ant-networking/src/metrics/mod.rs | 6 ++++++ 6 files changed, 53 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fb3a998472..fa4f76f051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5454,6 +5454,7 @@ dependencies = [ "libp2p-autonat", "libp2p-connection-limits", "libp2p-core", + "libp2p-dcutr", "libp2p-dns", "libp2p-gossipsub", "libp2p-identify", @@ -5557,6 +5558,29 @@ dependencies = [ "web-time", ] +[[package]] +name = "libp2p-dcutr" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3236a2e24cbcf2d05b398b003ed920e1e8cedede13784d90fa3961b109647ce0" +dependencies = [ + "asynchronous-codec", + "either", + "futures", + "futures-bounded", + "futures-timer", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "lru", + "quick-protobuf", + "quick-protobuf-codec", + "thiserror 1.0.69", + "tracing", + "void", + "web-time", +] + [[package]] name = "libp2p-dns" version = "0.42.0" @@ -5706,6 +5730,7 @@ checksum = "77ebafa94a717c8442d8db8d3ae5d1c6a15e30f2d347e0cd31d057ca72e42566" dependencies = [ "futures", "libp2p-core", + "libp2p-dcutr", "libp2p-identify", "libp2p-identity", "libp2p-kad", diff --git a/ant-networking/Cargo.toml b/ant-networking/Cargo.toml index c1b02c82df..93ae86bcc6 100644 --- a/ant-networking/Cargo.toml +++ b/ant-networking/Cargo.toml @@ -40,6 +40,7 @@ hyper = { version = "0.14", features = [ itertools = "~0.12.1" lazy_static = "~1.4.0" libp2p = { version = "0.54.1", features = [ + "dcutr", "tokio", "dns", "kad", diff --git a/ant-networking/src/driver.rs b/ant-networking/src/driver.rs index bb1637a099..30f5b3d45b 100644 --- a/ant-networking/src/driver.rs +++ b/ant-networking/src/driver.rs @@ -260,6 +260,7 @@ pub(super) struct NodeBehaviour { pub(super) upnp: libp2p::swarm::behaviour::toggle::Toggle, pub(super) relay_client: libp2p::relay::client::Behaviour, pub(super) relay_server: libp2p::relay::Behaviour, + pub(super) dcutr: libp2p::dcutr::Behaviour, pub(super) kademlia: kad::Behaviour, pub(super) request_response: request_response::cbor::Behaviour, } @@ -683,6 +684,7 @@ impl NetworkBuilder { #[cfg(feature = "upnp")] upnp, request_response, + dcutr: libp2p::dcutr::Behaviour::new(peer_id), kademlia, identify, #[cfg(feature = "local")] diff --git a/ant-networking/src/event/mod.rs b/ant-networking/src/event/mod.rs index ae6e2aefca..45569983cd 100644 --- a/ant-networking/src/event/mod.rs +++ b/ant-networking/src/event/mod.rs @@ -49,11 +49,18 @@ pub(super) enum NodeEvent { #[cfg(feature = "local")] Mdns(Box), Identify(Box), + Dcutr(Box), RelayClient(Box), RelayServer(Box), Void(void::Void), } +impl From for NodeEvent { + fn from(event: libp2p::dcutr::Event) -> Self { + NodeEvent::Dcutr(Box::new(event)) + } +} + #[cfg(feature = "upnp")] impl From for NodeEvent { fn from(event: libp2p::upnp::Event) -> Self { diff --git a/ant-networking/src/event/swarm.rs b/ant-networking/src/event/swarm.rs index fbd0c4bcb1..c2c2e923b6 100644 --- a/ant-networking/src/event/swarm.rs +++ b/ant-networking/src/event/swarm.rs @@ -73,6 +73,18 @@ impl SwarmDriver { } } } + SwarmEvent::Behaviour(NodeEvent::Dcutr(event)) => { + #[cfg(feature = "open-metrics")] + if let Some(metrics) = &self.metrics_recorder { + metrics.record(&(*event)); + } + + event_string = "dcutr_event"; + info!( + "Dcutr with remote peer: {:?} is: {:?}", + event.remote_peer_id, event.result + ); + } #[cfg(feature = "upnp")] SwarmEvent::Behaviour(NodeEvent::Upnp(upnp_event)) => { #[cfg(feature = "open-metrics")] diff --git a/ant-networking/src/metrics/mod.rs b/ant-networking/src/metrics/mod.rs index 71d7f8fdbb..a62e534dc6 100644 --- a/ant-networking/src/metrics/mod.rs +++ b/ant-networking/src/metrics/mod.rs @@ -355,3 +355,9 @@ impl Recorder> for NetworkMetricsRecorder { self.libp2p_metrics.record(event); } } + +impl Recorder for NetworkMetricsRecorder { + fn record(&self, event: &libp2p::dcutr::Event) { + self.libp2p_metrics.record(event) + } +}