From 528e7517536d31a990f71ea64f1c504f56be711c Mon Sep 17 00:00:00 2001 From: ferrell-code Date: Tue, 6 Jul 2021 22:50:52 -0400 Subject: [PATCH] parchain initializer.rs to FrameV2 (#3416) * migrate to FrameV2 * kusama & westend change pallet name * keep item private Co-authored-by: Guillaume Thiolliere --- polkadot/runtime/kusama/src/lib.rs | 4 +- .../runtime/parachains/src/initializer.rs | 140 +++++++++--------- polkadot/runtime/westend/src/lib.rs | 4 +- 3 files changed, 77 insertions(+), 71 deletions(-) diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index ccf90b90a0..5b0b2eecd9 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -321,7 +321,7 @@ impl_opaque_keys! { pub grandpa: Grandpa, pub babe: Babe, pub im_online: ImOnline, - pub para_validator: ParasInitializer, + pub para_validator: Initializer, pub para_assignment: ParasSessionInfo, pub authority_discovery: AuthorityDiscovery, } @@ -1487,7 +1487,7 @@ construct_runtime! { ParasInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent} = 54, ParasScheduler: parachains_scheduler::{Pallet, Call, Storage} = 55, Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 56, - ParasInitializer: parachains_initializer::{Pallet, Call, Storage} = 57, + Initializer: parachains_initializer::{Pallet, Call, Storage} = 57, ParasDmp: parachains_dmp::{Pallet, Call, Storage} = 58, ParasUmp: parachains_ump::{Pallet, Call, Storage, Event} = 59, ParasHrmp: parachains_hrmp::{Pallet, Call, Storage, Event} = 60, diff --git a/polkadot/runtime/parachains/src/initializer.rs b/polkadot/runtime/parachains/src/initializer.rs index 12e404575b..5bbc56c214 100644 --- a/polkadot/runtime/parachains/src/initializer.rs +++ b/polkadot/runtime/parachains/src/initializer.rs @@ -20,18 +20,16 @@ //! This module can throw fatal errors if session-change notifications are received after initialization. use sp_std::prelude::*; -use frame_support::weights::{Weight, DispatchClass}; -use frame_support::traits::EnsureOrigin; use primitives::v1::{ValidatorId, SessionIndex, ConsensusLog, BlockNumber}; -use frame_support::{ - decl_storage, decl_module, decl_error, traits::{OneSessionHandler, Randomness}, -}; +use frame_support::traits::{Randomness, OneSessionHandler}; use parity_scale_codec::{Encode, Decode}; use crate::{ configuration::{self, HostConfiguration}, shared, paras, scheduler, inclusion, session_info, dmp, ump, hrmp, }; +pub use pallet::*; + /// Information about a session change that has just occurred. #[derive(Clone)] pub struct SessionChangeNotification { @@ -69,55 +67,59 @@ struct BufferedSessionChange { session_index: SessionIndex, } -pub trait Config: - frame_system::Config - + configuration::Config - + shared::Config - + paras::Config - + scheduler::Config - + inclusion::Config - + session_info::Config - + dmp::Config - + ump::Config - + hrmp::Config -{ - /// A randomness beacon. - type Randomness: Randomness; - /// An origin which is allowed to force updates to parachains. - type ForceOrigin: EnsureOrigin<::Origin>; -} +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; -decl_storage! { - trait Store for Module as Initializer { - /// Whether the parachains modules have been initialized within this block. - /// - /// Semantically a bool, but this guarantees it should never hit the trie, - /// as this is cleared in `on_finalize` and Frame optimizes `None` values to be empty values. - /// - /// As a bool, `set(false)` and `remove()` both lead to the next `get()` being false, but one of - /// them writes to the trie and one does not. This confusion makes `Option<()>` more suitable for - /// the semantics of this variable. - HasInitialized: Option<()>; - /// Buffered session changes along with the block number at which they should be applied. - /// - /// Typically this will be empty or one element long. Apart from that this item never hits - /// the storage. - /// - /// However this is a `Vec` regardless to handle various edge cases that may occur at runtime - /// upgrade boundaries or if governance intervenes. - BufferedSessionChanges: Vec; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: + frame_system::Config + + configuration::Config + + shared::Config + + paras::Config + + scheduler::Config + + inclusion::Config + + session_info::Config + + dmp::Config + + ump::Config + + hrmp::Config + { + /// A randomness beacon. + type Randomness: Randomness; + /// An origin which is allowed to force updates to parachains. + type ForceOrigin: EnsureOrigin<::Origin>; } -} -decl_error! { - pub enum Error for Module { } -} -decl_module! { - /// The initializer module. - pub struct Module for enum Call where origin: ::Origin { - type Error = Error; + /// Whether the parachains modules have been initialized within this block. + /// + /// Semantically a bool, but this guarantees it should never hit the trie, + /// as this is cleared in `on_finalize` and Frame optimizes `None` values to be empty values. + /// + /// As a bool, `set(false)` and `remove()` both lead to the next `get()` being false, but one of + /// them writes to the trie and one does not. This confusion makes `Option<()>` more suitable for + /// the semantics of this variable. + #[pallet::storage] + pub(super) type HasInitialized = StorageValue<_, ()>; + /// Buffered session changes along with the block number at which they should be applied. + /// + /// Typically this will be empty or one element long. Apart from that this item never hits + /// the storage. + /// + /// However this is a `Vec` regardless to handle various edge cases that may occur at runtime + /// upgrade boundaries or if governance intervenes. + #[pallet::storage] + pub(super) type BufferedSessionChanges = StorageValue<_, Vec, ValueQuery>; + + #[pallet::hooks] + impl Hooks> for Pallet { fn on_initialize(now: T::BlockNumber) -> Weight { // The other modules are initialized in this order: // - Configuration @@ -139,12 +141,12 @@ decl_module! { ump::Module::::initializer_initialize(now) + hrmp::Module::::initializer_initialize(now); - HasInitialized::set(Some(())); + HasInitialized::::set(Some(())); total_weight } - fn on_finalize() { + fn on_finalize(_: T::BlockNumber) { // reverse initialization order. hrmp::Module::::initializer_finalize(); ump::Module::::initializer_finalize(); @@ -164,27 +166,31 @@ decl_module! { session_index, validators, queued, - }) = BufferedSessionChanges::take().pop() + }) = BufferedSessionChanges::::take().pop() { Self::apply_new_session(session_index, validators, queued); } - HasInitialized::take(); + HasInitialized::::take(); } + } + #[pallet::call] + impl Pallet { /// Issue a signal to the consensus engine to forcibly act as though all parachain /// blocks in all relay chain blocks up to and including the given number in the current /// chain are valid and should be finalized. - #[weight = (0, DispatchClass::Operational)] - fn force_approve(origin, up_to: BlockNumber) { + #[pallet::weight((0, DispatchClass::Operational))] + pub fn force_approve(origin: OriginFor, up_to: BlockNumber) -> DispatchResult { T::ForceOrigin::ensure_origin(origin)?; frame_system::Pallet::::deposit_log(ConsensusLog::ForceApprove(up_to).into()); + Ok(()) } } } -impl Module { +impl Pallet { fn apply_new_session( session_index: SessionIndex, all_validators: Vec, @@ -254,7 +260,7 @@ impl Module { // Genesis session should be immediately enacted. Self::apply_new_session(0, validators, queued); } else { - BufferedSessionChanges::mutate(|v| v.push(BufferedSessionChange { + BufferedSessionChanges::::mutate(|v| v.push(BufferedSessionChange { validators, queued, session_index, @@ -264,24 +270,24 @@ impl Module { } } -impl sp_runtime::BoundToRuntimeAppPublic for Module { +impl sp_runtime::BoundToRuntimeAppPublic for Pallet { type Public = ValidatorId; } -impl OneSessionHandler for Module { +impl OneSessionHandler for Pallet { type Key = ValidatorId; fn on_genesis_session<'a, I: 'a>(validators: I) where I: Iterator { - >::on_new_session(false, 0, validators, None); + >::on_new_session(false, 0, validators, None); } fn on_new_session<'a, I: 'a>(changed: bool, validators: I, queued: I) where I: Iterator { - let session_index = >::current_index(); - >::on_new_session(changed, session_index, validators, Some(queued)); + let session_index = >::current_index(); + >::on_new_session(changed, session_index, validators, Some(queued)); } fn on_disabled(_i: usize) { } @@ -311,7 +317,7 @@ mod tests { Some(Vec::new().into_iter()), ); - let v = ::get(); + let v = ::BufferedSessionChanges::get(); assert!(v.is_empty()); assert_eq!(SessionInfo::earliest_stored_session(), 0); @@ -332,7 +338,7 @@ mod tests { let now = System::block_number(); Initializer::on_initialize(now); - let v = ::get(); + let v = ::BufferedSessionChanges::get(); assert_eq!(v.len(), 1); }); } @@ -350,7 +356,7 @@ mod tests { Initializer::on_finalize(1); - assert!(::get().is_empty()); + assert!(::BufferedSessionChanges::get().is_empty()); }); } @@ -359,7 +365,7 @@ mod tests { new_test_ext(Default::default()).execute_with(|| { Initializer::on_initialize(1); - assert!(HasInitialized::get().is_some()); + assert!(::HasInitialized::get().is_some()); }) } @@ -369,7 +375,7 @@ mod tests { Initializer::on_initialize(1); Initializer::on_finalize(1); - assert!(HasInitialized::get().is_none()); + assert!(::HasInitialized::get().is_none()); }) } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 80b48258c5..654d5d50e3 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -306,7 +306,7 @@ impl_opaque_keys! { pub grandpa: Grandpa, pub babe: Babe, pub im_online: ImOnline, - pub para_validator: ParasInitializer, + pub para_validator: Initializer, pub para_assignment: ParasSessionInfo, pub authority_discovery: AuthorityDiscovery, } @@ -1078,7 +1078,7 @@ construct_runtime! { ParasInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent} = 45, ParasScheduler: parachains_scheduler::{Pallet, Call, Storage} = 46, Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 47, - ParasInitializer: parachains_initializer::{Pallet, Call, Storage} = 48, + Initializer: parachains_initializer::{Pallet, Call, Storage} = 48, ParasDmp: parachains_dmp::{Pallet, Call, Storage} = 49, ParasUmp: parachains_ump::{Pallet, Call, Storage, Event} = 50, ParasHrmp: parachains_hrmp::{Pallet, Call, Storage, Event} = 51,