referrerpolicy=no-referrer-when-downgrade

westend_runtime/weights/xcm/
mod.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17mod pallet_xcm_benchmarks_fungible;
18mod pallet_xcm_benchmarks_generic;
19
20use crate::Runtime;
21use alloc::vec::Vec;
22use frame_support::weights::Weight;
23use xcm::{
24	latest::{prelude::*, QueryResponseInfo},
25	DoubleEncoded,
26};
27
28use pallet_xcm_benchmarks_fungible::WeightInfo as XcmBalancesWeight;
29use pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric;
30use sp_runtime::BoundedVec;
31use xcm::latest::AssetTransferFilter;
32
33/// Types of asset supported by the westend runtime.
34pub enum AssetTypes {
35	/// An asset backed by `pallet-balances`.
36	Balances,
37	/// Unknown asset.
38	Unknown,
39}
40
41impl From<&Asset> for AssetTypes {
42	fn from(asset: &Asset) -> Self {
43		match asset {
44			Asset { id: AssetId(Location { parents: 0, interior: Here }), .. } =>
45				AssetTypes::Balances,
46			_ => AssetTypes::Unknown,
47		}
48	}
49}
50
51trait WeighAssets {
52	fn weigh_assets(&self, balances_weight: Weight) -> Weight;
53}
54
55// Westend only knows about one asset, the balances pallet.
56const MAX_ASSETS: u64 = 1;
57
58impl WeighAssets for AssetFilter {
59	fn weigh_assets(&self, balances_weight: Weight) -> Weight {
60		match self {
61			Self::Definite(assets) => assets
62				.inner()
63				.into_iter()
64				.map(From::from)
65				.map(|t| match t {
66					AssetTypes::Balances => balances_weight,
67					AssetTypes::Unknown => Weight::MAX,
68				})
69				.fold(Weight::zero(), |acc, x| acc.saturating_add(x)),
70			// We don't support any NFTs on Westend, so these two variants will always match
71			// only 1 kind of fungible asset.
72			Self::Wild(AllOf { .. } | AllOfCounted { .. }) => balances_weight,
73			Self::Wild(AllCounted(count)) =>
74				balances_weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
75			Self::Wild(All) => balances_weight.saturating_mul(MAX_ASSETS),
76		}
77	}
78}
79
80impl WeighAssets for Assets {
81	fn weigh_assets(&self, balances_weight: Weight) -> Weight {
82		self.inner()
83			.into_iter()
84			.map(|m| <AssetTypes as From<&Asset>>::from(m))
85			.map(|t| match t {
86				AssetTypes::Balances => balances_weight,
87				AssetTypes::Unknown => Weight::MAX,
88			})
89			.fold(Weight::zero(), |acc, x| acc.saturating_add(x))
90	}
91}
92
93pub struct WestendXcmWeight<RuntimeCall>(core::marker::PhantomData<RuntimeCall>);
94impl<RuntimeCall> XcmWeightInfo<RuntimeCall> for WestendXcmWeight<RuntimeCall> {
95	fn withdraw_asset(assets: &Assets) -> Weight {
96		assets.weigh_assets(XcmBalancesWeight::<Runtime>::withdraw_asset())
97	}
98	fn reserve_asset_deposited(assets: &Assets) -> Weight {
99		assets.weigh_assets(XcmBalancesWeight::<Runtime>::reserve_asset_deposited())
100	}
101	fn receive_teleported_asset(assets: &Assets) -> Weight {
102		assets.weigh_assets(XcmBalancesWeight::<Runtime>::receive_teleported_asset())
103	}
104	fn query_response(
105		_query_id: &u64,
106		_response: &Response,
107		_max_weight: &Weight,
108		_querier: &Option<Location>,
109	) -> Weight {
110		XcmGeneric::<Runtime>::query_response()
111	}
112	fn transfer_asset(assets: &Assets, _dest: &Location) -> Weight {
113		assets.weigh_assets(XcmBalancesWeight::<Runtime>::transfer_asset())
114	}
115	fn transfer_reserve_asset(assets: &Assets, _dest: &Location, _xcm: &Xcm<()>) -> Weight {
116		assets.weigh_assets(XcmBalancesWeight::<Runtime>::transfer_reserve_asset())
117	}
118	fn transact(
119		_origin_kind: &OriginKind,
120		_fallback_max_weight: &Option<Weight>,
121		_call: &DoubleEncoded<RuntimeCall>,
122	) -> Weight {
123		XcmGeneric::<Runtime>::transact()
124	}
125	fn hrmp_new_channel_open_request(
126		_sender: &u32,
127		_max_message_size: &u32,
128		_max_capacity: &u32,
129	) -> Weight {
130		// XCM Executor does not currently support HRMP channel operations
131		Weight::MAX
132	}
133	fn hrmp_channel_accepted(_recipient: &u32) -> Weight {
134		// XCM Executor does not currently support HRMP channel operations
135		Weight::MAX
136	}
137	fn hrmp_channel_closing(_initiator: &u32, _sender: &u32, _recipient: &u32) -> Weight {
138		// XCM Executor does not currently support HRMP channel operations
139		Weight::MAX
140	}
141	fn clear_origin() -> Weight {
142		XcmGeneric::<Runtime>::clear_origin()
143	}
144	fn descend_origin(_who: &InteriorLocation) -> Weight {
145		XcmGeneric::<Runtime>::descend_origin()
146	}
147	fn report_error(_query_response_info: &QueryResponseInfo) -> Weight {
148		XcmGeneric::<Runtime>::report_error()
149	}
150
151	fn deposit_asset(assets: &AssetFilter, _dest: &Location) -> Weight {
152		assets.weigh_assets(XcmBalancesWeight::<Runtime>::deposit_asset())
153	}
154	fn deposit_reserve_asset(assets: &AssetFilter, _dest: &Location, _xcm: &Xcm<()>) -> Weight {
155		assets.weigh_assets(XcmBalancesWeight::<Runtime>::deposit_reserve_asset())
156	}
157	fn exchange_asset(_give: &AssetFilter, _receive: &Assets, _maximal: &bool) -> Weight {
158		// Westend does not currently support exchange asset operations
159		Weight::MAX
160	}
161	fn initiate_reserve_withdraw(
162		assets: &AssetFilter,
163		_reserve: &Location,
164		_xcm: &Xcm<()>,
165	) -> Weight {
166		assets.weigh_assets(XcmBalancesWeight::<Runtime>::initiate_reserve_withdraw())
167	}
168	fn initiate_teleport(assets: &AssetFilter, _dest: &Location, _xcm: &Xcm<()>) -> Weight {
169		assets.weigh_assets(XcmBalancesWeight::<Runtime>::initiate_teleport())
170	}
171	fn initiate_transfer(
172		_dest: &Location,
173		remote_fees: &Option<AssetTransferFilter>,
174		_preserve_origin: &bool,
175		assets: &BoundedVec<AssetTransferFilter, MaxAssetTransferFilters>,
176		_xcm: &Xcm<()>,
177	) -> Weight {
178		let mut weight = if let Some(remote_fees) = remote_fees {
179			let fees = remote_fees.inner();
180			fees.weigh_assets(XcmBalancesWeight::<Runtime>::initiate_transfer())
181		} else {
182			Weight::zero()
183		};
184		for asset_filter in assets {
185			let assets = asset_filter.inner();
186			let extra = assets.weigh_assets(XcmBalancesWeight::<Runtime>::initiate_transfer());
187			weight = weight.saturating_add(extra);
188		}
189		weight
190	}
191	fn report_holding(_response_info: &QueryResponseInfo, _assets: &AssetFilter) -> Weight {
192		XcmGeneric::<Runtime>::report_holding()
193	}
194	fn buy_execution(_fees: &Asset, _weight_limit: &WeightLimit) -> Weight {
195		XcmGeneric::<Runtime>::buy_execution()
196	}
197	fn pay_fees(_asset: &Asset) -> Weight {
198		XcmGeneric::<Runtime>::pay_fees()
199	}
200	fn refund_surplus() -> Weight {
201		XcmGeneric::<Runtime>::refund_surplus()
202	}
203	fn set_error_handler(_xcm: &Xcm<RuntimeCall>) -> Weight {
204		XcmGeneric::<Runtime>::set_error_handler()
205	}
206	fn set_appendix(_xcm: &Xcm<RuntimeCall>) -> Weight {
207		XcmGeneric::<Runtime>::set_appendix()
208	}
209	fn clear_error() -> Weight {
210		XcmGeneric::<Runtime>::clear_error()
211	}
212	fn set_hints(hints: &BoundedVec<Hint, HintNumVariants>) -> Weight {
213		let mut weight = Weight::zero();
214		for hint in hints {
215			match hint {
216				AssetClaimer { .. } => {
217					weight = weight.saturating_add(XcmGeneric::<Runtime>::asset_claimer());
218				},
219			}
220		}
221		weight
222	}
223	fn claim_asset(_assets: &Assets, _ticket: &Location) -> Weight {
224		XcmGeneric::<Runtime>::claim_asset()
225	}
226	fn trap(_code: &u64) -> Weight {
227		XcmGeneric::<Runtime>::trap()
228	}
229	fn subscribe_version(_query_id: &QueryId, _max_response_weight: &Weight) -> Weight {
230		XcmGeneric::<Runtime>::subscribe_version()
231	}
232	fn unsubscribe_version() -> Weight {
233		XcmGeneric::<Runtime>::unsubscribe_version()
234	}
235	fn burn_asset(assets: &Assets) -> Weight {
236		assets.weigh_assets(XcmGeneric::<Runtime>::burn_asset())
237	}
238	fn expect_asset(assets: &Assets) -> Weight {
239		assets.weigh_assets(XcmGeneric::<Runtime>::expect_asset())
240	}
241	fn expect_origin(_origin: &Option<Location>) -> Weight {
242		XcmGeneric::<Runtime>::expect_origin()
243	}
244	fn expect_error(_error: &Option<(u32, XcmError)>) -> Weight {
245		XcmGeneric::<Runtime>::expect_error()
246	}
247	fn expect_transact_status(_transact_status: &MaybeErrorCode) -> Weight {
248		XcmGeneric::<Runtime>::expect_transact_status()
249	}
250	fn query_pallet(_module_name: &Vec<u8>, _response_info: &QueryResponseInfo) -> Weight {
251		XcmGeneric::<Runtime>::query_pallet()
252	}
253	fn expect_pallet(
254		_index: &u32,
255		_name: &Vec<u8>,
256		_module_name: &Vec<u8>,
257		_crate_major: &u32,
258		_min_crate_minor: &u32,
259	) -> Weight {
260		XcmGeneric::<Runtime>::expect_pallet()
261	}
262	fn report_transact_status(_response_info: &QueryResponseInfo) -> Weight {
263		XcmGeneric::<Runtime>::report_transact_status()
264	}
265	fn clear_transact_status() -> Weight {
266		XcmGeneric::<Runtime>::clear_transact_status()
267	}
268	fn universal_origin(_: &Junction) -> Weight {
269		// Westend does not currently support universal origin operations
270		Weight::MAX
271	}
272	fn export_message(_: &NetworkId, _: &Junctions, _: &Xcm<()>) -> Weight {
273		// Westend relay should not support export message operations
274		Weight::MAX
275	}
276	fn lock_asset(_: &Asset, _: &Location) -> Weight {
277		// Westend does not currently support asset locking operations
278		Weight::MAX
279	}
280	fn unlock_asset(_: &Asset, _: &Location) -> Weight {
281		// Westend does not currently support asset locking operations
282		Weight::MAX
283	}
284	fn note_unlockable(_: &Asset, _: &Location) -> Weight {
285		// Westend does not currently support asset locking operations
286		Weight::MAX
287	}
288	fn request_unlock(_: &Asset, _: &Location) -> Weight {
289		// Westend does not currently support asset locking operations
290		Weight::MAX
291	}
292	fn set_fees_mode(_: &bool) -> Weight {
293		XcmGeneric::<Runtime>::set_fees_mode()
294	}
295	fn set_topic(_topic: &[u8; 32]) -> Weight {
296		XcmGeneric::<Runtime>::set_topic()
297	}
298	fn clear_topic() -> Weight {
299		XcmGeneric::<Runtime>::clear_topic()
300	}
301	fn alias_origin(_: &Location) -> Weight {
302		XcmGeneric::<Runtime>::alias_origin()
303	}
304	fn unpaid_execution(_: &WeightLimit, _: &Option<Location>) -> Weight {
305		XcmGeneric::<Runtime>::unpaid_execution()
306	}
307	fn execute_with_origin(_: &Option<InteriorLocation>, _: &Xcm<RuntimeCall>) -> Weight {
308		XcmGeneric::<Runtime>::execute_with_origin()
309	}
310}
311
312#[test]
313fn all_counted_has_a_sane_weight_upper_limit() {
314	let assets = AssetFilter::Wild(AllCounted(4294967295));
315	let weight = Weight::from_parts(1000, 1000);
316
317	assert_eq!(assets.weigh_assets(weight), weight * MAX_ASSETS);
318}