Skip to content

feat: Add support for both ip and hostname for peers #3432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 69 additions & 6 deletions cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use rand::{Rng, SeedableRng};
use rand_chacha::ChaChaRng;
use serde::{Deserialize, Serialize};
use std::{
net::SocketAddr,
net::{SocketAddr, ToSocketAddrs},
path::PathBuf,
sync::{Arc, atomic::AtomicBool},
};
Expand Down Expand Up @@ -230,11 +230,18 @@ impl Start {
false => Ok(self
.peers
.split(',')
.flat_map(|ip| match ip.parse::<SocketAddr>() {
Ok(ip) => Some(ip),
Err(e) => {
eprintln!("The IP supplied to --peers ('{ip}') is malformed: {e}");
None
.flat_map(|ip_or_hostname| {
let trimmed = ip_or_hostname.trim();
match trimmed.to_socket_addrs() {
Ok(mut ip_iter) => {
// A hostname might resolve to multiple IP addresses. We will use only the first one,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid any surprises for node operators, is there any known syntax for selecting a particular index? Should we invent our own, e.g. hostname:port:1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative solution would be to enforce that only one IP is expected and allowed. So if there are more than one IP found, then we will print a warning and ignore the hostname.

The main idea behind this feature is to allow for a more flexible setup during tests. And in those cases, we can make sure that there is only one "validator-1" and only one "client-42". So I think it is a good trade-off, to enforce that only one IP is expected and found.

// assuming this aligns with the user's expectations.
ip_iter.next()
},
Err(e) => {
eprintln!("The hostname or IP supplied to --peers ('{trimmed}') is malformed: {e}");
None
}
}
})
.collect()),
Expand Down Expand Up @@ -1046,4 +1053,60 @@ mod tests {
panic!("Unexpected result of clap parsing!");
}
}

#[test]
fn parse_peers_when_ips() {
let arg_vec = vec!["snarkos", "start", "--peers", "127.0.0.1:3030,127.0.0.2:3030"];
let cli = CLI::parse_from(arg_vec);

if let Command::Start(start) = cli.command {
let peers = start.parse_trusted_peers();
assert!(peers.is_ok());
assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
} else {
panic!("Unexpected result of clap parsing!");
}
}

#[test]
fn parse_peers_when_hostnames() {
let arg_vec = vec!["snarkos", "start", "--peers", "www.example.com:4130,www.google.com:4130"];
let cli = CLI::parse_from(arg_vec);

if let Command::Start(start) = cli.command {
let peers = start.parse_trusted_peers();
assert!(peers.is_ok());
assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
} else {
panic!("Unexpected result of clap parsing!");
}
}

#[test]
fn parse_peers_when_mixed_and_with_whitespaces() {
let arg_vec = vec!["snarkos", "start", "--peers", " 127.0.0.1:3030, www.google.com:4130 "];
let cli = CLI::parse_from(arg_vec);

if let Command::Start(start) = cli.command {
let peers = start.parse_trusted_peers();
assert!(peers.is_ok());
assert_eq!(peers.unwrap().len(), 2, "Expected two peers");
} else {
panic!("Unexpected result of clap parsing!");
}
}

#[test]
fn parse_peers_when_unknown_hostname_gracefully() {
let arg_vec = vec!["snarkos", "start", "--peers", "banana.cake.eafafdaeefasdfasd.com"];
let cli = CLI::parse_from(arg_vec);

if let Command::Start(start) = cli.command {
let peers = start.parse_trusted_peers();
assert!(peers.is_ok());
assert!(peers.unwrap().is_empty(), "Expected no peers to be found");
} else {
panic!("Unexpected result of clap parsing!");
}
}
}