Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update benchmarks #790

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
3 changes: 3 additions & 0 deletions dip-template/runtimes/dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ impl pallet_web3_names::Config for Runtime {
type Web3Name = Web3Name;
type Web3NameOwner = DidIdentifier;
type WeightInfo = weights::pallet_web3_names::WeightInfo<Runtime>;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
3 changes: 3 additions & 0 deletions pallets/pallet-migration/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ pub mod runtime {
type Web3NameOwner = TestWeb3NameOwner;
type WeightInfo = ();
type BalanceMigrationManager = Migration;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

#[derive(
Expand Down
60 changes: 33 additions & 27 deletions pallets/pallet-web3-names/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use frame_support::{
};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use sp_runtime::app_crypto::sr25519;
use sp_std::{vec, vec::Vec};
use sp_std::vec::Vec;

use kilt_support::{traits::GenerateBenchmarkOrigin, Deposit};

Expand All @@ -38,6 +38,16 @@ use crate::{
Web3NameOwnerOf,
};

pub trait BenchmarkHelper {
fn generate_name_input_with_length(length: usize) -> Vec<u8>;
}

impl BenchmarkHelper for () {
fn generate_name_input_with_length(length: usize) -> Vec<u8> {
sp_std::vec![b'a'; length]
}
}

const CALLER_SEED: u32 = 0;
const OWNER_SEED: u32 = 1;

Expand All @@ -53,28 +63,24 @@ where
CurrencyOf::<T, I>::set_balance(account, balance);
}

fn generate_web3_name_input(length: usize) -> Vec<u8> {
vec![b'1'; length]
}

benchmarks_instance_pallet! {
where_clause {
where
T::AccountId: From<sr25519::Public>,
T::Web3NameOwner: From<T::AccountId>,
T::OwnerOrigin: GenerateBenchmarkOrigin<T::RuntimeOrigin, T::AccountId, T::Web3NameOwner>,
T::BanOrigin: EnsureOrigin<T::RuntimeOrigin>,
<T as Config<I>>::Web3NameOwner: From<T::AccountId>,
<T as Config<I>>::OwnerOrigin: GenerateBenchmarkOrigin<T::RuntimeOrigin, T::AccountId, <T as Config<I>>::Web3NameOwner>,
<T as Config<I>>::BanOrigin: EnsureOrigin<T::RuntimeOrigin>,
<<T as Config<I>>::Web3Name as TryFrom<Vec<u8>>>::Error: Into<Error<T, I>>,
<T as Config<I>>::Currency: Mutate<T::AccountId>,
}

claim {
let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get());
let n in (<T as Config<I>>::MinNameLength::get()) .. (<T as Config<I>>::MaxNameLength::get());
let caller: AccountIdOf<T> = account("caller", 0, CALLER_SEED);
let owner: Web3NameOwnerOf<T, I> = account("owner", 0, OWNER_SEED);
let web3_name_input: BoundedVec<u8, T::MaxNameLength> = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input: BoundedVec<u8, <T as Config<I>>::MaxNameLength> = BoundedVec::try_from(<T as Config<I>>::BenchmarkHelper::generate_name_input_with_length(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input_clone = web3_name_input.clone();
let origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone());
let origin = <T as Config<I>>::OwnerOrigin::generate_origin(caller.clone(), owner.clone());

make_free_for_did::<T, I>(&caller);
}: _<T::RuntimeOrigin>(origin, web3_name_input_clone)
Expand All @@ -89,8 +95,8 @@ benchmarks_instance_pallet! {
release_by_owner {
let caller: AccountIdOf<T> = account("caller", 0, CALLER_SEED);
let owner: Web3NameOwnerOf<T, I> = account("owner", 0, OWNER_SEED);
let web3_name_input: BoundedVec<u8, T::MaxNameLength> = BoundedVec::try_from(generate_web3_name_input(T::MaxNameLength::get().saturated_into())).expect("BoundedVec creation should not fail.");
let origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone());
let web3_name_input: BoundedVec<u8, <T as Config<I>>::MaxNameLength> = BoundedVec::try_from(<T as Config<I>>::BenchmarkHelper::generate_name_input_with_length(<T as Config<I>>::MaxNameLength::get().saturated_into())).expect("BoundedVec creation should not fail.");
let origin = <T as Config<I>>::OwnerOrigin::generate_origin(caller.clone(), owner.clone());

make_free_for_did::<T, I>(&caller);
Pallet::<T, I>::claim(origin.clone(), web3_name_input.clone()).expect("Should register the claimed web3 name.");
Expand All @@ -104,12 +110,12 @@ benchmarks_instance_pallet! {
}

reclaim_deposit {
let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get());
let n in (<T as Config<I>>::MinNameLength::get()) .. (<T as Config<I>>::MaxNameLength::get());
let caller: AccountIdOf<T> = account("caller", 0, CALLER_SEED);
let owner: Web3NameOwnerOf<T, I> = account("owner", 0, OWNER_SEED);
let web3_name_input: BoundedVec<u8, T::MaxNameLength> = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input: BoundedVec<u8, <T as Config<I>>::MaxNameLength> = BoundedVec::try_from(<T as Config<I>>::BenchmarkHelper::generate_name_input_with_length(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input_clone = web3_name_input.clone();
let did_origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone());
let did_origin = <T as Config<I>>::OwnerOrigin::generate_origin(caller.clone(), owner.clone());
let signed_origin = RawOrigin::Signed(caller.clone());

make_free_for_did::<T, I>(&caller);
Expand All @@ -124,12 +130,12 @@ benchmarks_instance_pallet! {
}

ban {
let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get());
let n in (<T as Config<I>>::MinNameLength::get()) .. (<T as Config<I>>::MaxNameLength::get());
let caller: AccountIdOf<T> = account("caller", 0, CALLER_SEED);
let owner: Web3NameOwnerOf<T, I> = account("owner", 0, OWNER_SEED);
let web3_name_input: BoundedVec<u8, T::MaxNameLength> = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input: BoundedVec<u8, <T as Config<I>>::MaxNameLength> = BoundedVec::try_from(<T as Config<I>>::BenchmarkHelper::generate_name_input_with_length(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input_clone = web3_name_input.clone();
let did_origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone());
let did_origin = <T as Config<I>>::OwnerOrigin::generate_origin(caller.clone(), owner.clone());
let ban_origin = RawOrigin::Root;

make_free_for_did::<T, I>(&caller);
Expand All @@ -145,10 +151,10 @@ benchmarks_instance_pallet! {
}

unban {
let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get());
let n in (<T as Config<I>>::MinNameLength::get()) .. (<T as Config<I>>::MaxNameLength::get());
let caller: AccountIdOf<T> = account("caller", 0, CALLER_SEED);
let owner: Web3NameOwnerOf<T, I> = account("owner", 0, OWNER_SEED);
let web3_name_input: BoundedVec<u8, T::MaxNameLength> = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input: BoundedVec<u8, <T as Config<I>>::MaxNameLength> = BoundedVec::try_from(<T as Config<I>>::BenchmarkHelper::generate_name_input_with_length(n.saturated_into())).expect("BoundedVec creation should not fail.");
let web3_name_input_clone = web3_name_input.clone();
let ban_origin = RawOrigin::Root;

Expand All @@ -168,17 +174,17 @@ benchmarks_instance_pallet! {
let deposit_owner_old: AccountIdOf<T> = account("caller", 0, CALLER_SEED);
let deposit_owner_new: AccountIdOf<T> = account("caller", 1, CALLER_SEED);
let owner: Web3NameOwnerOf<T, I> = account("owner", 0, OWNER_SEED);
let web3_name_input: BoundedVec<u8, T::MaxNameLength> = BoundedVec::try_from(
generate_web3_name_input(T::MaxNameLength::get().saturated_into())
let web3_name_input: BoundedVec<u8, <T as Config<I>>::MaxNameLength> = BoundedVec::try_from(
<T as Config<I>>::BenchmarkHelper::generate_name_input_with_length(<T as Config<I>>::MaxNameLength::get().saturated_into())
).expect("BoundedVec creation should not fail.");
let web3_name_input_clone = web3_name_input.clone();
let origin_create = T::OwnerOrigin::generate_origin(deposit_owner_old.clone(), owner.clone());
let origin_create = <T as Config<I>>::OwnerOrigin::generate_origin(deposit_owner_old.clone(), owner.clone());

make_free_for_did::<T, I>(&deposit_owner_old);
make_free_for_did::<T, I>(&deposit_owner_new);
Pallet::<T, I>::claim(origin_create, web3_name_input.clone()).expect("Should register the claimed web3 name.");

let origin = T::OwnerOrigin::generate_origin(deposit_owner_new.clone(), owner);
let origin = <T as Config<I>>::OwnerOrigin::generate_origin(deposit_owner_new.clone(), owner);
}: _<T::RuntimeOrigin>(origin)
verify {
let Ok(web3_name) = Web3NameOf::<T, I>::try_from(web3_name_input.to_vec()) else {
Expand All @@ -193,8 +199,8 @@ benchmarks_instance_pallet! {
update_deposit {
let deposit_owner: AccountIdOf<T> = account("caller", 0, CALLER_SEED);
let owner: Web3NameOwnerOf<T, I> = account("owner", 0, OWNER_SEED);
let web3_name_input: BoundedVec<u8, T::MaxNameLength> = BoundedVec::try_from(
generate_web3_name_input(T::MaxNameLength::get().saturated_into())
let web3_name_input: BoundedVec<u8, <T as Config<I>>::MaxNameLength> = BoundedVec::try_from(
<T as Config<I>>::BenchmarkHelper::generate_name_input_with_length(<T as Config<I>>::MaxNameLength::get().saturated_into())
).expect("BoundedVec creation should not fail.");
let Ok(web3_name) = Web3NameOf::<T, I>::try_from(web3_name_input.to_vec()) else {
panic!();
Expand Down
5 changes: 5 additions & 0 deletions pallets/pallet-web3-names/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::BenchmarkHelper;

pub use crate::{default_weights::WeightInfo, pallet::*};

Expand Down Expand Up @@ -149,6 +151,9 @@ pub mod pallet {

/// Migration manager to handle new created entries
type BalanceMigrationManager: BalanceMigrationManager<AccountIdOf<Self>, BalanceOf<Self, I>>;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: crate::benchmarking::BenchmarkHelper;
}

#[pallet::event]
Expand Down
3 changes: 3 additions & 0 deletions pallets/pallet-web3-names/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub(crate) mod runtime {
type Web3NameOwner = TestWeb3NameOwner;
type WeightInfo = ();
type BalanceMigrationManager = ();

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

impl mock_origin::Config for Test {
Expand Down
2 changes: 1 addition & 1 deletion runtimes/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ pub mod dot_names {

const MIN_NAME_LENGTH: u32 = 3;
const MAX_NAME_LENGTH: u32 = 28;
const DOT_NAME_SUFFIX: &str = ".dot";

pub const DOT_NAME_SUFFIX: &str = ".dot";
#[allow(clippy::as_conversions)]
pub const MIN_LENGTH: u32 = MIN_NAME_LENGTH + DOT_NAME_SUFFIX.len() as u32;
#[allow(clippy::as_conversions)]
Expand Down
3 changes: 3 additions & 0 deletions runtimes/common/src/dip/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ impl pallet_web3_names::Config for TestRuntime {
type Web3Name = AsciiWeb3Name<Self>;
type Web3NameOwner = DidIdentifier;
type WeightInfo = ();

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

impl pallet_did_lookup::Config for TestRuntime {
Expand Down
4 changes: 3 additions & 1 deletion runtimes/common/src/dot_names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use sp_std::vec::Vec;

use pallet_web3_names::{Config, Error};

use crate::constants::dot_names::DOT_NAME_SUFFIX;

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -64,7 +66,7 @@ impl<const MIN_LENGTH: u32, const MAX_LENGTH: u32> TryFrom<Vec<u8>> for DotName<
}
}
fn is_valid_dot_name(input: &[u8]) -> bool {
let Some(dot_name_without_suffix) = input.strip_suffix(b".dot") else {
let Some(dot_name_without_suffix) = input.strip_suffix(DOT_NAME_SUFFIX.as_bytes()) else {
return false;
};
// Char validation logic taken from https://github.com/paritytech/polkadot-sdk/blob/657b5503a04e97737696fa7344641019350fb521/substrate/frame/identity/src/lib.rs#L1435
Expand Down
3 changes: 3 additions & 0 deletions runtimes/kestrel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ impl pallet_web3_names::Config for Runtime {
type Web3NameOwner = DidIdentifier;
type WeightInfo = ();
type BalanceMigrationManager = ();

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
Expand Down
Loading
Loading