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

Polkadot: Constant yearly emission #471

Merged
merged 9 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 12 additions & 4 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "GPL-3.0-only" # TODO <https://

[workspace.dependencies]
assert_matches = { version = "1.5.0" }
approx = { version = "0.5.1" }
asset-hub-kusama-emulated-chain = { path = "integration-tests/emulated/chains/parachains/assets/asset-hub-kusama" }
asset-hub-kusama-runtime = { path = "system-parachains/asset-hubs/asset-hub-kusama" }
asset-hub-polkadot-emulated-chain = { path = "integration-tests/emulated/chains/parachains/assets/asset-hub-polkadot" }
Expand Down
1 change: 1 addition & 0 deletions relay/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ sp-debug-derive = { workspace = true }
polkadot-parachain-primitives = { workspace = true }

[dev-dependencies]
approx = { workspace = true }
sp-keyring = { workspace = true }
sp-trie = { workspace = true }
separator = { workspace = true }
Expand Down
101 changes: 76 additions & 25 deletions relay/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,39 +700,31 @@ impl pallet_parameters::Config for Runtime {
type AdminOrigin = DynamicParameterOrigin;
type WeightInfo = weights::pallet_parameters::WeightInfo<Runtime>;
}

/// Defines how much should the inflation be for an era given its duration.
pub struct EraPayout;
impl pallet_staking::EraPayout<Balance> for EraPayout {
fn era_payout(
total_staked: Balance,
_total_staked: Balance,
_total_issuance: Balance,
era_duration_millis: u64,
) -> (Balance, Balance) {
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;

let params = relay_common::EraPayoutParams {
total_staked,
total_stakable: Balances::total_issuance(),
ideal_stake: dynamic_params::inflation::IdealStake::get(),
max_annual_inflation: dynamic_params::inflation::MaxInflation::get(),
min_annual_inflation: dynamic_params::inflation::MinInflation::get(),
falloff: dynamic_params::inflation::Falloff::get(),
period_fraction: Perquintill::from_rational(era_duration_millis, MILLISECONDS_PER_YEAR),
legacy_auction_proportion: if dynamic_params::inflation::UseAuctionSlots::get() {
let auctioned_slots = parachains_paras::Parachains::<Runtime>::get()
.into_iter()
// all active para-ids that do not belong to a system chain is the number of
// parachains that we should take into account for inflation.
.filter(|i| *i >= LOWEST_PUBLIC_ID)
.count() as u64;
Some(Perquintill::from_rational(auctioned_slots.min(60), 300u64))
} else {
None
},
};
const MILLISECONDS_PER_YEAR: u64 = (1000 * 3600 * 24 * 36525) / 100;
// A normal-sized era will have 1 / 356.25 here:
let relative_era_len =
FixedU128::from_rational(era_duration_millis.into(), MILLISECONDS_PER_YEAR.into());

log::debug!(target: LOG_TARGET, "params: {:?}", params);
relay_common::relay_era_payout(params)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably now remove some unused code from both relay_common and polkadot-sdk related to the old inflation formula.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are still using it in Kusama and the other relay runtimes, but yea we can remove the dynamic parameter stuff again 🙈

// TI at the time of execution of [Referendum 1139](https://polkadot.subsquare.io/referenda/1139), block hash: `0x39422610299a75ef69860417f4d0e1d94e77699f45005645ffc5e8e619950f9f`.
let fixed_total_issuance: i128 = 15_011_657_390_566_252_333;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good here. i might take the preference to just keep this a round 15x10^18, so that numbers look clean and obvious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I thought about it, but did not want to make any deliberate alterations to the proposal... It is quite fortunate that it works out to 120M by accident already i think.

let yearly_inflation_rate = FixedU128::from_rational(8, 100);
let yearly_emission = yearly_inflation_rate.saturating_mul_int(fixed_total_issuance);

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

(to_stakers.saturated_into(), to_treasury.saturated_into())
}
}

Expand Down Expand Up @@ -3377,6 +3369,7 @@ mod multiplier_tests {
dispatch::DispatchInfo,
traits::{OnFinalize, PalletInfoAccess},
};
use pallet_staking::EraPayout;
use polkadot_runtime_common::{MinimumMultiplier, TargetBlockFullness};
use separator::Separatable;
use sp_runtime::traits::Convert;
Expand Down Expand Up @@ -3408,6 +3401,64 @@ mod multiplier_tests {
})
}

use approx::assert_relative_eq;
const MILLISECONDS_PER_DAY: u64 = 24 * 60 * 60 * 1000;

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

// Values are within 0.1%
assert_relative_eq!(to_stakers as f64, (279_477 * UNITS) as f64, max_relative = 0.1);
assert_relative_eq!(to_treasury as f64, (49_320 * UNITS) as f64, max_relative = 0.1);
}

#[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_DAY,
);
assert_relative_eq!(to_stakers as f64, (279_477 * UNITS) as f64 * 2.0, max_relative = 0.1);
assert_relative_eq!(to_treasury as f64, (49_320 * UNITS) as f64 * 2.0, max_relative = 0.1);
}

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

// 8% of the fixed total issuance: 120M Dot per year
let yearly_emission = 120_093_259 * UNITS;

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

// 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
(35625 * MILLISECONDS_PER_DAY) / 10, // 10 years
);
let initial_ti: i128 = 15_011_657_390_566_252_333;
let projected_total_issuance = (to_stakers as i128 + to_treasury as i128) + initial_ti;

// In 2034, there will be about 26.7 billion DOTs in existence.
assert_eq!(projected_total_issuance, 26_725_065_621_398_235_666);
}

#[test]
fn fast_unstake_estimate() {
use pallet_fast_unstake::WeightInfo;
Expand Down
Loading