Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add the ability to suspend the DMP queue
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed Jan 13, 2022
1 parent 1839ffc commit 363ca09
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
60 changes: 60 additions & 0 deletions pallets/dmp-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub mod pallet {

/// Origin which is allowed to execute overweight messages.
type ExecuteOverweightOrigin: EnsureOrigin<Self::Origin>;

/// The origin that is allowed to resume or suspend the XCMP queue.
type ControllerOrigin: EnsureOrigin<Self::Origin>;
}

/// The configuration.
Expand All @@ -109,6 +112,10 @@ pub mod pallet {
pub(super) type Overweight<T> =
StorageMap<_, Blake2_128Concat, OverweightIndex, (RelayBlockNumber, Vec<u8>), OptionQuery>;

/// Whether or not the XCMP queue is suspended from executing incoming XCMs or not.
#[pallet::storage]
pub(super) type QueueSuspended<T: Config> = StorageValue<_, bool, ValueQuery>;

#[pallet::error]
pub enum Error<T> {
/// The message index given is unknown.
Expand Down Expand Up @@ -154,6 +161,32 @@ pub mod pallet {
Self::deposit_event(Event::OverweightServiced(index, used));
Ok(Some(used.saturating_add(1_000_000)).into())
}

/// Suspends all XCM executions for the XCMP queue, regardless of the sender's origin.
///
/// - `origin`: Must pass `ControllerOrigin`.
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn suspend_xcm_execution(origin: OriginFor<T>) -> DispatchResult {
T::ControllerOrigin::ensure_origin(origin)?;

QueueSuspended::<T>::put(true);

Ok(())
}

/// Resumes all XCM executions for the XCMP queue.
///
/// Note that this function doesn't change the status of the in/out bound channels.
///
/// - `origin`: Must pass `ControllerOrigin`.
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn resume_xcm_execution(origin: OriginFor<T>) -> DispatchResult {
T::ControllerOrigin::ensure_origin(origin)?;

QueueSuspended::<T>::put(false);

Ok(())
}
}

#[pallet::event]
Expand Down Expand Up @@ -184,6 +217,11 @@ pub mod pallet {
///
/// Returns the weight consumed by executing messages in the queue.
fn service_queue(limit: Weight) -> Weight {
let suspended = QueueSuspended::<T>::get();
if suspended {
return 0
}

PageIndex::<T>::mutate(|page_index| Self::do_service_queue(limit, page_index))
}

Expand Down Expand Up @@ -263,6 +301,11 @@ pub mod pallet {
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
limit: Weight,
) -> Weight {
let suspended = QueueSuspended::<T>::get();
if suspended {
return 0
}

let mut page_index = PageIndex::<T>::get();
let config = Configuration::<T>::get();

Expand Down Expand Up @@ -447,6 +490,7 @@ mod tests {
type Event = Event;
type XcmExecutor = MockExec;
type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;
type ControllerOrigin = frame_system::EnsureRoot<AccountId>;
}

pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
Expand Down Expand Up @@ -786,4 +830,20 @@ mod tests {
assert_eq!(pages_queued(), 1);
});
}

#[test]
fn suspend_xcm_execution_works() {
new_test_ext().execute_with(|| {
QueueSuspended::<Test>::put(true);

let incoming = [msg(1000), msg(1001)];
enqueue(&incoming[..]);

let weight_used = handle_messages(&incoming, 5000);

assert_eq!(weight_used, 0);
assert_eq!(take_trace(), Vec::new());
assert_eq!(pages_queued(), 1);
});
}
}
1 change: 1 addition & 0 deletions parachain-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ impl cumulus_pallet_dmp_queue::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type ControllerOrigin = EnsureRoot<AccountId>;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion polkadot-parachains/rococo-parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime {
impl cumulus_pallet_dmp_queue::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type ControllerOrigin = EnsureRoot<AccountId>;
}

impl cumulus_ping::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions polkadot-parachains/statemine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,14 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime {
type ChannelInfo = ParachainSystem;
type VersionWrapper = PolkadotXcm;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type ControllerOrigin = EnsureRoot<AccountId>;
}

impl cumulus_pallet_dmp_queue::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type ControllerOrigin = EnsureRoot<AccountId>;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions polkadot-parachains/statemint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ impl cumulus_pallet_dmp_queue::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type ControllerOrigin = EnsureRoot<AccountId>;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions polkadot-parachains/westmint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ impl cumulus_pallet_dmp_queue::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type ControllerOrigin = EnsureRoot<AccountId>;
}

parameter_types! {
Expand Down

0 comments on commit 363ca09

Please sign in to comment.