Skip to content

Commit

Permalink
benchmarks(pallet-author-mapping, pallet-randomness): account existen…
Browse files Browse the repository at this point in the history
…tial deposit in benchmarks (#42)

* benchmarks: account existential deposit in author mapping benchmarks

* benchmarks: account existential deposit in pallet randomness benchmarks
  • Loading branch information
RomarQ authored May 17, 2024
1 parent 7295a81 commit 651741c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
4 changes: 3 additions & 1 deletion pallets/author-mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde = { workspace = true, optional = true }
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { workspace = true, optional = true }
parity-scale-codec = { workspace = true, features = [ "derive" ] }
scale-info = { workspace = true, features = [ "derive" ] }
sp-runtime = { workspace = true }
Expand All @@ -27,7 +28,6 @@ sp-std = { workspace = true }
session-keys-primitives = { workspace = true }

[dev-dependencies]
pallet-balances = { workspace = true, features = ["std"] }
sp-core = { workspace = true }
sp-io = { workspace = true }

Expand All @@ -45,9 +45,11 @@ std = [
"session-keys-primitives/std",
"sp-runtime/std",
"sp-std/std",
"pallet-balances/std"
]
runtime-benchmarks = [
"frame-benchmarking",
"pallet-balances",
"session-keys-primitives/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
15 changes: 9 additions & 6 deletions pallets/author-mapping/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ use nimbus_primitives::NimbusId;
use parity_scale_codec::Decode;

/// Create a funded user.
fn create_funded_user<T: Config>() -> T::AccountId {
fn create_funded_user<T: Config + pallet_balances::Config<Balance = BalanceOf<T>>>() -> T::AccountId
{
let user = account("account id", 0u32, 0u32);
T::DepositCurrency::make_free_balance_be(
&user,
<<T as Config>::DepositAmount as Get<BalanceOf<T>>>::get(),
);
T::DepositCurrency::issue(<<T as Config>::DepositAmount as Get<BalanceOf<T>>>::get());
let existential_deposit = <T as pallet_balances::Config>::ExistentialDeposit::get();
let amount = existential_deposit + <<T as Config>::DepositAmount as Get<BalanceOf<T>>>::get();

T::DepositCurrency::make_free_balance_be(&user, amount);
T::DepositCurrency::issue(amount);
user
}

Expand All @@ -45,6 +46,8 @@ pub fn nimbus_id(seed: u8) -> NimbusId {
}

benchmarks! {
where_clause { where T: pallet_balances::Config<Balance = BalanceOf<T>> }

add_association {
let caller = create_funded_user::<T>();
let id = nimbus_id(1u8);
Expand Down
6 changes: 4 additions & 2 deletions pallets/randomness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ sp-consensus-babe = { workspace = true }

# Benchmarks
frame-benchmarking = { workspace = true, optional = true }
pallet-balances = { workspace = true, optional = true, features = [ "insecure_zero_ed" ] }
hex = { workspace = true }


[dev-dependencies]
derive_more = { workspace = true }
pallet-author-mapping = { workspace = true, features = [ "std" ] }
pallet-balances = { workspace = true, features = [ "std", "insecure_zero_ed" ] }

[features]
default = [ "std" ]
Expand All @@ -49,10 +49,12 @@ std = [
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"schnorrkel/std"
"schnorrkel/std",
"pallet-balances/std"
]
runtime-benchmarks = [
"frame-benchmarking",
"pallet-balances",
"session-keys-primitives/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
27 changes: 24 additions & 3 deletions pallets/randomness/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,30 @@ use sp_runtime::traits::{Convert, One};
use sp_std::{mem::size_of, vec};

/// Create a funded user from the input
fn fund_user<T: Config>(user: H160, fee: BalanceOf<T>) {
let total_minted = fee + <<T as Config>::Deposit as Get<BalanceOf<T>>>::get();
fn fund_user<T: Config + pallet_balances::Config<Balance = BalanceOf<T>>>(
user: H160,
fee: BalanceOf<T>,
) {
let existential_deposit = <T as pallet_balances::Config>::ExistentialDeposit::get();
let total_minted =
existential_deposit + fee + <<T as Config>::Deposit as Get<BalanceOf<T>>>::get();
T::Currency::make_free_balance_be(&T::AddressMapping::convert(user), total_minted);
T::Currency::issue(total_minted);
}

fn fund_pallet_account_with_existential_deposit<
T: Config + pallet_balances::Config<Balance = BalanceOf<T>>,
>() {
let existential_deposit = <T as pallet_balances::Config>::ExistentialDeposit::get();
T::Currency::make_free_balance_be(&Pallet::<T>::account_id(), existential_deposit);
T::Currency::issue(existential_deposit);
}

benchmarks! {
where_clause {
where <T::VrfKeyLookup as KeysLookup<NimbusId, VrfId>>::Account: From<T::AccountId>
where
<T::VrfKeyLookup as KeysLookup<NimbusId, VrfId>>::Account: From<T::AccountId>,
T: pallet_balances::Config<Balance = BalanceOf<T>>
}
// Benchmark for inherent included in every block
set_babe_randomness_results {
Expand Down Expand Up @@ -190,6 +205,7 @@ benchmarks! {
let x in 1..T::MaxRandomWords::get().into();
let more = <<T as Config>::Deposit as Get<BalanceOf<T>>>::get();
fund_user::<T>(H160::default(), more);

let result = Pallet::<T>::request_randomness(Request {
refund_address: H160::default(),
contract_address: H160::default(),
Expand All @@ -214,6 +230,8 @@ benchmarks! {
finish_fulfillment {
let more = <<T as Config>::Deposit as Get<BalanceOf<T>>>::get();
fund_user::<T>(H160::default(), more);
fund_pallet_account_with_existential_deposit::<T>();

let result = Pallet::<T>::request_randomness(Request {
refund_address: H160::default(),
contract_address: H160::default(),
Expand Down Expand Up @@ -254,6 +272,7 @@ benchmarks! {
increase_fee {
let more = <<T as Config>::Deposit as Get<BalanceOf<T>>>::get();
fund_user::<T>(H160::default(), more);

let result = Pallet::<T>::request_randomness(Request {
refund_address: H160::default(),
contract_address: H160::default(),
Expand All @@ -276,6 +295,8 @@ benchmarks! {
execute_request_expiration {
let more = <<T as Config>::Deposit as Get<BalanceOf<T>>>::get();
fund_user::<T>(H160::default(), more);
fund_pallet_account_with_existential_deposit::<T>();

let result = Pallet::<T>::request_randomness(Request {
refund_address: H160::default(),
contract_address: H160::default(),
Expand Down

0 comments on commit 651741c

Please sign in to comment.