mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 05:51:02 +00:00
Pallet session new API (#4609)
* Initial work * Fix most things * fix test * fix old comment * migration * fix * remove useless stuff * fix * less spaghetti implementation * fix initial session * fix
This commit is contained in:
@@ -265,7 +265,7 @@ use frame_support::{
|
||||
WithdrawReasons, OnUnbalanced, Imbalance, Get, Time
|
||||
}
|
||||
};
|
||||
use pallet_session::{historical::OnSessionEnding, SelectInitialValidators};
|
||||
use pallet_session::historical::SessionManager;
|
||||
use sp_runtime::{
|
||||
Perbill,
|
||||
RuntimeDebug,
|
||||
@@ -575,8 +575,7 @@ impl<T: Trait> SessionInterface<<T as frame_system::Trait>::AccountId> for T whe
|
||||
FullIdentificationOf = ExposureOf<T>,
|
||||
>,
|
||||
T::SessionHandler: pallet_session::SessionHandler<<T as frame_system::Trait>::AccountId>,
|
||||
T::OnSessionEnding: pallet_session::OnSessionEnding<<T as frame_system::Trait>::AccountId>,
|
||||
T::SelectInitialValidators: pallet_session::SelectInitialValidators<<T as frame_system::Trait>::AccountId>,
|
||||
T::SessionManager: pallet_session::SessionManager<<T as frame_system::Trait>::AccountId>,
|
||||
T::ValidatorIdOf: Convert<<T as frame_system::Trait>::AccountId, Option<<T as frame_system::Trait>::AccountId>>
|
||||
{
|
||||
fn disable_validator(validator: &<T as frame_system::Trait>::AccountId) -> Result<bool, ()> {
|
||||
@@ -1346,11 +1345,8 @@ impl<T: Trait> Module<T> {
|
||||
imbalance
|
||||
}
|
||||
|
||||
/// Session has just ended. Provide the validator set for the next session if it's an era-end, along
|
||||
/// with the exposure of the prior validator set.
|
||||
fn new_session(session_index: SessionIndex)
|
||||
-> Option<(Vec<T::AccountId>, Vec<(T::AccountId, Exposure<T::AccountId, BalanceOf<T>>)>)>
|
||||
{
|
||||
/// Session has just ended. Provide the validator set for the next session if it's an era-end.
|
||||
fn new_session(session_index: SessionIndex) -> Option<Vec<T::AccountId>> {
|
||||
let era_length = session_index.checked_sub(Self::current_era_start_session_index()).unwrap_or(0);
|
||||
match ForceEra::get() {
|
||||
Forcing::ForceNew => ForceEra::kill(),
|
||||
@@ -1358,12 +1354,17 @@ impl<T: Trait> Module<T> {
|
||||
Forcing::NotForcing if era_length >= T::SessionsPerEra::get() => (),
|
||||
_ => return None,
|
||||
}
|
||||
let validators = T::SessionInterface::validators();
|
||||
let prior = validators.into_iter()
|
||||
.map(|v| { let e = Self::stakers(&v); (v, e) })
|
||||
.collect();
|
||||
|
||||
Self::new_era(session_index).map(move |new| (new, prior))
|
||||
Self::new_era(session_index)
|
||||
}
|
||||
|
||||
/// Initialise the first session (and consequently the first era)
|
||||
fn initial_session() -> Option<Vec<T::AccountId>> {
|
||||
// note: `CurrentEraStart` is set in `on_finalize` of the first block because now is not
|
||||
// available yet.
|
||||
CurrentEraStartSessionIndex::put(0);
|
||||
BondedEras::mutate(|bonded| bonded.push((0, 0)));
|
||||
Self::select_validators().1
|
||||
}
|
||||
|
||||
/// The era has changed - enact new staking set.
|
||||
@@ -1646,19 +1647,30 @@ impl<T: Trait> Module<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> pallet_session::OnSessionEnding<T::AccountId> for Module<T> {
|
||||
fn on_session_ending(_ending: SessionIndex, start_session: SessionIndex) -> Option<Vec<T::AccountId>> {
|
||||
impl<T: Trait> pallet_session::SessionManager<T::AccountId> for Module<T> {
|
||||
fn new_session(new_index: SessionIndex) -> Option<Vec<T::AccountId>> {
|
||||
Self::ensure_storage_upgraded();
|
||||
Self::new_session(start_session - 1).map(|(new, _old)| new)
|
||||
if new_index == 0 {
|
||||
return Self::initial_session();
|
||||
}
|
||||
Self::new_session(new_index - 1)
|
||||
}
|
||||
fn end_session(_end_index: SessionIndex) {}
|
||||
}
|
||||
|
||||
impl<T: Trait> OnSessionEnding<T::AccountId, Exposure<T::AccountId, BalanceOf<T>>> for Module<T> {
|
||||
fn on_session_ending(_ending: SessionIndex, start_session: SessionIndex)
|
||||
-> Option<(Vec<T::AccountId>, Vec<(T::AccountId, Exposure<T::AccountId, BalanceOf<T>>)>)>
|
||||
impl<T: Trait> SessionManager<T::AccountId, Exposure<T::AccountId, BalanceOf<T>>> for Module<T> {
|
||||
fn new_session(new_index: SessionIndex)
|
||||
-> Option<Vec<(T::AccountId, Exposure<T::AccountId, BalanceOf<T>>)>>
|
||||
{
|
||||
Self::ensure_storage_upgraded();
|
||||
Self::new_session(start_session - 1)
|
||||
<Self as pallet_session::SessionManager<_>>::new_session(new_index).map(|validators| {
|
||||
validators.into_iter().map(|v| {
|
||||
let exposure = <Stakers<T>>::get(&v);
|
||||
(v, exposure)
|
||||
}).collect()
|
||||
})
|
||||
}
|
||||
fn end_session(end_index: SessionIndex) {
|
||||
<Self as pallet_session::SessionManager<_>>::end_session(end_index)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1707,12 +1719,6 @@ impl<T: Trait> Convert<T::AccountId, Option<Exposure<T::AccountId, BalanceOf<T>>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> SelectInitialValidators<T::AccountId> for Module<T> {
|
||||
fn select_initial_validators() -> Option<Vec<T::AccountId>> {
|
||||
<Module<T>>::select_validators().1
|
||||
}
|
||||
}
|
||||
|
||||
/// This is intended to be used with `FilterHistoricalOffences`.
|
||||
impl <T: Trait> OnOffenceHandler<T::AccountId, pallet_session::historical::IdentificationTuple<T>> for Module<T> where
|
||||
T: pallet_session::Trait<ValidatorId = <T as frame_system::Trait>::AccountId>,
|
||||
@@ -1721,8 +1727,7 @@ impl <T: Trait> OnOffenceHandler<T::AccountId, pallet_session::historical::Ident
|
||||
FullIdentificationOf = ExposureOf<T>,
|
||||
>,
|
||||
T::SessionHandler: pallet_session::SessionHandler<<T as frame_system::Trait>::AccountId>,
|
||||
T::OnSessionEnding: pallet_session::OnSessionEnding<<T as frame_system::Trait>::AccountId>,
|
||||
T::SelectInitialValidators: pallet_session::SelectInitialValidators<<T as frame_system::Trait>::AccountId>,
|
||||
T::SessionManager: pallet_session::SessionManager<<T as frame_system::Trait>::AccountId>,
|
||||
T::ValidatorIdOf: Convert<<T as frame_system::Trait>::AccountId, Option<<T as frame_system::Trait>::AccountId>>
|
||||
{
|
||||
fn on_offence(
|
||||
|
||||
@@ -162,14 +162,13 @@ parameter_types! {
|
||||
pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(25);
|
||||
}
|
||||
impl pallet_session::Trait for Test {
|
||||
type OnSessionEnding = pallet_session::historical::NoteHistoricalRoot<Test, Staking>;
|
||||
type SessionManager = pallet_session::historical::NoteHistoricalRoot<Test, Staking>;
|
||||
type Keys = UintAuthorityId;
|
||||
type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;
|
||||
type SessionHandler = TestSessionHandler;
|
||||
type Event = ();
|
||||
type ValidatorId = AccountId;
|
||||
type ValidatorIdOf = crate::StashOf<Test>;
|
||||
type SelectInitialValidators = Staking;
|
||||
type DisabledValidatorsThreshold = DisabledValidatorsThreshold;
|
||||
}
|
||||
|
||||
|
||||
@@ -365,7 +365,7 @@ fn less_than_needed_candidates_works() {
|
||||
#[test]
|
||||
fn no_candidate_emergency_condition() {
|
||||
ExtBuilder::default()
|
||||
.minimum_validator_count(10)
|
||||
.minimum_validator_count(1)
|
||||
.validator_count(15)
|
||||
.num_validators(4)
|
||||
.validator_pool(true)
|
||||
@@ -374,21 +374,21 @@ fn no_candidate_emergency_condition() {
|
||||
.execute_with(|| {
|
||||
// initial validators
|
||||
assert_eq_uvec!(validator_controllers(), vec![10, 20, 30, 40]);
|
||||
let prefs = ValidatorPrefs { commission: Perbill::one() };
|
||||
<Staking as crate::Store>::Validators::insert(11, prefs.clone());
|
||||
|
||||
// set the minimum validator count.
|
||||
<Staking as crate::Store>::MinimumValidatorCount::put(10);
|
||||
<Staking as crate::Store>::ValidatorCount::put(15);
|
||||
assert_eq!(Staking::validator_count(), 15);
|
||||
|
||||
let _ = Staking::chill(Origin::signed(10));
|
||||
|
||||
// trigger era
|
||||
System::set_block_number(1);
|
||||
Session::on_initialize(System::block_number());
|
||||
start_era(1);
|
||||
|
||||
// Previous ones are elected. chill is invalidates. TODO: #2494
|
||||
assert_eq_uvec!(validator_controllers(), vec![10, 20, 30, 40]);
|
||||
assert_eq!(Staking::current_elected().len(), 0);
|
||||
// Though the validator preferences has been removed.
|
||||
assert!(Staking::validators(11) != prefs);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user