diff --git a/beacon_node/beacon_chain/src/builder.rs b/beacon_node/beacon_chain/src/builder.rs index 9067965f7..0090e1530 100644 --- a/beacon_node/beacon_chain/src/builder.rs +++ b/beacon_node/beacon_chain/src/builder.rs @@ -178,6 +178,19 @@ where .map_err(|e| format!("DB error whilst reading eth1 cache: {:?}", e)) } + /// Returns true if `self.store` contains a persisted beacon chain. + pub fn store_contains_beacon_chain(&self) -> Result { + let store = self + .store + .clone() + .ok_or_else(|| "load_from_store requires a store.".to_string())?; + + Ok(store + .get::(&Hash256::from_slice(&BEACON_CHAIN_DB_KEY)) + .map_err(|e| format!("DB error when reading persisted beacon chain: {:?}", e))? + .is_some()) + } + /// Attempt to load an existing chain from the builder's `Store`. /// /// May initialize several components; including the op_pool and finalized checkpoints. diff --git a/beacon_node/client/src/builder.rs b/beacon_node/client/src/builder.rs index 42080663b..6d793f6b3 100644 --- a/beacon_node/client/src/builder.rs +++ b/beacon_node/client/src/builder.rs @@ -151,6 +151,26 @@ where Ok((builder, spec, context)) }) .and_then(move |(builder, spec, context)| { + let chain_exists = builder + .store_contains_beacon_chain() + .unwrap_or_else(|_| false); + + // 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 + // is shutdown before finding genesis via eth1. + // + // Alternatively, if there's a beacon chain in the database then always resume + // using it. + let client_genesis = if client_genesis == ClientGenesis::Resume && !chain_exists { + info!(context.log, "Defaulting to deposit contract genesis"); + + ClientGenesis::DepositContract + } else if chain_exists { + ClientGenesis::Resume + } else { + client_genesis + }; + let genesis_state_future: Box + Send> = match client_genesis { ClientGenesis::Interop { diff --git a/beacon_node/client/src/config.rs b/beacon_node/client/src/config.rs index 3335ea123..8ce129ecb 100644 --- a/beacon_node/client/src/config.rs +++ b/beacon_node/client/src/config.rs @@ -12,7 +12,7 @@ const TESTNET_SPEC_CONSTANTS: &str = "minimal"; const DEFAULT_FREEZER_DB_DIR: &str = "freezer_db"; /// Defines how the client should initialize the `BeaconChain` and other components. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] pub enum ClientGenesis { /// Reads the genesis state and other persisted data from the `Store`. Resume,