referrerpolicy=no-referrer-when-downgrade

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