Refactor out MaxPossibleReward, fix staking arithmetic (#4041)

* Refactor out MaxPossibleReward, fix staking arithmetic

* Fix rounding error in test
This commit is contained in:
Gavin Wood
2019-11-07 12:34:13 +01:00
committed by GitHub
parent dcee91412c
commit bb6b3db57e
6 changed files with 52 additions and 44 deletions
+29 -20
View File
@@ -32,13 +32,17 @@ pub fn compute_total_payout<N>(
npos_token_staked: N,
total_tokens: N,
era_duration: u64
) -> N where N: SimpleArithmetic + Clone
{
) -> (N, N) where N: SimpleArithmetic + Clone {
// Milliseconds per year for the Julian year (365.25 days).
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR)
* yearly_inflation.calculate_for_fraction_times_denominator(npos_token_staked, total_tokens)
let portion = Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR);
let payout = portion * yearly_inflation.calculate_for_fraction_times_denominator(
npos_token_staked,
total_tokens.clone(),
);
let maximum = portion * (yearly_inflation.maximum * total_tokens);
(payout, maximum)
}
#[cfg(test)]
@@ -59,26 +63,31 @@ mod test {
#[test]
fn npos_curve_is_sensible() {
const YEAR: u64 = 365 * 24 * 60 * 60 * 1000;
// check maximum inflation.
// not 10_000 due to rounding error.
assert_eq!(super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).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), 2_498);
assert_eq!(super::compute_total_payout(&I_NPOS, 5_000, 100_000u64, YEAR), 3_248);
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, YEAR), 6_246);
assert_eq!(super::compute_total_payout(&I_NPOS, 40_000, 100_000u64, YEAR), 8_494);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, YEAR), 9_993);
assert_eq!(super::compute_total_payout(&I_NPOS, 60_000, 100_000u64, YEAR), 4_379);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, YEAR), 2_733);
assert_eq!(super::compute_total_payout(&I_NPOS, 95_000, 100_000u64, YEAR), 2_513);
assert_eq!(super::compute_total_payout(&I_NPOS, 100_000, 100_000u64, YEAR), 2_505);
assert_eq!(super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).0, 2_498);
assert_eq!(super::compute_total_payout(&I_NPOS, 5_000, 100_000u64, YEAR).0, 3_248);
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, YEAR).0, 6_246);
assert_eq!(super::compute_total_payout(&I_NPOS, 40_000, 100_000u64, YEAR).0, 8_494);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, YEAR).0, 9_993);
assert_eq!(super::compute_total_payout(&I_NPOS, 60_000, 100_000u64, YEAR).0, 4_379);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, YEAR).0, 2_733);
assert_eq!(super::compute_total_payout(&I_NPOS, 95_000, 100_000u64, YEAR).0, 2_513);
assert_eq!(super::compute_total_payout(&I_NPOS, 100_000, 100_000u64, YEAR).0, 2_505);
const DAY: u64 = 24 * 60 * 60 * 1000;
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, DAY), 17);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, DAY), 27);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, DAY), 7);
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, DAY).0, 17);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, DAY).0, 27);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, DAY).0, 7);
const SIX_HOURS: u64 = 6 * 60 * 60 * 1000;
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, SIX_HOURS), 4);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, SIX_HOURS), 7);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, SIX_HOURS), 2);
assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, SIX_HOURS).0, 4);
assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, SIX_HOURS).0, 7);
assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, SIX_HOURS).0, 2);
const HOUR: u64 = 60 * 60 * 1000;
assert_eq!(
@@ -87,7 +96,7 @@ mod test {
2_500_000_000_000_000_000_000_000_000u128,
5_000_000_000_000_000_000_000_000_000u128,
HOUR
),
).0,
57_038_500_000_000_000_000_000
);
}
+6 -12
View File
@@ -524,10 +524,6 @@ pub trait Trait: system::Trait {
/// The NPoS reward curve to use.
type RewardCurve: Get<&'static PiecewiseLinear<'static>>;
/// The maximum possible reward (in proportion of total issued tokens) that can be paid in one
/// reward cycle.
type MaxPossibleReward: Get<Perbill>;
}
/// Mode of era-forcing.
@@ -1203,7 +1199,7 @@ impl<T: Trait> Module<T> {
let validator_len: BalanceOf<T> = (validators.len() as u32).into();
let total_rewarded_stake = Self::slot_stake() * validator_len;
let total_payout = inflation::compute_total_payout(
let (total_payout, max_payout) = inflation::compute_total_payout(
&T::RewardCurve::get(),
total_rewarded_stake.clone(),
T::Currency::total_issuance(),
@@ -1220,16 +1216,14 @@ impl<T: Trait> Module<T> {
}
}
let total_reward = total_imbalance.peek();
// assert!(total_reward <= total_payout)
// assert!(total_imbalance.peek() == total_payout)
let total_payout = total_imbalance.peek();
let max_reward = T::MaxPossibleReward::get() * T::Currency::total_issuance();
let rest_reward = max_reward.saturating_sub(total_reward);
Self::deposit_event(RawEvent::Reward(total_reward, rest_reward));
let rest = max_payout.saturating_sub(total_payout);
Self::deposit_event(RawEvent::Reward(total_payout, rest));
T::Reward::on_unbalanced(total_imbalance);
T::RewardRemainder::on_unbalanced(T::Currency::issue(rest_reward));
T::RewardRemainder::on_unbalanced(T::Currency::issue(rest));
}
// Increment current era.
+2 -6
View File
@@ -192,7 +192,6 @@ parameter_types! {
pub const SessionsPerEra: SessionIndex = 3;
pub const BondingDuration: EraIndex = 3;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &I_NPOS;
pub const MaxReward: Perbill = Perbill::from_percent(10);
}
impl Trait for Test {
type Currency = balances::Module<Self>;
@@ -206,7 +205,6 @@ impl Trait for Test {
type BondingDuration = BondingDuration;
type SessionInterface = Self;
type RewardCurve = RewardCurve;
type MaxPossibleReward = MaxReward;
}
pub struct ExtBuilder {
@@ -434,14 +432,12 @@ pub fn start_era(era_index: EraIndex) {
}
pub fn current_total_payout_for_duration(duration: u64) -> u64 {
let res = inflation::compute_total_payout(
inflation::compute_total_payout(
<Test as Trait>::RewardCurve::get(),
<Module<Test>>::slot_stake() * 2,
Balances::total_issuance(),
duration,
);
res
).0
}
pub fn reward_all_elected() {