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 5 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: 44 additions & 15 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,53 @@ 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
.iter()
.flat_map(|s| s.to_socket_addrs())
.flatten()
.collect()
async fn parse_peers(peers: HashSet<String>) -> HashSet<SocketAddr> {
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
use futures::stream::StreamExt;
// See https://docs.rs/futures/0.3.12/futures/stream/trait.StreamExt.html
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
let peer_addresses = peers
.clone()
.into_iter()
.map(Config::resolve_host)
.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.clone()).await,
Network::Testnet => Config::parse_peers(self.initial_testnet_peers.clone()).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: String) -> 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 [Requirements for Internet Hosts]
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
/// suggest no less than 5 seconds for resolving timeout.
///
/// [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
16 changes: 10 additions & 6 deletions zebra-network/src/peer_set/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,18 @@ where

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

let initial_peers_fut = {
let initial_peers = config.initial_peers();
let connector = connector.clone();
let tx = peerset_tx.clone();
let connector = connector.clone();
let tx = peerset_tx.clone();

// Clone some values so we can move them
let config_clone = config.clone();
let connector_clone = connector.clone();

let initial_peers_fut = async move {
// Connect the tx end to the 3 peer sources:
add_initial_peers(initial_peers, connector, tx)
};
add_initial_peers(config_clone.initial_peers().await, connector_clone, tx).await
}
.boxed();

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