From b3ec74e93a2d849c94b0dcc0e118c4265b1c40d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20M=C3=BCller?= Date: Mon, 11 Apr 2022 09:50:47 +0200 Subject: [PATCH] More PR fixes --- core-primitives/stf-state-handler/src/file_io.rs | 10 +++++----- .../stf-state-handler/src/state_handler.rs | 4 ++-- .../src/state_snapshot_repository_loader.rs | 14 ++++++-------- .../stf-state-handler/src/test/sgx_tests.rs | 6 +++--- service/src/tests/ecalls.rs | 4 ++-- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/core-primitives/stf-state-handler/src/file_io.rs b/core-primitives/stf-state-handler/src/file_io.rs index 27e98ee709..860913b766 100644 --- a/core-primitives/stf-state-handler/src/file_io.rs +++ b/core-primitives/stf-state-handler/src/file_io.rs @@ -98,7 +98,7 @@ pub mod sgx { use sgx_tcrypto::rsgx_sha256_slice; use std::path::Path; - /// SGX state file I/O + /// SGX state file I/O. pub struct SgxStateFileIo { state_key: StateKey, } @@ -200,8 +200,8 @@ pub mod sgx { self.write(shard_identifier, state_id, state) } - /// Writes the state (without the state diff) encrypted into the enclave storage - /// Returns the hash of the saved state (independent of the diff!) + /// Writes the state (without the state diff) encrypted into the enclave storage. + /// Returns the hash of the saved state (independent of the diff!). fn write( &self, shard_identifier: &ShardIdentifier, @@ -211,7 +211,7 @@ pub mod sgx { let state_path = state_file_path(shard_identifier, state_id); trace!("writing state to: {:?}", state_path); - // only save the state, the state diff is pruned + // Only save the state, the state diff is pruned. let cyphertext = self.encrypt(state.state.encode())?; let state_hash = rsgx_sha256_slice(&cyphertext)?; @@ -257,7 +257,7 @@ pub mod sgx { } /// Remove a shard directory with all of its content. -pub fn remove_shard_dir(shard: &ShardIdentifier) { +pub fn purge_shard_dir(shard: &ShardIdentifier) { let shard_dir_path = shard_path(shard); if let Err(e) = std::fs::remove_dir_all(&shard_dir_path) { error!("Failed to remove shard directory {:?}: {:?}", shard_dir_path, e); diff --git a/core-primitives/stf-state-handler/src/state_handler.rs b/core-primitives/stf-state-handler/src/state_handler.rs index 032fa788b7..20e5dc04f7 100644 --- a/core-primitives/stf-state-handler/src/state_handler.rs +++ b/core-primitives/stf-state-handler/src/state_handler.rs @@ -32,8 +32,8 @@ use std::vec::Vec; /// Implementation of the `HandleState` trait. /// -/// It's concurrency wrapper around a state snapshot repository, which handles -/// access to any shards and state files. The state handler ensure we have thread-safe +/// It's a concurrency wrapper around a state snapshot repository, which handles +/// access to any shards and state files. The state handler ensures we have thread-safe /// concurrent access to that repository. pub struct StateHandler { state_snapshot_repository: RwLock, diff --git a/core-primitives/stf-state-handler/src/state_snapshot_repository_loader.rs b/core-primitives/stf-state-handler/src/state_snapshot_repository_loader.rs index c67fa649c6..71597712f5 100644 --- a/core-primitives/stf-state-handler/src/state_snapshot_repository_loader.rs +++ b/core-primitives/stf-state-handler/src/state_snapshot_repository_loader.rs @@ -103,17 +103,15 @@ where ) -> Vec> { state_ids .into_iter() - .flat_map(|state_id| { - self.file_io - .compute_hash(shard, state_id) - .map_err(|e| { - warn!( + .flat_map(|state_id| match self.file_io.compute_hash(shard, state_id) { + Ok(hash) => Some(StateSnapshotMetaData::new(hash, state_id)), + Err(e) => { + warn!( "Failed to compute hash for state snapshot with id {}: {:?}, ignoring snapshot as a result", state_id, e ); - }) - .ok() - .map(|h| StateSnapshotMetaData::new(h, state_id)) + None + }, }) .collect() } diff --git a/core-primitives/stf-state-handler/src/test/sgx_tests.rs b/core-primitives/stf-state-handler/src/test/sgx_tests.rs index 82048438c4..5348b9ea2b 100644 --- a/core-primitives/stf-state-handler/src/test/sgx_tests.rs +++ b/core-primitives/stf-state-handler/src/test/sgx_tests.rs @@ -18,7 +18,7 @@ use crate::{ error::{Error, Result}, file_io::{ - init_shard, remove_shard_dir, sgx::SgxStateFileIo, shard_exists, shard_path, StateFileIo, + init_shard, purge_shard_dir, sgx::SgxStateFileIo, shard_exists, shard_path, StateFileIo, }, handle_state::HandleState, query_shard_state::QueryShardState, @@ -57,7 +57,7 @@ impl ShardDirectoryHandle { impl Drop for ShardDirectoryHandle { fn drop(&mut self) { - remove_shard_dir(&self.shard) + purge_shard_dir(&self.shard) } } @@ -320,7 +320,7 @@ fn given_hello_world_state() -> StfState { fn given_initialized_shard(shard: &ShardIdentifier) -> Result<()> { if shard_exists(&shard) { - remove_shard_dir(shard); + purge_shard_dir(shard); } init_shard(&shard) } diff --git a/service/src/tests/ecalls.rs b/service/src/tests/ecalls.rs index 420477ce0d..62f904f67d 100644 --- a/service/src/tests/ecalls.rs +++ b/service/src/tests/ecalls.rs @@ -18,7 +18,7 @@ use crate::tests::commons::test_trusted_getter_signed; use codec::Encode; use itp_enclave_api::{enclave_base::EnclaveBase, EnclaveResult}; -use itp_stf_state_handler::file_io::remove_shard_dir; +use itp_stf_state_handler::file_io::purge_shard_dir; use log::*; use sp_core::hash::H256; use sp_keyring::AccountKeyring; @@ -34,7 +34,7 @@ pub fn get_state_works(enclave_api: &E) -> EnclaveResult<()> { assert!(!res.is_empty()); - remove_shard_dir(&shard); + purge_shard_dir(&shard); Ok(()) }