diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index bf807188f..ca089789d 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -336,6 +336,11 @@ impl BeaconChain { self.canonical_head.read() } + /// Returns the slot of the highest block in the canonical chain. + pub fn best_slot(&self) -> Slot { + self.canonical_head.read().beacon_block.slot + } + /// Updates the canonical `BeaconState` with the supplied state. /// /// Advances the chain forward to the present slot. This method is better than just setting diff --git a/beacon_node/client/src/beacon_chain_types.rs b/beacon_node/client/src/beacon_chain_types.rs index 7ffb26b8b..f92304299 100644 --- a/beacon_node/client/src/beacon_chain_types.rs +++ b/beacon_node/client/src/beacon_chain_types.rs @@ -5,6 +5,7 @@ use beacon_chain::{ BeaconChain, BeaconChainTypes, }; use fork_choice::ForkChoice; +use slog::{info, Logger}; use slot_clock::SlotClock; use std::sync::Arc; use tree_hash::TreeHash; @@ -14,7 +15,7 @@ use types::{ /// Provides a new, initialized `BeaconChain` pub trait InitialiseBeaconChain { - fn initialise_beacon_chain(store: Arc) -> BeaconChain; + fn initialise_beacon_chain(store: Arc, log: Logger) -> BeaconChain; } /// A testnet-suitable BeaconChainType, using `MemoryStore`. @@ -29,8 +30,8 @@ impl BeaconChainTypes for TestnetMemoryBeaconChainTypes { } impl InitialiseBeaconChain for TestnetMemoryBeaconChainTypes { - fn initialise_beacon_chain(store: Arc) -> BeaconChain { - maybe_load_from_store_for_testnet::<_, T::Store, T::EthSpec>(store) + fn initialise_beacon_chain(store: Arc, log: Logger) -> BeaconChain { + maybe_load_from_store_for_testnet::<_, T::Store, T::EthSpec>(store, log) } } @@ -46,20 +47,31 @@ impl BeaconChainTypes for TestnetDiskBeaconChainTypes { } impl InitialiseBeaconChain for TestnetDiskBeaconChainTypes { - fn initialise_beacon_chain(store: Arc) -> BeaconChain { - maybe_load_from_store_for_testnet::<_, T::Store, T::EthSpec>(store) + fn initialise_beacon_chain(store: Arc, log: Logger) -> BeaconChain { + maybe_load_from_store_for_testnet::<_, T::Store, T::EthSpec>(store, log) } } /// Loads a `BeaconChain` from `store`, if it exists. Otherwise, create a new chain from genesis. -fn maybe_load_from_store_for_testnet(store: Arc) -> BeaconChain +fn maybe_load_from_store_for_testnet( + store: Arc, + log: Logger, +) -> BeaconChain where T: BeaconChainTypes, T::ForkChoice: ForkChoice, { if let Ok(Some(beacon_chain)) = BeaconChain::from_store(store.clone()) { + info!( + log, + "Loaded BeaconChain from store"; + "slot" => beacon_chain.state.read().slot, + "best_slot" => beacon_chain.best_slot(), + ); + beacon_chain } else { + info!(log, "Initializing new BeaconChain from genesis"); let spec = T::EthSpec::spec(); let state_builder = diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index df9eb8646..734de4727 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -28,7 +28,7 @@ pub struct Client { /// Configuration for the lighthouse client. _config: ClientConfig, /// The beacon chain for the running client. - _beacon_chain: Arc>, + beacon_chain: Arc>, /// Reference to the network service. pub network: Arc>, /// Signal to terminate the RPC server. @@ -57,7 +57,7 @@ where let store = Arc::new(store); // Load a `BeaconChain` from the store, or create a new one if it does not exist. - let beacon_chain = Arc::new(T::initialise_beacon_chain(store)); + let beacon_chain = Arc::new(T::initialise_beacon_chain(store, log.clone())); if beacon_chain.read_slot_clock().is_none() { panic!("Cannot start client before genesis!") @@ -151,7 +151,7 @@ where Ok(Client { _config: config, - _beacon_chain: beacon_chain, + beacon_chain, http_exit_signal, rpc_exit_signal, slot_timer_exit_signal: Some(slot_timer_exit_signal), @@ -162,6 +162,14 @@ where } } +impl Drop for Client { + fn drop(&mut self) { + // Save the beacon chain to it's store before dropping. + let _result = self.beacon_chain.persist(); + dbg!("Saved BeaconChain to store"); + } +} + fn do_state_catchup(chain: &Arc>, log: &slog::Logger) { if let Some(genesis_height) = chain.slots_since_genesis() { let result = chain.catchup_state();