referrerpolicy=no-referrer-when-downgrade

pallet_indices/
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 Indices Pallet
19
20#![cfg(feature = "runtime-benchmarks")]
21
22use crate::*;
23use frame_benchmarking::v2::*;
24use frame_support::traits::Get;
25use frame_system::RawOrigin;
26use sp_runtime::traits::Bounded;
27
28const SEED: u32 = 0;
29
30#[benchmarks]
31mod benchmarks {
32	use super::*;
33
34	#[benchmark]
35	fn claim() {
36		let account_index = T::AccountIndex::from(SEED);
37		let caller: T::AccountId = whitelisted_caller();
38		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
39
40		#[extrinsic_call]
41		_(RawOrigin::Signed(caller.clone()), account_index);
42
43		assert_eq!(Accounts::<T>::get(account_index).unwrap().0, caller);
44	}
45
46	#[benchmark]
47	fn transfer() -> Result<(), BenchmarkError> {
48		let account_index = T::AccountIndex::from(SEED);
49		// Setup accounts
50		let caller: T::AccountId = whitelisted_caller();
51		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
52		let recipient: T::AccountId = account("recipient", 0, SEED);
53		let recipient_lookup = T::Lookup::unlookup(recipient.clone());
54		T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
55		// Claim the index
56		Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
57
58		#[extrinsic_call]
59		_(RawOrigin::Signed(caller.clone()), recipient_lookup, account_index);
60
61		assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
62		Ok(())
63	}
64
65	#[benchmark]
66	fn free() -> Result<(), BenchmarkError> {
67		let account_index = T::AccountIndex::from(SEED);
68		// Setup accounts
69		let caller: T::AccountId = whitelisted_caller();
70		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
71		// Claim the index
72		Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
73
74		#[extrinsic_call]
75		_(RawOrigin::Signed(caller.clone()), account_index);
76
77		assert_eq!(Accounts::<T>::get(account_index), None);
78		Ok(())
79	}
80
81	#[benchmark]
82	fn force_transfer() -> Result<(), BenchmarkError> {
83		let account_index = T::AccountIndex::from(SEED);
84		// Setup accounts
85		let original: T::AccountId = account("original", 0, SEED);
86		T::Currency::make_free_balance_be(&original, BalanceOf::<T>::max_value());
87		let recipient: T::AccountId = account("recipient", 0, SEED);
88		let recipient_lookup = T::Lookup::unlookup(recipient.clone());
89		T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
90		// Claim the index
91		Pallet::<T>::claim(RawOrigin::Signed(original).into(), account_index)?;
92
93		#[extrinsic_call]
94		_(RawOrigin::Root, recipient_lookup, account_index, false);
95
96		assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
97		Ok(())
98	}
99
100	#[benchmark]
101	fn freeze() -> Result<(), BenchmarkError> {
102		let account_index = T::AccountIndex::from(SEED);
103		// Setup accounts
104		let caller: T::AccountId = whitelisted_caller();
105		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
106		// Claim the index
107		Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
108
109		#[extrinsic_call]
110		_(RawOrigin::Signed(caller.clone()), account_index);
111
112		assert_eq!(Accounts::<T>::get(account_index).unwrap().2, true);
113		Ok(())
114	}
115
116	#[benchmark]
117	fn poke_deposit() -> Result<(), BenchmarkError> {
118		let account_index = T::AccountIndex::from(SEED);
119		// Setup accounts
120		let caller: T::AccountId = whitelisted_caller();
121		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
122
123		let original_deposit = T::Deposit::get();
124
125		// Claim the index
126		Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
127
128		// Verify the initial deposit amount in storage and reserved balance
129		assert_eq!(Accounts::<T>::get(account_index).unwrap().1, original_deposit);
130		assert_eq!(T::Currency::reserved_balance(&caller), original_deposit);
131
132		// The additional amount we'll add to the deposit for the index
133		let additional_amount = 2u32.into();
134
135		// Reserve the additional amount from the caller's balance
136		T::Currency::reserve(&caller, additional_amount)?;
137
138		// Verify the additional amount was reserved
139		assert_eq!(
140			T::Currency::reserved_balance(&caller),
141			original_deposit.saturating_add(additional_amount)
142		);
143
144		// Increase the deposited amount in storage by additional_amount
145		Accounts::<T>::try_mutate(account_index, |maybe_value| -> Result<(), BenchmarkError> {
146			let (account, amount, perm) = maybe_value
147				.take()
148				.ok_or(BenchmarkError::Stop("Mutating storage to change deposits failed"))?;
149			*maybe_value = Some((account, amount.saturating_add(additional_amount), perm));
150			Ok(())
151		})?;
152
153		// Verify the deposit was increased by additional_amount
154		assert_eq!(
155			Accounts::<T>::get(account_index).unwrap().1,
156			original_deposit.saturating_add(additional_amount)
157		);
158
159		#[extrinsic_call]
160		_(RawOrigin::Signed(caller.clone()), account_index);
161
162		assert!(Accounts::<T>::contains_key(account_index));
163		assert_eq!(Accounts::<T>::get(account_index).unwrap().0, caller);
164		assert_eq!(Accounts::<T>::get(account_index).unwrap().1, original_deposit);
165		assert_eq!(T::Currency::reserved_balance(&caller), original_deposit);
166		Ok(())
167	}
168
169	// TODO in another PR: lookup and unlookup trait weights (not critical)
170
171	impl_benchmark_test_suite!(Pallet, mock::new_test_ext(), mock::Test);
172}