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 18, 2024
1 parent f6d7a35 commit d644c5f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 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: 21 additions & 9 deletions src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,28 @@ 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}"
))?)
Ok(build_multiaddr(port, peer_id))
}

///
/// Private Helpers
///
/// Builds a MultiAddress taking into account any required ffeature set of the nodes.
/// Default is a quic multiaddress
fn build_multiaddr(port: u16, peer_id: PeerId) -> Multiaddr {
let addr = Multiaddr::from(std::net::Ipv4Addr::LOCALHOST);

#[cfg(feature = "tcp")]
let addr = addr.with(libp2p::multiaddr::Protocol::Tcp(port));
#[cfg(feature = "quic")]
let addr = addr
.with(libp2p::multiaddr::Protocol::Udp(port))
.with(libp2p::multiaddr::Protocol::QuicV1);

addr.with(libp2p::multiaddr::Protocol::P2p(peer_id))
}

#[cfg(target_os = "windows")]
fn get_username() -> Result<String> {
Ok(std::env::var("USERNAME")?)
Expand Down Expand Up @@ -376,8 +389,8 @@ mod tests {
let peer_id = PeerId::from_str("12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR")?;
let port = 12000;
let rpc_port = 13000;
let node_multiaddr =
Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"))?;

let node_multiaddr = build_multiaddr(port, peer_id);

mock_launcher
.expect_get_safenode_version()
Expand Down Expand Up @@ -452,8 +465,8 @@ mod tests {
#[tokio::test]
async fn run_node_should_launch_an_additional_node() -> Result<()> {
let peer_id = PeerId::from_str("12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR")?;
let genesis_peer_addr =
Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/12000/p2p/{peer_id}"))?;

let genesis_peer_addr = build_multiaddr(12000, peer_id);

let mut mock_launcher = MockLauncher::new();
let mut node_registry = NodeRegistry {
Expand All @@ -480,8 +493,7 @@ 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}"))?;
let node_peer_addr = build_multiaddr(port, peer_id);

mock_launcher
.expect_get_safenode_version()
Expand Down
12 changes: 9 additions & 3 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ use service_manager::{
ServiceUninstallCtx,
};
use std::ffi::OsString;
use std::net::{SocketAddr, TcpListener};
use std::net::SocketAddr;
#[cfg(feature = "tcp")]
use std::net::TcpListener as SocketBinder;
#[cfg(not(feature = "tcp"))]
use std::net::UdpSocket as SocketBinder;

use std::path::PathBuf;
use sysinfo::{Pid, System, SystemExt};

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

fn is_port_free(&self, port: u16) -> bool {
TcpListener::bind(("127.0.0.1", port)).is_ok()
SocketBinder::bind(("127.0.0.1", port)).is_ok()
}

fn is_service_process_running(&self, pid: u32) -> bool {
Expand All @@ -156,7 +161,8 @@ 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())

Ok(SocketBinder::bind(addr)?.local_addr()?.port())
}

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

0 comments on commit d644c5f

Please sign in to comment.