Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Lazy payouts (#4474)
Browse files Browse the repository at this point in the history
* TODOs

* Remove superfluous:

* partial implementation

* full implementation

* fix preferences

* update comments

* upgrade test WIP

* fix more tests

* fix cutoff

* fix saturation

* comment

* upgrade mock

* upgrade test

* WIP migration

* WIP migration

* remove slot stake stuff

* fix merge

* migration of ledger

* remove equalize from test

* add test

* fix

* update doc

* fix compilation

* improve test readibility

* improve doc

* fix most todo

* fix migration and test

* remove println

* WIP

* add test and spec

* weight

* update doc

* safer end_era

* fix exposure of conversion

* Revert "safer end_era"

This reverts commit 72ff737.

* fix useless put

* exposure clipped

* doc

* fix payout with clipped

* fix node runtime

* add doc

* pluggable and generalized staking module

* remove print

* update doc

* refactor

* improve documentation and implementation

* fix test

* Fix test

* fix test

* fix test

* fix remove lowest stake from exposure, not biggest.

* nomination index arguments in nominator_payout

* add test

* try to fix offence

* apply slashed and bond eras until active era

* doc

* update spec version

* add test upgrade from previous test environment

* Apply suggestions from code review

Co-Authored-By: Shawn Tabrizi <[email protected]>

* nominators upgrade has been cleaned

* dynamic history depth implementation

* make current_era - history_depth included

* Change equality check to start era to less than or equal

* Use era specific validator prefs

* Add print statement and comment about start era if <

* fix next_reward overflow

* make more check for bad era claim for zero cost

* small refactor

* code refactor + fix use of deprecated storage

* fix wasm build

* add comment

* Fix tests

* remove outdated comment

* Apply suggestions from code review

Co-Authored-By: Shawn Tabrizi <[email protected]>

* gather active era information into one storage

Co-authored-by: thiolliere <[email protected]>
Co-authored-by: Shawn Tabrizi <[email protected]>
  • Loading branch information
3 people authored Mar 3, 2020
1 parent 870540b commit 75116bd
Show file tree
Hide file tree
Showing 11 changed files with 1,701 additions and 669 deletions.
1 change: 0 additions & 1 deletion bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ pub fn testnet_genesis(
}).collect::<Vec<_>>(),
}),
pallet_staking: Some(StakingConfig {
current_era: 0,
validator_count: initial_authorities.len() as u32 * 2,
minimum_validator_count: initial_authorities.len() as u32,
stakers: initial_authorities.iter().map(|x| {
Expand Down
2 changes: 2 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ parameter_types! {
pub const BondingDuration: pallet_staking::EraIndex = 24 * 28;
pub const SlashDeferDuration: pallet_staking::EraIndex = 24 * 7; // 1/4 the bonding duration.
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const MaxNominatorRewardedPerValidator: u32 = 64;
}

impl pallet_staking::Trait for Runtime {
Expand All @@ -286,6 +287,7 @@ impl pallet_staking::Trait for Runtime {
type SlashCancelOrigin = pallet_collective::EnsureProportionAtLeast<_3, _4, AccountId, CouncilCollective>;
type SessionInterface = Self;
type RewardCurve = RewardCurve;
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
}

parameter_types! {
Expand Down
1 change: 0 additions & 1 deletion bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub fn config_endowed(
]
}),
pallet_staking: Some(StakingConfig {
current_era: 0,
stakers: vec![
(dave(), alice(), 111 * DOLLARS, pallet_staking::StakerStatus::Validator),
(eve(), bob(), 100 * DOLLARS, pallet_staking::StakerStatus::Validator),
Expand Down
2 changes: 2 additions & 0 deletions frame/im-online/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl pallet_session::SessionManager<u64> for TestSessionManager {
VALIDATORS.with(|l| l.borrow_mut().take())
}
fn end_session(_: SessionIndex) {}
fn start_session(_: SessionIndex) {}
}

impl pallet_session::historical::SessionManager<u64, u64> for TestSessionManager {
Expand All @@ -62,6 +63,7 @@ impl pallet_session::historical::SessionManager<u64, u64> for TestSessionManager
)
}
fn end_session(_: SessionIndex) {}
fn start_session(_: SessionIndex) {}
}

/// An extrinsic type used for tests.
Expand Down
4 changes: 4 additions & 0 deletions frame/session/src/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub trait SessionManager<ValidatorId, FullIdentification>: crate::SessionManager
/// If there was a validator set change, its returns the set of new validators along with their
/// full identifications.
fn new_session(new_index: SessionIndex) -> Option<Vec<(ValidatorId, FullIdentification)>>;
fn start_session(start_index: SessionIndex);
fn end_session(end_index: SessionIndex);
}

Expand Down Expand Up @@ -146,6 +147,9 @@ impl<T: Trait, I> crate::SessionManager<T::ValidatorId> for NoteHistoricalRoot<T

new_validators
}
fn start_session(start_index: SessionIndex) {
<I as SessionManager<_, _>>::start_session(start_index)
}
fn end_session(end_index: SessionIndex) {
<I as SessionManager<_, _>>::end_session(end_index)
}
Expand Down
13 changes: 11 additions & 2 deletions frame/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,15 @@ pub trait SessionManager<ValidatorId> {
/// Because the session pallet can queue validator set the ending session can be lower than the
/// last new session index.
fn end_session(end_index: SessionIndex);
/// Start the session.
///
/// The session start to be used for validation
fn start_session(start_index: SessionIndex);
}

impl<A> SessionManager<A> for () {
fn new_session(_: SessionIndex) -> Option<Vec<A>> { None }
fn start_session(_: SessionIndex) {}
fn end_session(_: SessionIndex) {}
}

Expand Down Expand Up @@ -423,6 +428,8 @@ decl_storage! {

<Validators<T>>::put(initial_validators_0);
<QueuedKeys<T>>::put(queued_keys);

T::SessionManager::start_session(0);
});
}
}
Expand Down Expand Up @@ -520,6 +527,8 @@ impl<T: Trait> Module<T> {
// Inform the session handlers that a session is going to end.
T::SessionHandler::on_before_session_ending();

T::SessionManager::end_session(session_index);

// Get queued session keys and validators.
let session_keys = <QueuedKeys<T>>::get();
let validators = session_keys.iter()
Expand All @@ -532,12 +541,12 @@ impl<T: Trait> Module<T> {
DisabledValidators::take();
}

T::SessionManager::end_session(session_index);

// Increment session index.
let session_index = session_index + 1;
CurrentIndex::put(session_index);

T::SessionManager::start_session(session_index);

// Get next validator set.
let maybe_next_validators = T::SessionManager::new_session(session_index + 1);
let (next_validators, next_identities_changed)
Expand Down
2 changes: 2 additions & 0 deletions frame/session/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl SessionHandler<u64> for TestSessionHandler {
pub struct TestSessionManager;
impl SessionManager<u64> for TestSessionManager {
fn end_session(_: SessionIndex) {}
fn start_session(_: SessionIndex) {}
fn new_session(_: SessionIndex) -> Option<Vec<u64>> {
if !TEST_SESSION_CHANGED.with(|l| *l.borrow()) {
VALIDATORS.with(|v| {
Expand All @@ -112,6 +113,7 @@ impl SessionManager<u64> for TestSessionManager {
#[cfg(feature = "historical")]
impl crate::historical::SessionManager<u64, u64> for TestSessionManager {
fn end_session(_: SessionIndex) {}
fn start_session(_: SessionIndex) {}
fn new_session(new_index: SessionIndex)
-> Option<Vec<(u64, u64)>>
{
Expand Down
Loading

0 comments on commit 75116bd

Please sign in to comment.