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

XCM: Limit the max number of assets weighable in MultiAssets #6786

Merged
merged 6 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 12 additions & 3 deletions runtime/kusama/src/weights/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ trait WeighMultiAssets {
}

// Kusama only knows about one asset, the balances pallet.
const MAX_ASSETS: u32 = 1;
const MAX_ASSETS: u64 = 1;

impl WeighMultiAssets for MultiAssetFilter {
fn weigh_multi_assets(&self, balances_weight: Weight) -> Weight {
Expand All @@ -63,8 +63,9 @@ impl WeighMultiAssets for MultiAssetFilter {
})
.fold(Weight::zero(), |acc, x| acc.saturating_add(x)),
Self::Wild(AllOf { .. } | AllOfCounted { .. }) => balances_weight,
Self::Wild(AllCounted(count)) => balances_weight.saturating_mul(*count as u64),
Self::Wild(All) => balances_weight.saturating_mul(MAX_ASSETS as u64),
Self::Wild(AllCounted(count)) =>
balances_weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
Self::Wild(All) => balances_weight.saturating_mul(MAX_ASSETS),
}
}
}
Expand Down Expand Up @@ -275,3 +276,11 @@ impl<RuntimeCall> XcmWeightInfo<RuntimeCall> for KusamaXcmWeight<RuntimeCall> {
XcmGeneric::<Runtime>::unpaid_execution()
}
}

#[test]
fn all_counted_has_a_sane_weight_upper_limit() {
let assets = MultiAssetFilter::Wild(AllCounted(4294967295));
let weight = Weight::from_ref_time(1000);
KiChjang marked this conversation as resolved.
Show resolved Hide resolved

assert_eq!(assets.weigh_multi_assets(weight), weight * MAX_ASSETS);
}
15 changes: 15 additions & 0 deletions runtime/kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,18 @@ impl pallet_xcm::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest;
}

#[test]
fn karura_liquid_staking_xcm_has_sane_weight_upper_limt() {
use parity_scale_codec::Decode;
use xcm::VersionedXcm;
use xcm_executor::traits::WeightBounds;
let blob = hex_literal::hex!("02140004000000000700e40b540213000000000700e40b54020006010700c817a804341801000006010b00c490bf4302140d010003ffffffff000100411f");
let Ok(VersionedXcm::V2(old_xcm)) = VersionedXcm::<super::RuntimeCall>::decode(&mut &blob[..]) else { panic!("can't decode XCM blob") };
let mut xcm: Xcm<super::RuntimeCall> =
old_xcm.try_into().expect("conversion from v2 to v3 failed");
let weight = <XcmConfig as xcm_executor::Config>::Weigher::weight(&mut xcm)
.expect("weighing XCM failed");

assert_eq!(weight.ref_time(), 20_313_281_000);
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
}
15 changes: 12 additions & 3 deletions runtime/rococo/src/weights/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ trait WeighMultiAssets {
}

// Rococo only knows about one asset, the balances pallet.
const MAX_ASSETS: u32 = 1;
const MAX_ASSETS: u64 = 1;

impl WeighMultiAssets for MultiAssetFilter {
fn weigh_multi_assets(&self, balances_weight: Weight) -> Weight {
Expand All @@ -63,8 +63,9 @@ impl WeighMultiAssets for MultiAssetFilter {
})
.fold(Weight::zero(), |acc, x| acc.saturating_add(x)),
Self::Wild(AllOf { .. } | AllOfCounted { .. }) => balances_weight,
Self::Wild(AllCounted(count)) => balances_weight.saturating_mul(*count as u64),
Self::Wild(All) => balances_weight.saturating_mul(MAX_ASSETS as u64),
Self::Wild(AllCounted(count)) =>
balances_weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
Self::Wild(All) => balances_weight.saturating_mul(MAX_ASSETS),
}
}
}
Expand Down Expand Up @@ -275,3 +276,11 @@ impl<RuntimeCall> XcmWeightInfo<RuntimeCall> for RococoXcmWeight<RuntimeCall> {
XcmGeneric::<Runtime>::unpaid_execution()
}
}

#[test]
fn all_counted_has_a_sane_weight_upper_limit() {
let assets = MultiAssetFilter::Wild(AllCounted(4294967295));
let weight = Weight::from_ref_time(1000);
KiChjang marked this conversation as resolved.
Show resolved Hide resolved

assert_eq!(assets.weigh_multi_assets(weight), weight * MAX_ASSETS);
}
15 changes: 12 additions & 3 deletions runtime/westend/src/weights/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ trait WeighMultiAssets {
}

// Westend only knows about one asset, the balances pallet.
const MAX_ASSETS: u32 = 1;
const MAX_ASSETS: u64 = 1;

impl WeighMultiAssets for MultiAssetFilter {
fn weigh_multi_assets(&self, balances_weight: Weight) -> Weight {
Expand All @@ -66,8 +66,9 @@ impl WeighMultiAssets for MultiAssetFilter {
})
.fold(Weight::zero(), |acc, x| acc.saturating_add(x)),
Self::Wild(AllOf { .. } | AllOfCounted { .. }) => balances_weight,
Self::Wild(AllCounted(count)) => balances_weight.saturating_mul(*count as u64),
Self::Wild(All) => balances_weight.saturating_mul(MAX_ASSETS as u64),
Self::Wild(AllCounted(count)) =>
balances_weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
Self::Wild(All) => balances_weight.saturating_mul(MAX_ASSETS),
}
}
}
Expand Down Expand Up @@ -279,3 +280,11 @@ impl<RuntimeCall> XcmWeightInfo<RuntimeCall> for WestendXcmWeight<RuntimeCall> {
XcmGeneric::<Runtime>::unpaid_execution()
}
}

#[test]
fn all_counted_has_a_sane_weight_upper_limit() {
let assets = MultiAssetFilter::Wild(AllCounted(4294967295));
let weight = Weight::from_ref_time(1000);
KiChjang marked this conversation as resolved.
Show resolved Hide resolved

assert_eq!(assets.weigh_multi_assets(weight), weight * MAX_ASSETS);
}