Skip to content

Commit 25a7330

Browse files
authored
use benchmarks v2 syntax (#2332)
# Goal The goal of this PR is to use benchmarks v2 macros Closes #2328 # Checklist - [x] Benchmarks updated
1 parent 85c4e96 commit 25a7330

File tree

10 files changed

+1009
-413
lines changed

10 files changed

+1009
-413
lines changed

pallets/capacity/src/benchmarking.rs

Lines changed: 128 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ use super::*;
22
use crate::Pallet as Capacity;
33

44
use crate::StakingType::*;
5-
use frame_benchmarking::{account, benchmarks, whitelist_account};
5+
use frame_benchmarking::{
6+
v1::{account, whitelist_account, BenchmarkError},
7+
v2::*,
8+
};
69
use frame_support::{assert_ok, BoundedVec};
710
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
811
use parity_scale_codec::alloc::vec::Vec;
9-
1012
const SEED: u32 = 0;
1113
const REWARD_POOL_TOTAL: u32 = 2_000_000;
1214

@@ -118,73 +120,94 @@ fn unclaimed_rewards_total<T: Config>(caller: &T::AccountId) -> BalanceOf<T> {
118120
.into()
119121
}
120122

121-
benchmarks! {
122-
stake {
123+
#[benchmarks]
124+
mod benchmarks {
125+
use super::*;
126+
127+
#[benchmark]
128+
fn stake() -> Result<(), BenchmarkError> {
123129
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 105u32);
124130
let amount: BalanceOf<T> = T::MinimumStakingAmount::get();
125131
let capacity: BalanceOf<T> = Capacity::<T>::capacity_generated(amount);
126132
let target = 1;
127-
let staking_type = MaximumCapacity;
128133

129134
set_era_and_reward_pool_at_block::<T>(1u32.into(), 1u32.into(), 1_000u32.into());
130135
register_provider::<T>(target, "Foo");
131136

132-
}: _ (RawOrigin::Signed(caller.clone()), target, amount)
133-
verify {
137+
#[extrinsic_call]
138+
_(RawOrigin::Signed(caller.clone()), target, amount);
139+
134140
assert!(StakingAccountLedger::<T>::contains_key(&caller));
135141
assert!(StakingTargetLedger::<T>::contains_key(&caller, target));
136142
assert!(CapacityLedger::<T>::contains_key(target));
137-
assert_last_event::<T>(Event::<T>::Staked {account: caller, amount, target, capacity}.into());
143+
assert_last_event::<T>(
144+
Event::<T>::Staked { account: caller, amount, target, capacity }.into(),
145+
);
146+
Ok(())
138147
}
139148

140-
withdraw_unstaked {
149+
#[benchmark]
150+
fn withdraw_unstaked() -> Result<(), BenchmarkError> {
141151
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 5u32);
142152
fill_unlock_chunks::<T>(&caller, T::MaxUnlockingChunks::get());
143153

144154
CurrentEpoch::<T>::set(T::EpochNumber::from(5u32));
155+
#[extrinsic_call]
156+
_(RawOrigin::Signed(caller.clone()));
145157

146-
}: _ (RawOrigin::Signed(caller.clone()))
147-
verify {
148158
let total = T::MaxUnlockingChunks::get();
149-
assert_last_event::<T>(Event::<T>::StakeWithdrawn {account: caller, amount: total.into() }.into());
159+
assert_last_event::<T>(
160+
Event::<T>::StakeWithdrawn { account: caller, amount: total.into() }.into(),
161+
);
162+
Ok(())
150163
}
151164

152-
start_new_epoch_if_needed {
165+
#[benchmark]
166+
fn start_new_epoch_if_needed() -> Result<(), BenchmarkError> {
153167
let current_block: BlockNumberFor<T> = 100_000u32.into();
154168
let current_epoch: T::EpochNumber = 10_000u32.into();
155169
set_up_epoch::<T>(current_block, current_epoch);
156-
}: {
157-
Capacity::<T>::start_new_epoch_if_needed(current_block)
158-
} verify {
170+
171+
#[block]
172+
{
173+
Capacity::<T>::start_new_epoch_if_needed(current_block);
174+
}
175+
159176
assert_eq!(current_epoch.saturating_add(1u32.into()), CurrentEpoch::<T>::get());
160177
assert_eq!(current_block, CurrentEpochInfo::<T>::get().epoch_start);
178+
Ok(())
161179
}
162180

163-
start_new_reward_era_if_needed {
181+
#[benchmark]
182+
fn start_new_reward_era_if_needed() -> Result<(), BenchmarkError> {
164183
let current_block: BlockNumberFor<T> = 1_209_600u32.into();
165184
let history_limit: u32 = <T as Config>::ProviderBoostHistoryLimit::get();
166-
let total_reward_pool: BalanceOf<T> = <T as Config>::RewardPoolPerEra::get();
167-
let unclaimed_balance: BalanceOf<T> = 5_000u32.into();
168-
let total_staked_token: BalanceOf<T> = 5_000u32.into();
169-
let started_at: BlockNumberFor<T> = current_block.saturating_sub(<T as Config>::EraLength::get().into());
185+
let started_at: BlockNumberFor<T> =
186+
current_block.saturating_sub(<T as Config>::EraLength::get().into());
170187

171188
let current_era: RewardEra = (history_limit + 1u32).into();
172-
CurrentEraInfo::<T>::set(RewardEraInfo{ era_index: current_era, started_at });
189+
CurrentEraInfo::<T>::set(RewardEraInfo { era_index: current_era, started_at });
173190
fill_reward_pool_chunks::<T>(current_era);
174-
}: {
175-
Capacity::<T>::start_new_reward_era_if_needed(current_block);
176-
} verify {
191+
192+
#[block]
193+
{
194+
Capacity::<T>::start_new_reward_era_if_needed(current_block);
195+
}
196+
177197
let new_era_info = CurrentEraInfo::<T>::get();
178198
assert_eq!(current_era.saturating_add(1u32.into()), new_era_info.era_index);
179199
assert_eq!(current_block, new_era_info.started_at);
200+
Ok(())
180201
}
181-
unstake {
202+
203+
#[benchmark]
204+
fn unstake() -> Result<(), BenchmarkError> {
182205
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 5u32);
183-
let staking_amount: BalanceOf<T> = T::MinimumStakingAmount::get().saturating_add(20u32.into());
206+
let staking_amount: BalanceOf<T> =
207+
T::MinimumStakingAmount::get().saturating_add(20u32.into());
184208
let unstaking_amount = 9u32;
185209
let capacity_amount: BalanceOf<T> = Capacity::<T>::capacity_generated(staking_amount);
186210
let target = 1;
187-
let block_number = 4u32;
188211

189212
// Adds a boost history entry for this era only so unstake succeeds and there is an update
190213
// to provider boost history.
@@ -196,31 +219,48 @@ benchmarks! {
196219
setup_provider_stake::<T>(&caller, &target, staking_amount, true);
197220

198221
fill_unlock_chunks::<T>(&caller, T::MaxUnlockingChunks::get() - 1);
199-
}: _ (RawOrigin::Signed(caller.clone()), target, unstaking_amount.into())
200-
verify {
201-
assert_last_event::<T>(Event::<T>::UnStaked {
202-
account: caller.clone(),
203-
target,
204-
amount: unstaking_amount.into(),
205-
capacity: Capacity::<T>::calculate_capacity_reduction(unstaking_amount.into(), staking_amount,capacity_amount)
206-
}.into());
222+
223+
#[extrinsic_call]
224+
_(RawOrigin::Signed(caller.clone()), target, unstaking_amount.into());
225+
226+
assert_last_event::<T>(
227+
Event::<T>::UnStaked {
228+
account: caller.clone(),
229+
target,
230+
amount: unstaking_amount.into(),
231+
capacity: Capacity::<T>::calculate_capacity_reduction(
232+
unstaking_amount.into(),
233+
staking_amount,
234+
capacity_amount,
235+
),
236+
}
237+
.into(),
238+
);
239+
Ok(())
207240
}
208241

209-
set_epoch_length {
242+
#[benchmark]
243+
fn set_epoch_length() -> Result<(), BenchmarkError> {
210244
let epoch_length: BlockNumberFor<T> = 9u32.into();
211-
}: _ (RawOrigin::Root, epoch_length)
212-
verify {
245+
246+
#[extrinsic_call]
247+
_(RawOrigin::Root, epoch_length);
248+
213249
assert_eq!(EpochLength::<T>::get(), 9u32.into());
214-
assert_last_event::<T>(Event::<T>::EpochLengthUpdated {blocks: epoch_length}.into());
250+
assert_last_event::<T>(Event::<T>::EpochLengthUpdated { blocks: epoch_length }.into());
251+
Ok(())
215252
}
216253

217-
change_staking_target {
254+
#[benchmark]
255+
fn change_staking_target() -> Result<(), BenchmarkError> {
218256
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 5u32);
219257
let from_msa = 33;
220258
let to_msa = 34;
221259
// amount in addition to minimum
222-
let from_msa_amount: BalanceOf<T> = T::MinimumStakingAmount::get().saturating_add(31u32.into());
223-
let to_msa_amount: BalanceOf<T> = T::MinimumStakingAmount::get().saturating_add(1u32.into());
260+
let from_msa_amount: BalanceOf<T> =
261+
T::MinimumStakingAmount::get().saturating_add(31u32.into());
262+
let to_msa_amount: BalanceOf<T> =
263+
T::MinimumStakingAmount::get().saturating_add(1u32.into());
224264

225265
set_era_and_reward_pool_at_block::<T>(1u32.into(), 1u32.into(), 1_000u32.into());
226266
register_provider::<T>(from_msa, "frommsa");
@@ -229,48 +269,72 @@ benchmarks! {
229269
setup_provider_stake::<T>(&caller, &to_msa, to_msa_amount, false);
230270
let restake_amount: BalanceOf<T> = from_msa_amount.saturating_sub(10u32.into());
231271

232-
}: _ (RawOrigin::Signed(caller.clone(), ), from_msa, to_msa, restake_amount)
233-
verify {
234-
assert_last_event::<T>(Event::<T>::StakingTargetChanged {
235-
account: caller,
236-
from_msa,
237-
to_msa,
238-
amount: restake_amount.into()
239-
}.into());
272+
#[extrinsic_call]
273+
_(RawOrigin::Signed(caller.clone()), from_msa, to_msa, restake_amount);
274+
275+
assert_last_event::<T>(
276+
Event::<T>::StakingTargetChanged {
277+
account: caller,
278+
from_msa,
279+
to_msa,
280+
amount: restake_amount.into(),
281+
}
282+
.into(),
283+
);
284+
Ok(())
240285
}
241286

242-
provider_boost {
287+
#[benchmark]
288+
fn provider_boost() -> Result<(), BenchmarkError> {
243289
let caller: T::AccountId = create_funded_account::<T>("boostaccount", SEED, 260u32);
244290
let boost_amount: BalanceOf<T> = T::MinimumStakingAmount::get().saturating_add(1u32.into());
245-
let capacity: BalanceOf<T> = Capacity::<T>::capacity_generated(<T>::RewardsProvider::capacity_boost(boost_amount));
291+
let capacity: BalanceOf<T> =
292+
Capacity::<T>::capacity_generated(<T>::RewardsProvider::capacity_boost(boost_amount));
246293
let target = 1;
247294

248295
set_era_and_reward_pool_at_block::<T>(1u32.into(), 1u32.into(), 1_000u32.into());
249296
register_provider::<T>(target, "Foo");
250297

251-
}: _ (RawOrigin::Signed(caller.clone()), target, boost_amount)
252-
verify {
253-
assert_last_event::<T>(Event::<T>::ProviderBoosted {account: caller, amount: boost_amount, target, capacity}.into());
298+
#[extrinsic_call]
299+
_(RawOrigin::Signed(caller.clone()), target, boost_amount);
300+
301+
assert_last_event::<T>(
302+
Event::<T>::ProviderBoosted { account: caller, amount: boost_amount, target, capacity }
303+
.into(),
304+
);
305+
Ok(())
254306
}
255307

256308
// TODO: vary the boost_history to get better weight estimates.
257-
claim_staking_rewards {
309+
#[benchmark]
310+
fn claim_staking_rewards() -> Result<(), BenchmarkError> {
258311
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 5u32);
259312
let from_msa = 33;
260313
let boost_amount: BalanceOf<T> = T::MinimumStakingAmount::get();
261314
setup_provider_stake::<T>(&caller, &from_msa, boost_amount, false);
262315
frame_system::Pallet::<T>::set_block_number(1002u32.into());
263316
let current_era: RewardEra = 100;
264-
set_era_and_reward_pool_at_block::<T>(current_era, 1001u32.into(), REWARD_POOL_TOTAL.into());
317+
set_era_and_reward_pool_at_block::<T>(
318+
current_era,
319+
1001u32.into(),
320+
REWARD_POOL_TOTAL.into(),
321+
);
265322
fill_reward_pool_chunks::<T>(current_era);
266323
fill_boost_history::<T>(&caller, 100u32.into(), current_era);
267324
let unclaimed_rewards = unclaimed_rewards_total::<T>(&caller);
268-
}: _ (RawOrigin::Signed(caller.clone()))
269-
verify {
270-
assert_last_event::<T>(Event::<T>::ProviderBoostRewardClaimed {account: caller.clone(), reward_amount: unclaimed_rewards}.into());
325+
326+
#[extrinsic_call]
327+
_(RawOrigin::Signed(caller.clone()));
328+
329+
assert_last_event::<T>(
330+
Event::<T>::ProviderBoostRewardClaimed {
331+
account: caller.clone(),
332+
reward_amount: unclaimed_rewards,
333+
}
334+
.into(),
335+
);
336+
Ok(())
271337
}
272338

273-
impl_benchmark_test_suite!(Capacity,
274-
tests::mock::new_test_ext(),
275-
tests::mock::Test);
339+
impl_benchmark_test_suite!(Capacity, tests::mock::new_test_ext(), tests::mock::Test);
276340
}

pallets/frequency-tx-payment/src/benchmarking.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
use super::*;
22

3-
use frame_benchmarking::{benchmarks, whitelisted_caller};
3+
use frame_benchmarking::v2::*;
44
use frame_system::{Call as SystemCall, RawOrigin};
55

6-
benchmarks! {
7-
pay_with_capacity {
6+
#[benchmarks]
7+
mod benchmarks {
8+
use super::*;
9+
10+
#[benchmark]
11+
fn pay_with_capacity() -> Result<(), BenchmarkError> {
812
let sender: T::AccountId = whitelisted_caller();
9-
let call: Box<<T as Config>::RuntimeCall> = Box::new(SystemCall::remark { remark: vec![] }.into());
10-
}: _ (RawOrigin::Signed(sender), call)
13+
let call: Box<<T as Config>::RuntimeCall> =
14+
Box::new(SystemCall::remark { remark: vec![] }.into());
15+
16+
#[extrinsic_call]
17+
_(RawOrigin::Signed(sender), call);
1118

12-
pay_with_capacity_batch_all {
13-
let n in 0 .. (T::MaximumCapacityBatchLength::get() as u32);
19+
Ok(())
20+
}
1421

22+
#[benchmark]
23+
fn pay_with_capacity_batch_all(
24+
n: Linear<0, { T::MaximumCapacityBatchLength::get() as u32 }>,
25+
) -> Result<(), BenchmarkError> {
1526
let sender: T::AccountId = whitelisted_caller();
1627

1728
let mut batched_calls: Vec<<T as Config>::RuntimeCall> = vec![];
1829

19-
for i in 0 .. n {
30+
for _ in 0..n {
2031
let call: <T as Config>::RuntimeCall = SystemCall::remark { remark: vec![] }.into();
2132
batched_calls.push(call);
2233
}
23-
}: _ (RawOrigin::Signed(sender), batched_calls)
34+
35+
#[extrinsic_call]
36+
_(RawOrigin::Signed(sender), batched_calls);
37+
38+
Ok(())
39+
}
2440

2541
impl_benchmark_test_suite!(
2642
Pallet,

0 commit comments

Comments
 (0)