referrerpolicy=no-referrer-when-downgrade

pallet_asset_conversion_tx_payment/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Benchmarks for Asset Conversion Tx Payment Pallet's transaction extension
19
20extern crate alloc;
21
22use super::*;
23use crate::Pallet;
24use frame_benchmarking::v2::*;
25use frame_support::{
26	dispatch::{DispatchInfo, PostDispatchInfo},
27	pallet_prelude::*,
28};
29use frame_system::RawOrigin;
30use sp_runtime::traits::{
31	AsSystemOriginSigner, AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable,
32};
33
34#[benchmarks(where
35	T::RuntimeOrigin: AsTransactionAuthorizedOrigin,
36	T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
37	BalanceOf<T>: Send + Sync + From<u64>,
38	T::AssetId: Send + Sync,
39	<T::RuntimeCall as Dispatchable>::RuntimeOrigin: AsSystemOriginSigner<T::AccountId> + Clone,
40)]
41mod benchmarks {
42	use super::*;
43
44	#[benchmark]
45	fn charge_asset_tx_payment_zero() {
46		let caller: T::AccountId = account("caller", 0, 0);
47		let ext: ChargeAssetTxPayment<T> = ChargeAssetTxPayment::from(0u64.into(), None);
48		let inner = frame_system::Call::remark { remark: alloc::vec![] };
49		let call = T::RuntimeCall::from(inner);
50		let info = DispatchInfo {
51			call_weight: Weight::zero(),
52			extension_weight: Weight::zero(),
53			class: DispatchClass::Normal,
54			pays_fee: Pays::No,
55		};
56		let post_info = PostDispatchInfo { actual_weight: None, pays_fee: Pays::No };
57		#[block]
58		{
59			assert!(ext
60				.test_run(RawOrigin::Signed(caller).into(), &call, &info, 0, 0, |_| Ok(post_info))
61				.unwrap()
62				.is_ok());
63		}
64	}
65
66	#[benchmark]
67	fn charge_asset_tx_payment_native() {
68		let caller: T::AccountId = account("caller", 0, 0);
69		let (fun_asset_id, _) = <T as Config>::BenchmarkHelper::create_asset_id_parameter(1);
70		<T as Config>::BenchmarkHelper::setup_balances_and_pool(fun_asset_id, caller.clone());
71		let ext: ChargeAssetTxPayment<T> = ChargeAssetTxPayment::from(10u64.into(), None);
72		let inner = frame_system::Call::remark { remark: alloc::vec![] };
73		let call = T::RuntimeCall::from(inner);
74		let info = DispatchInfo {
75			call_weight: Weight::from_parts(10, 0),
76			extension_weight: Weight::zero(),
77			class: DispatchClass::Operational,
78			pays_fee: Pays::Yes,
79		};
80		// Submit a lower post info weight to trigger the refund path.
81		let post_info =
82			PostDispatchInfo { actual_weight: Some(Weight::from_parts(5, 0)), pays_fee: Pays::Yes };
83
84		#[block]
85		{
86			assert!(ext
87				.test_run(RawOrigin::Signed(caller).into(), &call, &info, 0, 0, |_| Ok(post_info))
88				.unwrap()
89				.is_ok());
90		}
91	}
92
93	#[benchmark]
94	fn charge_asset_tx_payment_asset() {
95		let caller: T::AccountId = account("caller", 0, 0);
96		let (fun_asset_id, asset_id) = <T as Config>::BenchmarkHelper::create_asset_id_parameter(1);
97		<T as Config>::BenchmarkHelper::setup_balances_and_pool(fun_asset_id, caller.clone());
98
99		let tip = 10u64.into();
100		let ext: ChargeAssetTxPayment<T> = ChargeAssetTxPayment::from(tip, Some(asset_id));
101		let inner = frame_system::Call::remark { remark: alloc::vec![] };
102		let call = T::RuntimeCall::from(inner);
103		let info = DispatchInfo {
104			call_weight: Weight::from_parts(10, 0),
105			extension_weight: Weight::zero(),
106			class: DispatchClass::Operational,
107			pays_fee: Pays::Yes,
108		};
109		// Submit a lower post info weight to trigger the refund path.
110		let post_info =
111			PostDispatchInfo { actual_weight: Some(Weight::from_parts(5, 0)), pays_fee: Pays::Yes };
112
113		#[block]
114		{
115			assert!(ext
116				.test_run(RawOrigin::Signed(caller.clone()).into(), &call, &info, 0, 0, |_| Ok(
117					post_info
118				))
119				.unwrap()
120				.is_ok());
121		}
122	}
123
124	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
125}