Update bootstrapper libp2p address finding

This commit is contained in:
Paul Hauner 2019-08-15 18:48:39 +10:00
parent ce37f95861
commit 7cd963e6bb
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
4 changed files with 46 additions and 23 deletions

View File

@ -1,5 +1,9 @@
use eth2_libp2p::{Enr, Multiaddr};
use eth2_libp2p::{
multiaddr::{Multiaddr, Protocol},
Enr,
};
use reqwest::{Error as HttpError, Url};
use std::borrow::Cow;
use std::net::Ipv4Addr;
use types::{BeaconBlock, BeaconState, Checkpoint, EthSpec, Slot};
use url::Host;
@ -27,6 +31,31 @@ impl Bootstrapper {
})
}
pub fn best_effort_multiaddr(&self) -> Option<Multiaddr> {
let tcp_port = self.first_listening_tcp_port()?;
let mut multiaddr = Multiaddr::with_capacity(2);
match self.url.host()? {
Host::Ipv4(addr) => multiaddr.push(Protocol::Ip4(addr)),
Host::Domain(s) => multiaddr.push(Protocol::Dns4(Cow::Borrowed(s))),
_ => return None,
};
multiaddr.push(Protocol::Tcp(tcp_port));
Some(multiaddr)
}
fn first_listening_tcp_port(&self) -> Option<u16> {
self.listen_addresses().ok()?.iter().find_map(|multiaddr| {
multiaddr.iter().find_map(|protocol| match protocol {
Protocol::Tcp(port) => Some(port),
_ => None,
})
})
}
pub fn server_ipv4_addr(&self) -> Option<Ipv4Addr> {
match self.url.host()? {
Host::Ipv4(addr) => Some(addr),

View File

@ -1,6 +1,5 @@
use crate::{Bootstrapper, Eth2Config};
use clap::ArgMatches;
use eth2_libp2p::multiaddr::{Multiaddr, Protocol};
use network::NetworkConfig;
use serde_derive::{Deserialize, Serialize};
use slog::{info, o, warn, Drain};
@ -169,23 +168,18 @@ fn do_bootstrapping(config: &mut Config, server: String, log: &slog::Logger) ->
config.network.boot_nodes.push(bootstrapper.enr()?);
if let Some(server_ip) = bootstrapper.server_ipv4_addr() {
let server_multiaddr: Multiaddr = bootstrapper
.listen_addresses()?
.first()
.ok_or_else(|| "Bootstrap peer returned an empty list of listen addresses")?
// Iterate through the components of the Multiaddr, replacing any Ipv4 address with the
// server address.
.iter()
.map(|protocol| match protocol {
Protocol::Ip4(_) => Protocol::Ip4(server_ip),
_ => protocol,
})
.collect::<Multiaddr>();
if let Some(server_multiaddr) = bootstrapper.best_effort_multiaddr() {
info!(
log,
"Estimated bootstrapper libp2p address";
"multiaddr" => format!("{:?}", server_multiaddr)
);
config.network.libp2p_nodes.push(server_multiaddr);
} else {
warn!(log, "Unable to determine bootstrap server Ipv4 address. Unable to add server as libp2p peer.");
warn!(
log,
"Unable to estimate a bootstrapper libp2p address, this node may not find any peers."
);
}
Ok(())

View File

@ -237,6 +237,11 @@ fn main() {
let mut log = slog::Logger::root(drain.fuse(), o!());
warn!(
log,
"Ethereum 2.0 is pre-release. This software is experimental."
);
let data_dir = match matches
.value_of("datadir")
.and_then(|v| Some(PathBuf::from(v)))

View File

@ -4,7 +4,7 @@ use client::{
};
use futures::sync::oneshot;
use futures::Future;
use slog::{error, info, warn};
use slog::{error, info};
use std::cell::RefCell;
use std::path::Path;
use std::path::PathBuf;
@ -42,11 +42,6 @@ pub fn run_beacon_node(
let other_client_config = client_config.clone();
warn!(
log,
"Ethereum 2.0 is pre-release. This software is experimental."
);
info!(
log,
"BeaconNode init";