From 441a81a6838c8f949314008795852440d868988b Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Wed, 17 Jan 2024 11:27:57 +0100 Subject: [PATCH] feat: add quic/tcp features and set quic as default --- Cargo.toml | 6 ++++++ src/local.rs | 28 ++++++++++++++++++++++++---- src/service.rs | 10 +++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 267c0e1..94725fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,16 @@ description = "A command-line application for installing, managing and operating license = "GPL-3.0" repository = "https://github.com/maidsafe/sn-node-manager" + [[bin]] path="src/main.rs" name="safenode-manager" +[features] +default = ["quic"] +quic = [] +tcp = [] + [dependencies] clap = { version = "4.4.6", features = ["derive", "env"]} colored = "2.0.4" diff --git a/src/local.rs b/src/local.rs index 5f3f52a..58a4111 100644 --- a/src/local.rs +++ b/src/local.rs @@ -273,9 +273,14 @@ pub async fn run_node( safenode_path: Some(launcher.get_safenode_path()), }); + #[cfg(feature = "tcp")] Ok(Multiaddr::from_str(&format!( "/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}" ))?) + #[cfg(feature = "quic")] + Ok(Multiaddr::from_str(&format!( + "/ip4/127.0.0.1/quic/{port}/quic-v1/p2p/{peer_id}" + ))?) } /// @@ -376,8 +381,13 @@ mod tests { let peer_id = PeerId::from_str("12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR")?; let port = 12000; let rpc_port = 13000; + + #[cfg(feature = "tcp")] let node_multiaddr = - Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"))?; + Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"))?; + #[cfg(feature = "quic")] + let node_multiaddr = + Multiaddr::from_str(&format!("/ip4/127.0.0.1/quic/{port}/quic-v1/p2p/{peer_id}"))?; mock_launcher .expect_get_safenode_version() @@ -452,8 +462,14 @@ mod tests { #[tokio::test] async fn run_node_should_launch_an_additional_node() -> Result<()> { let peer_id = PeerId::from_str("12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR")?; + + #[cfg(feature = "tcp")] let genesis_peer_addr = - Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/12000/p2p/{peer_id}"))?; + Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/12000/p2p/{peer_id}"))?; + #[cfg(feature = "quic")] + let genesis_peer_addr = + Multiaddr::from_str(&format!("/ip4/127.0.0.1/quic/12000/quic-v1/p2p/{peer_id}"))?; + let mut mock_launcher = MockLauncher::new(); let mut node_registry = NodeRegistry { @@ -480,8 +496,12 @@ mod tests { let peer_id = PeerId::from_str("12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR")?; let port = 12001; let rpc_port = 13001; - let node_peer_addr = - Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"))?; + #[cfg(feature = "tcp")] + let node_multiaddr = + Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"))?; + #[cfg(feature = "quic")] + let node_multiaddr = + Multiaddr::from_str(&format!("/ip4/127.0.0.1/quic/{port}/quic-v1/p2p/{peer_id}"))?; mock_launcher .expect_get_safenode_version() diff --git a/src/service.rs b/src/service.rs index 7489f55..92bdd8e 100644 --- a/src/service.rs +++ b/src/service.rs @@ -15,7 +15,7 @@ use service_manager::{ ServiceUninstallCtx, }; use std::ffi::OsString; -use std::net::{SocketAddr, TcpListener}; +use std::net::{SocketAddr, TcpListener, UdpSocket}; use std::path::PathBuf; use sysinfo::{Pid, System, SystemExt}; @@ -145,7 +145,11 @@ impl ServiceControl for NodeServiceManager { } fn is_port_free(&self, port: u16) -> bool { + #[cfg(feature="tcp")] TcpListener::bind(("127.0.0.1", port)).is_ok() + + #[cfg(feature="quic")] + UdpSocket::bind(("127.0.0.1", port)).is_ok() } fn is_service_process_running(&self, pid: u32) -> bool { @@ -156,7 +160,11 @@ impl ServiceControl for NodeServiceManager { fn get_available_port(&self) -> Result { let addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); + #[cfg(feature="tcp")] Ok(TcpListener::bind(addr)?.local_addr()?.port()) + + #[cfg(feature="quic")] + Ok(UdpSocket::bind(addr)?.local_addr()?.port()) } fn install(&self, config: ServiceConfig) -> Result<()> {