From 50f0ef5bd367d1acf4ad0088c2050e86ac16f5f6 Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Wed, 20 Mar 2024 14:27:55 +0100
Subject: [PATCH] refactor distance result

---
 pallets/distance/src/lib.rs | 52 ++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 30 deletions(-)

diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs
index 2bd649f35..76d580b2d 100644
--- a/pallets/distance/src/lib.rs
+++ b/pallets/distance/src/lib.rs
@@ -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
         }
-- 
GitLab