Skip to content

Commit

Permalink
Kusama: Make the current inflation formula adjustable. (#364)
Browse files Browse the repository at this point in the history
This PR does not alter the inflation of Kusama, but instead make the
current parameters of the system fully configurable.

## Parameters

The parameters are as follows: 

```rust
/// Minimum inflation rate used to calculate era payouts.
pub static MinInflation: Perquintill = Perquintill::from_rational(25u64, 1000);

/// Maximum inflation rate used to calculate era payouts.
pub static MaxInflation: Perquintill = Perquintill::from_percent(10);

/// Ideal stake ratio used to calculate era payouts.
pub static IdealStake: Perquintill = Perquintill::from_percent(75);

/// Falloff used to calculate era payouts.
pub static Falloff: Perquintill = Perquintill::from_percent(5);

/// Whether to use auction slots or not in the calculation of era payouts, then we subtract
/// `num_auctioned_slots.min(60) / 200` from `ideal_stake`.
///
/// That is, we assume up to 60 parachains that are leased can reduce the ideal stake by a
/// maximum of 30%.
///
/// With the move to agile-coretime, this parameter does not make much sense and should
/// generally be set to false.
pub static UseAuctionSlots: bool = true;
```

All of the above are exactly the current values in Kusama, and leave
everything unchanged, leading to roughly `1kKSM` minted for validators,
and around `90KSM` for treasury.

All of the above can be changed via the `Root` track only. 

Given that it is hard to come to consensus, I highly advise this PR (and
a similar PR to Polkadot) to NOT alter any parameter and leave that to
the token holders.

Just to provide one example, if we set `UseAuctionSlots = false` and
leave everything else un-changed, the amount minted per era would be:

* `807KSM` for staking
* `238KSM` for treasury

> This, in my opinion, is the most non-controversial change as the
incorporation of auctions in the inflation rate is already meaningless
with agile-coretime.

## Consideration for UIs 

This PR is shipped with a new runtime API that is only added to the
Kusama runtime: `Inflation_experimental_inflation_info`. I hope this API
is used by the UIs to show the inflation parameters of the network,
rather than re-creating the Rust logic in the front-end.

---------

Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Co-authored-by: Oliver Tale-Yazdi <[email protected]>
Co-authored-by: s0me0ne-unkn0wn <[email protected]>
Co-authored-by: clangenb <[email protected]>
Co-authored-by: Dónal Murray <[email protected]>
Co-authored-by: Gonçalo Pestana <[email protected]>
Co-authored-by: joe petrowski <[email protected]>
Co-authored-by: Dónal Murray <[email protected]>
Co-authored-by: fellowship-merge-bot[bot] <151052383+fellowship-merge-bot[bot]@users.noreply.github.com>
  • Loading branch information
9 people authored Aug 20, 2024
1 parent f42acab commit 4fd2981
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add the Polkadot Coretime Chain runtime ([polkadot-fellows/runtimes#410](https://github.com/polkadot-fellows/runtimes/pull/410))
- Kusama: Add a "Spokesperson" proxy type only allowed to send remarks ([polkadot-fellows/runtimes#430](https://github.com/polkadot-fellows/runtimes/pull/430))
- Add the Polkadot and Kusama Coretime Chain specs ([polkadot-fellows/runtimes#432](https://github.com/polkadot-fellows/runtimes/pull/432))
- Kusama: Make the current inflation formula adjustable ([polkadot-fellows/runtimes#364](https://github.com/polkadot-fellows/runtimes/pull/364))
- Port Agile Coretime migration from polkadot-sdk in order to fix leases with gaps handling([polkadot-fellows/runtimes#426](https://github.com/polkadot-fellows/runtimes/pull/426))


#### From [#322](https://github.com/polkadot-fellows/runtimes/pull/322):

- Add `claim_assets` extrinsic to `pallet-xcm` ([SDK v1.9 #3403](https://github.com/paritytech/polkadot-sdk/pull/3403)).
Expand Down
38 changes: 36 additions & 2 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pallet-nomination-pools-benchmarking = { version = "34.0.0", default-features =
pallet-nomination-pools-runtime-api = { version = "31.0.0", default-features = false }
pallet-offences = { version = "35.0.0", default-features = false }
pallet-offences-benchmarking = { version = "36.0.0", default-features = false }
pallet-parameters = { version = "0.7.0", default-features = false }
pallet-preimage = { version = "36.0.0", default-features = false }
pallet-proxy = { version = "36.0.0", default-features = false }
pallet-ranked-collective = { version = "36.0.0", default-features = false }
Expand Down Expand Up @@ -243,6 +244,8 @@ subxt = { version = "0.35.0", default-features = false }
tracing-subscriber = { version = "0.3.18" }
zombienet-sdk = { version = "0.2.4" }
tuplex = { version = "0.1.0", default-features = false }
relay-common = { path = "relay/common", default-features = false }
ss58-registry = { version = "1.47.0" }

[workspace]
resolver = "2"
Expand Down Expand Up @@ -275,6 +278,7 @@ members = [
"integration-tests/emulated/tests/people/people-kusama",
"integration-tests/emulated/tests/people/people-polkadot",
"integration-tests/zombienet",
"relay/common",
"relay/kusama",
"relay/kusama/constants",
"relay/polkadot",
Expand Down
30 changes: 30 additions & 0 deletions relay/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
authors.workspace = true
description = "Shared utilities between relay runtimes"
edition.workspace = true
license.workspace = true
name = "relay-common"
repository.workspace = true
version.workspace = true

[dependencies]
codec = { features = ["derive", "max-encoded-len"], workspace = true }
scale-info = { features = ["derive"], workspace = true }

sp-api ={ workspace = true }
sp-runtime = { workspace = true }
polkadot-primitives = { workspace = true }
pallet-staking-reward-fn ={ workspace = true }


[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",

"sp-api/std",
"sp-runtime/std",
"polkadot-primitives/std",
"pallet-staking-reward-fn/std",
]
115 changes: 115 additions & 0 deletions relay/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! Shared code between the Kusama nd Polkadot RC Runtimes.
#![cfg_attr(not(feature = "std"), no_std)]

use polkadot_primitives::Balance;
use sp_runtime::{Perquintill, Saturating};

/// Extra runtime APIs for kusama runtime.
pub mod apis {
/// Information about the current inflation rate of the system.
///
/// Both fields should be treated as best-effort, given that the inflation rate might not be
/// fully predict-able.
#[derive(scale_info::TypeInfo, codec::Encode, codec::Decode)]
pub struct InflationInfo {
/// The rate of inflation estimated per annum.
pub inflation: sp_runtime::Perquintill,
/// Next amount that we anticipate to mint.
///
/// First item is the amount that goes to stakers, second is the leftover that is usually
/// forwarded to the treasury.
pub next_mint: (polkadot_primitives::Balance, polkadot_primitives::Balance),
}

sp_api::decl_runtime_apis! {
pub trait Inflation {
/// Return the current estimates of the inflation amount.
///
/// This is marked as experimental in light of RFC#89. Nonetheless, its usage is highly
/// recommended over trying to read-storage, or re-create the onchain logic.
fn experimental_inflation_prediction_info() -> InflationInfo;
}
}
}

// ---- TODO: Below is copy pasted from sdk, remove once we pull the version containing
// https://github.com/paritytech/polkadot-sdk/pull/4938

#[derive(Debug, Clone)]
/// Parameters passed into [`relay_era_payout`] function.
pub struct EraPayoutParams {
/// Total staked amount.
pub total_staked: Balance,
/// Total stakable amount.
///
/// Usually, this is equal to the total issuance, except if a large part of the issuance is
/// locked in another sub-system.
pub total_stakable: Balance,
/// Ideal stake ratio, which is reduced by `legacy_auction_proportion` if not `None`.
pub ideal_stake: Perquintill,
/// Maximum inflation rate.
pub max_annual_inflation: Perquintill,
/// Minimum inflation rate.
pub min_annual_inflation: Perquintill,
/// Falloff used to calculate era payouts.
pub falloff: Perquintill,
/// Fraction of the era period used to calculate era payouts.
pub period_fraction: Perquintill,
/// Legacy auction proportion, which, if not `None`, is subtracted from `ideal_stake`.
pub legacy_auction_proportion: Option<Perquintill>,
}

/// A specialized function to compute the inflation of the staking system, tailored for Polkadot
/// Relay Chains, such as Polkadot, Kusama, and Westend.
pub fn relay_era_payout(params: EraPayoutParams) -> (Balance, Balance) {
let EraPayoutParams {
total_staked,
total_stakable,
ideal_stake,
max_annual_inflation,
min_annual_inflation,
falloff,
period_fraction,
legacy_auction_proportion,
} = params;

let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);

let ideal_stake = ideal_stake.saturating_sub(legacy_auction_proportion.unwrap_or_default());

let stake = Perquintill::from_rational(total_staked, total_stakable);
let adjustment = pallet_staking_reward_fn::compute_inflation(stake, ideal_stake, falloff);
let staking_inflation =
min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);

let max_payout = period_fraction * max_annual_inflation * total_stakable;
let staking_payout = (period_fraction * staking_inflation) * total_stakable;
let rest = max_payout.saturating_sub(staking_payout);

let other_issuance = total_stakable.saturating_sub(total_staked);
if total_staked > other_issuance {
let _cap_rest = Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
// We don't do anything with this, but if we wanted to, we could introduce a cap on the
// treasury amount with: `rest = rest.min(cap_rest);`
}
(staking_payout, rest)
}

// ---- TODO: Above is copy pasted from sdk, remove once we pull the version containing
// https://github.com/paritytech/polkadot-sdk/pull/4938
7 changes: 7 additions & 0 deletions relay/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pallet-whitelist = { workspace = true }
pallet-xcm = { workspace = true }
pallet-xcm-benchmarks = { optional = true, workspace = true }
frame-election-provider-support = { workspace = true }
pallet-parameters = { workspace = true }

frame-benchmarking = { optional = true, workspace = true }
frame-try-runtime = { optional = true, workspace = true }
Expand All @@ -105,6 +106,7 @@ xcm-builder = { workspace = true }
xcm-runtime-apis = { workspace = true }

sp-debug-derive = { workspace = true }
relay-common = { workspace = true }

[dev-dependencies]
sp-keyring = { workspace = true }
Expand All @@ -113,6 +115,7 @@ separator = { workspace = true }
remote-externalities = { workspace = true }
tokio = { features = ["macros"], workspace = true }
sp-tracing = { workspace = true }
ss58-registry = { workspace = true }

[build-dependencies]
substrate-wasm-builder = { workspace = true, optional = true }
Expand Down Expand Up @@ -166,6 +169,7 @@ std = [
"pallet-offences/std",
"pallet-preimage/std",
"pallet-proxy/std",
"pallet-parameters/std",
"pallet-ranked-collective/std",
"pallet-recovery/std",
"pallet-referenda/std",
Expand All @@ -186,6 +190,7 @@ std = [
"pallet-xcm/std",
"polkadot-primitives/std",
"polkadot-runtime-common/std",
"relay-common/std",
"runtime-parachains/std",
"scale-info/std",
"serde_json/std",
Expand Down Expand Up @@ -241,6 +246,7 @@ runtime-benchmarks = [
"pallet-nomination-pools/runtime-benchmarks",
"pallet-offences-benchmarking/runtime-benchmarks",
"pallet-offences/runtime-benchmarks",
"pallet-parameters/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-ranked-collective/runtime-benchmarks",
Expand Down Expand Up @@ -314,6 +320,7 @@ try-runtime = [
"polkadot-runtime-common/try-runtime",
"runtime-parachains/try-runtime",
"sp-runtime/try-runtime",
"pallet-parameters/try-runtime",
]

# Enable metadata hash generation at compile time for the `CheckMetadataHash` extension.
Expand Down
Loading

0 comments on commit 4fd2981

Please sign in to comment.