Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Initial abstraction
  • Loading branch information
gavofyork committed Mar 11, 2021
commit d461b25dfb09263bd8f0e8e929e994081f962a3c
47 changes: 36 additions & 11 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,36 @@ impl<T: Config> SessionInterface<<T as frame_system::Config>::AccountId> for T w
}
}

pub trait EraPayout<Balance> {
fn era_payout(
total_staked: Balance,
total_issuance: Balance,
era_duration_millis: u64,
) -> (Balance, Balance);
}

pub struct ConvertCurve<T>(sp_std::marker::PhantomData<T>);
Copy link
Member

Choose a reason for hiding this comment

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

probably could use a description

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>;
Expand Down Expand Up @@ -836,9 +866,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>;
Expand Down Expand Up @@ -2831,15 +2861,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));

Expand Down
21 changes: 9 additions & 12 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,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;
Expand Down Expand Up @@ -669,25 +669,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.
Expand Down