1
// Copyright 2019-2022 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 fungible;
18
pub mod generic;
19

            
20
pub use crate::weights::generic::WeightInfo;
21
use core::cmp::min;
22
use frame_support::weights::Weight;
23
use fungible::WeightInfo as XcmFungibleWeight;
24
use generic::SubstrateWeight as XcmGeneric;
25
use sp_std::prelude::*;
26
use xcm::{
27
	latest::{prelude::*, Weight as XCMWeight},
28
	DoubleEncoded,
29
};
30
use xcm_primitives::MAX_ASSETS;
31

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

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

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

            
54
impl WeighMultiAssets for Assets {
55
476
	fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight {
56
476
		weight.saturating_mul(self.inner().into_iter().count() as u64)
57
476
	}
58
}
59

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