From 9fa64f18a3e91c3959aaea75ddce01fc0a722a42 Mon Sep 17 00:00:00 2001 From: bgallois <benjamin@gallois.cc> Date: Tue, 19 Mar 2024 11:58:37 +0100 Subject: [PATCH] fix https://git.duniter.org/nodes/rust/duniter-v2s/-/issues/219 --- pallets/distance/src/benchmarking.rs | 24 ++++++++++++++--- pallets/distance/src/lib.rs | 34 ++++++++++++++++++------- runtime/common/src/providers.rs | 5 +++- runtime/gdev/tests/integration_tests.rs | 5 +++- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/pallets/distance/src/benchmarking.rs b/pallets/distance/src/benchmarking.rs index 7b8d7c850..cf11d85e3 100644 --- a/pallets/distance/src/benchmarking.rs +++ b/pallets/distance/src/benchmarking.rs @@ -163,7 +163,13 @@ mod benchmarks { #[extrinsic_call] _(RawOrigin::Root, idty); - assert_has_event::<T>(Event::<T>::EvaluatedValid { idty_index: idty }.into()); + assert_has_event::<T>( + Event::<T>::EvaluatedValid { + idty_index: idty, + distance: Perbill::one(), + } + .into(), + ); } #[benchmark] @@ -211,7 +217,13 @@ mod benchmarks { Pallet::<T>::do_evaluation(0); } - assert_has_event::<T>(Event::<T>::EvaluatedValid { idty_index: idty }.into()); + assert_has_event::<T>( + Event::<T>::EvaluatedValid { + idty_index: idty, + distance: Perbill::one(), + } + .into(), + ); Ok(()) } @@ -249,7 +261,13 @@ mod benchmarks { Pallet::<T>::do_evaluation(0); } - assert_has_event::<T>(Event::<T>::EvaluatedInvalid { idty_index: idty }.into()); + assert_has_event::<T>( + Event::<T>::EvaluatedInvalid { + idty_index: idty, + distance: Perbill::zero(), + } + .into(), + ); Ok(()) } diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index f42651850..2bd649f35 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -172,9 +172,15 @@ pub mod pallet { who: T::AccountId, }, /// Distance rule was found valid. - EvaluatedValid { idty_index: T::IdtyIndex }, + EvaluatedValid { + idty_index: T::IdtyIndex, + distance: Perbill, + }, /// Distance rule was found invalid. - EvaluatedInvalid { idty_index: T::IdtyIndex }, + EvaluatedInvalid { + idty_index: T::IdtyIndex, + distance: Perbill, + }, } // ERRORS // @@ -313,7 +319,7 @@ pub mod pallet { ) -> DispatchResult { ensure_root(origin)?; - Self::do_valid_distance_status(identity); + Self::do_valid_distance_status(identity, Perbill::one()); Ok(()) } } @@ -509,11 +515,17 @@ pub mod pallet { } /// Set the distance status using IdtyIndex and AccountId - pub fn do_valid_distance_status(idty: <T as pallet_identity::Config>::IdtyIndex) { + pub fn do_valid_distance_status( + idty: <T as pallet_identity::Config>::IdtyIndex, + distance: Perbill, + ) { // callback T::OnValidDistanceStatus::on_valid_distance_status(idty); // deposit event - Self::deposit_event(Event::EvaluatedValid { idty_index: idty }); + Self::deposit_event(Event::EvaluatedValid { + idty_index: idty, + distance, + }); } pub fn do_evaluation(index: u32) -> Weight { @@ -532,15 +544,16 @@ pub mod pallet { 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 if let Some(median_result) = median_acc.get_median() { - let median = match median_result { + 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(median >= T::MinAccessibleReferees::get()); + distance_result = Some(distance >= T::MinAccessibleReferees::get()); } // take requester and perform unreserve or slash @@ -566,7 +579,7 @@ pub mod pallet { &requester, <T as Config>::EvaluationPrice::get(), ); - Self::do_valid_distance_status(idty); + Self::do_valid_distance_status(idty, distance); weight = weight.saturating_add( <T as pallet::Config>::WeightInfo::do_evaluation_success() .saturating_sub( @@ -580,7 +593,10 @@ pub mod pallet { &requester, <T as Config>::EvaluationPrice::get(), ); - Self::deposit_event(Event::EvaluatedInvalid { idty_index: idty }); + Self::deposit_event(Event::EvaluatedInvalid { + idty_index: idty, + distance, + }); weight = weight.saturating_add( <T as pallet::Config>::WeightInfo::do_evaluation_failure() .saturating_sub( diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs index c947ce588..829be1a07 100644 --- a/runtime/common/src/providers.rs +++ b/runtime/common/src/providers.rs @@ -97,7 +97,10 @@ macro_rules! impl_benchmark_setup_handler { <T as pallet_certification::Config>::IdtyIndex: From<u32>, { fn force_valid_distance_status(idty_id: &IdtyIndex) -> () { - let _ = pallet_distance::Pallet::<T>::do_valid_distance_status(*idty_id); + let _ = pallet_distance::Pallet::<T>::do_valid_distance_status( + *idty_id, + sp_runtime::Perbill::one(), + ); } fn add_cert(issuer: &IdtyIndex, receiver: &IdtyIndex) { diff --git a/runtime/gdev/tests/integration_tests.rs b/runtime/gdev/tests/integration_tests.rs index b91e0ee90..b4ed381a4 100644 --- a/runtime/gdev/tests/integration_tests.rs +++ b/runtime/gdev/tests/integration_tests.rs @@ -373,7 +373,10 @@ fn test_validate_identity_when_claim() { // Pass 3rd evaluation period run_to_block(3 * eval_period); System::assert_has_event(RuntimeEvent::Distance( - pallet_distance::Event::EvaluatedValid { idty_index: 5 }, + pallet_distance::Event::EvaluatedValid { + idty_index: 5, + distance: Perbill::one(), + }, )); // eve can not claim her membership manually because it is done automatically -- GitLab