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

[Westend] Parameterises minimum era inflation to treasury #6165

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub mod dynamic_params {
use super::*;

/// Parameters used to calculate era payouts, see
/// [`polkadot_runtime_common::impls::EraPayoutParams`].
/// [`polkadot_runtime_common::impls::EraPayoutParams`] and [`crate::EraPayout`].
#[dynamic_pallet_params]
#[codec(index = 0)]
pub mod inflation {
Expand All @@ -281,6 +281,11 @@ pub mod dynamic_params {
/// the `legacy_auction_proportion` of 60% will be used in the calculation of era payouts.
#[codec(index = 4)]
pub static UseAuctionSlots: bool = false;

/// Minimum percentage of the total era inflation that is sent to the treasury. 15% as per
/// Polkadot ref 1139.
#[codec(index = 5)]
pub static MinTreasuryEraInflation: FixedU128 = FixedU128::from_rational(15, 100);
}
}

Expand Down Expand Up @@ -687,6 +692,8 @@ impl pallet_staking::EraPayout<Balance> for EraPayout {
_total_issuance: Balance,
era_duration_millis: u64,
) -> (Balance, Balance) {
use crate::dynamic_params::inflation::MinTreasuryEraInflation;

const MILLISECONDS_PER_YEAR: u64 = (1000 * 3600 * 24 * 36525) / 100;
// A normal-sized era will have 1 / 365.25 here:
let relative_era_len =
Expand All @@ -698,8 +705,7 @@ impl pallet_staking::EraPayout<Balance> for EraPayout {
let yearly_emission = fixed_inflation_rate.saturating_mul_int(fixed_total_issuance);

let era_emission = relative_era_len.saturating_mul_int(yearly_emission);
// 15% to treasury, as per Polkadot ref 1139.
let to_treasury = FixedU128::from_rational(15, 100).saturating_mul_int(era_emission);
let to_treasury = MinTreasuryEraInflation::get().saturating_mul_int(era_emission);
let to_stakers = era_emission.saturating_sub(to_treasury);

(to_stakers.saturated_into(), to_treasury.saturated_into())
Expand Down
158 changes: 89 additions & 69 deletions polkadot/runtime/westend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ use xcm_runtime_apis::conversions::LocationToAccountHelper;

const MILLISECONDS_PER_HOUR: u64 = 60 * 60 * 1000;

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut ext = sp_io::TestExternalities::new(Default::default());
ext.execute_with(|| System::set_block_number(1));
ext
}

#[test]
fn remove_keys_weight_is_sensible() {
use polkadot_runtime_common::crowdloan::WeightInfo;
Expand Down Expand Up @@ -318,91 +324,105 @@ fn location_conversion_works() {

#[test]
fn staking_inflation_correct_single_era() {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
MILLISECONDS_PER_HOUR,
);

assert_relative_eq!(to_stakers as f64, (4_046 * CENTS) as f64, max_relative = 0.01);
assert_relative_eq!(to_treasury as f64, (714 * CENTS) as f64, max_relative = 0.01);
// Total per hour is ~47.6 WND
assert_relative_eq!(
(to_stakers as f64 + to_treasury as f64),
(4_760 * CENTS) as f64,
max_relative = 0.001
);
new_test_ext().execute_with(|| {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
MILLISECONDS_PER_HOUR,
);

assert_relative_eq!(to_stakers as f64, (4_046 * CENTS) as f64, max_relative = 0.01);
assert_relative_eq!(to_treasury as f64, (714 * CENTS) as f64, max_relative = 0.01);
// Total per hour is ~47.6 WND
assert_relative_eq!(
(to_stakers as f64 + to_treasury as f64),
(4_760 * CENTS) as f64,
max_relative = 0.001
);
})
}

#[test]
fn staking_inflation_correct_longer_era() {
// Twice the era duration means twice the emission:
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
2 * MILLISECONDS_PER_HOUR,
);

assert_relative_eq!(to_stakers as f64, (4_046 * CENTS) as f64 * 2.0, max_relative = 0.001);
assert_relative_eq!(to_treasury as f64, (714 * CENTS) as f64 * 2.0, max_relative = 0.001);
new_test_ext().execute_with(|| {
// Twice the era duration means twice the emission:
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
2 * MILLISECONDS_PER_HOUR,
);

assert_relative_eq!(to_stakers as f64, (4_046 * CENTS) as f64 * 2.0, max_relative = 0.001);
assert_relative_eq!(to_treasury as f64, (714 * CENTS) as f64 * 2.0, max_relative = 0.001);
})
}

#[test]
fn staking_inflation_correct_whole_year() {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
(36525 * 24 * MILLISECONDS_PER_HOUR) / 100, // 1 year
);

// Our yearly emissions is about 417k WND:
let yearly_emission = 417_307 * UNITS;
assert_relative_eq!(
to_stakers as f64 + to_treasury as f64,
yearly_emission as f64,
max_relative = 0.001
);

assert_relative_eq!(to_stakers as f64, yearly_emission as f64 * 0.85, max_relative = 0.001);
assert_relative_eq!(to_treasury as f64, yearly_emission as f64 * 0.15, max_relative = 0.001);
new_test_ext().execute_with(|| {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
(36525 * 24 * MILLISECONDS_PER_HOUR) / 100, // 1 year
);

// Our yearly emissions is about 417k WND:
let yearly_emission = 417_307 * UNITS;
assert_relative_eq!(
to_stakers as f64 + to_treasury as f64,
yearly_emission as f64,
max_relative = 0.001
);

assert_relative_eq!(to_stakers as f64, yearly_emission as f64 * 0.85, max_relative = 0.001);
assert_relative_eq!(
to_treasury as f64,
yearly_emission as f64 * 0.15,
max_relative = 0.001
);
})
}

// 10 years into the future, our values do not overflow.
#[test]
fn staking_inflation_correct_not_overflow() {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
(36525 * 24 * MILLISECONDS_PER_HOUR) / 10, // 10 years
);
let initial_ti: i128 = 5_216_342_402_773_185_773;
let projected_total_issuance = (to_stakers as i128 + to_treasury as i128) + initial_ti;

// In 2034, there will be about 9.39 million WND in existence.
assert_relative_eq!(
projected_total_issuance as f64,
(9_390_000 * UNITS) as f64,
max_relative = 0.001
);
new_test_ext().execute_with(|| {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
(36525 * 24 * MILLISECONDS_PER_HOUR) / 10, // 10 years
);
let initial_ti: i128 = 5_216_342_402_773_185_773;
let projected_total_issuance = (to_stakers as i128 + to_treasury as i128) + initial_ti;

// In 2034, there will be about 9.39 million WND in existence.
assert_relative_eq!(
projected_total_issuance as f64,
(9_390_000 * UNITS) as f64,
max_relative = 0.001
);
})
}

// Print percent per year, just as convenience.
#[test]
fn staking_inflation_correct_print_percent() {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
(36525 * 24 * MILLISECONDS_PER_HOUR) / 100, // 1 year
);
let yearly_emission = to_stakers + to_treasury;
let mut ti: i128 = 5_216_342_402_773_185_773;

for y in 0..10 {
let new_ti = ti + yearly_emission as i128;
let inflation = 100.0 * (new_ti - ti) as f64 / ti as f64;
println!("Year {y} inflation: {inflation}%");
ti = new_ti;

assert!(inflation <= 8.0 && inflation > 2.0, "sanity check");
}
new_test_ext().execute_with(|| {
let (to_stakers, to_treasury) = super::EraPayout::era_payout(
123, // ignored
456, // ignored
(36525 * 24 * MILLISECONDS_PER_HOUR) / 100, // 1 year
);
let yearly_emission = to_stakers + to_treasury;
let mut ti: i128 = 5_216_342_402_773_185_773;

for y in 0..10 {
let new_ti = ti + yearly_emission as i128;
let inflation = 100.0 * (new_ti - ti) as f64 / ti as f64;
println!("Year {y} inflation: {inflation}%");
ti = new_ti;

assert!(inflation <= 8.0 && inflation > 2.0, "sanity check");
}
})
}
11 changes: 11 additions & 0 deletions prdoc/pr_6165.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: Parameterises Westend minimum era infaltion to treasury

doc:
- audience: Runtime User
description: |
Parameterises the minimum era inflation percentage that should be automatically minted into
the treasury. This parameterisation exposes the config directly to governance.

crates:
- name: westend-runtime
bump: patch
Loading