diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index 3ed6ab2902d6424d0963a1f844f5a1ed51d37729..92ed6acc00b1444f63491dab40d48d4d24d52244 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 1cee79c284a31d3d7b3cb6edcc892b80becee4ca..27894c2aad0611f505e56cd91cfcb5dbf7fe2315 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::BenchmarkSetupHandler::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 25733edb5dcf702b504fed68b751405a2856be64..46b953a075d3848f7a29c0244b7f1512c8cdd8aa 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 BenchmarkSetupHandler: SetDistance<Self::IdtyIndex, Self::AccountId>; } // GENESIS STUFFĂ‚ // diff --git a/pallets/identity/src/mock.rs b/pallets/identity/src/mock.rs index ce6032d08c8059ab0935ce54a21d2b68961b0229..7621c49555059f9e1113b2f9029e810d5e0f8c7b 100644 --- a/pallets/identity/src/mock.rs +++ b/pallets/identity/src/mock.rs @@ -117,6 +117,8 @@ impl pallet_identity::Config for Test { type RevocationSignature = Signature; type RuntimeEvent = RuntimeEvent; type WeightInfo = (); + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkSetupHandler = (); } // Build genesis storage according to the mock runtime. diff --git a/pallets/identity/src/traits.rs b/pallets/identity/src/traits.rs index 54b50a06c4e6e660779b2a6eda7b09c73ebc3f25..1d8dd0152ae9c14d137ae9b3965cf44ed99a1d2d 100644 --- a/pallets/identity/src/traits.rs +++ b/pallets/identity/src/traits.rs @@ -76,3 +76,13 @@ 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) -> (); +} + +#[cfg(feature = "runtime-benchmarks")] +impl<IdtyIndex, AccountId> SetDistance<IdtyIndex, AccountId> for () { + fn force_status_ok(_idty_id: &IdtyIndex, _account: &AccountId) -> () {} +} diff --git a/pallets/membership/src/benchmarking.rs b/pallets/membership/src/benchmarking.rs index 9b0ad18d041b5dfb8dba4c0936d4e234cffc76dc..3021d59f50069fe39e22ad381a9c295a4377d06a 100644 --- a/pallets/membership/src/benchmarking.rs +++ b/pallets/membership/src/benchmarking.rs @@ -56,19 +56,23 @@ benchmarks_instance_pallet! { } } claim_membership { - let idty: T::IdtyId = 3.into(); + let idty_index: u32 = 3; + let idty: T::IdtyId = idty_index.into(); Membership::<T, I>::take(idty); PendingMembership::<T, I>::insert(idty.clone(), T::MetaData::default()); let caller: T::AccountId = T::AccountIdOf::convert(idty.clone()).unwrap(); let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + T::BenchmarkSetupHandler::force_status_ok(&idty_index, &caller); }: _<T::RuntimeOrigin>(caller_origin) verify { assert_has_event::<T, I>(Event::<T, I>::MembershipAcquired(idty).into()); } renew_membership { - let idty: T::IdtyId = 3.into(); + let idty_index: u32 = 3; + let idty: T::IdtyId = idty_index.into(); let caller: T::AccountId = T::AccountIdOf::convert(idty.clone()).unwrap(); let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + T::BenchmarkSetupHandler::force_status_ok(&idty_index, &caller); }: _<T::RuntimeOrigin>(caller_origin) verify { assert_has_event::<T, I>(Event::<T, I>::MembershipRenewed(idty).into()); diff --git a/pallets/membership/src/lib.rs b/pallets/membership/src/lib.rs index 8fec34f89f39d6656565a09d01d07b348ad4acf1..6a90624e7c44eff1a8dfd8a20b612e348d8135cd 100644 --- a/pallets/membership/src/lib.rs +++ b/pallets/membership/src/lib.rs @@ -42,6 +42,16 @@ use sp_std::prelude::*; #[cfg(feature = "std")] use std::collections::BTreeMap; +#[cfg(feature = "runtime-benchmarks")] +pub trait SetDistance<IdtyId, AccountId> { + fn force_status_ok(idty_index: &IdtyId, account: &AccountId) -> (); +} + +#[cfg(feature = "runtime-benchmarks")] +impl<IdtyId, AccountId> SetDistance<IdtyId, AccountId> for () { + fn force_status_ok(_idty_id: &IdtyId, _account: &AccountId) -> () {} +} + #[frame_support::pallet] pub mod pallet { use super::*; @@ -83,6 +93,8 @@ pub mod pallet { type RuntimeEvent: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; type WeightInfo: WeightInfo; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkSetupHandler: SetDistance<u32, Self::AccountId>; } // GENESIS STUFFĂ‚ // diff --git a/pallets/membership/src/mock.rs b/pallets/membership/src/mock.rs index b4a65ac6a33a5779c5fe77df9f95b06a26180393..d97f1f86ab0e480c6a3fa439b11af5a7939f8f18 100644 --- a/pallets/membership/src/mock.rs +++ b/pallets/membership/src/mock.rs @@ -93,6 +93,8 @@ impl pallet_membership::Config for Test { type PendingMembershipPeriod = PendingMembershipPeriod; type RuntimeEvent = RuntimeEvent; type WeightInfo = (); + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkSetupHandler = (); } // Build genesis storage according to the mock runtime. diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index 665bd81935479198b11c3ef011f0b9ec0c20fd82..e5aaa8c3ff5d82da3d25b61f03e694f0422ec58b 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 BenchmarkSetupHandler = common_runtime::providers::BenchmarkSetupHandler<Runtime>; } impl pallet_membership::Config<frame_support::instances::Instance1> for Runtime { @@ -467,6 +469,8 @@ parameter_types! { type PendingMembershipPeriod = PendingMembershipPeriod; type RuntimeEvent = RuntimeEvent; type WeightInfo = common_runtime::weights::pallet_membership_membership::WeightInfo<Runtime>; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkSetupHandler = common_runtime::providers::BenchmarkSetupHandler<Runtime>; } impl pallet_certification::Config<Instance1> for Runtime { @@ -514,6 +518,8 @@ parameter_types! { type PendingMembershipPeriod = SmithPendingMembershipPeriod; type RuntimeEvent = RuntimeEvent; type WeightInfo = common_runtime::weights::pallet_membership_smith_membership::WeightInfo<Runtime>; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkSetupHandler = common_runtime::providers::BenchmarkSetupHandler<Runtime>; } impl pallet_certification::Config<Instance2> for Runtime { diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs index 151f70ece87da144afce3ebca0f12f04ecba6b3e..e8ddf8102dbae4d471b35a370260bdba7c76b6f8 100644 --- a/runtime/common/src/providers.rs +++ b/runtime/common/src/providers.rs @@ -121,3 +121,34 @@ where ) } } + +#[cfg(feature = "runtime-benchmarks")] +pub struct BenchmarkSetupHandler<T>(PhantomData<T>); + +// Macro implementing the BenchmarkSetupHandler trait for pallets requiring identity preparation for benchmarks. +#[cfg(feature = "runtime-benchmarks")] +macro_rules! impl_benchmark_setup_handler { + ($t:ty) => { + impl<T> $t for BenchmarkSetupHandler<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)), + ); + } + // TODO: All the required preparation for the benchmarks, depending on the coupling + // between pallets, would be implemented here when moving away from prepared identities from the gdev-benchmark. + } + }; +} + +#[cfg(feature = "runtime-benchmarks")] +impl_benchmark_setup_handler!(pallet_membership::SetDistance<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>); +#[cfg(feature = "runtime-benchmarks")] +impl_benchmark_setup_handler!(pallet_identity::traits::SetDistance<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>);