Adds beacon chain events, websocket event handler

This commit is contained in:
Paul Hauner
2019-09-14 15:41:35 -04:00
parent 110e627d7b
commit 07990e0e92
11 changed files with 262 additions and 52 deletions

View File

@@ -20,18 +20,19 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::runtime::TaskExecutor;
use tokio::timer::Interval;
use types::EthSpec;
use websocket_server::WebSocketSender;
pub use beacon_chain::{BeaconChainTypes, Eth1ChainBackend, InteropEth1ChainBackend};
pub use config::{BeaconChainStartMethod, Config as ClientConfig, Eth1BackendMethod};
pub use eth2_config::Eth2Config;
#[derive(Clone)]
pub struct ClientType<S: Store, E: EthSpec> {
pub struct RuntimeBeaconChainTypes<S: Store, E: EthSpec> {
_phantom_s: PhantomData<S>,
_phantom_e: PhantomData<E>,
}
impl<S, E> BeaconChainTypes for ClientType<S, E>
impl<S, E> BeaconChainTypes for RuntimeBeaconChainTypes<S, E>
where
S: Store + 'static,
E: EthSpec,
@@ -41,17 +42,22 @@ where
type LmdGhost = ThreadSafeReducedTree<S, E>;
type Eth1Chain = InteropEth1ChainBackend<E>;
type EthSpec = E;
type EventHandler = WebSocketSender<E>;
}
/// Main beacon node client service. This provides the connection and initialisation of the clients
/// sub-services in multiple threads.
pub struct Client<T: BeaconChainTypes> {
pub struct Client<S, E>
where
S: Store + Clone + 'static,
E: EthSpec,
{
/// Configuration for the lighthouse client.
_client_config: ClientConfig,
/// The beacon chain for the running client.
beacon_chain: Arc<BeaconChain<T>>,
beacon_chain: Arc<BeaconChain<RuntimeBeaconChainTypes<S, E>>>,
/// Reference to the network service.
pub network: Arc<NetworkService<T>>,
pub network: Arc<NetworkService<RuntimeBeaconChainTypes<S, E>>>,
/// Signal to terminate the RPC server.
pub rpc_exit_signal: Option<Signal>,
/// Signal to terminate the slot timer.
@@ -60,19 +66,22 @@ pub struct Client<T: BeaconChainTypes> {
pub api_exit_signal: Option<Signal>,
/// The clients logger.
log: slog::Logger,
/*
/// Marker to pin the beacon chain generics.
phantom: PhantomData<T>,
phantom: PhantomData<BeaconChainTypes>,
*/
}
impl<T> Client<T>
impl<S, E> Client<S, E>
where
T: BeaconChainTypes + Clone,
S: Store + Clone + 'static,
E: EthSpec,
{
/// Generate an instance of the client. Spawn and link all internal sub-processes.
pub fn new(
client_config: ClientConfig,
eth2_config: Eth2Config,
store: T::Store,
store: S,
log: slog::Logger,
executor: &TaskExecutor,
) -> error::Result<Self> {
@@ -169,11 +178,19 @@ where
}
};
let eth1_backend = T::Eth1Chain::new(String::new()).map_err(|e| format!("{:?}", e))?;
let eth1_backend =
InteropEth1ChainBackend::new(String::new()).map_err(|e| format!("{:?}", e))?;
let beacon_chain: Arc<BeaconChain<T>> = Arc::new(
// Start the websocket server.
let websocket_sender: WebSocketSender<E> = if client_config.websocket_server.enabled {
websocket_server::start_server(&client_config.websocket_server, &log)?
} else {
WebSocketSender::dummy()
};
let beacon_chain: Arc<BeaconChain<RuntimeBeaconChainTypes<S, E>>> = Arc::new(
beacon_chain_builder
.build(store, eth1_backend)
.build(store, eth1_backend, websocket_sender)
.map_err(error::Error::from)?,
);
@@ -229,11 +246,6 @@ where
None
};
// Start the websocket server
let _websocket_sender = if client_config.websocket_server.enabled {
websocket_server::start_server::<T::EthSpec>(&client_config.websocket_server, &log)?;
};
let (slot_timer_exit_signal, exit) = exit_future::signal();
if let Some(duration_to_next_slot) = beacon_chain.slot_clock.duration_to_next_slot() {
// set up the validator work interval - start at next slot and proceed every slot
@@ -268,12 +280,11 @@ where
api_exit_signal,
log,
network,
phantom: PhantomData,
})
}
}
impl<T: BeaconChainTypes> Drop for Client<T> {
impl<S: Store + Clone, E: EthSpec> Drop for Client<S, E> {
fn drop(&mut self) {
// Save the beacon chain to it's store before dropping.
let _result = self.beacon_chain.persist();

View File

@@ -1,11 +1,12 @@
use crate::Client;
use beacon_chain::BeaconChainTypes;
use exit_future::Exit;
use futures::{Future, Stream};
use slog::{debug, o, warn};
use std::time::{Duration, Instant};
use store::Store;
use tokio::runtime::TaskExecutor;
use tokio::timer::Interval;
use types::EthSpec;
/// The interval between heartbeat events.
pub const HEARTBEAT_INTERVAL_SECONDS: u64 = 15;
@@ -17,7 +18,11 @@ pub const WARN_PEER_COUNT: usize = 1;
/// durations.
///
/// Presently unused, but remains for future use.
pub fn run<T: BeaconChainTypes>(client: &Client<T>, executor: TaskExecutor, exit: Exit) {
pub fn run<S, E>(client: &Client<S, E>, executor: TaskExecutor, exit: Exit)
where
S: Store + Clone + 'static,
E: EthSpec,
{
// notification heartbeat
let interval = Interval::new(
Instant::now(),