Get bootstrapper working for ENR address

This commit is contained in:
Paul Hauner 2019-08-15 13:58:04 +10:00
parent fda208b103
commit b244826749
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
4 changed files with 62 additions and 31 deletions

View File

@ -1,5 +1,5 @@
use crate::bootstrapper::Bootstrapper;
use crate::error::Result; use crate::error::Result;
use crate::local_bootstrap::BootstrapParams;
use crate::{config::GenesisState, ClientConfig}; use crate::{config::GenesisState, ClientConfig};
use beacon_chain::{ use beacon_chain::{
lmd_ghost::{LmdGhost, ThreadSafeReducedTree}, lmd_ghost::{LmdGhost, ThreadSafeReducedTree},
@ -7,7 +7,6 @@ use beacon_chain::{
store::Store, store::Store,
BeaconChain, BeaconChainTypes, BeaconChain, BeaconChainTypes,
}; };
use reqwest::Url;
use slog::{crit, info, Logger}; use slog::{crit, info, Logger};
use slot_clock::SlotClock; use slot_clock::SlotClock;
use std::fs::File; use std::fs::File;
@ -77,13 +76,14 @@ where
.map_err(|e| format!("Unable to parse YAML genesis state file: {:?}", e))? .map_err(|e| format!("Unable to parse YAML genesis state file: {:?}", e))?
} }
GenesisState::HttpBootstrap { server } => { GenesisState::HttpBootstrap { server } => {
let url: Url = let bootstrapper = Bootstrapper::from_server_string(server.to_string())
Url::parse(&server).map_err(|e| format!("Invalid bootstrap server url: {}", e))?; .map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?;
let params = BootstrapParams::from_http_api(url) let (state, _block) = bootstrapper
.map_err(|e| format!("Failed to bootstrap from HTTP server: {:?}", e))?; .genesis()
.map_err(|e| format!("Failed to bootstrap genesis state: {}", e))?;
params.genesis_state state
} }
}; };

View File

@ -14,33 +14,44 @@ impl From<HttpError> for Error {
} }
} }
pub struct BootstrapParams<T: EthSpec> { pub struct Bootstrapper {
pub finalized_block: BeaconBlock<T>, url: Url,
pub finalized_state: BeaconState<T>,
pub genesis_block: BeaconBlock<T>,
pub genesis_state: BeaconState<T>,
pub enr: Enr,
} }
impl<T: EthSpec> BootstrapParams<T> { impl Bootstrapper {
pub fn from_http_api(url: Url) -> Result<Self, String> { pub fn from_server_string(server: String) -> Result<Self, String> {
let slots_per_epoch = get_slots_per_epoch(url.clone()) Ok(Self {
.map_err(|e| format!("Unable to get slots per epoch: {:?}", e))?; url: Url::parse(&server).map_err(|e| format!("Invalid bootstrap server url: {}", e))?,
})
}
pub fn enr(&self) -> Result<Enr, String> {
get_enr(self.url.clone()).map_err(|e| format!("Unable to get ENR: {:?}", e))
}
pub fn genesis<T: EthSpec>(&self) -> Result<(BeaconState<T>, BeaconBlock<T>), String> {
let genesis_slot = Slot::new(0); let genesis_slot = Slot::new(0);
let finalized_slot = get_finalized_slot(url.clone(), slots_per_epoch.as_u64())
let block = get_block(self.url.clone(), genesis_slot)
.map_err(|e| format!("Unable to get genesis block: {:?}", e))?;
let state = get_state(self.url.clone(), genesis_slot)
.map_err(|e| format!("Unable to get genesis state: {:?}", e))?;
Ok((state, block))
}
pub fn finalized<T: EthSpec>(&self) -> Result<(BeaconState<T>, BeaconBlock<T>), String> {
let slots_per_epoch = get_slots_per_epoch(self.url.clone())
.map_err(|e| format!("Unable to get slots per epoch: {:?}", e))?;
let finalized_slot = get_finalized_slot(self.url.clone(), slots_per_epoch.as_u64())
.map_err(|e| format!("Unable to get finalized slot: {:?}", e))?; .map_err(|e| format!("Unable to get finalized slot: {:?}", e))?;
Ok(Self { let block = get_block(self.url.clone(), finalized_slot)
finalized_block: get_block(url.clone(), finalized_slot) .map_err(|e| format!("Unable to get finalized block: {:?}", e))?;
.map_err(|e| format!("Unable to get finalized block: {:?}", e))?, let state = get_state(self.url.clone(), finalized_slot)
finalized_state: get_state(url.clone(), finalized_slot) .map_err(|e| format!("Unable to get finalized state: {:?}", e))?;
.map_err(|e| format!("Unable to get finalized state: {:?}", e))?,
genesis_block: get_block(url.clone(), genesis_slot) Ok((state, block))
.map_err(|e| format!("Unable to get genesis block: {:?}", e))?,
genesis_state: get_state(url.clone(), genesis_slot)
.map_err(|e| format!("Unable to get genesis state: {:?}", e))?,
enr: get_enr(url.clone()).map_err(|e| format!("Unable to get ENR: {:?}", e))?,
})
} }
} }

View File

@ -1,8 +1,8 @@
extern crate slog; extern crate slog;
mod beacon_chain_types; mod beacon_chain_types;
mod bootstrapper;
mod config; mod config;
mod local_bootstrap;
pub mod error; pub mod error;
pub mod notifier; pub mod notifier;
@ -22,6 +22,7 @@ use tokio::timer::Interval;
pub use beacon_chain::BeaconChainTypes; pub use beacon_chain::BeaconChainTypes;
pub use beacon_chain_types::ClientType; pub use beacon_chain_types::ClientType;
pub use beacon_chain_types::InitialiseBeaconChain; pub use beacon_chain_types::InitialiseBeaconChain;
pub use bootstrapper::Bootstrapper;
pub use config::{Config as ClientConfig, GenesisState}; pub use config::{Config as ClientConfig, GenesisState};
pub use eth2_config::Eth2Config; pub use eth2_config::Eth2Config;

View File

@ -1,7 +1,7 @@
mod run; mod run;
use clap::{App, Arg}; use clap::{App, Arg};
use client::{ClientConfig, Eth2Config, GenesisState}; use client::{Bootstrapper, ClientConfig, Eth2Config, GenesisState};
use env_logger::{Builder, Env}; use env_logger::{Builder, Env};
use eth2_config::{read_from_file, write_to_file}; use eth2_config::{read_from_file, write_to_file};
use slog::{crit, o, warn, Drain, Level}; use slog::{crit, o, warn, Drain, Level};
@ -300,9 +300,28 @@ fn main() {
// If the `--bootstrap` flag is provided, overwrite the default configuration. // If the `--bootstrap` flag is provided, overwrite the default configuration.
if let Some(server) = matches.value_of("bootstrap") { if let Some(server) = matches.value_of("bootstrap") {
// Set the genesis state source.
client_config.genesis_state = GenesisState::HttpBootstrap { client_config.genesis_state = GenesisState::HttpBootstrap {
server: server.to_string(), server: server.to_string(),
}; };
let bootstrapper = match Bootstrapper::from_server_string(server.to_string()) {
Ok(b) => b,
Err(e) => {
crit!(log, "Failed to load bootstrapper"; "error" => format!("{:?}", e));
return;
}
};
let enr = match bootstrapper.enr() {
Ok(b) => b,
Err(e) => {
crit!(log, "Failed to read ENR from bootstrap server"; "error" => format!("{:?}", e));
return;
}
};
client_config.network.boot_nodes.push(enr);
} }
let eth2_config_path = data_dir.join(ETH2_CONFIG_FILENAME); let eth2_config_path = data_dir.join(ETH2_CONFIG_FILENAME);