Skip to content
Snippets Groups Projects
Commit fe571d88 authored by Benjamin Gallois's avatar Benjamin Gallois Committed by Hugo Trentesaux
Browse files

Fix benchmarks (!185)

* refac(benchmark) refactore BenchmarkSetupHandler

* fix(pallet_membership): fix benchmarks

* fix(pallet_identity): fix benchmarks
parent 5db045f3
No related branches found
No related tags found
1 merge request!185Fix benchmarks
Pipeline #33679 waiting for manual action
......@@ -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> {
......
......@@ -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());
......
......@@ -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 //
......
......@@ -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.
......
......@@ -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) -> () {}
}
......@@ -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());
......
......@@ -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 //
......
......@@ -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.
......
......@@ -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 {
......
......@@ -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>);
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