1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

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

            
17
pub mod pallet_xcm_benchmarks_generic;
18

            
19
use frame_support::BoundedVec;
20
use xcm::latest::AssetTransferFilter;
21
use {
22
    crate::Runtime,
23
    frame_support::weights::Weight,
24
    pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric,
25
    sp_std::prelude::*,
26
    xcm::{
27
        latest::{prelude::*, Weight as XCMWeight},
28
        DoubleEncoded,
29
    },
30
};
31

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

            
36
impl WeighAssets for Assets {
37
126
    fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight {
38
126
        weight.saturating_mul(self.inner().iter().count() as u64)
39
126
    }
40
}
41

            
42
// Values copied from statemint benchmarks
43
const ASSET_BURN_MAX_PROOF_SIZE: u64 = 7242;
44
const ASSET_MINT_MAX_PROOF_SIZE: u64 = 7242;
45
const ASSET_TRANSFER_MAX_PROOF_SIZE: u64 = 13412;
46

            
47
// For now we are returning benchmarked weights only for generic XCM instructions.
48
// Fungible XCM instructions will return a fixed weight value of
49
// 200_000_000 ref_time and its proper PoV weight taken from statemint benchmarks.
50
//
51
// TODO: add the fungible benchmarked values once these are calculated.
52
pub struct XcmWeight<RuntimeCall>(core::marker::PhantomData<RuntimeCall>);
53
impl<RuntimeCall> XcmWeightInfo<RuntimeCall> for XcmWeight<RuntimeCall>
54
where
55
    Runtime: frame_system::Config,
56
{
57
1
    fn withdraw_asset(assets: &Assets) -> XCMWeight {
58
1
        assets.weigh_multi_assets(XCMWeight::from_parts(
59
1
            200_000_000u64,
60
1
            ASSET_BURN_MAX_PROOF_SIZE,
61
1
        ))
62
1
    }
63
11
    fn reserve_asset_deposited(assets: &Assets) -> XCMWeight {
64
11
        assets.weigh_multi_assets(XCMWeight::from_parts(200_000_000u64, 0))
65
11
    }
66
    fn receive_teleported_asset(_assets: &Assets) -> XCMWeight {
67
        XCMWeight::MAX
68
    }
69
    fn query_response(
70
        _query_id: &u64,
71
        _response: &Response,
72
        _max_weight: &Weight,
73
        _querier: &Option<Location>,
74
    ) -> XCMWeight {
75
        XcmGeneric::<Runtime>::query_response()
76
    }
77
4
    fn transfer_asset(assets: &Assets, _dest: &Location) -> XCMWeight {
78
4
        assets.weigh_multi_assets(XCMWeight::from_parts(
79
4
            200_000_000u64,
80
4
            ASSET_TRANSFER_MAX_PROOF_SIZE,
81
4
        ))
82
4
    }
83
    fn transfer_reserve_asset(assets: &Assets, _dest: &Location, _xcm: &Xcm<()>) -> XCMWeight {
84
        assets.weigh_multi_assets(XCMWeight::from_parts(
85
            200_000_000u64,
86
            ASSET_TRANSFER_MAX_PROOF_SIZE,
87
        ))
88
    }
89
    fn transact(
90
        _origin_type: &OriginKind,
91
        _require_weight_at_most: &Option<Weight>,
92
        _call: &DoubleEncoded<RuntimeCall>,
93
    ) -> XCMWeight {
94
        XcmGeneric::<Runtime>::transact()
95
    }
96
    fn hrmp_new_channel_open_request(
97
        _sender: &u32,
98
        _max_message_size: &u32,
99
        _max_capacity: &u32,
100
    ) -> XCMWeight {
101
        // XCM Executor does not currently support HRMP channel operations
102
        Weight::MAX
103
    }
104
    fn hrmp_channel_accepted(_recipient: &u32) -> XCMWeight {
105
        // XCM Executor does not currently support HRMP channel operations
106
        Weight::MAX
107
    }
108
    fn hrmp_channel_closing(_initiator: &u32, _sender: &u32, _recipient: &u32) -> XCMWeight {
109
        // XCM Executor does not currently support HRMP channel operations
110
        Weight::MAX
111
    }
112
11
    fn clear_origin() -> XCMWeight {
113
11
        XcmGeneric::<Runtime>::clear_origin()
114
11
    }
115
    fn descend_origin(_who: &InteriorLocation) -> XCMWeight {
116
        XcmGeneric::<Runtime>::descend_origin()
117
    }
118
    fn report_error(_query_response_info: &QueryResponseInfo) -> XCMWeight {
119
        XcmGeneric::<Runtime>::report_error()
120
    }
121
13
    fn deposit_asset(_assets: &AssetFilter, _dest: &Location) -> XCMWeight {
122
13
        Weight::from_parts(200_000_000u64, ASSET_MINT_MAX_PROOF_SIZE)
123
13
    }
124
    fn deposit_reserve_asset(_assets: &AssetFilter, _dest: &Location, _xcm: &Xcm<()>) -> XCMWeight {
125
        Weight::from_parts(200_000_000u64, ASSET_MINT_MAX_PROOF_SIZE)
126
    }
127
    fn exchange_asset(_give: &AssetFilter, _receive: &Assets, _maximal: &bool) -> XCMWeight {
128
        Weight::MAX
129
    }
130
    fn initiate_reserve_withdraw(
131
        _assets: &AssetFilter,
132
        _reserve: &Location,
133
        _xcm: &Xcm<()>,
134
    ) -> XCMWeight {
135
        XCMWeight::from_parts(200_000_000u64, ASSET_TRANSFER_MAX_PROOF_SIZE)
136
    }
137
    fn initiate_teleport(_assets: &AssetFilter, _dest: &Location, _xcm: &Xcm<()>) -> XCMWeight {
138
        XCMWeight::MAX
139
    }
140
    fn report_holding(_response_info: &QueryResponseInfo, _assets: &AssetFilter) -> Weight {
141
        XcmGeneric::<Runtime>::report_holding()
142
    }
143
11
    fn buy_execution(_fees: &Asset, _weight_limit: &WeightLimit) -> XCMWeight {
144
11
        XcmGeneric::<Runtime>::buy_execution()
145
11
    }
146
    fn refund_surplus() -> XCMWeight {
147
        XcmGeneric::<Runtime>::refund_surplus()
148
    }
149
    fn set_error_handler(_xcm: &Xcm<RuntimeCall>) -> XCMWeight {
150
        XcmGeneric::<Runtime>::set_error_handler()
151
    }
152
    fn set_appendix(_xcm: &Xcm<RuntimeCall>) -> XCMWeight {
153
        XcmGeneric::<Runtime>::set_appendix()
154
    }
155
    fn clear_error() -> XCMWeight {
156
        XcmGeneric::<Runtime>::clear_error()
157
    }
158
    fn claim_asset(assets: &Assets, _ticket: &Location) -> XCMWeight {
159
        assets.weigh_multi_assets(XcmGeneric::<Runtime>::claim_asset())
160
    }
161
    fn trap(_code: &u64) -> XCMWeight {
162
        XcmGeneric::<Runtime>::trap()
163
    }
164
    fn subscribe_version(_query_id: &QueryId, _max_response_weight: &Weight) -> XCMWeight {
165
        XcmGeneric::<Runtime>::subscribe_version()
166
    }
167
    fn unsubscribe_version() -> XCMWeight {
168
        XcmGeneric::<Runtime>::unsubscribe_version()
169
    }
170
    fn burn_asset(assets: &Assets) -> Weight {
171
        assets.weigh_multi_assets(XcmGeneric::<Runtime>::burn_asset())
172
    }
173
    fn expect_asset(assets: &Assets) -> Weight {
174
        assets.weigh_multi_assets(XcmGeneric::<Runtime>::expect_asset())
175
    }
176
    fn expect_origin(_origin: &Option<Location>) -> Weight {
177
        XcmGeneric::<Runtime>::expect_origin()
178
    }
179
    fn expect_error(_error: &Option<(u32, XcmError)>) -> Weight {
180
        XcmGeneric::<Runtime>::expect_error()
181
    }
182
    fn expect_transact_status(_transact_status: &MaybeErrorCode) -> Weight {
183
        XcmGeneric::<Runtime>::expect_transact_status()
184
    }
185
    fn query_pallet(_module_name: &Vec<u8>, _response_info: &QueryResponseInfo) -> Weight {
186
        XcmGeneric::<Runtime>::query_pallet()
187
    }
188
    fn expect_pallet(
189
        _index: &u32,
190
        _name: &Vec<u8>,
191
        _module_name: &Vec<u8>,
192
        _crate_major: &u32,
193
        _min_crate_minor: &u32,
194
    ) -> Weight {
195
        XcmGeneric::<Runtime>::expect_pallet()
196
    }
197
    fn report_transact_status(_response_info: &QueryResponseInfo) -> Weight {
198
        XcmGeneric::<Runtime>::report_transact_status()
199
    }
200
    fn clear_transact_status() -> Weight {
201
        XcmGeneric::<Runtime>::clear_transact_status()
202
    }
203
    fn universal_origin(_: &Junction) -> Weight {
204
        Weight::MAX
205
    }
206
    fn export_message(_: &NetworkId, _: &Junctions, _: &Xcm<()>) -> Weight {
207
        Weight::MAX
208
    }
209
    fn lock_asset(_: &Asset, _: &Location) -> Weight {
210
        Weight::MAX
211
    }
212
    fn unlock_asset(_: &Asset, _: &Location) -> Weight {
213
        Weight::MAX
214
    }
215
    fn note_unlockable(_: &Asset, _: &Location) -> Weight {
216
        Weight::MAX
217
    }
218
    fn request_unlock(_: &Asset, _: &Location) -> Weight {
219
        Weight::MAX
220
    }
221
    fn set_fees_mode(_: &bool) -> Weight {
222
        XcmGeneric::<Runtime>::set_fees_mode()
223
    }
224
10
    fn set_topic(_topic: &[u8; 32]) -> Weight {
225
10
        XcmGeneric::<Runtime>::set_topic()
226
10
    }
227
    fn clear_topic() -> Weight {
228
        XcmGeneric::<Runtime>::clear_topic()
229
    }
230
    fn alias_origin(_: &Location) -> Weight {
231
        // XCM Executor does not currently support alias origin operations
232
        Weight::MAX
233
    }
234
    fn unpaid_execution(_: &WeightLimit, _: &Option<Location>) -> Weight {
235
        XcmGeneric::<Runtime>::unpaid_execution()
236
    }
237

            
238
    fn pay_fees(_: &Asset) -> Weight {
239
        XcmGeneric::<Runtime>::pay_fees()
240
    }
241

            
242
    fn initiate_transfer(
243
        _dest: &Location,
244
        _remote_fees: &Option<AssetTransferFilter>,
245
        _preserve_origin: &bool,
246
        _assets: &Vec<AssetTransferFilter>,
247
        _xcm: &Xcm<()>,
248
    ) -> Weight {
249
        Weight::MAX
250
    }
251

            
252
    fn execute_with_origin(_: &Option<InteriorLocation>, _: &Xcm<RuntimeCall>) -> Weight {
253
        XcmGeneric::<Runtime>::execute_with_origin()
254
    }
255

            
256
    fn set_hints(hints: &BoundedVec<Hint, HintNumVariants>) -> Weight {
257
        let mut weight = Weight::zero();
258
        for hint in hints {
259
            match hint {
260
                AssetClaimer { .. } => {
261
                    weight = weight.saturating_add(XcmGeneric::<Runtime>::asset_claimer());
262
                }
263
            }
264
        }
265
        weight
266
    }
267
}