From 14115c82de55de2d581e9ad9f7b8a4c7093c2f71 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Mon, 19 Sep 2022 20:29:31 +0200 Subject: [PATCH] [Fix] Weight calculations for Wild -> affects teleports (#1639) * [Fix] Weight calculations for Wild -> affects teleports * introduce a separate asset limit for teleports * fix deposit * reshuffle abstractions * fix imports * little refactoring * Update parachains/common/src/xcm_config.rs Co-authored-by: Squirrel * add comments Co-authored-by: Squirrel --- parachains/common/src/xcm_config.rs | 14 +++++++++ .../assets/statemine/src/weights/xcm/mod.rs | 29 ++++++++++++------- .../assets/statemint/src/weights/xcm/mod.rs | 29 ++++++++++++------- .../assets/westmint/src/weights/xcm/mod.rs | 29 ++++++++++++------- 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/parachains/common/src/xcm_config.rs b/parachains/common/src/xcm_config.rs index b9ae5ef87ce..346e0f9cb61 100644 --- a/parachains/common/src/xcm_config.rs +++ b/parachains/common/src/xcm_config.rs @@ -119,3 +119,17 @@ impl> FilterAssetLocation for ConcreteNativeAssetFr matches!(asset.id, Concrete(ref id) if id == origin && origin == &Location::get()) } } + +/// A generic function to use for MultiAssetFilter implementations, currently used to differentiate +/// between reserve operations and the rest of them. +pub fn weigh_multi_assets_generic( + filter: &MultiAssetFilter, + weight: Weight, + max_assets: u32, +) -> XCMWeight { + let multiplier = match filter { + MultiAssetFilter::Definite(assets) => assets.len() as u64, + MultiAssetFilter::Wild(_) => max_assets as u64, + }; + weight.saturating_mul(multiplier).ref_time() +} diff --git a/parachains/runtimes/assets/statemine/src/weights/xcm/mod.rs b/parachains/runtimes/assets/statemine/src/weights/xcm/mod.rs index 79fbd0812bb..9dd4345ef73 100644 --- a/parachains/runtimes/assets/statemine/src/weights/xcm/mod.rs +++ b/parachains/runtimes/assets/statemine/src/weights/xcm/mod.rs @@ -21,7 +21,7 @@ use crate::Runtime; use frame_support::weights::Weight; use pallet_xcm_benchmarks_fungible::WeightInfo as XcmFungibleWeight; use pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric; -use sp_std::prelude::*; +use parachains_common::xcm_config::weigh_multi_assets_generic; use xcm::{ latest::{prelude::*, Weight as XCMWeight}, DoubleEncoded, @@ -31,22 +31,29 @@ trait WeighMultiAssets { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight; } -const MAX_ASSETS: u32 = 100; +trait WeighMultiAssetsReserve { + fn weigh_multi_assets_reserve(&self, weight: Weight) -> XCMWeight; +} + +const RESERVE_MAX_ASSETS: u32 = 100; +/// For teleports and deposits +const MAX_ASSETS: u32 = 1; impl WeighMultiAssets for MultiAssetFilter { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight { - let weight = match self { - Self::Definite(assets) => - weight.saturating_mul(assets.inner().into_iter().count() as u64), - Self::Wild(_) => weight.saturating_mul(MAX_ASSETS as u64), - }; - weight.ref_time() + weigh_multi_assets_generic(self, weight, MAX_ASSETS) + } +} + +impl WeighMultiAssetsReserve for MultiAssetFilter { + fn weigh_multi_assets_reserve(&self, weight: Weight) -> XCMWeight { + weigh_multi_assets_generic(self, weight, RESERVE_MAX_ASSETS) } } impl WeighMultiAssets for MultiAssets { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight { - weight.saturating_mul(self.inner().into_iter().count() as u64).ref_time() + weight.saturating_mul(self.len() as u64).ref_time() } } @@ -125,7 +132,7 @@ impl XcmWeightInfo for StatemineXcmWeight { _dest: &MultiLocation, _xcm: &Xcm<()>, ) -> XCMWeight { - assets.weigh_multi_assets(XcmFungibleWeight::::deposit_reserve_asset()) + assets.weigh_multi_assets_reserve(XcmFungibleWeight::::deposit_reserve_asset()) } fn exchange_asset(_give: &MultiAssetFilter, _receive: &MultiAssets) -> XCMWeight { Weight::MAX.ref_time() @@ -135,7 +142,7 @@ impl XcmWeightInfo for StatemineXcmWeight { _reserve: &MultiLocation, _xcm: &Xcm<()>, ) -> XCMWeight { - assets.weigh_multi_assets(XcmGeneric::::initiate_reserve_withdraw()) + assets.weigh_multi_assets_reserve(XcmGeneric::::initiate_reserve_withdraw()) } fn initiate_teleport( assets: &MultiAssetFilter, diff --git a/parachains/runtimes/assets/statemint/src/weights/xcm/mod.rs b/parachains/runtimes/assets/statemint/src/weights/xcm/mod.rs index b51f8b207a5..2fbe469aab7 100644 --- a/parachains/runtimes/assets/statemint/src/weights/xcm/mod.rs +++ b/parachains/runtimes/assets/statemint/src/weights/xcm/mod.rs @@ -21,7 +21,7 @@ use crate::Runtime; use frame_support::weights::Weight; use pallet_xcm_benchmarks_fungible::WeightInfo as XcmFungibleWeight; use pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric; -use sp_std::prelude::*; +use parachains_common::xcm_config::weigh_multi_assets_generic; use xcm::{ latest::{prelude::*, Weight as XCMWeight}, DoubleEncoded, @@ -31,22 +31,29 @@ trait WeighMultiAssets { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight; } -const MAX_ASSETS: u32 = 100; +trait WeighMultiAssetsReserve { + fn weigh_multi_assets_reserve(&self, weight: Weight) -> XCMWeight; +} + +const RESERVE_MAX_ASSETS: u32 = 100; +/// For teleports and deposits +const MAX_ASSETS: u32 = 1; impl WeighMultiAssets for MultiAssetFilter { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight { - let weight = match self { - Self::Definite(assets) => - weight.saturating_mul(assets.inner().into_iter().count() as u64), - Self::Wild(_) => weight.saturating_mul(MAX_ASSETS as u64), - }; - weight.ref_time() + weigh_multi_assets_generic(self, weight, MAX_ASSETS) + } +} + +impl WeighMultiAssetsReserve for MultiAssetFilter { + fn weigh_multi_assets_reserve(&self, weight: Weight) -> XCMWeight { + weigh_multi_assets_generic(self, weight, RESERVE_MAX_ASSETS) } } impl WeighMultiAssets for MultiAssets { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight { - weight.saturating_mul(self.inner().into_iter().count() as u64).ref_time() + weight.saturating_mul(self.len() as u64).ref_time() } } @@ -125,7 +132,7 @@ impl XcmWeightInfo for StatemintXcmWeight { _dest: &MultiLocation, _xcm: &Xcm<()>, ) -> XCMWeight { - assets.weigh_multi_assets(XcmFungibleWeight::::deposit_reserve_asset()) + assets.weigh_multi_assets_reserve(XcmFungibleWeight::::deposit_reserve_asset()) } fn exchange_asset(_give: &MultiAssetFilter, _receive: &MultiAssets) -> XCMWeight { Weight::MAX.ref_time() @@ -135,7 +142,7 @@ impl XcmWeightInfo for StatemintXcmWeight { _reserve: &MultiLocation, _xcm: &Xcm<()>, ) -> XCMWeight { - assets.weigh_multi_assets(XcmGeneric::::initiate_reserve_withdraw()) + assets.weigh_multi_assets_reserve(XcmGeneric::::initiate_reserve_withdraw()) } fn initiate_teleport( assets: &MultiAssetFilter, diff --git a/parachains/runtimes/assets/westmint/src/weights/xcm/mod.rs b/parachains/runtimes/assets/westmint/src/weights/xcm/mod.rs index 018f8f7d9f7..0c1f96ae7d7 100644 --- a/parachains/runtimes/assets/westmint/src/weights/xcm/mod.rs +++ b/parachains/runtimes/assets/westmint/src/weights/xcm/mod.rs @@ -21,7 +21,7 @@ use crate::Runtime; use frame_support::weights::Weight; use pallet_xcm_benchmarks_fungible::WeightInfo as XcmFungibleWeight; use pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric; -use sp_std::prelude::*; +use parachains_common::xcm_config::weigh_multi_assets_generic; use xcm::{ latest::{prelude::*, Weight as XCMWeight}, DoubleEncoded, @@ -31,22 +31,29 @@ trait WeighMultiAssets { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight; } -const MAX_ASSETS: u32 = 100; +trait WeighMultiAssetsReserve { + fn weigh_multi_assets_reserve(&self, weight: Weight) -> XCMWeight; +} + +const RESERVE_MAX_ASSETS: u32 = 100; +/// For teleports and deposits +const MAX_ASSETS: u32 = 1; impl WeighMultiAssets for MultiAssetFilter { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight { - let weight = match self { - Self::Definite(assets) => - weight.saturating_mul(assets.inner().into_iter().count() as u64), - Self::Wild(_) => weight.saturating_mul(MAX_ASSETS as u64), - }; - weight.ref_time() + weigh_multi_assets_generic(self, weight, MAX_ASSETS) + } +} + +impl WeighMultiAssetsReserve for MultiAssetFilter { + fn weigh_multi_assets_reserve(&self, weight: Weight) -> XCMWeight { + weigh_multi_assets_generic(self, weight, RESERVE_MAX_ASSETS) } } impl WeighMultiAssets for MultiAssets { fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight { - weight.saturating_mul(self.inner().into_iter().count() as u64).ref_time() + weight.saturating_mul(self.len() as u64).ref_time() } } @@ -125,7 +132,7 @@ impl XcmWeightInfo for WestmintXcmWeight { _dest: &MultiLocation, _xcm: &Xcm<()>, ) -> XCMWeight { - assets.weigh_multi_assets(XcmFungibleWeight::::deposit_reserve_asset()) + assets.weigh_multi_assets_reserve(XcmFungibleWeight::::deposit_reserve_asset()) } fn exchange_asset(_give: &MultiAssetFilter, _receive: &MultiAssets) -> XCMWeight { Weight::MAX.ref_time() @@ -135,7 +142,7 @@ impl XcmWeightInfo for WestmintXcmWeight { _reserve: &MultiLocation, _xcm: &Xcm<()>, ) -> XCMWeight { - assets.weigh_multi_assets(XcmGeneric::::initiate_reserve_withdraw()) + assets.weigh_multi_assets_reserve(XcmGeneric::::initiate_reserve_withdraw()) } fn initiate_teleport( assets: &MultiAssetFilter,