diff --git a/pallets/maintenance-mode/src/lib.rs b/pallets/maintenance-mode/src/lib.rs index 4ff1d9f9..fa8aaa15 100644 --- a/pallets/maintenance-mode/src/lib.rs +++ b/pallets/maintenance-mode/src/lib.rs @@ -45,28 +45,17 @@ mod mock; #[cfg(test)] mod tests; -mod types; - use frame_support::pallet; pub use pallet::*; -pub use types::*; #[pallet] pub mod pallet { #[cfg(feature = "xcm-support")] - use cumulus_primitives_core::{ - relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, - }; use frame_support::pallet_prelude::*; - use frame_support::traits::{ - BeforeAllRuntimeMigrations, BuildGenesisConfig, Contains, EnsureOrigin, OffchainWorker, - OnFinalize, OnIdle, OnInitialize, OnRuntimeUpgrade, - }; + use frame_support::traits::{BuildGenesisConfig, Contains, EnsureOrigin, QueuePausedQuery}; use frame_system::pallet_prelude::*; #[cfg(feature = "xcm-support")] - use sp_std::vec::Vec; - #[cfg(feature = "xcm-support")] use xcm_primitives::PauseXcmExecution; /// Pallet for migrations @@ -94,32 +83,6 @@ pub mod pallet { /// Handler to suspend and resume XCM execution #[cfg(feature = "xcm-support")] type XcmExecutionManager: PauseXcmExecution; - /// The DMP handler to be used in normal operating mode - /// TODO: remove once https://github.com/paritytech/polkadot/pull/5035 is merged - #[cfg(feature = "xcm-support")] - type NormalDmpHandler: DmpMessageHandler; - /// The DMP handler to be used in maintenance mode - /// TODO: remove once https://github.com/paritytech/polkadot/pull/5035 is merged - #[cfg(feature = "xcm-support")] - type MaintenanceDmpHandler: DmpMessageHandler; - /// The executive hooks that will be used in normal operating mode - /// Important: Use AllPalletsWithSystem here if you dont want to modify the - /// hooks behaviour - type NormalExecutiveHooks: OnRuntimeUpgrade - + BeforeAllRuntimeMigrations - + OnInitialize> - + OnIdle> - + OnFinalize> - + OffchainWorker>; - /// The executive hooks that will be used in maintenance mode - /// Important: Use AllPalletsWithSystem here if you dont want to modify the - /// hooks behaviour - type MaintenanceExecutiveHooks: OnRuntimeUpgrade - + BeforeAllRuntimeMigrations - + OnInitialize> - + OnIdle> - + OnFinalize> - + OffchainWorker>; } #[pallet::event] @@ -246,17 +209,11 @@ pub mod pallet { } } } + #[cfg(feature = "xcm-support")] - impl DmpMessageHandler for Pallet { - fn handle_dmp_messages( - iter: impl Iterator)>, - limit: Weight, - ) -> Weight { - if MaintenanceMode::::get() { - T::MaintenanceDmpHandler::handle_dmp_messages(iter, limit) - } else { - T::NormalDmpHandler::handle_dmp_messages(iter, limit) - } + impl QueuePausedQuery for Pallet { + fn is_paused(_origin: &Origin) -> bool { + MaintenanceMode::::get() } } } diff --git a/pallets/maintenance-mode/src/mock.rs b/pallets/maintenance-mode/src/mock.rs index 9ef596cf..f6a2cbaf 100644 --- a/pallets/maintenance-mode/src/mock.rs +++ b/pallets/maintenance-mode/src/mock.rs @@ -17,12 +17,9 @@ //! A minimal runtime including the maintenance-mode pallet use super::*; use crate as pallet_maintenance_mode; -use cumulus_primitives_core::{relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler}; use frame_support::{ construct_runtime, parameter_types, - traits::{ - Contains, Everything, OffchainWorker, OnFinalize, OnIdle, OnInitialize, OnRuntimeUpgrade, - }, + traits::{Contains, Everything}, weights::Weight, }; use frame_system::EnsureRoot; @@ -34,7 +31,6 @@ use sp_runtime::{ //TODO use TestAccount once it is in a common place (currently it lives with democracy precompiles) pub type AccountId = u64; -pub type BlockNumber = u64; type Block = frame_system::mocking::MockBlock; @@ -44,7 +40,6 @@ construct_runtime!( { System: frame_system::{Pallet, Call, Config, Storage, Event}, MaintenanceMode: pallet_maintenance_mode::{Pallet, Call, Storage, Event, Config}, - MockPalletMaintenanceHooks: mock_pallet_maintenance_hooks::{Pallet, Call, Event}, } ); @@ -90,158 +85,6 @@ impl Contains for MaintenanceCallFilter { } } -pub struct MaintenanceDmpHandler; -#[cfg(feature = "xcm-support")] -impl DmpMessageHandler for MaintenanceDmpHandler { - // This implementation makes messages be queued - // Since the limit is 0, messages are queued for next iteration - fn handle_dmp_messages( - _iter: impl Iterator)>, - _limit: Weight, - ) -> Weight { - return Weight::from_parts(1, 0); - } -} - -pub struct NormalDmpHandler; -#[cfg(feature = "xcm-support")] -impl DmpMessageHandler for NormalDmpHandler { - // This implementation makes messages be queued - // Since the limit is 0, messages are queued for next iteration - fn handle_dmp_messages( - _iter: impl Iterator)>, - _limit: Weight, - ) -> Weight { - Weight::zero() - } -} - -impl mock_pallet_maintenance_hooks::Config for Test { - type RuntimeEvent = RuntimeEvent; -} - -// Pallet to throw events, used to test maintenance mode hooks -#[frame_support::pallet] -pub mod mock_pallet_maintenance_hooks { - use frame_support::pallet_prelude::*; - - #[pallet::config] - pub trait Config: frame_system::Config { - type RuntimeEvent: From + IsType<::RuntimeEvent>; - } - - #[pallet::pallet] - pub struct Pallet(_); - - #[pallet::call] - impl Pallet {} - - #[pallet::event] - #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - MaintenanceOnIdle, - MaintenanceOnInitialize, - MaintenanceOffchainWorker, - MaintenanceOnFinalize, - MaintenanceOnRuntimeUpgrade, - NormalOnIdle, - NormalOnInitialize, - NormalOffchainWorker, - NormalOnFinalize, - NormalOnRuntimeUpgrade, - } -} - -pub struct MaintenanceHooks; - -impl OnInitialize for MaintenanceHooks { - fn on_initialize(_n: BlockNumber) -> Weight { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::MaintenanceOnInitialize, - ); - Weight::from_parts(1, 0) - } -} - -impl OnIdle for MaintenanceHooks { - fn on_idle(_n: BlockNumber, _max_weight: Weight) -> Weight { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::MaintenanceOnIdle, - ); - Weight::from_parts(1, 0) - } -} - -impl OnRuntimeUpgrade for MaintenanceHooks { - fn on_runtime_upgrade() -> Weight { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::MaintenanceOnRuntimeUpgrade, - ); - Weight::from_parts(1, 0) - } -} - -impl OnFinalize for MaintenanceHooks { - fn on_finalize(_n: BlockNumber) { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::MaintenanceOnFinalize, - ); - } -} - -impl OffchainWorker for MaintenanceHooks { - fn offchain_worker(_n: BlockNumber) { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::MaintenanceOffchainWorker, - ); - } -} - -pub struct NormalHooks; - -impl OnInitialize for NormalHooks { - fn on_initialize(_n: BlockNumber) -> Weight { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::NormalOnInitialize, - ); - Weight::zero() - } -} - -impl OnIdle for NormalHooks { - fn on_idle(_n: BlockNumber, _max_weight: Weight) -> Weight { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::NormalOnIdle, - ); - Weight::zero() - } -} - -impl OnRuntimeUpgrade for NormalHooks { - fn on_runtime_upgrade() -> Weight { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::NormalOnRuntimeUpgrade, - ); - Weight::zero() - } -} - -impl OnFinalize for NormalHooks { - fn on_finalize(_n: BlockNumber) { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::NormalOnFinalize, - ); - } -} - -impl OffchainWorker for NormalHooks { - fn offchain_worker(_n: BlockNumber) { - MockPalletMaintenanceHooks::deposit_event( - mock_pallet_maintenance_hooks::Event::NormalOffchainWorker, - ); - } -} - impl Config for Test { type RuntimeEvent = RuntimeEvent; type NormalCallFilter = Everything; @@ -249,12 +92,6 @@ impl Config for Test { type MaintenanceOrigin = EnsureRoot; #[cfg(feature = "xcm-support")] type XcmExecutionManager = (); - #[cfg(feature = "xcm-support")] - type NormalDmpHandler = NormalDmpHandler; - #[cfg(feature = "xcm-support")] - type MaintenanceDmpHandler = MaintenanceDmpHandler; - type NormalExecutiveHooks = NormalHooks; - type MaintenanceExecutiveHooks = MaintenanceHooks; } /// Externality builder for pallet maintenance mode's mock runtime @@ -309,17 +146,3 @@ pub(crate) fn events() -> Vec { }) .collect::>() } - -pub(crate) fn mock_events() -> Vec { - System::events() - .into_iter() - .map(|r| r.event) - .filter_map(|e| { - if let RuntimeEvent::MockPalletMaintenanceHooks(inner) = e { - Some(inner) - } else { - None - } - }) - .collect::>() -} diff --git a/pallets/maintenance-mode/src/tests.rs b/pallets/maintenance-mode/src/tests.rs index 016f7dbd..e20d00be 100644 --- a/pallets/maintenance-mode/src/tests.rs +++ b/pallets/maintenance-mode/src/tests.rs @@ -16,15 +16,12 @@ //! Unit testing use crate::mock::{ - events, mock_events, ExtBuilder, MaintenanceMode, RuntimeCall as OuterCall, RuntimeOrigin, Test, -}; -use crate::{Call, Error, Event, ExecutiveHooks}; -use cumulus_primitives_core::DmpMessageHandler; -use frame_support::{ - assert_noop, assert_ok, - traits::{OffchainWorker, OnFinalize, OnIdle, OnInitialize, OnRuntimeUpgrade}, - weights::Weight, + events, ExtBuilder, MaintenanceMode, RuntimeCall as OuterCall, RuntimeOrigin, Test, }; +use crate::{Call, Error, Event}; +use cumulus_primitives_core::AggregateMessageOrigin; +use frame_support::traits::QueuePausedQuery; +use frame_support::{assert_noop, assert_ok}; use sp_runtime::traits::Dispatchable; #[test] fn can_remark_during_normal_operation() { @@ -126,90 +123,25 @@ fn cannot_resume_normal_operation_while_already_operating_normally() { #[cfg(feature = "xcm-support")] #[test] -fn normal_dmp_in_non_maintenance() { +fn queue_pause_in_non_maintenance() { ExtBuilder::default() .with_maintenance_mode(false) .build() .execute_with(|| { assert_eq!( - MaintenanceMode::handle_dmp_messages(vec![].into_iter(), Weight::from_parts(1, 0)), - Weight::zero() + MaintenanceMode::is_paused(&AggregateMessageOrigin::Here), + false ); }) } #[cfg(feature = "xcm-support")] #[test] -fn maintenance_dmp_in_maintenance() { +fn queue_pause_in_maintenance() { ExtBuilder::default() .with_maintenance_mode(true) .build() .execute_with(|| { - assert_eq!( - MaintenanceMode::handle_dmp_messages(vec![].into_iter(), Weight::from_parts(1, 0)), - Weight::from_parts(1, 0) - ); - }) -} - -#[test] -fn normal_hooks_in_non_maintenance() { - ExtBuilder::default() - .with_maintenance_mode(false) - .build() - .execute_with(|| { - assert_eq!( - ExecutiveHooks::::on_idle(0, Weight::zero()), - Weight::zero() - ); - assert_eq!(ExecutiveHooks::::on_initialize(0), Weight::zero()); - assert_eq!(ExecutiveHooks::::on_runtime_upgrade(), Weight::zero()); - ExecutiveHooks::::on_finalize(0); - ExecutiveHooks::::offchain_worker(0); - - assert_eq!( - mock_events(), - [ - crate::mock::mock_pallet_maintenance_hooks::Event::NormalOnIdle, - crate::mock::mock_pallet_maintenance_hooks::Event::NormalOnInitialize, - crate::mock::mock_pallet_maintenance_hooks::Event::NormalOnRuntimeUpgrade, - crate::mock::mock_pallet_maintenance_hooks::Event::NormalOnFinalize, - crate::mock::mock_pallet_maintenance_hooks::Event::NormalOffchainWorker - ] - ); - }) -} - -#[test] -fn maintenance_hooks_in_maintenance() { - ExtBuilder::default() - .with_maintenance_mode(true) - .build() - .execute_with(|| { - assert_eq!( - ExecutiveHooks::::on_idle(0, Weight::zero()), - Weight::from_parts(1, 0) - ); - assert_eq!( - ExecutiveHooks::::on_initialize(0), - Weight::from_parts(1, 0) - ); - assert_eq!( - ExecutiveHooks::::on_runtime_upgrade(), - Weight::from_parts(1, 0) - ); - - ExecutiveHooks::::on_finalize(0); - ExecutiveHooks::::offchain_worker(0); - assert_eq!( - mock_events(), - [ - crate::mock::mock_pallet_maintenance_hooks::Event::MaintenanceOnIdle, - crate::mock::mock_pallet_maintenance_hooks::Event::MaintenanceOnInitialize, - crate::mock::mock_pallet_maintenance_hooks::Event::MaintenanceOnRuntimeUpgrade, - crate::mock::mock_pallet_maintenance_hooks::Event::MaintenanceOnFinalize, - crate::mock::mock_pallet_maintenance_hooks::Event::MaintenanceOffchainWorker - ] - ); + assert!(MaintenanceMode::is_paused(&AggregateMessageOrigin::Here),); }) }