Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat: add quic/tcp features and set quic as default
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Jan 17, 2024
1 parent f6d7a35 commit 96142bc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 26 additions & 4 deletions src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,17 @@ pub async fn run_node(
safenode_path: Some(launcher.get_safenode_path()),
});

Ok(Multiaddr::from_str(&format!(
"/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"
))?)
if cfg!(feature = "tcp") {
Ok(Multiaddr::from_str(&format!(
"/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"
))?)
} else
// efault to quic
{
Ok(Multiaddr::from_str(&format!(
"/ip4/127.0.0.1/quic/{port}/quic-v1/p2p/{peer_id}"
))?)
}
}

///
Expand Down Expand Up @@ -376,8 +384,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}"))?;
#[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()
Expand Down Expand Up @@ -452,8 +465,13 @@ 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}"))?;
#[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 {
Expand All @@ -480,8 +498,12 @@ mod tests {
let peer_id = PeerId::from_str("12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR")?;
let port = 12001;
let rpc_port = 13001;
let node_peer_addr =
#[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()
Expand Down
18 changes: 15 additions & 3 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -145,7 +145,13 @@ impl ServiceControl for NodeServiceManager {
}

fn is_port_free(&self, port: u16) -> bool {
TcpListener::bind(("127.0.0.1", port)).is_ok()
if cfg!(feature = "tcp") {
TcpListener::bind(("127.0.0.1", port)).is_ok()
} else if cfg!(feature = "quic") {
UdpSocket::bind(("127.0.0.1", port)).is_ok()
} else {
false
}
}

fn is_service_process_running(&self, pid: u32) -> bool {
Expand All @@ -156,7 +162,13 @@ impl ServiceControl for NodeServiceManager {

fn get_available_port(&self) -> Result<u16> {
let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
Ok(TcpListener::bind(addr)?.local_addr()?.port())
if cfg!(feature = "tcp") {
Ok(TcpListener::bind(addr)?.local_addr()?.port())
} else
// default to quic
{
Ok(UdpSocket::bind(addr)?.local_addr()?.port())
}
}

fn install(&self, config: ServiceConfig) -> Result<()> {
Expand Down

0 comments on commit 96142bc

Please sign in to comment.