Skip to content

Commit

Permalink
Benchmarks part1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Jan 16, 2024
1 parent 8285faa commit 2051546
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 57 deletions.
70 changes: 44 additions & 26 deletions pallets/dapp-staking-v3/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>();
assert_eq!(dapp_staking_events.len(), x as usize);
dapp_staking_events.iter().for_each(|e| {
Expand Down Expand Up @@ -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::<T>();
assert_eq!(dapp_staking_events.len(), x as usize);
dapp_staking_events.iter().for_each(|e| {
Expand Down Expand Up @@ -662,7 +662,7 @@ mod benchmarks {
// Advance enough eras so dApp reward can be claimed.
force_advance_to_next_subperiod::<T>();

// 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::<T>::mutate(|config| {
let max_number_of_contracts: u16 = T::MaxNumberOfContracts::get().try_into().unwrap();
config.number_of_slots = max_number_of_contracts;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -960,39 +960,57 @@ mod benchmarks {
// Prepare init config (protocol state, tier params & config, etc.)
initial_config::<T>();

// 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>(T::EraRewardSpanLength::get());

// Advance enough periods to make cleanup feasible.
let retention_period = T::RewardRetentionInPeriods::get();
force_advance_to_period::<T>(
ActiveProtocolState::<T>::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::<T>::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::<T>::insert(&cleanup_marker.era_reward_index, reward_span);

// Prepare completely filled up tier rewards and insert it into storage.
DAppTiers::<T>::insert(
&cleanup_marker.dapp_tiers_index,
DAppTierRewardsFor::<T> {
dapps: (0..T::MaxNumberOfContracts::get())
.map(|dapp_id| (dapp_id as DAppId, 0))
.collect::<BTreeMap<DAppId, TierId>>()
.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::<T>::contains_key(first_era_span_index),
"Sanity check - era reward span entry must exist."
);
let first_period = 1;
assert!(
PeriodEnd::<T>::contains_key(first_period),
"Sanity check - period end info must exist."
);
let block_number = System::<T>::block_number();

#[block]
{
DappStaking::<T>::on_idle(block_number, Weight::MAX);
}

assert!(
!EraRewards::<T>::contains_key(first_era_span_index),
"Entry should have been cleaned up."
!EraRewards::<T>::contains_key(cleanup_marker.era_reward_index),
"Reward span should have been cleaned up."
);
assert!(
!PeriodEnd::<T>::contains_key(first_period),
!DAppTiers::<T>::contains_key(cleanup_marker.dapp_tiers_index),
"Period end info should have been cleaned up."
);
}
Expand Down
31 changes: 0 additions & 31 deletions pallets/dapp-staking-v3/src/benchmarking/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,6 @@ pub(crate) fn force_advance_to_next_era<T: Config>() {
run_for_blocks::<T>(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<T: Config>(period: PeriodNumber) {
assert!(period >= ActiveProtocolState::<T>::get().period_number());
while ActiveProtocolState::<T>::get().period_number() < period {
run_for_blocks::<T>(One::one());
}
}

/// Advance to the specified period, using the `force` approach.
pub(super) fn force_advance_to_period<T: Config>(period: PeriodNumber) {
assert!(period >= ActiveProtocolState::<T>::get().period_number());
while ActiveProtocolState::<T>::get().period_number() < period {
force_advance_to_next_subperiod::<T>();
}
}

/// Advance blocks until next period has been reached.
pub(super) fn _advance_to_next_period<T: Config>() {
_advance_to_period::<T>(ActiveProtocolState::<T>::get().period_number() + 1);
}

/// Advance blocks until next period has been reached.
///
/// Relies on the `force` approach to advance one subperiod per block.
Expand All @@ -114,14 +91,6 @@ pub(super) fn force_advance_to_next_period<T: Config>() {
}
}

/// Advance blocks until next period type has been reached.
pub(super) fn _advance_to_next_subperiod<T: Config>() {
let subperiod = ActiveProtocolState::<T>::get().subperiod();
while ActiveProtocolState::<T>::get().subperiod() == subperiod {
run_for_blocks::<T>(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<T: Config>() {
assert_ok!(DappStaking::<T>::force(
Expand Down

0 comments on commit 2051546

Please sign in to comment.