From 33b105b9943b77803b1afb69c309b94c17f6df36 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 17 Sep 2020 16:41:46 +0800 Subject: [PATCH 01/33] Decouple the session validators from im-online --- bin/node/runtime/src/lib.rs | 7 +++++++ frame/im-online/src/lib.rs | 15 ++++++++++++--- frame/im-online/src/mock.rs | 9 ++++++++- frame/offences/benchmarking/src/mock.rs | 7 +++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 5737fcfd2e2f1..49f4d855c13e1 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -730,9 +730,16 @@ impl frame_system::offchain::SendTransactionTypes for Runtime where type OverarchingCall = Call; } +impl pallet_im_online::ValidatorSet<::ValidatorId> for Runtime { + fn validators() -> Vec<::ValidatorId> { + Session::validators() + } +} + impl pallet_im_online::Trait for Runtime { type AuthorityId = ImOnlineId; type Event = Event; + type ValidatorSet = Self; type SessionDuration = SessionDuration; type ReportUnresponsiveness = Offences; type UnsignedPriority = ImOnlineUnsignedPriority; diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 7856ecfd5aa46..e27939b092fa8 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -238,6 +238,12 @@ impl WeightInfo for () { fn validate_unsigned_and_then_heartbeat(_k: u32, _e: u32, ) -> Weight { 1_000_000_000 } } +/// The set of validators that are considered to be online. +pub trait ValidatorSet { + /// Returns all the validators ought to be online. + fn validators() -> Vec; +} + pub trait Trait: SendTransactionTypes> + pallet_session::historical::Trait { /// The identifier type for an authority. type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + Ord; @@ -253,6 +259,9 @@ pub trait Trait: SendTransactionTypes> + pallet_session::historical:: /// there is a chance the authority will produce a block and they won't be necessary. type SessionDuration: Get; + /// A set of validators expected to be online. + type ValidatorSet: ValidatorSet<::ValidatorId>; + /// A type that gives us the ability to submit unresponsiveness offence reports. type ReportUnresponsiveness: ReportOffence< @@ -427,7 +436,7 @@ impl Module { /// authored at least one block, during the current session. Otherwise /// `false`. pub fn is_online(authority_index: AuthIndex) -> bool { - let current_validators = >::validators(); + let current_validators = T::ValidatorSet::validators(); if authority_index >= current_validators.len() as u32 { return false; @@ -475,7 +484,7 @@ impl Module { } let session_index = >::current_index(); - let validators_len = >::validators().len() as u32; + let validators_len = T::ValidatorSet::validators().len() as u32; Ok(Self::local_authority_keys() .map(move |(authority_index, key)| @@ -655,7 +664,7 @@ impl pallet_session::OneSessionHandler for Module { fn on_before_session_ending() { let session_index = >::current_index(); let keys = Keys::::get(); - let current_validators = >::validators(); + let current_validators = T::ValidatorSet::validators(); let offenders = current_validators.into_iter().enumerate() .filter(|(index, id)| diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 29fe6acb3337c..dfc828cfbc03a 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -21,7 +21,7 @@ use std::cell::RefCell; -use crate::{Module, Trait}; +use crate::{Module, Trait, ValidatorSet}; use sp_runtime::Perbill; use sp_staking::{SessionIndex, offence::{ReportOffence, OffenceError}}; use sp_runtime::testing::{Header, UintAuthorityId, TestXt}; @@ -179,10 +179,17 @@ parameter_types! { pub const UnsignedPriority: u64 = 1 << 20; } +impl ValidatorSet<::ValidatorId> for Runtime { + fn validators() -> Vec<::ValidatorId> { + Session::validators() + } +} + impl Trait for Runtime { type AuthorityId = UintAuthorityId; type Event = (); type ReportUnresponsiveness = OffenceHandler; + type ValidatorSet = Self; type SessionDuration = Period; type UnsignedPriority = UnsignedPriority; type WeightInfo = (); diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 12a14e90b0e53..f0b31feb36c48 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -186,9 +186,16 @@ impl pallet_staking::Trait for Test { type WeightInfo = (); } +impl pallet_im_online::ValidatorSet<::ValidatorId> for Test { + fn validators() -> Vec<::ValidatorId> { + Session::validators() + } +} + impl pallet_im_online::Trait for Test { type AuthorityId = UintAuthorityId; type Event = Event; + type ValidatorSet = Self; type SessionDuration = Period; type ReportUnresponsiveness = Offences; type UnsignedPriority = (); From 526dbd7895e2d1a92c5f7fb0cd683542fae2748d Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sun, 20 Sep 2020 21:56:43 +0800 Subject: [PATCH 02/33] . --- frame/im-online/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 45cc895c500ea..9c1a45b15c38e 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -231,7 +231,7 @@ pub trait WeightInfo { fn validate_unsigned_and_then_heartbeat(k: u32, e: u32, ) -> Weight; } -/// The set of validators that are considered to be online. +/// The set of validators that are considered to be running an authority node. pub trait ValidatorSet { /// Returns all the validators ought to be online. fn validators() -> Vec; From 5f1ab56aa2ce8e57bc9a703204e2888689912136 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Wed, 23 Sep 2020 17:28:40 +0800 Subject: [PATCH 03/33] Add SessionInterface trait in im-online Add ValidatorId in im-online Trait Make im-online compile Make substrate binary compile --- Cargo.lock | 3 ++ bin/node/runtime/src/lib.rs | 25 ++++++++--- frame/im-online/Cargo.toml | 2 + frame/im-online/src/lib.rs | 54 +++++++++++++----------- frame/session/src/historical/mod.rs | 27 ++++-------- frame/session/src/historical/offchain.rs | 2 +- frame/session/src/historical/onchain.rs | 5 ++- frame/session/src/lib.rs | 11 +---- frame/staking/Cargo.toml | 2 + frame/staking/src/lib.rs | 4 +- primitives/session/Cargo.toml | 4 +- primitives/session/src/lib.rs | 14 ++++++ 12 files changed, 89 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e80ba143d3de8..5f79fa96b8060 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4615,6 +4615,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-session", "sp-staking", "sp-std", ] @@ -4895,6 +4896,7 @@ dependencies = [ "sp-io", "sp-npos-elections", "sp-runtime", + "sp-session", "sp-staking", "sp-std", "sp-storage", @@ -8346,6 +8348,7 @@ dependencies = [ name = "sp-session" version = "2.0.0-rc6" dependencies = [ + "frame-support", "parity-scale-codec", "sp-api", "sp-core", diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 4e8022b563a04..53685b3b65d93 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -391,10 +391,16 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } -impl pallet_session::Trait for Runtime { - type Event = Event; + +impl sp_session::ValidatorIdentification<::AccountId> for Runtime { type ValidatorId = ::AccountId; type ValidatorIdOf = pallet_staking::StashOf; + type FullIdentification = pallet_staking::Exposure; + type FullIdentificationOf = pallet_staking::ExposureOf; +} + +impl pallet_session::Trait for Runtime { + type Event = Event; type ShouldEndSession = Babe; type NextSessionRotation = Babe; type SessionManager = pallet_session::historical::NoteHistoricalRoot; @@ -405,8 +411,6 @@ impl pallet_session::Trait for Runtime { } impl pallet_session::historical::Trait for Runtime { - type FullIdentification = pallet_staking::Exposure; - type FullIdentificationOf = pallet_staking::ExposureOf; } pallet_staking_reward_curve::build! { @@ -730,15 +734,22 @@ impl frame_system::offchain::SendTransactionTypes for Runtime where type OverarchingCall = Call; } -impl pallet_im_online::ValidatorSet<::ValidatorId> for Runtime { - fn validators() -> Vec<::ValidatorId> { +impl pallet_im_online::ValidatorSet<::AccountId>>::ValidatorId> for Runtime { + fn validators() -> Vec<::AccountId>>::ValidatorId> { Session::validators() } } +impl pallet_im_online::SessionInterface for Runtime { + fn current_index() -> sp_staking::SessionIndex { + Session::current_index() + } +} + impl pallet_im_online::Trait for Runtime { type AuthorityId = ImOnlineId; type Event = Event; + type SessionInterface = Self; type ValidatorSet = Self; type SessionDuration = SessionDuration; type ReportUnresponsiveness = Offences; @@ -752,7 +763,7 @@ parameter_types! { impl pallet_offences::Trait for Runtime { type Event = Event; - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = sp_session::IdentificationTuple<::AccountId, Self>; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; type WeightInfo = (); diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 8541b46c9c879..ed47692c9b62a 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -20,6 +20,7 @@ sp-std = { version = "2.0.0-rc6", default-features = false, path = "../../primit serde = { version = "1.0.101", optional = true } pallet-session = { version = "2.0.0-rc6", default-features = false, path = "../session" } sp-io = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/io" } +sp-session = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/session" } sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/staking" } frame-support = { version = "2.0.0-rc6", default-features = false, path = "../support" } @@ -38,6 +39,7 @@ std = [ "serde", "pallet-session/std", "sp-io/std", + "sp-session/std", "sp-runtime/std", "sp-staking/std", "frame-support/std", diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 45cc895c500ea..addc71470cccb 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -79,7 +79,7 @@ use codec::{Encode, Decode}; use sp_core::offchain::OpaqueNetworkState; use sp_std::prelude::*; use sp_std::convert::TryInto; -use pallet_session::historical::IdentificationTuple; +use sp_session::{IdentificationTuple, ValidatorIdentification}; use sp_runtime::{ offchain::storage::StorageValueRef, RuntimeDebug, @@ -237,7 +237,11 @@ pub trait ValidatorSet { fn validators() -> Vec; } -pub trait Trait: SendTransactionTypes> + pallet_session::historical::Trait { +pub trait SessionInterface { + fn current_index() -> SessionIndex; +} + +pub trait Trait: SendTransactionTypes> + ValidatorIdentification<::AccountId> + frame_system::Trait { /// The identifier type for an authority. type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + Ord; @@ -252,15 +256,17 @@ pub trait Trait: SendTransactionTypes> + pallet_session::historical:: /// there is a chance the authority will produce a block and they won't be necessary. type SessionDuration: Get; + type SessionInterface: SessionInterface; + /// A set of validators expected to be online. - type ValidatorSet: ValidatorSet<::ValidatorId>; + type ValidatorSet: ValidatorSet<::AccountId>>::ValidatorId>; /// A type that gives us the ability to submit unresponsiveness offence reports. type ReportUnresponsiveness: ReportOffence< Self::AccountId, - IdentificationTuple, - UnresponsivenessOffence>, + IdentificationTuple<::AccountId, Self>, + UnresponsivenessOffence::AccountId, Self>>, >; /// A configuration for base priority of unsigned transactions. @@ -276,7 +282,7 @@ pub trait Trait: SendTransactionTypes> + pallet_session::historical:: decl_event!( pub enum Event where ::AuthorityId, - IdentificationTuple = IdentificationTuple, + IdentificationTuple = IdentificationTuple<::AccountId, T>, { /// A new heartbeat was received from `AuthorityId` \[authority_id\] HeartbeatReceived(AuthorityId), @@ -306,10 +312,10 @@ decl_storage! { double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) AuthIndex => Option>; - /// For each session index, we keep a mapping of `T::ValidatorId` to the + /// For each session index, we keep a mapping of `::ValidatorId` to the /// number of blocks authored by the given authority. AuthoredBlocks get(fn authored_blocks): - double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) T::ValidatorId + double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) >::ValidatorId => u32; } add_extra_genesis { @@ -358,7 +364,7 @@ decl_module! { ) { ensure_none(origin)?; - let current_session = >::current_index(); + let current_session = T::SessionInterface::current_index(); let exists = ::contains_key( ¤t_session, &heartbeat.authority_index @@ -410,12 +416,12 @@ type OffchainResult = Result::Bl /// Keep track of number of authored blocks per authority, uncles are counted as /// well since they're a valid proof of being online. -impl pallet_authorship::EventHandler for Module { - fn note_author(author: T::ValidatorId) { +impl pallet_authorship::EventHandler<>::ValidatorId, T::BlockNumber> for Module { + fn note_author(author: >::ValidatorId) { Self::note_authorship(author); } - fn note_uncle(author: T::ValidatorId, _age: T::BlockNumber) { + fn note_uncle(author: >::ValidatorId, _age: T::BlockNumber) { Self::note_authorship(author); } } @@ -437,8 +443,8 @@ impl Module { Self::is_online_aux(authority_index, authority) } - fn is_online_aux(authority_index: AuthIndex, authority: &T::ValidatorId) -> bool { - let current_session = >::current_index(); + fn is_online_aux(authority_index: AuthIndex, authority: &>::ValidatorId) -> bool { + let current_session = T::SessionInterface::current_index(); ::contains_key(¤t_session, &authority_index) || >::get( @@ -450,13 +456,13 @@ impl Module { /// Returns `true` if a heartbeat has been received for the authority at `authority_index` in /// the authorities series, during the current session. Otherwise `false`. pub fn received_heartbeat_in_current_session(authority_index: AuthIndex) -> bool { - let current_session = >::current_index(); + let current_session = T::SessionInterface::current_index(); ::contains_key(¤t_session, &authority_index) } /// Note that the given authority has authored a block in the current session. - fn note_authorship(author: T::ValidatorId) { - let current_session = >::current_index(); + fn note_authorship(author: >::ValidatorId) { + let current_session = T::SessionInterface::current_index(); >::mutate( ¤t_session, @@ -473,7 +479,7 @@ impl Module { return Err(OffchainErr::TooEarly(heartbeat_after)) } - let session_index = >::current_index(); + let session_index = T::SessionInterface::current_index(); let validators_len = T::ValidatorSet::validators().len() as u32; Ok(Self::local_authority_keys() @@ -652,7 +658,7 @@ impl pallet_session::OneSessionHandler for Module { } fn on_before_session_ending() { - let session_index = >::current_index(); + let session_index = T::SessionInterface::current_index(); let keys = Keys::::get(); let current_validators = T::ValidatorSet::validators(); @@ -660,14 +666,14 @@ impl pallet_session::OneSessionHandler for Module { .filter(|(index, id)| !Self::is_online_aux(*index as u32, id) ).filter_map(|(_, id)| - T::FullIdentificationOf::convert(id.clone()).map(|full_id| (id, full_id)) - ).collect::>>(); + >::FullIdentificationOf::convert(id.clone()).map(|full_id| (id, full_id)) + ).collect::>>(); // Remove all received heartbeats and number of authored blocks from the // current session, they have already been processed and won't be needed // anymore. - ::remove_prefix(&>::current_index()); - >::remove_prefix(&>::current_index()); + ::remove_prefix(&T::SessionInterface::current_index()); + >::remove_prefix(&T::SessionInterface::current_index()); if offenders.is_empty() { Self::deposit_event(RawEvent::AllGood); @@ -704,7 +710,7 @@ impl frame_support::unsigned::ValidateUnsigned for Module { } // check if session index from heartbeat is recent - let current_session = >::current_index(); + let current_session = T::SessionInterface::current_index(); if heartbeat.session_index != current_session { return InvalidTransaction::Stale.into(); } diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index 20c3d57464c89..d2e0fa61091c3 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -32,28 +32,19 @@ 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::print; use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX}; use sp_trie::trie_types::{TrieDBMut, TrieDB}; use super::{SessionIndex, Module as SessionModule}; +pub use sp_session::IdentificationTuple; + mod shared; pub mod offchain; pub mod onchain; /// Trait necessary for the historical module. pub trait Trait: super::Trait { - /// Full identification of the validator. - type FullIdentification: Parameter; - - /// A conversion from validator ID to full identification. - /// - /// This should contain any references to economic actors associated with the - /// validator, since they may be outdated by the time this is queried from a - /// historical trie. - /// - /// It must return the identification for the current session index. - type FullIdentificationOf: Convert>; } decl_storage! { @@ -160,7 +151,7 @@ impl crate::SessionManager for NoteHistoricalRoot = (::ValidatorId, ::FullIdentification); +// pub type IdentificationTuple = (::ValidatorId, ::FullIdentification); /// A trie instance for checking and generating proofs. pub struct ProvingTrie { @@ -235,7 +226,7 @@ impl ProvingTrie { val_idx.using_encoded(|s| { trie.get_with(s, &mut recorder) .ok()? - .and_then(|raw| >::decode(&mut &*raw).ok()) + .and_then(|raw| >::decode(&mut &*raw).ok()) })?; Some(recorder.drain().into_iter().map(|r| r.data).collect()) @@ -248,7 +239,7 @@ impl ProvingTrie { // Check a proof contained within the current memory-db. Returns `None` if the // nodes within the current `MemoryDB` are insufficient to query the item. - fn query(&self, key_id: KeyTypeId, key_data: &[u8]) -> Option> { + fn query(&self, key_id: KeyTypeId, key_data: &[u8]) -> Option> { let trie = TrieDB::new(&self.db, &self.root).ok()?; let val_idx = (key_id, key_data).using_encoded(|s| trie.get(s)) .ok()? @@ -256,7 +247,7 @@ impl ProvingTrie { val_idx.using_encoded(|s| trie.get(s)) .ok()? - .and_then(|raw| >::decode(&mut &*raw).ok()) + .and_then(|raw| >::decode(&mut &*raw).ok()) } } @@ -264,7 +255,7 @@ impl> frame_support::traits::KeyOwnerProofSystem<(KeyTy for Module { type Proof = MembershipProof; - type IdentificationTuple = IdentificationTuple; + type IdentificationTuple = IdentificationTuple; fn prove(key: (KeyTypeId, D)) -> Option { let session = >::current_index(); @@ -289,7 +280,7 @@ impl> frame_support::traits::KeyOwnerProofSystem<(KeyTy }) } - fn check_proof(key: (KeyTypeId, D), proof: Self::Proof) -> Option> { + fn check_proof(key: (KeyTypeId, D), proof: Self::Proof) -> Option> { let (id, data) = key; if proof.session == >::current_index() { diff --git a/frame/session/src/historical/offchain.rs b/frame/session/src/historical/offchain.rs index 97655d1a18b32..1916862123478 100644 --- a/frame/session/src/historical/offchain.rs +++ b/frame/session/src/historical/offchain.rs @@ -37,7 +37,7 @@ use sp_std::prelude::*; /// A set of validators, which was used for a fixed session index. struct ValidatorSet { - validator_set: Vec>, + validator_set: Vec::AccountId, T>>, } impl ValidatorSet { diff --git a/frame/session/src/historical/onchain.rs b/frame/session/src/historical/onchain.rs index 745603a49829b..82b9d3ef8d2ae 100644 --- a/frame/session/src/historical/onchain.rs +++ b/frame/session/src/historical/onchain.rs @@ -26,6 +26,7 @@ use super::Trait as HistoricalTrait; use super::shared; use sp_std::prelude::*; +use sp_session::ValidatorIdentification; /// Store the validator-set associated to the `session_index` to the off-chain database. /// @@ -40,9 +41,9 @@ pub fn store_session_validator_set_to_offchain>::validators() .into_iter() - .filter_map(|validator_id: ::ValidatorId| { + .filter_map(|validator_id: >::ValidatorId| { let full_identification = - <::FullIdentificationOf>::convert(validator_id.clone()); + <>::FullIdentificationOf>::convert(validator_id.clone()); full_identification.map(|full_identification| (validator_id, full_identification)) }) .collect::>(); diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index ede88b26f99bb..8a32c30f1c8f1 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -102,6 +102,7 @@ use sp_std::{prelude::*, marker::PhantomData, ops::{Sub, Rem}}; use codec::Decode; +use sp_session::ValidatorIdentification; use sp_runtime::{KeyTypeId, Perbill, RuntimeAppPublic, BoundToRuntimeAppPublic}; use sp_runtime::traits::{Convert, Zero, Member, OpaqueKeys, Saturating}; use sp_staking::SessionIndex; @@ -361,18 +362,10 @@ impl WeightInfo for () { fn purge_keys(_n: u32, ) -> Weight { 1_000_000_000 } } -pub trait Trait: frame_system::Trait { +pub trait Trait: ValidatorIdentification<::AccountId> + frame_system::Trait { /// The overarching event type. type Event: From + Into<::Event>; - /// A stable ID for a validator. - type ValidatorId: Member + Parameter; - - /// A conversion from account ID to validator ID. - /// - /// Its cost must be at most one storage read. - type ValidatorIdOf: Convert>; - /// Indicator for when to end the session. type ShouldEndSession: ShouldEndSession; diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index bd64dbcb1d3e0..6ed7b15110939 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -20,6 +20,7 @@ sp-npos-elections = { version = "2.0.0-rc6", default-features = false, path = ". sp-io ={ version = "2.0.0-rc6", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/staking" } +sp-session = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0-rc6", default-features = false, path = "../support" } frame-system = { version = "2.0.0-rc6", default-features = false, path = "../system" } pallet-session = { version = "2.0.0-rc6", default-features = false, features = ["historical"], path = "../session" } @@ -54,6 +55,7 @@ std = [ "frame-support/std", "sp-runtime/std", "sp-staking/std", + "sp-session/std", "pallet-session/std", "frame-system/std", "pallet-authorship/std", diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 7061832b0460c..496cac3bd5b81 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -3144,7 +3144,7 @@ impl Convert> /// This is intended to be used with `FilterHistoricalOffences`. impl - OnOffenceHandler, Weight> + OnOffenceHandler::AccountId, T>, Weight> for Module where T: pallet_session::Trait::AccountId>, T: pallet_session::historical::Trait< @@ -3159,7 +3159,7 @@ for Module where >, { fn on_offence( - offenders: &[OffenceDetails>], + offenders: &[OffenceDetails::AccountId, T>>], slash_fraction: &[Perbill], slash_session: SessionIndex, ) -> Result { diff --git a/primitives/session/Cargo.toml b/primitives/session/Cargo.toml index b8bad3ed8daba..3a2d138960318 100644 --- a/primitives/session/Cargo.toml +++ b/primitives/session/Cargo.toml @@ -17,7 +17,8 @@ sp-api = { version = "2.0.0-rc6", default-features = false, path = "../api" } sp-core = { version = "2.0.0-rc6", default-features = false, path = "../core" } sp-std = { version = "2.0.0-rc6", default-features = false, path = "../std" } sp-staking = { version = "2.0.0-rc6", default-features = false, path = "../staking" } -sp-runtime = { version = "2.0.0-rc6", optional = true, path = "../runtime" } +sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../runtime" } +frame-support = { version = "2.0.0-rc6", default-features = false, path = "../../frame/support" } [features] default = [ "std" ] @@ -28,4 +29,5 @@ std = [ "sp-std/std", "sp-staking/std", "sp-runtime/std", + "frame-support/std", ] diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index 38a852dafd1dd..7792a7c65d93d 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -29,6 +29,8 @@ use sp_api::ProvideRuntimeApi; use sp_core::RuntimeDebug; use sp_core::crypto::KeyTypeId; use sp_staking::SessionIndex; +use sp_runtime::traits::{Member, Convert}; +use frame_support::Parameter; use sp_std::vec::Vec; sp_api::decl_runtime_apis! { @@ -53,6 +55,18 @@ sp_api::decl_runtime_apis! { /// Number of validators in a given session. pub type ValidatorCount = u32; +pub type IdentificationTuple = (>::ValidatorId, >::FullIdentification); + +pub trait ValidatorIdentification { + type ValidatorId: Member + Parameter; + + type ValidatorIdOf: Convert>; + + type FullIdentification: Parameter; + + type FullIdentificationOf: Convert>; +} + /// Proof of membership of a specific key in a given session. #[derive(Encode, Decode, Clone, Eq, PartialEq, Default, RuntimeDebug)] pub struct MembershipProof { From 141b27b36c1341682487fd6365c2809273e85b56 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 24 Sep 2020 13:48:38 +0800 Subject: [PATCH 04/33] Fix merging issue --- frame/im-online/Cargo.toml | 14 +++++++------- frame/session/src/historical/mod.rs | 3 --- frame/staking/Cargo.toml | 22 +++++++++++----------- primitives/session/Cargo.toml | 12 ++++++------ primitives/session/src/lib.rs | 13 +++++++++++++ 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 7a31ce0b91421..6e975ed4c7155 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -19,13 +19,13 @@ codec = { package = "parity-scale-codec", version = "1.3.4", default-features = sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } serde = { version = "1.0.101", optional = true } -pallet-session = { version = "2.0.0-rc6", default-features = false, path = "../session" } -sp-io = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/io" } -sp-session = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/session" } -sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/runtime" } -sp-staking = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/staking" } -frame-support = { version = "2.0.0-rc6", default-features = false, path = "../support" } -frame-system = { version = "2.0.0-rc6", default-features = false, path = "../system" } +pallet-session = { version = "2.0.0", default-features = false, path = "../session" } +sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } +sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } +sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } +sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } +frame-support = { version = "2.0.0", default-features = false, path = "../support" } +frame-system = { version = "2.0.0", default-features = false, path = "../system" } frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index d2e0fa61091c3..e8be032fce37a 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -150,9 +150,6 @@ impl crate::SessionManager for NoteHistoricalRoot = (::ValidatorId, ::FullIdentification); - /// A trie instance for checking and generating proofs. pub struct ProvingTrie { db: MemoryDB, diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index a7216a98f1bb3..4c8cbf0b4dd10 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -16,17 +16,17 @@ targets = ["x86_64-unknown-linux-gnu"] static_assertions = "1.1.0" serde = { version = "1.0.101", optional = true } codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/std" } -sp-npos-elections = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/npos-elections" } -sp-io ={ version = "2.0.0-rc6", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/runtime" } -sp-staking = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/staking" } -sp-session = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/session" } -frame-support = { version = "2.0.0-rc6", default-features = false, path = "../support" } -frame-system = { version = "2.0.0-rc6", default-features = false, path = "../system" } -pallet-session = { version = "2.0.0-rc6", default-features = false, features = ["historical"], path = "../session" } -pallet-authorship = { version = "2.0.0-rc6", default-features = false, path = "../authorship" } -sp-application-crypto = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/application-crypto" } +sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-npos-elections = { version = "2.0.0", default-features = false, path = "../../primitives/npos-elections" } +sp-io ={ version = "2.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } +sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } +sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } +frame-support = { version = "2.0.0", default-features = false, path = "../support" } +frame-system = { version = "2.0.0", default-features = false, path = "../system" } +pallet-session = { version = "2.0.0", default-features = false, features = ["historical"], path = "../session" } +pallet-authorship = { version = "2.0.0", default-features = false, path = "../authorship" } +sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } # Optional imports for benchmarking frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } diff --git a/primitives/session/Cargo.toml b/primitives/session/Cargo.toml index ba56261199dd0..10aa4fdfcfcde 100644 --- a/primitives/session/Cargo.toml +++ b/primitives/session/Cargo.toml @@ -14,12 +14,12 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false, features = ["derive"] } -sp-api = { version = "2.0.0-rc6", default-features = false, path = "../api" } -sp-core = { version = "2.0.0-rc6", default-features = false, path = "../core" } -sp-std = { version = "2.0.0-rc6", default-features = false, path = "../std" } -sp-staking = { version = "2.0.0-rc6", default-features = false, path = "../staking" } -sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../runtime" } -frame-support = { version = "2.0.0-rc6", default-features = false, path = "../../frame/support" } +sp-api = { version = "2.0.0", default-features = false, path = "../api" } +sp-core = { version = "2.0.0", default-features = false, path = "../core" } +sp-std = { version = "2.0.0", default-features = false, path = "../std" } +sp-staking = { version = "2.0.0", default-features = false, path = "../staking" } +sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } +frame-support = { version = "2.0.0", default-features = false, path = "../../frame/support" } [features] default = [ "std" ] diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index 7792a7c65d93d..c219c78265c44 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -55,15 +55,28 @@ sp_api::decl_runtime_apis! { /// Number of validators in a given session. pub type ValidatorCount = u32; +/// A tuple of the validator's ID and their full identification.:w pub type IdentificationTuple = (>::ValidatorId, >::FullIdentification); pub trait ValidatorIdentification { + /// A stable ID for a validator. type ValidatorId: Member + Parameter; + /// A conversion from account ID to validator ID. + /// + /// Its cost must be at most one storage read. type ValidatorIdOf: Convert>; + /// Full identification of the validator. type FullIdentification: Parameter; + /// A conversion from validator ID to full identification. + /// + /// This should contain any references to economic actors associated with the + /// validator, since they may be outdated by the time this is queried from a + /// historical trie. + /// + /// It must return the identification for the current session index. type FullIdentificationOf: Convert>; } From 9bed3212314294ad41c3609cafb492a571c5ef5b Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 24 Sep 2020 14:58:29 +0800 Subject: [PATCH 05/33] Make all compile --- Cargo.lock | 2 ++ frame/offences/benchmarking/Cargo.toml | 2 ++ frame/offences/benchmarking/src/lib.rs | 17 +++++++++-------- frame/staking/fuzzer/Cargo.toml | 1 + frame/staking/fuzzer/src/mock.rs | 7 +++++-- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1df116f5870d3..15ecd6b96bebd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4708,6 +4708,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-session", "sp-staking", "sp-std", ] @@ -4898,6 +4899,7 @@ dependencies = [ "sp-io", "sp-npos-elections", "sp-runtime", + "sp-session", "sp-std", ] diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml index 7a95cebc4fb21..19ddaabd917d7 100644 --- a/frame/offences/benchmarking/Cargo.toml +++ b/frame/offences/benchmarking/Cargo.toml @@ -26,6 +26,7 @@ pallet-session = { version = "2.0.0", default-features = false, path = "../../se pallet-staking = { version = "2.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../../primitives/staking" } +sp-session = { version = "2.0.0", default-features = false, path = "../../../primitives/session" } sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } [dev-dependencies] @@ -50,6 +51,7 @@ std = [ "pallet-staking/std", "sp-runtime/std", "sp-staking/std", + "sp-session/std", "sp-std/std", "codec/std", ] diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index e35050992368a..af51531d71477 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -30,13 +30,14 @@ use frame_support::traits::{Currency, OnInitialize}; use sp_runtime::{Perbill, traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto}}; use sp_staking::offence::{ReportOffence, Offence, OffenceDetails}; +use sp_session::{IdentificationTuple, ValidatorIdentification}; use pallet_balances::{Trait as BalancesTrait}; use pallet_babe::BabeEquivocationOffence; use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot}; use pallet_im_online::{Trait as ImOnlineTrait, Module as ImOnline, UnresponsivenessOffence}; use pallet_offences::{Trait as OffencesTrait, Module as Offences}; -use pallet_session::historical::{Trait as HistoricalTrait, IdentificationTuple}; +use pallet_session::historical::{Trait as HistoricalTrait}; use pallet_session::{Trait as SessionTrait, SessionManager}; use pallet_staking::{ Module as Staking, Trait as StakingTrait, RewardDestination, ValidatorPrefs, @@ -66,13 +67,13 @@ pub trait Trait: /// and the one required by offences. pub trait IdTupleConvert { /// Convert identification tuple from `historical` trait to the one expected by `offences`. - fn convert(id: IdentificationTuple) -> ::IdentificationTuple; + fn convert(id: IdentificationTuple<::AccountId, T>) -> ::IdentificationTuple; } impl IdTupleConvert for T where - ::IdentificationTuple: From> + ::IdentificationTuple: From::AccountId, T>> { - fn convert(id: IdentificationTuple) -> ::IdentificationTuple { + fn convert(id: IdentificationTuple<::AccountId, T>) -> ::IdentificationTuple { id.into() } } @@ -150,7 +151,7 @@ fn create_offender(n: u32, nominators: u32) -> Result, &'s } fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< - (Vec>, Vec>), + (Vec::AccountId, T>>, Vec>), &'static str > { Staking::::new_session(0); @@ -165,13 +166,13 @@ fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< let id_tuples = offenders.iter() .map(|offender| - ::ValidatorIdOf::convert(offender.controller.clone()) + ::AccountId>>::ValidatorIdOf::convert(offender.controller.clone()) .expect("failed to get validator id from account id")) .map(|validator_id| - ::FullIdentificationOf::convert(validator_id.clone()) + ::AccountId>>::FullIdentificationOf::convert(validator_id.clone()) .map(|full_id| (validator_id, full_id)) .expect("failed to convert validator id to full identification")) - .collect::>>(); + .collect::::AccountId, T>>>(); Ok((id_tuples, offenders)) } diff --git a/frame/staking/fuzzer/Cargo.toml b/frame/staking/fuzzer/Cargo.toml index e1431aa54d4a7..5b758f7e7c2a1 100644 --- a/frame/staking/fuzzer/Cargo.toml +++ b/frame/staking/fuzzer/Cargo.toml @@ -28,6 +28,7 @@ sp-io ={ version = "2.0.0", path = "../../../primitives/io" } sp-core = { version = "2.0.0", path = "../../../primitives/core" } sp-npos-elections = { version = "2.0.0", path = "../../../primitives/npos-elections" } sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } +sp-session = { version = "2.0.0", path = "../../../primitives/session" } [[bin]] name = "submit_solution" diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index aae044d75acd9..3d07d3401731f 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -112,6 +112,11 @@ impl pallet_timestamp::Trait for Test { type WeightInfo = (); } impl pallet_session::historical::Trait for Test { +} + +impl sp_session::ValidatorIdentification<::AccountId> for Test { + type ValidatorId = AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -144,8 +149,6 @@ impl pallet_session::Trait for Test { type NextSessionRotation = pallet_session::PeriodicSessions<(), ()>; type SessionHandler = TestSessionHandler; type Event = (); - type ValidatorId = AccountId; - type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); type WeightInfo = (); } From 1582b7bbadbc9a32cf363176e488ff9e217dd319 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 24 Sep 2020 16:06:57 +0800 Subject: [PATCH 06/33] Fix tests --- Cargo.lock | 1 + frame/authority-discovery/Cargo.toml | 1 + frame/authority-discovery/src/lib.rs | 14 +++++++------ frame/babe/src/mock.rs | 16 ++++++++------- frame/grandpa/src/mock.rs | 16 ++++++++------- frame/im-online/src/mock.rs | 26 ++++++++++++++++-------- frame/offences/benchmarking/src/mock.rs | 20 ++++++++++++------ frame/session/Cargo.toml | 1 + frame/session/benchmarking/src/mock.rs | 7 ++++--- frame/session/src/historical/offchain.rs | 7 +++---- frame/session/src/mock.rs | 14 +++++++------ frame/staking/src/mock.rs | 19 +++++++++-------- frame/staking/src/tests.rs | 2 +- 13 files changed, 88 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 15ecd6b96bebd..f52e86718ffc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4255,6 +4255,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-session", "sp-staking", "sp-std", ] diff --git a/frame/authority-discovery/Cargo.toml b/frame/authority-discovery/Cargo.toml index 0e1db74632786..2b53d196a4ca8 100644 --- a/frame/authority-discovery/Cargo.toml +++ b/frame/authority-discovery/Cargo.toml @@ -26,6 +26,7 @@ frame-system = { version = "2.0.0", default-features = false, path = "../system" [dev-dependencies] sp-core = { version = "2.0.0", path = "../../primitives/core" } sp-io = { version = "2.0.0", path = "../../primitives/io" } +sp-session = { version = "2.0.0", path = "../../primitives/session" } sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } [features] diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 09be533474fca..1a4016b7d01eb 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -113,23 +113,25 @@ mod tests { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } + impl sp_session::ValidatorIdentification for Test { + type ValidatorId = AuthorityId; + type ValidatorIdOf = ConvertInto; + type FullIdentification = (); + type FullIdentificationOf = (); + } + impl pallet_session::Trait for Test { type SessionManager = (); type Keys = UintAuthorityId; type ShouldEndSession = pallet_session::PeriodicSessions; type SessionHandler = TestSessionHandler; type Event = (); - type ValidatorId = AuthorityId; - type ValidatorIdOf = ConvertInto; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; type WeightInfo = (); } - impl pallet_session::historical::Trait for Test { - type FullIdentification = (); - type FullIdentificationOf = (); - } + impl pallet_session::historical::Trait for Test {} pub type BlockNumber = u64; diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index 3adad1c4bf794..df0cb05673fac 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -107,10 +107,15 @@ impl_opaque_keys! { } } -impl pallet_session::Trait for Test { - type Event = (); +impl sp_session::ValidatorIdentification for Test { type ValidatorId = ::AccountId; type ValidatorIdOf = pallet_staking::StashOf; + type FullIdentification = pallet_staking::Exposure; + type FullIdentificationOf = pallet_staking::ExposureOf; +} + +impl pallet_session::Trait for Test { + type Event = (); type ShouldEndSession = Babe; type NextSessionRotation = Babe; type SessionManager = pallet_session::historical::NoteHistoricalRoot; @@ -120,10 +125,7 @@ impl pallet_session::Trait for Test { type WeightInfo = (); } -impl pallet_session::historical::Trait for Test { - type FullIdentification = pallet_staking::Exposure; - type FullIdentificationOf = pallet_staking::ExposureOf; -} +impl pallet_session::historical::Trait for Test {} parameter_types! { pub const UncleGenerations: u64 = 0; @@ -227,7 +229,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = (); - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = sp_session::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index 215a1f0a35932..43c516f661224 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -121,11 +121,16 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } +impl sp_session::ValidatorIdentification for Test { + type ValidatorId = u64; + type ValidatorIdOf = pallet_staking::StashOf; + type FullIdentification = pallet_staking::Exposure; + type FullIdentificationOf = pallet_staking::ExposureOf; +} + /// Custom `SessionHandler` since we use `TestSessionKeys` as `Keys`. impl pallet_session::Trait for Test { type Event = TestEvent; - type ValidatorId = u64; - type ValidatorIdOf = pallet_staking::StashOf; type ShouldEndSession = pallet_session::PeriodicSessions; type NextSessionRotation = pallet_session::PeriodicSessions; type SessionManager = pallet_session::historical::NoteHistoricalRoot; @@ -135,10 +140,7 @@ impl pallet_session::Trait for Test { type WeightInfo = (); } -impl pallet_session::historical::Trait for Test { - type FullIdentification = pallet_staking::Exposure; - type FullIdentificationOf = pallet_staking::ExposureOf; -} +impl pallet_session::historical::Trait for Test {} parameter_types! { pub const UncleGenerations: u64 = 0; @@ -242,7 +244,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = TestEvent; - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = sp_session::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 05f1344bbe89f..f37390fc1fb03 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -28,6 +28,7 @@ use sp_runtime::testing::{Header, UintAuthorityId, TestXt}; use sp_runtime::traits::{IdentityLookup, BlakeTwo256, ConvertInto}; use sp_core::H256; use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types, weights::Weight}; +use sp_session::ValidatorIdentification; impl_outer_origin!{ pub enum Origin for Runtime {} @@ -146,12 +147,17 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } +impl sp_session::ValidatorIdentification for Runtime { + type ValidatorId = u64; + type ValidatorIdOf = ConvertInto; + type FullIdentification = u64; + type FullIdentificationOf = ConvertInto; +} + impl pallet_session::Trait for Runtime { type ShouldEndSession = pallet_session::PeriodicSessions; type SessionManager = pallet_session::historical::NoteHistoricalRoot; type SessionHandler = (ImOnline, ); - type ValidatorId = u64; - type ValidatorIdOf = ConvertInto; type Keys = UintAuthorityId; type Event = (); type DisabledValidatorsThreshold = DisabledValidatorsThreshold; @@ -159,10 +165,7 @@ impl pallet_session::Trait for Runtime { type WeightInfo = (); } -impl pallet_session::historical::Trait for Runtime { - type FullIdentification = u64; - type FullIdentificationOf = ConvertInto; -} +impl pallet_session::historical::Trait for Runtime {} parameter_types! { pub const UncleGenerations: u32 = 5; @@ -179,17 +182,24 @@ parameter_types! { pub const UnsignedPriority: u64 = 1 << 20; } -impl ValidatorSet<::ValidatorId> for Runtime { - fn validators() -> Vec<::ValidatorId> { +impl ValidatorSet<>::ValidatorId> for Runtime { + fn validators() -> Vec<>::ValidatorId> { Session::validators() } } +impl crate::SessionInterface for Runtime { + fn current_index() -> SessionIndex { + Session::current_index() + } +} + impl Trait for Runtime { type AuthorityId = UintAuthorityId; type Event = (); type ReportUnresponsiveness = OffenceHandler; type ValidatorSet = Self; + type SessionInterface = Self; type SessionDuration = Period; type UnsignedPriority = UnsignedPriority; type WeightInfo = (); diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index a6927fd22a6c9..7b6c4960d5ccd 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -90,10 +90,13 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl pallet_session::historical::Trait for Test { +impl sp_session::ValidatorIdentification for Test { + type ValidatorId = AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } +impl pallet_session::historical::Trait for Test {} sp_runtime::impl_opaque_keys! { pub struct SessionKeys { @@ -128,8 +131,6 @@ impl pallet_session::Trait for Test { type NextSessionRotation = pallet_session::PeriodicSessions; type SessionHandler = TestSessionHandler; type Event = Event; - type ValidatorId = AccountId; - type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); type WeightInfo = (); } @@ -186,16 +187,23 @@ impl pallet_staking::Trait for Test { type WeightInfo = (); } -impl pallet_im_online::ValidatorSet<::ValidatorId> for Test { - fn validators() -> Vec<::ValidatorId> { +impl pallet_im_online::ValidatorSet<>::ValidatorId> for Test { + fn validators() -> Vec<>::ValidatorId> { Session::validators() } } +impl pallet_im_online::SessionInterface for Test { + fn current_index() -> sp_staking::SessionIndex { + Session::current_index() + } +} + impl pallet_im_online::Trait for Test { type AuthorityId = UintAuthorityId; type Event = Event; type ValidatorSet = Self; + type SessionInterface = Self; type SessionDuration = Period; type ReportUnresponsiveness = Offences; type UnsignedPriority = (); @@ -208,7 +216,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = Event; - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = sp_session::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index ea3a3d3cdf7fa..8dfd59651a4f7 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -29,6 +29,7 @@ impl-trait-for-tuples = "0.1.3" [dev-dependencies] sp-application-crypto = { version = "2.0.0", path = "../../primitives/application-crypto" } +sp-session = { version = "2.0.0", path = "../../primitives/session" } lazy_static = "1.4.0" [features] diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index 645ac14d88a44..2af2339db25fa 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -106,10 +106,13 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl pallet_session::historical::Trait for Test { +impl sp_session::ValidatorIdentification for Test { + type ValidatorId = AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } +impl pallet_session::historical::Trait for Test {} sp_runtime::impl_opaque_keys! { pub struct SessionKeys { @@ -139,8 +142,6 @@ impl pallet_session::Trait for Test { type NextSessionRotation = pallet_session::PeriodicSessions<(), ()>; type SessionHandler = TestSessionHandler; type Event = (); - type ValidatorId = AccountId; - type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); type WeightInfo = (); } diff --git a/frame/session/src/historical/offchain.rs b/frame/session/src/historical/offchain.rs index 1916862123478..1eecc52051d45 100644 --- a/frame/session/src/historical/offchain.rs +++ b/frame/session/src/historical/offchain.rs @@ -152,6 +152,7 @@ mod tests { }; use sp_runtime::testing::UintAuthorityId; + use sp_session::ValidatorIdentification; type Historical = Module; @@ -189,12 +190,10 @@ mod tests { #[test] fn encode_decode_roundtrip() { use codec::{Decode, Encode}; - use super::super::super::Trait as SessionTrait; - use super::super::Trait as HistoricalTrait; let sample = ( - 22u32 as ::ValidatorId, - 7_777_777 as ::FullIdentification); + 22u32 as >::ValidatorId, + 7_777_777 as >::FullIdentification); let encoded = sample.encode(); let decoded = Decode::decode(&mut encoded.as_slice()).expect("Must decode"); diff --git a/frame/session/src/mock.rs b/frame/session/src/mock.rs index 1d787ac53b438..700e8394cfc4c 100644 --- a/frame/session/src/mock.rs +++ b/frame/session/src/mock.rs @@ -211,6 +211,13 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } +impl sp_session::ValidatorIdentification for Test { + type ValidatorId = u64; + type ValidatorIdOf = ConvertInto; + type FullIdentification = u64; + type FullIdentificationOf = sp_runtime::traits::ConvertInto; +} + impl Trait for Test { type ShouldEndSession = TestShouldEndSession; #[cfg(feature = "historical")] @@ -218,8 +225,6 @@ impl Trait for Test { #[cfg(not(feature = "historical"))] type SessionManager = TestSessionManager; type SessionHandler = TestSessionHandler; - type ValidatorId = u64; - type ValidatorIdOf = ConvertInto; type Keys = MockSessionKeys; type Event = (); type DisabledValidatorsThreshold = DisabledValidatorsThreshold; @@ -228,10 +233,7 @@ impl Trait for Test { } #[cfg(feature = "historical")] -impl crate::historical::Trait for Test { - type FullIdentification = u64; - type FullIdentificationOf = sp_runtime::traits::ConvertInto; -} +impl crate::historical::Trait for Test {} pub type System = frame_system::Module; pub type Session = Module; diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 4b499d5b4626a..bcd26d05aff52 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -245,23 +245,26 @@ sp_runtime::impl_opaque_keys! { pub other: OtherSessionHandler, } } + +impl sp_session::ValidatorIdentification for Test { + type ValidatorId = AccountId; + type ValidatorIdOf = crate::StashOf; + type FullIdentification = crate::Exposure; + type FullIdentificationOf = crate::ExposureOf; +} + impl pallet_session::Trait for Test { type SessionManager = pallet_session::historical::NoteHistoricalRoot; type Keys = SessionKeys; type ShouldEndSession = pallet_session::PeriodicSessions; type SessionHandler = (OtherSessionHandler,); type Event = MetaEvent; - type ValidatorId = AccountId; - type ValidatorIdOf = crate::StashOf; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; type WeightInfo = (); } -impl pallet_session::historical::Trait for Test { - type FullIdentification = crate::Exposure; - type FullIdentificationOf = crate::ExposureOf; -} +impl pallet_session::historical::Trait for Test {} impl pallet_authorship::Trait for Test { type FindAuthor = Author11; type UncleGenerations = UncleGenerations; @@ -739,7 +742,7 @@ pub(crate) fn validator_controllers() -> Vec { pub(crate) fn on_offence_in_era( offenders: &[OffenceDetails< AccountId, - pallet_session::historical::IdentificationTuple, + sp_session::IdentificationTuple, >], slash_fraction: &[Perbill], era: EraIndex, @@ -767,7 +770,7 @@ pub(crate) fn on_offence_in_era( } pub(crate) fn on_offence_now( - offenders: &[OffenceDetails>], + offenders: &[OffenceDetails>], slash_fraction: &[Perbill], ) { let now = Staking::active_era().unwrap().index; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index a568214f9a2e8..a335a6d8dfee1 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -4576,7 +4576,7 @@ fn offences_weight_calculated_correctly() { let n_offence_unapplied_weight = ::DbWeight::get().reads_writes(4, 1) + ::DbWeight::get().reads_writes(4, 5); - let offenders: Vec::AccountId, pallet_session::historical::IdentificationTuple>> + let offenders: Vec::AccountId, sp_session::IdentificationTuple<::AccountId, Test>>> = (1..10).map(|i| OffenceDetails { offender: (i, Staking::eras_stakers(Staking::active_era().unwrap().index, i)), From 8fded1264c936ed01f10112e5d2a950e69fc34b6 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 24 Sep 2020 20:07:51 +0800 Subject: [PATCH 07/33] Avoid using frame dep in primitives via pallet-session-common --- Cargo.lock | 11 +++++++++- Cargo.toml | 1 + bin/node/runtime/src/lib.rs | 8 +++---- frame/authority-discovery/src/lib.rs | 2 +- frame/babe/src/mock.rs | 4 ++-- frame/grandpa/src/mock.rs | 4 ++-- frame/im-online/Cargo.toml | 2 ++ frame/im-online/src/lib.rs | 2 +- frame/im-online/src/mock.rs | 4 ++-- frame/offences/benchmarking/src/lib.rs | 3 +-- frame/offences/benchmarking/src/mock.rs | 8 +++---- frame/session/Cargo.toml | 2 ++ frame/session/benchmarking/src/mock.rs | 2 +- frame/session/src/historical/mod.rs | 2 +- frame/session/src/historical/offchain.rs | 2 +- frame/session/src/historical/onchain.rs | 2 +- frame/session/src/lib.rs | 3 ++- frame/session/src/mock.rs | 2 +- frame/staking/fuzzer/src/mock.rs | 2 +- frame/staking/src/lib.rs | 4 ++-- frame/staking/src/mock.rs | 6 +++--- frame/staking/src/tests.rs | 2 +- primitives/session/Cargo.toml | 2 -- primitives/session/src/lib.rs | 27 ------------------------ 24 files changed, 46 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f52e86718ffc7..c8a754e037920 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4585,6 +4585,7 @@ dependencies = [ "frame-system", "pallet-authorship", "pallet-session", + "pallet-session-common", "parity-scale-codec", "serde", "sp-application-crypto", @@ -4800,6 +4801,7 @@ dependencies = [ "frame-system", "impl-trait-for-tuples", "lazy_static", + "pallet-session-common", "pallet-timestamp", "parity-scale-codec", "serde", @@ -4835,6 +4837,14 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-session-common" +version = "2.0.0" +dependencies = [ + "frame-support", + "sp-runtime", +] + [[package]] name = "pallet-society" version = "2.0.0" @@ -8333,7 +8343,6 @@ dependencies = [ name = "sp-session" version = "2.0.0" dependencies = [ - "frame-support", "parity-scale-codec", "sp-api", "sp-core", diff --git a/Cargo.toml b/Cargo.toml index 99b1d418a5153..6bb991577e43b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,6 +95,7 @@ members = [ "frame/scored-pool", "frame/session", "frame/session/benchmarking", + "frame/session/common", "frame/society", "frame/staking", "frame/staking/reward-curve", diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 31d0b882e1ce8..273556c0eea9b 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -403,7 +403,7 @@ parameter_types! { } -impl sp_session::ValidatorIdentification<::AccountId> for Runtime { +impl pallet_session::ValidatorIdentification<::AccountId> for Runtime { type ValidatorId = ::AccountId; type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; @@ -757,8 +757,8 @@ impl frame_system::offchain::SendTransactionTypes for Runtime where type OverarchingCall = Call; } -impl pallet_im_online::ValidatorSet<::AccountId>>::ValidatorId> for Runtime { - fn validators() -> Vec<::AccountId>>::ValidatorId> { +impl pallet_im_online::ValidatorSet<::AccountId>>::ValidatorId> for Runtime { + fn validators() -> Vec<::AccountId>>::ValidatorId> { Session::validators() } } @@ -786,7 +786,7 @@ parameter_types! { impl pallet_offences::Trait for Runtime { type Event = Event; - type IdentificationTuple = sp_session::IdentificationTuple<::AccountId, Self>; + type IdentificationTuple = pallet_session::IdentificationTuple<::AccountId, Self>; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 1a4016b7d01eb..e9f829697584b 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -113,7 +113,7 @@ mod tests { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } - impl sp_session::ValidatorIdentification for Test { + impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AuthorityId; type ValidatorIdOf = ConvertInto; type FullIdentification = (); diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index df0cb05673fac..3b76e4a789260 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -107,7 +107,7 @@ impl_opaque_keys! { } } -impl sp_session::ValidatorIdentification for Test { +impl pallet_session::ValidatorIdentification for Test { type ValidatorId = ::AccountId; type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; @@ -229,7 +229,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = (); - type IdentificationTuple = sp_session::IdentificationTuple; + type IdentificationTuple = pallet_session::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index 43c516f661224..07b6167a95ce1 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -121,7 +121,7 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } -impl sp_session::ValidatorIdentification for Test { +impl pallet_session::ValidatorIdentification for Test { type ValidatorId = u64; type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; @@ -244,7 +244,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = TestEvent; - type IdentificationTuple = sp_session::IdentificationTuple; + type IdentificationTuple = pallet_session::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 6e975ed4c7155..14dbf842644e3 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -26,6 +26,7 @@ sp-runtime = { version = "2.0.0", default-features = false, path = "../../primit sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } +pallet-session-common = { version = "2.0.0", default-features = false, path = "../session/common" } frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } @@ -45,5 +46,6 @@ std = [ "sp-staking/std", "frame-support/std", "frame-system/std", + "pallet-session-common/std", ] runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 0b248f66ab434..dfca32d6b89b5 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -79,7 +79,6 @@ use codec::{Encode, Decode}; use sp_core::offchain::OpaqueNetworkState; use sp_std::prelude::*; use sp_std::convert::TryInto; -use sp_session::{IdentificationTuple, ValidatorIdentification}; use sp_runtime::{ offchain::storage::StorageValueRef, RuntimeDebug, @@ -103,6 +102,7 @@ use frame_system::offchain::{ SendTransactionTypes, SubmitTransaction, }; +use pallet_session_common::{IdentificationTuple, ValidatorIdentification}; pub mod sr25519 { mod app_sr25519 { diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index f37390fc1fb03..a1119ed823f4f 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -28,7 +28,7 @@ use sp_runtime::testing::{Header, UintAuthorityId, TestXt}; use sp_runtime::traits::{IdentityLookup, BlakeTwo256, ConvertInto}; use sp_core::H256; use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types, weights::Weight}; -use sp_session::ValidatorIdentification; +use pallet_session::ValidatorIdentification; impl_outer_origin!{ pub enum Origin for Runtime {} @@ -147,7 +147,7 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } -impl sp_session::ValidatorIdentification for Runtime { +impl pallet_session::ValidatorIdentification for Runtime { type ValidatorId = u64; type ValidatorIdOf = ConvertInto; type FullIdentification = u64; diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index af51531d71477..43b56efc54c05 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -30,7 +30,6 @@ use frame_support::traits::{Currency, OnInitialize}; use sp_runtime::{Perbill, traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto}}; use sp_staking::offence::{ReportOffence, Offence, OffenceDetails}; -use sp_session::{IdentificationTuple, ValidatorIdentification}; use pallet_balances::{Trait as BalancesTrait}; use pallet_babe::BabeEquivocationOffence; @@ -38,7 +37,7 @@ use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot}; use pallet_im_online::{Trait as ImOnlineTrait, Module as ImOnline, UnresponsivenessOffence}; use pallet_offences::{Trait as OffencesTrait, Module as Offences}; use pallet_session::historical::{Trait as HistoricalTrait}; -use pallet_session::{Trait as SessionTrait, SessionManager}; +use pallet_session::{Trait as SessionTrait, SessionManager, IdentificationTuple, ValidatorIdentification}; use pallet_staking::{ Module as Staking, Trait as StakingTrait, RewardDestination, ValidatorPrefs, Exposure, IndividualExposure, ElectionStatus, MAX_NOMINATIONS, Event as StakingEvent diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 7b6c4960d5ccd..c452e9cbe34de 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -90,7 +90,7 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl sp_session::ValidatorIdentification for Test { +impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; @@ -187,8 +187,8 @@ impl pallet_staking::Trait for Test { type WeightInfo = (); } -impl pallet_im_online::ValidatorSet<>::ValidatorId> for Test { - fn validators() -> Vec<>::ValidatorId> { +impl pallet_im_online::ValidatorSet<>::ValidatorId> for Test { + fn validators() -> Vec<>::ValidatorId> { Session::validators() } } @@ -216,7 +216,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = Event; - type IdentificationTuple = sp_session::IdentificationTuple; + type IdentificationTuple = pallet_session::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index 8dfd59651a4f7..5862c76df9229 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -23,6 +23,7 @@ sp-session = { version = "2.0.0", default-features = false, path = "../../primit sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } +pallet-session-common = { version = "2.0.0", default-features = false, path = "./common" } pallet-timestamp = { version = "2.0.0", default-features = false, path = "../timestamp" } sp-trie = { version = "2.0.0", optional = true, default-features = false, path = "../../primitives/trie" } impl-trait-for-tuples = "0.1.3" @@ -45,6 +46,7 @@ std = [ "sp-runtime/std", "sp-session/std", "sp-staking/std", + "pallet-session-common/std", "pallet-timestamp/std", "sp-trie/std", ] diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index 2af2339db25fa..5418a2977d084 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -106,7 +106,7 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl sp_session::ValidatorIdentification for Test { +impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index e8be032fce37a..86a9d4c752d5d 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -37,7 +37,7 @@ use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX}; use sp_trie::trie_types::{TrieDBMut, TrieDB}; use super::{SessionIndex, Module as SessionModule}; -pub use sp_session::IdentificationTuple; +pub use pallet_session_common::IdentificationTuple; mod shared; pub mod offchain; diff --git a/frame/session/src/historical/offchain.rs b/frame/session/src/historical/offchain.rs index 1eecc52051d45..4b76d6356b58a 100644 --- a/frame/session/src/historical/offchain.rs +++ b/frame/session/src/historical/offchain.rs @@ -152,7 +152,7 @@ mod tests { }; use sp_runtime::testing::UintAuthorityId; - use sp_session::ValidatorIdentification; + use crate::ValidatorIdentification; type Historical = Module; diff --git a/frame/session/src/historical/onchain.rs b/frame/session/src/historical/onchain.rs index 82b9d3ef8d2ae..03136d2e3ff2f 100644 --- a/frame/session/src/historical/onchain.rs +++ b/frame/session/src/historical/onchain.rs @@ -26,7 +26,7 @@ use super::Trait as HistoricalTrait; use super::shared; use sp_std::prelude::*; -use sp_session::ValidatorIdentification; +use pallet_session_common::ValidatorIdentification; /// Store the validator-set associated to the `session_index` to the off-chain database. /// diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index f2fd9ec776bf5..63dd712bae3e9 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -102,7 +102,6 @@ use sp_std::{prelude::*, marker::PhantomData, ops::{Sub, Rem}}; use codec::Decode; -use sp_session::ValidatorIdentification; use sp_runtime::{KeyTypeId, Perbill, RuntimeAppPublic, BoundToRuntimeAppPublic}; use sp_runtime::traits::{Convert, Zero, Member, OpaqueKeys, Saturating}; use sp_staking::SessionIndex; @@ -116,6 +115,8 @@ use frame_support::{ }; use frame_system::ensure_signed; +pub use pallet_session_common::{IdentificationTuple, ValidatorIdentification}; + #[cfg(test)] mod mock; #[cfg(test)] diff --git a/frame/session/src/mock.rs b/frame/session/src/mock.rs index 700e8394cfc4c..d2b0e09d9bb08 100644 --- a/frame/session/src/mock.rs +++ b/frame/session/src/mock.rs @@ -211,7 +211,7 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } -impl sp_session::ValidatorIdentification for Test { +impl crate::ValidatorIdentification for Test { type ValidatorId = u64; type ValidatorIdOf = ConvertInto; type FullIdentification = u64; diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index 3d07d3401731f..b37f74bbdfa9b 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -114,7 +114,7 @@ impl pallet_timestamp::Trait for Test { impl pallet_session::historical::Trait for Test { } -impl sp_session::ValidatorIdentification<::AccountId> for Test { +impl pallet_session::ValidatorIdentification<::AccountId> for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; type FullIdentification = pallet_staking::Exposure; diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 18114724a6d3e..c6f2dc32ad9ac 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -3175,7 +3175,7 @@ impl Convert> /// This is intended to be used with `FilterHistoricalOffences`. impl - OnOffenceHandler::AccountId, T>, Weight> + OnOffenceHandler::AccountId, T>, Weight> for Module where T: pallet_session::Trait::AccountId>, T: pallet_session::historical::Trait< @@ -3190,7 +3190,7 @@ for Module where >, { fn on_offence( - offenders: &[OffenceDetails::AccountId, T>>], + offenders: &[OffenceDetails::AccountId, T>>], slash_fraction: &[Perbill], slash_session: SessionIndex, ) -> Result { diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index bcd26d05aff52..7f142cec8b585 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -246,7 +246,7 @@ sp_runtime::impl_opaque_keys! { } } -impl sp_session::ValidatorIdentification for Test { +impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AccountId; type ValidatorIdOf = crate::StashOf; type FullIdentification = crate::Exposure; @@ -742,7 +742,7 @@ pub(crate) fn validator_controllers() -> Vec { pub(crate) fn on_offence_in_era( offenders: &[OffenceDetails< AccountId, - sp_session::IdentificationTuple, + pallet_session::IdentificationTuple, >], slash_fraction: &[Perbill], era: EraIndex, @@ -770,7 +770,7 @@ pub(crate) fn on_offence_in_era( } pub(crate) fn on_offence_now( - offenders: &[OffenceDetails>], + offenders: &[OffenceDetails>], slash_fraction: &[Perbill], ) { let now = Staking::active_era().unwrap().index; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index a335a6d8dfee1..cbe1ff6cb8ae8 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -4576,7 +4576,7 @@ fn offences_weight_calculated_correctly() { let n_offence_unapplied_weight = ::DbWeight::get().reads_writes(4, 1) + ::DbWeight::get().reads_writes(4, 5); - let offenders: Vec::AccountId, sp_session::IdentificationTuple<::AccountId, Test>>> + let offenders: Vec::AccountId, pallet_session::IdentificationTuple<::AccountId, Test>>> = (1..10).map(|i| OffenceDetails { offender: (i, Staking::eras_stakers(Staking::active_era().unwrap().index, i)), diff --git a/primitives/session/Cargo.toml b/primitives/session/Cargo.toml index 10aa4fdfcfcde..71e6184c7e100 100644 --- a/primitives/session/Cargo.toml +++ b/primitives/session/Cargo.toml @@ -19,7 +19,6 @@ sp-core = { version = "2.0.0", default-features = false, path = "../core" } sp-std = { version = "2.0.0", default-features = false, path = "../std" } sp-staking = { version = "2.0.0", default-features = false, path = "../staking" } sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../../frame/support" } [features] default = [ "std" ] @@ -30,5 +29,4 @@ std = [ "sp-std/std", "sp-staking/std", "sp-runtime/std", - "frame-support/std", ] diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index c219c78265c44..38a852dafd1dd 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -29,8 +29,6 @@ use sp_api::ProvideRuntimeApi; use sp_core::RuntimeDebug; use sp_core::crypto::KeyTypeId; use sp_staking::SessionIndex; -use sp_runtime::traits::{Member, Convert}; -use frame_support::Parameter; use sp_std::vec::Vec; sp_api::decl_runtime_apis! { @@ -55,31 +53,6 @@ sp_api::decl_runtime_apis! { /// Number of validators in a given session. pub type ValidatorCount = u32; -/// A tuple of the validator's ID and their full identification.:w -pub type IdentificationTuple = (>::ValidatorId, >::FullIdentification); - -pub trait ValidatorIdentification { - /// A stable ID for a validator. - type ValidatorId: Member + Parameter; - - /// A conversion from account ID to validator ID. - /// - /// Its cost must be at most one storage read. - type ValidatorIdOf: Convert>; - - /// Full identification of the validator. - type FullIdentification: Parameter; - - /// A conversion from validator ID to full identification. - /// - /// This should contain any references to economic actors associated with the - /// validator, since they may be outdated by the time this is queried from a - /// historical trie. - /// - /// It must return the identification for the current session index. - type FullIdentificationOf: Convert>; -} - /// Proof of membership of a specific key in a given session. #[derive(Encode, Decode, Clone, Eq, PartialEq, Default, RuntimeDebug)] pub struct MembershipProof { From db2d1a4c918331384c1fb7b5e4289c096a98e2a2 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 24 Sep 2020 20:52:49 +0800 Subject: [PATCH 08/33] Merge ValidatorSet into SessionInterface trait Wrap a few too long lines Add some docs --- Cargo.lock | 5 -- bin/node/runtime/src/lib.rs | 18 +------ frame/authority-discovery/Cargo.toml | 1 - frame/im-online/Cargo.toml | 3 +- frame/im-online/src/lib.rs | 68 ++++++++++++++++++------- frame/im-online/src/mock.rs | 16 +----- frame/offences/benchmarking/Cargo.toml | 2 - frame/offences/benchmarking/src/lib.rs | 21 +++++--- frame/offences/benchmarking/src/mock.rs | 13 ----- frame/session/Cargo.toml | 1 - frame/session/common/Cargo.toml | 24 +++++++++ frame/session/common/src/lib.rs | 49 ++++++++++++++++++ frame/session/src/historical/mod.rs | 7 +-- frame/staking/Cargo.toml | 2 - frame/staking/fuzzer/Cargo.toml | 1 - frame/staking/fuzzer/src/mock.rs | 3 +- frame/staking/src/lib.rs | 11 +++- frame/staking/src/tests.rs | 5 +- primitives/session/Cargo.toml | 2 +- 19 files changed, 158 insertions(+), 94 deletions(-) create mode 100644 frame/session/common/Cargo.toml create mode 100644 frame/session/common/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c8a754e037920..dcc1e678e6bb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4255,7 +4255,6 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", - "sp-session", "sp-staking", "sp-std", ] @@ -4592,7 +4591,6 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", - "sp-session", "sp-staking", "sp-std", ] @@ -4710,7 +4708,6 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", - "sp-session", "sp-staking", "sp-std", ] @@ -4883,7 +4880,6 @@ dependencies = [ "sp-io", "sp-npos-elections", "sp-runtime", - "sp-session", "sp-staking", "sp-std", "sp-storage", @@ -4910,7 +4906,6 @@ dependencies = [ "sp-io", "sp-npos-elections", "sp-runtime", - "sp-session", "sp-std", ] diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 273556c0eea9b..c6ea1c39b7490 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -421,8 +421,7 @@ impl pallet_session::Trait for Runtime { type WeightInfo = weights::pallet_session::WeightInfo; } -impl pallet_session::historical::Trait for Runtime { -} +impl pallet_session::historical::Trait for Runtime {} pallet_staking_reward_curve::build! { const REWARD_CURVE: PiecewiseLinear<'static> = curve!( @@ -757,23 +756,10 @@ impl frame_system::offchain::SendTransactionTypes for Runtime where type OverarchingCall = Call; } -impl pallet_im_online::ValidatorSet<::AccountId>>::ValidatorId> for Runtime { - fn validators() -> Vec<::AccountId>>::ValidatorId> { - Session::validators() - } -} - -impl pallet_im_online::SessionInterface for Runtime { - fn current_index() -> sp_staking::SessionIndex { - Session::current_index() - } -} - impl pallet_im_online::Trait for Runtime { type AuthorityId = ImOnlineId; type Event = Event; type SessionInterface = Self; - type ValidatorSet = Self; type SessionDuration = SessionDuration; type ReportUnresponsiveness = Offences; type UnsignedPriority = ImOnlineUnsignedPriority; @@ -786,7 +772,7 @@ parameter_types! { impl pallet_offences::Trait for Runtime { type Event = Event; - type IdentificationTuple = pallet_session::IdentificationTuple<::AccountId, Self>; + type IdentificationTuple = pallet_session::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/authority-discovery/Cargo.toml b/frame/authority-discovery/Cargo.toml index 2b53d196a4ca8..0e1db74632786 100644 --- a/frame/authority-discovery/Cargo.toml +++ b/frame/authority-discovery/Cargo.toml @@ -26,7 +26,6 @@ frame-system = { version = "2.0.0", default-features = false, path = "../system" [dev-dependencies] sp-core = { version = "2.0.0", path = "../../primitives/core" } sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-session = { version = "2.0.0", path = "../../primitives/session" } sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } [features] diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 14dbf842644e3..cf28872d06af1 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -21,7 +21,6 @@ sp-std = { version = "2.0.0", default-features = false, path = "../../primitives serde = { version = "1.0.101", optional = true } pallet-session = { version = "2.0.0", default-features = false, path = "../session" } sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } @@ -41,11 +40,11 @@ std = [ "serde", "pallet-session/std", "sp-io/std", - "sp-session/std", "sp-runtime/std", "sp-staking/std", "frame-support/std", "frame-system/std", + "pallet-session/std", "pallet-session-common/std", ] runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index dfca32d6b89b5..8a193441cf664 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -231,17 +231,39 @@ pub trait WeightInfo { fn validate_unsigned_and_then_heartbeat(k: u32, e: u32, ) -> Weight; } -/// The set of validators that are considered to be running an authority node. -pub trait ValidatorSet { - /// Returns all the validators ought to be online. +/// Trait for retrieving the session info needed for online node inspection. +/// +/// This trait is used for decouple the pallet-session dependency from im-online +/// module so that the user of im-online & offences modules can pass any list of +/// validators that are considered to be online in each session, particularly useful +/// for the Substrate-based projects having their own staking implementation +/// instead of using pallet-staking directly. +pub trait SessionInterface { + /// Returns current session index. + fn current_index() -> SessionIndex; + + /// Returns all the validators ought to be online in a session. + /// + /// The returned validators are all expected to be running an authority node. fn validators() -> Vec; } -pub trait SessionInterface { - fn current_index() -> SessionIndex; +impl + SessionInterface<::AccountId>>::ValidatorId> for T +{ + fn current_index() -> sp_staking::SessionIndex { + pallet_session::Module::::current_index() + } + fn validators() -> Vec<::AccountId>>::ValidatorId> { + pallet_session::Module::::validators() + } } -pub trait Trait: SendTransactionTypes> + ValidatorIdentification<::AccountId> + frame_system::Trait { +pub trait Trait: + SendTransactionTypes> + + ValidatorIdentification<::AccountId> + + frame_system::Trait +{ /// The identifier type for an authority. type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + Ord; @@ -256,10 +278,11 @@ pub trait Trait: SendTransactionTypes> + ValidatorIdentification<; - type SessionInterface: SessionInterface; - - /// A set of validators expected to be online. - type ValidatorSet: ValidatorSet<::AccountId>>::ValidatorId>; + /// A type for retrieving the necessary session info. + /// + /// This is currently used to get the session index and list of validators expected to be online. + type SessionInterface: + SessionInterface<::AccountId>>::ValidatorId>; /// A type that gives us the ability to submit unresponsiveness offence reports. type ReportUnresponsiveness: @@ -312,10 +335,11 @@ decl_storage! { double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) AuthIndex => Option>; - /// For each session index, we keep a mapping of `::ValidatorId` to the - /// number of blocks authored by the given authority. + /// For each session index, we keep a mapping of `>::ValidatorId` + /// to the number of blocks authored by the given authority. AuthoredBlocks get(fn authored_blocks): - double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) >::ValidatorId + double_map hasher(twox_64_concat) SessionIndex, + hasher(twox_64_concat) >::ValidatorId => u32; } add_extra_genesis { @@ -416,7 +440,12 @@ type OffchainResult = Result::Bl /// Keep track of number of authored blocks per authority, uncles are counted as /// well since they're a valid proof of being online. -impl pallet_authorship::EventHandler<>::ValidatorId, T::BlockNumber> for Module { +impl + pallet_authorship::EventHandler< + >::ValidatorId, + T::BlockNumber, + > for Module +{ fn note_author(author: >::ValidatorId) { Self::note_authorship(author); } @@ -432,7 +461,7 @@ impl Module { /// authored at least one block, during the current session. Otherwise /// `false`. pub fn is_online(authority_index: AuthIndex) -> bool { - let current_validators = T::ValidatorSet::validators(); + let current_validators = T::SessionInterface::validators(); if authority_index >= current_validators.len() as u32 { return false; @@ -443,7 +472,10 @@ impl Module { Self::is_online_aux(authority_index, authority) } - fn is_online_aux(authority_index: AuthIndex, authority: &>::ValidatorId) -> bool { + fn is_online_aux( + authority_index: AuthIndex, + authority: &>::ValidatorId, + ) -> bool { let current_session = T::SessionInterface::current_index(); ::contains_key(¤t_session, &authority_index) || @@ -480,7 +512,7 @@ impl Module { } let session_index = T::SessionInterface::current_index(); - let validators_len = T::ValidatorSet::validators().len() as u32; + let validators_len = T::SessionInterface::validators().len() as u32; Ok(Self::local_authority_keys() .map(move |(authority_index, key)| @@ -660,7 +692,7 @@ impl pallet_session::OneSessionHandler for Module { fn on_before_session_ending() { let session_index = T::SessionInterface::current_index(); let keys = Keys::::get(); - let current_validators = T::ValidatorSet::validators(); + let current_validators = T::SessionInterface::validators(); let offenders = current_validators.into_iter().enumerate() .filter(|(index, id)| diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index a1119ed823f4f..6f06fa269194f 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -21,14 +21,13 @@ use std::cell::RefCell; -use crate::{Module, Trait, ValidatorSet}; +use crate::{Module, Trait}; use sp_runtime::Perbill; use sp_staking::{SessionIndex, offence::{ReportOffence, OffenceError}}; use sp_runtime::testing::{Header, UintAuthorityId, TestXt}; use sp_runtime::traits::{IdentityLookup, BlakeTwo256, ConvertInto}; use sp_core::H256; use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types, weights::Weight}; -use pallet_session::ValidatorIdentification; impl_outer_origin!{ pub enum Origin for Runtime {} @@ -182,23 +181,10 @@ parameter_types! { pub const UnsignedPriority: u64 = 1 << 20; } -impl ValidatorSet<>::ValidatorId> for Runtime { - fn validators() -> Vec<>::ValidatorId> { - Session::validators() - } -} - -impl crate::SessionInterface for Runtime { - fn current_index() -> SessionIndex { - Session::current_index() - } -} - impl Trait for Runtime { type AuthorityId = UintAuthorityId; type Event = (); type ReportUnresponsiveness = OffenceHandler; - type ValidatorSet = Self; type SessionInterface = Self; type SessionDuration = Period; type UnsignedPriority = UnsignedPriority; diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml index 19ddaabd917d7..7a95cebc4fb21 100644 --- a/frame/offences/benchmarking/Cargo.toml +++ b/frame/offences/benchmarking/Cargo.toml @@ -26,7 +26,6 @@ pallet-session = { version = "2.0.0", default-features = false, path = "../../se pallet-staking = { version = "2.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../../primitives/staking" } -sp-session = { version = "2.0.0", default-features = false, path = "../../../primitives/session" } sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } [dev-dependencies] @@ -51,7 +50,6 @@ std = [ "pallet-staking/std", "sp-runtime/std", "sp-staking/std", - "sp-session/std", "sp-std/std", "codec/std", ] diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 43b56efc54c05..6f245cff7f61c 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -66,13 +66,17 @@ pub trait Trait: /// and the one required by offences. pub trait IdTupleConvert { /// Convert identification tuple from `historical` trait to the one expected by `offences`. - fn convert(id: IdentificationTuple<::AccountId, T>) -> ::IdentificationTuple; + fn convert( + id: IdentificationTuple<::AccountId, T>, + ) -> ::IdentificationTuple; } impl IdTupleConvert for T where ::IdentificationTuple: From::AccountId, T>> { - fn convert(id: IdentificationTuple<::AccountId, T>) -> ::IdentificationTuple { + fn convert( + id: IdentificationTuple<::AccountId, T>, + ) -> ::IdentificationTuple { id.into() } } @@ -163,12 +167,15 @@ fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< Staking::::start_session(0); + let validator_id_of = + ::AccountId>>::ValidatorIdOf::convert; + let full_identification_of = + ::AccountId>>::FullIdentificationOf::convert; + let id_tuples = offenders.iter() - .map(|offender| - ::AccountId>>::ValidatorIdOf::convert(offender.controller.clone()) - .expect("failed to get validator id from account id")) - .map(|validator_id| - ::AccountId>>::FullIdentificationOf::convert(validator_id.clone()) + .map(|offender| validator_id_of(offender.controller.clone()) + .expect("failed to get validator id from account id")) + .map(|validator_id| full_identification_of(validator_id.clone()) .map(|full_id| (validator_id, full_id)) .expect("failed to convert validator id to full identification")) .collect::::AccountId, T>>>(); diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index c452e9cbe34de..3c8f076b353ac 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -187,22 +187,9 @@ impl pallet_staking::Trait for Test { type WeightInfo = (); } -impl pallet_im_online::ValidatorSet<>::ValidatorId> for Test { - fn validators() -> Vec<>::ValidatorId> { - Session::validators() - } -} - -impl pallet_im_online::SessionInterface for Test { - fn current_index() -> sp_staking::SessionIndex { - Session::current_index() - } -} - impl pallet_im_online::Trait for Test { type AuthorityId = UintAuthorityId; type Event = Event; - type ValidatorSet = Self; type SessionInterface = Self; type SessionDuration = Period; type ReportUnresponsiveness = Offences; diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index 5862c76df9229..0378e2b5c89b5 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -30,7 +30,6 @@ impl-trait-for-tuples = "0.1.3" [dev-dependencies] sp-application-crypto = { version = "2.0.0", path = "../../primitives/application-crypto" } -sp-session = { version = "2.0.0", path = "../../primitives/session" } lazy_static = "1.4.0" [features] diff --git a/frame/session/common/Cargo.toml b/frame/session/common/Cargo.toml new file mode 100644 index 0000000000000..53dd0a8ca2efd --- /dev/null +++ b/frame/session/common/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "pallet-session-common" +version = "2.0.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "FRAME sessions common pallet" +readme = "README.md" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } +frame-support = { version = "2.0.0", default-features = false, path = "../../support" } + +[features] +default = ["std"] +std = [ + "sp-runtime/std", + "frame-support/std", +] \ No newline at end of file diff --git a/frame/session/common/src/lib.rs b/frame/session/common/src/lib.rs new file mode 100644 index 0000000000000..c0f8956e4bede --- /dev/null +++ b/frame/session/common/src/lib.rs @@ -0,0 +1,49 @@ +// This file is part of Substrate. + +// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![cfg_attr(not(feature = "std"), no_std)] + +use sp_runtime::traits::{Member, Convert}; +use frame_support::Parameter; + +pub trait ValidatorIdentification { + /// A stable ID for a validator. + type ValidatorId: Member + Parameter; + + /// A conversion from account ID to validator ID. + /// + /// Its cost must be at most one storage read. + type ValidatorIdOf: Convert>; + + /// Full identification of the validator. + type FullIdentification: Parameter; + + /// A conversion from validator ID to full identification. + /// + /// This should contain any references to economic actors associated with the + /// validator, since they may be outdated by the time this is queried from a + /// historical trie. + /// + /// It must return the identification for the current session index. + type FullIdentificationOf: Convert>; +} + +/// A tuple of the validator's ID and their full identification. +pub type IdentificationTuple = ( + >::ValidatorId, + >::FullIdentification +); diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index 86a9d4c752d5d..5c2b1303a43d1 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -35,17 +35,14 @@ use frame_support::{decl_module, decl_storage}; use frame_support::print; use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX}; use sp_trie::trie_types::{TrieDBMut, TrieDB}; -use super::{SessionIndex, Module as SessionModule}; - -pub use pallet_session_common::IdentificationTuple; +use super::{SessionIndex, Module as SessionModule, IdentificationTuple}; mod shared; pub mod offchain; pub mod onchain; /// Trait necessary for the historical module. -pub trait Trait: super::Trait { -} +pub trait Trait: super::Trait {} decl_storage! { trait Store for Module as Session { diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index 4c8cbf0b4dd10..88b8c1270a4e1 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -21,7 +21,6 @@ sp-npos-elections = { version = "2.0.0", default-features = false, path = "../.. sp-io ={ version = "2.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } pallet-session = { version = "2.0.0", default-features = false, features = ["historical"], path = "../session" } @@ -56,7 +55,6 @@ std = [ "frame-support/std", "sp-runtime/std", "sp-staking/std", - "sp-session/std", "pallet-session/std", "frame-system/std", "pallet-authorship/std", diff --git a/frame/staking/fuzzer/Cargo.toml b/frame/staking/fuzzer/Cargo.toml index 5b758f7e7c2a1..e1431aa54d4a7 100644 --- a/frame/staking/fuzzer/Cargo.toml +++ b/frame/staking/fuzzer/Cargo.toml @@ -28,7 +28,6 @@ sp-io ={ version = "2.0.0", path = "../../../primitives/io" } sp-core = { version = "2.0.0", path = "../../../primitives/core" } sp-npos-elections = { version = "2.0.0", path = "../../../primitives/npos-elections" } sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-session = { version = "2.0.0", path = "../../../primitives/session" } [[bin]] name = "submit_solution" diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index b37f74bbdfa9b..b24d06f9b741b 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -111,8 +111,7 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl pallet_session::historical::Trait for Test { -} +impl pallet_session::historical::Trait for Test {} impl pallet_session::ValidatorIdentification<::AccountId> for Test { type ValidatorId = AccountId; diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index c6f2dc32ad9ac..46af630dcbdc2 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -3175,7 +3175,11 @@ impl Convert> /// This is intended to be used with `FilterHistoricalOffences`. impl - OnOffenceHandler::AccountId, T>, Weight> + OnOffenceHandler< + T::AccountId, + pallet_session::IdentificationTuple<::AccountId, T>, + Weight, + > for Module where T: pallet_session::Trait::AccountId>, T: pallet_session::historical::Trait< @@ -3190,7 +3194,10 @@ for Module where >, { fn on_offence( - offenders: &[OffenceDetails::AccountId, T>>], + offenders: &[OffenceDetails< + T::AccountId, + pallet_session::IdentificationTuple<::AccountId, T>> + ], slash_fraction: &[Perbill], slash_session: SessionIndex, ) -> Result { diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index cbe1ff6cb8ae8..840d5bdf8bd25 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -4576,7 +4576,10 @@ fn offences_weight_calculated_correctly() { let n_offence_unapplied_weight = ::DbWeight::get().reads_writes(4, 1) + ::DbWeight::get().reads_writes(4, 5); - let offenders: Vec::AccountId, pallet_session::IdentificationTuple<::AccountId, Test>>> + let offenders: Vec::AccountId, + pallet_session::IdentificationTuple<::AccountId, Test>, + >> = (1..10).map(|i| OffenceDetails { offender: (i, Staking::eras_stakers(Staking::active_era().unwrap().index, i)), diff --git a/primitives/session/Cargo.toml b/primitives/session/Cargo.toml index 71e6184c7e100..4fccce6283142 100644 --- a/primitives/session/Cargo.toml +++ b/primitives/session/Cargo.toml @@ -18,7 +18,7 @@ sp-api = { version = "2.0.0", default-features = false, path = "../api" } sp-core = { version = "2.0.0", default-features = false, path = "../core" } sp-std = { version = "2.0.0", default-features = false, path = "../std" } sp-staking = { version = "2.0.0", default-features = false, path = "../staking" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } +sp-runtime = { version = "2.0.0", optional = true, path = "../runtime" } [features] default = [ "std" ] From 9f1344d4504cfd1eb0e515240195a143c9a70e6c Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 25 Sep 2020 17:37:45 +0800 Subject: [PATCH 09/33] Move pallet-sesion-common into pallet-session --- Cargo.lock | 10 ----- Cargo.toml | 1 - frame/im-online/Cargo.toml | 3 -- frame/im-online/src/lib.rs | 2 +- frame/session/Cargo.toml | 2 - frame/session/common/Cargo.toml | 24 ------------ frame/session/common/src/lib.rs | 49 ------------------------- frame/session/src/historical/onchain.rs | 3 +- frame/session/src/lib.rs | 30 ++++++++++++++- 9 files changed, 30 insertions(+), 94 deletions(-) delete mode 100644 frame/session/common/Cargo.toml delete mode 100644 frame/session/common/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index dcc1e678e6bb5..1bf8b6a11f58b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4584,7 +4584,6 @@ dependencies = [ "frame-system", "pallet-authorship", "pallet-session", - "pallet-session-common", "parity-scale-codec", "serde", "sp-application-crypto", @@ -4798,7 +4797,6 @@ dependencies = [ "frame-system", "impl-trait-for-tuples", "lazy_static", - "pallet-session-common", "pallet-timestamp", "parity-scale-codec", "serde", @@ -4834,14 +4832,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "pallet-session-common" -version = "2.0.0" -dependencies = [ - "frame-support", - "sp-runtime", -] - [[package]] name = "pallet-society" version = "2.0.0" diff --git a/Cargo.toml b/Cargo.toml index 6bb991577e43b..99b1d418a5153 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,7 +95,6 @@ members = [ "frame/scored-pool", "frame/session", "frame/session/benchmarking", - "frame/session/common", "frame/society", "frame/staking", "frame/staking/reward-curve", diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index cf28872d06af1..ef22d67688732 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -25,7 +25,6 @@ sp-runtime = { version = "2.0.0", default-features = false, path = "../../primit sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-session-common = { version = "2.0.0", default-features = false, path = "../session/common" } frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } @@ -44,7 +43,5 @@ std = [ "sp-staking/std", "frame-support/std", "frame-system/std", - "pallet-session/std", - "pallet-session-common/std", ] runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 8a193441cf664..eb0e27a2ab54d 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -102,7 +102,7 @@ use frame_system::offchain::{ SendTransactionTypes, SubmitTransaction, }; -use pallet_session_common::{IdentificationTuple, ValidatorIdentification}; +use pallet_session::{IdentificationTuple, ValidatorIdentification}; pub mod sr25519 { mod app_sr25519 { diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index 0378e2b5c89b5..ea3a3d3cdf7fa 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -23,7 +23,6 @@ sp-session = { version = "2.0.0", default-features = false, path = "../../primit sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-session-common = { version = "2.0.0", default-features = false, path = "./common" } pallet-timestamp = { version = "2.0.0", default-features = false, path = "../timestamp" } sp-trie = { version = "2.0.0", optional = true, default-features = false, path = "../../primitives/trie" } impl-trait-for-tuples = "0.1.3" @@ -45,7 +44,6 @@ std = [ "sp-runtime/std", "sp-session/std", "sp-staking/std", - "pallet-session-common/std", "pallet-timestamp/std", "sp-trie/std", ] diff --git a/frame/session/common/Cargo.toml b/frame/session/common/Cargo.toml deleted file mode 100644 index 53dd0a8ca2efd..0000000000000 --- a/frame/session/common/Cargo.toml +++ /dev/null @@ -1,24 +0,0 @@ -[package] -name = "pallet-session-common" -version = "2.0.0" -authors = ["Parity Technologies "] -edition = "2018" -license = "Apache-2.0" -homepage = "https://substrate.dev" -repository = "https://github.com/paritytech/substrate/" -description = "FRAME sessions common pallet" -readme = "README.md" - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] - -[dependencies] -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../../support" } - -[features] -default = ["std"] -std = [ - "sp-runtime/std", - "frame-support/std", -] \ No newline at end of file diff --git a/frame/session/common/src/lib.rs b/frame/session/common/src/lib.rs deleted file mode 100644 index c0f8956e4bede..0000000000000 --- a/frame/session/common/src/lib.rs +++ /dev/null @@ -1,49 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#![cfg_attr(not(feature = "std"), no_std)] - -use sp_runtime::traits::{Member, Convert}; -use frame_support::Parameter; - -pub trait ValidatorIdentification { - /// A stable ID for a validator. - type ValidatorId: Member + Parameter; - - /// A conversion from account ID to validator ID. - /// - /// Its cost must be at most one storage read. - type ValidatorIdOf: Convert>; - - /// Full identification of the validator. - type FullIdentification: Parameter; - - /// A conversion from validator ID to full identification. - /// - /// This should contain any references to economic actors associated with the - /// validator, since they may be outdated by the time this is queried from a - /// historical trie. - /// - /// It must return the identification for the current session index. - type FullIdentificationOf: Convert>; -} - -/// A tuple of the validator's ID and their full identification. -pub type IdentificationTuple = ( - >::ValidatorId, - >::FullIdentification -); diff --git a/frame/session/src/historical/onchain.rs b/frame/session/src/historical/onchain.rs index 03136d2e3ff2f..0a4038b4c3921 100644 --- a/frame/session/src/historical/onchain.rs +++ b/frame/session/src/historical/onchain.rs @@ -21,12 +21,11 @@ use codec::Encode; use sp_runtime::traits::Convert; use super::super::Trait as SessionTrait; -use super::super::{Module as SessionModule, SessionIndex}; +use super::super::{Module as SessionModule, SessionIndex, ValidatorIdentification}; use super::Trait as HistoricalTrait; use super::shared; use sp_std::prelude::*; -use pallet_session_common::ValidatorIdentification; /// Store the validator-set associated to the `session_index` to the off-chain database. /// diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 63dd712bae3e9..189f01f636d32 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -115,8 +115,6 @@ use frame_support::{ }; use frame_system::ensure_signed; -pub use pallet_session_common::{IdentificationTuple, ValidatorIdentification}; - #[cfg(test)] mod mock; #[cfg(test)] @@ -127,6 +125,34 @@ pub mod historical; mod default_weights; +pub trait ValidatorIdentification { + /// A stable ID for a validator. + type ValidatorId: Member + Parameter; + + /// A conversion from account ID to validator ID. + /// + /// Its cost must be at most one storage read. + type ValidatorIdOf: Convert>; + + /// Full identification of the validator. + type FullIdentification: Parameter; + + /// A conversion from validator ID to full identification. + /// + /// This should contain any references to economic actors associated with the + /// validator, since they may be outdated by the time this is queried from a + /// historical trie. + /// + /// It must return the identification for the current session index. + type FullIdentificationOf: Convert>; +} + +/// A tuple of the validator's ID and their full identification. +pub type IdentificationTuple = ( + >::ValidatorId, + >::FullIdentification +); + /// Decides whether the session should be ended. pub trait ShouldEndSession { /// Return `true` if the session should be ended. From d5a3b4ff0f23e83b7f8342d1144f42762027b9e2 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 25 Sep 2020 22:15:16 +0800 Subject: [PATCH 10/33] Move SessionInterface to sp-session and impl it in session pallet Ref https://github.com/paritytech/substrate/pull/7127#discussion_r494892472 --- Cargo.lock | 1 + bin/node/runtime/src/lib.rs | 2 +- frame/im-online/Cargo.toml | 2 ++ frame/im-online/src/lib.rs | 29 +------------------------ frame/im-online/src/mock.rs | 2 +- frame/offences/benchmarking/src/mock.rs | 2 +- frame/session/src/lib.rs | 13 +++++++++++ primitives/session/src/lib.rs | 17 +++++++++++++++ 8 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bf8b6a11f58b..0a60c94c93849 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4590,6 +4590,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-session", "sp-staking", "sp-std", ] diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index c6ea1c39b7490..097327c4f77ec 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -759,7 +759,7 @@ impl frame_system::offchain::SendTransactionTypes for Runtime where impl pallet_im_online::Trait for Runtime { type AuthorityId = ImOnlineId; type Event = Event; - type SessionInterface = Self; + type SessionInterface = Session; type SessionDuration = SessionDuration; type ReportUnresponsiveness = Offences; type UnsignedPriority = ImOnlineUnsignedPriority; diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index ef22d67688732..226c03422d1c3 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -23,6 +23,7 @@ pallet-session = { version = "2.0.0", default-features = false, path = "../sessi sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } +sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } @@ -41,6 +42,7 @@ std = [ "sp-io/std", "sp-runtime/std", "sp-staking/std", + "sp-session/std", "frame-support/std", "frame-system/std", ] diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index eb0e27a2ab54d..54dea77ec7bcd 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -92,6 +92,7 @@ use sp_staking::{ SessionIndex, offence::{ReportOffence, Offence, Kind}, }; +use sp_session::SessionInterface; use frame_support::{ decl_module, decl_event, decl_storage, Parameter, debug, decl_error, traits::Get, @@ -231,34 +232,6 @@ pub trait WeightInfo { fn validate_unsigned_and_then_heartbeat(k: u32, e: u32, ) -> Weight; } -/// Trait for retrieving the session info needed for online node inspection. -/// -/// This trait is used for decouple the pallet-session dependency from im-online -/// module so that the user of im-online & offences modules can pass any list of -/// validators that are considered to be online in each session, particularly useful -/// for the Substrate-based projects having their own staking implementation -/// instead of using pallet-staking directly. -pub trait SessionInterface { - /// Returns current session index. - fn current_index() -> SessionIndex; - - /// Returns all the validators ought to be online in a session. - /// - /// The returned validators are all expected to be running an authority node. - fn validators() -> Vec; -} - -impl - SessionInterface<::AccountId>>::ValidatorId> for T -{ - fn current_index() -> sp_staking::SessionIndex { - pallet_session::Module::::current_index() - } - fn validators() -> Vec<::AccountId>>::ValidatorId> { - pallet_session::Module::::validators() - } -} - pub trait Trait: SendTransactionTypes> + ValidatorIdentification<::AccountId> diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 6f06fa269194f..0fcb13d5e6ec5 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -185,7 +185,7 @@ impl Trait for Runtime { type AuthorityId = UintAuthorityId; type Event = (); type ReportUnresponsiveness = OffenceHandler; - type SessionInterface = Self; + type SessionInterface = Session; type SessionDuration = Period; type UnsignedPriority = UnsignedPriority; type WeightInfo = (); diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 3c8f076b353ac..618d1411a921c 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -190,7 +190,7 @@ impl pallet_staking::Trait for Test { impl pallet_im_online::Trait for Test { type AuthorityId = UintAuthorityId; type Event = Event; - type SessionInterface = Self; + type SessionInterface = Session; type SessionDuration = Period; type ReportUnresponsiveness = Offences; type UnsignedPriority = (); diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 189f01f636d32..9fc213494da87 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -826,3 +826,16 @@ impl EstimateNextNewSession for Module { T::NextSessionRotation::weight(now) } } + +impl + sp_session::SessionInterface< + ::AccountId>>::ValidatorId + > for Module +{ + fn current_index() -> sp_staking::SessionIndex { + Module::::current_index() + } + fn validators() -> Vec<::AccountId>>::ValidatorId> { + Module::::validators() + } +} diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index 38a852dafd1dd..eac841270f7ad 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -106,6 +106,23 @@ impl GetValidatorCount for MembershipProof { } } +/// Trait for retrieving the session info needed for online node inspection. +/// +/// This trait is used for decouple the pallet-session dependency from im-online +/// module so that the user of im-online & offences modules can pass any list of +/// validators that are considered to be online in each session, particularly useful +/// for the Substrate-based projects having their own staking implementation +/// instead of using pallet-staking directly. +pub trait SessionInterface { + /// Returns current session index. + fn current_index() -> SessionIndex; + + /// Returns all the validators ought to be online in a session. + /// + /// The returned validators are all expected to be running an authority node. + fn validators() -> Vec; +} + /// Generate the initial session keys with the given seeds, at the given block and store them in /// the client's keystore. #[cfg(feature = "std")] From 004bde7857ec5881dc6b1156069d86f513c13add Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sat, 26 Sep 2020 20:30:01 +0800 Subject: [PATCH 11/33] Split put historical::FullValidatorIdentification trait --- bin/node/runtime/src/lib.rs | 7 +++--- frame/authority-discovery/src/lib.rs | 4 ++-- frame/babe/src/mock.rs | 6 ++--- frame/grandpa/src/mock.rs | 6 ++--- frame/im-online/src/lib.rs | 9 +++++--- frame/im-online/src/mock.rs | 4 ++-- frame/offences/benchmarking/src/lib.rs | 7 ++++-- frame/offences/benchmarking/src/mock.rs | 5 +++-- frame/session/benchmarking/src/mock.rs | 3 ++- frame/session/src/historical/mod.rs | 28 +++++++++++++++++++++--- frame/session/src/historical/offchain.rs | 2 +- frame/session/src/historical/onchain.rs | 4 ++-- frame/session/src/lib.rs | 18 --------------- frame/session/src/mock.rs | 5 ++--- frame/staking/fuzzer/src/mock.rs | 4 +++- frame/staking/src/lib.rs | 4 ++-- frame/staking/src/mock.rs | 7 +++--- frame/staking/src/tests.rs | 2 +- 18 files changed, 69 insertions(+), 56 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 097327c4f77ec..eeff63b7d9902 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -402,10 +402,11 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } - impl pallet_session::ValidatorIdentification<::AccountId> for Runtime { type ValidatorId = ::AccountId; type ValidatorIdOf = pallet_staking::StashOf; +} +impl pallet_session::historical::FullValidatorIdentification<::AccountId> for Runtime { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -421,8 +422,6 @@ impl pallet_session::Trait for Runtime { type WeightInfo = weights::pallet_session::WeightInfo; } -impl pallet_session::historical::Trait for Runtime {} - pallet_staking_reward_curve::build! { const REWARD_CURVE: PiecewiseLinear<'static> = curve!( min_inflation: 0_025_000, @@ -772,7 +771,7 @@ parameter_types! { impl pallet_offences::Trait for Runtime { type Event = Event; - type IdentificationTuple = pallet_session::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index e9f829697584b..79ac0f26afa76 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -116,6 +116,8 @@ mod tests { impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AuthorityId; type ValidatorIdOf = ConvertInto; + } + impl pallet_session::historical::FullValidatorIdentification for Test { type FullIdentification = (); type FullIdentificationOf = (); } @@ -131,8 +133,6 @@ mod tests { type WeightInfo = (); } - impl pallet_session::historical::Trait for Test {} - pub type BlockNumber = u64; parameter_types! { diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index 3b76e4a789260..f8b827251ada7 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -110,6 +110,8 @@ impl_opaque_keys! { impl pallet_session::ValidatorIdentification for Test { type ValidatorId = ::AccountId; type ValidatorIdOf = pallet_staking::StashOf; +} +impl pallet_session::historical::FullValidatorIdentification for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -125,8 +127,6 @@ impl pallet_session::Trait for Test { type WeightInfo = (); } -impl pallet_session::historical::Trait for Test {} - parameter_types! { pub const UncleGenerations: u64 = 0; } @@ -229,7 +229,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = (); - type IdentificationTuple = pallet_session::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index 07b6167a95ce1..34102535541af 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -124,6 +124,8 @@ parameter_types! { impl pallet_session::ValidatorIdentification for Test { type ValidatorId = u64; type ValidatorIdOf = pallet_staking::StashOf; +} +impl pallet_session::historical::FullValidatorIdentification for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -140,8 +142,6 @@ impl pallet_session::Trait for Test { type WeightInfo = (); } -impl pallet_session::historical::Trait for Test {} - parameter_types! { pub const UncleGenerations: u64 = 0; } @@ -244,7 +244,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = TestEvent; - type IdentificationTuple = pallet_session::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 54dea77ec7bcd..7fd0ecb41eedb 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -103,7 +103,10 @@ use frame_system::offchain::{ SendTransactionTypes, SubmitTransaction, }; -use pallet_session::{IdentificationTuple, ValidatorIdentification}; +use pallet_session::{ + historical::{FullValidatorIdentification, IdentificationTuple}, + ValidatorIdentification, +}; pub mod sr25519 { mod app_sr25519 { @@ -234,7 +237,7 @@ pub trait WeightInfo { pub trait Trait: SendTransactionTypes> - + ValidatorIdentification<::AccountId> + + FullValidatorIdentification<::AccountId> + frame_system::Trait { /// The identifier type for an authority. @@ -671,7 +674,7 @@ impl pallet_session::OneSessionHandler for Module { .filter(|(index, id)| !Self::is_online_aux(*index as u32, id) ).filter_map(|(_, id)| - >::FullIdentificationOf::convert(id.clone()).map(|full_id| (id, full_id)) + >::FullIdentificationOf::convert(id.clone()).map(|full_id| (id, full_id)) ).collect::>>(); // Remove all received heartbeats and number of authored blocks from the diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 0fcb13d5e6ec5..7b9464f9b8e4a 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -149,6 +149,8 @@ parameter_types! { impl pallet_session::ValidatorIdentification for Runtime { type ValidatorId = u64; type ValidatorIdOf = ConvertInto; +} +impl pallet_session::historical::FullValidatorIdentification for Runtime { type FullIdentification = u64; type FullIdentificationOf = ConvertInto; } @@ -164,8 +166,6 @@ impl pallet_session::Trait for Runtime { type WeightInfo = (); } -impl pallet_session::historical::Trait for Runtime {} - parameter_types! { pub const UncleGenerations: u32 = 5; } diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 6f245cff7f61c..b65b3c61e8859 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -37,7 +37,10 @@ use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot}; use pallet_im_online::{Trait as ImOnlineTrait, Module as ImOnline, UnresponsivenessOffence}; use pallet_offences::{Trait as OffencesTrait, Module as Offences}; use pallet_session::historical::{Trait as HistoricalTrait}; -use pallet_session::{Trait as SessionTrait, SessionManager, IdentificationTuple, ValidatorIdentification}; +use pallet_session::{ + Trait as SessionTrait, SessionManager, ValidatorIdentification, + historical::{IdentificationTuple, FullValidatorIdentification}, +}; use pallet_staking::{ Module as Staking, Trait as StakingTrait, RewardDestination, ValidatorPrefs, Exposure, IndividualExposure, ElectionStatus, MAX_NOMINATIONS, Event as StakingEvent @@ -170,7 +173,7 @@ fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< let validator_id_of = ::AccountId>>::ValidatorIdOf::convert; let full_identification_of = - ::AccountId>>::FullIdentificationOf::convert; + ::AccountId>>::FullIdentificationOf::convert; let id_tuples = offenders.iter() .map(|offender| validator_id_of(offender.controller.clone()) diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 618d1411a921c..0579a8db79763 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -93,10 +93,11 @@ impl pallet_timestamp::Trait for Test { impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; +} +impl pallet_session::historical::FullValidatorIdentification for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } -impl pallet_session::historical::Trait for Test {} sp_runtime::impl_opaque_keys! { pub struct SessionKeys { @@ -203,7 +204,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = Event; - type IdentificationTuple = pallet_session::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index 5418a2977d084..6509384adb3bf 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -109,10 +109,11 @@ impl pallet_timestamp::Trait for Test { impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; +} +impl pallet_session::historical::FullValidatorIdentification for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } -impl pallet_session::historical::Trait for Test {} sp_runtime::impl_opaque_keys! { pub struct SessionKeys { diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index 5c2b1303a43d1..f846e7ed4c6d7 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -31,18 +31,40 @@ 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::{decl_module, decl_storage, Parameter}; use frame_support::print; use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX}; use sp_trie::trie_types::{TrieDBMut, TrieDB}; -use super::{SessionIndex, Module as SessionModule, IdentificationTuple}; +use super::{SessionIndex, Module as SessionModule, ValidatorIdentification}; mod shared; pub mod offchain; pub mod onchain; +pub trait FullValidatorIdentification: ValidatorIdentification { + /// Full identification of the validator. + type FullIdentification: Parameter; + + /// A conversion from validator ID to full identification. + /// + /// This should contain any references to economic actors associated with the + /// validator, since they may be outdated by the time this is queried from a + /// historical trie. + /// + /// It must return the identification for the current session index. + type FullIdentificationOf: Convert>; +} + +/// A tuple of the validator's ID and their full identification. +pub type IdentificationTuple = ( + >::ValidatorId, + >::FullIdentification +); + /// Trait necessary for the historical module. -pub trait Trait: super::Trait {} +pub trait Trait: FullValidatorIdentification<::AccountId> + super::Trait {} + +impl::AccountId> + super::Trait> Trait for T {} decl_storage! { trait Store for Module as Session { diff --git a/frame/session/src/historical/offchain.rs b/frame/session/src/historical/offchain.rs index 4b76d6356b58a..5200607d14521 100644 --- a/frame/session/src/historical/offchain.rs +++ b/frame/session/src/historical/offchain.rs @@ -193,7 +193,7 @@ mod tests { let sample = ( 22u32 as >::ValidatorId, - 7_777_777 as >::FullIdentification); + 7_777_777 as >::FullIdentification); let encoded = sample.encode(); let decoded = Decode::decode(&mut encoded.as_slice()).expect("Must decode"); diff --git a/frame/session/src/historical/onchain.rs b/frame/session/src/historical/onchain.rs index 0a4038b4c3921..2ceca4ec6a7f4 100644 --- a/frame/session/src/historical/onchain.rs +++ b/frame/session/src/historical/onchain.rs @@ -22,7 +22,7 @@ use sp_runtime::traits::Convert; use super::super::Trait as SessionTrait; use super::super::{Module as SessionModule, SessionIndex, ValidatorIdentification}; -use super::Trait as HistoricalTrait; +use super::{Trait as HistoricalTrait, FullValidatorIdentification}; use super::shared; use sp_std::prelude::*; @@ -42,7 +42,7 @@ pub fn store_session_validator_set_to_offchain>::ValidatorId| { let full_identification = - <>::FullIdentificationOf>::convert(validator_id.clone()); + <>::FullIdentificationOf>::convert(validator_id.clone()); full_identification.map(|full_identification| (validator_id, full_identification)) }) .collect::>(); diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 9fc213494da87..113f8af6ec14b 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -133,26 +133,8 @@ pub trait ValidatorIdentification { /// /// Its cost must be at most one storage read. type ValidatorIdOf: Convert>; - - /// Full identification of the validator. - type FullIdentification: Parameter; - - /// A conversion from validator ID to full identification. - /// - /// This should contain any references to economic actors associated with the - /// validator, since they may be outdated by the time this is queried from a - /// historical trie. - /// - /// It must return the identification for the current session index. - type FullIdentificationOf: Convert>; } -/// A tuple of the validator's ID and their full identification. -pub type IdentificationTuple = ( - >::ValidatorId, - >::FullIdentification -); - /// Decides whether the session should be ended. pub trait ShouldEndSession { /// Return `true` if the session should be ended. diff --git a/frame/session/src/mock.rs b/frame/session/src/mock.rs index d2b0e09d9bb08..dfbd6deb4ab61 100644 --- a/frame/session/src/mock.rs +++ b/frame/session/src/mock.rs @@ -214,6 +214,8 @@ parameter_types! { impl crate::ValidatorIdentification for Test { type ValidatorId = u64; type ValidatorIdOf = ConvertInto; +} +impl crate::historical::FullValidatorIdentification for Test { type FullIdentification = u64; type FullIdentificationOf = sp_runtime::traits::ConvertInto; } @@ -232,8 +234,5 @@ impl Trait for Test { type WeightInfo = (); } -#[cfg(feature = "historical")] -impl crate::historical::Trait for Test {} - pub type System = frame_system::Module; pub type Session = Module; diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index b24d06f9b741b..2d9f0ccd7b0d7 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -111,11 +111,13 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl pallet_session::historical::Trait for Test {} impl pallet_session::ValidatorIdentification<::AccountId> for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; +} + +impl pallet_session::historical::FullValidatorIdentification<::AccountId> for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 46af630dcbdc2..ec5a7b7127e78 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -3177,7 +3177,7 @@ impl Convert> impl OnOffenceHandler< T::AccountId, - pallet_session::IdentificationTuple<::AccountId, T>, + pallet_session::historical::IdentificationTuple<::AccountId, T>, Weight, > for Module where @@ -3196,7 +3196,7 @@ for Module where fn on_offence( offenders: &[OffenceDetails< T::AccountId, - pallet_session::IdentificationTuple<::AccountId, T>> + pallet_session::historical::IdentificationTuple<::AccountId, T>> ], slash_fraction: &[Perbill], slash_session: SessionIndex, diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 7f142cec8b585..1892e32a56f64 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -249,6 +249,8 @@ sp_runtime::impl_opaque_keys! { impl pallet_session::ValidatorIdentification for Test { type ValidatorId = AccountId; type ValidatorIdOf = crate::StashOf; +} +impl pallet_session::historical::FullValidatorIdentification for Test { type FullIdentification = crate::Exposure; type FullIdentificationOf = crate::ExposureOf; } @@ -264,7 +266,6 @@ impl pallet_session::Trait for Test { type WeightInfo = (); } -impl pallet_session::historical::Trait for Test {} impl pallet_authorship::Trait for Test { type FindAuthor = Author11; type UncleGenerations = UncleGenerations; @@ -742,7 +743,7 @@ pub(crate) fn validator_controllers() -> Vec { pub(crate) fn on_offence_in_era( offenders: &[OffenceDetails< AccountId, - pallet_session::IdentificationTuple, + pallet_session::historical::IdentificationTuple, >], slash_fraction: &[Perbill], era: EraIndex, @@ -770,7 +771,7 @@ pub(crate) fn on_offence_in_era( } pub(crate) fn on_offence_now( - offenders: &[OffenceDetails>], + offenders: &[OffenceDetails>], slash_fraction: &[Perbill], ) { let now = Staking::active_era().unwrap().index; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 840d5bdf8bd25..9643817d2f335 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -4578,7 +4578,7 @@ fn offences_weight_calculated_correctly() { let offenders: Vec::AccountId, - pallet_session::IdentificationTuple<::AccountId, Test>, + pallet_session::historical::IdentificationTuple<::AccountId, Test>, >> = (1..10).map(|i| OffenceDetails { From c395c81542ab7360b43ef57fdf03dd932628e914 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sat, 26 Sep 2020 20:52:27 +0800 Subject: [PATCH 12/33] Fix line width --- frame/im-online/src/lib.rs | 4 +++- frame/offences/benchmarking/src/lib.rs | 3 ++- frame/session/src/historical/mod.rs | 7 +++++-- frame/session/src/historical/onchain.rs | 4 +++- frame/staking/fuzzer/src/mock.rs | 4 +++- frame/staking/src/mock.rs | 5 ++++- frame/staking/src/tests.rs | 5 ++++- 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 7fd0ecb41eedb..3a07b5d4935a0 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -674,7 +674,9 @@ impl pallet_session::OneSessionHandler for Module { .filter(|(index, id)| !Self::is_online_aux(*index as u32, id) ).filter_map(|(_, id)| - >::FullIdentificationOf::convert(id.clone()).map(|full_id| (id, full_id)) + >::FullIdentificationOf::convert( + id.clone() + ).map(|full_id| (id, full_id)) ).collect::>>(); // Remove all received heartbeats and number of authored blocks from the diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index b65b3c61e8859..231d4d18ad3c3 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -75,7 +75,8 @@ pub trait IdTupleConvert { } impl IdTupleConvert for T where - ::IdentificationTuple: From::AccountId, T>> + ::IdentificationTuple: + From::AccountId, T>> { fn convert( id: IdentificationTuple<::AccountId, T>, diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index f846e7ed4c6d7..1c5f5a94b6c45 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -62,9 +62,12 @@ pub type IdentificationTuple = ( ); /// Trait necessary for the historical module. -pub trait Trait: FullValidatorIdentification<::AccountId> + super::Trait {} +pub trait Trait: + FullValidatorIdentification<::AccountId> + super::Trait {} + +impl Trait for T + where T: FullValidatorIdentification<::AccountId> + super::Trait {} -impl::AccountId> + super::Trait> Trait for T {} decl_storage! { trait Store for Module as Session { diff --git a/frame/session/src/historical/onchain.rs b/frame/session/src/historical/onchain.rs index 2ceca4ec6a7f4..b431833bb7c11 100644 --- a/frame/session/src/historical/onchain.rs +++ b/frame/session/src/historical/onchain.rs @@ -42,7 +42,9 @@ pub fn store_session_validator_set_to_offchain>::ValidatorId| { let full_identification = - <>::FullIdentificationOf>::convert(validator_id.clone()); + <>::FullIdentificationOf>::convert( + validator_id.clone(), + ); full_identification.map(|full_identification| (validator_id, full_identification)) }) .collect::>(); diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index 2d9f0ccd7b0d7..1f437f22904a6 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -117,7 +117,9 @@ impl pallet_session::ValidatorIdentification<::Acco type ValidatorIdOf = pallet_staking::StashOf; } -impl pallet_session::historical::FullValidatorIdentification<::AccountId> for Test { +impl pallet_session::historical::FullValidatorIdentification<::AccountId> + for Test +{ type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 1892e32a56f64..c705e2c1a81f6 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -771,7 +771,10 @@ pub(crate) fn on_offence_in_era( } pub(crate) fn on_offence_now( - offenders: &[OffenceDetails>], + offenders: &[OffenceDetails< + AccountId, + pallet_session::historical::IdentificationTuple>, + ], slash_fraction: &[Perbill], ) { let now = Staking::active_era().unwrap().index; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 9643817d2f335..0330692531dae 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -4578,7 +4578,10 @@ fn offences_weight_calculated_correctly() { let offenders: Vec::AccountId, - pallet_session::historical::IdentificationTuple<::AccountId, Test>, + pallet_session::historical::IdentificationTuple< + ::AccountId, + Test, + >, >> = (1..10).map(|i| OffenceDetails { From 4ebb2ef903137ba2a0677e00f8567dde69c6b539 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sat, 26 Sep 2020 21:58:13 +0800 Subject: [PATCH 13/33] Fix staking mock --- frame/staking/src/mock.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index c705e2c1a81f6..37cd1a581b116 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -773,8 +773,8 @@ pub(crate) fn on_offence_in_era( pub(crate) fn on_offence_now( offenders: &[OffenceDetails< AccountId, - pallet_session::historical::IdentificationTuple>, - ], + pallet_session::historical::IdentificationTuple, + >], slash_fraction: &[Perbill], ) { let now = Staking::active_era().unwrap().index; From 5a92424e80ed018072768f12a45db24abbebc510 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sat, 26 Sep 2020 22:45:37 +0800 Subject: [PATCH 14/33] Fix session doc test --- frame/session/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 113f8af6ec14b..81fb62e8bac19 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -88,7 +88,9 @@ //! ``` //! use pallet_session as session; //! -//! fn validators() -> Vec<::ValidatorId> { +//! fn validators() -> Vec< +//! ::AccountId>::ValidatorId +//! > { //! >::validators() //! } //! # fn main(){} From 90efb6730ade932dd70e7fe96fcc2ddc9bd54d2f Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sun, 27 Sep 2020 10:39:34 +0800 Subject: [PATCH 15/33] Simplify >::ValidatorId as ValidatorId --- frame/im-online/src/lib.rs | 13 +++++-------- frame/session/src/historical/onchain.rs | 4 ++-- frame/session/src/lib.rs | 15 ++++++--------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 3a07b5d4935a0..4872838bfda84 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -105,7 +105,7 @@ use frame_system::offchain::{ }; use pallet_session::{ historical::{FullValidatorIdentification, IdentificationTuple}, - ValidatorIdentification, + ValidatorIdentification, ValidatorId }; pub mod sr25519 { @@ -417,16 +417,13 @@ type OffchainResult = Result::Bl /// Keep track of number of authored blocks per authority, uncles are counted as /// well since they're a valid proof of being online. impl - pallet_authorship::EventHandler< - >::ValidatorId, - T::BlockNumber, - > for Module + pallet_authorship::EventHandler, T::BlockNumber> for Module { - fn note_author(author: >::ValidatorId) { + fn note_author(author: ValidatorId) { Self::note_authorship(author); } - fn note_uncle(author: >::ValidatorId, _age: T::BlockNumber) { + fn note_uncle(author: ValidatorId, _age: T::BlockNumber) { Self::note_authorship(author); } } @@ -469,7 +466,7 @@ impl Module { } /// Note that the given authority has authored a block in the current session. - fn note_authorship(author: >::ValidatorId) { + fn note_authorship(author: ValidatorId) { let current_session = T::SessionInterface::current_index(); >::mutate( diff --git a/frame/session/src/historical/onchain.rs b/frame/session/src/historical/onchain.rs index b431833bb7c11..9c230cfd4ae9a 100644 --- a/frame/session/src/historical/onchain.rs +++ b/frame/session/src/historical/onchain.rs @@ -21,7 +21,7 @@ use codec::Encode; use sp_runtime::traits::Convert; use super::super::Trait as SessionTrait; -use super::super::{Module as SessionModule, SessionIndex, ValidatorIdentification}; +use super::super::{Module as SessionModule, SessionIndex, ValidatorId}; use super::{Trait as HistoricalTrait, FullValidatorIdentification}; use super::shared; @@ -40,7 +40,7 @@ pub fn store_session_validator_set_to_offchain>::validators() .into_iter() - .filter_map(|validator_id: >::ValidatorId| { + .filter_map(|validator_id: ValidatorId| { let full_identification = <>::FullIdentificationOf>::convert( validator_id.clone(), diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 81fb62e8bac19..09583c77a6068 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -88,9 +88,7 @@ //! ``` //! use pallet_session as session; //! -//! fn validators() -> Vec< -//! ::AccountId>::ValidatorId -//! > { +//! fn validators() -> Vec> { //! >::validators() //! } //! # fn main(){} @@ -137,6 +135,9 @@ pub trait ValidatorIdentification { type ValidatorIdOf: Convert>; } +pub type ValidatorId = + ::AccountId>>::ValidatorId; + /// Decides whether the session should be ended. pub trait ShouldEndSession { /// Return `true` if the session should be ended. @@ -811,15 +812,11 @@ impl EstimateNextNewSession for Module { } } -impl - sp_session::SessionInterface< - ::AccountId>>::ValidatorId - > for Module -{ +impl sp_session::SessionInterface> for Module { fn current_index() -> sp_staking::SessionIndex { Module::::current_index() } - fn validators() -> Vec<::AccountId>>::ValidatorId> { + fn validators() -> Vec> { Module::::validators() } } From 1a71e3e939c5dfc00214cd3b822df23dd441f853 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sun, 27 Sep 2020 11:07:16 +0800 Subject: [PATCH 16/33] Nits --- frame/im-online/src/lib.rs | 15 +++++---------- frame/session/src/historical/mod.rs | 22 ++++++++++++++-------- frame/session/src/lib.rs | 6 ++++-- frame/staking/src/mock.rs | 5 +---- frame/staking/src/tests.rs | 14 +++++++------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 4872838bfda84..903c53a24983c 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -311,11 +311,10 @@ decl_storage! { double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) AuthIndex => Option>; - /// For each session index, we keep a mapping of `>::ValidatorId` - /// to the number of blocks authored by the given authority. + /// For each session index, we keep a mapping of `ValidatorId` to the + /// number of blocks authored by the given authority. AuthoredBlocks get(fn authored_blocks): - double_map hasher(twox_64_concat) SessionIndex, - hasher(twox_64_concat) >::ValidatorId + double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) ValidatorId => u32; } add_extra_genesis { @@ -416,8 +415,7 @@ type OffchainResult = Result::Bl /// Keep track of number of authored blocks per authority, uncles are counted as /// well since they're a valid proof of being online. -impl - pallet_authorship::EventHandler, T::BlockNumber> for Module +impl pallet_authorship::EventHandler, T::BlockNumber> for Module { fn note_author(author: ValidatorId) { Self::note_authorship(author); @@ -445,10 +443,7 @@ impl Module { Self::is_online_aux(authority_index, authority) } - fn is_online_aux( - authority_index: AuthIndex, - authority: &>::ValidatorId, - ) -> bool { + fn is_online_aux(authority_index: AuthIndex, authority: &ValidatorId) -> bool { let current_session = T::SessionInterface::current_index(); ::contains_key(¤t_session, &authority_index) || diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index 1c5f5a94b6c45..ba03f52468b8b 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -55,12 +55,6 @@ pub trait FullValidatorIdentification: ValidatorIdentification>; } -/// A tuple of the validator's ID and their full identification. -pub type IdentificationTuple = ( - >::ValidatorId, - >::FullIdentification -); - /// Trait necessary for the historical module. pub trait Trait: FullValidatorIdentification<::AccountId> + super::Trait {} @@ -68,6 +62,11 @@ pub trait Trait: impl Trait for T where T: FullValidatorIdentification<::AccountId> + super::Trait {} +/// A tuple of the validator's ID and their full identification. +pub type IdentificationTuple = ( + >::ValidatorId, + >::FullIdentification +); decl_storage! { trait Store for Module as Session { @@ -258,7 +257,11 @@ impl ProvingTrie { // Check a proof contained within the current memory-db. Returns `None` if the // nodes within the current `MemoryDB` are insufficient to query the item. - fn query(&self, key_id: KeyTypeId, key_data: &[u8]) -> Option> { + fn query( + &self, + key_id: KeyTypeId, + key_data: &[u8], + ) -> Option> { let trie = TrieDB::new(&self.db, &self.root).ok()?; let val_idx = (key_id, key_data).using_encoded(|s| trie.get(s)) .ok()? @@ -299,7 +302,10 @@ impl> frame_support::traits::KeyOwnerProofSystem<(KeyTy }) } - fn check_proof(key: (KeyTypeId, D), proof: Self::Proof) -> Option> { + fn check_proof( + key: (KeyTypeId, D), + proof: Self::Proof, + ) -> Option> { let (id, data) = key; if proof.session == >::current_index() { diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 09583c77a6068..e11111507a5f7 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -371,7 +371,9 @@ pub trait WeightInfo { fn purge_keys() -> Weight; } -pub trait Trait: ValidatorIdentification<::AccountId> + frame_system::Trait { +pub trait Trait: + ValidatorIdentification<::AccountId> + frame_system::Trait +{ /// The overarching event type. type Event: From + Into<::Event>; @@ -819,4 +821,4 @@ impl sp_session::SessionInterface> for Module { fn validators() -> Vec> { Module::::validators() } -} +} \ No newline at end of file diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 37cd1a581b116..1892e32a56f64 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -771,10 +771,7 @@ pub(crate) fn on_offence_in_era( } pub(crate) fn on_offence_now( - offenders: &[OffenceDetails< - AccountId, - pallet_session::historical::IdentificationTuple, - >], + offenders: &[OffenceDetails>], slash_fraction: &[Perbill], ) { let now = Staking::active_era().unwrap().index; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 0330692531dae..7b396542d4577 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -4568,6 +4568,8 @@ fn bond_during_era_correctly_populates_claimed_rewards() { #[test] fn offences_weight_calculated_correctly() { ExtBuilder::default().nominate(true).build_and_execute(|| { + use pallet_session::historical::IdentificationTuple; + // On offence with zero offenders: 4 Reads, 1 Write let zero_offence_weight = ::DbWeight::get().reads_writes(4, 1); assert_eq!(Staking::on_offence(&[], &[Perbill::from_percent(50)], 0), Ok(zero_offence_weight)); @@ -4576,13 +4578,11 @@ fn offences_weight_calculated_correctly() { let n_offence_unapplied_weight = ::DbWeight::get().reads_writes(4, 1) + ::DbWeight::get().reads_writes(4, 5); - let offenders: Vec::AccountId, - pallet_session::historical::IdentificationTuple< - ::AccountId, - Test, - >, - >> + let offenders: Vec< + OffenceDetails< + ::AccountId, + IdentificationTuple<::AccountId, Test>> + > = (1..10).map(|i| OffenceDetails { offender: (i, Staking::eras_stakers(Staking::active_era().unwrap().index, i)), From f27454189c0f085be3ffd29d33bc02c85a670d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 19 Oct 2020 15:38:58 +0200 Subject: [PATCH 17/33] Clean up. --- frame/im-online/src/lib.rs | 69 ++++++++++++++---------- frame/offences/benchmarking/src/lib.rs | 35 +++++------- frame/offences/benchmarking/src/mock.rs | 11 ++-- frame/session/src/historical/mod.rs | 60 +++++++++++---------- frame/session/src/historical/offchain.rs | 9 ++-- frame/session/src/historical/onchain.rs | 10 ++-- frame/session/src/lib.rs | 48 ++++++++--------- frame/staking/fuzzer/src/mock.rs | 12 ++--- frame/staking/src/lib.rs | 11 +--- frame/staking/src/mock.rs | 20 +++---- frame/staking/src/tests.rs | 8 +-- primitives/session/Cargo.toml | 2 +- primitives/session/src/lib.rs | 16 +++++- 13 files changed, 149 insertions(+), 162 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 903c53a24983c..d14d0b98d3077 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -92,7 +92,7 @@ use sp_staking::{ SessionIndex, offence::{ReportOffence, Offence, Kind}, }; -use sp_session::SessionInterface; +use sp_session::{ValidatorSet, ValidatorSetWithIdentification}; use frame_support::{ decl_module, decl_event, decl_storage, Parameter, debug, decl_error, traits::Get, @@ -103,10 +103,10 @@ use frame_system::offchain::{ SendTransactionTypes, SubmitTransaction, }; -use pallet_session::{ - historical::{FullValidatorIdentification, IdentificationTuple}, - ValidatorIdentification, ValidatorId -}; +// use pallet_session::{ +// historical::{FullValidatorIdentification, IdentificationTuple}, +// ValidatorIdentification, ValidatorId +// }; pub mod sr25519 { mod app_sr25519 { @@ -237,7 +237,6 @@ pub trait WeightInfo { pub trait Trait: SendTransactionTypes> - + FullValidatorIdentification<::AccountId> + frame_system::Trait { /// The identifier type for an authority. @@ -254,18 +253,22 @@ pub trait Trait: /// there is a chance the authority will produce a block and they won't be necessary. type SessionDuration: Get; - /// A type for retrieving the necessary session info. - /// - /// This is currently used to get the session index and list of validators expected to be online. - type SessionInterface: - SessionInterface<::AccountId>>::ValidatorId>; + type ValidatorSet: ValidatorSetWithIdentification; /// A type that gives us the ability to submit unresponsiveness offence reports. type ReportUnresponsiveness: ReportOffence< Self::AccountId, - IdentificationTuple<::AccountId, Self>, - UnresponsivenessOffence::AccountId, Self>>, + // TODO [ToDr] This deserves a typedef (IdentificationTuple), but not the one in + // session module - we need it decoupled. + ( + >::ValidatorId, + >::Identification + ), + UnresponsivenessOffence<( + >::ValidatorId, + >::Identification + )>, >; /// A configuration for base priority of unsigned transactions. @@ -281,7 +284,10 @@ pub trait Trait: decl_event!( pub enum Event where ::AuthorityId, - IdentificationTuple = IdentificationTuple<::AccountId, T>, + IdentificationTuple = ( + <::ValidatorSet as ValidatorSet<::AccountId>>::ValidatorId, + <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification + ), { /// A new heartbeat was received from `AuthorityId` \[authority_id\] HeartbeatReceived(AuthorityId), @@ -292,6 +298,10 @@ decl_event!( } ); +type ValidatorId = < + ::ValidatorSet as ValidatorSet<::AccountId> +>::ValidatorId; + decl_storage! { trait Store for Module as ImOnline { /// The block number after which it's ok to send heartbeats in current session. @@ -363,7 +373,7 @@ decl_module! { ) { ensure_none(origin)?; - let current_session = T::SessionInterface::current_index(); + let current_session = T::ValidatorSet::current_index(); let exists = ::contains_key( ¤t_session, &heartbeat.authority_index @@ -432,7 +442,7 @@ impl Module { /// authored at least one block, during the current session. Otherwise /// `false`. pub fn is_online(authority_index: AuthIndex) -> bool { - let current_validators = T::SessionInterface::validators(); + let current_validators = T::ValidatorSet::validators(); if authority_index >= current_validators.len() as u32 { return false; @@ -444,7 +454,7 @@ impl Module { } fn is_online_aux(authority_index: AuthIndex, authority: &ValidatorId) -> bool { - let current_session = T::SessionInterface::current_index(); + let current_session = T::ValidatorSet::current_index(); ::contains_key(¤t_session, &authority_index) || >::get( @@ -456,13 +466,13 @@ impl Module { /// Returns `true` if a heartbeat has been received for the authority at `authority_index` in /// the authorities series, during the current session. Otherwise `false`. pub fn received_heartbeat_in_current_session(authority_index: AuthIndex) -> bool { - let current_session = T::SessionInterface::current_index(); + let current_session = T::ValidatorSet::current_index(); ::contains_key(¤t_session, &authority_index) } /// Note that the given authority has authored a block in the current session. fn note_authorship(author: ValidatorId) { - let current_session = T::SessionInterface::current_index(); + let current_session = T::ValidatorSet::current_index(); >::mutate( ¤t_session, @@ -479,8 +489,8 @@ impl Module { return Err(OffchainErr::TooEarly(heartbeat_after)) } - let session_index = T::SessionInterface::current_index(); - let validators_len = T::SessionInterface::validators().len() as u32; + let session_index = T::ValidatorSet::current_index(); + let validators_len = T::ValidatorSet::validators().len() as u32; Ok(Self::local_authority_keys() .map(move |(authority_index, key)| @@ -658,24 +668,27 @@ impl pallet_session::OneSessionHandler for Module { } fn on_before_session_ending() { - let session_index = T::SessionInterface::current_index(); + let session_index = T::ValidatorSet::current_index(); let keys = Keys::::get(); - let current_validators = T::SessionInterface::validators(); + let current_validators = T::ValidatorSet::validators(); let offenders = current_validators.into_iter().enumerate() .filter(|(index, id)| !Self::is_online_aux(*index as u32, id) ).filter_map(|(_, id)| - >::FullIdentificationOf::convert( + >::IdentificationOf::convert( id.clone() ).map(|full_id| (id, full_id)) - ).collect::>>(); + ).collect::>::ValidatorId, + >::Identification + )>>(); // Remove all received heartbeats and number of authored blocks from the // current session, they have already been processed and won't be needed // anymore. - ::remove_prefix(&T::SessionInterface::current_index()); - >::remove_prefix(&T::SessionInterface::current_index()); + ::remove_prefix(&T::ValidatorSet::current_index()); + >::remove_prefix(&T::ValidatorSet::current_index()); if offenders.is_empty() { Self::deposit_event(RawEvent::AllGood); @@ -712,7 +725,7 @@ impl frame_support::unsigned::ValidateUnsigned for Module { } // check if session index from heartbeat is recent - let current_session = T::SessionInterface::current_index(); + let current_session = T::ValidatorSet::current_index(); if heartbeat.session_index != current_session { return InvalidTransaction::Stale.into(); } diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 231d4d18ad3c3..e35050992368a 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -36,11 +36,8 @@ use pallet_babe::BabeEquivocationOffence; use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot}; use pallet_im_online::{Trait as ImOnlineTrait, Module as ImOnline, UnresponsivenessOffence}; use pallet_offences::{Trait as OffencesTrait, Module as Offences}; -use pallet_session::historical::{Trait as HistoricalTrait}; -use pallet_session::{ - Trait as SessionTrait, SessionManager, ValidatorIdentification, - historical::{IdentificationTuple, FullValidatorIdentification}, -}; +use pallet_session::historical::{Trait as HistoricalTrait, IdentificationTuple}; +use pallet_session::{Trait as SessionTrait, SessionManager}; use pallet_staking::{ Module as Staking, Trait as StakingTrait, RewardDestination, ValidatorPrefs, Exposure, IndividualExposure, ElectionStatus, MAX_NOMINATIONS, Event as StakingEvent @@ -69,18 +66,13 @@ pub trait Trait: /// and the one required by offences. pub trait IdTupleConvert { /// Convert identification tuple from `historical` trait to the one expected by `offences`. - fn convert( - id: IdentificationTuple<::AccountId, T>, - ) -> ::IdentificationTuple; + fn convert(id: IdentificationTuple) -> ::IdentificationTuple; } impl IdTupleConvert for T where - ::IdentificationTuple: - From::AccountId, T>> + ::IdentificationTuple: From> { - fn convert( - id: IdentificationTuple<::AccountId, T>, - ) -> ::IdentificationTuple { + fn convert(id: IdentificationTuple) -> ::IdentificationTuple { id.into() } } @@ -158,7 +150,7 @@ fn create_offender(n: u32, nominators: u32) -> Result, &'s } fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< - (Vec::AccountId, T>>, Vec>), + (Vec>, Vec>), &'static str > { Staking::::new_session(0); @@ -171,18 +163,15 @@ fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< Staking::::start_session(0); - let validator_id_of = - ::AccountId>>::ValidatorIdOf::convert; - let full_identification_of = - ::AccountId>>::FullIdentificationOf::convert; - let id_tuples = offenders.iter() - .map(|offender| validator_id_of(offender.controller.clone()) - .expect("failed to get validator id from account id")) - .map(|validator_id| full_identification_of(validator_id.clone()) + .map(|offender| + ::ValidatorIdOf::convert(offender.controller.clone()) + .expect("failed to get validator id from account id")) + .map(|validator_id| + ::FullIdentificationOf::convert(validator_id.clone()) .map(|full_id| (validator_id, full_id)) .expect("failed to convert validator id to full identification")) - .collect::::AccountId, T>>>(); + .collect::>>(); Ok((id_tuples, offenders)) } diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index f06101120f0d6..527e0ede81ab9 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -89,11 +89,7 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl pallet_session::ValidatorIdentification for Test { - type ValidatorId = AccountId; - type ValidatorIdOf = pallet_staking::StashOf; -} -impl pallet_session::historical::FullValidatorIdentification for Test { +impl pallet_session::historical::Trait for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -131,6 +127,8 @@ impl pallet_session::Trait for Test { type NextSessionRotation = pallet_session::PeriodicSessions; type SessionHandler = TestSessionHandler; type Event = Event; + type ValidatorId = AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); type WeightInfo = (); } @@ -179,7 +177,6 @@ impl pallet_staking::Trait for Test { impl pallet_im_online::Trait for Test { type AuthorityId = UintAuthorityId; type Event = Event; - type SessionInterface = Session; type SessionDuration = Period; type ReportUnresponsiveness = Offences; type UnsignedPriority = (); @@ -192,7 +189,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = Event; - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index ba03f52468b8b..d7894a344012e 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -31,17 +31,18 @@ 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, Parameter}; -use frame_support::print; +use frame_support::{decl_module, decl_storage}; +use frame_support::{Parameter, print}; use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX}; use sp_trie::trie_types::{TrieDBMut, TrieDB}; -use super::{SessionIndex, Module as SessionModule, ValidatorIdentification}; +use super::{SessionIndex, Module as SessionModule}; mod shared; pub mod offchain; pub mod onchain; -pub trait FullValidatorIdentification: ValidatorIdentification { +/// Trait necessary for the historical module. +pub trait Trait: super::Trait { /// Full identification of the validator. type FullIdentification: Parameter; @@ -55,19 +56,6 @@ pub trait FullValidatorIdentification: ValidatorIdentification>; } -/// Trait necessary for the historical module. -pub trait Trait: - FullValidatorIdentification<::AccountId> + super::Trait {} - -impl Trait for T - where T: FullValidatorIdentification<::AccountId> + super::Trait {} - -/// A tuple of the validator's ID and their full identification. -pub type IdentificationTuple = ( - >::ValidatorId, - >::FullIdentification -); - decl_storage! { trait Store for Module as Session { /// Mapping from historical session indices to session-data root hash and validator count. @@ -114,6 +102,24 @@ impl Module { } } +impl sp_session::ValidatorSet for Module { + type ValidatorId = T::ValidatorId; + type ValidatorIdOf = T::ValidatorIdOf; + + fn current_index() -> sp_staking::SessionIndex { + super::Module::::current_index() + } + + fn validators() -> Vec { + super::Module::::validators() + } +} + +impl sp_session::ValidatorSetWithIdentification for Module { + 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: crate::SessionManager { @@ -171,6 +177,9 @@ impl crate::SessionManager for NoteHistoricalRoot = (::ValidatorId, ::FullIdentification); + /// A trie instance for checking and generating proofs. pub struct ProvingTrie { db: MemoryDB, @@ -244,7 +253,7 @@ impl ProvingTrie { val_idx.using_encoded(|s| { trie.get_with(s, &mut recorder) .ok()? - .and_then(|raw| >::decode(&mut &*raw).ok()) + .and_then(|raw| >::decode(&mut &*raw).ok()) })?; Some(recorder.drain().into_iter().map(|r| r.data).collect()) @@ -257,11 +266,7 @@ impl ProvingTrie { // Check a proof contained within the current memory-db. Returns `None` if the // nodes within the current `MemoryDB` are insufficient to query the item. - fn query( - &self, - key_id: KeyTypeId, - key_data: &[u8], - ) -> Option> { + fn query(&self, key_id: KeyTypeId, key_data: &[u8]) -> Option> { let trie = TrieDB::new(&self.db, &self.root).ok()?; let val_idx = (key_id, key_data).using_encoded(|s| trie.get(s)) .ok()? @@ -269,7 +274,7 @@ impl ProvingTrie { val_idx.using_encoded(|s| trie.get(s)) .ok()? - .and_then(|raw| >::decode(&mut &*raw).ok()) + .and_then(|raw| >::decode(&mut &*raw).ok()) } } @@ -277,7 +282,7 @@ impl> frame_support::traits::KeyOwnerProofSystem<(KeyTy for Module { type Proof = MembershipProof; - type IdentificationTuple = IdentificationTuple; + type IdentificationTuple = IdentificationTuple; fn prove(key: (KeyTypeId, D)) -> Option { let session = >::current_index(); @@ -302,10 +307,7 @@ impl> frame_support::traits::KeyOwnerProofSystem<(KeyTy }) } - fn check_proof( - key: (KeyTypeId, D), - proof: Self::Proof, - ) -> Option> { + fn check_proof(key: (KeyTypeId, D), proof: Self::Proof) -> Option> { let (id, data) = key; if proof.session == >::current_index() { diff --git a/frame/session/src/historical/offchain.rs b/frame/session/src/historical/offchain.rs index 5200607d14521..97655d1a18b32 100644 --- a/frame/session/src/historical/offchain.rs +++ b/frame/session/src/historical/offchain.rs @@ -37,7 +37,7 @@ use sp_std::prelude::*; /// A set of validators, which was used for a fixed session index. struct ValidatorSet { - validator_set: Vec::AccountId, T>>, + validator_set: Vec>, } impl ValidatorSet { @@ -152,7 +152,6 @@ mod tests { }; use sp_runtime::testing::UintAuthorityId; - use crate::ValidatorIdentification; type Historical = Module; @@ -190,10 +189,12 @@ mod tests { #[test] fn encode_decode_roundtrip() { use codec::{Decode, Encode}; + use super::super::super::Trait as SessionTrait; + use super::super::Trait as HistoricalTrait; let sample = ( - 22u32 as >::ValidatorId, - 7_777_777 as >::FullIdentification); + 22u32 as ::ValidatorId, + 7_777_777 as ::FullIdentification); let encoded = sample.encode(); let decoded = Decode::decode(&mut encoded.as_slice()).expect("Must decode"); diff --git a/frame/session/src/historical/onchain.rs b/frame/session/src/historical/onchain.rs index 9c230cfd4ae9a..745603a49829b 100644 --- a/frame/session/src/historical/onchain.rs +++ b/frame/session/src/historical/onchain.rs @@ -21,8 +21,8 @@ use codec::Encode; use sp_runtime::traits::Convert; use super::super::Trait as SessionTrait; -use super::super::{Module as SessionModule, SessionIndex, ValidatorId}; -use super::{Trait as HistoricalTrait, FullValidatorIdentification}; +use super::super::{Module as SessionModule, SessionIndex}; +use super::Trait as HistoricalTrait; use super::shared; use sp_std::prelude::*; @@ -40,11 +40,9 @@ pub fn store_session_validator_set_to_offchain>::validators() .into_iter() - .filter_map(|validator_id: ValidatorId| { + .filter_map(|validator_id: ::ValidatorId| { let full_identification = - <>::FullIdentificationOf>::convert( - validator_id.clone(), - ); + <::FullIdentificationOf>::convert(validator_id.clone()); full_identification.map(|full_identification| (validator_id, full_identification)) }) .collect::>(); diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index e11111507a5f7..32f85d80df27a 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -125,19 +125,6 @@ pub mod historical; mod default_weights; -pub trait ValidatorIdentification { - /// A stable ID for a validator. - type ValidatorId: Member + Parameter; - - /// A conversion from account ID to validator ID. - /// - /// Its cost must be at most one storage read. - type ValidatorIdOf: Convert>; -} - -pub type ValidatorId = - ::AccountId>>::ValidatorId; - /// Decides whether the session should be ended. pub trait ShouldEndSession { /// Return `true` if the session should be ended. @@ -264,6 +251,7 @@ pub trait SessionHandler { fn on_disabled(validator_index: usize); } +// TODO [ToDr] This should be moved out to primitives/session /// A session handler for specific key type. pub trait OneSessionHandler: BoundToRuntimeAppPublic { /// The key type expected. @@ -371,12 +359,18 @@ pub trait WeightInfo { fn purge_keys() -> Weight; } -pub trait Trait: - ValidatorIdentification<::AccountId> + frame_system::Trait -{ +pub trait Trait: frame_system::Trait { /// The overarching event type. type Event: From + Into<::Event>; + /// A stable ID for a validator. + type ValidatorId: Member + Parameter; + + /// A conversion from account ID to validator ID. + /// + /// Its cost must be at most one storage read. + type ValidatorIdOf: Convert>; + /// Indicator for when to end the session. type ShouldEndSession: ShouldEndSession; @@ -784,6 +778,19 @@ impl Module { } } +impl sp_session::ValidatorSet for Module { + type ValidatorId = T::ValidatorId; + type ValidatorIdOf = T::ValidatorIdOf; + + fn current_index() -> sp_staking::SessionIndex { + Module::::current_index() + } + + fn validators() -> Vec { + Module::::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. @@ -813,12 +820,3 @@ impl EstimateNextNewSession for Module { T::NextSessionRotation::weight(now) } } - -impl sp_session::SessionInterface> for Module { - fn current_index() -> sp_staking::SessionIndex { - Module::::current_index() - } - fn validators() -> Vec> { - Module::::validators() - } -} \ No newline at end of file diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index cfd2ddb14185a..96df7674e9f44 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -98,15 +98,7 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } - -impl pallet_session::ValidatorIdentification<::AccountId> for Test { - type ValidatorId = AccountId; - type ValidatorIdOf = pallet_staking::StashOf; -} - -impl pallet_session::historical::FullValidatorIdentification<::AccountId> - for Test -{ +impl pallet_session::historical::Trait for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -139,6 +131,8 @@ impl pallet_session::Trait for Test { type NextSessionRotation = pallet_session::PeriodicSessions<(), ()>; type SessionHandler = TestSessionHandler; type Event = (); + type ValidatorId = AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); type WeightInfo = (); } diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 1ad3d7ce22064..9436dfd3bb847 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -3194,11 +3194,7 @@ impl Convert> /// This is intended to be used with `FilterHistoricalOffences`. impl - OnOffenceHandler< - T::AccountId, - pallet_session::historical::IdentificationTuple<::AccountId, T>, - Weight, - > + OnOffenceHandler, Weight> for Module where T: pallet_session::Trait::AccountId>, T: pallet_session::historical::Trait< @@ -3213,10 +3209,7 @@ for Module where >, { fn on_offence( - offenders: &[OffenceDetails< - T::AccountId, - pallet_session::historical::IdentificationTuple<::AccountId, T>> - ], + offenders: &[OffenceDetails>], slash_fraction: &[Perbill], slash_session: SessionIndex, ) -> Result { diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 79a5f264155c2..055ebb9730805 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -233,27 +233,23 @@ sp_runtime::impl_opaque_keys! { pub other: OtherSessionHandler, } } - -impl pallet_session::ValidatorIdentification for Test { - type ValidatorId = AccountId; - type ValidatorIdOf = crate::StashOf; -} -impl pallet_session::historical::FullValidatorIdentification for Test { - type FullIdentification = crate::Exposure; - type FullIdentificationOf = crate::ExposureOf; -} - impl pallet_session::Trait for Test { type SessionManager = pallet_session::historical::NoteHistoricalRoot; type Keys = SessionKeys; type ShouldEndSession = pallet_session::PeriodicSessions; type SessionHandler = (OtherSessionHandler,); type Event = MetaEvent; + type ValidatorId = AccountId; + type ValidatorIdOf = crate::StashOf; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; type WeightInfo = (); } +impl pallet_session::historical::Trait for Test { + type FullIdentification = crate::Exposure; + type FullIdentificationOf = crate::ExposureOf; +} impl pallet_authorship::Trait for Test { type FindAuthor = Author11; type UncleGenerations = UncleGenerations; @@ -734,7 +730,7 @@ pub(crate) fn validator_controllers() -> Vec { pub(crate) fn on_offence_in_era( offenders: &[OffenceDetails< AccountId, - pallet_session::historical::IdentificationTuple, + pallet_session::historical::IdentificationTuple, >], slash_fraction: &[Perbill], era: EraIndex, @@ -762,7 +758,7 @@ pub(crate) fn on_offence_in_era( } pub(crate) fn on_offence_now( - offenders: &[OffenceDetails>], + offenders: &[OffenceDetails>], slash_fraction: &[Perbill], ) { let now = Staking::active_era().unwrap().index; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 455344d3d4a19..2a02d87aa2c57 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -4568,8 +4568,6 @@ fn bond_during_era_correctly_populates_claimed_rewards() { #[test] fn offences_weight_calculated_correctly() { ExtBuilder::default().nominate(true).build_and_execute(|| { - use pallet_session::historical::IdentificationTuple; - // On offence with zero offenders: 4 Reads, 1 Write let zero_offence_weight = ::DbWeight::get().reads_writes(4, 1); assert_eq!(Staking::on_offence(&[], &[Perbill::from_percent(50)], 0), Ok(zero_offence_weight)); @@ -4578,11 +4576,7 @@ fn offences_weight_calculated_correctly() { let n_offence_unapplied_weight = ::DbWeight::get().reads_writes(4, 1) + ::DbWeight::get().reads_writes(4, 5); - let offenders: Vec< - OffenceDetails< - ::AccountId, - IdentificationTuple<::AccountId, Test>> - > + let offenders: Vec::AccountId, pallet_session::historical::IdentificationTuple>> = (1..10).map(|i| OffenceDetails { offender: (i, Staking::eras_stakers(Staking::active_era().unwrap().index, i)), diff --git a/primitives/session/Cargo.toml b/primitives/session/Cargo.toml index 4fccce6283142..71e6184c7e100 100644 --- a/primitives/session/Cargo.toml +++ b/primitives/session/Cargo.toml @@ -18,7 +18,7 @@ sp-api = { version = "2.0.0", default-features = false, path = "../api" } sp-core = { version = "2.0.0", default-features = false, path = "../core" } sp-std = { version = "2.0.0", default-features = false, path = "../std" } sp-staking = { version = "2.0.0", default-features = false, path = "../staking" } -sp-runtime = { version = "2.0.0", optional = true, path = "../runtime" } +sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } [features] default = [ "std" ] diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index eac841270f7ad..67d88f5261a97 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -28,6 +28,7 @@ use sp_api::ProvideRuntimeApi; use sp_core::RuntimeDebug; use sp_core::crypto::KeyTypeId; +use sp_runtime::traits::Convert; use sp_staking::SessionIndex; use sp_std::vec::Vec; @@ -113,14 +114,25 @@ impl GetValidatorCount for MembershipProof { /// validators that are considered to be online in each session, particularly useful /// for the Substrate-based projects having their own staking implementation /// instead of using pallet-staking directly. -pub trait SessionInterface { +pub trait ValidatorSet { + // TODO [ToDr] This could use `frame_support::Parameter` instead,although don't know if such + // import is legal. + type ValidatorId: codec::Codec + codec::EncodeLike + Clone + Eq + sp_std::fmt::Debug; + // TODO [ToDr] This is most likely not needed along with `AccountId` + type ValidatorIdOf: Convert>; + /// Returns current session index. fn current_index() -> SessionIndex; /// Returns all the validators ought to be online in a session. /// /// The returned validators are all expected to be running an authority node. - fn validators() -> Vec; + fn validators() -> Vec; +} + +pub trait ValidatorSetWithIdentification: ValidatorSet { + type Identification: codec::Codec + codec::EncodeLike + Clone + Eq + sp_std::fmt::Debug; + type IdentificationOf: Convert>; } /// Generate the initial session keys with the given seeds, at the given block and store them in From c87f862cc4ce06d9b6d3fbca8d7ea531ed5ee4e6 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 22 Oct 2020 16:32:33 +0800 Subject: [PATCH 18/33] Make it compile by commenting out report_offence_im_online bench --- bin/node/runtime/src/lib.rs | 12 +++++------- frame/im-online/src/lib.rs | 25 +++++++++---------------- frame/offences/benchmarking/src/lib.rs | 11 ++++++----- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index a1568eeb09a49..77ce13b12e2c4 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -404,17 +404,15 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } -impl pallet_session::ValidatorIdentification<::AccountId> for Runtime { - type ValidatorId = ::AccountId; - type ValidatorIdOf = pallet_staking::StashOf; -} -impl pallet_session::historical::FullValidatorIdentification<::AccountId> for Runtime { +impl pallet_session::historical::Trait for Runtime { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } impl pallet_session::Trait for Runtime { type Event = Event; + type ValidatorId = ::AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type ShouldEndSession = Babe; type NextSessionRotation = Babe; type SessionManager = pallet_session::historical::NoteHistoricalRoot; @@ -777,7 +775,7 @@ impl frame_system::offchain::SendTransactionTypes for Runtime where impl pallet_im_online::Trait for Runtime { type AuthorityId = ImOnlineId; type Event = Event; - type SessionInterface = Session; + type ValidatorSet = Historical; type SessionDuration = SessionDuration; type ReportUnresponsiveness = Offences; type UnsignedPriority = ImOnlineUnsignedPriority; @@ -790,7 +788,7 @@ parameter_types! { impl pallet_offences::Trait for Runtime { type Event = Event; - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index d14d0b98d3077..8572040e3c22a 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -235,6 +235,11 @@ pub trait WeightInfo { fn validate_unsigned_and_then_heartbeat(k: u32, e: u32, ) -> Weight; } +pub type IdentificationTuple = ( + <::ValidatorSet as ValidatorSet<::AccountId>>::ValidatorId, + <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification, +); + pub trait Trait: SendTransactionTypes> + frame_system::Trait @@ -261,14 +266,8 @@ pub trait Trait: Self::AccountId, // TODO [ToDr] This deserves a typedef (IdentificationTuple), but not the one in // session module - we need it decoupled. - ( - >::ValidatorId, - >::Identification - ), - UnresponsivenessOffence<( - >::ValidatorId, - >::Identification - )>, + IdentificationTuple, + UnresponsivenessOffence>, >; /// A configuration for base priority of unsigned transactions. @@ -284,10 +283,7 @@ pub trait Trait: decl_event!( pub enum Event where ::AuthorityId, - IdentificationTuple = ( - <::ValidatorSet as ValidatorSet<::AccountId>>::ValidatorId, - <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification - ), + IdentificationTuple = IdentificationTuple, { /// A new heartbeat was received from `AuthorityId` \[authority_id\] HeartbeatReceived(AuthorityId), @@ -679,10 +675,7 @@ impl pallet_session::OneSessionHandler for Module { >::IdentificationOf::convert( id.clone() ).map(|full_id| (id, full_id)) - ).collect::>::ValidatorId, - >::Identification - )>>(); + ).collect::>>(); // Remove all received heartbeats and number of authored blocks from the // current session, they have already been processed and won't be needed diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index e35050992368a..2e3f17313b2c6 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -212,7 +212,7 @@ benchmarks! { let n in 0 .. MAX_NOMINATORS.min(MAX_NOMINATIONS as u32); // Make r reporters - let mut reporters = vec![]; + let mut reporters: Vec = vec![]; for i in 0 .. r { let reporter = account("reporter", i, SEED); reporters.push(reporter); @@ -235,11 +235,12 @@ benchmarks! { }; assert_eq!(System::::event_count(), 0); }: { - let _ = ::ReportUnresponsiveness::report_offence( - reporters.clone(), - offence - ); + //let _ = ::ReportUnresponsiveness::report_offence( + // reporters.clone(), + // offence + //); } + verify { // make sure the report was not deferred assert!(Offences::::deferred_offences().is_empty()); From 1840186dc966e8fc2067fcb5615fef4adcc3be33 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 22 Oct 2020 17:03:10 +0800 Subject: [PATCH 19/33] Tests --- bin/node/runtime/src/lib.rs | 10 +++++----- frame/authority-discovery/src/lib.rs | 16 +++++++--------- frame/babe/src/mock.rs | 18 ++++++++---------- frame/grandpa/src/mock.rs | 18 ++++++++---------- frame/im-online/src/lib.rs | 4 ---- frame/im-online/src/mock.rs | 19 +++++++++---------- frame/session/benchmarking/src/mock.rs | 8 +++----- frame/session/src/lib.rs | 2 +- frame/session/src/mock.rs | 17 ++++++++--------- 9 files changed, 49 insertions(+), 63 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 77ce13b12e2c4..b01051df072b7 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -404,11 +404,6 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } -impl pallet_session::historical::Trait for Runtime { - type FullIdentification = pallet_staking::Exposure; - type FullIdentificationOf = pallet_staking::ExposureOf; -} - impl pallet_session::Trait for Runtime { type Event = Event; type ValidatorId = ::AccountId; @@ -422,6 +417,11 @@ impl pallet_session::Trait for Runtime { type WeightInfo = weights::pallet_session::WeightInfo; } +impl pallet_session::historical::Trait for Runtime { + type FullIdentification = pallet_staking::Exposure; + type FullIdentificationOf = pallet_staking::ExposureOf; +} + pallet_staking_reward_curve::build! { const REWARD_CURVE: PiecewiseLinear<'static> = curve!( min_inflation: 0_025_000, diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 79ac0f26afa76..09be533474fca 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -113,26 +113,24 @@ mod tests { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } - impl pallet_session::ValidatorIdentification for Test { - type ValidatorId = AuthorityId; - type ValidatorIdOf = ConvertInto; - } - impl pallet_session::historical::FullValidatorIdentification for Test { - type FullIdentification = (); - type FullIdentificationOf = (); - } - impl pallet_session::Trait for Test { type SessionManager = (); type Keys = UintAuthorityId; type ShouldEndSession = pallet_session::PeriodicSessions; type SessionHandler = TestSessionHandler; type Event = (); + type ValidatorId = AuthorityId; + type ValidatorIdOf = ConvertInto; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; type WeightInfo = (); } + impl pallet_session::historical::Trait for Test { + type FullIdentification = (); + type FullIdentificationOf = (); + } + pub type BlockNumber = u64; parameter_types! { diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index 68c0d8dd9af1c..9f00a4ddfc3cd 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -107,17 +107,10 @@ impl_opaque_keys! { } } -impl pallet_session::ValidatorIdentification for Test { - type ValidatorId = ::AccountId; - type ValidatorIdOf = pallet_staking::StashOf; -} -impl pallet_session::historical::FullValidatorIdentification for Test { - type FullIdentification = pallet_staking::Exposure; - type FullIdentificationOf = pallet_staking::ExposureOf; -} - impl pallet_session::Trait for Test { type Event = (); + type ValidatorId = ::AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type ShouldEndSession = Babe; type NextSessionRotation = Babe; type SessionManager = pallet_session::historical::NoteHistoricalRoot; @@ -127,6 +120,11 @@ impl pallet_session::Trait for Test { type WeightInfo = (); } +impl pallet_session::historical::Trait for Test { + type FullIdentification = pallet_staking::Exposure; + type FullIdentificationOf = pallet_staking::ExposureOf; +} + parameter_types! { pub const UncleGenerations: u64 = 0; } @@ -216,7 +214,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = (); - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index c7565deac727e..d3461eec12dc4 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -121,18 +121,11 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } -impl pallet_session::ValidatorIdentification for Test { - type ValidatorId = u64; - type ValidatorIdOf = pallet_staking::StashOf; -} -impl pallet_session::historical::FullValidatorIdentification for Test { - type FullIdentification = pallet_staking::Exposure; - type FullIdentificationOf = pallet_staking::ExposureOf; -} - /// Custom `SessionHandler` since we use `TestSessionKeys` as `Keys`. impl pallet_session::Trait for Test { type Event = TestEvent; + type ValidatorId = u64; + type ValidatorIdOf = pallet_staking::StashOf; type ShouldEndSession = pallet_session::PeriodicSessions; type NextSessionRotation = pallet_session::PeriodicSessions; type SessionManager = pallet_session::historical::NoteHistoricalRoot; @@ -142,6 +135,11 @@ impl pallet_session::Trait for Test { type WeightInfo = (); } +impl pallet_session::historical::Trait for Test { + type FullIdentification = pallet_staking::Exposure; + type FullIdentificationOf = pallet_staking::ExposureOf; +} + parameter_types! { pub const UncleGenerations: u64 = 0; } @@ -231,7 +229,7 @@ parameter_types! { impl pallet_offences::Trait for Test { type Event = TestEvent; - type IdentificationTuple = pallet_session::historical::IdentificationTuple; + type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 8572040e3c22a..3a3249de585e8 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -103,10 +103,6 @@ use frame_system::offchain::{ SendTransactionTypes, SubmitTransaction, }; -// use pallet_session::{ -// historical::{FullValidatorIdentification, IdentificationTuple}, -// ValidatorIdentification, ValidatorId -// }; pub mod sr25519 { mod app_sr25519 { diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 7b9464f9b8e4a..d18538b58905e 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -146,19 +146,12 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } -impl pallet_session::ValidatorIdentification for Runtime { - type ValidatorId = u64; - type ValidatorIdOf = ConvertInto; -} -impl pallet_session::historical::FullValidatorIdentification for Runtime { - type FullIdentification = u64; - type FullIdentificationOf = ConvertInto; -} - impl pallet_session::Trait for Runtime { type ShouldEndSession = pallet_session::PeriodicSessions; type SessionManager = pallet_session::historical::NoteHistoricalRoot; type SessionHandler = (ImOnline, ); + type ValidatorId = u64; + type ValidatorIdOf = ConvertInto; type Keys = UintAuthorityId; type Event = (); type DisabledValidatorsThreshold = DisabledValidatorsThreshold; @@ -166,6 +159,11 @@ impl pallet_session::Trait for Runtime { type WeightInfo = (); } +impl pallet_session::historical::Trait for Runtime { + type FullIdentification = u64; + type FullIdentificationOf = ConvertInto; +} + parameter_types! { pub const UncleGenerations: u32 = 5; } @@ -185,7 +183,7 @@ impl Trait for Runtime { type AuthorityId = UintAuthorityId; type Event = (); type ReportUnresponsiveness = OffenceHandler; - type SessionInterface = Session; + type ValidatorSet = Historical; type SessionDuration = Period; type UnsignedPriority = UnsignedPriority; type WeightInfo = (); @@ -202,6 +200,7 @@ impl frame_system::offchain::SendTransactionTypes for Runt pub type ImOnline = Module; pub type System = frame_system::Module; pub type Session = pallet_session::Module; +pub type Historical = pallet_session::historical::Module; pub fn advance_session() { let now = System::block_number().max(1); diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index b203f8ab4cdeb..6a9cfc5f98a1b 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -94,11 +94,7 @@ impl pallet_timestamp::Trait for Test { type MinimumPeriod = MinimumPeriod; type WeightInfo = (); } -impl pallet_session::ValidatorIdentification for Test { - type ValidatorId = AccountId; - type ValidatorIdOf = pallet_staking::StashOf; -} -impl pallet_session::historical::FullValidatorIdentification for Test { +impl pallet_session::historical::Trait for Test { type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -131,6 +127,8 @@ impl pallet_session::Trait for Test { type NextSessionRotation = pallet_session::PeriodicSessions<(), ()>; type SessionHandler = TestSessionHandler; type Event = (); + type ValidatorId = AccountId; + type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); type WeightInfo = (); } diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 32f85d80df27a..28dacfdd08689 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -88,7 +88,7 @@ //! ``` //! use pallet_session as session; //! -//! fn validators() -> Vec> { +//! fn validators() -> Vec<< T as pallet_session::Trait>::ValidatorId> { //! >::validators() //! } //! # fn main(){} diff --git a/frame/session/src/mock.rs b/frame/session/src/mock.rs index dfbd6deb4ab61..1d787ac53b438 100644 --- a/frame/session/src/mock.rs +++ b/frame/session/src/mock.rs @@ -211,15 +211,6 @@ parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } -impl crate::ValidatorIdentification for Test { - type ValidatorId = u64; - type ValidatorIdOf = ConvertInto; -} -impl crate::historical::FullValidatorIdentification for Test { - type FullIdentification = u64; - type FullIdentificationOf = sp_runtime::traits::ConvertInto; -} - impl Trait for Test { type ShouldEndSession = TestShouldEndSession; #[cfg(feature = "historical")] @@ -227,6 +218,8 @@ impl Trait for Test { #[cfg(not(feature = "historical"))] type SessionManager = TestSessionManager; type SessionHandler = TestSessionHandler; + type ValidatorId = u64; + type ValidatorIdOf = ConvertInto; type Keys = MockSessionKeys; type Event = (); type DisabledValidatorsThreshold = DisabledValidatorsThreshold; @@ -234,5 +227,11 @@ impl Trait for Test { type WeightInfo = (); } +#[cfg(feature = "historical")] +impl crate::historical::Trait for Test { + type FullIdentification = u64; + type FullIdentificationOf = sp_runtime::traits::ConvertInto; +} + pub type System = frame_system::Module; pub type Session = Module; From da2d4a59dab6d94e2b1b8644e8929c23ba622a16 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 22 Oct 2020 17:13:34 +0800 Subject: [PATCH 20/33] Nits --- frame/im-online/src/lib.rs | 10 +++++----- frame/im-online/src/mock.rs | 2 +- frame/session/src/lib.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 3a3249de585e8..4473bc4fd652f 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -231,7 +231,11 @@ pub trait WeightInfo { fn validate_unsigned_and_then_heartbeat(k: u32, e: u32, ) -> Weight; } -pub type IdentificationTuple = ( +type ValidatorId = < + ::ValidatorSet as ValidatorSet<::AccountId> +>::ValidatorId; + +type IdentificationTuple = ( <::ValidatorSet as ValidatorSet<::AccountId>>::ValidatorId, <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification, ); @@ -290,10 +294,6 @@ decl_event!( } ); -type ValidatorId = < - ::ValidatorSet as ValidatorSet<::AccountId> ->::ValidatorId; - decl_storage! { trait Store for Module as ImOnline { /// The block number after which it's ok to send heartbeats in current session. diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index d18538b58905e..1274fb7c65c4a 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -183,7 +183,7 @@ impl Trait for Runtime { type AuthorityId = UintAuthorityId; type Event = (); type ReportUnresponsiveness = OffenceHandler; - type ValidatorSet = Historical; + type ValidatorSet = Historical; type SessionDuration = Period; type UnsignedPriority = UnsignedPriority; type WeightInfo = (); diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 28dacfdd08689..6bb5cf25fd005 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -88,7 +88,7 @@ //! ``` //! use pallet_session as session; //! -//! fn validators() -> Vec<< T as pallet_session::Trait>::ValidatorId> { +//! fn validators() -> Vec<::ValidatorId> { //! >::validators() //! } //! # fn main(){} From 58e9fcc1fd3205bc44f0c834e14a5bf9b24c7e65 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 22 Oct 2020 17:36:04 +0800 Subject: [PATCH 21/33] Move OneSessionHandler to sp-session --- Cargo.lock | 3 ++- frame/aura/Cargo.toml | 2 ++ frame/aura/src/lib.rs | 2 +- frame/authority-discovery/Cargo.toml | 2 ++ frame/authority-discovery/src/lib.rs | 2 +- frame/babe/src/lib.rs | 4 +-- frame/grandpa/src/lib.rs | 4 +-- frame/im-online/Cargo.toml | 4 +-- frame/im-online/src/lib.rs | 2 +- frame/session/src/lib.rs | 40 ++-------------------------- primitives/session/src/lib.rs | 35 ++++++++++++++++++++++++ 11 files changed, 51 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a40f9ee977993..9abaecdf5a7c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4251,6 +4251,7 @@ dependencies = [ "sp-inherents", "sp-io", "sp-runtime", + "sp-session", "sp-std", "sp-timestamp", ] @@ -4269,6 +4270,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-session", "sp-staking", "sp-std", ] @@ -4581,7 +4583,6 @@ dependencies = [ "frame-support", "frame-system", "pallet-authorship", - "pallet-session", "parity-scale-codec", "serde", "sp-application-crypto", diff --git a/frame/aura/Cargo.toml b/frame/aura/Cargo.toml index 27a579e0f9f8b..dd862befe14ac 100644 --- a/frame/aura/Cargo.toml +++ b/frame/aura/Cargo.toml @@ -20,6 +20,7 @@ sp-std = { version = "2.0.0", default-features = false, path = "../../primitives serde = { version = "1.0.101", optional = true } pallet-session = { version = "2.0.0", default-features = false, path = "../session" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } +sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } sp-consensus-aura = { version = "0.8.0", path = "../../primitives/consensus/aura", default-features = false } frame-system = { version = "2.0.0", default-features = false, path = "../system" } @@ -42,6 +43,7 @@ std = [ "sp-std/std", "serde", "sp-runtime/std", + "sp-session/std", "frame-support/std", "sp-consensus-aura/std", "frame-system/std", diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index ca3d1f15f421b..10b38632ef108 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -112,7 +112,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = T::AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl sp_session::OneSessionHandler for Module { type Key = T::AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) diff --git a/frame/authority-discovery/Cargo.toml b/frame/authority-discovery/Cargo.toml index 0e1db74632786..299ec6f82bb7c 100644 --- a/frame/authority-discovery/Cargo.toml +++ b/frame/authority-discovery/Cargo.toml @@ -20,6 +20,7 @@ sp-std = { version = "2.0.0", default-features = false, path = "../../primitives serde = { version = "1.0.101", optional = true } pallet-session = { version = "2.0.0", features = ["historical" ], path = "../session", default-features = false } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } +sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } @@ -38,6 +39,7 @@ std = [ "serde", "pallet-session/std", "sp-runtime/std", + "sp-session/std", "frame-support/std", "frame-system/std", ] diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 09be533474fca..274096f9d6b91 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -64,7 +64,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl sp_session::OneSessionHandler for Module { type Key = AuthorityId; fn on_genesis_session<'a, I: 'a>(authorities: I) diff --git a/frame/babe/src/lib.rs b/frame/babe/src/lib.rs index efada5f18cbf8..724adcff5aead 100644 --- a/frame/babe/src/lib.rs +++ b/frame/babe/src/lib.rs @@ -36,7 +36,7 @@ use sp_runtime::{ traits::{Hash, IsMember, One, SaturatedConversion, Saturating}, ConsensusEngineId, KeyTypeId, }; -use sp_session::{GetSessionNumber, GetValidatorCount}; +use sp_session::{GetSessionNumber, GetValidatorCount, OneSessionHandler}; use sp_std::{prelude::*, result}; use sp_timestamp::OnTimestampSet; @@ -716,7 +716,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl OneSessionHandler for Module { type Key = AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) diff --git a/frame/grandpa/src/lib.rs b/frame/grandpa/src/lib.rs index d4612e1760057..fa48a7137e9c7 100644 --- a/frame/grandpa/src/lib.rs +++ b/frame/grandpa/src/lib.rs @@ -49,7 +49,7 @@ use sp_runtime::{ traits::Zero, DispatchResult, KeyTypeId, }; -use sp_session::{GetSessionNumber, GetValidatorCount}; +use sp_session::{GetSessionNumber, GetValidatorCount, OneSessionHandler}; use sp_staking::SessionIndex; mod equivocation; @@ -587,7 +587,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = AuthorityId; } -impl pallet_session::OneSessionHandler for Module +impl OneSessionHandler for Module where T: pallet_session::Trait { type Key = AuthorityId; diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 226c03422d1c3..b7e56d59b0fa1 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -19,7 +19,6 @@ codec = { package = "parity-scale-codec", version = "1.3.4", default-features = sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } serde = { version = "1.0.101", optional = true } -pallet-session = { version = "2.0.0", default-features = false, path = "../session" } sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } @@ -30,7 +29,7 @@ frame-system = { version = "2.0.0", default-features = false, path = "../system" frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } [features] -default = ["std", "pallet-session/historical"] +default = ["std"] std = [ "sp-application-crypto/std", "pallet-authorship/std", @@ -38,7 +37,6 @@ std = [ "sp-core/std", "sp-std/std", "serde", - "pallet-session/std", "sp-io/std", "sp-runtime/std", "sp-staking/std", diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 4473bc4fd652f..ec93a4c1958d7 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -635,7 +635,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = T::AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl sp_session::OneSessionHandler for Module { type Key = T::AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 6bb5cf25fd005..d4ded0e2ddc20 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -101,8 +101,7 @@ #![cfg_attr(not(feature = "std"), no_std)] 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::{ @@ -251,45 +250,10 @@ pub trait SessionHandler { fn on_disabled(validator_index: usize); } -// TODO [ToDr] This should be moved out to primitives/session -/// A session handler for specific key type. -pub trait OneSessionHandler: BoundToRuntimeAppPublic { - /// The key type expected. - type Key: Decode + Default + RuntimeAppPublic; - - fn on_genesis_session<'a, I: 'a>(validators: I) - where I: Iterator, 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, 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 SessionHandler for Tuple { - for_tuples!( where #( Tuple: OneSessionHandler )* ); + for_tuples!( where #( Tuple: sp_session::OneSessionHandler )* ); for_tuples!( const KEY_TYPE_IDS: &'static [KeyTypeId] = &[ #( ::ID ),* ]; diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index 67d88f5261a97..dcf634e1be935 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -29,6 +29,7 @@ use sp_api::ProvideRuntimeApi; use sp_core::RuntimeDebug; use sp_core::crypto::KeyTypeId; use sp_runtime::traits::Convert; +use sp_runtime::{RuntimeAppPublic, BoundToRuntimeAppPublic}; use sp_staking::SessionIndex; use sp_std::vec::Vec; @@ -135,6 +136,40 @@ pub trait ValidatorSetWithIdentification: ValidatorSet { type IdentificationOf: Convert>; } +/// A session handler for specific key type. +pub trait OneSessionHandler: BoundToRuntimeAppPublic { + /// The key type expected. + type Key: Decode + Default + RuntimeAppPublic; + + fn on_genesis_session<'a, I: 'a>(validators: I) + where I: Iterator, 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, 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); +} + /// Generate the initial session keys with the given seeds, at the given block and store them in /// the client's keystore. #[cfg(feature = "std")] From 7066c131f7443149d372d3be998babccd6ca09da Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 22 Oct 2020 19:01:20 +0800 Subject: [PATCH 22/33] Fix tests --- Cargo.lock | 3 +++ frame/authority-discovery/src/lib.rs | 2 +- frame/grandpa/src/tests.rs | 2 +- frame/im-online/Cargo.toml | 3 +++ frame/im-online/src/lib.rs | 6 ++--- frame/offences/benchmarking/Cargo.toml | 2 ++ frame/offences/benchmarking/src/lib.rs | 36 +++++++++++++++++++++---- frame/offences/benchmarking/src/mock.rs | 4 +++ frame/session/src/tests.rs | 1 + frame/staking/Cargo.toml | 1 + frame/staking/src/mock.rs | 2 +- 11 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9abaecdf5a7c8..3945d8d5fc870 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4583,6 +4583,7 @@ dependencies = [ "frame-support", "frame-system", "pallet-authorship", + "pallet-session", "parity-scale-codec", "serde", "sp-application-crypto", @@ -4707,6 +4708,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-session", "sp-staking", "sp-std", ] @@ -4870,6 +4872,7 @@ dependencies = [ "sp-io", "sp-npos-elections", "sp-runtime", + "sp-session", "sp-staking", "sp-std", "sp-storage", diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 274096f9d6b91..4587c18f191ba 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -238,7 +238,7 @@ mod tests { let mut externalities = TestExternalities::new(t); externalities.execute_with(|| { - use pallet_session::OneSessionHandler; + use sp_session::OneSessionHandler; AuthorityDiscovery::on_genesis_session( first_authorities.iter().map(|id| (id, id.clone())) diff --git a/frame/grandpa/src/tests.rs b/frame/grandpa/src/tests.rs index 4916808fe000f..b78fe782f30c3 100644 --- a/frame/grandpa/src/tests.rs +++ b/frame/grandpa/src/tests.rs @@ -29,7 +29,7 @@ use frame_support::{ weights::{GetDispatchInfo, Pays}, }; use frame_system::{EventRecord, Phase}; -use pallet_session::OneSessionHandler; +use sp_session::OneSessionHandler; use sp_core::H256; use sp_keyring::Ed25519Keyring; use sp_runtime::testing::Digest; diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index b7e56d59b0fa1..f6c9cabee466b 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -28,6 +28,9 @@ frame-system = { version = "2.0.0", default-features = false, path = "../system" frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +[dev-dependencies] +pallet-session = { version = "2.0.0", path = "../session" } + [features] default = ["std"] std = [ diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index ec93a4c1958d7..9ddf29349fc2f 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -231,12 +231,12 @@ pub trait WeightInfo { fn validate_unsigned_and_then_heartbeat(k: u32, e: u32, ) -> Weight; } -type ValidatorId = < +pub type ValidatorId = < ::ValidatorSet as ValidatorSet<::AccountId> >::ValidatorId; -type IdentificationTuple = ( - <::ValidatorSet as ValidatorSet<::AccountId>>::ValidatorId, +pub type IdentificationTuple = ( + ValidatorId, <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification, ); diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml index 7a95cebc4fb21..19ddaabd917d7 100644 --- a/frame/offences/benchmarking/Cargo.toml +++ b/frame/offences/benchmarking/Cargo.toml @@ -26,6 +26,7 @@ pallet-session = { version = "2.0.0", default-features = false, path = "../../se pallet-staking = { version = "2.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../../primitives/staking" } +sp-session = { version = "2.0.0", default-features = false, path = "../../../primitives/session" } sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } [dev-dependencies] @@ -50,6 +51,7 @@ std = [ "pallet-staking/std", "sp-runtime/std", "sp-staking/std", + "sp-session/std", "sp-std/std", "codec/std", ] diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 2e3f17313b2c6..b52e2fbe86146 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -175,6 +175,32 @@ fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< Ok((id_tuples, offenders)) } +fn make_offenders_im_online(num_offenders: u32, num_nominators: u32) -> Result< + (Vec>, Vec>), + &'static str +> { + Staking::::new_session(0); + + let mut offenders = vec![]; + for i in 0 .. num_offenders { + let offender = create_offender::(i + 1, num_nominators)?; + offenders.push(offender); + } + + Staking::::start_session(0); + + let id_tuples = offenders.iter() + .map(|offender| + <::ValidatorSet as sp_session::ValidatorSet>::ValidatorIdOf::convert(offender.controller.clone()) + .expect("failed to get validator id from account id")) + .map(|validator_id| + <::ValidatorSet as sp_session::ValidatorSetWithIdentification>::IdentificationOf::convert(validator_id.clone()) + .map(|full_id| (validator_id, full_id)) + .expect("failed to convert validator id to full identification")) + .collect::>>(); + Ok((id_tuples, offenders)) +} + #[cfg(test)] fn check_events::Event>>(expected: I) { let events = System::::events() .into_iter() @@ -221,7 +247,7 @@ benchmarks! { // make sure reporters actually get rewarded Staking::::set_slash_reward_fraction(Perbill::one()); - let (offenders, raw_offenders) = make_offenders::(o, n)?; + let (offenders, raw_offenders) = make_offenders_im_online::(o, n)?; let keys = ImOnline::::keys(); let validator_set_count = keys.len() as u32; @@ -235,10 +261,10 @@ benchmarks! { }; assert_eq!(System::::event_count(), 0); }: { - //let _ = ::ReportUnresponsiveness::report_offence( - // reporters.clone(), - // offence - //); + let _ = ::ReportUnresponsiveness::report_offence( + reporters.clone(), + offence + ); } verify { diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 527e0ede81ab9..015beb4676d10 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -29,6 +29,7 @@ use sp_runtime::{ traits::{IdentityLookup, Block as BlockT}, testing::{Header, UintAuthorityId}, }; +use pallet_session::historical as pallet_session_historical; type AccountId = u64; @@ -132,6 +133,7 @@ impl pallet_session::Trait 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, @@ -177,6 +179,7 @@ impl pallet_staking::Trait for Test { impl pallet_im_online::Trait for Test { type AuthorityId = UintAuthorityId; type Event = Event; + type ValidatorSet = Historical; type SessionDuration = Period; type ReportUnresponsiveness = Offences; type UnsignedPriority = (); @@ -216,6 +219,7 @@ frame_support::construct_runtime!( Session: pallet_session::{Module, Call, Storage, Event, Config}, ImOnline: pallet_im_online::{Module, Call, Storage, Event, ValidateUnsigned, Config}, Offences: pallet_offences::{Module, Call, Storage, Event}, + Historical: pallet_session_historical::{Module}, } ); diff --git a/frame/session/src/tests.rs b/frame/session/src/tests.rs index 75def78046beb..04544c916f242 100644 --- a/frame/session/src/tests.rs +++ b/frame/session/src/tests.rs @@ -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; diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index 88b8c1270a4e1..5df672b3a7f5f 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -35,6 +35,7 @@ rand_chacha = { version = "0.2", default-features = false, optional = true } sp-core = { version = "2.0.0", path = "../../primitives/core" } sp-storage = { version = "2.0.0", path = "../../primitives/storage" } sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } +sp-session = { version = "2.0.0", path = "../../primitives/session" } pallet-balances = { version = "2.0.0", path = "../balances" } pallet-timestamp = { version = "2.0.0", path = "../timestamp" } pallet-staking-reward-curve = { version = "2.0.0", path = "../staking/reward-curve" } diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 055ebb9730805..cc80ae545d342 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -57,7 +57,7 @@ thread_local! { /// Another session handler struct to test on_disabled. pub struct OtherSessionHandler; -impl pallet_session::OneSessionHandler for OtherSessionHandler { +impl sp_session::OneSessionHandler for OtherSessionHandler { type Key = UintAuthorityId; fn on_genesis_session<'a, I: 'a>(_: I) From 1848bd74462984d325e1a5347402407b6708977d Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 22 Oct 2020 22:27:16 +0800 Subject: [PATCH 23/33] Add some docs --- frame/im-online/src/lib.rs | 7 +++++-- primitives/session/src/lib.rs | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 9ddf29349fc2f..0e2c2f4dd3a56 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -258,14 +258,17 @@ pub trait Trait: /// there is a chance the authority will produce a block and they won't be necessary. type SessionDuration: Get; + /// A type for retrieving the validators supposed to be online in a session. + /// + /// This is used for decoupling the pallet-session dependency since the user of this + /// module might have their own defintion for the set of expected online validators + /// in a session. type ValidatorSet: ValidatorSetWithIdentification; /// A type that gives us the ability to submit unresponsiveness offence reports. type ReportUnresponsiveness: ReportOffence< Self::AccountId, - // TODO [ToDr] This deserves a typedef (IdentificationTuple), but not the one in - // session module - we need it decoupled. IdentificationTuple, UnresponsivenessOffence>, >; diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index dcf634e1be935..b7ff116888e0d 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -131,6 +131,7 @@ pub trait ValidatorSet { fn validators() -> Vec; } +/// `ValidatorSet` combined with identification type for pallet-session-historical module. pub trait ValidatorSetWithIdentification: ValidatorSet { type Identification: codec::Codec + codec::EncodeLike + Clone + Eq + sp_std::fmt::Debug; type IdentificationOf: Convert>; From 650f27996077fce23ed48acd503e321adbfa026e Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 23 Oct 2020 10:51:27 +0800 Subject: [PATCH 24/33] . --- frame/im-online/src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 0e2c2f4dd3a56..d62aaf3f0ef0b 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -240,10 +240,7 @@ pub type IdentificationTuple = ( <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification, ); -pub trait Trait: - SendTransactionTypes> - + frame_system::Trait -{ +pub trait Trait: SendTransactionTypes> + frame_system::Trait { /// The identifier type for an authority. type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + Ord; From ee81bf1da3bb751410dc16540f1500aa3c8efdb2 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 23 Oct 2020 10:59:39 +0800 Subject: [PATCH 25/33] Fix typo --- frame/offences/benchmarking/src/mock.rs | 1 - primitives/session/src/lib.rs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 015beb4676d10..16c3c0475ac29 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -31,7 +31,6 @@ use sp_runtime::{ }; use pallet_session::historical as pallet_session_historical; - type AccountId = u64; type AccountIndex = u32; type BlockNumber = u64; diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index b7ff116888e0d..c935ab0a614c9 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -108,9 +108,9 @@ impl GetValidatorCount for MembershipProof { } } -/// Trait for retrieving the session info needed for online node inspection. +/// A trait for online node inspection in a session. /// -/// This trait is used for decouple the pallet-session dependency from im-online +/// This trait is used to decouple the pallet-session dependency from im-online /// module so that the user of im-online & offences modules can pass any list of /// validators that are considered to be online in each session, particularly useful /// for the Substrate-based projects having their own staking implementation From ff94e6b3223a062aab560bf0fbfc41f6070e1e48 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Wed, 28 Oct 2020 11:05:05 +0800 Subject: [PATCH 26/33] Rename to ValidatorSet::session_index() --- frame/im-online/src/lib.rs | 22 ++++++++++++---------- frame/offences/benchmarking/src/lib.rs | 12 +++++++----- frame/session/src/historical/mod.rs | 2 +- frame/session/src/lib.rs | 2 +- primitives/session/src/lib.rs | 2 +- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 12dfa4fc121cc..0c8410ec0423c 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -227,10 +227,12 @@ pub struct Heartbeat pub validators_len: u32, } +/// A type for representing the validator id in a session. pub type ValidatorId = < ::ValidatorSet as ValidatorSet<::AccountId> >::ValidatorId; +/// A tuple of (ValidatorId, Identification) where `Identification` is the full identification of `ValidatorId`. pub type IdentificationTuple = ( ValidatorId, <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification, @@ -254,7 +256,7 @@ pub trait Trait: SendTransactionTypes> + frame_system::Trait { /// A type for retrieving the validators supposed to be online in a session. /// /// This is used for decoupling the pallet-session dependency since the user of this - /// module might have their own defintion for the set of expected online validators + /// module might have their own definition for the set of expected online validators /// in a session. type ValidatorSet: ValidatorSetWithIdentification; @@ -361,7 +363,7 @@ decl_module! { ) { ensure_none(origin)?; - let current_session = T::ValidatorSet::current_index(); + let current_session = T::ValidatorSet::session_index(); let exists = ::contains_key( ¤t_session, &heartbeat.authority_index @@ -442,7 +444,7 @@ impl Module { } fn is_online_aux(authority_index: AuthIndex, authority: &ValidatorId) -> bool { - let current_session = T::ValidatorSet::current_index(); + let current_session = T::ValidatorSet::session_index(); ::contains_key(¤t_session, &authority_index) || >::get( @@ -454,13 +456,13 @@ impl Module { /// Returns `true` if a heartbeat has been received for the authority at `authority_index` in /// the authorities series, during the current session. Otherwise `false`. pub fn received_heartbeat_in_current_session(authority_index: AuthIndex) -> bool { - let current_session = T::ValidatorSet::current_index(); + let current_session = T::ValidatorSet::session_index(); ::contains_key(¤t_session, &authority_index) } /// Note that the given authority has authored a block in the current session. fn note_authorship(author: ValidatorId) { - let current_session = T::ValidatorSet::current_index(); + let current_session = T::ValidatorSet::session_index(); >::mutate( ¤t_session, @@ -477,7 +479,7 @@ impl Module { return Err(OffchainErr::TooEarly(heartbeat_after)) } - let session_index = T::ValidatorSet::current_index(); + let session_index = T::ValidatorSet::session_index(); let validators_len = T::ValidatorSet::validators().len() as u32; Ok(Self::local_authority_keys() @@ -656,7 +658,7 @@ impl sp_session::OneSessionHandler for Module { } fn on_before_session_ending() { - let session_index = T::ValidatorSet::current_index(); + let session_index = T::ValidatorSet::session_index(); let keys = Keys::::get(); let current_validators = T::ValidatorSet::validators(); @@ -672,8 +674,8 @@ impl sp_session::OneSessionHandler for Module { // Remove all received heartbeats and number of authored blocks from the // current session, they have already been processed and won't be needed // anymore. - ::remove_prefix(&T::ValidatorSet::current_index()); - >::remove_prefix(&T::ValidatorSet::current_index()); + ::remove_prefix(&T::ValidatorSet::session_index()); + >::remove_prefix(&T::ValidatorSet::session_index()); if offenders.is_empty() { Self::deposit_event(RawEvent::AllGood); @@ -710,7 +712,7 @@ impl frame_support::unsigned::ValidateUnsigned for Module { } // check if session index from heartbeat is recent - let current_session = T::ValidatorSet::current_index(); + let current_session = T::ValidatorSet::session_index(); if heartbeat.session_index != current_session { return InvalidTransaction::Stale.into(); } diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 6189e8348c6bb..f0b8d41be9737 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -190,11 +190,13 @@ fn make_offenders_im_online(num_offenders: u32, num_nominators: u32) - Staking::::start_session(0); let id_tuples = offenders.iter() - .map(|offender| - <::ValidatorSet as sp_session::ValidatorSet>::ValidatorIdOf::convert(offender.controller.clone()) - .expect("failed to get validator id from account id")) - .map(|validator_id| - <::ValidatorSet as sp_session::ValidatorSetWithIdentification>::IdentificationOf::convert(validator_id.clone()) + .map(|offender| < + ::ValidatorSet as sp_session::ValidatorSet + >::ValidatorIdOf::convert(offender.controller.clone()) + .expect("failed to get validator id from account id")) + .map(|validator_id| < + ::ValidatorSet as sp_session::ValidatorSetWithIdentification + >::IdentificationOf::convert(validator_id.clone()) .map(|full_id| (validator_id, full_id)) .expect("failed to convert validator id to full identification")) .collect::>>(); diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index d7894a344012e..37b9f98a79279 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -106,7 +106,7 @@ impl sp_session::ValidatorSet for Module { type ValidatorId = T::ValidatorId; type ValidatorIdOf = T::ValidatorIdOf; - fn current_index() -> sp_staking::SessionIndex { + fn session_index() -> sp_staking::SessionIndex { super::Module::::current_index() } diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 72938e59005ea..1602d56f6b976 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -740,7 +740,7 @@ impl sp_session::ValidatorSet for Module { type ValidatorId = T::ValidatorId; type ValidatorIdOf = T::ValidatorIdOf; - fn current_index() -> sp_staking::SessionIndex { + fn session_index() -> sp_staking::SessionIndex { Module::::current_index() } diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index c935ab0a614c9..5d69a00ff7aff 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -123,7 +123,7 @@ pub trait ValidatorSet { type ValidatorIdOf: Convert>; /// Returns current session index. - fn current_index() -> SessionIndex; + fn session_index() -> SessionIndex; /// Returns all the validators ought to be online in a session. /// From 7d5cceb1c917b488ade0038bffd61b21e2c738d4 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Wed, 28 Oct 2020 11:47:36 +0800 Subject: [PATCH 27/33] Add some more docs --- primitives/session/src/lib.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index 5d69a00ff7aff..b0e7b771156d6 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -116,10 +116,11 @@ impl GetValidatorCount for MembershipProof { /// for the Substrate-based projects having their own staking implementation /// instead of using pallet-staking directly. pub trait ValidatorSet { - // TODO [ToDr] This could use `frame_support::Parameter` instead,although don't know if such - // import is legal. + /// Type for representing validator id in a session. + /// + /// This could be `frame_support::Parameter`, but such import is not allowed in primitives. type ValidatorId: codec::Codec + codec::EncodeLike + Clone + Eq + sp_std::fmt::Debug; - // TODO [ToDr] This is most likely not needed along with `AccountId` + /// A type for converting `AccountId` to `ValidatorId`. type ValidatorIdOf: Convert>; /// Returns current session index. @@ -133,7 +134,9 @@ pub trait ValidatorSet { /// `ValidatorSet` combined with identification type for pallet-session-historical module. pub trait ValidatorSetWithIdentification: ValidatorSet { + /// Full identification of `ValidatorId`. type Identification: codec::Codec + codec::EncodeLike + Clone + Eq + sp_std::fmt::Debug; + /// A type for converting `ValidatorId` to `Identification`. type IdentificationOf: Convert>; } @@ -142,6 +145,10 @@ pub trait OneSessionHandler: BoundToRuntimeAppPublic { /// The key type expected. type Key: Decode + Default + RuntimeAppPublic; + /// The given validator set will be used for the genesis session. + /// It is guaranteed that the given validator set will also be used + /// for the second session, therefore the first call to `on_new_session` + /// should provide the same validator set. fn on_genesis_session<'a, I: 'a>(validators: I) where I: Iterator, ValidatorId: 'a; From de089374afe0da317172b221eb7c0613ae45b4c7 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Wed, 28 Oct 2020 12:47:25 +0800 Subject: [PATCH 28/33] . --- frame/offences/benchmarking/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index f0b8d41be9737..8e1f60a3730c7 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -240,7 +240,7 @@ benchmarks! { let n in 0 .. MAX_NOMINATORS.min(MAX_NOMINATIONS as u32); // Make r reporters - let mut reporters: Vec = vec![]; + let mut reporters = vec![]; for i in 0 .. r { let reporter = account("reporter", i, SEED); reporters.push(reporter); From fe9e17a3fc325fd8fb18de0930abe63719ca492a Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Mon, 2 Nov 2020 13:28:42 +0800 Subject: [PATCH 29/33] Remove extra empty line --- frame/offences/benchmarking/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 8e1f60a3730c7..7d36a90d5eb9c 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -268,7 +268,6 @@ benchmarks! { offence ); } - verify { // make sure the report was not deferred assert!(Offences::::deferred_offences().is_empty()); From ea357a378bd75c1c27563bb047fbb088139f39f1 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Wed, 20 Jan 2021 14:12:41 +0800 Subject: [PATCH 30/33] Fix line width check . --- frame/im-online/src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index d1b4c27791289..6549c795dc29b 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -235,7 +235,8 @@ pub type ValidatorId = < /// A tuple of (ValidatorId, Identification) where `Identification` is the full identification of `ValidatorId`. pub type IdentificationTuple = ( ValidatorId, - <::ValidatorSet as ValidatorSetWithIdentification<::AccountId>>::Identification, + <::ValidatorSet as + ValidatorSetWithIdentification<::AccountId>>::Identification, ); pub trait Config: SendTransactionTypes> + frame_system::Config { @@ -415,7 +416,9 @@ type OffchainResult = Result::B /// Keep track of number of authored blocks per authority, uncles are counted as /// well since they're a valid proof of being online. -impl pallet_authorship::EventHandler, T::BlockNumber> for Module +impl< + T: Config + pallet_authorship::Config, +> pallet_authorship::EventHandler, T::BlockNumber> for Module { fn note_author(author: ValidatorId) { Self::note_authorship(author); From 12ff7097b8717e71d2223700a1873fce3d2f670c Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Tue, 2 Feb 2021 23:42:37 +0800 Subject: [PATCH 31/33] Apply suggestions from code review --- Cargo.lock | 1 + frame/aura/src/lib.rs | 4 +- frame/authority-discovery/src/lib.rs | 6 +-- frame/babe/src/lib.rs | 4 +- frame/grandpa/src/lib.rs | 4 +- frame/grandpa/src/tests.rs | 3 +- frame/im-online/src/lib.rs | 11 ++-- frame/offences/benchmarking/src/lib.rs | 6 +-- frame/session/src/historical/mod.rs | 10 ++-- frame/session/src/lib.rs | 5 +- frame/staking/src/mock.rs | 4 +- frame/support/Cargo.toml | 2 + frame/support/src/traits.rs | 71 ++++++++++++++++++++++++-- primitives/session/src/lib.rs | 71 -------------------------- 14 files changed, 97 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72b3180f7a2eb..0577f6333a064 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1628,6 +1628,7 @@ dependencies = [ "sp-inherents", "sp-io", "sp-runtime", + "sp-staking", "sp-state-machine", "sp-std", "sp-tracing", diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 697b6eb9a83f9..1e795403d9d4b 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -46,7 +46,7 @@ use sp_std::{result, prelude::*}; use codec::{Encode, Decode}; -use frame_support::{Parameter, traits::{Get, FindAuthor}, ConsensusEngineId}; +use frame_support::{Parameter, traits::{Get, FindAuthor, OneSessionHandler}, ConsensusEngineId}; use sp_runtime::{ RuntimeAppPublic, traits::{SaturatedConversion, Saturating, Zero, Member, IsMember}, generic::DigestItem, @@ -144,7 +144,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Pallet { type Public = T::AuthorityId; } -impl sp_session::OneSessionHandler for Module { +impl OneSessionHandler for Module { type Key = T::AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 1784a5d3bd24f..cc3f41f59ed89 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -24,7 +24,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use sp_std::prelude::*; -use frame_support::{decl_module, decl_storage}; +use frame_support::{decl_module, decl_storage, traits::OneSessionHandler}; use sp_authority_discovery::AuthorityId; /// The module's config trait. @@ -85,7 +85,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = AuthorityId; } -impl sp_session::OneSessionHandler for Module { +impl OneSessionHandler for Module { type Key = AuthorityId; fn on_genesis_session<'a, I: 'a>(authorities: I) @@ -265,7 +265,7 @@ mod tests { let mut externalities = TestExternalities::new(t); externalities.execute_with(|| { - use sp_session::OneSessionHandler; + use frame_support::traits::OneSessionHandler; AuthorityDiscovery::on_genesis_session( first_authorities.iter().map(|id| (id, id.clone())) diff --git a/frame/babe/src/lib.rs b/frame/babe/src/lib.rs index 190e72c1a084a..0afa0e1d0980f 100644 --- a/frame/babe/src/lib.rs +++ b/frame/babe/src/lib.rs @@ -25,7 +25,7 @@ use codec::{Decode, Encode}; use frame_support::{ decl_error, decl_module, decl_storage, dispatch::DispatchResultWithPostInfo, - traits::{FindAuthor, Get, KeyOwnerProofSystem, Randomness as RandomnessT}, + traits::{FindAuthor, Get, KeyOwnerProofSystem, OneSessionHandler, Randomness as RandomnessT}, weights::{Pays, Weight}, Parameter, }; @@ -36,7 +36,7 @@ use sp_runtime::{ traits::{Hash, IsMember, One, SaturatedConversion, Saturating}, ConsensusEngineId, KeyTypeId, }; -use sp_session::{GetSessionNumber, GetValidatorCount, OneSessionHandler}; +use sp_session::{GetSessionNumber, GetValidatorCount}; use sp_std::{prelude::*, result}; use sp_timestamp::OnTimestampSet; diff --git a/frame/grandpa/src/lib.rs b/frame/grandpa/src/lib.rs index 685ca9d9effe8..b68624df7b5dc 100644 --- a/frame/grandpa/src/lib.rs +++ b/frame/grandpa/src/lib.rs @@ -41,7 +41,7 @@ use fg_primitives::{ }; use frame_support::{ decl_error, decl_event, decl_module, decl_storage, dispatch::DispatchResultWithPostInfo, - storage, traits::KeyOwnerProofSystem, weights::{Pays, Weight}, Parameter, + storage, traits::{OneSessionHandler, KeyOwnerProofSystem}, weights::{Pays, Weight}, Parameter, }; use frame_system::{ensure_none, ensure_root, ensure_signed}; use sp_runtime::{ @@ -49,7 +49,7 @@ use sp_runtime::{ traits::Zero, DispatchResult, KeyTypeId, }; -use sp_session::{GetSessionNumber, GetValidatorCount, OneSessionHandler}; +use sp_session::{GetSessionNumber, GetValidatorCount}; use sp_staking::SessionIndex; mod equivocation; diff --git a/frame/grandpa/src/tests.rs b/frame/grandpa/src/tests.rs index bf9eea9d4944b..4870bf606286c 100644 --- a/frame/grandpa/src/tests.rs +++ b/frame/grandpa/src/tests.rs @@ -25,11 +25,10 @@ use codec::{Decode, Encode}; use fg_primitives::ScheduledChange; use frame_support::{ assert_err, assert_ok, - traits::{Currency, OnFinalize}, + traits::{Currency, OnFinalize, OneSessionHandler}, weights::{GetDispatchInfo, Pays}, }; use frame_system::{EventRecord, Phase}; -use sp_session::OneSessionHandler; use sp_core::H256; use sp_keyring::Ed25519Keyring; use sp_runtime::testing::Digest; diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 6549c795dc29b..bd597acfb1ed5 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -92,10 +92,9 @@ use sp_staking::{ SessionIndex, offence::{ReportOffence, Offence, Kind}, }; -use sp_session::{ValidatorSet, ValidatorSetWithIdentification}; use frame_support::{ decl_module, decl_event, decl_storage, Parameter, debug, decl_error, - traits::Get, + traits::{Get, ValidatorSet, ValidatorSetWithIdentification, OneSessionHandler}, }; use frame_system::ensure_none; use frame_system::offchain::{ @@ -255,10 +254,6 @@ pub trait Config: SendTransactionTypes> + frame_system::Config { type SessionDuration: Get; /// A type for retrieving the validators supposed to be online in a session. - /// - /// This is used for decoupling the pallet-session dependency since the user of this - /// module might have their own definition for the set of expected online validators - /// in a session. type ValidatorSet: ValidatorSetWithIdentification; /// A type that gives us the ability to submit unresponsiveness offence reports. @@ -483,7 +478,7 @@ impl Module { } let session_index = T::ValidatorSet::session_index(); - let validators_len = T::ValidatorSet::validators().len() as u32; + let validators_len = Keys::::decode_len().unwrap_or_default() as u32; Ok(Self::local_authority_keys() .map(move |(authority_index, key)| @@ -636,7 +631,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = T::AuthorityId; } -impl sp_session::OneSessionHandler for Module { +impl OneSessionHandler for Module { type Key = T::AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 750119a1d35e5..57672f13ed711 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -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}; @@ -192,11 +192,11 @@ fn make_offenders_im_online(num_offenders: u32, num_nominators: u32) let id_tuples = offenders.iter() .map(|offender| < - ::ValidatorSet as sp_session::ValidatorSet + ::ValidatorSet as ValidatorSet >::ValidatorIdOf::convert(offender.controller.clone()) .expect("failed to get validator id from account id")) .map(|validator_id| < - ::ValidatorSet as sp_session::ValidatorSetWithIdentification + ::ValidatorSet as ValidatorSetWithIdentification >::IdentificationOf::convert(validator_id.clone()) .map(|full_id| (validator_id, full_id)) .expect("failed to convert validator id to full identification")) diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index 85b397baed595..9b4d2704cf456 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -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,7 +104,7 @@ impl Module { } } -impl sp_session::ValidatorSet for Module { +impl ValidatorSet for Module { type ValidatorId = T::ValidatorId; type ValidatorIdOf = T::ValidatorIdOf; @@ -115,7 +117,7 @@ impl sp_session::ValidatorSet for Module { } } -impl sp_session::ValidatorSetWithIdentification for Module { +impl ValidatorSetWithIdentification for Module { type Identification = T::FullIdentification; type IdentificationOf = T::FullIdentificationOf; } diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index db65cf8ee15b5..0793d5e74b988 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -123,6 +123,7 @@ 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, @@ -259,7 +260,7 @@ pub trait SessionHandler { #[impl_trait_for_tuples::impl_for_tuples(1, 30)] #[tuple_types_no_default_trait_bound] impl SessionHandler for Tuple { - for_tuples!( where #( Tuple: sp_session::OneSessionHandler )* ); + for_tuples!( where #( Tuple: OneSessionHandler )* ); for_tuples!( const KEY_TYPE_IDS: &'static [KeyTypeId] = &[ #( ::ID ),* ]; @@ -796,7 +797,7 @@ impl Module { } } -impl sp_session::ValidatorSet for Module { +impl ValidatorSet for Module { type ValidatorId = T::ValidatorId; type ValidatorIdOf = T::ValidatorIdOf; diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index a9129fac266ed..0eb77e7c14ac1 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -21,7 +21,7 @@ use crate::*; use crate as staking; use frame_support::{ assert_ok, parameter_types, - traits::{Currency, FindAuthor, Get, OnFinalize, OnInitialize}, + traits::{Currency, FindAuthor, Get, OnFinalize, OnInitialize, OneSessionHandler}, weights::{constants::RocksDbWeight, Weight}, IterableStorageMap, StorageDoubleMap, StorageMap, StorageValue, }; @@ -53,7 +53,7 @@ thread_local! { /// Another session handler struct to test on_disabled. pub struct OtherSessionHandler; -impl sp_session::OneSessionHandler for OtherSessionHandler { +impl OneSessionHandler for OtherSessionHandler { type Key = UintAuthorityId; fn on_genesis_session<'a, I: 'a>(_: I) diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index 3354f17d27c90..294e4c1574a32 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -24,6 +24,7 @@ sp-tracing = { version = "2.0.0", default-features = false, path = "../../primit sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } sp-arithmetic = { version = "2.0.0", default-features = false, path = "../../primitives/arithmetic" } sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } +sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } frame-support-procedural = { version = "2.0.1", default-features = false, path = "./procedural" } paste = "0.1.6" once_cell = { version = "1", default-features = false, optional = true } @@ -52,6 +53,7 @@ std = [ "sp-arithmetic/std", "frame-metadata/std", "sp-inherents/std", + "sp-staking/std", "sp-state-machine", "frame-support-procedural/std", ] diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 2888abc306b3d..c52aa60c20b15 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -23,13 +23,15 @@ use sp_std::{prelude::*, result, marker::PhantomData, ops::Div, fmt::Debug}; use codec::{FullCodec, Codec, Encode, Decode, EncodeLike}; use sp_core::u32_trait::Value as U32; use sp_runtime::{ - RuntimeDebug, ConsensusEngineId, DispatchResult, DispatchError, + RuntimeAppPublic, RuntimeDebug, BoundToRuntimeAppPublic, + ConsensusEngineId, DispatchResult, DispatchError, traits::{ - MaybeSerializeDeserialize, AtLeast32Bit, Saturating, TrailingZeroInput, Bounded, Zero, - BadOrigin, AtLeast32BitUnsigned, UniqueSaturatedFrom, UniqueSaturatedInto, - SaturatedConversion, StoredMapError, + MaybeSerializeDeserialize, AtLeast32Bit, Saturating, TrailingZeroInput, Bounded, Zero, + BadOrigin, AtLeast32BitUnsigned, Convert, UniqueSaturatedFrom, UniqueSaturatedInto, + SaturatedConversion, StoredMapError, }, }; +use sp_staking::SessionIndex; use crate::dispatch::Parameter; use crate::storage::StorageMap; use crate::weights::Weight; @@ -40,6 +42,67 @@ use impl_trait_for_tuples::impl_for_tuples; #[doc(hidden)] pub use sp_std::{mem::{swap, take}, cell::RefCell, vec::Vec, boxed::Box}; +/// A trait for online node inspection in a session. +/// +/// Something that can give information about the current validator set. +pub trait ValidatorSet { + /// Type for representing validator id in a session. + type ValidatorId: Parameter; + /// A type for converting `AccountId` to `ValidatorId`. + type ValidatorIdOf: Convert>; + + /// Returns current session index. + fn session_index() -> SessionIndex; + + /// Returns the active set of validators. + fn validators() -> Vec; +} + +/// [`ValidatorSet`] combined with an identification. +pub trait ValidatorSetWithIdentification: ValidatorSet { + /// Full identification of `ValidatorId`. + type Identification: Parameter; + /// A type for converting `ValidatorId` to `Identification`. + type IdentificationOf: Convert>; +} + +/// A session handler for specific key type. +pub trait OneSessionHandler: BoundToRuntimeAppPublic { + /// The key type expected. + type Key: Decode + Default + RuntimeAppPublic; + + /// The given validator set will be used for the genesis session. + /// It is guaranteed that the given validator set will also be used + /// for the second session, therefore the first call to `on_new_session` + /// should provide the same validator set. + fn on_genesis_session<'a, I: 'a>(validators: I) + where I: Iterator, 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, 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); +} + /// Simple trait for providing a filter over a reference to some type. pub trait Filter { /// Determine if a given value should be allowed through the filter (returns `true`) or not. diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index 4c9462d98306b..dcf3d6c9bbef2 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -28,8 +28,6 @@ use sp_api::ProvideRuntimeApi; use sp_core::RuntimeDebug; use sp_core::crypto::KeyTypeId; -use sp_runtime::traits::Convert; -use sp_runtime::{RuntimeAppPublic, BoundToRuntimeAppPublic}; use sp_staking::SessionIndex; use sp_std::vec::Vec; @@ -108,75 +106,6 @@ impl GetValidatorCount for MembershipProof { } } -/// A trait for online node inspection in a session. -/// -/// This trait is used to decouple the pallet-session dependency from im-online -/// module so that the user of im-online & offences modules can pass any list of -/// validators that are considered to be online in each session, particularly useful -/// for the Substrate-based projects having their own staking implementation -/// instead of using pallet-staking directly. -pub trait ValidatorSet { - /// Type for representing validator id in a session. - /// - /// This could be `frame_support::Parameter`, but such import is not allowed in primitives. - type ValidatorId: codec::FullCodec + Clone + Eq + sp_std::fmt::Debug; - /// A type for converting `AccountId` to `ValidatorId`. - type ValidatorIdOf: Convert>; - - /// Returns current session index. - fn session_index() -> SessionIndex; - - /// Returns all the validators ought to be online in a session. - /// - /// The returned validators are all expected to be running an authority node. - fn validators() -> Vec; -} - -/// `ValidatorSet` combined with identification type for pallet-session-historical module. -pub trait ValidatorSetWithIdentification: ValidatorSet { - /// Full identification of `ValidatorId`. - type Identification: codec::FullCodec + Clone + Eq + sp_std::fmt::Debug; - /// A type for converting `ValidatorId` to `Identification`. - type IdentificationOf: Convert>; -} - -/// A session handler for specific key type. -pub trait OneSessionHandler: BoundToRuntimeAppPublic { - /// The key type expected. - type Key: Decode + Default + RuntimeAppPublic; - - /// The given validator set will be used for the genesis session. - /// It is guaranteed that the given validator set will also be used - /// for the second session, therefore the first call to `on_new_session` - /// should provide the same validator set. - fn on_genesis_session<'a, I: 'a>(validators: I) - where I: Iterator, 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, 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); -} /// Generate the initial session keys with the given seeds, at the given block and store them in /// the client's keystore. From 12324f3c01e9771e8f1ecac6cdfe16b904edf4e7 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Wed, 3 Feb 2021 00:03:01 +0800 Subject: [PATCH 32/33] Cleaup Cargo.toml --- Cargo.lock | 5 ----- frame/aura/Cargo.toml | 2 -- frame/authority-discovery/Cargo.toml | 2 -- frame/im-online/Cargo.toml | 2 -- frame/offences/benchmarking/Cargo.toml | 2 -- frame/staking/Cargo.toml | 1 - primitives/session/Cargo.toml | 2 +- primitives/session/src/lib.rs | 1 - 8 files changed, 1 insertion(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0577f6333a064..d5197ba4ea65b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4324,7 +4324,6 @@ dependencies = [ "sp-inherents", "sp-io", "sp-runtime", - "sp-session", "sp-std", "sp-timestamp", ] @@ -4343,7 +4342,6 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", - "sp-session", "sp-staking", "sp-std", ] @@ -4686,7 +4684,6 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", - "sp-session", "sp-staking", "sp-std", ] @@ -4853,7 +4850,6 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", - "sp-session", "sp-staking", "sp-std", ] @@ -5018,7 +5014,6 @@ dependencies = [ "sp-io", "sp-npos-elections", "sp-runtime", - "sp-session", "sp-staking", "sp-std", "sp-storage", diff --git a/frame/aura/Cargo.toml b/frame/aura/Cargo.toml index 66beb6e00d092..2cd7e5c15f5ca 100644 --- a/frame/aura/Cargo.toml +++ b/frame/aura/Cargo.toml @@ -20,7 +20,6 @@ sp-std = { version = "2.0.0", default-features = false, path = "../../primitives serde = { version = "1.0.101", optional = true } pallet-session = { version = "2.0.0", default-features = false, path = "../session" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } sp-consensus-aura = { version = "0.8.0", path = "../../primitives/consensus/aura", default-features = false } frame-system = { version = "2.0.0", default-features = false, path = "../system" } @@ -42,7 +41,6 @@ std = [ "sp-std/std", "serde", "sp-runtime/std", - "sp-session/std", "frame-support/std", "sp-consensus-aura/std", "frame-system/std", diff --git a/frame/authority-discovery/Cargo.toml b/frame/authority-discovery/Cargo.toml index 4f5147e193f5f..5b83de19a515d 100644 --- a/frame/authority-discovery/Cargo.toml +++ b/frame/authority-discovery/Cargo.toml @@ -20,7 +20,6 @@ sp-std = { version = "2.0.0", default-features = false, path = "../../primitives serde = { version = "1.0.101", optional = true } pallet-session = { version = "2.0.0", features = ["historical" ], path = "../session", default-features = false } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } @@ -39,7 +38,6 @@ std = [ "serde", "pallet-session/std", "sp-runtime/std", - "sp-session/std", "frame-support/std", "frame-system/std", ] diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 134782fc72dd1..41eb433478d4e 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -22,7 +22,6 @@ serde = { version = "1.0.101", optional = true } sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } @@ -43,7 +42,6 @@ std = [ "sp-io/std", "sp-runtime/std", "sp-staking/std", - "sp-session/std", "frame-support/std", "frame-system/std", ] diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml index 35107d6a0748c..0199d23d38583 100644 --- a/frame/offences/benchmarking/Cargo.toml +++ b/frame/offences/benchmarking/Cargo.toml @@ -26,7 +26,6 @@ pallet-session = { version = "2.0.0", default-features = false, path = "../../se pallet-staking = { version = "2.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } sp-staking = { version = "2.0.0", default-features = false, path = "../../../primitives/staking" } -sp-session = { version = "2.0.0", default-features = false, path = "../../../primitives/session" } sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } [dev-dependencies] @@ -51,7 +50,6 @@ std = [ "pallet-staking/std", "sp-runtime/std", "sp-staking/std", - "sp-session/std", "sp-std/std", "codec/std", ] diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index 0e715858f9b80..2cd25daa8094e 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -35,7 +35,6 @@ rand_chacha = { version = "0.2", default-features = false, optional = true } sp-core = { version = "2.0.0", path = "../../primitives/core" } sp-storage = { version = "2.0.0", path = "../../primitives/storage" } sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } -sp-session = { version = "2.0.0", path = "../../primitives/session" } pallet-balances = { version = "2.0.0", path = "../balances" } pallet-timestamp = { version = "2.0.0", path = "../timestamp" } pallet-staking-reward-curve = { version = "2.0.0", path = "../staking/reward-curve" } diff --git a/primitives/session/Cargo.toml b/primitives/session/Cargo.toml index 752f8f85c9098..5b83e88c44fae 100644 --- a/primitives/session/Cargo.toml +++ b/primitives/session/Cargo.toml @@ -18,7 +18,7 @@ sp-api = { version = "2.0.0", default-features = false, path = "../api" } sp-core = { version = "2.0.0", default-features = false, path = "../core" } sp-std = { version = "2.0.0", default-features = false, path = "../std" } sp-staking = { version = "2.0.0", default-features = false, path = "../staking" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } +sp-runtime = { version = "2.0.0", optional = true, path = "../runtime" } [features] default = [ "std" ] diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index dcf3d6c9bbef2..8000c23dd4311 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -106,7 +106,6 @@ impl GetValidatorCount for MembershipProof { } } - /// Generate the initial session keys with the given seeds, at the given block and store them in /// the client's keystore. #[cfg(feature = "std")] From 355103603733bd161c3a4a8b1a53cddae454efb1 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Wed, 3 Feb 2021 00:06:16 +0800 Subject: [PATCH 33/33] Aura has migrated to Pallet now --- frame/aura/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 1e795403d9d4b..61937da286ada 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -144,7 +144,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Pallet { type Public = T::AuthorityId; } -impl OneSessionHandler for Module { +impl OneSessionHandler for Pallet { type Key = T::AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I)