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
+22 -2
View File
@@ -31,8 +31,10 @@ use codec::{Encode, Decode};
use sp_runtime::KeyTypeId;
use sp_runtime::traits::{Convert, OpaqueKeys};
use sp_session::{MembershipProof, ValidatorCount};
use frame_support::{decl_module, decl_storage};
use frame_support::{Parameter, print};
use frame_support::{
decl_module, decl_storage, Parameter, print,
traits::{ValidatorSet, ValidatorSetWithIdentification},
};
use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX};
use sp_trie::trie_types::{TrieDBMut, TrieDB};
use super::{SessionIndex, Module as SessionModule};
@@ -102,6 +104,24 @@ impl<T: Config> Module<T> {
}
}
impl<T: Config> ValidatorSet<T::AccountId> for Module<T> {
type ValidatorId = T::ValidatorId;
type ValidatorIdOf = T::ValidatorIdOf;
fn session_index() -> sp_staking::SessionIndex {
super::Module::<T>::current_index()
}
fn validators() -> Vec<Self::ValidatorId> {
super::Module::<T>::validators()
}
}
impl<T: Config> ValidatorSetWithIdentification<T::AccountId> for Module<T> {
type Identification = T::FullIdentification;
type IdentificationOf = T::FullIdentificationOf;
}
/// Specialization of the crate-level `SessionManager` which returns the set of full identification
/// when creating a new session.
pub trait SessionManager<ValidatorId, FullIdentification>: crate::SessionManager<ValidatorId> {
+15 -35
View File
@@ -116,13 +116,14 @@ pub mod weights;
use sp_std::{prelude::*, marker::PhantomData, ops::{Sub, Rem}};
use codec::Decode;
use sp_runtime::{KeyTypeId, Perbill, RuntimeAppPublic, BoundToRuntimeAppPublic};
use sp_runtime::{KeyTypeId, Perbill, RuntimeAppPublic};
use sp_runtime::traits::{Convert, Zero, Member, OpaqueKeys, Saturating};
use sp_staking::SessionIndex;
use frame_support::{
ensure, decl_module, decl_event, decl_storage, decl_error, ConsensusEngineId, Parameter,
traits::{
Get, FindAuthor, ValidatorRegistration, EstimateNextSessionRotation, EstimateNextNewSession,
OneSessionHandler, ValidatorSet,
},
dispatch::{self, DispatchResult, DispatchError},
weights::Weight,
@@ -256,40 +257,6 @@ pub trait SessionHandler<ValidatorId> {
fn on_disabled(validator_index: usize);
}
/// A session handler for specific key type.
pub trait OneSessionHandler<ValidatorId>: BoundToRuntimeAppPublic {
/// The key type expected.
type Key: Decode + Default + RuntimeAppPublic;
fn on_genesis_session<'a, I: 'a>(validators: I)
where I: Iterator<Item=(&'a ValidatorId, Self::Key)>, ValidatorId: 'a;
/// Session set has changed; act appropriately. Note that this can be called
/// before initialization of your module.
///
/// `changed` is true when at least one of the session keys
/// or the underlying economic identities/distribution behind one the
/// session keys has changed, false otherwise.
///
/// The `validators` are the validators of the incoming session, and `queued_validators`
/// will follow.
fn on_new_session<'a, I: 'a>(
changed: bool,
validators: I,
queued_validators: I,
) where I: Iterator<Item=(&'a ValidatorId, Self::Key)>, ValidatorId: 'a;
/// A notification for end of the session.
///
/// Note it is triggered before any `SessionManager::end_session` handlers,
/// so we can still affect the validator set.
fn on_before_session_ending() {}
/// A validator got disabled. Act accordingly until a new session begins.
fn on_disabled(_validator_index: usize);
}
#[impl_trait_for_tuples::impl_for_tuples(1, 30)]
#[tuple_types_no_default_trait_bound]
impl<AId> SessionHandler<AId> for Tuple {
@@ -830,6 +797,19 @@ impl<T: Config> Module<T> {
}
}
impl<T: Config> ValidatorSet<T::AccountId> for Module<T> {
type ValidatorId = T::ValidatorId;
type ValidatorIdOf = T::ValidatorIdOf;
fn session_index() -> sp_staking::SessionIndex {
Module::<T>::current_index()
}
fn validators() -> Vec<Self::ValidatorId> {
Module::<T>::validators()
}
}
/// Wraps the author-scraping logic for consensus engines that can recover
/// the canonical index of an author. This then transforms it into the
/// registering account-ID of that session key index.
+1
View File
@@ -18,6 +18,7 @@
// Tests for the Session Pallet
use super::*;
use codec::Decode;
use frame_support::{traits::OnInitialize, assert_ok};
use sp_core::crypto::key_types::DUMMY;
use sp_runtime::testing::UintAuthorityId;