diff --git a/distance-oracle/src/api.rs b/distance-oracle/src/api.rs index 16f98a61bfc7f824429d84b06e316e66ebf274ee..b94946ff6569d3ee5211dab20cf7b54e94e8e020 100644 --- a/distance-oracle/src/api.rs +++ b/distance-oracle/src/api.rs @@ -40,6 +40,7 @@ pub async fn parent_hash(client: &Client) -> H256 { .hash() } +/// get current pool index (value between 0 and 2) pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 { client .storage() @@ -50,6 +51,27 @@ pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 { .unwrap_or_default() } +/// get evaluation_period and first block of current evaluation period +// example: +// block number 1234 +// evaluation period 100 +// period is the one of block 1200 +// return is (100, 1200) +pub async fn current_period(client: &Client, parent_hash: H256) -> (u32, u32) { + let height: u32 = client + .storage() + .at(parent_hash) + .fetch(&runtime::storage().system().number()) + .await + .expect("Cannot fetch current pool index") + .unwrap_or_default(); + let evaluation_period: u32 = client + .constants() + .at(&runtime::constants().distance().evaluation_period()) + .expect("Cannot get evaluation period"); + (evaluation_period, height - height % evaluation_period) +} + pub async fn current_pool( client: &Client, parent_hash: H256, diff --git a/distance-oracle/src/lib.rs b/distance-oracle/src/lib.rs index b2b25f1c535144167bc6c3aea7c47ce7cc3b4425..58dd64f999c2b2037a0e73c6d8566952654467d0 100644 --- a/distance-oracle/src/lib.rs +++ b/distance-oracle/src/lib.rs @@ -84,9 +84,14 @@ impl Default for Settings { } /// Asynchronously runs a computation using the provided client and saves the result to a file. +/// Also removes files of old evaluation results that are not needed anymore. pub async fn run_and_save(client: &api::Client, settings: &Settings) { - let Some((evaluation, current_pool_index, evaluation_result_path)) = - run(client, settings, true).await + let Some(RunReturnValue { + evaluation, + current_period, + evaluation_period, + evaluation_result_path, + }) = run(client, settings, true).await else { return; }; @@ -128,8 +133,8 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) { .flatten() { if let Ok(entry_name) = entry.file_name().into_string() { - if let Ok(entry_pool) = entry_name.parse::<isize>() { - if current_pool_index as isize - entry_pool > 3 { + if let Ok(entry_period) = entry_name.parse::<u32>() { + if current_period - entry_period > 3 * evaluation_period { files_to_remove.push(entry.path()); } } @@ -141,18 +146,32 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) { }); } +/// run() function return value +// this struct simply replaced a struct in order to add comments and to avoid confusion +pub struct RunReturnValue { + /// computation result + evaluation: Vec<sp_runtime::Perbill>, + /// current period (block number of the period beginning) + current_period: u32, + /// distance evaluation period constant + evaluation_period: u32, + /// file to write result + evaluation_result_path: PathBuf, +} + /// Asynchronously runs a computation based on the provided client and settings. /// Returns `Option<(evaluation, current_pool_index, evaluation_result_path)>`. pub async fn run( client: &api::Client, settings: &Settings, handle_fs: bool, -) -> Option<(Vec<sp_runtime::Perbill>, u32, PathBuf)> { +) -> Option<RunReturnValue> { let parent_hash = api::parent_hash(client).await; let max_depth = api::max_referee_distance(client).await; let current_pool_index = api::current_pool_index(client, parent_hash).await; + let (evaluation_period, current_period) = api::current_period(client, parent_hash).await; // Fetch the pending identities let Some(evaluation_pool) = api::current_pool(client, parent_hash, current_pool_index).await @@ -167,9 +186,10 @@ pub async fn run( return None; } + // the path is simply the block number of the evaluation let evaluation_result_path = settings .evaluation_result_dir - .join((current_pool_index + 1).to_string()); + .join(current_period.to_string()); if handle_fs { // Stop if already evaluated @@ -189,7 +209,10 @@ pub async fn run( }); } - info!("Evaluating distance for pool {}", current_pool_index); + info!( + "Evaluating distance for period starting at block {}", + current_period + ); let evaluation_block = api::evaluation_block(client, parent_hash).await; // member idty -> issued certs @@ -245,7 +268,12 @@ pub async fn run( .map(|(idty, _)| distance_rule(&received_certs, &referees, max_depth, *idty)) .collect(); - Some((evaluation, current_pool_index, evaluation_result_path)) + Some(RunReturnValue { + evaluation, + current_period, + evaluation_period, + evaluation_result_path, + }) } fn distance_rule_recursive( diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index f1866bf17e584a6e5f52a68be25825beb2d0a650..07fcfcd76c206c74a4e8c7b54472839be0b8699d 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -234,7 +234,7 @@ pub mod pallet { #[pallet::storage] pub(super) type DidUpdate<T: Config> = StorageValue<_, bool, ValueQuery>; - /// The current evaluation pool index. + /// The current evaluation pool index. Value between 0 and 2. #[pallet::storage] #[pallet::getter(fn current_pool_index)] pub(super) type CurrentPoolIndex<T: Config> = StorageValue<_, u32, ValueQuery>;