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 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
59 changes: 45 additions & 14 deletions zebra-network/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
collections::HashSet,
net::{SocketAddr, ToSocketAddrs},
string::String,
time::Duration,
};
use std::{collections::HashSet, net::SocketAddr, string::String, time::Duration};

use zebra_chain::parameters::Network;

Expand Down Expand Up @@ -37,19 +32,55 @@ 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> {
peers
/// Concurrently resolves `peers` into zero or more IP addresses, with a timeout
/// of a few seconds on each DNS request.
///
/// If DNS resolution fails or times out for all peers, returns an empty list.
async fn parse_peers(peers: &HashSet<String>) -> HashSet<SocketAddr> {
use futures::stream::StreamExt;
let peer_addresses = peers
.iter()
.flat_map(|s| s.to_socket_addrs())
.flatten()
.collect()
.map(|s| Config::resolve_host(s))
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
.collect::<futures::stream::FuturesUnordered<_>>()
.concat()
.await;

if peer_addresses.is_empty() {
tracing::warn!(
?peers,
?peer_addresses,
"empty peer list after DNS resolution"
);
};
peer_addresses
}

/// 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).await,
Network::Testnet => Config::parse_peers(&self.initial_testnet_peers).await,
}
}

/// Resolves `host` into zero or more IP addresses.
///
/// If `host` is a DNS name, performs DNS resolution with a timeout of a few seconds.
/// If DNS resolution fails or times out, returns an empty list.
async fn resolve_host(host: &str) -> HashSet<SocketAddr> {
let fut = tokio::net::lookup_host(host);
let fut = tokio::time::timeout(crate::constants::DNS_LOOKUP_TIMEOUT, fut);

match fut.await {
Ok(Ok(ips)) => ips.collect(),
Ok(Err(e)) => {
tracing::info!(?host, ?e, "DNS error resolving peer IP address");
HashSet::new()
}
Err(e) => {
tracing::info!(?host, ?e, "DNS timeout resolving peer IP address");
HashSet::new()
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions zebra-network/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ lazy_static! {
}.expect("regex is valid");
}

/// The timeout for DNS lookups.
///
/// [6.1.3.3 Efficient Resource Usage] from [RFC 1123: Requirements for Internet Hosts]
/// suggest no less than 5 seconds for resolving timeout.
///
/// [RFC 1123: Requirements for Internet Hosts] https://tools.ietf.org/rfcmarkup?doc=1123
/// [6.1.3.3 Efficient Resource Usage] https://tools.ietf.org/rfcmarkup?doc=1123#page-77
pub const DNS_LOOKUP_TIMEOUT: Duration = Duration::from_secs(5);

/// Magic numbers used to identify different Zcash networks.
pub mod magics {
use super::*;
Expand Down
15 changes: 9 additions & 6 deletions zebra-network/src/peer_set/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,19 @@ where

let listen_guard = tokio::spawn(listen(config.listen_addr, listener, peerset_tx.clone()));

// 2. Initial peers, specified in the config.
let initial_peers_fut = {
let initial_peers = config.initial_peers();
let config = config.clone();
let connector = connector.clone();
let tx = peerset_tx.clone();

// Connect the tx end to the 3 peer sources:
add_initial_peers(initial_peers, connector, tx)
let peerset_tx = peerset_tx.clone();
async move {
let initial_peers = config.initial_peers().await;
// Connect the tx end to the 3 peer sources:
add_initial_peers(initial_peers, connector, peerset_tx).await
}
.boxed()
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
};

// 2. Initial peers, specified in the config.
let add_guard = tokio::spawn(initial_peers_fut);

// 3. Outgoing peers we connect to in response to load.
Expand Down