Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: more accurate weights for staking heavy operations #725

Merged
merged 7 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pallets/parachain-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ benchmarks! {
Perquintill::from_percent(40),
Perquintill::from_percent(10)
);
}: _(RawOrigin::Root, inflation.collator.max_rate, inflation.collator.reward_rate.annual, inflation.delegator.max_rate, inflation.delegator.reward_rate.annual)

let candidate_pool_size = candidates.len() as u32;
}: _(RawOrigin::Root, inflation.collator.max_rate, inflation.collator.reward_rate.annual, inflation.delegator.max_rate, inflation.delegator.reward_rate.annual, candidate_pool_size)
verify {
assert_eq!(InflationConfig::<T>::get(), inflation);
candidates.into_iter().for_each(|candidate| {
Expand Down Expand Up @@ -627,7 +629,8 @@ benchmarks! {
let old = InflationConfig::<T>::get();
assert_eq!(LastRewardReduction::<T>::get(), BlockNumberFor::<T>::zero());
System::<T>::set_block_number(T::BLOCKS_PER_YEAR + BlockNumberFor::<T>::one());
}: _(RawOrigin::Signed(collator))
let candidate_pool_size = candidates.len() as u32;
}: _(RawOrigin::Signed(collator), candidate_pool_size)
verify {
let new = InflationConfig::<T>::get();
assert_eq!(LastRewardReduction::<T>::get(), BlockNumberFor::<T>::one());
Expand Down
23 changes: 20 additions & 3 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ pub mod pallet {
UnstakingIsEmpty,
/// Cannot claim rewards if empty.
RewardsNotFound,
/// Invalid input provided. The meaning of this error is
/// extrinsic-dependent.
InvalidInput,
}

#[pallet::event]
Expand Down Expand Up @@ -762,16 +765,22 @@ pub mod pallet {
///
/// Emits `RoundInflationSet`.
#[pallet::call_index(1)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::set_inflation(T::MaxTopCandidates::get(), T::MaxDelegatorsPerCollator::get()))]
#[pallet::weight(<T as pallet::Config>::WeightInfo::set_inflation(*current_collator_candidate_pool_size, T::MaxDelegatorsPerCollator::get()))]
pub fn set_inflation(
origin: OriginFor<T>,
collator_max_rate_percentage: Perquintill,
collator_annual_reward_rate_percentage: Perquintill,
delegator_max_rate_percentage: Perquintill,
delegator_annual_reward_rate_percentage: Perquintill,
current_collator_candidate_pool_size: u32,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;

ensure!(
current_collator_candidate_pool_size >= CandidatePool::<T>::count(),
Error::<T>::InvalidInput
);

// Update inflation and increment rewards
let (num_col, num_del) = Self::do_set_inflation(
T::BLOCKS_PER_YEAR,
Expand Down Expand Up @@ -1725,10 +1734,18 @@ pub mod pallet {
///
/// Emits `RoundInflationSet`.
#[pallet::call_index(20)]
#[pallet::weight(<T as Config>::WeightInfo::execute_scheduled_reward_change(T::MaxTopCandidates::get(), T::MaxDelegatorsPerCollator::get()))]
pub fn execute_scheduled_reward_change(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
#[pallet::weight(<T as Config>::WeightInfo::execute_scheduled_reward_change(*current_collator_candidate_pool_size, T::MaxDelegatorsPerCollator::get()))]
pub fn execute_scheduled_reward_change(
origin: OriginFor<T>,
current_collator_candidate_pool_size: u32,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;

ensure!(
current_collator_candidate_pool_size >= CandidatePool::<T>::count(),
Error::<T>::InvalidInput
);

let now = frame_system::Pallet::<T>::block_number();
let year = now / T::BLOCKS_PER_YEAR;

Expand Down
36 changes: 32 additions & 4 deletions pallets/parachain-staking/src/tests/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

//! Unit testing

use frame_support::assert_ok;
use frame_support::{assert_noop, assert_ok};
use pallet_authorship::EventHandler;
use sp_runtime::{traits::Zero, Perquintill};

use crate::{
mock::{roll_to_claim_rewards, ExtBuilder, RuntimeOrigin, StakePallet, System, Test, DECIMALS},
Config, InflationInfo, RewardRate, StakingInfo,
Config, Error, InflationInfo, RewardRate, StakingInfo,
};

#[test]
Expand Down Expand Up @@ -52,7 +52,8 @@ fn rewards_set_inflation() {
hundred,
hundred,
hundred,
hundred
hundred,
2
));
// rewards and counters should be set
(1..=5).for_each(|id| {
Expand Down Expand Up @@ -89,7 +90,10 @@ fn rewards_yearly_inflation_adjustment() {
});

// execute to trigger reward increment
assert_ok!(StakePallet::execute_scheduled_reward_change(RuntimeOrigin::signed(1)));
assert_ok!(StakePallet::execute_scheduled_reward_change(
RuntimeOrigin::signed(1),
2
));
(1..=5).for_each(|id| {
assert!(
!StakePallet::blocks_rewarded(id).is_zero(),
Expand Down Expand Up @@ -133,27 +137,51 @@ fn update_inflation() {
Perquintill::from_percent(100),
Perquintill::from_percent(100),
Perquintill::from_percent(100),
2
));
assert_ok!(StakePallet::set_inflation(
RuntimeOrigin::root(),
Perquintill::from_percent(100),
Perquintill::from_percent(0),
Perquintill::from_percent(100),
Perquintill::from_percent(100),
2
));
assert_ok!(StakePallet::set_inflation(
RuntimeOrigin::root(),
Perquintill::from_percent(100),
Perquintill::from_percent(100),
Perquintill::from_percent(0),
Perquintill::from_percent(100),
2
));
assert_ok!(StakePallet::set_inflation(
RuntimeOrigin::root(),
Perquintill::from_percent(100),
Perquintill::from_percent(100),
Perquintill::from_percent(100),
Perquintill::from_percent(0),
2
));
});
}

#[test]
fn too_small_candidate_size_provided_for_new_inflation() {
ExtBuilder::default()
.with_balances(vec![(1, 10), (2, 100)])
.with_collators(vec![(1, 10), (2, 10)])
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
StakePallet::set_inflation(
RuntimeOrigin::root(),
Perquintill::from_percent(0),
Perquintill::from_percent(100),
Perquintill::from_percent(100),
Perquintill::from_percent(100),
1
),
Error::<Test>::InvalidInput
);
});
}
28 changes: 25 additions & 3 deletions pallets/parachain-staking/src/tests/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ fn adjust_reward_rates() {
roll_to_claim_rewards(<Test as Config>::BLOCKS_PER_YEAR + 1, vec![]);
// reward reduction should not happen automatically anymore
assert_eq!(StakePallet::last_reward_reduction(), 0u64);
assert_ok!(StakePallet::execute_scheduled_reward_change(RuntimeOrigin::signed(1)));
assert_ok!(StakePallet::execute_scheduled_reward_change(
RuntimeOrigin::signed(1),
2
));
assert_eq!(StakePallet::last_reward_reduction(), 1u64);
let inflation_1 = InflationInfo::new(
<Test as Config>::BLOCKS_PER_YEAR,
Expand Down Expand Up @@ -363,7 +366,10 @@ fn adjust_reward_rates() {
roll_to_claim_rewards(2 * <Test as Config>::BLOCKS_PER_YEAR + 1, vec![]);
// reward reduction should not happen automatically anymore
assert_eq!(StakePallet::last_reward_reduction(), 1u64);
assert_ok!(StakePallet::execute_scheduled_reward_change(RuntimeOrigin::signed(1)));
assert_ok!(StakePallet::execute_scheduled_reward_change(
RuntimeOrigin::signed(1),
2
));
assert_eq!(StakePallet::last_reward_reduction(), 2u64);
let inflation_2 = InflationInfo::new(
<Test as Config>::BLOCKS_PER_YEAR,
Expand Down Expand Up @@ -393,7 +399,10 @@ fn adjust_reward_rates() {
roll_to_claim_rewards(3 * <Test as Config>::BLOCKS_PER_YEAR + 1, vec![]);
// reward reduction should not happen automatically anymore
assert_eq!(StakePallet::last_reward_reduction(), 2u64);
assert_ok!(StakePallet::execute_scheduled_reward_change(RuntimeOrigin::signed(1)));
assert_ok!(StakePallet::execute_scheduled_reward_change(
RuntimeOrigin::signed(1),
2
));
assert_eq!(StakePallet::last_reward_reduction(), 3u64);
let inflation_3 = InflationInfo::new(
<Test as Config>::BLOCKS_PER_YEAR,
Expand Down Expand Up @@ -994,3 +1003,16 @@ fn api_get_unclaimed_staking_rewards() {
assert!(StakePallet::get_unclaimed_staking_rewards(&3).is_zero());
});
}

#[test]
fn too_small_candidate_size_provided_for_reward_adjustment() {
ExtBuilder::default()
.with_balances(vec![(1, 10), (2, 100)])
.with_collators(vec![(1, 10), (2, 10)])
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
StakePallet::execute_scheduled_reward_change(RuntimeOrigin::signed(1), 1),
Error::<Test>::InvalidInput
);
});
}
125 changes: 63 additions & 62 deletions runtimes/peregrine/src/weights/parachain_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,42 @@ impl<T: frame_system::Config> parachain_staking::WeightInfo for WeightInfo<T> {
.saturating_add(Weight::from_parts(0, 0))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: ParachainStaking CandidatePool (r:76 w:0)
/// Proof: ParachainStaking CandidatePool (max_values: None, max_size: Some(1790), added: 4265, mode: MaxEncodedLen)
/// Storage: ParachainStaking BlocksAuthored (r:75 w:0)
/// Proof: ParachainStaking BlocksAuthored (max_values: None, max_size: Some(48), added: 2523, mode: MaxEncodedLen)
/// Storage: ParachainStaking BlocksRewarded (r:2700 w:2700)
/// Proof: ParachainStaking BlocksRewarded (max_values: None, max_size: Some(48), added: 2523, mode: MaxEncodedLen)
/// Storage: ParachainStaking Rewards (r:2700 w:2700)
/// Proof: ParachainStaking Rewards (max_values: None, max_size: Some(56), added: 2531, mode: MaxEncodedLen)
/// Storage: ParachainStaking TotalCollatorStake (r:1 w:0)
/// Proof: ParachainStaking TotalCollatorStake (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen)
/// Storage: ParachainStaking InflationConfig (r:1 w:1)
/// Proof: ParachainStaking InflationConfig (max_values: Some(1), max_size: Some(96), added: 591, mode: MaxEncodedLen)
/// Storage: ParachainStaking CounterForCandidatePool (r:1 w:0)
/// Proof: ParachainStaking CounterForCandidatePool (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)

/// Storage: `ParachainStaking::CounterForCandidatePool` (r:1 w:0)
/// Proof: `ParachainStaking::CounterForCandidatePool` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::CandidatePool` (r:76 w:0)
/// Proof: `ParachainStaking::CandidatePool` (`max_values`: None, `max_size`: Some(1790), added: 4265, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::BlocksAuthored` (r:75 w:0)
/// Proof: `ParachainStaking::BlocksAuthored` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::BlocksRewarded` (r:2700 w:2700)
/// Proof: `ParachainStaking::BlocksRewarded` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::Rewards` (r:2700 w:2700)
/// Proof: `ParachainStaking::Rewards` (`max_values`: None, `max_size`: Some(56), added: 2531, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::TotalCollatorStake` (r:1 w:0)
/// Proof: `ParachainStaking::TotalCollatorStake` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::InflationConfig` (r:1 w:1)
/// Proof: `ParachainStaking::InflationConfig` (`max_values`: Some(1), `max_size`: Some(96), added: 591, mode: `MaxEncodedLen`)
/// The range of component `n` is `[0, 75]`.
/// The range of component `m` is `[0, 35]`.
fn set_inflation(n: u32, m: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0 + n * (3647 ±0) + m * (7587 ±0)`
// Estimated: `383346 + n * (77565 ±30_589) + m * (151620 ±65_549)`
// Minimum execution time: 749_094_000 picoseconds.
Weight::from_parts(749_094_000, 0)
.saturating_add(Weight::from_parts(0, 383346))
// Standard Error: 86_792_174
.saturating_add(Weight::from_parts(100_338_066, 0).saturating_mul(n.into()))
// Standard Error: 185_983_231
.saturating_add(Weight::from_parts(214_452_085, 0).saturating_mul(m.into()))
// Measured: `0 + m * (7573 ±0) + n * (3709 ±0)`
// Estimated: `183222 + m * (64823 ±2_167) + n * (31965 ±1_009)`
// Minimum execution time: 704_611_000 picoseconds.
Weight::from_parts(707_105_000, 0)
.saturating_add(Weight::from_parts(0, 183222))
// Standard Error: 4_876_636
.saturating_add(Weight::from_parts(150_694_630, 0).saturating_mul(n.into()))
// Standard Error: 10_470_032
.saturating_add(Weight::from_parts(290_425_882, 0).saturating_mul(m.into()))
.saturating_add(T::DbWeight::get().reads(152))
.saturating_add(T::DbWeight::get().reads((30_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().reads((60_u64).saturating_mul(m.into())))
.saturating_add(T::DbWeight::get().reads((27_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().reads((51_u64).saturating_mul(m.into())))
.saturating_add(T::DbWeight::get().writes(145))
.saturating_add(T::DbWeight::get().writes((28_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes((60_u64).saturating_mul(m.into())))
.saturating_add(Weight::from_parts(0, 77565).saturating_mul(n.into()))
.saturating_add(Weight::from_parts(0, 151620).saturating_mul(m.into()))
.saturating_add(T::DbWeight::get().writes((25_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes((51_u64).saturating_mul(m.into())))
.saturating_add(Weight::from_parts(0, 64823).saturating_mul(m.into()))
.saturating_add(Weight::from_parts(0, 31965).saturating_mul(n.into()))
}
/// Storage: ParachainStaking MaxSelectedCandidates (r:1 w:1)
/// Proof: ParachainStaking MaxSelectedCandidates (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
Expand Down Expand Up @@ -674,43 +675,43 @@ impl<T: frame_system::Config> parachain_staking::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(2))
}
/// Storage: ParachainStaking LastRewardReduction (r:1 w:1)
/// Proof: ParachainStaking LastRewardReduction (max_values: Some(1), max_size: Some(8), added: 503, mode: MaxEncodedLen)
/// Storage: ParachainStaking InflationConfig (r:1 w:1)
/// Proof: ParachainStaking InflationConfig (max_values: Some(1), max_size: Some(96), added: 591, mode: MaxEncodedLen)
/// Storage: ParachainStaking CandidatePool (r:76 w:0)
/// Proof: ParachainStaking CandidatePool (max_values: None, max_size: Some(1790), added: 4265, mode: MaxEncodedLen)
/// Storage: ParachainStaking BlocksAuthored (r:75 w:0)
/// Proof: ParachainStaking BlocksAuthored (max_values: None, max_size: Some(48), added: 2523, mode: MaxEncodedLen)
/// Storage: ParachainStaking BlocksRewarded (r:2700 w:2700)
/// Proof: ParachainStaking BlocksRewarded (max_values: None, max_size: Some(48), added: 2523, mode: MaxEncodedLen)
/// Storage: ParachainStaking Rewards (r:2700 w:2700)
/// Proof: ParachainStaking Rewards (max_values: None, max_size: Some(56), added: 2531, mode: MaxEncodedLen)
/// Storage: ParachainStaking TotalCollatorStake (r:1 w:0)
/// Proof: ParachainStaking TotalCollatorStake (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen)
/// Storage: ParachainStaking CounterForCandidatePool (r:1 w:0)
/// Proof: ParachainStaking CounterForCandidatePool (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: `ParachainStaking::CounterForCandidatePool` (r:1 w:0)
/// Proof: `ParachainStaking::CounterForCandidatePool` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::LastRewardReduction` (r:1 w:1)
/// Proof: `ParachainStaking::LastRewardReduction` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::InflationConfig` (r:1 w:1)
/// Proof: `ParachainStaking::InflationConfig` (`max_values`: Some(1), `max_size`: Some(96), added: 591, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::CandidatePool` (r:76 w:0)
/// Proof: `ParachainStaking::CandidatePool` (`max_values`: None, `max_size`: Some(1790), added: 4265, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::BlocksAuthored` (r:75 w:0)
/// Proof: `ParachainStaking::BlocksAuthored` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::BlocksRewarded` (r:2700 w:2700)
/// Proof: `ParachainStaking::BlocksRewarded` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::Rewards` (r:2700 w:2700)
/// Proof: `ParachainStaking::Rewards` (`max_values`: None, `max_size`: Some(56), added: 2531, mode: `MaxEncodedLen`)
/// Storage: `ParachainStaking::TotalCollatorStake` (r:1 w:0)
/// Proof: `ParachainStaking::TotalCollatorStake` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// The range of component `n` is `[0, 75]`.
/// The range of component `m` is `[0, 35]`.
fn execute_scheduled_reward_change(n: u32, m: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0 + n * (3587 ±0) + m * (7587 ±0)`
// Estimated: `383849 + n * (77565 ±0) + m * (151620 ±65_549)`
// Minimum execution time: 449_942_000 picoseconds.
Weight::from_parts(449_942_000, 0)
.saturating_add(Weight::from_parts(0, 383849))
// Standard Error: 84_159_623
.saturating_add(Weight::from_parts(100_410_008, 0).saturating_mul(n.into()))
// Standard Error: 180_342_051
.saturating_add(Weight::from_parts(208_232_902, 0).saturating_mul(m.into()))
// Measured: `0 + m * (7573 ±0) + n * (3647 ±0)`
// Estimated: `183222 + m * (64823 ±2_167) + n * (31965 ±1_009)`
// Minimum execution time: 683_160_000 picoseconds.
Weight::from_parts(687_351_000, 0)
.saturating_add(Weight::from_parts(0, 183222))
// Standard Error: 4_643_820
.saturating_add(Weight::from_parts(141_740_256, 0).saturating_mul(n.into()))
// Standard Error: 9_970_180
.saturating_add(Weight::from_parts(280_674_034, 0).saturating_mul(m.into()))
.saturating_add(T::DbWeight::get().reads(153))
.saturating_add(T::DbWeight::get().reads((30_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().reads((60_u64).saturating_mul(m.into())))
.saturating_add(T::DbWeight::get().reads((27_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().reads((51_u64).saturating_mul(m.into())))
.saturating_add(T::DbWeight::get().writes(146))
.saturating_add(T::DbWeight::get().writes((28_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes((60_u64).saturating_mul(m.into())))
.saturating_add(Weight::from_parts(0, 77565).saturating_mul(n.into()))
.saturating_add(Weight::from_parts(0, 151620).saturating_mul(m.into()))
.saturating_add(T::DbWeight::get().writes((25_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes((51_u64).saturating_mul(m.into())))
.saturating_add(Weight::from_parts(0, 64823).saturating_mul(m.into()))
.saturating_add(Weight::from_parts(0, 31965).saturating_mul(n.into()))
}
}

Expand Down Expand Up @@ -761,7 +762,7 @@ mod tests {
.max_extrinsic
.unwrap_or_else(<sp_weights::Weight as sp_runtime::traits::Bounded>::max_value)
.proof_size()
> 383346
> 183222
);
}
#[test]
Expand Down Expand Up @@ -977,7 +978,7 @@ mod tests {
.max_extrinsic
.unwrap_or_else(<sp_weights::Weight as sp_runtime::traits::Bounded>::max_value)
.proof_size()
> 383849
> 183222
);
}
}
Loading
Loading