diff --git a/pallets/capacity/src/benchmarking.rs b/pallets/capacity/src/benchmarking.rs index 014e81fa11..5693c2d413 100644 --- a/pallets/capacity/src/benchmarking.rs +++ b/pallets/capacity/src/benchmarking.rs @@ -120,16 +120,39 @@ benchmarks! { assert_last_event::(Event::::StakeWithdrawn {account: caller, amount: total.into() }.into()); } - on_initialize { + start_new_epoch_if_needed { let current_block: BlockNumberFor = 100_000u32.into(); let current_epoch: T::EpochNumber = 10_000u32.into(); set_up_epoch::(current_block, current_epoch); - }: { - Capacity::::on_initialize(current_block); + }: { + Capacity::::start_new_epoch_if_needed(current_block) } verify { assert_eq!(current_epoch.saturating_add(1u32.into()), Capacity::::get_current_epoch()); assert_eq!(current_block, CurrentEpochInfo::::get().epoch_start); } + + start_new_reward_era_if_needed { + let current_block: BlockNumberFor = 1_209_600u32.into(); + let history_limit: u32 = ::StakingRewardsPastErasMax::get(); + let total_reward_pool: BalanceOf = ::RewardPoolEachEra::get(); + let unclaimed_balance: BalanceOf = 5_000u32.into(); + let total_staked_token: BalanceOf = 5_000u32.into(); + let started_at: BlockNumberFor = current_block.saturating_sub(::EraLength::get().into()); + + let current_era: T::RewardEra = (history_limit + 1u32).into(); + CurrentEraInfo::::set(RewardEraInfo{ era_index: current_era, started_at }); + + for i in 0..history_limit { + let era: T::RewardEra = i.into(); + StakingRewardPool::::insert(era, RewardPoolInfo { total_staked_token, total_reward_pool, unclaimed_balance}); + } + }: { + Capacity::::start_new_reward_era_if_needed(current_block); + } verify { + let new_era_info = Capacity::::get_current_era(); + assert_eq!(current_era.saturating_add(1u32.into()), new_era_info.era_index); + assert_eq!(current_block, new_era_info.started_at); + } unstake { let caller: T::AccountId = create_funded_account::("account", SEED, 5u32); let staking_amount: BalanceOf = T::MinimumStakingAmount::get().saturating_add(20u32.into()); diff --git a/pallets/capacity/src/lib.rs b/pallets/capacity/src/lib.rs index 756ac60594..a5f419c63b 100644 --- a/pallets/capacity/src/lib.rs +++ b/pallets/capacity/src/lib.rs @@ -952,9 +952,7 @@ impl Pallet { let current_epoch = Self::get_current_epoch(); CurrentEpoch::::set(current_epoch.saturating_add(One::one())); CurrentEpochInfo::::set(EpochInfo { epoch_start: current_block }); - T::WeightInfo::on_initialize() - .saturating_add(T::DbWeight::get().reads(1)) - .saturating_add(T::DbWeight::get().writes(2)) + T::WeightInfo::start_new_epoch_if_needed() } else { // 1 for get_current_epoch_info, 1 for get_epoch_length T::DbWeight::get().reads(2).saturating_add(RocksDbWeight::get().writes(1)) @@ -966,6 +964,7 @@ impl Pallet { Self::get_current_era(); // 1r if current_block.saturating_sub(current_era_info.started_at) >= T::EraLength::get().into() { + // 1r let new_era_info = RewardEraInfo { era_index: current_era_info.era_index.saturating_add(One::one()), started_at: current_block, @@ -992,10 +991,7 @@ impl Pallet { unclaimed_balance: total_reward_pool, }; StakingRewardPool::::insert(new_era_info.era_index, new_reward_pool); // 1w - - T::WeightInfo::on_initialize() - .saturating_add(T::DbWeight::get().reads(3)) - .saturating_add(T::DbWeight::get().writes(3)) + T::WeightInfo::start_new_reward_era_if_needed() } else { T::DbWeight::get().reads(1) } diff --git a/pallets/capacity/src/weights.rs b/pallets/capacity/src/weights.rs index b16ba512a9..88d8039664 100644 --- a/pallets/capacity/src/weights.rs +++ b/pallets/capacity/src/weights.rs @@ -18,9 +18,9 @@ //! Autogenerated weights for pallet_capacity //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2024-05-08, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-05-10, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` -//! HOSTNAME: `UL-Mac`, CPU: `` +//! HOSTNAME: `UL-Mac.local`, CPU: `` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("frequency-bench"), DB CACHE: 1024 // Executed Command: @@ -51,7 +51,8 @@ use core::marker::PhantomData; pub trait WeightInfo { fn stake() -> Weight; fn withdraw_unstaked() -> Weight; - fn on_initialize() -> Weight; + fn start_new_epoch_if_needed() -> Weight; + fn start_new_reward_era_if_needed() -> Weight; fn unstake() -> Weight; fn set_epoch_length() -> Weight; fn change_staking_target() -> Weight; @@ -79,7 +80,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `174` // Estimated: `6249` - // Minimum execution time: 35_000_000 picoseconds. + // Minimum execution time: 36_000_000 picoseconds. Weight::from_parts(37_000_000, 6249) .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) @@ -97,7 +98,7 @@ impl WeightInfo for SubstrateWeight { // Measured: `226` // Estimated: `6249` // Minimum execution time: 23_000_000 picoseconds. - Weight::from_parts(23_000_000, 6249) + Weight::from_parts(24_000_000, 6249) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } @@ -105,15 +106,28 @@ impl WeightInfo for SubstrateWeight { /// Proof: `Capacity::CurrentEpochInfo` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) /// Storage: `Capacity::EpochLength` (r:1 w:0) /// Proof: `Capacity::EpochLength` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) - fn on_initialize() -> Weight { + fn start_new_epoch_if_needed() -> Weight { // Proof Size summary in bytes: // Measured: `0` // Estimated: `2974` - // Minimum execution time: 4_000_000 picoseconds. + // Minimum execution time: 3_000_000 picoseconds. Weight::from_parts(4_000_000, 2974) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } + /// Storage: `Capacity::StakingRewardPool` (r:3 w:2) + /// Proof: `Capacity::StakingRewardPool` (`max_values`: None, `max_size`: Some(60), added: 2535, mode: `MaxEncodedLen`) + /// Storage: `Capacity::CounterForStakingRewardPool` (r:1 w:1) + /// Proof: `Capacity::CounterForStakingRewardPool` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) + fn start_new_reward_era_if_needed() -> Weight { + // Proof Size summary in bytes: + // Measured: `638` + // Estimated: `10080` + // Minimum execution time: 14_000_000 picoseconds. + Weight::from_parts(15_000_000, 10080) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } /// Storage: `Capacity::StakingAccountLedger` (r:1 w:1) /// Proof: `Capacity::StakingAccountLedger` (`max_values`: None, `max_size`: Some(57), added: 2532, mode: `MaxEncodedLen`) /// Storage: `Capacity::StakingRewardPool` (r:1 w:1) @@ -128,8 +142,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `343` // Estimated: `5071` - // Minimum execution time: 28_000_000 picoseconds. - Weight::from_parts(29_000_000, 5071) + // Minimum execution time: 29_000_000 picoseconds. + Weight::from_parts(30_000_000, 5071) .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(5_u64)) } @@ -157,7 +171,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `315` // Estimated: `7601` - // Minimum execution time: 29_000_000 picoseconds. + // Minimum execution time: 30_000_000 picoseconds. Weight::from_parts(31_000_000, 7601) .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(5_u64)) @@ -185,7 +199,7 @@ impl WeightInfo for SubstrateWeight { // Measured: `247` // Estimated: `6249` // Minimum execution time: 45_000_000 picoseconds. - Weight::from_parts(48_000_000, 6249) + Weight::from_parts(46_000_000, 6249) .saturating_add(T::DbWeight::get().reads(9_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } @@ -211,7 +225,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `174` // Estimated: `6249` - // Minimum execution time: 35_000_000 picoseconds. + // Minimum execution time: 36_000_000 picoseconds. Weight::from_parts(37_000_000, 6249) .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) @@ -229,7 +243,7 @@ impl WeightInfo for () { // Measured: `226` // Estimated: `6249` // Minimum execution time: 23_000_000 picoseconds. - Weight::from_parts(23_000_000, 6249) + Weight::from_parts(24_000_000, 6249) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } @@ -237,15 +251,28 @@ impl WeightInfo for () { /// Proof: `Capacity::CurrentEpochInfo` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) /// Storage: `Capacity::EpochLength` (r:1 w:0) /// Proof: `Capacity::EpochLength` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) - fn on_initialize() -> Weight { + fn start_new_epoch_if_needed() -> Weight { // Proof Size summary in bytes: // Measured: `0` // Estimated: `2974` - // Minimum execution time: 4_000_000 picoseconds. + // Minimum execution time: 3_000_000 picoseconds. Weight::from_parts(4_000_000, 2974) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } + /// Storage: `Capacity::StakingRewardPool` (r:3 w:2) + /// Proof: `Capacity::StakingRewardPool` (`max_values`: None, `max_size`: Some(60), added: 2535, mode: `MaxEncodedLen`) + /// Storage: `Capacity::CounterForStakingRewardPool` (r:1 w:1) + /// Proof: `Capacity::CounterForStakingRewardPool` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) + fn start_new_reward_era_if_needed() -> Weight { + // Proof Size summary in bytes: + // Measured: `638` + // Estimated: `10080` + // Minimum execution time: 14_000_000 picoseconds. + Weight::from_parts(15_000_000, 10080) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } /// Storage: `Capacity::StakingAccountLedger` (r:1 w:1) /// Proof: `Capacity::StakingAccountLedger` (`max_values`: None, `max_size`: Some(57), added: 2532, mode: `MaxEncodedLen`) /// Storage: `Capacity::StakingRewardPool` (r:1 w:1) @@ -260,8 +287,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `343` // Estimated: `5071` - // Minimum execution time: 28_000_000 picoseconds. - Weight::from_parts(29_000_000, 5071) + // Minimum execution time: 29_000_000 picoseconds. + Weight::from_parts(30_000_000, 5071) .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(5_u64)) } @@ -289,7 +316,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `315` // Estimated: `7601` - // Minimum execution time: 29_000_000 picoseconds. + // Minimum execution time: 30_000_000 picoseconds. Weight::from_parts(31_000_000, 7601) .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(5_u64)) @@ -317,7 +344,7 @@ impl WeightInfo for () { // Measured: `247` // Estimated: `6249` // Minimum execution time: 45_000_000 picoseconds. - Weight::from_parts(48_000_000, 6249) + Weight::from_parts(46_000_000, 6249) .saturating_add(RocksDbWeight::get().reads(9_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) }