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

Inject blocks per year and blocks per round to the inflation info #2

Open
wants to merge 2 commits into
base: subsocial/staking
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Inject blocks per year and blocks per round to the inflation info
TarekkMA committed May 20, 2022
commit 1bb4b657bc8cbb039b6151fa684083bc94e30f5f
52 changes: 37 additions & 15 deletions pallets/parachain-staking/src/inflation.rs
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! 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<T: Config>() -> u32 {
let blocks_per_round = <Pallet<T>>::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<T: Config>(annual: Range<Perbill>) -> Range<Perbill> {
let periods = rounds_per_year::<T>();
pub fn annual_to_round(
annual: Range<Perbill>,
blocks_per_round: u32,
blocks_per_year: u32,
) -> Range<Perbill> {
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<T: Config>(round: Range<Perbill>) -> Range<BalanceOf
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Eq, PartialEq, Clone, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct InflationInfo<Balance> {
/// 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<Balance>,
/// Annual inflation range
@@ -111,24 +118,39 @@ pub struct InflationInfo<Balance> {
}

impl<Balance> InflationInfo<Balance> {
pub fn new<T: Config>(
pub fn new(
blocks_per_round: u32,
blocks_per_year: u32,
annual: Range<Perbill>,
expect: Range<Balance>,
) -> InflationInfo<Balance> {
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::<T>(annual),
round,
}
}
/// Set round inflation range according to input annual inflation range
pub fn set_round_from_annual<T: Config>(&mut self, new: Range<Perbill>) {
self.round = annual_to_round::<T>(new);
pub fn set_round_from_annual(&mut self, new: Range<Perbill>, 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<Balance>) {
2 changes: 1 addition & 1 deletion pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -755,7 +755,7 @@ pub mod pallet {
let mut config = <InflationConfig<T>>::get();
ensure!(config.annual != schedule, Error::<T>::NoWritingSameValue);
config.annual = schedule;
config.set_round_from_annual::<T>(schedule);
config.set_round_from_annual(schedule, <Round<T>>::get().length);
Self::deposit_event(Event::InflationSet {
annual_min: config.annual.min,
annual_ideal: config.annual.ideal,
2 changes: 2 additions & 0 deletions pallets/parachain-staking/src/mock.rs
Original file line number Diff line number Diff line change
@@ -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,