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
No related branches found
No related tags found
1 merge request!257Fix #219 and #220
......@@ -542,39 +542,23 @@ pub mod pallet {
> = Pallet::<T>::take_current_pool(index);
for (idty, median_acc) in current_pool.evaluations.into_iter() {
// distance result
let mut distance_result: Option<bool> = None;
let mut distance = Perbill::zero();
// get result of the computation
let mut distance_result: Option<Perbill> = None;
// Retrieve the result of the computation from the median accumulator
if let Some(median_result) = median_acc.get_median() {
distance = match median_result {
let distance = match median_result {
MedianResult::One(m) => m,
MedianResult::Two(m1, m2) => m1 + (m2 - m1) / 2, // Avoid overflow (since max is 1)
};
// update distance result
distance_result = Some(distance >= T::MinAccessibleReferees::get());
// Update distance result
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) {
match distance_result {
None => {
// 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(),
),
);
}
Some(true) => {
// positive result, unreserve and apply
// If distance_result is available
if let Some(distance) = distance_result {
if distance >= T::MinAccessibleReferees::get() {
// Positive result, unreserve and apply
T::Currency::unreserve(
&requester,
<T as Config>::EvaluationPrice::get(),
......@@ -586,9 +570,8 @@ pub mod pallet {
<T as pallet::Config>::WeightInfo::do_evaluation_overhead(),
),
);
}
Some(false) => {
// negative result, slash and deposit event
} else {
// Negative result, slash and deposit event
let _ = T::Currency::slash_reserved(
&requester,
<T as Config>::EvaluationPrice::get(),
......@@ -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
}
......
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