diff --git a/substrate/frame/authority-discovery/src/lib.rs b/substrate/frame/authority-discovery/src/lib.rs index fdc13cd747..9a7c209887 100644 --- a/substrate/frame/authority-discovery/src/lib.rs +++ b/substrate/frame/authority-discovery/src/lib.rs @@ -17,13 +17,13 @@ //! # Authority discovery module. //! -//! This module is used by the `client/authority-discovery` to retrieve the -//! current set of authorities. +//! This module is used by the `client/authority-discovery` and by polkadot's parachain logic +//! to retrieve the current and the next set of authorities. // Ensure we're `no_std` when compiling for Wasm. #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::{collections::btree_set::BTreeSet, prelude::*}; +use sp_std::prelude::*; use frame_support::{decl_module, decl_storage}; use sp_authority_discovery::AuthorityId; @@ -32,8 +32,10 @@ pub trait Config: frame_system::Config + pallet_session::Config {} decl_storage! { trait Store for Module as AuthorityDiscovery { - /// Keys of the current and next authority set. + /// Keys of the current authority set. Keys get(fn keys): Vec; + /// Keys of the next authority set. + NextKeys get(fn next_keys): Vec; } add_extra_genesis { config(keys): Vec; @@ -47,15 +49,34 @@ decl_module! { } impl Module { - /// Retrieve authority identifiers of the current and next authority set. + /// Retrieve authority identifiers of the current and next authority set + /// sorted and deduplicated. pub fn authorities() -> Vec { + let mut keys = Keys::get(); + let next = NextKeys::get(); + + keys.extend(next); + keys.sort(); + keys.dedup(); + + keys + } + + /// Retrieve authority identifiers of the current authority set in the original order. + pub fn current_authorities() -> Vec { Keys::get() } + /// Retrieve authority identifiers of the next authority set in the original order. + pub fn next_authorities() -> Vec { + NextKeys::get() + } + fn initialize_keys(keys: &[AuthorityId]) { if !keys.is_empty() { assert!(Keys::get().is_empty(), "Keys are already initialized!"); Keys::put(keys); + NextKeys::put(keys); } } } @@ -80,8 +101,10 @@ impl pallet_session::OneSessionHandler for Module { { // Remember who the authorities are for the new and next session. if changed { - let keys = validators.chain(queued_validators).map(|x| x.1).collect::>(); - Keys::put(keys.into_iter().collect::>()); + let keys = validators.map(|x| x.1); + Keys::put(keys.collect::>()); + let next_keys = queued_validators.map(|x| x.1); + NextKeys::put(next_keys.collect::>()); } } @@ -250,8 +273,7 @@ mod tests { second_authorities_and_account_ids.clone().into_iter(), third_authorities_and_account_ids.clone().into_iter(), ); - let mut authorities_returned = AuthorityDiscovery::authorities(); - authorities_returned.sort(); + let authorities_returned = AuthorityDiscovery::authorities(); assert_eq!( first_authorities, authorities_returned,