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

fix #262

parent 3ef713ee
No related branches found
No related tags found
1 merge request!290Fix #262 distance oracle pool index
......@@ -40,19 +40,19 @@ where
Backend: sc_client_api::Backend<B>,
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.
let pool_index = client
let period_index = client
.storage(
parent,
&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(
|| {
Err(sc_client_api::blockchain::Error::Storage(
"CurrentPoolIndex value not found".to_string(),
"CurrentPeriodIndex value not found".to_string(),
))
},
|raw| {
......@@ -70,7 +70,7 @@ where
&StorageKey(
frame_support::storage::storage_prefix(
b"Distance",
match pool_index {
match period_index % 3 {
0 => b"EvaluationPool0",
1 => b"EvaluationPool1",
2 => b"EvaluationPool2",
......@@ -120,10 +120,9 @@ where
// 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())
distance_dir.clone().join(period_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) {
let evaluation_result = match std::fs::read(distance_dir.join(period_index.to_string())) {
Ok(data) => data,
Err(e) => {
match e.kind() {
......
......@@ -40,11 +40,11 @@ pub async fn parent_hash(client: &Client) -> H256 {
.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
.storage()
.at(parent_hash)
.fetch(&runtime::storage().distance().current_pool_index())
.fetch(&runtime::storage().distance().current_period_index())
.await
.expect("Cannot fetch current pool index")
.unwrap_or_default()
......
......@@ -113,10 +113,42 @@ pub async fn run_and_save(client: &api::Client, settings: &Settings) {
"Cannot write distance evaluation result to file `{evaluation_result_path:?}`: {e:?}"
)
});
<<<<<<< HEAD
=======
// 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.
settings
.evaluation_result_dir
.read_dir()
.unwrap_or_else(|e| {
panic!(
"Cannot read distance evaluation result directory `{:?}`: {:?}",
settings.evaluation_result_dir, e
)
})
.flatten()
.filter_map(|entry| {
entry
.file_name()
.into_string()
.ok()
.and_then(|name| {
name.parse::<isize>().ok().filter(|&pool| {
pool != current_period_index as isize
&& pool != (current_period_index + 1) as isize
})
})
.map(|_| entry.path())
})
.for_each(|path| {
std::fs::remove_file(&path)
.unwrap_or_else(|e| warn!("Cannot remove file `{:?}`: {:?}", path, e));
});
>>>>>>> 8925cfd4 (fix #262)
}
/// 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(
client: &api::Client,
settings: &Settings,
......@@ -126,10 +158,11 @@ pub async fn run(
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
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 {
info!("Nothing to do: Pool does not exist");
return None;
......@@ -143,7 +176,11 @@ pub async fn run(
let evaluation_result_path = settings
.evaluation_result_dir
<<<<<<< HEAD
.join(((current_pool_index + 1) % 3).to_string());
=======
.join((current_period_index + 1).to_string());
>>>>>>> 8925cfd4 (fix #262)
if handle_fs {
// Stop if already evaluated
......@@ -163,7 +200,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;
// member idty -> issued certs
......@@ -219,7 +256,7 @@ pub async fn run(
.map(|(idty, _)| distance_rule(&received_certs, &referees, max_depth, *idty))
.collect();
Some((evaluation, current_pool_index, evaluation_result_path))
Some((evaluation, current_period_index, evaluation_result_path))
}
fn distance_rule_recursive(
......
......@@ -46,7 +46,7 @@ pub async fn parent_hash(_client: &Client) -> H256 {
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
}
......
......@@ -186,7 +186,7 @@ mod benchmarks {
#[benchmark]
fn do_evaluation_success() -> Result<(), BenchmarkError> {
// Benchmarking do_evaluation in case of a single success.
CurrentPoolIndex::<T>::put(0);
CurrentPeriodIndex::<T>::put(0);
// More than membership renewal to avoid antispam
frame_system::pallet::Pallet::<T>::set_block_number(500_000_000u32.into());
let idty = T::IdtyIndex::one();
......@@ -203,7 +203,7 @@ mod benchmarks {
.into(),
);
CurrentPoolIndex::<T>::put(2);
CurrentPeriodIndex::<T>::put(2);
Pallet::<T>::force_update_evaluation(
RawOrigin::Root.into(),
caller,
......@@ -230,7 +230,7 @@ mod benchmarks {
#[benchmark]
fn do_evaluation_failure() -> Result<(), BenchmarkError> {
// Benchmarking do_evaluation in case of a single failure.
CurrentPoolIndex::<T>::put(0);
CurrentPeriodIndex::<T>::put(0);
// More than membership renewal to avoid antispam
frame_system::pallet::Pallet::<T>::set_block_number(500_000_000u32.into());
let idty = T::IdtyIndex::one();
......@@ -247,7 +247,7 @@ mod benchmarks {
.into(),
);
CurrentPoolIndex::<T>::put(2);
CurrentPeriodIndex::<T>::put(2);
Pallet::<T>::force_update_evaluation(
RawOrigin::Root.into(),
caller,
......
......@@ -234,10 +234,10 @@ pub mod pallet {
#[pallet::storage]
pub(super) type DidUpdate<T: Config> = StorageValue<_, bool, ValueQuery>;
/// The current evaluation pool index.
/// The current evaluation period index.
#[pallet::storage]
#[pallet::getter(fn current_pool_index)]
pub(super) type CurrentPoolIndex<T: Config> = StorageValue<_, u32, ValueQuery>;
#[pallet::getter(fn current_period_index)]
pub(super) type CurrentPeriodIndex<T: Config> = StorageValue<_, u32, ValueQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
......@@ -300,7 +300,7 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
CurrentPoolIndex::<T>::put(0u32);
CurrentPeriodIndex::<T>::put(0u32);
}
}
......@@ -314,10 +314,10 @@ pub mod pallet {
if block % BlockNumberFor::<T>::one().saturating_mul(T::EvaluationPeriod::get().into())
== BlockNumberFor::<T>::zero()
{
let index = (CurrentPoolIndex::<T>::get() + 1) % 3;
CurrentPoolIndex::<T>::put(index);
let index = CurrentPeriodIndex::<T>::get() + 1;
CurrentPeriodIndex::<T>::put(index);
weight = weight
.saturating_add(Self::do_evaluation(index))
.saturating_add(Self::do_evaluation(index % 3))
.saturating_add(T::DbWeight::get().reads_writes(1, 1));
}
weight.saturating_add(<T as pallet::Config>::WeightInfo::on_finalize())
......@@ -557,7 +557,7 @@ pub mod pallet {
who: &T::AccountId,
idty_index: <T as pallet_identity::Config>::IdtyIndex,
) -> 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
ensure!(
current_pool.evaluations.len() < (MAX_EVALUATIONS_PER_SESSION as usize),
......@@ -590,7 +590,7 @@ pub mod pallet {
evaluator: <T as frame_system::Config>::AccountId,
computation_result: ComputationResult,
) -> 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)
ensure!(
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