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 {
20
    crate::Runtime,
21
    frame_support::{weights::Weight, BoundedVec},
22
    pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric,
23
    sp_std::prelude::*,
24
    xcm::{
25
        latest::{prelude::*, AssetTransferFilter, Weight as XCMWeight},
26
        DoubleEncoded,
27
    },
28
};
29

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

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

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

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

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

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

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

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