Skip to content
Snippets Groups Projects
Commit 4f0f3825 authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist:
Browse files

Fix distance evaluation client

parent 38111644
No related branches found
No related tags found
1 merge request!291Fix distance evaluation client
...@@ -85,24 +85,46 @@ where ...@@ -85,24 +85,46 @@ where
}); });
// Have we already published a result for this period? // Have we already published a result for this period?
// The block author is guaranteed to be in the owner_keys.
if let Some(results) = published_results { if let Some(results) = published_results {
if owner_keys // Find the account associated with the BABE key that is in our owner keys.
.iter() let Some((_key, local_account)) = client
.map(|&key| sp_runtime::AccountId32::new(key.0)) .storage_pairs(
.any(|key| results.evaluators.contains(&key)) parent,
{ Some(&StorageKey(
frame_support::storage::storage_prefix(b"Session", b"KeyOwner").to_vec(),
)),
None,
)?
.filter_map(|(raw_key, raw_data)| {
if &raw_key.0[40..44] == b"babe" {
Some((
sp_core::sr25519::Public::from_raw(
raw_key.0[45..45 + 32].try_into().unwrap(),
),
AccountId32::decode(&mut &raw_data.0[..]).ok()?,
))
} else {
None
}
})
.find(|(key, _data)| owner_keys.contains(key))
else {
log::error!("🧙 [distance oracle] Cannot find our BABE owner key");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
};
if results.evaluators.contains(&local_account) {
log::debug!("🧙 [distance oracle] Already published a result for this period"); log::debug!("🧙 [distance oracle] Already published a result for this period");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
} }
} }
// Read evaluation result from file, if it exists // Read evaluation result from file, if it exists, then remove it
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(pool_index.to_string())
); );
let evaluation_result = match std::fs::read(distance_dir.join(pool_index.to_string())) { let evaluation_result_file_path = distance_dir.join(pool_index.to_string());
let evaluation_result = match std::fs::read(&evaluation_result_file_path) {
Ok(data) => data, Ok(data) => data,
Err(e) => { Err(e) => {
match e.kind() { match e.kind() {
...@@ -118,6 +140,9 @@ where ...@@ -118,6 +140,9 @@ where
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
} }
}; };
std::fs::remove_file(&evaluation_result_file_path).unwrap_or_else(move |e| {
log::warn!("🧙 [distance oracle] Cannot remove old result file `{evaluation_result_file_path:?}`: {e:?}")
});
log::info!("🧙 [distance oracle] Providing evaluation result"); log::info!("🧙 [distance oracle] Providing evaluation result");
Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(Some( Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(Some(
......
...@@ -28,7 +28,7 @@ use api::{AccountId, IdtyIndex}; ...@@ -28,7 +28,7 @@ use api::{AccountId, IdtyIndex};
use codec::Encode; use codec::Encode;
use fnv::{FnvHashMap, FnvHashSet}; use fnv::{FnvHashMap, FnvHashSet};
use log::{debug, error, info, warn}; use log::{debug, error, info};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::{io::Write, path::PathBuf}; use std::{io::Write, path::PathBuf};
...@@ -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_pool_index, evaluation_result_path)) =
run(client, settings, true).await run(client, settings, true).await
else { else {
return; return;
...@@ -113,32 +113,6 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) { ...@@ -113,32 +113,6 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) {
"Cannot write distance evaluation result to file `{evaluation_result_path:?}`: {e:?}" "Cannot write distance evaluation result to file `{evaluation_result_path:?}`: {e:?}"
) )
}); });
// Remove old results
let mut files_to_remove = Vec::new();
for entry in settings
.evaluation_result_dir
.read_dir()
.unwrap_or_else(|e| {
panic!(
"Cannot read distance evaluation result directory `{0:?}`: {e:?}",
settings.evaluation_result_dir
)
})
.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 {
files_to_remove.push(entry.path());
}
}
}
}
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:?}"));
});
} }
/// Asynchronously runs a computation based on the provided client and settings. /// Asynchronously runs a computation based on the provided client and settings.
...@@ -169,7 +143,7 @@ pub async fn run( ...@@ -169,7 +143,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_pool_index + 1) % 3).to_string());
if handle_fs { if handle_fs {
// Stop if already evaluated // Stop if already evaluated
......
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