Skip to content

Commit

Permalink
Go back to block based staking rounds and make inflation dynamic (slo…
Browse files Browse the repository at this point in the history
…t based) (#2690)

* make staking rounds block based again and inflation slot based

* remove unused imports

* create migration skeleton

* implement migration

* integrate round migration to common migrations

* apply proportion duration to the current round

* compile benchs and test

* add moonbase migration to multiply round length by 2

* compute round duration before round update

* fix some bugs

* fix some rust tests

* fix migration

* apply suggestions

* First staking rewards at round 3

* revert moonwall config local changes

* fix rounds per year

* fix periods calculation in reset_round()

* fix rustc warning

* fmt

* rework compute issuance

* Remove Staked storage item

* Remove Staked storage item on rust tests

* fix two rust tests more

* fix test_on_initialize_weights

* fix payouts_follow_delegation_changes test

* Update pallets/parachain-staking/src/lib.rs

Co-authored-by: Rodrigo Quelhas <[email protected]>

* Comment moonbase migration

* typo

* Update runtime/moonbase/src/migrations.rs

Co-authored-by: Rodrigo Quelhas <[email protected]>

* Update runtime/moonbase/src/migrations.rs

* Update pallets/parachain-staking/src/lib.rs

---------

Co-authored-by: Agusrodri <[email protected]>
Co-authored-by: Rodrigo Quelhas <[email protected]>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent 85b81ca commit 3be4e9a
Show file tree
Hide file tree
Showing 20 changed files with 467 additions and 371 deletions.
18 changes: 11 additions & 7 deletions pallets/parachain-staking/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::{
AwardedPts, BalanceOf, BottomDelegations, Call, CandidateBondLessRequest, Config,
DelegationAction, EnableMarkingOffline, Pallet, ParachainBondConfig, ParachainBondInfo, Points,
Range, RewardPayment, Round, ScheduledRequest, Staked, TopDelegations,
Range, RewardPayment, Round, ScheduledRequest, TopDelegations,
};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_support::traits::{Currency, Get, OnFinalize, OnInitialize};
Expand Down Expand Up @@ -205,7 +205,7 @@ fn roll_to_and_author<T: Config>(round_delay: u32, author: T::AccountId) {
let total_rounds = round_delay + 1u32;
let round_length: BlockNumberFor<T> = Pallet::<T>::round().length.into();
let mut now = <frame_system::Pallet<T>>::block_number() + 1u32.into();
let first: BlockNumberFor<T> = (Pallet::<T>::round().first as u32).into();
let first: BlockNumberFor<T> = Pallet::<T>::round().first;
let end = first + (round_length * total_rounds.into());
while now < end {
parachain_staking_on_finalize::<T>(author.clone());
Expand Down Expand Up @@ -1542,15 +1542,19 @@ benchmarks! {

prepare_staking_payouts {
let reward_delay = <<T as Config>::RewardPaymentDelay as Get<u32>>::get();
let round = reward_delay + 2u32;
let payout_round = round - reward_delay;
let round = crate::RoundInfo {
current: reward_delay + 2u32,
length: 10,
first: 5u32.into(),
first_slot: 5,
};
let current_slot = 15;
let payout_round = round.current - reward_delay;
// may need:
// <Points<T>>
// <Staked<T>>
// <ParachainBondInfo<T>>
// ensure parachain bond account exists so that deposit_into_existing succeeds
<Points<T>>::insert(payout_round, 100);
<Staked<T>>::insert(payout_round, min_candidate_stk::<T>());

// set an account in the bond config so that we will measure the payout to it
let account = create_funded_user::<T>(
Expand All @@ -1563,7 +1567,7 @@ benchmarks! {
percent: Percent::from_percent(50),
});

}: { Pallet::<T>::prepare_staking_payouts(round); }
}: { Pallet::<T>::prepare_staking_payouts(round, current_slot); }
verify {
}

Expand Down
15 changes: 9 additions & 6 deletions pallets/parachain-staking/src/delegation_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<T: Config> Pallet<T> {
let mut scheduled_requests = <DelegationScheduledRequests<T>>::get(&collator);

let actual_weight =
T::WeightInfo::schedule_revoke_delegation(scheduled_requests.len() as u32);
<T as Config>::WeightInfo::schedule_revoke_delegation(scheduled_requests.len() as u32);

ensure!(
!scheduled_requests
Expand Down Expand Up @@ -131,8 +131,9 @@ impl<T: Config> Pallet<T> {
let mut state = <DelegatorState<T>>::get(&delegator).ok_or(<Error<T>>::DelegatorDNE)?;
let mut scheduled_requests = <DelegationScheduledRequests<T>>::get(&collator);

let actual_weight =
T::WeightInfo::schedule_delegator_bond_less(scheduled_requests.len() as u32);
let actual_weight = <T as Config>::WeightInfo::schedule_delegator_bond_less(
scheduled_requests.len() as u32,
);

ensure!(
!scheduled_requests
Expand Down Expand Up @@ -211,7 +212,7 @@ impl<T: Config> Pallet<T> {
let mut state = <DelegatorState<T>>::get(&delegator).ok_or(<Error<T>>::DelegatorDNE)?;
let mut scheduled_requests = <DelegationScheduledRequests<T>>::get(&collator);
let actual_weight =
T::WeightInfo::cancel_delegation_request(scheduled_requests.len() as u32);
<T as Config>::WeightInfo::cancel_delegation_request(scheduled_requests.len() as u32);

let request =
Self::cancel_request_with_state(&delegator, &mut state, &mut scheduled_requests)
Expand Down Expand Up @@ -270,7 +271,8 @@ impl<T: Config> Pallet<T> {

match request.action {
DelegationAction::Revoke(amount) => {
let actual_weight = T::WeightInfo::execute_delegator_revoke_delegation_worst();
let actual_weight =
<T as Config>::WeightInfo::execute_delegator_revoke_delegation_worst();

// revoking last delegation => leaving set of delegators
let leaving = if state.delegations.0.len() == 1usize {
Expand Down Expand Up @@ -321,7 +323,8 @@ impl<T: Config> Pallet<T> {
Ok(Some(actual_weight).into())
}
DelegationAction::Decrease(_) => {
let actual_weight = T::WeightInfo::execute_delegator_revoke_delegation_worst();
let actual_weight =
<T as Config>::WeightInfo::execute_delegator_revoke_delegation_worst();

// remove from pending requests
let amount = scheduled_requests.remove(request_idx).action.amount();
Expand Down
12 changes: 8 additions & 4 deletions pallets/parachain-staking/src/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ use sp_runtime::{Perbill, RuntimeDebug};
use substrate_fixed::transcendental::pow as floatpow;
use substrate_fixed::types::I64F64;

// Milliseconds per year
const MS_PER_YEAR: u64 = 31_557_600_000;

fn rounds_per_year<T: Config>() -> u32 {
let blocks_per_round = <Pallet<T>>::round().length;
T::SlotsPerYear::get() / blocks_per_round
let blocks_per_round = <Pallet<T>>::round().length as u64;
let blocks_per_year = MS_PER_YEAR / T::BlockTime::get();
(blocks_per_year / blocks_per_round) as u32
}

#[derive(
Expand Down Expand Up @@ -133,8 +137,8 @@ impl<Balance> InflationInfo<Balance> {
}
/// Reset round inflation rate based on changes to round length
pub fn reset_round<T: Config>(&mut self, new_length: u32) {
let periods = T::SlotsPerYear::get() / new_length;
self.round = perbill_annual_to_perbill_round(self.annual, periods);
let periods = (MS_PER_YEAR / T::BlockTime::get()) / (new_length as u64);
self.round = perbill_annual_to_perbill_round(self.annual, periods as u32);
}
/// Set staking expectations
pub fn set_expectations(&mut self, expect: Range<Balance>) {
Expand Down
Loading

0 comments on commit 3be4e9a

Please sign in to comment.