Skip to content
Snippets Groups Projects
Commit 71f940e2 authored by bgallois's avatar bgallois Committed by Hugo Trentesaux
Browse files

refactor distance result

parent 1c35dc5d
Branches
Tags
1 merge request!257Fix #219 and #220
...@@ -542,39 +542,23 @@ pub mod pallet { ...@@ -542,39 +542,23 @@ pub mod pallet {
> = Pallet::<T>::take_current_pool(index); > = Pallet::<T>::take_current_pool(index);
for (idty, median_acc) in current_pool.evaluations.into_iter() { for (idty, median_acc) in current_pool.evaluations.into_iter() {
// distance result let mut distance_result: Option<Perbill> = None;
let mut distance_result: Option<bool> = None; // Retrieve the result of the computation from the median accumulator
let mut distance = Perbill::zero();
// get result of the computation
if let Some(median_result) = median_acc.get_median() { if let Some(median_result) = median_acc.get_median() {
distance = match median_result { let distance = match median_result {
MedianResult::One(m) => m, MedianResult::One(m) => m,
MedianResult::Two(m1, m2) => m1 + (m2 - m1) / 2, // Avoid overflow (since max is 1) MedianResult::Two(m1, m2) => m1 + (m2 - m1) / 2, // Avoid overflow (since max is 1)
}; };
// update distance result // Update distance result
distance_result = Some(distance >= T::MinAccessibleReferees::get()); distance_result = Some(distance);
} }
// take requester and perform unreserve or slash // If there's a pending evaluation request with the provided identity
if let Some(requester) = PendingEvaluationRequest::<T>::take(idty) { if let Some(requester) = PendingEvaluationRequest::<T>::take(idty) {
match distance_result { // If distance_result is available
None => { if let Some(distance) = distance_result {
// no result, unreserve if distance >= T::MinAccessibleReferees::get() {
T::Currency::unreserve( // Positive result, unreserve and apply
&requester,
<T as Config>::EvaluationPrice::get(),
);
weight = weight.saturating_add(
<T as pallet::Config>::WeightInfo::do_evaluation_failure()
.saturating_sub(
<T as pallet::Config>::WeightInfo::do_evaluation_overhead(),
),
);
}
Some(true) => {
// positive result, unreserve and apply
T::Currency::unreserve( T::Currency::unreserve(
&requester, &requester,
<T as Config>::EvaluationPrice::get(), <T as Config>::EvaluationPrice::get(),
...@@ -586,9 +570,8 @@ pub mod pallet { ...@@ -586,9 +570,8 @@ pub mod pallet {
<T as pallet::Config>::WeightInfo::do_evaluation_overhead(), <T as pallet::Config>::WeightInfo::do_evaluation_overhead(),
), ),
); );
} } else {
Some(false) => { // Negative result, slash and deposit event
// negative result, slash and deposit event
let _ = T::Currency::slash_reserved( let _ = T::Currency::slash_reserved(
&requester, &requester,
<T as Config>::EvaluationPrice::get(), <T as Config>::EvaluationPrice::get(),
...@@ -604,9 +587,18 @@ pub mod pallet { ...@@ -604,9 +587,18 @@ pub mod pallet {
), ),
); );
} }
} else {
// No result, unreserve
T::Currency::unreserve(&requester, <T as Config>::EvaluationPrice::get());
weight = weight.saturating_add(
<T as pallet::Config>::WeightInfo::do_evaluation_failure()
.saturating_sub(
<T as pallet::Config>::WeightInfo::do_evaluation_overhead(),
),
);
} }
} }
// if evaluation happened without request, it's ok to do nothing // If evaluation happened without request, it's ok to do nothing
} }
weight weight
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment