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

Go back to block based staking rounds and make inflation dynamic (slot based) #2690

Merged
merged 32 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4cc6ad4
make staking rounds block based again and inflation slot based
librelois Feb 26, 2024
7081f63
remove unused imports
librelois Feb 26, 2024
4bca269
create migration skeleton
librelois Feb 26, 2024
6569530
implement migration
librelois Feb 27, 2024
c767ee5
integrate round migration to common migrations
librelois Feb 27, 2024
6f4ebca
apply proportion duration to the current round
librelois Feb 28, 2024
c552dd5
compile benchs and test
librelois Feb 28, 2024
eac1c7a
add moonbase migration to multiply round length by 2
librelois Feb 28, 2024
7a40e08
compute round duration before round update
librelois Feb 28, 2024
82d249d
fix some bugs
librelois Feb 29, 2024
a55186e
fix some rust tests
librelois Feb 29, 2024
b8074d9
fix migration
librelois Mar 1, 2024
e284107
Merge branch 'master' into elois-rework-staking-rounds
librelois Mar 1, 2024
2dad7f9
apply suggestions
librelois Mar 1, 2024
e6bc039
First staking rewards at round 3
librelois Mar 1, 2024
03c751e
revert moonwall config local changes
librelois Mar 1, 2024
00f1935
fix rounds per year
librelois Mar 1, 2024
09f610b
fix periods calculation in reset_round()
Agusrodri Mar 1, 2024
122681a
fix rustc warning
librelois Mar 1, 2024
77049b8
fmt
librelois Mar 1, 2024
1335e31
rework compute issuance
librelois Mar 1, 2024
43e87c3
Remove Staked storage item
librelois Mar 1, 2024
ef7fdda
Remove Staked storage item on rust tests
librelois Mar 1, 2024
17fa448
fix two rust tests more
Agusrodri Mar 1, 2024
ec43a24
fix test_on_initialize_weights
Agusrodri Mar 1, 2024
9cbe017
fix payouts_follow_delegation_changes test
Agusrodri Mar 3, 2024
2953eb9
Update pallets/parachain-staking/src/lib.rs
librelois Mar 4, 2024
08472ab
Comment moonbase migration
librelois Mar 4, 2024
4b65251
typo
librelois Mar 4, 2024
e9dab8b
Update runtime/moonbase/src/migrations.rs
librelois Mar 4, 2024
96336ef
Update runtime/moonbase/src/migrations.rs
RomarQ Mar 4, 2024
6b2f75c
Update pallets/parachain-staking/src/lib.rs
RomarQ Mar 4, 2024
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
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably have a new helper function named blocks_per_year and replace MS_PER_YEAR / T::BlockTime::get() by it everywhere.

(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
Loading