Skip to content
Snippets Groups Projects
Commit 83cc1ca0 authored by Benjamin Gallois's avatar Benjamin Gallois Committed by Hugo Trentesaux
Browse files

Fix #255 distance reading discarded state (!284)

* fix #255

* fix unset CurrentPoolIndex during first evaluation period
parent c521f5e1
No related branches found
No related tags found
1 merge request!284Fix #255 distance reading discarded state
Pipeline #38252 passed
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment