1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam 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
// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
pub mod pallet_xcm_benchmarks_fungible;
18
pub mod pallet_xcm_benchmarks_generic;
19

            
20
use core::cmp::min;
21
use frame_support::{weights::Weight, BoundedVec};
22
use pallet_xcm_benchmarks_fungible::WeightInfo as XcmFungibleWeight;
23
use pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric;
24
use sp_std::prelude::*;
25
use xcm::{
26
	latest::{prelude::*, Weight as XCMWeight},
27
	opaque::latest::AssetTransferFilter,
28
	v5, DoubleEncoded,
29
};
30
use xcm_primitives::MAX_ASSETS;
31

            
32
trait WeighMultiAssets {
33
	fn weigh_assets(&self, weight: Weight) -> XCMWeight;
34
}
35

            
36
trait WeighMultiAssetsFilter {
37
	fn weigh_assets(&self, weight: Weight) -> XCMWeight;
38
}
39

            
40
impl WeighMultiAssetsFilter for AssetFilter {
41
70
	fn weigh_assets(&self, weight: Weight) -> XCMWeight {
42
70
		match self {
43
			Definite(assets) => weight.saturating_mul(assets.inner().into_iter().count() as u64),
44
70
			Wild(AllCounted(count) | AllOfCounted { count, .. }) => {
45
70
				weight.saturating_mul(min(MAX_ASSETS, *count) as u64)
46
			}
47
			Wild(All | AllOf { .. }) => weight.saturating_mul(MAX_ASSETS as u64),
48
		}
49
70
	}
50
}
51

            
52
impl WeighMultiAssets for Assets {
53
77
	fn weigh_assets(&self, weight: Weight) -> XCMWeight {
54
77
		weight.saturating_mul(self.inner().into_iter().count() as u64)
55
77
	}
56
}
57

            
58
pub struct XcmWeight<Runtime, Call>(core::marker::PhantomData<(Runtime, Call)>);
59
impl<Runtime, Call> XcmWeightInfo<Call> for XcmWeight<Runtime, Call>
60
where
61
	Runtime: frame_system::Config
62
		+ pallet_erc20_xcm_bridge::Config
63
		+ pallet_moonbeam_foreign_assets::Config,
64
{
65
33
	fn withdraw_asset(assets: &Assets) -> XCMWeight {
66
33
		assets.weigh_assets(XcmFungibleWeight::<Runtime>::withdraw_asset())
67
33
	}
68
	// Currently there is no trusted reserve
69
	fn reserve_asset_deposited(assets: &Assets) -> XCMWeight {
70
		assets.weigh_assets(XcmFungibleWeight::<Runtime>::reserve_asset_deposited())
71
	}
72
	fn receive_teleported_asset(_assets: &Assets) -> XCMWeight {
73
		// Instruction disabled
74
		Weight::MAX
75
	}
76
	fn query_response(
77
		_query_id: &u64,
78
		_response: &Response,
79
		_max_weight: &Weight,
80
		_querier: &Option<Location>,
81
	) -> XCMWeight {
82
		XcmGeneric::<Runtime>::query_response()
83
	}
84
14
	fn transfer_asset(assets: &Assets, _dest: &Location) -> XCMWeight {
85
14
		assets.weigh_assets(XcmFungibleWeight::<Runtime>::transfer_asset())
86
14
	}
87
	fn transfer_reserve_asset(assets: &Assets, _dest: &Location, _xcm: &Xcm<()>) -> XCMWeight {
88
		assets.weigh_assets(XcmFungibleWeight::<Runtime>::transfer_reserve_asset())
89
	}
90
1
	fn transact(
91
1
		_origin_type: &OriginKind,
92
1
		_require_weight_at_most: &Option<Weight>,
93
1
		_call: &DoubleEncoded<Call>,
94
1
	) -> XCMWeight {
95
1
		XcmGeneric::<Runtime>::transact()
96
1
	}
97
	fn hrmp_new_channel_open_request(
98
		_sender: &u32,
99
		_max_message_size: &u32,
100
		_max_capacity: &u32,
101
	) -> XCMWeight {
102
		// XCM Executor does not currently support HRMP channel operations
103
		Weight::MAX
104
	}
105
	fn hrmp_channel_accepted(_recipient: &u32) -> XCMWeight {
106
		// XCM Executor does not currently support HRMP channel operations
107
		Weight::MAX
108
	}
109
	fn hrmp_channel_closing(_initiator: &u32, _sender: &u32, _recipient: &u32) -> XCMWeight {
110
		// XCM Executor does not currently support HRMP channel operations
111
		Weight::MAX
112
	}
113
2
	fn clear_origin() -> XCMWeight {
114
2
		XcmGeneric::<Runtime>::clear_origin()
115
2
	}
116
	fn descend_origin(_who: &InteriorLocation) -> XCMWeight {
117
		XcmGeneric::<Runtime>::descend_origin()
118
	}
119
	fn report_error(_query_response_info: &QueryResponseInfo) -> XCMWeight {
120
		XcmGeneric::<Runtime>::report_error()
121
	}
122
2
	fn deposit_asset(assets: &AssetFilter, _dest: &Location) -> XCMWeight {
123
2
		assets.weigh_assets(XcmFungibleWeight::<Runtime>::deposit_asset())
124
2
	}
125
	fn deposit_reserve_asset(assets: &AssetFilter, _dest: &Location, _xcm: &Xcm<()>) -> XCMWeight {
126
		assets.weigh_assets(XcmFungibleWeight::<Runtime>::deposit_reserve_asset())
127
	}
128
	fn exchange_asset(_give: &AssetFilter, _receive: &Assets, _maximal: &bool) -> XCMWeight {
129
		Weight::MAX
130
	}
131
32
	fn initiate_reserve_withdraw(
132
32
		assets: &AssetFilter,
133
32
		_reserve: &Location,
134
32
		_xcm: &Xcm<()>,
135
32
	) -> XCMWeight {
136
32
		assets.weigh_assets(XcmFungibleWeight::<Runtime>::initiate_reserve_withdraw())
137
32
	}
138
	fn initiate_teleport(_assets: &AssetFilter, _dest: &Location, _xcm: &Xcm<()>) -> XCMWeight {
139
		Weight::MAX
140
	}
141
	fn report_holding(_response_info: &QueryResponseInfo, _assets: &AssetFilter) -> Weight {
142
		XcmGeneric::<Runtime>::report_holding()
143
	}
144
	fn buy_execution(_fees: &Asset, _weight_limit: &WeightLimit) -> XCMWeight {
145
		XcmGeneric::<Runtime>::buy_execution()
146
	}
147
	fn refund_surplus() -> XCMWeight {
148
		XcmGeneric::<Runtime>::refund_surplus()
149
	}
150
	fn set_error_handler(_xcm: &Xcm<Call>) -> XCMWeight {
151
		XcmGeneric::<Runtime>::set_error_handler()
152
	}
153
	fn set_appendix(_xcm: &Xcm<Call>) -> XCMWeight {
154
		XcmGeneric::<Runtime>::set_appendix()
155
	}
156
	fn clear_error() -> XCMWeight {
157
		XcmGeneric::<Runtime>::clear_error()
158
	}
159
2
	fn claim_asset(_assets: &Assets, _ticket: &Location) -> XCMWeight {
160
2
		XcmGeneric::<Runtime>::claim_asset()
161
2
	}
162
	fn trap(_code: &u64) -> XCMWeight {
163
		XcmGeneric::<Runtime>::trap()
164
	}
165
	fn subscribe_version(_query_id: &QueryId, _max_response_weight: &Weight) -> XCMWeight {
166
		XcmGeneric::<Runtime>::subscribe_version()
167
	}
168
	fn unsubscribe_version() -> XCMWeight {
169
		XcmGeneric::<Runtime>::unsubscribe_version()
170
	}
171
	fn burn_asset(assets: &Assets) -> Weight {
172
		assets.weigh_assets(XcmGeneric::<Runtime>::burn_asset())
173
	}
174
	fn expect_asset(assets: &Assets) -> Weight {
175
		assets.weigh_assets(XcmGeneric::<Runtime>::expect_asset())
176
	}
177
	fn expect_origin(_origin: &Option<Location>) -> Weight {
178
		XcmGeneric::<Runtime>::expect_origin()
179
	}
180
	fn expect_error(_error: &Option<(u32, XcmError)>) -> Weight {
181
		XcmGeneric::<Runtime>::expect_error()
182
	}
183
	fn expect_transact_status(_transact_status: &MaybeErrorCode) -> Weight {
184
		XcmGeneric::<Runtime>::expect_transact_status()
185
	}
186
	fn query_pallet(_module_name: &Vec<u8>, _response_info: &QueryResponseInfo) -> Weight {
187
		XcmGeneric::<Runtime>::query_pallet()
188
	}
189
	fn expect_pallet(
190
		_index: &u32,
191
		_name: &Vec<u8>,
192
		_module_name: &Vec<u8>,
193
		_crate_major: &u32,
194
		_min_crate_minor: &u32,
195
	) -> Weight {
196
		XcmGeneric::<Runtime>::expect_pallet()
197
	}
198
	fn report_transact_status(_response_info: &QueryResponseInfo) -> Weight {
199
		XcmGeneric::<Runtime>::report_transact_status()
200
	}
201
	fn clear_transact_status() -> Weight {
202
		XcmGeneric::<Runtime>::clear_transact_status()
203
	}
204
	fn universal_origin(_: &Junction) -> Weight {
205
		XcmGeneric::<Runtime>::clear_origin()
206
	}
207
	fn export_message(_: &NetworkId, _: &Junctions, _: &Xcm<()>) -> Weight {
208
		Weight::MAX
209
	}
210
	fn lock_asset(_: &Asset, _: &Location) -> Weight {
211
		Weight::MAX
212
	}
213
	fn unlock_asset(_: &Asset, _: &Location) -> Weight {
214
		Weight::MAX
215
	}
216
	fn note_unlockable(_: &Asset, _: &Location) -> Weight {
217
		Weight::MAX
218
	}
219
	fn request_unlock(_: &Asset, _: &Location) -> Weight {
220
		Weight::MAX
221
	}
222
32
	fn set_fees_mode(_: &bool) -> Weight {
223
32
		XcmGeneric::<Runtime>::set_fees_mode()
224
32
	}
225
	fn set_topic(_topic: &[u8; 32]) -> Weight {
226
		XcmGeneric::<Runtime>::set_topic()
227
	}
228
	fn clear_topic() -> Weight {
229
		XcmGeneric::<Runtime>::clear_topic()
230
	}
231
	fn alias_origin(_: &Location) -> Weight {
232
		// XCM Executor does not currently support alias origin operations
233
		Weight::MAX
234
	}
235
	fn unpaid_execution(_: &WeightLimit, _: &Option<Location>) -> Weight {
236
		XcmGeneric::<Runtime>::unpaid_execution()
237
	}
238
	fn pay_fees(_: &v5::Asset) -> Weight {
239
		XcmGeneric::<Runtime>::pay_fees()
240
	}
241
	fn initiate_transfer(
242
		_dest: &Location,
243
		remote_fees: &Option<AssetTransferFilter>,
244
		_preserve_origin: &bool,
245
		assets: &BoundedVec<AssetTransferFilter, MaxAssetTransferFilters>,
246
		_xcm: &Xcm<()>,
247
	) -> Weight {
248
		let base_weight = XcmFungibleWeight::<Runtime>::initiate_transfer();
249
		let mut weight = if let Some(remote_fees) = remote_fees {
250
			let fees = remote_fees.inner();
251
			fees.weigh_assets(base_weight)
252
		} else {
253
			base_weight
254
		};
255

            
256
		for transfer_filter in assets {
257
			let filter = transfer_filter.inner();
258
			let extra = filter.weigh_assets(XcmFungibleWeight::<Runtime>::initiate_transfer());
259
			weight = weight.saturating_add(extra);
260
		}
261
		weight
262
	}
263
	fn execute_with_origin(_: &Option<v5::Junctions>, _: &v5::Xcm<Call>) -> Weight {
264
		XcmGeneric::<Runtime>::execute_with_origin()
265
	}
266
	fn set_hints(hints: &BoundedVec<Hint, HintNumVariants>) -> Weight {
267
		let mut weight = Weight::zero();
268
		for hint in hints {
269
			match hint {
270
				AssetClaimer { .. } => {
271
					weight = weight.saturating_add(XcmGeneric::<Runtime>::asset_claimer());
272
				}
273
			}
274
		}
275
		weight
276
	}
277
}