diff --git a/pallets/parachain-staking/src/inflation.rs b/pallets/parachain-staking/src/inflation.rs index f2181565e5..f6ec910b3d 100644 --- a/pallets/parachain-staking/src/inflation.rs +++ b/pallets/parachain-staking/src/inflation.rs @@ -26,13 +26,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 +82,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 +104,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 +117,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,