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

Commit

Permalink
remove initial era with no exposure, and generate it in test instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Jun 14, 2021
1 parent c666a25 commit fe0a216
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
10 changes: 1 addition & 9 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2529,15 +2529,7 @@ impl<T: Config> Pallet<T> {
exposures.len(),
Self::minimum_validator_count(),
),
None => {
// The initial era is allowed to have no exposures.
// In this case the SessionManager is expected to choose a sensible validator
// set.
// TODO: this should be simplified #8911
CurrentEra::<T>::put(0);
ErasStartSessionIndex::<T>::insert(&0, &start_session_index);
},
_ => ()
_ => (),
}

Self::deposit_event(Event::StakingElectionFailed);
Expand Down
37 changes: 36 additions & 1 deletion frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,34 @@ impl Config for Test {
type NextNewSession = Session;
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type ElectionProvider = onchain::OnChainSequentialPhragmen<Self>;
type GenesisElectionProvider = Self::ElectionProvider;
type GenesisElectionProvider = GenesisElectionProviderMock;
type WeightInfo = ();
}

thread_local! {
pub static GENESIS_ERA: RefCell<Option<Supports<AccountId>>> = RefCell::new(None);
}

pub struct GenesisElectionProviderMock;

impl ElectionProvider<AccountId, BlockNumber> for GenesisElectionProviderMock {
type Error = <
<Test as Config>::ElectionProvider as ElectionProvider<AccountId, BlockNumber>
>::Error;

type DataProvider = <
<Test as Config>::ElectionProvider as ElectionProvider<AccountId, BlockNumber>
>::DataProvider;

fn elect() -> Result<(Supports<AccountId>, Weight), Self::Error> {
if let Some(genesis_era) = GENESIS_ERA.with(|genesis_era| genesis_era.borrow().clone()) {
Ok((genesis_era, 0))
} else {
<Test as Config>::ElectionProvider::elect()
}
}
}

impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Test
where
Call: From<LocalCall>,
Expand Down Expand Up @@ -349,6 +373,9 @@ impl ExtBuilder {
PERIOD.with(|v| *v.borrow_mut() = length);
self
}
/// with stakers: some validators, nominators and idle staker are generated.
/// without stakers: the first era will elect the validator who have configured keys, without
/// any exposure.
pub fn has_stakers(mut self, has: bool) -> Self {
self.has_stakers = has;
self
Expand Down Expand Up @@ -427,6 +454,14 @@ impl ExtBuilder {
// nominator
(101, 100, balance_factor * 500, StakerStatus::<AccountId>::Nominator(nominated))
];
} else {
GENESIS_ERA.with(|genesis_era| {
*genesis_era.borrow_mut() = Some(
validators.iter()
.map(|x| (*x, Default::default()))
.collect()
);
});
}
let _ = staking::GenesisConfig::<Test>{
stakers: stakers,
Expand Down

0 comments on commit fe0a216

Please sign in to comment.