Skip to content
Snippets Groups Projects
Unverified Commit 8925cfd4 authored by bgallois's avatar bgallois
Browse files

fix #262

parent 1ea5bce4
No related branches found
No related tags found
No related merge requests found
Pipeline #38612 passed
...@@ -40,19 +40,19 @@ where ...@@ -40,19 +40,19 @@ where
Backend: sc_client_api::Backend<B>, Backend: sc_client_api::Backend<B>,
IdtyIndex: Decode + Encode + PartialEq + TypeInfo, IdtyIndex: Decode + Encode + PartialEq + TypeInfo,
{ {
// Retrieve the pool_index from storage. If storage is inaccessible or the data is corrupted, // Retrieve the period_index from storage. If storage is inaccessible or the data is corrupted,
// return the appropriate error. // return the appropriate error.
let pool_index = client let period_index = client
.storage( .storage(
parent, parent,
&StorageKey( &StorageKey(
frame_support::storage::storage_prefix(b"Distance", b"CurrentPoolIndex").to_vec(), frame_support::storage::storage_prefix(b"Distance", b"CurrentPeriodIndex").to_vec(),
), ),
)? )?
.map_or_else( .map_or_else(
|| { || {
Err(sc_client_api::blockchain::Error::Storage( Err(sc_client_api::blockchain::Error::Storage(
"CurrentPoolIndex value not found".to_string(), "CurrentPeriodIndex value not found".to_string(),
)) ))
}, },
|raw| { |raw| {
...@@ -70,7 +70,7 @@ where ...@@ -70,7 +70,7 @@ where
&StorageKey( &StorageKey(
frame_support::storage::storage_prefix( frame_support::storage::storage_prefix(
b"Distance", b"Distance",
match pool_index { match period_index % 3 {
0 => b"EvaluationPool0", 0 => b"EvaluationPool0",
1 => b"EvaluationPool1", 1 => b"EvaluationPool1",
2 => b"EvaluationPool2", 2 => b"EvaluationPool2",
...@@ -100,9 +100,9 @@ where ...@@ -100,9 +100,9 @@ where
// Read evaluation result from file, if it exists // Read evaluation result from file, if it exists
log::debug!( log::debug!(
"🧙 [distance oracle] Reading evaluation result from file {:?}", "🧙 [distance oracle] Reading evaluation result from file {:?}",
distance_dir.clone().join(pool_index.to_string()) distance_dir.clone().join(period_index.to_string())
); );
let evaluation_result = match std::fs::read(distance_dir.join(pool_index.to_string())) { let evaluation_result = match std::fs::read(distance_dir.join(period_index.to_string())) {
Ok(data) => data, Ok(data) => data,
Err(e) => { Err(e) => {
match e.kind() { match e.kind() {
......
...@@ -40,11 +40,11 @@ pub async fn parent_hash(client: &Client) -> H256 { ...@@ -40,11 +40,11 @@ pub async fn parent_hash(client: &Client) -> H256 {
.hash() .hash()
} }
pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 { pub async fn current_period_index(client: &Client, parent_hash: H256) -> u32 {
client client
.storage() .storage()
.at(parent_hash) .at(parent_hash)
.fetch(&runtime::storage().distance().current_pool_index()) .fetch(&runtime::storage().distance().current_period_index())
.await .await
.expect("Cannot fetch current pool index") .expect("Cannot fetch current pool index")
.unwrap_or_default() .unwrap_or_default()
......
...@@ -85,7 +85,7 @@ impl Default for Settings { ...@@ -85,7 +85,7 @@ impl Default for Settings {
/// Asynchronously runs a computation using the provided client and saves the result to a file. /// Asynchronously runs a computation using the provided client and saves the result to a file.
pub async fn run_and_save(client: &api::Client, settings: &Settings) { pub async fn run_and_save(client: &api::Client, settings: &Settings) {
let Some((evaluation, current_pool_index, evaluation_result_path)) = let Some((evaluation, current_period_index, evaluation_result_path)) =
run(client, settings, true).await run(client, settings, true).await
else { else {
return; return;
...@@ -114,35 +114,38 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) { ...@@ -114,35 +114,38 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) {
) )
}); });
// Remove old results // When a new result is written, remove old results except for the current period used by the inherent logic and the next period that was just generated.
let mut files_to_remove = Vec::new(); settings
for entry in settings
.evaluation_result_dir .evaluation_result_dir
.read_dir() .read_dir()
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
panic!( panic!(
"Cannot read distance evaluation result directory `{0:?}`: {e:?}", "Cannot read distance evaluation result directory `{:?}`: {:?}",
settings.evaluation_result_dir settings.evaluation_result_dir, e
) )
}) })
.flatten() .flatten()
{ .filter_map(|entry| {
if let Ok(entry_name) = entry.file_name().into_string() { entry
if let Ok(entry_pool) = entry_name.parse::<isize>() { .file_name()
if current_pool_index as isize - entry_pool > 3 { .into_string()
files_to_remove.push(entry.path()); .ok()
} .and_then(|name| {
} name.parse::<isize>().ok().filter(|&pool| {
} pool != current_period_index as isize
} && pool != (current_period_index + 1) as isize
files_to_remove.into_iter().for_each(|f| { })
std::fs::remove_file(&f) })
.unwrap_or_else(move |e| warn!("Cannot remove old result file `{f:?}`: {e:?}")); .map(|_| entry.path())
}); })
.for_each(|path| {
std::fs::remove_file(&path)
.unwrap_or_else(|e| warn!("Cannot remove file `{:?}`: {:?}", path, e));
});
} }
/// Asynchronously runs a computation based on the provided client and settings. /// Asynchronously runs a computation based on the provided client and settings.
/// Returns `Option<(evaluation, current_pool_index, evaluation_result_path)>`. /// Returns `Option<(evaluation, current_period_index, evaluation_result_path)>`.
pub async fn run( pub async fn run(
client: &api::Client, client: &api::Client,
settings: &Settings, settings: &Settings,
...@@ -152,10 +155,11 @@ pub async fn run( ...@@ -152,10 +155,11 @@ pub async fn run(
let max_depth = api::max_referee_distance(client).await; let max_depth = api::max_referee_distance(client).await;
let current_pool_index = api::current_pool_index(client, parent_hash).await; let current_period_index = api::current_period_index(client, parent_hash).await;
// Fetch the pending identities // Fetch the pending identities
let Some(evaluation_pool) = api::current_pool(client, parent_hash, current_pool_index).await let Some(evaluation_pool) =
api::current_pool(client, parent_hash, current_period_index % 3).await
else { else {
info!("Nothing to do: Pool does not exist"); info!("Nothing to do: Pool does not exist");
return None; return None;
...@@ -169,7 +173,7 @@ pub async fn run( ...@@ -169,7 +173,7 @@ pub async fn run(
let evaluation_result_path = settings let evaluation_result_path = settings
.evaluation_result_dir .evaluation_result_dir
.join((current_pool_index + 1).to_string()); .join((current_period_index + 1).to_string());
if handle_fs { if handle_fs {
// Stop if already evaluated // Stop if already evaluated
...@@ -189,7 +193,7 @@ pub async fn run( ...@@ -189,7 +193,7 @@ pub async fn run(
}); });
} }
info!("Evaluating distance for pool {}", current_pool_index); info!("Evaluating distance for period {}", current_period_index);
let evaluation_block = api::evaluation_block(client, parent_hash).await; let evaluation_block = api::evaluation_block(client, parent_hash).await;
// member idty -> issued certs // member idty -> issued certs
...@@ -245,7 +249,7 @@ pub async fn run( ...@@ -245,7 +249,7 @@ pub async fn run(
.map(|(idty, _)| distance_rule(&received_certs, &referees, max_depth, *idty)) .map(|(idty, _)| distance_rule(&received_certs, &referees, max_depth, *idty))
.collect(); .collect();
Some((evaluation, current_pool_index, evaluation_result_path)) Some((evaluation, current_period_index, evaluation_result_path))
} }
fn distance_rule_recursive( fn distance_rule_recursive(
......
...@@ -46,7 +46,7 @@ pub async fn parent_hash(_client: &Client) -> H256 { ...@@ -46,7 +46,7 @@ pub async fn parent_hash(_client: &Client) -> H256 {
Default::default() Default::default()
} }
pub async fn current_pool_index(_client: &Client, _parent_hash: H256) -> u32 { pub async fn current_period_index(_client: &Client, _parent_hash: H256) -> u32 {
0 0
} }
......
...@@ -186,7 +186,7 @@ mod benchmarks { ...@@ -186,7 +186,7 @@ mod benchmarks {
#[benchmark] #[benchmark]
fn do_evaluation_success() -> Result<(), BenchmarkError> { fn do_evaluation_success() -> Result<(), BenchmarkError> {
// Benchmarking do_evaluation in case of a single success. // Benchmarking do_evaluation in case of a single success.
CurrentPoolIndex::<T>::put(0); CurrentPeriodIndex::<T>::put(0);
// More than membership renewal to avoid antispam // More than membership renewal to avoid antispam
frame_system::pallet::Pallet::<T>::set_block_number(500_000_000u32.into()); frame_system::pallet::Pallet::<T>::set_block_number(500_000_000u32.into());
let idty = T::IdtyIndex::one(); let idty = T::IdtyIndex::one();
...@@ -203,7 +203,7 @@ mod benchmarks { ...@@ -203,7 +203,7 @@ mod benchmarks {
.into(), .into(),
); );
CurrentPoolIndex::<T>::put(2); CurrentPeriodIndex::<T>::put(2);
Pallet::<T>::force_update_evaluation( Pallet::<T>::force_update_evaluation(
RawOrigin::Root.into(), RawOrigin::Root.into(),
caller, caller,
...@@ -230,7 +230,7 @@ mod benchmarks { ...@@ -230,7 +230,7 @@ mod benchmarks {
#[benchmark] #[benchmark]
fn do_evaluation_failure() -> Result<(), BenchmarkError> { fn do_evaluation_failure() -> Result<(), BenchmarkError> {
// Benchmarking do_evaluation in case of a single failure. // Benchmarking do_evaluation in case of a single failure.
CurrentPoolIndex::<T>::put(0); CurrentPeriodIndex::<T>::put(0);
// More than membership renewal to avoid antispam // More than membership renewal to avoid antispam
frame_system::pallet::Pallet::<T>::set_block_number(500_000_000u32.into()); frame_system::pallet::Pallet::<T>::set_block_number(500_000_000u32.into());
let idty = T::IdtyIndex::one(); let idty = T::IdtyIndex::one();
...@@ -247,7 +247,7 @@ mod benchmarks { ...@@ -247,7 +247,7 @@ mod benchmarks {
.into(), .into(),
); );
CurrentPoolIndex::<T>::put(2); CurrentPeriodIndex::<T>::put(2);
Pallet::<T>::force_update_evaluation( Pallet::<T>::force_update_evaluation(
RawOrigin::Root.into(), RawOrigin::Root.into(),
caller, caller,
......
...@@ -234,10 +234,10 @@ pub mod pallet { ...@@ -234,10 +234,10 @@ pub mod pallet {
#[pallet::storage] #[pallet::storage]
pub(super) type DidUpdate<T: Config> = StorageValue<_, bool, ValueQuery>; pub(super) type DidUpdate<T: Config> = StorageValue<_, bool, ValueQuery>;
/// The current evaluation pool index. /// The current evaluation period index.
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn current_pool_index)] #[pallet::getter(fn current_period_index)]
pub(super) type CurrentPoolIndex<T: Config> = StorageValue<_, u32, ValueQuery>; pub(super) type CurrentPeriodIndex<T: Config> = StorageValue<_, u32, ValueQuery>;
#[pallet::event] #[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)] #[pallet::generate_deposit(pub(super) fn deposit_event)]
...@@ -300,7 +300,7 @@ pub mod pallet { ...@@ -300,7 +300,7 @@ pub mod pallet {
#[pallet::genesis_build] #[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> { impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) { fn build(&self) {
CurrentPoolIndex::<T>::put(0u32); CurrentPeriodIndex::<T>::put(0u32);
} }
} }
...@@ -314,10 +314,10 @@ pub mod pallet { ...@@ -314,10 +314,10 @@ pub mod pallet {
if block % BlockNumberFor::<T>::one().saturating_mul(T::EvaluationPeriod::get().into()) if block % BlockNumberFor::<T>::one().saturating_mul(T::EvaluationPeriod::get().into())
== BlockNumberFor::<T>::zero() == BlockNumberFor::<T>::zero()
{ {
let index = (CurrentPoolIndex::<T>::get() + 1) % 3; let index = CurrentPeriodIndex::<T>::get() + 1;
CurrentPoolIndex::<T>::put(index); CurrentPeriodIndex::<T>::put(index);
weight = weight weight = weight
.saturating_add(Self::do_evaluation(index)) .saturating_add(Self::do_evaluation(index % 3))
.saturating_add(T::DbWeight::get().reads_writes(1, 1)); .saturating_add(T::DbWeight::get().reads_writes(1, 1));
} }
weight.saturating_add(<T as pallet::Config>::WeightInfo::on_finalize()) weight.saturating_add(<T as pallet::Config>::WeightInfo::on_finalize())
...@@ -557,7 +557,7 @@ pub mod pallet { ...@@ -557,7 +557,7 @@ pub mod pallet {
who: &T::AccountId, who: &T::AccountId,
idty_index: <T as pallet_identity::Config>::IdtyIndex, idty_index: <T as pallet_identity::Config>::IdtyIndex,
) -> Result<(), DispatchError> { ) -> Result<(), DispatchError> {
Pallet::<T>::mutate_current_pool(CurrentPoolIndex::<T>::get(), |current_pool| { Pallet::<T>::mutate_current_pool(CurrentPeriodIndex::<T>::get() % 3, |current_pool| {
// extrinsics are transactional by default, this check might not be needed // extrinsics are transactional by default, this check might not be needed
ensure!( ensure!(
current_pool.evaluations.len() < (MAX_EVALUATIONS_PER_SESSION as usize), current_pool.evaluations.len() < (MAX_EVALUATIONS_PER_SESSION as usize),
...@@ -590,7 +590,7 @@ pub mod pallet { ...@@ -590,7 +590,7 @@ pub mod pallet {
evaluator: <T as frame_system::Config>::AccountId, evaluator: <T as frame_system::Config>::AccountId,
computation_result: ComputationResult, computation_result: ComputationResult,
) -> DispatchResult { ) -> DispatchResult {
Pallet::<T>::mutate_next_pool(CurrentPoolIndex::<T>::get(), |result_pool| { Pallet::<T>::mutate_next_pool(CurrentPeriodIndex::<T>::get() % 3, |result_pool| {
// evaluation must be provided for all identities (no more, no less) // evaluation must be provided for all identities (no more, no less)
ensure!( ensure!(
computation_result.distances.len() == result_pool.evaluations.len(), computation_result.distances.len() == result_pool.evaluations.len(),
......
No preview for this file type
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