diff --git a/client/distance/src/lib.rs b/client/distance/src/lib.rs index 1b67e85fe2dba9c4a155440231ec8f5e664289d8..85885d33903d745f02f74ce962e485c3dbf0207c 100644 --- a/client/distance/src/lib.rs +++ b/client/distance/src/lib.rs @@ -40,18 +40,30 @@ where Backend: sc_client_api::Backend<B>, IdtyIndex: Decode + Encode + PartialEq + TypeInfo, { + // Retrieve the pool_index from storage. If storage is inaccessible or the data is corrupted, + // return the appropriate error. let pool_index = client .storage( parent, &StorageKey( frame_support::storage::storage_prefix(b"Distance", b"CurrentPoolIndex").to_vec(), ), - ) - .expect("CurrentIndex is Err") - .map_or(0, |raw| { - u32::decode(&mut &raw.0[..]).expect("cannot decode CurrentIndex") - }); + )? + .map_or_else( + || { + Err(sc_client_api::blockchain::Error::Storage( + "CurrentPoolIndex value not found".to_string(), + )) + }, + |raw| { + u32::decode(&mut &raw.0[..]) + .map_err(|e| sc_client_api::blockchain::Error::from_state(Box::new(e))) + }, + )?; + // Retrieve the published_results from storage. + // Return an error if the storage is inaccessible. + // If accessible, continue execution. If None, it means there are no published_results at this block. let published_results = client .storage( parent, @@ -68,21 +80,21 @@ where .to_vec(), ), )? - .map_or_else(Default::default, |raw| { - pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..]) - .expect("cannot decode EvaluationPool") + .and_then(|raw| { + pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..]).ok() }); // Have we already published a result for this period? // The block author is guaranteed to be in the owner_keys. - let owner_keys = owner_keys - .iter() - .map(|&key| sp_runtime::AccountId32::new(key.0)) - .any(|key| published_results.evaluators.contains(&key)); - - if owner_keys { - log::debug!("🧙 [distance oracle] Already published a result for this period"); - return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); + if let Some(results) = published_results { + if owner_keys + .iter() + .map(|&key| sp_runtime::AccountId32::new(key.0)) + .any(|key| results.evaluators.contains(&key)) + { + log::debug!("🧙 [distance oracle] Already published a result for this period"); + return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); + } } // Read evaluation result from file, if it exists diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index f63a8d613025bca059d2490319a72e5c23d8fbd5..f1866bf17e584a6e5f52a68be25825beb2d0a650 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -81,10 +81,13 @@ pub use traits::*; pub use types::*; pub use weights::WeightInfo; -use frame_support::traits::{ - fungible::{self, hold, Credit, Mutate, MutateHold}, - tokens::Precision, - OnUnbalanced, StorageVersion, +use frame_support::{ + traits::{ + fungible::{self, hold, Credit, Mutate, MutateHold}, + tokens::Precision, + OnUnbalanced, StorageVersion, + }, + DefaultNoBound, }; use sp_distance::{InherentError, INHERENT_IDENTIFIER}; use sp_inherents::{InherentData, InherentIdentifier}; @@ -288,6 +291,19 @@ pub mod pallet { TargetMustBeUnvalidated, } + #[pallet::genesis_config] + #[derive(DefaultNoBound)] + pub struct GenesisConfig<T: Config> { + pub _config: core::marker::PhantomData<T>, + } + + #[pallet::genesis_build] + impl<T: Config> BuildGenesisConfig for GenesisConfig<T> { + fn build(&self) { + CurrentPoolIndex::<T>::put(0u32); + } + } + #[pallet::hooks] impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { fn on_initialize(block: BlockNumberFor<T>) -> Weight