Skip to content

Commit

Permalink
P0 [dev.net] - Allow hostnames to be parsed for Narwhal config (Myste…
Browse files Browse the repository at this point in the history
…nLabs#1595)

and also hostnames to be used for gateway to authority config
  • Loading branch information
velvia authored Apr 27, 2022
1 parent d5a3882 commit 33677cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
17 changes: 14 additions & 3 deletions network_utils/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,20 @@ pub struct TcpDataStream {

impl TcpDataStream {
async fn connect(address: String, max_data_size: usize) -> Result<Self, std::io::Error> {
let addr = address
.parse()
.map_err(|e| std::io::Error::new(ErrorKind::Other, e))?;
let addr = match address.parse() {
Ok(addr) => addr,
Err(_) => {
// Maybe it's a host name, try doing lookup first
if let Some(addr) = tokio::net::lookup_host(address.clone()).await?.next() {
addr
} else {
return Err(std::io::Error::new(
ErrorKind::Other,
format!("Could not lookup address {address}"),
));
}
}
};
let socket = TcpSocket::new_v4()?;
socket.set_send_buffer_size(max_data_size as u32)?;
socket.set_recv_buffer_size(max_data_size as u32)?;
Expand Down
34 changes: 20 additions & 14 deletions sui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::fmt::Write;
use std::fmt::{Display, Formatter};
use std::fs::{self, File};
use std::io::BufReader;
use std::net::SocketAddr;
use std::net::{SocketAddr, ToSocketAddrs};
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
Expand Down Expand Up @@ -56,6 +56,16 @@ pub struct AuthorityPrivateInfo {
pub consensus_address: SocketAddr,
}

// Warning: to_socket_addrs() is blocking and can fail. Be careful where you use it.
fn socket_addr_from_hostport(host: &str, port: u16) -> SocketAddr {
let mut addresses = format!("{host}:{port}")
.to_socket_addrs()
.expect("Cannot parse {host} and {port} into socket address");
addresses
.next()
.expect("Hostname/IP resolution failed for {host}")
}

// Custom deserializer with optional default fields
impl<'de> Deserialize<'de> for AuthorityPrivateInfo {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
Expand Down Expand Up @@ -104,7 +114,7 @@ impl<'de> Deserialize<'de> for AuthorityPrivateInfo {
.map_err(serde::de::Error::custom)?
.next_port()
.ok_or_else(|| serde::de::Error::custom("No available port."))?;
format!("127.0.0.1:{port}").parse().unwrap()
socket_addr_from_hostport("localhost", port)
};

Ok(AuthorityPrivateInfo {
Expand Down Expand Up @@ -178,19 +188,15 @@ impl NetworkConfig {
.map(|x| {
let name = x.key_pair.make_narwhal_keypair().name;
let primary = PrimaryAddresses {
primary_to_primary: format!("{}:{}", x.host, x.port + 100).parse().unwrap(),
worker_to_primary: format!("{}:{}", x.host, x.port + 200).parse().unwrap(),
primary_to_primary: socket_addr_from_hostport(&x.host, x.port + 100),
worker_to_primary: socket_addr_from_hostport(&x.host, x.port + 200),
};
let workers = [(
/* worker_id */ 0,
WorkerAddresses {
primary_to_worker: format!("{}:{}", x.host, x.port + 300)
.parse()
.unwrap(),
primary_to_worker: socket_addr_from_hostport(&x.host, x.port + 300),
transactions: x.consensus_address,
worker_to_worker: format!("{}:{}", x.host, x.port + 400)
.parse()
.unwrap(),
worker_to_worker: socket_addr_from_hostport(&x.host, x.port + 400),
},
)]
.iter()
Expand Down Expand Up @@ -394,15 +400,15 @@ pub fn make_default_narwhal_committee(
let name = x.key_pair.make_narwhal_keypair().name;

let primary = PrimaryAddresses {
primary_to_primary: format!("127.0.0.1:{}", ports[i][0]).parse().unwrap(),
worker_to_primary: format!("127.0.0.1:{}", ports[i][1]).parse().unwrap(),
primary_to_primary: socket_addr_from_hostport("127.0.0.1", ports[i][0]),
worker_to_primary: socket_addr_from_hostport("127.0.0.1", ports[i][1]),
};
let workers = [(
/* worker_id */ 0,
WorkerAddresses {
primary_to_worker: format!("127.0.0.1:{}", ports[i][2]).parse().unwrap(),
primary_to_worker: socket_addr_from_hostport("127.0.0.1", ports[i][2]),
transactions: x.consensus_address,
worker_to_worker: format!("127.0.0.1:{}", ports[i][3]).parse().unwrap(),
worker_to_worker: socket_addr_from_hostport("127.0.0.1", ports[i][3]),
},
)]
.iter()
Expand Down

0 comments on commit 33677cf

Please sign in to comment.