From 8a8c1af6c57bffd2b33eb1452db64a34d2dc7762 Mon Sep 17 00:00:00 2001 From: bgallois <benjamin@gallois.cc> Date: Tue, 24 Oct 2023 19:25:22 +0200 Subject: [PATCH] fix(pallet_identity): fix benchmarks --- pallets/distance/src/lib.rs | 21 +++++++++++++++++++++ pallets/identity/src/benchmarking.rs | 1 + pallets/identity/src/lib.rs | 3 +++ pallets/identity/src/traits.rs | 5 +++++ runtime/common/src/pallets_config.rs | 2 ++ runtime/common/src/providers.rs | 21 +++++++++++++++++++++ 6 files changed, 53 insertions(+) diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index 3ed6ab290..92ed6acc0 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -276,6 +276,27 @@ pub mod pallet { } } + // BENCHMARK FUNCTIONS // + + impl<T: Config> Pallet<T> { + /// Force the distance status using IdtyIndex and AccountId + /// only to prepare identity for benchmarking. + pub fn set_distance_status( + identity: <T as pallet_identity::Config>::IdtyIndex, + status: Option<(<T as frame_system::Config>::AccountId, DistanceStatus)>, + ) -> DispatchResult { + IdentityDistanceStatus::<T>::set(identity, status); + DistanceStatusExpireOn::<T>::mutate( + pallet_session::CurrentIndex::<T>::get() + T::ResultExpiration::get(), + move |identities| { + identities + .try_push(identity) + .map_err(|_| Error::<T>::ManyEvaluationsInBlock.into()) + }, + ) + } + } + // INTERNAL FUNCTIONS // impl<T: Config> Pallet<T> { diff --git a/pallets/identity/src/benchmarking.rs b/pallets/identity/src/benchmarking.rs index 1cee79c28..377826bbe 100644 --- a/pallets/identity/src/benchmarking.rs +++ b/pallets/identity/src/benchmarking.rs @@ -147,6 +147,7 @@ benchmarks! { let owner_key: T::AccountId = Identities::<T>::get(index).unwrap().owner_key; let owner_key_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(owner_key.clone()).into(); Pallet::<T>::confirm_identity(owner_key_origin.clone(), name.clone())?; + T::DistanceHandler::force_status_ok(&index, &owner_key); }: _<T::RuntimeOrigin>(caller_origin, index) verify { assert_has_event::<T>(Event::<T>::IdtyValidated { idty_index: index }.into()); diff --git a/pallets/identity/src/lib.rs b/pallets/identity/src/lib.rs index 25733edb5..99467103a 100644 --- a/pallets/identity/src/lib.rs +++ b/pallets/identity/src/lib.rs @@ -114,6 +114,9 @@ pub mod pallet { type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; /// Type representing the weight of this pallet type WeightInfo: WeightInfo; + /// Type representing the a distance handler to prepare identity for benchmarking + #[cfg(feature = "runtime-benchmarks")] + type DistanceHandler: SetDistance<Self::IdtyIndex, Self::AccountId>; } // GENESIS STUFF // diff --git a/pallets/identity/src/traits.rs b/pallets/identity/src/traits.rs index 54b50a06c..508cd0030 100644 --- a/pallets/identity/src/traits.rs +++ b/pallets/identity/src/traits.rs @@ -76,3 +76,8 @@ impl<IndtyIndex> RemoveIdentityConsumers<IndtyIndex> for () { Weight::zero() } } + +#[cfg(feature = "runtime-benchmarks")] +pub trait SetDistance<IndtyIndex, AccountId> { + fn force_status_ok(idty_index: &IndtyIndex, account: &AccountId) -> (); +} diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index 665bd8193..19e844796 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -454,6 +454,8 @@ parameter_types! { type RevocationSignature = Signature; type RuntimeEvent = RuntimeEvent; type WeightInfo = common_runtime::weights::pallet_identity::WeightInfo<Runtime>; + #[cfg(feature = "runtime-benchmarks")] + type DistanceHandler = common_runtime::providers::DistanceHandler<Runtime>; } impl pallet_membership::Config<frame_support::instances::Instance1> for Runtime { diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs index 151f70ece..22de128c7 100644 --- a/runtime/common/src/providers.rs +++ b/runtime/common/src/providers.rs @@ -121,3 +121,24 @@ where ) } } + +#[cfg(feature = "runtime-benchmarks")] +pub struct DistanceHandler<T>(PhantomData<T>); + +#[cfg(feature = "runtime-benchmarks")] +impl<T> + pallet_identity::traits::SetDistance<<T as pallet_identity::Config>::IdtyIndex, T::AccountId> + for DistanceHandler<T> +where + T: pallet_distance::Config, +{ + fn force_status_ok( + idty_id: &<T as pallet_identity::Config>::IdtyIndex, + account: &<T as frame_system::Config>::AccountId, + ) -> () { + let _ = pallet_distance::Pallet::<T>::set_distance_status( + *idty_id, + Some((account.clone(), pallet_distance::DistanceStatus::Valid)), + ); + } +} -- GitLab