diff --git a/pallets/dapp-staking-v3/src/benchmarking/mod.rs b/pallets/dapp-staking-v3/src/benchmarking/mod.rs index abb37ee071..ad8d21d034 100644 --- a/pallets/dapp-staking-v3/src/benchmarking/mod.rs +++ b/pallets/dapp-staking-v3/src/benchmarking/mod.rs @@ -490,7 +490,7 @@ mod benchmarks { #[extrinsic_call] claim_staker_rewards(RawOrigin::Signed(staker.clone())); - // No need to do precise check of values, but predetermiend amount of 'Reward' events is expected. + // No need to do precise check of values, but predetermined amount of 'Reward' events is expected. let dapp_staking_events = dapp_staking_events::(); assert_eq!(dapp_staking_events.len(), x as usize); dapp_staking_events.iter().for_each(|e| { @@ -561,7 +561,7 @@ mod benchmarks { #[extrinsic_call] claim_staker_rewards(RawOrigin::Signed(staker.clone())); - // No need to do precise check of values, but predetermiend amount of 'Reward' events is expected. + // No need to do precise check of values, but predetermined amount of 'Reward' events is expected. let dapp_staking_events = dapp_staking_events::(); assert_eq!(dapp_staking_events.len(), x as usize); dapp_staking_events.iter().for_each(|e| { @@ -662,7 +662,7 @@ mod benchmarks { // Advance enough eras so dApp reward can be claimed. force_advance_to_next_subperiod::(); - // This is a hacky part to ensure we accomodate max number of contracts. + // This is a hacky part to ensure we accommodate max number of contracts. TierConfig::::mutate(|config| { let max_number_of_contracts: u16 = T::MaxNumberOfContracts::get().try_into().unwrap(); config.number_of_slots = max_number_of_contracts; @@ -928,7 +928,7 @@ mod benchmarks { // Investigate why the PoV size is so large here, even after removing read of `IntegratedDApps` storage. // Relevant file: polkadot-sdk/substrate/utils/frame/benchmarking-cli/src/pallet/writer.rs - // UPDATE: after some investigation, it seems that PoV size benchmarks are very unprecise + // UPDATE: after some investigation, it seems that PoV size benchmarks are very imprecise // - the worst case measured is usually very far off the actual value that is consumed on chain. // There's an ongoing item to improve it (mentioned on roundtable meeting). #[benchmark] @@ -960,39 +960,57 @@ mod benchmarks { // Prepare init config (protocol state, tier params & config, etc.) initial_config::(); - // Advance to era just after the last era covered by the first span. - // This is sufficient to completely fill up the first span with entries for the ongoing era. - force_advance_to_era::(T::EraRewardSpanLength::get()); - - // Advance enough periods to make cleanup feasible. - let retention_period = T::RewardRetentionInPeriods::get(); - force_advance_to_period::( - ActiveProtocolState::::get().period_number() + retention_period + 2, + // Hack + // Prepare state prior to the cleanup manually to ensure worst case. + let cleanup_marker = CleanupMarker { + era_reward_index: 0, + dapp_tiers_index: 0, + oldest_valid_era: T::EraRewardSpanLength::get().into(), + }; + HistoryCleanupMarker::::put(cleanup_marker); + + // Prepare completely filled up reward span and insert it into storage. + let mut reward_span = EraRewardSpan::<_>::new(); + (0..T::EraRewardSpanLength::get()).for_each(|era| { + assert_ok!(reward_span.push( + era as EraNumber, + EraReward { + staker_reward_pool: 1_000_000_000_000, + staked: 1_000_000_000_000, + dapp_reward_pool: 1_000_000_000_000, + }, + )); + }); + EraRewards::::insert(&cleanup_marker.era_reward_index, reward_span); + + // Prepare completely filled up tier rewards and insert it into storage. + DAppTiers::::insert( + &cleanup_marker.dapp_tiers_index, + DAppTierRewardsFor:: { + dapps: (0..T::MaxNumberOfContracts::get()) + .map(|dapp_id| (dapp_id as DAppId, 0)) + .collect::>() + .try_into() + .expect("Using `MaxNumberOfContracts` as length; QED."), + rewards: vec![1_000_000_000_000; T::NumberOfTiers::get() as usize] + .try_into() + .expect("Using `NumberOfTiers` as length; QED."), + period: 1, + }, ); - let first_era_span_index = 0; - assert!( - EraRewards::::contains_key(first_era_span_index), - "Sanity check - era reward span entry must exist." - ); - let first_period = 1; - assert!( - PeriodEnd::::contains_key(first_period), - "Sanity check - period end info must exist." - ); let block_number = System::::block_number(); - #[block] { DappStaking::::on_idle(block_number, Weight::MAX); } assert!( - !EraRewards::::contains_key(first_era_span_index), - "Entry should have been cleaned up." + !EraRewards::::contains_key(cleanup_marker.era_reward_index), + "Reward span should have been cleaned up." ); assert!( - !PeriodEnd::::contains_key(first_period), + !DAppTiers::::contains_key(cleanup_marker.dapp_tiers_index), "Period end info should have been cleaned up." ); } diff --git a/pallets/dapp-staking-v3/src/benchmarking/utils.rs b/pallets/dapp-staking-v3/src/benchmarking/utils.rs index 28dde52267..5226a8d61e 100644 --- a/pallets/dapp-staking-v3/src/benchmarking/utils.rs +++ b/pallets/dapp-staking-v3/src/benchmarking/utils.rs @@ -77,29 +77,6 @@ pub(crate) fn force_advance_to_next_era() { run_for_blocks::(One::one()); } -/// Advance blocks until the specified period has been reached. -/// -/// Function has no effect if period is already passed. -pub(super) fn _advance_to_period(period: PeriodNumber) { - assert!(period >= ActiveProtocolState::::get().period_number()); - while ActiveProtocolState::::get().period_number() < period { - run_for_blocks::(One::one()); - } -} - -/// Advance to the specified period, using the `force` approach. -pub(super) fn force_advance_to_period(period: PeriodNumber) { - assert!(period >= ActiveProtocolState::::get().period_number()); - while ActiveProtocolState::::get().period_number() < period { - force_advance_to_next_subperiod::(); - } -} - -/// Advance blocks until next period has been reached. -pub(super) fn _advance_to_next_period() { - _advance_to_period::(ActiveProtocolState::::get().period_number() + 1); -} - /// Advance blocks until next period has been reached. /// /// Relies on the `force` approach to advance one subperiod per block. @@ -114,14 +91,6 @@ pub(super) fn force_advance_to_next_period() { } } -/// Advance blocks until next period type has been reached. -pub(super) fn _advance_to_next_subperiod() { - let subperiod = ActiveProtocolState::::get().subperiod(); - while ActiveProtocolState::::get().subperiod() == subperiod { - run_for_blocks::(One::one()); - } -} - /// Use the `force` approach to advance to the next subperiod immediately in the next block. pub(super) fn force_advance_to_next_subperiod() { assert_ok!(DappStaking::::force(