Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xcm queue paused and resume #1737

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion modules/transaction-pause/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ frame-support = { git = "https://github.com/paritytech/substrate", branch = "pol
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false }

primitives = { package = "acala-primitives", path = "../../primitives", default-features = false}
polkadot-core-primitives = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.13", default-features = false }
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.13", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
orml-tokens = { path = "../../orml/tokens" }
orml-traits = { path = "../../orml/traits" }
primitives = { package = "acala-primitives", path = "../../primitives" }
smallvec = "1.4.1"

[features]
Expand All @@ -29,5 +32,8 @@ std = [
"frame-support/std",
"frame-system/std",
"sp-std/std",
"primitives/std",
"polkadot-core-primitives/std",
"cumulus-primitives-core/std",
]
try-runtime = ["frame-support/try-runtime"]
77 changes: 77 additions & 0 deletions modules/transaction-pause/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ use frame_support::{
transactional,
};
use frame_system::pallet_prelude::*;
use primitives::BlockNumber;
use sp_runtime::DispatchResult;
use sp_std::{prelude::*, vec::Vec};

use cumulus_primitives_core::relay_chain::v1::Id;
use cumulus_primitives_core::{DmpMessageHandler, XcmpMessageHandler};
/// Block number type used by the relay chain.
pub use polkadot_core_primitives::BlockNumber as RelayChainBlockNumber;

mod mock;
mod tests;
pub mod weights;
Expand Down Expand Up @@ -72,6 +78,10 @@ pub mod module {
pallet_name_bytes: Vec<u8>,
function_name_bytes: Vec<u8>,
},
/// Paused Xcm message
XcmPaused,
/// Resumed Xcm message
XcmResumed,
}

/// The paused transaction map
Expand All @@ -81,6 +91,10 @@ pub mod module {
#[pallet::getter(fn paused_transactions)]
pub type PausedTransactions<T: Config> = StorageMap<_, Twox64Concat, (Vec<u8>, Vec<u8>), (), OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn xcm_paused)]
pub type XcmPaused<T: Config> = StorageValue<_, bool, ValueQuery>;

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

Expand Down Expand Up @@ -129,6 +143,26 @@ pub mod module {
};
Ok(())
}

#[pallet::weight(T::WeightInfo::pause_xcm())]
pub fn pause_xcm(origin: OriginFor<T>) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
if !XcmPaused::<T>::get() {
XcmPaused::<T>::set(true);
Self::deposit_event(Event::XcmPaused);
}
Ok(())
}

#[pallet::weight(T::WeightInfo::resume_xcm())]
pub fn resume_xcm(origin: OriginFor<T>) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
if XcmPaused::<T>::get() {
XcmPaused::<T>::set(false);
Self::deposit_event(Event::XcmResumed);
}
Ok(())
}
}
}

Expand All @@ -145,3 +179,46 @@ where
PausedTransactions::<T>::contains_key((pallet_name.as_bytes(), function_name.as_bytes()))
}
}

/// Dmp and Xcmp message handler
pub struct XcmMessageHandler<T, H>(PhantomData<(T, H)>);

/// XcmMessageHandler implements `DmpMessageHandler`. if xcm paused, the `max_weight` is set to `0`.
///
/// Parameters type:
/// - `H`: `DmpMessageHandler`
impl<T: Config, H> DmpMessageHandler for XcmMessageHandler<T, H>
where
H: DmpMessageHandler,
{
fn handle_dmp_messages(iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>, max_weight: Weight) -> Weight {
let xcm_paused: bool = Pallet::<T>::xcm_paused();
if !xcm_paused {
H::handle_dmp_messages(iter, max_weight)
} else {
H::handle_dmp_messages(iter, 0)
}
}
}

/// XcmMessageHandler implements `XcmpMessageHandler`. if xcm paused, the `max_weight` is set to
/// `0`.
///
/// Parameters type:
/// - `H`: `XcmpMessageHandler`
impl<T: Config, H> XcmpMessageHandler for XcmMessageHandler<T, H>
where
H: XcmpMessageHandler,
{
fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, BlockNumber, &'a [u8])>>(
iter: I,
max_weight: Weight,
) -> Weight {
let xcm_paused: bool = Pallet::<T>::xcm_paused();
if !xcm_paused {
H::handle_xcmp_messages(iter, max_weight)
} else {
H::handle_xcmp_messages(iter, 0)
}
}
}
22 changes: 22 additions & 0 deletions modules/transaction-pause/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn pause_transaction() -> Weight;
fn unpause_transaction() -> Weight;
fn pause_xcm() -> Weight;
fn resume_xcm() -> Weight;
}

/// Weights for module_transaction_pause using the Acala node and recommended hardware.
Expand All @@ -64,6 +66,16 @@ impl<T: frame_system::Config> WeightInfo for AcalaWeight<T> {
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn pause_xcm() -> Weight {
(25_798_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn resume_xcm() -> Weight {
(25_355_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
}

// For backwards compatibility and tests
Expand All @@ -78,4 +90,14 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn pause_xcm() -> Weight {
(25_798_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn resume_xcm() -> Weight {
(25_355_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
}
5 changes: 3 additions & 2 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use module_evm::{CallInfo, CreateInfo, EvmTask, Runner};
use module_evm_accounts::EvmAddressMapping;
use module_relaychain::RelayChainCallBuilder;
use module_support::{AssetIdMapping, DispatchableTask};
use module_transaction_pause::XcmMessageHandler;
use module_transaction_payment::{Multiplier, TargetedFeeAdjustment, TransactionFeePoolTrader};
use orml_traits::{
create_median_value_data_provider, parameter_type_with_key, DataFeeder, DataProviderExtended, MultiCurrency,
Expand Down Expand Up @@ -1366,10 +1367,10 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type Event = Event;
type OnValidationData = ();
type SelfParaId = ParachainInfo;
type DmpMessageHandler = DmpQueue;
type DmpMessageHandler = XcmMessageHandler<Runtime, DmpQueue>;
type ReservedDmpWeight = ReservedDmpWeight;
type OutboundXcmpMessageSource = XcmpQueue;
type XcmpMessageHandler = XcmpQueue;
type XcmpMessageHandler = XcmMessageHandler<Runtime, XcmpQueue>;
type ReservedXcmpWeight = ReservedXcmpWeight;
}

Expand Down
18 changes: 13 additions & 5 deletions runtime/acala/src/weights/module_transaction_pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! Autogenerated weights for module_transaction_pause
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2022-01-05, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-latest"), DB CACHE: 128

// Executed Command:
Expand All @@ -28,15 +28,14 @@
// --chain=acala-latest
// --steps=50
// --repeat=20
// --pallet=*
// --pallet=module_transaction_pause
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --template=./templates/runtime-weight-template.hbs
// --output=./runtime/acala/src/weights/


#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
Expand All @@ -48,13 +47,22 @@ use sp_std::marker::PhantomData;
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> module_transaction_pause::WeightInfo for WeightInfo<T> {
fn pause_transaction() -> Weight {
(23_319_000 as Weight)
(23_381_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn unpause_transaction() -> Weight {
(23_899_000 as Weight)
(23_853_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn pause_xcm() -> Weight {
(20_265_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn resume_xcm() -> Weight {
(4_521_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
}
}
3 changes: 2 additions & 1 deletion runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ orml-unknown-tokens = { path = "../../orml/unknown-tokens" }
orml-xcm = { path = "../../orml/xcm" }

module-transaction-payment = { path = "../../modules/transaction-payment" }
module-transaction-pause = { path = "../../modules/transaction-pause" }
module-asset-registry = { path = "../../modules/asset-registry" }
module-auction-manager = { path = "../../modules/auction-manager" }
module-cdp-engine = { path = "../../modules/cdp-engine" }
Expand Down Expand Up @@ -133,7 +134,7 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch =
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.13" }
kusama-runtime = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.13" }

xcm-emulator = { git = "https://github.com/shaunxw/xcm-simulator", rev = "4d3bb9dd4fa2cd554a9970ffff816d9346269eaa" }
xcm-emulator = { git = "https://github.com/shaunxw/xcm-simulator", rev = "87af52d07fc742cc9220bab6acb9afa061d09fd3" }

acala-service = { path = "../../node/service", features = ["with-all-runtime"] }

Expand Down
Loading