Skip to content

Commit 961ed42

Browse files
committed
feat: Add support for both ip and hostname for peers
It is convinent to be able to specify both IPs and hostnames as peers. Especially, when you are building ephemeral testnets and want to specify peers as ``` ./snarkos start --peers "validator0:4130,client0:4130" ``` Where you can populate the `/etc/hosts` file with subnet hostnames.
1 parent e8d3a90 commit 961ed42

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

cli/src/commands/start.rs

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use rand::{Rng, SeedableRng};
4242
use rand_chacha::ChaChaRng;
4343
use serde::{Deserialize, Serialize};
4444
use std::{
45-
net::SocketAddr,
45+
net::{SocketAddr, ToSocketAddrs},
4646
path::PathBuf,
4747
sync::{Arc, atomic::AtomicBool},
4848
};
@@ -230,11 +230,18 @@ impl Start {
230230
false => Ok(self
231231
.peers
232232
.split(',')
233-
.flat_map(|ip| match ip.parse::<SocketAddr>() {
234-
Ok(ip) => Some(ip),
235-
Err(e) => {
236-
eprintln!("The IP supplied to --peers ('{ip}') is malformed: {e}");
237-
None
233+
.flat_map(|ip_or_hostname| {
234+
let trimmed = ip_or_hostname.trim();
235+
match trimmed.to_socket_addrs() {
236+
Ok(mut ip_iter) => {
237+
// A hostname might resolve to multiple IP addresses. We will use only the first one,
238+
// assuming this aligns with the user's expectations.
239+
ip_iter.next()
240+
},
241+
Err(e) => {
242+
eprintln!("The hostname or IP supplied to --peers ('{trimmed}') is malformed: {e}");
243+
None
244+
}
238245
}
239246
})
240247
.collect()),
@@ -1046,4 +1053,60 @@ mod tests {
10461053
panic!("Unexpected result of clap parsing!");
10471054
}
10481055
}
1056+
1057+
#[test]
1058+
fn parse_peers_when_ips() {
1059+
let arg_vec = vec!["snarkos", "start", "--peers", "127.0.0.1:3030,127.0.0.2:3030"];
1060+
let cli = CLI::parse_from(arg_vec);
1061+
1062+
if let Command::Start(start) = cli.command {
1063+
let peers = start.parse_trusted_peers();
1064+
assert!(peers.is_ok());
1065+
assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
1066+
} else {
1067+
panic!("Unexpected result of clap parsing!");
1068+
}
1069+
}
1070+
1071+
#[test]
1072+
fn parse_peers_when_hostnames() {
1073+
let arg_vec = vec!["snarkos", "start", "--peers", "www.example.com:4130,www.google.com:4130"];
1074+
let cli = CLI::parse_from(arg_vec);
1075+
1076+
if let Command::Start(start) = cli.command {
1077+
let peers = start.parse_trusted_peers();
1078+
assert!(peers.is_ok());
1079+
assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
1080+
} else {
1081+
panic!("Unexpected result of clap parsing!");
1082+
}
1083+
}
1084+
1085+
#[test]
1086+
fn parse_peers_when_mixed_and_with_whitespaces() {
1087+
let arg_vec = vec!["snarkos", "start", "--peers", " 127.0.0.1:3030, www.google.com:4130 "];
1088+
let cli = CLI::parse_from(arg_vec);
1089+
1090+
if let Command::Start(start) = cli.command {
1091+
let peers = start.parse_trusted_peers();
1092+
assert!(peers.is_ok());
1093+
assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
1094+
} else {
1095+
panic!("Unexpected result of clap parsing!");
1096+
}
1097+
}
1098+
1099+
#[test]
1100+
fn parse_peers_when_unknown_hostname_gracefully() {
1101+
let arg_vec = vec!["snarkos", "start", "--peers", "banana.cake.eafafdaeefasdfasd.com"];
1102+
let cli = CLI::parse_from(arg_vec);
1103+
1104+
if let Command::Start(start) = cli.command {
1105+
let peers = start.parse_trusted_peers();
1106+
assert!(peers.is_ok());
1107+
assert!(peers.unwrap().is_empty(), "Expected no peers to be found");
1108+
} else {
1109+
panic!("Unexpected result of clap parsing!");
1110+
}
1111+
}
10491112
}

0 commit comments

Comments
 (0)