From bfac62d14c9678812469f4a1cde42c556be00cb9 Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Tue, 18 Oct 2022 11:26:21 +0800 Subject: [PATCH] add xcm fee limit --- runtime/common/src/message_router/mod.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/runtime/common/src/message_router/mod.rs b/runtime/common/src/message_router/mod.rs index dc66893d..08f83ce9 100644 --- a/runtime/common/src/message_router/mod.rs +++ b/runtime/common/src/message_router/mod.rs @@ -30,6 +30,8 @@ use xcm::prelude::*; use xcm_executor::traits::WeightBounds; pub type AssetUnitsPerSecond = u128; +/// Fee type of xcm message fee, paid on this chain. +pub type XcmFee = u128; /// router target #[derive(Clone, Encode, Decode, TypeInfo, PartialEq, Debug)] @@ -74,7 +76,7 @@ pub mod pallet { target_location: MultiLocation, local_asset_units_per_second: AssetUnitsPerSecond, }, - ForwardTo(MultiLocation, Target, Xcm<()>, Weight, u128), + ForwardTo(MultiLocation, Target, Xcm<()>, Weight, XcmFee), } #[pallet::error] @@ -83,14 +85,17 @@ pub mod pallet { TargetXcmExecNotConfig, /// The message's weight could not be determined. UnweighableMessage, - /// XCM execution failed. https://github.com/paritytech/substrate/pull/10242 - XcmExecutionFailed, + /// Failed to transfer xcm fee. + FailedPayXcmFee, BadVersion, /// MultiLocation value too large to descend further. MultiLocationFull, /// Failed to send xcm. XcmSendFailed, + /// Failed to convert account id to [u8; 32]. AccountIdConversionFailed, + /// When the `xcm_fee_limit` is less than required. + TooExpensive, } /// Stores the units per second executed by the target chain for local asset(e.g. CRAB). @@ -136,6 +141,7 @@ pub mod pallet { origin: OriginFor, target: Target, message: Box::Call>>, + xcm_fee_limit: Option, ) -> DispatchResultWithPostInfo { // MultiLocation origin used to execute xcm let origin_location = T::ExecuteXcmOrigin::ensure_origin(origin.clone())?; @@ -164,8 +170,12 @@ pub mod pallet { .map_err(|()| Error::::UnweighableMessage)?; }, } - let amount = local_asset_units_per_second.saturating_mul(remote_weight as u128) - / (WEIGHT_PER_SECOND as u128); + let amount: XcmFee = local_asset_units_per_second + .saturating_mul(remote_weight as XcmFee) + / (WEIGHT_PER_SECOND as XcmFee); + if let Some(fee_limit) = xcm_fee_limit { + ensure!(amount <= fee_limit, Error::::TooExpensive); + } let remote_xcm_fee = MultiAsset { id: AssetId::from(T::LocalAssetId::get()), fun: Fungible(amount) }; @@ -189,8 +199,8 @@ pub mod pallet { ) .ensure_complete() .map_err(|error| { - log::error!("Failed execute route message with {:?}", error); - Error::::XcmExecutionFailed + log::error!("Failed transfer xcm fee with {:?}", error); + Error::::FailedPayXcmFee })?; // Toggle the xcm_fee relative to a target context