2019-03-01 01:45:01 +00:00
|
|
|
extern crate slog;
|
|
|
|
|
|
|
|
mod client_config;
|
|
|
|
pub mod client_types;
|
|
|
|
pub mod error;
|
|
|
|
pub mod notifier;
|
|
|
|
|
2019-03-19 12:47:58 +00:00
|
|
|
use beacon_chain::BeaconChain;
|
2019-05-21 07:45:35 +00:00
|
|
|
pub use client_config::{ClientConfig, DBType};
|
2019-03-01 01:45:01 +00:00
|
|
|
pub use client_types::ClientTypes;
|
2019-03-19 11:53:51 +00:00
|
|
|
use exit_future::Signal;
|
2019-03-27 00:25:15 +00:00
|
|
|
use fork_choice::ForkChoice;
|
2019-03-26 23:36:20 +00:00
|
|
|
use futures::{future::Future, Stream};
|
2019-03-04 07:31:01 +00:00
|
|
|
use network::Service as NetworkService;
|
2019-03-26 23:36:20 +00:00
|
|
|
use slog::{error, info, o};
|
|
|
|
use slot_clock::SlotClock;
|
2019-03-01 01:45:01 +00:00
|
|
|
use std::marker::PhantomData;
|
2019-03-04 07:31:01 +00:00
|
|
|
use std::sync::Arc;
|
2019-03-26 23:36:20 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2019-05-21 08:20:23 +00:00
|
|
|
use store::Store;
|
2019-03-01 01:45:01 +00:00
|
|
|
use tokio::runtime::TaskExecutor;
|
2019-03-26 23:36:20 +00:00
|
|
|
use tokio::timer::Interval;
|
2019-05-10 04:47:09 +00:00
|
|
|
use types::EthSpec;
|
2019-03-01 01:45:01 +00:00
|
|
|
|
2019-05-09 03:35:00 +00:00
|
|
|
type ArcBeaconChain<D, S, F, B> = Arc<BeaconChain<D, S, F, B>>;
|
|
|
|
|
2019-03-04 05:39:37 +00:00
|
|
|
/// Main beacon node client service. This provides the connection and initialisation of the clients
|
|
|
|
/// sub-services in multiple threads.
|
2019-03-01 01:45:01 +00:00
|
|
|
pub struct Client<T: ClientTypes> {
|
2019-03-18 06:38:23 +00:00
|
|
|
/// Configuration for the lighthouse client.
|
2019-04-03 05:23:09 +00:00
|
|
|
_config: ClientConfig,
|
2019-03-18 06:38:23 +00:00
|
|
|
/// The beacon chain for the running client.
|
2019-05-10 04:47:09 +00:00
|
|
|
_beacon_chain: ArcBeaconChain<T::DB, T::SlotClock, T::ForkChoice, T::EthSpec>,
|
2019-03-18 06:38:23 +00:00
|
|
|
/// Reference to the network service.
|
2019-05-10 04:47:09 +00:00
|
|
|
pub network: Arc<NetworkService<T::EthSpec>>,
|
2019-03-22 05:46:52 +00:00
|
|
|
/// Signal to terminate the RPC server.
|
|
|
|
pub rpc_exit_signal: Option<Signal>,
|
2019-03-26 23:36:20 +00:00
|
|
|
/// Signal to terminate the slot timer.
|
|
|
|
pub slot_timer_exit_signal: Option<Signal>,
|
2019-03-18 06:38:23 +00:00
|
|
|
/// The clients logger.
|
2019-03-01 01:45:01 +00:00
|
|
|
log: slog::Logger,
|
2019-03-18 06:38:23 +00:00
|
|
|
/// Marker to pin the beacon chain generics.
|
|
|
|
phantom: PhantomData<T>,
|
2019-03-01 01:45:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 06:38:23 +00:00
|
|
|
impl<TClientType: ClientTypes> Client<TClientType> {
|
|
|
|
/// Generate an instance of the client. Spawn and link all internal sub-processes.
|
2019-03-01 01:45:01 +00:00
|
|
|
pub fn new(
|
|
|
|
config: ClientConfig,
|
|
|
|
log: slog::Logger,
|
2019-03-17 10:49:56 +00:00
|
|
|
executor: &TaskExecutor,
|
2019-03-01 01:45:01 +00:00
|
|
|
) -> error::Result<Self> {
|
2019-03-18 05:16:54 +00:00
|
|
|
// generate a beacon chain
|
2019-03-18 06:38:23 +00:00
|
|
|
let beacon_chain = TClientType::initialise_beacon_chain(&config);
|
2019-03-04 05:39:37 +00:00
|
|
|
|
2019-03-27 00:25:15 +00:00
|
|
|
if beacon_chain.read_slot_clock().is_none() {
|
|
|
|
panic!("Cannot start client before genesis!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block starting the client until we have caught the state up to the current slot.
|
|
|
|
//
|
|
|
|
// If we don't block here we create an initial scenario where we're unable to process any
|
|
|
|
// blocks and we're basically useless.
|
2019-03-26 23:36:20 +00:00
|
|
|
{
|
2019-03-27 00:25:15 +00:00
|
|
|
let state_slot = beacon_chain.state.read().slot;
|
|
|
|
let wall_clock_slot = beacon_chain.read_slot_clock().unwrap();
|
|
|
|
let slots_since_genesis = beacon_chain.slots_since_genesis().unwrap();
|
2019-03-26 23:36:20 +00:00
|
|
|
info!(
|
|
|
|
log,
|
2019-03-27 00:25:15 +00:00
|
|
|
"Initializing state";
|
|
|
|
"state_slot" => state_slot,
|
|
|
|
"wall_clock_slot" => wall_clock_slot,
|
|
|
|
"slots_since_genesis" => slots_since_genesis,
|
|
|
|
"catchup_distance" => wall_clock_slot - state_slot,
|
2019-03-26 23:36:20 +00:00
|
|
|
);
|
|
|
|
}
|
2019-03-27 00:25:15 +00:00
|
|
|
do_state_catchup(&beacon_chain, &log);
|
|
|
|
info!(
|
|
|
|
log,
|
|
|
|
"State initialized";
|
|
|
|
"state_slot" => beacon_chain.state.read().slot,
|
|
|
|
"wall_clock_slot" => beacon_chain.read_slot_clock().unwrap(),
|
|
|
|
);
|
2019-03-26 23:36:20 +00:00
|
|
|
|
2019-03-04 07:31:01 +00:00
|
|
|
// Start the network service, libp2p and syncing threads
|
2019-03-04 05:39:37 +00:00
|
|
|
// TODO: Add beacon_chain reference to network parameters
|
2019-03-18 05:16:54 +00:00
|
|
|
let network_config = &config.net_conf;
|
2019-03-04 07:31:01 +00:00
|
|
|
let network_logger = log.new(o!("Service" => "Network"));
|
2019-03-25 11:00:11 +00:00
|
|
|
let (network, network_send) = NetworkService::new(
|
2019-03-18 05:16:54 +00:00
|
|
|
beacon_chain.clone(),
|
|
|
|
network_config,
|
|
|
|
executor,
|
|
|
|
network_logger,
|
|
|
|
)?;
|
2019-03-04 05:39:37 +00:00
|
|
|
|
2019-03-19 12:47:58 +00:00
|
|
|
// spawn the RPC server
|
2019-04-03 05:23:09 +00:00
|
|
|
let rpc_exit_signal = if config.rpc_conf.enabled {
|
|
|
|
Some(rpc::start_server(
|
2019-03-22 05:46:52 +00:00
|
|
|
&config.rpc_conf,
|
|
|
|
executor,
|
2019-03-25 11:00:11 +00:00
|
|
|
network_send,
|
2019-03-22 05:46:52 +00:00
|
|
|
beacon_chain.clone(),
|
|
|
|
&log,
|
2019-04-03 05:23:09 +00:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-03-19 12:47:58 +00:00
|
|
|
|
2019-03-26 23:36:20 +00:00
|
|
|
let (slot_timer_exit_signal, exit) = exit_future::signal();
|
|
|
|
if let Ok(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
|
|
|
|
let interval = {
|
|
|
|
// Set the interval to start at the next slot, and every slot after
|
|
|
|
let slot_duration = Duration::from_secs(config.spec.seconds_per_slot);
|
|
|
|
//TODO: Handle checked add correctly
|
|
|
|
Interval::new(Instant::now() + duration_to_next_slot, slot_duration)
|
|
|
|
};
|
|
|
|
|
|
|
|
let chain = beacon_chain.clone();
|
|
|
|
let log = log.new(o!("Service" => "SlotTimer"));
|
|
|
|
executor.spawn(
|
|
|
|
exit.until(
|
|
|
|
interval
|
|
|
|
.for_each(move |_| {
|
2019-03-27 00:25:15 +00:00
|
|
|
do_state_catchup(&chain, &log);
|
2019-03-26 23:36:20 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.map_err(|_| ()),
|
|
|
|
)
|
|
|
|
.map(|_| ()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-01 01:45:01 +00:00
|
|
|
Ok(Client {
|
2019-04-03 05:23:09 +00:00
|
|
|
_config: config,
|
|
|
|
_beacon_chain: beacon_chain,
|
2019-03-22 05:46:52 +00:00
|
|
|
rpc_exit_signal,
|
2019-03-26 23:36:20 +00:00
|
|
|
slot_timer_exit_signal: Some(slot_timer_exit_signal),
|
2019-03-01 01:45:01 +00:00
|
|
|
log,
|
2019-03-19 12:20:39 +00:00
|
|
|
network,
|
2019-03-01 01:45:01 +00:00
|
|
|
phantom: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 00:25:15 +00:00
|
|
|
|
2019-05-13 04:44:43 +00:00
|
|
|
fn do_state_catchup<T, U, F, E>(chain: &Arc<BeaconChain<T, U, F, E>>, log: &slog::Logger)
|
2019-03-27 00:25:15 +00:00
|
|
|
where
|
2019-05-21 07:45:35 +00:00
|
|
|
T: Store,
|
2019-03-27 00:25:15 +00:00
|
|
|
U: SlotClock,
|
|
|
|
F: ForkChoice,
|
2019-05-13 04:44:43 +00:00
|
|
|
E: EthSpec,
|
2019-03-27 00:25:15 +00:00
|
|
|
{
|
|
|
|
if let Some(genesis_height) = chain.slots_since_genesis() {
|
|
|
|
let result = chain.catchup_state();
|
|
|
|
|
|
|
|
let common = o!(
|
|
|
|
"best_slot" => chain.head().beacon_block.slot,
|
|
|
|
"latest_block_root" => format!("{}", chain.head().beacon_block_root),
|
|
|
|
"wall_clock_slot" => chain.read_slot_clock().unwrap(),
|
|
|
|
"state_slot" => chain.state.read().slot,
|
|
|
|
"slots_since_genesis" => genesis_height,
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => info!(
|
|
|
|
log,
|
|
|
|
"NewSlot";
|
|
|
|
common
|
|
|
|
),
|
|
|
|
Err(e) => error!(
|
|
|
|
log,
|
|
|
|
"StateCatchupFailed";
|
|
|
|
"error" => format!("{:?}", e),
|
|
|
|
common
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|