Skip to content

Commit 1c4141a

Browse files
authored
Fix benchmark failures when using insecure_zero_ed flag (#5354)
Currently, when the pallet is compiled with the `insecure_zero_ed flag`, benchmarks fail because the minimum balance is set to zero. The PR aims to resolve this issue by implementing a placeholder value for the minimum balance when the `insecure_zero_ed` flag is active. it ensures that benchmarks run successfully regardless of whether this flag is used or not
1 parent 8e0cefc commit 1c4141a

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

prdoc/pr_5354.prdoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: Fix benchmark failures when using insecure_zero_ed flag
5+
6+
doc:
7+
- audience: Runtime Dev
8+
description: |
9+
Currently, when the pallet is compiled with the insecure_zero_ed flag, benchmarks fail because the minimum balance is set to zero.
10+
11+
The PR aims to resolve this issue by implementing a placeholder value for the minimum balance when the insecure_zero_ed flag is active. it ensures that benchmarks run successfully regardless of whether this flag is used or not
12+
13+
crates:
14+
- name: pallet-balances
15+
bump: minor

substrate/frame/balances/src/benchmarking.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ const SEED: u32 = 0;
3131
// existential deposit multiplier
3232
const ED_MULTIPLIER: u32 = 10;
3333

34+
fn minimum_balance<T: Config<I>, I: 'static>() -> T::Balance {
35+
if cfg!(feature = "insecure_zero_ed") {
36+
100u32.into()
37+
} else {
38+
T::ExistentialDeposit::get()
39+
}
40+
}
41+
3442
#[instance_benchmarks]
3543
mod benchmarks {
3644
use super::*;
@@ -40,7 +48,7 @@ mod benchmarks {
4048
// * Transfer will create the recipient account.
4149
#[benchmark]
4250
fn transfer_allow_death() {
43-
let existential_deposit = T::ExistentialDeposit::get();
51+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
4452
let caller = whitelisted_caller();
4553

4654
// Give some multiple of the existential deposit
@@ -75,7 +83,7 @@ mod benchmarks {
7583
<Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
7684

7785
// Give the recipient account existential deposit (thus their account already exists).
78-
let existential_deposit = T::ExistentialDeposit::get();
86+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
7987
let _ =
8088
<Balances<T, I> as Currency<_>>::make_free_balance_be(&recipient, existential_deposit);
8189
let transfer_amount = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
@@ -98,7 +106,7 @@ mod benchmarks {
98106
// Give the sender account max funds, thus a transfer will not kill account.
99107
let _ =
100108
<Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
101-
let existential_deposit = T::ExistentialDeposit::get();
109+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
102110
let transfer_amount = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
103111

104112
#[extrinsic_call]
@@ -115,7 +123,7 @@ mod benchmarks {
115123
let user_lookup = T::Lookup::unlookup(user.clone());
116124

117125
// Give the user some initial balance.
118-
let existential_deposit = T::ExistentialDeposit::get();
126+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
119127
let balance_amount = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
120128
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&user, balance_amount);
121129

@@ -132,7 +140,7 @@ mod benchmarks {
132140
let user_lookup = T::Lookup::unlookup(user.clone());
133141

134142
// Give the user some initial balance.
135-
let existential_deposit = T::ExistentialDeposit::get();
143+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
136144
let balance_amount = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
137145
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&user, balance_amount);
138146

@@ -147,7 +155,7 @@ mod benchmarks {
147155
// * Transfer will create the recipient account.
148156
#[benchmark]
149157
fn force_transfer() {
150-
let existential_deposit = T::ExistentialDeposit::get();
158+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
151159
let source: T::AccountId = account("source", 0, SEED);
152160
let source_lookup = T::Lookup::unlookup(source.clone());
153161

@@ -175,7 +183,7 @@ mod benchmarks {
175183
#[benchmark(extra)]
176184
fn transfer_increasing_users(u: Linear<0, 1_000>) {
177185
// 1_000 is not very much, but this upper bound can be controlled by the CLI.
178-
let existential_deposit = T::ExistentialDeposit::get();
186+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
179187
let caller = whitelisted_caller();
180188

181189
// Give some multiple of the existential deposit
@@ -214,7 +222,7 @@ mod benchmarks {
214222
let recipient_lookup = T::Lookup::unlookup(recipient.clone());
215223

216224
// Give some multiple of the existential deposit
217-
let existential_deposit = T::ExistentialDeposit::get();
225+
let existential_deposit: T::Balance = minimum_balance::<T, I>();
218226
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
219227
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, balance);
220228

@@ -231,7 +239,7 @@ mod benchmarks {
231239
let user_lookup = T::Lookup::unlookup(user.clone());
232240

233241
// Give some multiple of the existential deposit
234-
let ed = T::ExistentialDeposit::get();
242+
let ed = minimum_balance::<T, I>();
235243
let balance = ed + ed;
236244
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&user, balance);
237245

@@ -257,8 +265,8 @@ mod benchmarks {
257265
.map(|i| -> T::AccountId {
258266
let user = account("old_user", i, SEED);
259267
let account = AccountData {
260-
free: T::ExistentialDeposit::get(),
261-
reserved: T::ExistentialDeposit::get(),
268+
free: minimum_balance::<T, I>(),
269+
reserved: minimum_balance::<T, I>(),
262270
frozen: Zero::zero(),
263271
flags: ExtraFlags::old_logic(),
264272
};

0 commit comments

Comments
 (0)