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

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi committed Jul 27, 2019
1 parent f97603f commit 777dbde
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 31 deletions.
12 changes: 12 additions & 0 deletions core/sr-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,18 @@ pub trait IsMember<MemberId> {
fn is_member(member_id: &MemberId) -> bool;
}

/// Mean for getting the current session keys.
pub trait CurrentSessionKeys<AccountId> {
/// Get the keys of the current session.
fn current_keys<Key: Decode + Default + TypedKey>() -> Vec<(AccountId, Key)>;
}

impl<T> CurrentSessionKeys<T> for () {
fn current_keys<Key>() -> Vec<(T, Key)> {
Vec::new()
}
}

/// Disable a validator referenced by an `AccountId`.
pub trait DisableValidator<AccountId> {
/// Disable a validator referenced by an `AccountId`.
Expand Down
1 change: 1 addition & 0 deletions core/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ parameter_types! {

impl srml_babe::Trait for Runtime {
type EpochDuration = EpochDuration;
type CurrentSessionKeys = ();
}

/// Adds one to the given input and returns the final result.
Expand Down
2 changes: 1 addition & 1 deletion node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ parameter_types! {

impl babe::Trait for Runtime {
type EpochDuration = EpochDuration;
type SessionInterface = Self;
type CurrentSessionKeys = session;
}

impl indices::Trait for Runtime {
Expand Down
37 changes: 8 additions & 29 deletions srml/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use rstd::{result, prelude::*};
use srml_support::{decl_storage, decl_module, StorageValue, traits::FindAuthor, traits::Get};
use timestamp::{OnTimestampSet};
use primitives::{generic::DigestItem, ConsensusEngineId};
use primitives::traits::{IsMember, SaturatedConversion, Saturating, RandomnessBeacon, Convert, TypedKey};
use primitives::traits::{
CurrentSessionKeys, IsMember, SaturatedConversion, Saturating, RandomnessBeacon, Convert,
TypedKey,
};
#[cfg(feature = "std")]
use timestamp::TimestampInherentData;
use parity_codec::{Encode, Decode};
Expand Down Expand Up @@ -107,11 +110,11 @@ impl ProvideInherentData for InherentDataProvider {
}
}

pub trait Trait: timestamp::Trait + session::Trait {
pub trait Trait: timestamp::Trait {
type EpochDuration: Get<u64>;

/// Interface for interacting with a session module.
type SessionInterface: self::SessionInterface<Self::AccountId>;
/// Retrieve the current session keys.
type CurrentSessionKeys: CurrentSessionKeys<Self::AccountId>;
}

/// The length of the BABE randomness
Expand Down Expand Up @@ -214,30 +217,6 @@ impl<T: Trait> IsMember<AuthorityId> for Module<T> {
}
}

/// Means for interacting with a specialized version of the `session` trait.
///
/// This is needed because `Staking` sets the `ValidatorIdOf` of the `session::Trait`
pub trait SessionInterface<AccountId>: system::Trait {
/// Get the keys of the current session.
fn current_keys<Key: Decode + Default + TypedKey>() -> Vec<(AccountId, Key)>;
}

impl<T: Trait> SessionInterface<<T as system::Trait>::AccountId> for T where
T::AccountIdOf: Convert<<T as session::Trait>::ValidatorId, Option<<T as system::Trait>::AccountId>>,
{
fn current_keys<Key>() -> Vec<(<T as system::Trait>::AccountId, Key)>
where Key: Decode + Default + TypedKey
{
<session::Module<T>>::get_current_keys::<Key>()
.into_iter()
.map(|(validator_id, keys)| {
let account_id = T::AccountIdOf::convert(validator_id).unwrap_or_default();
(account_id, keys)
})
.collect()
}
}

/// A `Convert` implementation that finds the babe authority id of the given controller
/// account, if any.
pub struct AuthorityIdOf<T, Id>(rstd::marker::PhantomData<T>, rstd::marker::PhantomData<Id>);
Expand All @@ -248,7 +227,7 @@ impl<T, AuthorityId> Convert<T::AccountId, Option<AuthorityId>> for AuthorityIdO
AuthorityId: Decode + Default + TypedKey,
{
fn convert(account_id: T::AccountId) -> Option<AuthorityId> {
let keys = T::SessionInterface::current_keys::<AuthorityId>();
let keys = T::CurrentSessionKeys::current_keys::<AuthorityId>();
let maybe_authority_id = keys
.into_iter()
.find(|(id, _)| {
Expand Down
20 changes: 19 additions & 1 deletion srml/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ use rstd::{prelude::*, marker::PhantomData, ops::{Sub, Rem}};
use parity_codec::Decode;
use primitives::KeyTypeId;
use primitives::weights::SimpleDispatchInfo;
use primitives::traits::{Convert, Zero, Member, OpaqueKeys, TypedKey};
use primitives::traits::{
Convert, CurrentSessionKeys, Zero, Member, OpaqueKeys, TypedKey,
};
use srml_support::{
dispatch::Result, ConsensusEngineId, StorageValue, StorageDoubleMap, for_each_tuple,
decl_module, decl_event, decl_storage,
Expand Down Expand Up @@ -559,6 +561,22 @@ impl<T: Trait> OnFreeBalanceZero<T::ValidatorId> for Module<T> {
}
}

impl<T: Trait> CurrentSessionKeys<<T as system::Trait>::AccountId> for Module<T> where
T::AccountIdOf: Convert<T::ValidatorId, Option<<T as system::Trait>::AccountId>>,
{
fn current_keys<Key>() -> Vec<(<T as system::Trait>::AccountId, Key)>
where Key: Decode + Default + TypedKey
{
Self::get_current_keys::<Key>()
.into_iter()
.map(|(validator_id, keys)| {
let account_id = T::AccountIdOf::convert(validator_id).unwrap_or_default();
(account_id, keys)
})
.collect()
}
}

/// 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.
Expand Down
1 change: 1 addition & 0 deletions srml/session/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl Trait for Test {
type ValidatorIdOf = ConvertInto;
type Keys = UintAuthorityId;
type Event = ();
type AccountIdOf = ();
type SelectInitialValidators = ();
}

Expand Down
1 change: 1 addition & 0 deletions srml/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl session::Trait for Test {
type ValidatorId = AccountId;
type ValidatorIdOf = crate::StashOf<Test>;
type SelectInitialValidators = Staking;
type AccountIdOf = crate::ControllerOf<Self>;
}

impl session::historical::Trait for Test {
Expand Down

0 comments on commit 777dbde

Please sign in to comment.