Decouple the session validators from im-online (#7127)

* Decouple the session validators from im-online

* .

* Add SessionInterface trait in im-online

Add ValidatorId in im-online Trait

Make im-online compile

Make substrate binary compile

* Fix merging issue

* Make all compile

* Fix tests

* Avoid using frame dep in primitives via pallet-session-common

* Merge ValidatorSet into SessionInterface trait

Wrap a few too long lines

Add some docs

* Move pallet-sesion-common into pallet-session

* Move SessionInterface to sp-session and impl it in session pallet

Ref https://github.com/paritytech/substrate/pull/7127#discussion_r494892472

* Split put historical::FullValidatorIdentification trait

* Fix line width

* Fix staking mock

* Fix session doc test

* Simplify <T as ValidatorIdentification<AccountId>>::ValidatorId as ValidatorId<T>

* Nits

* Clean up.

* Make it compile by commenting out report_offence_im_online bench

* Tests

* Nits

* Move OneSessionHandler to sp-session

* Fix tests

* Add some docs

* .

* Fix typo

* Rename to ValidatorSet::session_index()

* Add some more docs

* .

* Remove extra empty line

* Fix line width check

.

* Apply suggestions from code review

* Cleaup Cargo.toml

* Aura has migrated to Pallet now

Co-authored-by: Tomasz Drwięga <tomasz@parity.io>
This commit is contained in:
Liu-Cheng Xu
2021-02-03 00:38:37 +08:00
committed by GitHub
parent 38b6182fb8
commit 9904267e23
18 changed files with 204 additions and 84 deletions
@@ -26,7 +26,7 @@ use sp_std::vec;
use frame_system::{RawOrigin, Module as System, Config as SystemConfig};
use frame_benchmarking::{benchmarks, account};
use frame_support::traits::{Currency, OnInitialize};
use frame_support::traits::{Currency, OnInitialize, ValidatorSet, ValidatorSetWithIdentification};
use sp_runtime::{Perbill, traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto}};
use sp_staking::offence::{ReportOffence, Offence, OffenceDetails};
@@ -176,6 +176,34 @@ fn make_offenders<T: Config>(num_offenders: u32, num_nominators: u32) -> Result<
Ok((id_tuples, offenders))
}
fn make_offenders_im_online<T: Config>(num_offenders: u32, num_nominators: u32) -> Result<
(Vec<pallet_im_online::IdentificationTuple<T>>, Vec<Offender<T>>),
&'static str
> {
Staking::<T>::new_session(0);
let mut offenders = vec![];
for i in 0 .. num_offenders {
let offender = create_offender::<T>(i + 1, num_nominators)?;
offenders.push(offender);
}
Staking::<T>::start_session(0);
let id_tuples = offenders.iter()
.map(|offender| <
<T as ImOnlineConfig>::ValidatorSet as ValidatorSet<T::AccountId>
>::ValidatorIdOf::convert(offender.controller.clone())
.expect("failed to get validator id from account id"))
.map(|validator_id| <
<T as ImOnlineConfig>::ValidatorSet as ValidatorSetWithIdentification<T::AccountId>
>::IdentificationOf::convert(validator_id.clone())
.map(|full_id| (validator_id, full_id))
.expect("failed to convert validator id to full identification"))
.collect::<Vec<pallet_im_online::IdentificationTuple<T>>>();
Ok((id_tuples, offenders))
}
#[cfg(test)]
fn check_events<T: Config, I: Iterator<Item = <T as SystemConfig>::Event>>(expected: I) {
let events = System::<T>::events() .into_iter()
@@ -220,7 +248,7 @@ benchmarks! {
// make sure reporters actually get rewarded
Staking::<T>::set_slash_reward_fraction(Perbill::one());
let (offenders, raw_offenders) = make_offenders::<T>(o, n)?;
let (offenders, raw_offenders) = make_offenders_im_online::<T>(o, n)?;
let keys = ImOnline::<T>::keys();
let validator_set_count = keys.len() as u32;
@@ -29,7 +29,7 @@ use sp_runtime::{
traits::IdentityLookup,
testing::{Header, UintAuthorityId},
};
use pallet_session::historical as pallet_session_historical;
type AccountId = u64;
type AccountIndex = u32;
@@ -130,6 +130,7 @@ impl pallet_session::Config for Test {
type DisabledValidatorsThreshold = ();
type WeightInfo = ();
}
pallet_staking_reward_curve::build! {
const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!(
min_inflation: 0_025_000,
@@ -175,6 +176,7 @@ impl pallet_staking::Config for Test {
impl pallet_im_online::Config for Test {
type AuthorityId = UintAuthorityId;
type Event = Event;
type ValidatorSet = Historical;
type SessionDuration = Period;
type ReportUnresponsiveness = Offences;
type UnsignedPriority = ();
@@ -214,6 +216,7 @@ frame_support::construct_runtime!(
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
ImOnline: pallet_im_online::{Module, Call, Storage, Event<T>, ValidateUnsigned, Config<T>},
Offences: pallet_offences::{Module, Call, Storage, Event},
Historical: pallet_session_historical::{Module},
}
);