Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 5 additions & 48 deletions pallets/maintenance-mode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<BlockNumberFor<Self>>
+ OnIdle<BlockNumberFor<Self>>
+ OnFinalize<BlockNumberFor<Self>>
+ OffchainWorker<BlockNumberFor<Self>>;
/// 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<BlockNumberFor<Self>>
+ OnIdle<BlockNumberFor<Self>>
+ OnFinalize<BlockNumberFor<Self>>
+ OffchainWorker<BlockNumberFor<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -246,17 +209,11 @@ pub mod pallet {
}
}
}

#[cfg(feature = "xcm-support")]
impl<T: Config> DmpMessageHandler for Pallet<T> {
fn handle_dmp_messages(
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
limit: Weight,
) -> Weight {
if MaintenanceMode::<T>::get() {
T::MaintenanceDmpHandler::handle_dmp_messages(iter, limit)
} else {
T::NormalDmpHandler::handle_dmp_messages(iter, limit)
}
impl<T: Config, Origin> QueuePausedQuery<Origin> for Pallet<T> {
fn is_paused(_origin: &Origin) -> bool {
MaintenanceMode::<T>::get()
}
}
}
179 changes: 1 addition & 178 deletions pallets/maintenance-mode/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Test>;

Expand All @@ -44,7 +40,6 @@ construct_runtime!(
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
MaintenanceMode: pallet_maintenance_mode::{Pallet, Call, Storage, Event, Config<T>},
MockPalletMaintenanceHooks: mock_pallet_maintenance_hooks::{Pallet, Call, Event},
}
);

Expand Down Expand Up @@ -90,171 +85,13 @@ impl Contains<RuntimeCall> 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<Item = (RelayBlockNumber, Vec<u8>)>,
_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<Item = (RelayBlockNumber, Vec<u8>)>,
_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<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::call]
impl<T: Config> Pallet<T> {}

#[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<BlockNumber> for MaintenanceHooks {
fn on_initialize(_n: BlockNumber) -> Weight {
MockPalletMaintenanceHooks::deposit_event(
mock_pallet_maintenance_hooks::Event::MaintenanceOnInitialize,
);
Weight::from_parts(1, 0)
}
}

impl OnIdle<BlockNumber> 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<BlockNumber> for MaintenanceHooks {
fn on_finalize(_n: BlockNumber) {
MockPalletMaintenanceHooks::deposit_event(
mock_pallet_maintenance_hooks::Event::MaintenanceOnFinalize,
);
}
}

impl OffchainWorker<BlockNumber> for MaintenanceHooks {
fn offchain_worker(_n: BlockNumber) {
MockPalletMaintenanceHooks::deposit_event(
mock_pallet_maintenance_hooks::Event::MaintenanceOffchainWorker,
);
}
}

pub struct NormalHooks;

impl OnInitialize<BlockNumber> for NormalHooks {
fn on_initialize(_n: BlockNumber) -> Weight {
MockPalletMaintenanceHooks::deposit_event(
mock_pallet_maintenance_hooks::Event::NormalOnInitialize,
);
Weight::zero()
}
}

impl OnIdle<BlockNumber> 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<BlockNumber> for NormalHooks {
fn on_finalize(_n: BlockNumber) {
MockPalletMaintenanceHooks::deposit_event(
mock_pallet_maintenance_hooks::Event::NormalOnFinalize,
);
}
}

impl OffchainWorker<BlockNumber> 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;
type MaintenanceCallFilter = MaintenanceCallFilter;
type MaintenanceOrigin = EnsureRoot<AccountId>;
#[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
Expand Down Expand Up @@ -309,17 +146,3 @@ pub(crate) fn events() -> Vec<pallet_maintenance_mode::Event> {
})
.collect::<Vec<_>>()
}

pub(crate) fn mock_events() -> Vec<mock_pallet_maintenance_hooks::Event> {
System::events()
.into_iter()
.map(|r| r.event)
.filter_map(|e| {
if let RuntimeEvent::MockPalletMaintenanceHooks(inner) = e {
Some(inner)
} else {
None
}
})
.collect::<Vec<_>>()
}
Loading