referrerpolicy=no-referrer-when-downgrade

pallet_hrmp_para/
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 `pallet-hrmp-para`.
19
20use super::*;
21use frame_benchmarking::v2::*;
22use frame_support::traits::fungible::{Inspect, Mutate};
23use frame_system::RawOrigin;
24
25const CHANNEL: ChannelId = ChannelId { sender: 1, recipient: 2 };
26
27/// Endows both paras of [`CHANNEL`] and returns a deposit each can cover.
28fn endow<T: Config>() -> Balance
29where
30	T::Currency: Mutate<T::AccountId>,
31{
32	let ed = T::Currency::minimum_balance().max(1);
33	for side in [DepositSide::Sender, DepositSide::Recipient] {
34		T::Currency::set_balance(&Pallet::<T>::payer(&CHANNEL, side), ed.saturating_mul(100));
35	}
36	ed.saturating_mul(10)
37}
38
39#[benchmarks(where T::Currency: Mutate<T::AccountId>)]
40mod benchmarks {
41	use super::*;
42
43	/// Both sides held from nothing, and the answer sent.
44	#[benchmark]
45	fn receive_set_deposits() {
46		T::BenchmarkHelper::ensure_send_to_relay();
47		let deposit = endow::<T>();
48		let message = MessageToPara::V1(MessageToParaV1::SetDeposits {
49			id: 0,
50			channel: CHANNEL,
51			sender: Some(deposit),
52			recipient: Some(deposit),
53		});
54
55		#[extrinsic_call]
56		receive(RawOrigin::Root, message);
57
58		let recorded = Deposits::<T>::get((CHANNEL, DepositSide::Recipient)).map(|d| d.amount);
59		assert_eq!(recorded, Some(deposit));
60	}
61
62	/// Both sides released.
63	#[benchmark]
64	fn receive_release() {
65		let deposit = endow::<T>();
66		for side in [DepositSide::Sender, DepositSide::Recipient] {
67			Pallet::<T>::set_side(CHANNEL, side, deposit).expect("endowed; qed");
68		}
69		let message = MessageToPara::V1(MessageToParaV1::Release {
70			channel: CHANNEL,
71			sender: true,
72			recipient: true,
73		});
74
75		#[extrinsic_call]
76		receive(RawOrigin::Root, message);
77
78		assert_eq!(Deposits::<T>::iter().count(), 0);
79	}
80
81	/// A deposit moved whole from the migration's hold to a side that held nothing.
82	#[benchmark]
83	fn receive_deposit() {
84		let deposit = endow::<T>();
85		let side = DepositSide::Sender;
86		// The benchmark can name no other hold reason than the pallet's own. It stands for the
87		// migration's: the storage read and written is the same.
88		let source = HoldReason::ChannelDeposit.into();
89		T::Currency::hold(&source, &Pallet::<T>::payer(&CHANNEL, side), deposit)
90			.expect("endowed; qed");
91		let result;
92
93		#[block]
94		{
95			result = Pallet::<T>::receive_deposit(
96				DepositKey { channel: CHANNEL, side },
97				deposit,
98				&source,
99			);
100		}
101
102		assert!(result.is_ok());
103		assert_eq!(Deposits::<T>::get((CHANNEL, side)).map(|d| d.amount), Some(deposit));
104	}
105
106	impl_benchmark_test_suite!(Pallet, crate::mock::build_ext(), crate::mock::Test);
107}