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

Add polymesh specific changes - do not merge #1

Open
wants to merge 2 commits into
base: polymesh-v6-monthly-2023-03-fmt
Choose a base branch
from
Open
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
753 changes: 396 additions & 357 deletions substrate/frame/staking/src/benchmarking.rs

Large diffs are not rendered by default.

209 changes: 192 additions & 17 deletions substrate/frame/staking/src/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub fn compute_total_payout<N>(
npos_token_staked: N,
total_tokens: N,
era_duration: u64,
max_inflated_issuance: N,
non_inflated_yearly_reward: N,
) -> (N, N)
where
N: AtLeast32BitUnsigned + Clone,
Expand All @@ -45,6 +47,18 @@ where
let payout = portion
* yearly_inflation
.calculate_for_fraction_times_denominator(npos_token_staked, total_tokens.clone());

// Polymesh change
// -----------------------------------------------------------------
if total_tokens >= max_inflated_issuance {
let fixed_payout = portion * non_inflated_yearly_reward;
if fixed_payout <= payout {
// payout is always maximum.
return (fixed_payout.clone(), fixed_payout);
}
}
// -----------------------------------------------------------------

let maximum = portion * (yearly_inflation.maximum * total_tokens);
(payout, maximum)
}
Expand All @@ -67,77 +81,207 @@ mod test {
#[test]
fn npos_curve_is_sensible() {
const YEAR: u64 = 365 * 24 * 60 * 60 * 1000;
const FIXED_YEARLY_REWARD: u64 = 1_000_000;
const MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE: u64 = 1_000_000_000;

// check maximum inflation.
// not 10_000 due to rounding error.
assert_eq!(
super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).1,
super::compute_total_payout(
&I_NPOS,
0,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.1,
9_993
);

// super::I_NPOS.calculate_for_fraction_times_denominator(25, 100)
assert_eq!(
super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
0,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
2_498
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 5_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
5_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
3_248
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
25_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
6_246
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 40_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
40_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
8_494
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
50_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
9_993
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 60_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
60_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
4_379
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
75_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
2_733
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 95_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
95_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
2_513
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 100_000, 100_000u64, YEAR).0,
super::compute_total_payout(
&I_NPOS,
100_000,
100_000u64,
YEAR,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
2_505
);

const DAY: u64 = 24 * 60 * 60 * 1000;
assert_eq!(
super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, DAY).0,
super::compute_total_payout(
&I_NPOS,
25_000,
100_000u64,
DAY,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
17
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, DAY).0,
super::compute_total_payout(
&I_NPOS,
50_000,
100_000u64,
DAY,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
27
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, DAY).0,
super::compute_total_payout(
&I_NPOS,
75_000,
100_000u64,
DAY,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
7
);

const SIX_HOURS: u64 = 6 * 60 * 60 * 1000;
assert_eq!(
super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, SIX_HOURS).0,
super::compute_total_payout(
&I_NPOS,
25_000,
100_000u64,
SIX_HOURS,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
4
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, SIX_HOURS).0,
super::compute_total_payout(
&I_NPOS,
50_000,
100_000u64,
SIX_HOURS,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
7
);
assert_eq!(
super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, SIX_HOURS).0,
super::compute_total_payout(
&I_NPOS,
75_000,
100_000u64,
SIX_HOURS,
MAX_VARIABLE_INFLATION_TOTAL_ISSUANCE,
FIXED_YEARLY_REWARD
)
.0,
2
);

Expand All @@ -147,10 +291,41 @@ mod test {
&I_NPOS,
2_500_000_000_000_000_000_000_000_000u128,
5_000_000_000_000_000_000_000_000_000u128,
HOUR
HOUR,
7_000_000_000_000_000_000_000_000_000u128,
FIXED_YEARLY_REWARD.into()
)
.0,
57_038_500_000_000_000_000_000
);

// Even though the total issuance is above `max_inflated_issuance` we still have
// inflation calculated via the curve as this is below the non_inflated_yearly_reward
assert_eq!(
super::compute_total_payout(
&I_NPOS,
1_000_000,
1_074_582_300_000_000u128,
SIX_HOURS,
1_000_000_000_000_000u128,
sp_runtime::Perbill::from_percent(5) * 1_000_000_000_000_000u128
),
(18387768858, 73551075022)
);

// Since the staking ratio is high enough, the curve calculated inflation is
// above the fixed `non_inflated_yearly_reward` hence we use the latter
// i.e. expected response is 5% of 1 billion rather than 10% of 1.5 billion.
assert_eq!(
super::compute_total_payout(
&I_NPOS,
750_000_000_000_000u128, //50% staking ratio
1_500_000_000_000_000u128,
YEAR,
1_000_000_000_000_000u128,
sp_runtime::Perbill::from_percent(5) * 1_000_000_000_000_000u128
),
(49_965_776_850_000, 49_965_776_850_000)
);
}
}
Loading