From 1bb4b657bc8cbb039b6151fa684083bc94e30f5f Mon Sep 17 00:00:00 2001 From: Tarek Mohamed Abdalla Date: Fri, 20 May 2022 11:05:25 +0200 Subject: [PATCH 1/2] Inject blocks per year and blocks per round to the inflation info --- pallets/parachain-staking/src/inflation.rs | 52 +++++++++++++++------- pallets/parachain-staking/src/lib.rs | 2 +- pallets/parachain-staking/src/mock.rs | 2 + 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/pallets/parachain-staking/src/inflation.rs b/pallets/parachain-staking/src/inflation.rs index f2181565e5..2df3b344f7 100644 --- a/pallets/parachain-staking/src/inflation.rs +++ b/pallets/parachain-staking/src/inflation.rs @@ -15,6 +15,7 @@ // along with Moonbeam. If not, see . //! Helper methods for computing issuance based on inflation +use std::ops::Div; use crate::pallet::{BalanceOf, Config, Pallet}; use frame_support::traits::Currency; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; @@ -26,13 +27,11 @@ use sp_runtime::{Perbill, RuntimeDebug}; use substrate_fixed::transcendental::pow as floatpow; use substrate_fixed::types::{I32F32, I64F64}; -const SECONDS_PER_YEAR: u32 = 31557600; -const SECONDS_PER_BLOCK: u32 = 12; -pub const BLOCKS_PER_YEAR: u32 = SECONDS_PER_YEAR / SECONDS_PER_BLOCK; - -fn rounds_per_year() -> u32 { - let blocks_per_round = >::round().length; - BLOCKS_PER_YEAR / blocks_per_round +fn rounds_per_year( + blocks_per_round: u32, + blocks_per_year: u32, +) -> u32 { + blocks_per_year / blocks_per_round } #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] @@ -84,8 +83,12 @@ pub fn perbill_annual_to_perbill_round( } } /// Convert annual inflation rate range to round inflation range -pub fn annual_to_round(annual: Range) -> Range { - let periods = rounds_per_year::(); +pub fn annual_to_round( + annual: Range, + blocks_per_round: u32, + blocks_per_year: u32, +) -> Range { + let periods = rounds_per_year(blocks_per_round, blocks_per_year); perbill_annual_to_perbill_round(annual, periods) } @@ -102,6 +105,10 @@ pub fn round_issuance_range(round: Range) -> Range { + /// blocks in a round + pub blocks_per_round: u32, + /// How many blocks are in a year. + pub blocks_per_year: u32, /// Staking expectations pub expect: Range, /// Annual inflation range @@ -111,24 +118,39 @@ pub struct InflationInfo { } impl InflationInfo { - pub fn new( + pub fn new( + blocks_per_round: u32, + blocks_per_year: u32, annual: Range, expect: Range, ) -> InflationInfo { + let round = annual_to_round( + annual, + blocks_per_round, + blocks_per_year, + ); InflationInfo { + blocks_per_year, + blocks_per_round, expect, annual, - round: annual_to_round::(annual), + round, } } /// Set round inflation range according to input annual inflation range - pub fn set_round_from_annual(&mut self, new: Range) { - self.round = annual_to_round::(new); + pub fn set_round_from_annual(&mut self, new: Range, blocks_per_round: u32) { + self.blocks_per_round = blocks_per_round; + self.round = annual_to_round( + new, + self.blocks_per_round, + self.blocks_per_year, + ); } /// Reset round inflation rate based on changes to round length pub fn reset_round(&mut self, new_length: u32) { - let periods = BLOCKS_PER_YEAR / new_length; - self.round = perbill_annual_to_perbill_round(self.annual, periods); + self.blocks_per_round = new_length; + let rounds_per_year = self.blocks_per_year / self.blocks_per_round; + self.round = perbill_annual_to_perbill_round(self.annual, rounds_per_year); } /// Set staking expectations pub fn set_expectations(&mut self, expect: Range) { diff --git a/pallets/parachain-staking/src/lib.rs b/pallets/parachain-staking/src/lib.rs index ba4eafe3ea..051ed595bd 100644 --- a/pallets/parachain-staking/src/lib.rs +++ b/pallets/parachain-staking/src/lib.rs @@ -755,7 +755,7 @@ pub mod pallet { let mut config = >::get(); ensure!(config.annual != schedule, Error::::NoWritingSameValue); config.annual = schedule; - config.set_round_from_annual::(schedule); + config.set_round_from_annual(schedule, >::get().length); Self::deposit_event(Event::InflationSet { annual_min: config.annual.min, annual_ideal: config.annual.ideal, diff --git a/pallets/parachain-staking/src/mock.rs b/pallets/parachain-staking/src/mock.rs index 06e2709602..71d9906d12 100644 --- a/pallets/parachain-staking/src/mock.rs +++ b/pallets/parachain-staking/src/mock.rs @@ -161,6 +161,8 @@ impl Default for ExtBuilder { delegations: vec![], collators: vec![], inflation: InflationInfo { + blocks_per_round: MinBlocksPerRound::get(), + blocks_per_year: 31557600 / 12, expect: Range { min: 700, ideal: 700, From bbea752b658eb5a2141683fb9b163c7673b420ac Mon Sep 17 00:00:00 2001 From: Tarek Mohamed Abdalla Date: Fri, 20 May 2022 15:29:21 +0200 Subject: [PATCH 2/2] remove unnecessary import --- pallets/parachain-staking/src/inflation.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/parachain-staking/src/inflation.rs b/pallets/parachain-staking/src/inflation.rs index 2df3b344f7..f6ec910b3d 100644 --- a/pallets/parachain-staking/src/inflation.rs +++ b/pallets/parachain-staking/src/inflation.rs @@ -15,7 +15,6 @@ // along with Moonbeam. If not, see . //! Helper methods for computing issuance based on inflation -use std::ops::Div; use crate::pallet::{BalanceOf, Config, Pallet}; use frame_support::traits::Currency; use parity_scale_codec::{Decode, Encode, MaxEncodedLen};