From 92630df14329923a6ef87c317d89e9c245b59da6 Mon Sep 17 00:00:00 2001 From: ferrell-code <70108835+ferrell-code@users.noreply.github.com> Date: Sat, 17 Apr 2021 22:23:27 -0400 Subject: [PATCH] Authority Discovery to FRAME v2 (#8620) * migrate to new macro * formatting * Apply suggestions from code review * Update frame/authority-discovery/src/lib.rs Co-authored-by: Guillaume Thiolliere --- .../frame/authority-discovery/src/lib.rs | 117 +++++++++++++----- 1 file changed, 85 insertions(+), 32 deletions(-) diff --git a/substrate/frame/authority-discovery/src/lib.rs b/substrate/frame/authority-discovery/src/lib.rs index 93466d4f35..6b7608b10c 100644 --- a/substrate/frame/authority-discovery/src/lib.rs +++ b/substrate/frame/authority-discovery/src/lib.rs @@ -15,45 +15,87 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! # Authority discovery module. +//! # Authority discovery pallet. //! -//! This module is used by the `client/authority-discovery` and by polkadot's parachain logic +//! This pallet 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::prelude::*; -use frame_support::{decl_module, decl_storage, traits::OneSessionHandler}; +use frame_support::traits::OneSessionHandler; +#[cfg(feature = "std")] +use frame_support::traits::GenesisBuild; use sp_authority_discovery::AuthorityId; -/// The module's config trait. -pub trait Config: frame_system::Config + pallet_session::Config {} +pub use pallet::*; -decl_storage! { - trait Store for Module as AuthorityDiscovery { - /// Keys of the current authority set. - Keys get(fn keys): Vec; - /// Keys of the next authority set. - NextKeys get(fn next_keys): Vec; +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; + + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::config] + /// The pallet's config trait. + pub trait Config: frame_system::Config + pallet_session::Config {} + + #[pallet::storage] + #[pallet::getter(fn keys)] + /// Keys of the current authority set. + pub(super) type Keys = StorageValue< + _, + Vec, + ValueQuery, + >; + + #[pallet::storage] + #[pallet::getter(fn next_keys)] + /// Keys of the next authority set. + pub(super) type NextKeys = StorageValue< + _, + Vec, + ValueQuery, + >; + + #[pallet::genesis_config] + pub struct GenesisConfig { + pub keys: Vec, } - add_extra_genesis { - config(keys): Vec; - build(|config| Module::::initialize_keys(&config.keys)) + + #[cfg(feature = "std")] + impl Default for GenesisConfig { + fn default() -> Self { + Self { + keys: Default::default(), + } + } } + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + Pallet::::initialize_keys(&self.keys) + } + } + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::call] + impl Pallet {} } -decl_module! { - pub struct Module for enum Call where origin: T::Origin { - } -} - -impl Module { +impl Pallet { /// 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(); + let mut keys = Keys::::get(); + let next = NextKeys::::get(); keys.extend(next); keys.sort(); @@ -64,28 +106,28 @@ impl Module { /// Retrieve authority identifiers of the current authority set in the original order. pub fn current_authorities() -> Vec { - Keys::get() + Keys::::get() } /// Retrieve authority identifiers of the next authority set in the original order. pub fn next_authorities() -> Vec { - NextKeys::get() + 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); + assert!(Keys::::get().is_empty(), "Keys are already initialized!"); + Keys::::put(keys); + NextKeys::::put(keys); } } } -impl sp_runtime::BoundToRuntimeAppPublic for Module { +impl sp_runtime::BoundToRuntimeAppPublic for Pallet { type Public = AuthorityId; } -impl OneSessionHandler for Module { +impl OneSessionHandler for Pallet { type Key = AuthorityId; fn on_genesis_session<'a, I: 'a>(authorities: I) @@ -102,9 +144,9 @@ impl OneSessionHandler for Module { // Remember who the authorities are for the new and next session. if changed { let keys = validators.map(|x| x.1); - Keys::put(keys.collect::>()); + Keys::::put(keys.collect::>()); let next_keys = queued_validators.map(|x| x.1); - NextKeys::put(next_keys.collect::>()); + NextKeys::::put(next_keys.collect::>()); } } @@ -113,6 +155,17 @@ impl OneSessionHandler for Module { } } +#[cfg(feature = "std")] +impl GenesisConfig { + /// Direct implementation of `GenesisBuild::assimilate_storage`. + pub fn assimilate_storage( + &self, + storage: &mut sp_runtime::Storage + ) -> Result<(), String> { + >::assimilate_storage(self, storage) + } +} + #[cfg(test)] mod tests { use crate as pallet_authority_discovery; @@ -221,7 +274,7 @@ mod tests { #[test] fn authorities_returns_current_and_next_authority_set() { - // The whole authority discovery module ignores account ids, but we still need them for + // The whole authority discovery pallet ignores account ids, but we still need them for // `pallet_session::OneSessionHandler::on_new_session`, thus its safe to use the same value // everywhere. let account_id = AuthorityPair::from_seed_slice(vec![10; 32].as_ref()).unwrap().public();