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

staking inflation #232

Merged
merged 25 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ moonbeam-runtime = { path = "../runtime" }
moonbeam-rpc-txpool = { path = "../client/rpc/txpool" }
moonbeam-rpc-primitives-txpool = { path = "../primitives/rpc/txpool" }
author-inherent = { path = "../pallets/author-inherent"}
stake = { path = "../pallets/stake"}

# Substrate dependencies
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
Expand Down
9 changes: 8 additions & 1 deletion node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use moonbeam_runtime::{
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::ChainType;
use serde::{Deserialize, Serialize};
use stake::InflationSchedule;
use std::collections::BTreeMap;
use std::str::FromStr;

Expand Down Expand Up @@ -60,6 +61,8 @@ pub fn development_chain_spec() -> ChainSpec {
None,
100_000 * GLMR,
)],
(100_000 * GLMR).into(),
(10 * GLMR).into(),
vec![AccountId::from_str("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").unwrap()],
Default::default(), // para_id
1281, //ChainId
Expand Down Expand Up @@ -92,6 +95,8 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
None,
100_000 * GLMR,
)],
(100_000 * GLMR).into(),
(10 * GLMR).into(),
vec![AccountId::from_str("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").unwrap()],
para_id,
1280, //ChainId
Expand All @@ -111,6 +116,8 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
fn testnet_genesis(
root_key: AccountId,
stakers: Vec<(AccountId, Option<AccountId>, Balance)>,
stake_expectations: InflationSchedule<Balance>,
round_issuance: InflationSchedule<Balance>,
endowed_accounts: Vec<AccountId>,
para_id: ParaId,
chain_id: u64,
Expand Down Expand Up @@ -138,6 +145,6 @@ fn testnet_genesis(
accounts: BTreeMap::new(),
}),
pallet_ethereum: Some(EthereumConfig {}),
stake: Some(StakeConfig { stakers }),
stake: Some(StakeConfig { stakers, stake_expectations, round_issuance }),
}
}
2 changes: 1 addition & 1 deletion pallets/stake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std = [
"pallet-balances/std",
"pallet-staking/std",
"parity-scale-codec/std",
"serde/std",
"serde",
"sp-std/std",
"sp-runtime/std",
]
163 changes: 163 additions & 0 deletions pallets/stake/src/inflation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//! Helper methods for computing issuance based on inflation
use crate::{BalanceOf, Config};
use frame_support::traits::{Currency, Get};
use parity_scale_codec::{Decode, Encode};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::{Perbill, RuntimeDebug};

const SECONDS_PER_YEAR: u32 = 31557600;
const SECONDS_PER_BLOCK: u32 = 6;
const BLOCKS_PER_YEAR: u32 = SECONDS_PER_YEAR / SECONDS_PER_BLOCK;

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Eq, PartialEq, Clone, Encode, Decode, Default, RuntimeDebug)]
pub struct InflationSchedule<T: Ord> {
pub min: T,
pub ideal: T,
pub max: T,
}

impl<T: Ord> InflationSchedule<T> {
pub fn is_valid(&self) -> bool {
self.max >= self.ideal && self.ideal >= self.min
}
pub fn is_one(&self) -> bool {
self.max == self.ideal && self.ideal == self.min
}
}

impl<T: Ord + Copy> From<T> for InflationSchedule<T> {
fn from(other: T) -> InflationSchedule<T> {
InflationSchedule {
min: other,
ideal: other,
max: other,
}
}
}

fn rounds_per_year<T: Config>() -> u32 {
BLOCKS_PER_YEAR / T::BlocksPerRound::get()
}

/// Converts annual inflation schedule to round issuance settings
pub fn per_round<T: Config>(
schedule: InflationSchedule<Perbill>,
) -> InflationSchedule<BalanceOf<T>> {
let rounds_per_year = rounds_per_year::<T>();
let total_issuance = T::Currency::total_issuance();
let ideal_annual_issuance = schedule.ideal * total_issuance;
let max_annual_issuance = schedule.max * total_issuance;
let min_annual_issuance = schedule.min * total_issuance;
let (min, ideal, max): (BalanceOf<T>, BalanceOf<T>, BalanceOf<T>) = (
min_annual_issuance / rounds_per_year.into(),
ideal_annual_issuance / rounds_per_year.into(),
max_annual_issuance / rounds_per_year.into(),
);
InflationSchedule { min, ideal, max }
}

#[cfg(test)]
mod tests {
use super::*;
fn mock_periodic_issuance(
// Annual inflation schedule
schedule: InflationSchedule<Perbill>,
// Total circulating before minting
pre_issuance_circulating: u128,
// Total number of periods
periods: u128,
) -> InflationSchedule<u128> {
let ideal_issuance = schedule.ideal * pre_issuance_circulating;
let max_issuance = schedule.max * pre_issuance_circulating;
let min_issuance = schedule.min * pre_issuance_circulating;
let (min, ideal, max): (u128, u128, u128) = (
min_issuance / periods,
ideal_issuance / periods,
max_issuance / periods,
);
InflationSchedule { min, ideal, max }
}
#[test]
fn simple_issuance_conversion() {
// 5% inflation for 10_000_0000 = 500,000 minted over the year
// let's assume there are 10 periods in a year
// => mint 500_000 over 10 periods => 50_000 minted per period
let expected_round_schedule: InflationSchedule<u128> = InflationSchedule {
min: 50_000,
ideal: 50_000,
max: 50_000,
};
let schedule = InflationSchedule {
min: Perbill::from_percent(5),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
};
assert_eq!(
expected_round_schedule,
mock_periodic_issuance(schedule, 10_000_000, 10)
);
}
#[test]
fn range_issuance_conversion() {
// 3-5% inflation for 10_000_0000 = 300_000-500,000 minted over the year
// let's assume there are 10 periods in a year
// => mint 300_000-500_000 over 10 periods => 30_000-50_000 minted per period
let expected_round_schedule: InflationSchedule<u128> = InflationSchedule {
min: 30_000,
ideal: 40_000,
max: 50_000,
};
let schedule = InflationSchedule {
min: Perbill::from_percent(3),
ideal: Perbill::from_percent(4),
max: Perbill::from_percent(5),
};
assert_eq!(
expected_round_schedule,
mock_periodic_issuance(schedule, 10_000_000, 10)
);
}
#[test]
fn current_parameterization() {
let expected_round_schedule: InflationSchedule<u128> = InflationSchedule {
min: 1,
ideal: 1,
max: 2,
};
let schedule = InflationSchedule {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(6),
};
assert_eq!(
expected_round_schedule,
mock_periodic_issuance(schedule, 10_000_000, 262980)
);
}
// this shows us that we need to increase the `BlocksPerRound` in order for the `stake` pallet to work
// what is the minimum `BlocksPerRound`?
#[test]
fn proposed_parameterization() {
// 4-6% annual inflation
// 10_000_000 total circulating pre issuance
// RoundsPerYear = BLOCKS_PER_YEAR / BLOCKS_PER_ROUND = (31557600 / 6) / X = 10000
// solve for X = 525.96 ~= 526 BLOCKS_PER_ROUND => ROUND is 52.596 hours = 2.17 days
// 400_000-600_000 minted
let expected_round_schedule: InflationSchedule<u128> = InflationSchedule {
min: 40,
ideal: 50,
max: 60,
};
let schedule = InflationSchedule {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(6),
};
assert_eq!(
expected_round_schedule,
mock_periodic_issuance(schedule, 10_000_000, 10000)
);
}
}
Loading