Builder update

This commit is contained in:
Age Manning
2020-05-08 15:06:52 +10:00
parent 167530e3f4
commit c4d5af81df
6 changed files with 171 additions and 230 deletions

View File

@@ -285,8 +285,8 @@ impl<T: EthSpec, S: Store<T>> CachingEth1Backend<T, S> {
} }
/// Starts the routine which connects to the external eth1 node and updates the caches. /// Starts the routine which connects to the external eth1 node and updates the caches.
pub async fn start(&self, exit: tokio::sync::oneshot::Receiver<()>) -> Result<(), ()> { pub async fn start(&self, exit: tokio::sync::oneshot::Receiver<()>) {
self.core.auto_update(exit).await tokio::spawn(async move { self.core.auto_update(exit).await });
} }
/// Instantiates `self` from an existing service. /// Instantiates `self` from an existing service.

View File

@@ -13,7 +13,6 @@ use environment::RuntimeContext;
use eth1::{Config as Eth1Config, Service as Eth1Service}; use eth1::{Config as Eth1Config, Service as Eth1Service};
use eth2_config::Eth2Config; use eth2_config::Eth2Config;
use eth2_libp2p::NetworkGlobals; use eth2_libp2p::NetworkGlobals;
use futures::{future, Future, IntoFuture};
use genesis::{interop_genesis_state, Eth1GenesisService}; use genesis::{interop_genesis_state, Eth1GenesisService};
use network::{NetworkConfig, NetworkMessage, NetworkService}; use network::{NetworkConfig, NetworkMessage, NetworkService};
use slog::info; use slog::info;
@@ -109,136 +108,103 @@ where
/// Initializes the `BeaconChainBuilder`. The `build_beacon_chain` method will need to be /// Initializes the `BeaconChainBuilder`. The `build_beacon_chain` method will need to be
/// called later in order to actually instantiate the `BeaconChain`. /// called later in order to actually instantiate the `BeaconChain`.
pub fn beacon_chain_builder( pub async fn beacon_chain_builder(
mut self, mut self,
client_genesis: ClientGenesis, client_genesis: ClientGenesis,
config: ClientConfig, config: ClientConfig,
) -> impl Future<Item = Self, Error = String> { ) -> Result<Self, String> {
let store = self.store.clone(); let store = self
let store_migrator = self.store_migrator.take(); .store
let chain_spec = self.chain_spec.clone(); .ok_or_else(|| "beacon_chain_start_method requires a store".to_string())?;
let runtime_context = self.runtime_context.clone(); let store_migrator = self
let eth_spec_instance = self.eth_spec_instance.clone(); .store_migrator
let data_dir = config.data_dir.clone(); .ok_or_else(|| "beacon_chain_start_method requires a store migrator".to_string())?;
let disabled_forks = config.disabled_forks.clone(); let context = self
.runtime_context
.ok_or_else(|| "beacon_chain_start_method requires a runtime context".to_string())?
.service_context("beacon".into());
let spec = self
.chain_spec
.ok_or_else(|| "beacon_chain_start_method requires a chain spec".to_string())?;
future::ok(()) let builder = BeaconChainBuilder::new(self.eth_spec_instance)
.and_then(move |()| { .logger(context.log.clone())
let store = store .store(store)
.ok_or_else(|| "beacon_chain_start_method requires a store".to_string())?; .store_migrator(store_migrator)
let store_migrator = store_migrator.ok_or_else(|| { .data_dir(config.data_dir)
"beacon_chain_start_method requires a store migrator".to_string() .custom_spec(spec.clone())
})?; .disabled_forks(config.disabled_forks);
let context = runtime_context
.ok_or_else(|| {
"beacon_chain_start_method requires a runtime context".to_string()
})?
.service_context("beacon".into());
let spec = chain_spec
.ok_or_else(|| "beacon_chain_start_method requires a chain spec".to_string())?;
let builder = BeaconChainBuilder::new(eth_spec_instance) let chain_exists = builder
.logger(context.log.clone()) .store_contains_beacon_chain()
.store(store) .unwrap_or_else(|_| false);
.store_migrator(store_migrator)
.data_dir(data_dir)
.custom_spec(spec.clone())
.disabled_forks(disabled_forks);
Ok((builder, spec, context)) // If the client is expect to resume but there's no beacon chain in the database,
}) // use the `DepositContract` method. This scenario is quite common when the client
.and_then(move |(builder, spec, context)| { // is shutdown before finding genesis via eth1.
let chain_exists = builder //
.store_contains_beacon_chain() // Alternatively, if there's a beacon chain in the database then always resume
.unwrap_or_else(|_| false); // using it.
let client_genesis = if client_genesis == ClientGenesis::FromStore && !chain_exists {
info!(context.log, "Defaulting to deposit contract genesis");
// If the client is expect to resume but there's no beacon chain in the database, ClientGenesis::DepositContract
// use the `DepositContract` method. This scenario is quite common when the client } else if chain_exists {
// is shutdown before finding genesis via eth1. ClientGenesis::FromStore
// } else {
// Alternatively, if there's a beacon chain in the database then always resume client_genesis
// using it. };
let client_genesis = if client_genesis == ClientGenesis::FromStore && !chain_exists
{
info!(context.log, "Defaulting to deposit contract genesis");
ClientGenesis::DepositContract let (beacon_chain_builder, eth1_service_option) = match client_genesis {
} else if chain_exists { ClientGenesis::Interop {
ClientGenesis::FromStore validator_count,
} else { genesis_time,
client_genesis } => {
}; let keypairs = generate_deterministic_keypairs(validator_count);
let genesis_state = interop_genesis_state(&keypairs, genesis_time, &spec)?;
builder.genesis_state(genesis_state).map(|v| (v, None))?
}
ClientGenesis::SszBytes {
genesis_state_bytes,
} => {
info!(
context.log,
"Starting from known genesis state";
);
let genesis_state_future: Box<dyn Future<Item = _, Error = _> + Send> = let genesis_state = BeaconState::from_ssz_bytes(&genesis_state_bytes)
match client_genesis { .map_err(|e| format!("Unable to parse genesis state SSZ: {:?}", e))?;
ClientGenesis::Interop {
validator_count,
genesis_time,
} => {
let keypairs = generate_deterministic_keypairs(validator_count);
let result = interop_genesis_state(&keypairs, genesis_time, &spec);
let future = result builder.genesis_state(genesis_state).map(|v| (v, None))?
.and_then(move |genesis_state| builder.genesis_state(genesis_state)) }
.into_future() ClientGenesis::DepositContract => {
.map(|v| (v, None)); info!(
context.log,
"Waiting for eth2 genesis from eth1";
"eth1_endpoint" => &config.eth1.endpoint,
"contract_deploy_block" => config.eth1.deposit_contract_deploy_block,
"deposit_contract" => &config.eth1.deposit_contract_address
);
Box::new(future) let genesis_service = Eth1GenesisService::new(config.eth1, context.log.clone());
}
ClientGenesis::SszBytes {
genesis_state_bytes,
} => {
info!(
context.log,
"Starting from known genesis state";
);
let result = BeaconState::from_ssz_bytes(&genesis_state_bytes) let genesis_state = genesis_service
.map_err(|e| format!("Unable to parse genesis state SSZ: {:?}", e)); .wait_for_genesis_state(
Duration::from_millis(ETH1_GENESIS_UPDATE_INTERVAL_MILLIS),
context.eth2_config().spec.clone(),
)
.await?;
let future = result builder
.and_then(move |genesis_state| builder.genesis_state(genesis_state)) .genesis_state(genesis_state)
.into_future() .map(|v| (v, Some(genesis_service.into_core_service())))?
.map(|v| (v, None)); }
ClientGenesis::FromStore => builder.resume_from_db().map(|v| (v, None))?,
};
Box::new(future) self.eth1_service = eth1_service_option;
} self.beacon_chain_builder = Some(beacon_chain_builder);
ClientGenesis::DepositContract => { Ok(self)
info!(
context.log,
"Waiting for eth2 genesis from eth1";
"eth1_endpoint" => &config.eth1.endpoint,
"contract_deploy_block" => config.eth1.deposit_contract_deploy_block,
"deposit_contract" => &config.eth1.deposit_contract_address
);
let genesis_service =
Eth1GenesisService::new(config.eth1, context.log.clone());
let future = genesis_service
.wait_for_genesis_state(
Duration::from_millis(ETH1_GENESIS_UPDATE_INTERVAL_MILLIS),
context.eth2_config().spec.clone(),
)
.and_then(move |genesis_state| builder.genesis_state(genesis_state))
.map(|v| (v, Some(genesis_service.into_core_service())));
Box::new(future)
}
ClientGenesis::FromStore => {
let future = builder.resume_from_db().into_future().map(|v| (v, None));
Box::new(future)
}
};
genesis_state_future
})
.map(move |(beacon_chain_builder, eth1_service_option)| {
self.eth1_service = eth1_service_option;
self.beacon_chain_builder = Some(beacon_chain_builder);
self
})
} }
/// Immediately starts the networking stack. /// Immediately starts the networking stack.
@@ -254,7 +220,7 @@ where
.service_context("network".into()); .service_context("network".into());
let (network_globals, network_send, network_exit) = let (network_globals, network_send, network_exit) =
NetworkService::start(beacon_chain, config, &context.executor, context.log) NetworkService::start(beacon_chain, config, &context.runtime_handle, context.log)
.map_err(|e| format!("Failed to start network: {:?}", e))?; .map_err(|e| format!("Failed to start network: {:?}", e))?;
self.network_globals = Some(network_globals); self.network_globals = Some(network_globals);
@@ -281,13 +247,10 @@ where
.ok_or_else(|| "node timer requires a chain spec".to_string())? .ok_or_else(|| "node timer requires a chain spec".to_string())?
.milliseconds_per_slot; .milliseconds_per_slot;
let timer_exit = timer::spawn( let timer_exit = context
&context.executor, .runtime_handle
beacon_chain, .enter(|| timer::spawn(beacon_chain, milliseconds_per_slot))
milliseconds_per_slot, .map_err(|e| format!("Unable to start node timer: {}", e))?;
context.log,
)
.map_err(|e| format!("Unable to start node timer: {}", e))?;
self.exit_channels.push(timer_exit); self.exit_channels.push(timer_exit);
@@ -323,21 +286,22 @@ where
network_chan: network_send, network_chan: network_send,
}; };
let (exit_channel, listening_addr) = rest_api::start_server( let (exit_channel, listening_addr) = context.runtime_handle.enter(|| {
&client_config.rest_api, rest_api::start_server(
&context.executor, &client_config.rest_api,
beacon_chain, beacon_chain,
network_info, network_info,
client_config client_config
.create_db_path() .create_db_path()
.map_err(|_| "unable to read data dir")?, .map_err(|_| "unable to read data dir")?,
client_config client_config
.create_freezer_db_path() .create_freezer_db_path()
.map_err(|_| "unable to read freezer DB dir")?, .map_err(|_| "unable to read freezer DB dir")?,
eth2_config.clone(), eth2_config.clone(),
context.log, context.log,
) )
.map_err(|e| format!("Failed to start HTTP API: {:?}", e))?; .map_err(|e| format!("Failed to start HTTP API: {:?}", e))
})?;
self.exit_channels.push(exit_channel); self.exit_channels.push(exit_channel);
self.http_listen_addr = Some(listening_addr); self.http_listen_addr = Some(listening_addr);
@@ -472,8 +436,9 @@ where
Option<_>, Option<_>,
Option<_>, Option<_>,
) = if config.enabled { ) = if config.enabled {
let (sender, exit, listening_addr) = let (sender, exit, listening_addr) = context
websocket_server::start_server(&config, &context.executor, &context.log)?; .runtime_handle
.enter(|| websocket_server::start_server(&config, &context.log))?;
(sender, Some(exit), Some(listening_addr)) (sender, Some(exit), Some(listening_addr))
} else { } else {
(WebSocketSender::dummy(), None, None) (WebSocketSender::dummy(), None, None)
@@ -692,7 +657,7 @@ where
}; };
// Starts the service that connects to an eth1 node and periodically updates caches. // Starts the service that connects to an eth1 node and periodically updates caches.
context.executor.spawn(backend.start(exit)); context.runtime_handle.spawn(backend.start(exit));
self.beacon_chain_builder = Some(beacon_chain_builder.eth1_backend(Some(backend))); self.beacon_chain_builder = Some(beacon_chain_builder.eth1_backend(Some(backend)));

View File

@@ -246,54 +246,39 @@ impl Service {
pub async fn update( pub async fn update(
&self, &self,
) -> Result<(DepositCacheUpdateOutcome, BlockCacheUpdateOutcome), String> { ) -> Result<(DepositCacheUpdateOutcome, BlockCacheUpdateOutcome), String> {
let deposit_future = self let update_deposit_cache = async {
.update_deposit_cache() let outcome = self
.map_err(|e| format!("Failed to update eth1 cache: {:?}", e)) .update_deposit_cache()
.then(|result| async move{ .await
match &result { .map_err(|e| format!("Failed to update eth1 cache: {:?}", e))?;
Ok(DepositCacheUpdateOutcome { logs_imported }) => trace!(
self.log,
"Updated eth1 deposit cache";
"cached_deposits" => self.inner.deposit_cache.read().cache.len(),
"logs_imported" => logs_imported,
"last_processed_eth1_block" => self.inner.deposit_cache.read().last_processed_block,
),
Err(e) => error!(
self.log,
"Failed to update eth1 deposit cache";
"error" => e
),
};
result trace!(
}); self.log,
"Updated eth1 deposit cache";
"cached_deposits" => self.inner.deposit_cache.read().cache.len(),
"logs_imported" => outcome.logs_imported,
"last_processed_eth1_block" => self.inner.deposit_cache.read().last_processed_block,
);
Ok(outcome)
};
let block_future = self let update_block_cache = async {
.update_block_cache() let outcome = self
.map_err(|e| format!("Failed to update eth1 cache: {:?}", e)) .update_block_cache()
.then(|result| async move { .await
match &result { .map_err(|e| format!("Failed to update eth1 cache: {:?}", e))?;
Ok(BlockCacheUpdateOutcome {
blocks_imported,
head_block_number,
}) => trace!(
self.log,
"Updated eth1 block cache";
"cached_blocks" => self.inner.block_cache.read().len(),
"blocks_imported" => blocks_imported,
"head_block" => head_block_number,
),
Err(e) => error!(
self.log,
"Failed to update eth1 block cache";
"error" => e
),
};
result trace!(
}); self.log,
"Updated eth1 block cache";
"cached_blocks" => self.inner.block_cache.read().len(),
"blocks_imported" => outcome.blocks_imported,
"head_block" => outcome.head_block_number,
);
Ok(outcome)
};
futures::try_join!(deposit_future, block_future) futures::try_join!(update_deposit_cache, update_block_cache)
} }
/// A looping future that updates the cache, then waits `config.auto_update_interval` before /// A looping future that updates the cache, then waits `config.auto_update_interval` before
@@ -305,40 +290,40 @@ impl Service {
/// - Err(_) if there is an error. /// - Err(_) if there is an error.
/// ///
/// Emits logs for debugging and errors. /// Emits logs for debugging and errors.
pub async fn auto_update( pub async fn auto_update(&self, exit: tokio::sync::oneshot::Receiver<()>) {
&self,
mut exit: tokio::sync::oneshot::Receiver<()>,
) -> Result<(), ()> {
let update_interval = Duration::from_millis(self.config().auto_update_interval_millis); let update_interval = Duration::from_millis(self.config().auto_update_interval_millis);
loop { loop {
let update_result = self.update().await; let update_future = async move {
/*
let update_result = self.update().await;
match update_result { match update_result {
Err(e) => error!( Err(e) => error!(
self.log, self.log,
"Failed to update eth1 cache"; "Failed to update eth1 cache";
"retry_millis" => update_interval.as_millis(), "retry_millis" => update_interval.as_millis(),
"error" => e, "error" => e,
), ),
Ok((deposit, block)) => debug!( Ok((deposit, block)) => debug!(
self.log, self.log,
"Updated eth1 cache"; "Updated eth1 cache";
"retry_millis" => update_interval.as_millis(), "retry_millis" => update_interval.as_millis(),
"blocks" => format!("{:?}", block), "blocks" => format!("{:?}", block),
"deposits" => format!("{:?}", deposit), "deposits" => format!("{:?}", deposit),
), ),
};
*/
// WARNING: delay_for doesn't return an error and panics on error.
delay_for(update_interval).await;
}; };
if let futures::future::Either::Right(_) =
// WARNING: delay_for doesn't return an error and panics on error. futures::future::select(Box::pin(update_future), futures::future::ready(())).await
delay_for(update_interval).await; {
match exit.try_recv() { // the exit future returned end
Ok(_) | Err(TryRecvError::Closed) => break, break;
Err(TryRecvError::Empty) => {}
} }
} }
Ok(())
} }
/// Contacts the remote eth1 node and attempts to import deposit logs up to the configured /// Contacts the remote eth1 node and attempts to import deposit logs up to the configured

View File

@@ -35,7 +35,6 @@ use std::net::SocketAddr;
use std::ops::Deref; use std::ops::Deref;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use tokio::runtime::Handle;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use url_query::UrlQuery; use url_query::UrlQuery;
@@ -53,7 +52,6 @@ pub struct NetworkInfo<T: BeaconChainTypes> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn start_server<T: BeaconChainTypes>( pub fn start_server<T: BeaconChainTypes>(
config: &Config, config: &Config,
handle: &Handle,
beacon_chain: Arc<BeaconChain<T>>, beacon_chain: Arc<BeaconChain<T>>,
network_info: NetworkInfo<T>, network_info: NetworkInfo<T>,
db_path: PathBuf, db_path: PathBuf,
@@ -130,7 +128,7 @@ pub fn start_server<T: BeaconChainTypes>(
"port" => actual_listen_addr.port(), "port" => actual_listen_addr.port(),
); );
handle.spawn(server_future); tokio::spawn(server_future);
Ok((exit_signal, actual_listen_addr)) Ok((exit_signal, actual_listen_addr))
} }

View File

@@ -3,12 +3,11 @@
//! This service allows task execution on the beacon node for various functionality. //! This service allows task execution on the beacon node for various functionality.
use beacon_chain::{BeaconChain, BeaconChainTypes}; use beacon_chain::{BeaconChain, BeaconChainTypes};
use futures::future;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use futures::{future, prelude::*};
use slot_clock::SlotClock; use slot_clock::SlotClock;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tokio::runtime::Handle;
use tokio::time::{interval_at, Instant}; use tokio::time::{interval_at, Instant};
/// Spawns a timer service which periodically executes tasks for the beacon chain /// Spawns a timer service which periodically executes tasks for the beacon chain
@@ -16,7 +15,6 @@ use tokio::time::{interval_at, Instant};
/// called from the context of a runtime and we can simply spawn using task::spawn. /// called from the context of a runtime and we can simply spawn using task::spawn.
/// Check for issues without the Handle. /// Check for issues without the Handle.
pub fn spawn<T: BeaconChainTypes>( pub fn spawn<T: BeaconChainTypes>(
handle: &Handle,
beacon_chain: Arc<BeaconChain<T>>, beacon_chain: Arc<BeaconChain<T>>,
milliseconds_per_slot: u64, milliseconds_per_slot: u64,
) -> Result<tokio::sync::oneshot::Sender<()>, &'static str> { ) -> Result<tokio::sync::oneshot::Sender<()>, &'static str> {
@@ -35,8 +33,8 @@ pub fn spawn<T: BeaconChainTypes>(
future::ready(()) future::ready(())
}); });
let future = futures::future::select(timer_future, exit.map_err(|_| ()).map(|_| ())); let future = futures::future::select(timer_future, exit);
handle.spawn(future); tokio::spawn(future);
Ok(exit_signal) Ok(exit_signal)
} }

View File

@@ -2,7 +2,6 @@ use futures::future::TryFutureExt;
use slog::{debug, error, info, warn, Logger}; use slog::{debug, error, info, warn, Logger};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::net::SocketAddr; use std::net::SocketAddr;
use tokio::runtime::Handle;
use types::EthSpec; use types::EthSpec;
use ws::{Sender, WebSocket}; use ws::{Sender, WebSocket};
@@ -37,7 +36,6 @@ impl<T: EthSpec> WebSocketSender<T> {
pub fn start_server<T: EthSpec>( pub fn start_server<T: EthSpec>(
config: &Config, config: &Config,
handle: &Handle,
log: &Logger, log: &Logger,
) -> Result< ) -> Result<
( (
@@ -92,15 +90,12 @@ pub fn start_server<T: EthSpec>(
// Place a future on the handle that will shutdown the websocket server when the // Place a future on the handle that will shutdown the websocket server when the
// application exits. // application exits.
// TODO: check if we should spawn using a `Handle` or using `task::spawn` tokio::spawn(exit_future);
handle.spawn(exit_future);
exit_channel exit_channel
}; };
let log_inner = log.clone(); let log_inner = log.clone();
// TODO: using tokio `spawn_blocking` instead of `thread::spawn`
// Check which is more apt.
let _handle = tokio::task::spawn_blocking(move || match server.run() { let _handle = tokio::task::spawn_blocking(move || match server.run() {
Ok(_) => { Ok(_) => {
debug!( debug!(