From 258e17a2643c6012b35917ccc9271d648b85c822 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Tue, 16 Jan 2024 14:47:44 +0100 Subject: [PATCH] implement failover to backup db --- core/parentchain/light-client/src/io.rs | 45 +++++++++++++++++-- .../src/initialization/parentchain/mod.rs | 10 ++++- enclave-runtime/src/lib.rs | 2 - 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/core/parentchain/light-client/src/io.rs b/core/parentchain/light-client/src/io.rs index afcdafacca..263ccb1cf2 100644 --- a/core/parentchain/light-client/src/io.rs +++ b/core/parentchain/light-client/src/io.rs @@ -126,13 +126,47 @@ impl LightClientSealing // unseals db with automatic failover to db backup fn unseal(&self) -> Result { Ok(unseal(self.db_path()) - .or(unseal(self.backup_path())) + .or_else(|e| { + warn!( + "can't unseal db at {:?}. error {:?}. trying backup at {:?}", + self.db_path(), + e, + self.backup_path() + ); + // create a copy because we will overwrite the db in the next step + fs::copy(self.db_path(), self.db_path().with_extension("cantunseal")).and_then( + |_| { + fs::copy(self.backup_path(), self.db_path()).and_then(|_| { + unseal(self.db_path()).map_err(|e| { + warn!("{:?}", e); + e + }) + }) + }, + ) + }) .map(|b| Decode::decode(&mut b.as_slice()))??) } - // checks if either the db or its backup can be opened + // checks if either the db or its backup can be opened in opaque mode (no unseal) fn exists(&self) -> bool { - SgxFile::open(self.db_path()).or(SgxFile::open(self.backup_path())).is_ok() + debug!("check if db exists at {:?}", self.db_path()); + fs::File::open(self.db_path()) + .or_else(|e| { + warn!( + "can't open db at {:?}. error: {:?}. trying restore backup at {:?}", + self.db_path(), + e, + self.backup_path() + ); + fs::copy(self.backup_path(), self.db_path()) + .and_then(|_| fs::File::open(self.db_path())) + .map_err(|e| { + warn!("{:?}", e); + e + }) + }) + .is_ok() } fn path(&self) -> &Path { @@ -261,9 +295,11 @@ where OCallApi: EnclaveOnChainOCallApi, LightClientSeal: LightClientSealing>, { + trace!("[{:?}] init light client db", parentchain_id); if !seal.exists() { info!( - "[Enclave] ChainRelay DB for parachain validator not found, creating new! {}", + "[{:?}] ChainRelay DB for parachain validator not found, creating new! {}", + parentchain_id, seal.path().display() ); let validator = init_parachain_validator::( @@ -276,6 +312,7 @@ where } let validation_state = seal.unseal()?; + info!("unseal success"); let genesis_hash = validation_state.genesis_hash()?; let init_state = if genesis_hash == params.genesis_header.hash() { diff --git a/enclave-runtime/src/initialization/parentchain/mod.rs b/enclave-runtime/src/initialization/parentchain/mod.rs index 0062df8d46..d421e21301 100644 --- a/enclave-runtime/src/initialization/parentchain/mod.rs +++ b/enclave-runtime/src/initialization/parentchain/mod.rs @@ -44,7 +44,7 @@ use itc_parentchain::{ }; use itp_component_container::ComponentInitializer; use itp_settings::worker_mode::ProvideWorkerMode; - +use log::*; use std::{path::PathBuf, vec::Vec}; mod common; @@ -61,6 +61,10 @@ pub(crate) fn init_parentchain_components ) -> Result> { match ParentchainInitParams::decode(&mut encoded_params.as_slice())? { ParentchainInitParams::Parachain { id, shard, params } => { + info!( + "[{:?}] initializing parachain parentchain components for shard: {:?}", + id, shard + ); let shard_creation_info = get_shard_creation_info_internal(shard)?; // todo: query timestamp of creation header to give a creation reference to target_a/b as well in order to fast-sync @@ -104,6 +108,10 @@ pub(crate) fn init_parentchain_components } }, ParentchainInitParams::Solochain { id, shard, params } => { + info!( + "[{:?}] initializing solochain parentchain components for shard: {:?}", + id, shard + ); let shard_creation_info = get_shard_creation_info_internal(shard)?; // todo: query timestamp of creation header to give a creation reference to target_a/b as well in order to fast-sync match id { diff --git a/enclave-runtime/src/lib.rs b/enclave-runtime/src/lib.rs index d6aa75dded..d5ecdb2f42 100644 --- a/enclave-runtime/src/lib.rs +++ b/enclave-runtime/src/lib.rs @@ -394,8 +394,6 @@ pub unsafe extern "C" fn init_parentchain_components( latest_header: *mut u8, latest_header_size: usize, ) -> sgx_status_t { - info!("Initializing light client!"); - let encoded_params = slice::from_raw_parts(params, params_size); let latest_header_slice = slice::from_raw_parts_mut(latest_header, latest_header_size);