Skip to content
Snippets Groups Projects
Commit 9383f7a0 authored by ordian's avatar ordian Committed by GitHub
Browse files

pallet_authority_discovery: introduce current_authorities and next_authorities methods (#7892)

* split authorities discovery keys for the current and next session

* Revert "split authorities discovery keys for the current and next session"

This reverts commit 0a40b8b4c14e85d95357a27f6db30199cbe0aa4d.

* pallet_authority_discovery: introduce a next_authorities method

* address feedback

* amend the doccomments
parent aa9d2966
Branches
No related merge requests found
......@@ -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<T: Config> as AuthorityDiscovery {
/// Keys of the current and next authority set.
/// Keys of the current authority set.
Keys get(fn keys): Vec<AuthorityId>;
/// Keys of the next authority set.
NextKeys get(fn next_keys): Vec<AuthorityId>;
}
add_extra_genesis {
config(keys): Vec<AuthorityId>;
......@@ -47,15 +49,34 @@ decl_module! {
}
impl<T: Config> Module<T> {
/// 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<AuthorityId> {
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<AuthorityId> {
Keys::get()
}
/// Retrieve authority identifiers of the next authority set in the original order.
pub fn next_authorities() -> Vec<AuthorityId> {
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<T: Config> pallet_session::OneSessionHandler<T::AccountId> for Module<T> {
{
// Remember who the authorities are for the new and next session.
if changed {
let keys = validators.chain(queued_validators).map(|x| x.1).collect::<BTreeSet<_>>();
Keys::put(keys.into_iter().collect::<Vec<_>>());
let keys = validators.map(|x| x.1);
Keys::put(keys.collect::<Vec<_>>());
let next_keys = queued_validators.map(|x| x.1);
NextKeys::put(next_keys.collect::<Vec<_>>());
}
}
......@@ -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,
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment