Skip to content
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

Async DNS seeder lookups #1662

Merged
merged 9 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 26 additions & 4 deletions zebra-network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ pub struct Config {
}

teor2345 marked this conversation as resolved.
Show resolved Hide resolved
impl Config {
fn parse_peers<S: ToSocketAddrs>(peers: HashSet<S>) -> HashSet<SocketAddr> {
async fn parse_peers<S: ToSocketAddrs>(peers: HashSet<S>) -> HashSet<SocketAddr> {
// Test dns function
let _test_dns = Config::resolve("willnotresolve.whatev3r:8233".to_string()).await;

peers
.iter()
.flat_map(|s| s.to_socket_addrs())
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -46,10 +49,29 @@ impl Config {
}

/// Get the initial seed peers based on the configured network.
pub fn initial_peers(&self) -> HashSet<SocketAddr> {
pub async fn initial_peers(&self) -> HashSet<SocketAddr> {
match self.network {
Network::Mainnet => Config::parse_peers(self.initial_mainnet_peers.clone()),
Network::Testnet => Config::parse_peers(self.initial_testnet_peers.clone()),
Network::Mainnet => Config::parse_peers(self.initial_mainnet_peers.clone()).await,
Network::Testnet => Config::parse_peers(self.initial_testnet_peers.clone()).await,
}
}

async fn resolve(s: String) -> Option<impl Iterator<Item = SocketAddr>> {
// Resolve using `lookup_host`
let fut = tokio::net::lookup_host(s.clone());
// Add timeout
let fut = tokio::time::timeout(Duration::from_nanos(1), fut);
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved

match fut.await {
Ok(Ok(ips)) => Some(ips),
Ok(Err(e)) => {
tracing::warn!("Can't resolve {:?}. Error: {:?}", s, e);
None
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
}
Err(e) => {
tracing::warn!("Timeout trying to resolve {:?}: {:?}", s, e);
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
None
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion zebra-network/src/peer_set/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ where
let tx = peerset_tx.clone();

// Connect the tx end to the 3 peer sources:
add_initial_peers(initial_peers, connector, tx)
add_initial_peers(initial_peers.await, connector, tx)
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
};

// 2. Initial peers, specified in the config.
Expand Down