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

Fix distance evaluation client (!291)

* Avoid decoding storage by hand

* Fix distance evaluation client
parent 38111644
No related branches found
No related tags found
1 merge request!291Fix distance evaluation client
......@@ -85,24 +85,45 @@ where
});
// 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 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");
// Find the account associated with the BABE key that is in our owner keys.
let mut local_account = None;
for key in owner_keys {
// Session::KeyOwner is StorageMap<_, Twox64Concat, (KeyTypeId, Vec<u8>), AccountId32, OptionQuery>
// Slices (variable length) and array (fixed length) are encoded differently, so the `.as_slice()` is needed
let item_key = (sp_runtime::KeyTypeId(*b"babe"), key.0.as_slice()).encode();
let mut storage_key =
frame_support::storage::storage_prefix(b"Session", b"KeyOwner").to_vec();
storage_key.extend_from_slice(&sp_core::twox_64(&item_key));
storage_key.extend_from_slice(&item_key);
if let Some(raw_data) = client.storage(parent, &StorageKey(storage_key))? {
if let Ok(key_owner) = AccountId32::decode(&mut &raw_data.0[..]) {
local_account = Some(key_owner);
break;
} else {
log::warn!("🧙 [distance oracle] Cannot decode key owner value");
}
}
}
if let Some(local_account) = local_account {
if results.evaluators.contains(&local_account) {
log::debug!("🧙 [distance oracle] Already published a result for this period");
return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
}
} else {
log::error!("🧙 [distance oracle] Cannot find our BABE owner key");
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!(
"🧙 [distance oracle] Reading evaluation result from file {:?}",
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,
Err(e) => {
match e.kind() {
......@@ -118,6 +139,9 @@ where
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");
Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(Some(
......
......@@ -28,7 +28,7 @@ use api::{AccountId, IdtyIndex};
use codec::Encode;
use fnv::{FnvHashMap, FnvHashSet};
use log::{debug, error, info, warn};
use log::{debug, error, info};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::{io::Write, path::PathBuf};
......@@ -85,7 +85,7 @@ impl Default for Settings {
/// 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) {
let Some((evaluation, current_pool_index, evaluation_result_path)) =
let Some((evaluation, _current_pool_index, evaluation_result_path)) =
run(client, settings, true).await
else {
return;
......@@ -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:?}"
)
});
// 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.
......@@ -169,7 +143,7 @@ pub async fn run(
let evaluation_result_path = settings
.evaluation_result_dir
.join((current_pool_index + 1).to_string());
.join(((current_pool_index + 1) % 3).to_string());
if handle_fs {
// 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