Make bootstrapper block til connection established
This commit is contained in:
parent
009a7eb9c7
commit
572df4f37e
@ -87,7 +87,7 @@ impl<T: BeaconChainTypes> BeaconChainBuilder<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn http_bootstrap(server: &str, spec: ChainSpec, log: Logger) -> Result<Self, String> {
|
pub fn http_bootstrap(server: &str, spec: ChainSpec, log: Logger) -> Result<Self, String> {
|
||||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())
|
let bootstrapper = Bootstrapper::connect(server.to_string(), &log)
|
||||||
.map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?;
|
.map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?;
|
||||||
|
|
||||||
let (genesis_state, genesis_block) = bootstrapper
|
let (genesis_state, genesis_block) = bootstrapper
|
||||||
|
@ -310,7 +310,7 @@ impl<'a> ConfigBuilder<'a> {
|
|||||||
server: &str,
|
server: &str,
|
||||||
port: Option<u16>,
|
port: Option<u16>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())?;
|
let bootstrapper = Bootstrapper::connect(server.to_string(), &self.log)?;
|
||||||
|
|
||||||
if let Some(server_multiaddr) = bootstrapper.best_effort_multiaddr(port) {
|
if let Some(server_multiaddr) = bootstrapper.best_effort_multiaddr(port) {
|
||||||
info!(
|
info!(
|
||||||
@ -347,7 +347,7 @@ impl<'a> ConfigBuilder<'a> {
|
|||||||
|
|
||||||
/// Imports an `Eth2Config` from `server`, returning an error if this fails.
|
/// Imports an `Eth2Config` from `server`, returning an error if this fails.
|
||||||
pub fn import_bootstrap_eth2_config(&mut self, server: &str) -> Result<()> {
|
pub fn import_bootstrap_eth2_config(&mut self, server: &str) -> Result<()> {
|
||||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())?;
|
let bootstrapper = Bootstrapper::connect(server.to_string(), &self.log)?;
|
||||||
|
|
||||||
self.update_eth2_config(bootstrapper.eth2_config()?);
|
self.update_eth2_config(bootstrapper.eth2_config()?);
|
||||||
|
|
||||||
|
@ -13,3 +13,4 @@ reqwest = "0.9"
|
|||||||
url = "1.2"
|
url = "1.2"
|
||||||
types = { path = "../../types" }
|
types = { path = "../../types" }
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
|
slog = { version = "^2.2.3" , features = ["max_level_trace", "release_max_level_trace"] }
|
||||||
|
@ -5,11 +5,16 @@ use eth2_libp2p::{
|
|||||||
};
|
};
|
||||||
use reqwest::{Error as HttpError, Url};
|
use reqwest::{Error as HttpError, Url};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use slog::{error, Logger};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
|
use std::time::Duration;
|
||||||
use types::{BeaconBlock, BeaconState, Checkpoint, EthSpec, Hash256, Slot};
|
use types::{BeaconBlock, BeaconState, Checkpoint, EthSpec, Hash256, Slot};
|
||||||
use url::Host;
|
use url::Host;
|
||||||
|
|
||||||
|
pub const RETRY_SLEEP_MILLIS: u64 = 100;
|
||||||
|
pub const RETRY_WARN_INTERVAL: u64 = 30;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Error {
|
enum Error {
|
||||||
InvalidUrl,
|
InvalidUrl,
|
||||||
@ -31,11 +36,35 @@ pub struct Bootstrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Bootstrapper {
|
impl Bootstrapper {
|
||||||
/// Parses the given `server` as a URL, instantiating `Self`.
|
/// Parses the given `server` as a URL, instantiating `Self` and blocking until a connection
|
||||||
pub fn from_server_string(server: String) -> Result<Self, String> {
|
/// can be made with the server.
|
||||||
Ok(Self {
|
///
|
||||||
|
/// Never times out.
|
||||||
|
pub fn connect(server: String, log: &Logger) -> Result<Self, String> {
|
||||||
|
let bootstrapper = Self {
|
||||||
url: Url::parse(&server).map_err(|e| format!("Invalid bootstrap server url: {}", e))?,
|
url: Url::parse(&server).map_err(|e| format!("Invalid bootstrap server url: {}", e))?,
|
||||||
})
|
};
|
||||||
|
|
||||||
|
let mut retry_count = 0;
|
||||||
|
loop {
|
||||||
|
match bootstrapper.enr() {
|
||||||
|
Ok(_) => break,
|
||||||
|
Err(_) => {
|
||||||
|
if retry_count % RETRY_WARN_INTERVAL == 0 {
|
||||||
|
error!(
|
||||||
|
log,
|
||||||
|
"Failed to contact bootstrap server";
|
||||||
|
"retry_count" => retry_count,
|
||||||
|
"retry_delay_millis" => RETRY_SLEEP_MILLIS,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
retry_count += 1;
|
||||||
|
std::thread::sleep(Duration::from_millis(RETRY_SLEEP_MILLIS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(bootstrapper)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build a multiaddr using the HTTP server URL that is not guaranteed to be correct.
|
/// Build a multiaddr using the HTTP server URL that is not guaranteed to be correct.
|
||||||
|
@ -247,10 +247,13 @@ fn process_testnet_subcommand(
|
|||||||
) -> Result<(ClientConfig, Eth2Config)> {
|
) -> Result<(ClientConfig, Eth2Config)> {
|
||||||
let eth2_config = if cli_args.is_present("bootstrap") {
|
let eth2_config = if cli_args.is_present("bootstrap") {
|
||||||
info!(log, "Connecting to bootstrap server");
|
info!(log, "Connecting to bootstrap server");
|
||||||
let bootstrapper = Bootstrapper::from_server_string(format!(
|
let bootstrapper = Bootstrapper::connect(
|
||||||
"http://{}:{}",
|
format!(
|
||||||
client_config.server, client_config.server_http_port
|
"http://{}:{}",
|
||||||
))?;
|
client_config.server, client_config.server_http_port
|
||||||
|
),
|
||||||
|
&log,
|
||||||
|
)?;
|
||||||
|
|
||||||
let eth2_config = bootstrapper.eth2_config()?;
|
let eth2_config = bootstrapper.eth2_config()?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user