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 441a81a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 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
28 changes: 24 additions & 4 deletions src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
))?)
}

///
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
10 changes: 9 additions & 1 deletion 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,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 {
Expand All @@ -156,7 +160,11 @@ impl ServiceControl for NodeServiceManager {

fn get_available_port(&self) -> Result<u16> {
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<()> {
Expand Down

0 comments on commit 441a81a

Please sign in to comment.