@@ -2,11 +2,13 @@ use super::*;
2
2
use crate :: Pallet as Capacity ;
3
3
4
4
use crate :: StakingType :: * ;
5
- use frame_benchmarking:: { account, benchmarks, whitelist_account} ;
5
+ use frame_benchmarking:: {
6
+ v1:: { account, whitelist_account, BenchmarkError } ,
7
+ v2:: * ,
8
+ } ;
6
9
use frame_support:: { assert_ok, BoundedVec } ;
7
10
use frame_system:: { pallet_prelude:: BlockNumberFor , RawOrigin } ;
8
11
use parity_scale_codec:: alloc:: vec:: Vec ;
9
-
10
12
const SEED : u32 = 0 ;
11
13
const REWARD_POOL_TOTAL : u32 = 2_000_000 ;
12
14
@@ -118,73 +120,94 @@ fn unclaimed_rewards_total<T: Config>(caller: &T::AccountId) -> BalanceOf<T> {
118
120
. into ( )
119
121
}
120
122
121
- benchmarks ! {
122
- stake {
123
+ #[ benchmarks]
124
+ mod benchmarks {
125
+ use super :: * ;
126
+
127
+ #[ benchmark]
128
+ fn stake ( ) -> Result < ( ) , BenchmarkError > {
123
129
let caller: T :: AccountId = create_funded_account :: < T > ( "account" , SEED , 105u32 ) ;
124
130
let amount: BalanceOf < T > = T :: MinimumStakingAmount :: get ( ) ;
125
131
let capacity: BalanceOf < T > = Capacity :: < T > :: capacity_generated ( amount) ;
126
132
let target = 1 ;
127
- let staking_type = MaximumCapacity ;
128
133
129
134
set_era_and_reward_pool_at_block :: < T > ( 1u32 . into ( ) , 1u32 . into ( ) , 1_000u32 . into ( ) ) ;
130
135
register_provider :: < T > ( target, "Foo" ) ;
131
136
132
- } : _ ( RawOrigin :: Signed ( caller. clone( ) ) , target, amount)
133
- verify {
137
+ #[ extrinsic_call]
138
+ _ ( RawOrigin :: Signed ( caller. clone ( ) ) , target, amount) ;
139
+
134
140
assert ! ( StakingAccountLedger :: <T >:: contains_key( & caller) ) ;
135
141
assert ! ( StakingTargetLedger :: <T >:: contains_key( & caller, target) ) ;
136
142
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 ( ( ) )
138
147
}
139
148
140
- withdraw_unstaked {
149
+ #[ benchmark]
150
+ fn withdraw_unstaked ( ) -> Result < ( ) , BenchmarkError > {
141
151
let caller: T :: AccountId = create_funded_account :: < T > ( "account" , SEED , 5u32 ) ;
142
152
fill_unlock_chunks :: < T > ( & caller, T :: MaxUnlockingChunks :: get ( ) ) ;
143
153
144
154
CurrentEpoch :: < T > :: set ( T :: EpochNumber :: from ( 5u32 ) ) ;
155
+ #[ extrinsic_call]
156
+ _ ( RawOrigin :: Signed ( caller. clone ( ) ) ) ;
145
157
146
- } : _ ( RawOrigin :: Signed ( caller. clone( ) ) )
147
- verify {
148
158
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 ( ( ) )
150
163
}
151
164
152
- start_new_epoch_if_needed {
165
+ #[ benchmark]
166
+ fn start_new_epoch_if_needed ( ) -> Result < ( ) , BenchmarkError > {
153
167
let current_block: BlockNumberFor < T > = 100_000u32 . into ( ) ;
154
168
let current_epoch: T :: EpochNumber = 10_000u32 . into ( ) ;
155
169
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
+
159
176
assert_eq ! ( current_epoch. saturating_add( 1u32 . into( ) ) , CurrentEpoch :: <T >:: get( ) ) ;
160
177
assert_eq ! ( current_block, CurrentEpochInfo :: <T >:: get( ) . epoch_start) ;
178
+ Ok ( ( ) )
161
179
}
162
180
163
- start_new_reward_era_if_needed {
181
+ #[ benchmark]
182
+ fn start_new_reward_era_if_needed ( ) -> Result < ( ) , BenchmarkError > {
164
183
let current_block: BlockNumberFor < T > = 1_209_600u32 . into ( ) ;
165
184
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 ( ) ) ;
170
187
171
188
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 } ) ;
173
190
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
+
177
197
let new_era_info = CurrentEraInfo :: < T > :: get ( ) ;
178
198
assert_eq ! ( current_era. saturating_add( 1u32 . into( ) ) , new_era_info. era_index) ;
179
199
assert_eq ! ( current_block, new_era_info. started_at) ;
200
+ Ok ( ( ) )
180
201
}
181
- unstake {
202
+
203
+ #[ benchmark]
204
+ fn unstake ( ) -> Result < ( ) , BenchmarkError > {
182
205
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 ( ) ) ;
184
208
let unstaking_amount = 9u32 ;
185
209
let capacity_amount: BalanceOf < T > = Capacity :: < T > :: capacity_generated ( staking_amount) ;
186
210
let target = 1 ;
187
- let block_number = 4u32 ;
188
211
189
212
// Adds a boost history entry for this era only so unstake succeeds and there is an update
190
213
// to provider boost history.
@@ -196,31 +219,48 @@ benchmarks! {
196
219
setup_provider_stake :: < T > ( & caller, & target, staking_amount, true ) ;
197
220
198
221
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 ( ( ) )
207
240
}
208
241
209
- set_epoch_length {
242
+ #[ benchmark]
243
+ fn set_epoch_length ( ) -> Result < ( ) , BenchmarkError > {
210
244
let epoch_length: BlockNumberFor < T > = 9u32 . into ( ) ;
211
- } : _ ( RawOrigin :: Root , epoch_length)
212
- verify {
245
+
246
+ #[ extrinsic_call]
247
+ _ ( RawOrigin :: Root , epoch_length) ;
248
+
213
249
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 ( ( ) )
215
252
}
216
253
217
- change_staking_target {
254
+ #[ benchmark]
255
+ fn change_staking_target ( ) -> Result < ( ) , BenchmarkError > {
218
256
let caller: T :: AccountId = create_funded_account :: < T > ( "account" , SEED , 5u32 ) ;
219
257
let from_msa = 33 ;
220
258
let to_msa = 34 ;
221
259
// 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 ( ) ) ;
224
264
225
265
set_era_and_reward_pool_at_block :: < T > ( 1u32 . into ( ) , 1u32 . into ( ) , 1_000u32 . into ( ) ) ;
226
266
register_provider :: < T > ( from_msa, "frommsa" ) ;
@@ -229,48 +269,72 @@ benchmarks! {
229
269
setup_provider_stake :: < T > ( & caller, & to_msa, to_msa_amount, false ) ;
230
270
let restake_amount: BalanceOf < T > = from_msa_amount. saturating_sub ( 10u32 . into ( ) ) ;
231
271
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 ( ( ) )
240
285
}
241
286
242
- provider_boost {
287
+ #[ benchmark]
288
+ fn provider_boost ( ) -> Result < ( ) , BenchmarkError > {
243
289
let caller: T :: AccountId = create_funded_account :: < T > ( "boostaccount" , SEED , 260u32 ) ;
244
290
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) ) ;
246
293
let target = 1 ;
247
294
248
295
set_era_and_reward_pool_at_block :: < T > ( 1u32 . into ( ) , 1u32 . into ( ) , 1_000u32 . into ( ) ) ;
249
296
register_provider :: < T > ( target, "Foo" ) ;
250
297
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 ( ( ) )
254
306
}
255
307
256
308
// TODO: vary the boost_history to get better weight estimates.
257
- claim_staking_rewards {
309
+ #[ benchmark]
310
+ fn claim_staking_rewards ( ) -> Result < ( ) , BenchmarkError > {
258
311
let caller: T :: AccountId = create_funded_account :: < T > ( "account" , SEED , 5u32 ) ;
259
312
let from_msa = 33 ;
260
313
let boost_amount: BalanceOf < T > = T :: MinimumStakingAmount :: get ( ) ;
261
314
setup_provider_stake :: < T > ( & caller, & from_msa, boost_amount, false ) ;
262
315
frame_system:: Pallet :: < T > :: set_block_number ( 1002u32 . into ( ) ) ;
263
316
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
+ ) ;
265
322
fill_reward_pool_chunks :: < T > ( current_era) ;
266
323
fill_boost_history :: < T > ( & caller, 100u32 . into ( ) , current_era) ;
267
324
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 ( ( ) )
271
337
}
272
338
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 ) ;
276
340
}
0 commit comments