staking: Flexible generation of reward curve and associated tweaks (#8327)

* Initial abstraction

* Alter rest of APIs

* Fixes

* Some extra getters in Gilt pallet.

* Refactor Gilt to avoid u128 conversions

* Simplify and improve pow in per_things

* Add scalar division to per_things

* Renaming from_fraction -> from_float, drop _approximation

* Fixes

* Fixes

* Fixes

* Fixes

* Make stuff build

* Fixes

* Fixes

* Fixes

* Fixes

* Update .gitignore

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/gilt/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/gilt/src/mock.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Fixes

* Fixes

* Fixes

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Gavin Wood
2021-03-16 13:03:58 +01:00
committed by GitHub
parent b6c626399e
commit 363db4f086
32 changed files with 342 additions and 226 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ pub fn compute_total_payout<N>(
// Milliseconds per year for the Julian year (365.25 days).
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
let portion = Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR);
let portion = Perbill::from_rational(era_duration as u64, MILLISECONDS_PER_YEAR);
let payout = portion * yearly_inflation.calculate_for_fraction_times_denominator(
npos_token_staked,
total_tokens.clone(),
+54 -14
View File
@@ -782,6 +782,51 @@ impl<T: Config> SessionInterface<<T as frame_system::Config>::AccountId> for T w
}
}
/// Handler for determining how much of a balance should be paid out on the current era.
pub trait EraPayout<Balance> {
/// Determine the payout for this era.
///
/// Returns the amount to be paid to stakers in this era, as well as whatever else should be
/// paid out ("the rest").
fn era_payout(
total_staked: Balance,
total_issuance: Balance,
era_duration_millis: u64,
) -> (Balance, Balance);
}
impl<Balance: Default> EraPayout<Balance> for () {
fn era_payout(
_total_staked: Balance,
_total_issuance: Balance,
_era_duration_millis: u64,
) -> (Balance, Balance) {
(Default::default(), Default::default())
}
}
pub struct ConvertCurve<T>(sp_std::marker::PhantomData<T>);
impl<
Balance: AtLeast32BitUnsigned + Clone,
T: Get<&'static PiecewiseLinear<'static>>,
> EraPayout<Balance> for ConvertCurve<T> {
fn era_payout(
total_staked: Balance,
total_issuance: Balance,
era_duration_millis: u64,
) -> (Balance, Balance) {
let (validator_payout, max_payout) = inflation::compute_total_payout(
&T::get(),
total_staked,
total_issuance,
// Duration of era; more than u64::MAX is rewarded as u64::MAX.
era_duration_millis,
);
let rest = max_payout.saturating_sub(validator_payout.clone());
(validator_payout, rest)
}
}
pub trait Config: frame_system::Config + SendTransactionTypes<Call<Self>> {
/// The staking balance.
type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
@@ -838,9 +883,9 @@ pub trait Config: frame_system::Config + SendTransactionTypes<Call<Self>> {
/// Interface for interacting with a session module.
type SessionInterface: self::SessionInterface<Self::AccountId>;
/// The NPoS reward curve used to define yearly inflation.
/// The payout for validators and the system for the current era.
/// See [Era payout](./index.html#era-payout).
type RewardCurve: Get<&'static PiecewiseLinear<'static>>;
type EraPayout: EraPayout<BalanceOf<Self>>;
/// Something that can estimate the next session change, accurately or as a best effort guess.
type NextNewSession: EstimateNextNewSession<Self::BlockNumber>;
@@ -2413,7 +2458,7 @@ impl<T: Config> Module<T> {
// This is the fraction of the total reward that the validator and the
// nominators will get.
let validator_total_reward_part = Perbill::from_rational_approximation(
let validator_total_reward_part = Perbill::from_rational(
validator_reward_points,
total_reward_points,
);
@@ -2428,7 +2473,7 @@ impl<T: Config> Module<T> {
let validator_leftover_payout = validator_total_payout - validator_commission_payout;
// Now let's calculate how this is split to the validator.
let validator_exposure_part = Perbill::from_rational_approximation(
let validator_exposure_part = Perbill::from_rational(
exposure.own,
exposure.total,
);
@@ -2445,7 +2490,7 @@ impl<T: Config> Module<T> {
// Lets now calculate how this is split to the nominators.
// Reward only the clipped exposures. Note this is not necessarily sorted.
for nominator in exposure.others.iter() {
let nominator_exposure_part = Perbill::from_rational_approximation(
let nominator_exposure_part = Perbill::from_rational(
nominator.value,
exposure.total,
);
@@ -2837,15 +2882,10 @@ impl<T: Config> Module<T> {
if let Some(active_era_start) = active_era.start {
let now_as_millis_u64 = T::UnixTime::now().as_millis().saturated_into::<u64>();
let era_duration = now_as_millis_u64 - active_era_start;
let (validator_payout, max_payout) = inflation::compute_total_payout(
&T::RewardCurve::get(),
Self::eras_total_stake(&active_era.index),
T::Currency::total_issuance(),
// Duration of era; more than u64::MAX is rewarded as u64::MAX.
era_duration.saturated_into::<u64>(),
);
let rest = max_payout.saturating_sub(validator_payout);
let era_duration = (now_as_millis_u64 - active_era_start).saturated_into::<u64>();
let staked = Self::eras_total_stake(&active_era.index);
let issuance = T::Currency::total_issuance();
let (validator_payout, rest) = T::EraPayout::era_payout(staked, issuance, era_duration);
Self::deposit_event(RawEvent::EraPayout(active_era.index, validator_payout, rest));
+9 -12
View File
@@ -260,7 +260,7 @@ impl Config for Test {
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = BondingDuration;
type SessionInterface = Self;
type RewardCurve = RewardCurve;
type EraPayout = ConvertCurve<RewardCurve>;
type NextNewSession = Session;
type ElectionLookahead = ElectionLookahead;
type Call = Call;
@@ -670,25 +670,22 @@ pub(crate) fn start_active_era(era_index: EraIndex) {
}
pub(crate) fn current_total_payout_for_duration(duration: u64) -> Balance {
let reward = inflation::compute_total_payout(
<Test as Config>::RewardCurve::get(),
let (payout, _rest) = <Test as Config>::EraPayout::era_payout(
Staking::eras_total_stake(active_era()),
Balances::total_issuance(),
duration,
)
.0;
assert!(reward > 0);
reward
);
assert!(payout > 0);
payout
}
pub(crate) fn maximum_payout_for_duration(duration: u64) -> Balance {
inflation::compute_total_payout(
<Test as Config>::RewardCurve::get(),
0,
let (payout, rest) = <Test as Config>::EraPayout::era_payout(
Staking::eras_total_stake(active_era()),
Balances::total_issuance(),
duration,
)
.1
);
payout + rest
}
/// Time it takes to finish a session.
+1 -1
View File
@@ -376,7 +376,7 @@ pub fn create_assignments_for_offchain<T: Config>(
),
&'static str
> {
let ratio = OffchainAccuracy::from_rational_approximation(1, MAX_NOMINATIONS);
let ratio = OffchainAccuracy::from_rational(1, MAX_NOMINATIONS);
let assignments: Vec<Assignment<T::AccountId, OffchainAccuracy>> = <Nominators<T>>::iter()
.take(num_assignments as usize)
.map(|(n, t)| Assignment {
+8 -8
View File
@@ -209,10 +209,10 @@ fn rewards_should_work() {
individual: vec![(11, 100), (21, 50)].into_iter().collect(),
}
);
let part_for_10 = Perbill::from_rational_approximation::<u32>(1000, 1125);
let part_for_20 = Perbill::from_rational_approximation::<u32>(1000, 1375);
let part_for_100_from_10 = Perbill::from_rational_approximation::<u32>(125, 1125);
let part_for_100_from_20 = Perbill::from_rational_approximation::<u32>(375, 1375);
let part_for_10 = Perbill::from_rational::<u32>(1000, 1125);
let part_for_20 = Perbill::from_rational::<u32>(1000, 1375);
let part_for_100_from_10 = Perbill::from_rational::<u32>(125, 1125);
let part_for_100_from_20 = Perbill::from_rational::<u32>(375, 1375);
start_session(2);
start_session(3);
@@ -598,8 +598,8 @@ fn nominators_also_get_slashed_pro_rata() {
let slash_amount = slash_percent * exposed_stake;
let validator_share =
Perbill::from_rational_approximation(exposed_validator, exposed_stake) * slash_amount;
let nominator_share = Perbill::from_rational_approximation(
Perbill::from_rational(exposed_validator, exposed_stake) * slash_amount;
let nominator_share = Perbill::from_rational(
exposed_nominator,
exposed_stake,
) * slash_amount;
@@ -4270,8 +4270,8 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() {
let init_balance_10 = Balances::total_balance(&10);
let init_balance_100 = Balances::total_balance(&100);
let part_for_10 = Perbill::from_rational_approximation::<u32>(1000, 1125);
let part_for_100 = Perbill::from_rational_approximation::<u32>(125, 1125);
let part_for_10 = Perbill::from_rational::<u32>(1000, 1125);
let part_for_100 = Perbill::from_rational::<u32>(125, 1125);
// Check state
Payee::<Test>::insert(11, RewardDestination::Controller);