Skip to content
Snippets Groups Projects
Commit effde467 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

wip fix distance oracle

parent 1ea5bce4
No related branches found
No related tags found
No related merge requests found
Pipeline #38626 waiting for manual action
...@@ -40,6 +40,7 @@ pub async fn parent_hash(client: &Client) -> H256 { ...@@ -40,6 +40,7 @@ pub async fn parent_hash(client: &Client) -> H256 {
.hash() .hash()
} }
/// get current pool index (value between 0 and 2)
pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 { pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 {
client client
.storage() .storage()
...@@ -50,6 +51,27 @@ pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 { ...@@ -50,6 +51,27 @@ pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 {
.unwrap_or_default() .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( pub async fn current_pool(
client: &Client, client: &Client,
parent_hash: H256, parent_hash: H256,
......
...@@ -84,9 +84,14 @@ impl Default for Settings { ...@@ -84,9 +84,14 @@ 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.
/// Also removes files of old evaluation results that are not needed anymore.
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(RunReturnValue {
run(client, settings, true).await evaluation,
current_period,
evaluation_period,
evaluation_result_path,
}) = run(client, settings, true).await
else { else {
return; return;
}; };
...@@ -128,8 +133,8 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) { ...@@ -128,8 +133,8 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) {
.flatten() .flatten()
{ {
if let Ok(entry_name) = entry.file_name().into_string() { if let Ok(entry_name) = entry.file_name().into_string() {
if let Ok(entry_pool) = entry_name.parse::<isize>() { if let Ok(entry_period) = entry_name.parse::<u32>() {
if current_pool_index as isize - entry_pool > 3 { if current_period - entry_period > 3 * evaluation_period {
files_to_remove.push(entry.path()); files_to_remove.push(entry.path());
} }
} }
...@@ -141,18 +146,32 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) { ...@@ -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. /// Asynchronously runs a computation based on the provided client and settings.
/// Returns `Option<(evaluation, current_pool_index, evaluation_result_path)>`. /// Returns `Option<(evaluation, current_pool_index, evaluation_result_path)>`.
pub async fn run( pub async fn run(
client: &api::Client, client: &api::Client,
settings: &Settings, settings: &Settings,
handle_fs: bool, handle_fs: bool,
) -> Option<(Vec<sp_runtime::Perbill>, u32, PathBuf)> { ) -> Option<RunReturnValue> {
let parent_hash = api::parent_hash(client).await; let parent_hash = api::parent_hash(client).await;
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_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 // 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_pool_index).await
...@@ -167,9 +186,10 @@ pub async fn run( ...@@ -167,9 +186,10 @@ pub async fn run(
return None; return None;
} }
// the path is simply the block number of the evaluation
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.to_string());
if handle_fs { if handle_fs {
// Stop if already evaluated // Stop if already evaluated
...@@ -189,7 +209,10 @@ pub async fn run( ...@@ -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; let evaluation_block = api::evaluation_block(client, parent_hash).await;
// member idty -> issued certs // member idty -> issued certs
...@@ -245,7 +268,12 @@ pub async fn run( ...@@ -245,7 +268,12 @@ 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(RunReturnValue {
evaluation,
current_period,
evaluation_period,
evaluation_result_path,
})
} }
fn distance_rule_recursive( fn distance_rule_recursive(
......
...@@ -234,7 +234,7 @@ pub mod pallet { ...@@ -234,7 +234,7 @@ 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 pool index. Value between 0 and 2.
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn current_pool_index)] #[pallet::getter(fn current_pool_index)]
pub(super) type CurrentPoolIndex<T: Config> = StorageValue<_, u32, ValueQuery>; pub(super) type CurrentPoolIndex<T: Config> = StorageValue<_, u32, ValueQuery>;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment