referrerpolicy=no-referrer-when-downgrade

pallet_preimage/
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//! Preimage pallet benchmarking.
19
20use alloc::vec;
21use frame_benchmarking::v2::*;
22use frame_support::assert_ok;
23use frame_system::RawOrigin;
24use sp_runtime::traits::Bounded;
25
26use crate::*;
27
28fn funded_account<T: Config>() -> T::AccountId {
29	let caller: T::AccountId = whitelisted_caller();
30	T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value() / 2u32.into());
31	caller
32}
33
34fn preimage_and_hash<T: Config>() -> (Vec<u8>, T::Hash) {
35	sized_preimage_and_hash::<T>(MAX_SIZE)
36}
37
38fn sized_preimage_and_hash<T: Config>(size: u32) -> (Vec<u8>, T::Hash) {
39	let mut preimage = vec![];
40	preimage.resize(size as usize, 0);
41	let hash = <T as frame_system::Config>::Hashing::hash(&preimage[..]);
42	(preimage, hash)
43}
44
45fn insert_old_unrequested<T: Config>(s: u32) -> <T as frame_system::Config>::Hash {
46	let acc = account("old", s, 0);
47	T::Currency::make_free_balance_be(&acc, BalanceOf::<T>::max_value() / 2u32.into());
48
49	// The preimage size does not matter here as it is not touched.
50	let preimage = s.to_le_bytes();
51	let hash = <T as frame_system::Config>::Hashing::hash(&preimage[..]);
52
53	#[allow(deprecated)]
54	StatusFor::<T>::insert(
55		&hash,
56		OldRequestStatus::Unrequested { deposit: (acc, 123u32.into()), len: preimage.len() as u32 },
57	);
58	hash
59}
60
61#[benchmarks]
62mod benchmarks {
63	use super::*;
64
65	// Expensive note - will reserve.
66	#[benchmark]
67	fn note_preimage(s: Linear<0, MAX_SIZE>) {
68		let caller = funded_account::<T>();
69		let (preimage, hash) = sized_preimage_and_hash::<T>(s);
70
71		#[extrinsic_call]
72		_(RawOrigin::Signed(caller), preimage);
73
74		assert!(Pallet::<T>::have_preimage(&hash));
75	}
76
77	// Cheap note - will not reserve since it was requested.
78	#[benchmark]
79	fn note_requested_preimage(s: Linear<0, MAX_SIZE>) {
80		let caller = funded_account::<T>();
81		let (preimage, hash) = sized_preimage_and_hash::<T>(s);
82		assert_ok!(Pallet::<T>::request_preimage(
83			T::ManagerOrigin::try_successful_origin()
84				.expect("ManagerOrigin has no successful origin required for the benchmark"),
85			hash,
86		));
87
88		#[extrinsic_call]
89		note_preimage(RawOrigin::Signed(caller), preimage);
90
91		assert!(Pallet::<T>::have_preimage(&hash));
92	}
93
94	// Cheap note - will not reserve since it's the manager.
95	#[benchmark]
96	fn note_no_deposit_preimage(s: Linear<0, MAX_SIZE>) {
97		let o = T::ManagerOrigin::try_successful_origin()
98			.expect("ManagerOrigin has no successful origin required for the benchmark");
99		let (preimage, hash) = sized_preimage_and_hash::<T>(s);
100		assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
101
102		#[extrinsic_call]
103		note_preimage(o as T::RuntimeOrigin, preimage);
104
105		assert!(Pallet::<T>::have_preimage(&hash));
106	}
107
108	// Expensive unnote - will unreserve.
109	#[benchmark]
110	fn unnote_preimage() {
111		let caller = funded_account::<T>();
112		let (preimage, hash) = preimage_and_hash::<T>();
113		assert_ok!(Pallet::<T>::note_preimage(RawOrigin::Signed(caller.clone()).into(), preimage));
114
115		#[extrinsic_call]
116		_(RawOrigin::Signed(caller), hash);
117
118		assert!(!Pallet::<T>::have_preimage(&hash));
119	}
120
121	// Cheap unnote - will not unreserve since there's no deposit held.
122	#[benchmark]
123	fn unnote_no_deposit_preimage() {
124		let o = T::ManagerOrigin::try_successful_origin()
125			.expect("ManagerOrigin has no successful origin required for the benchmark");
126		let (preimage, hash) = preimage_and_hash::<T>();
127		assert_ok!(Pallet::<T>::note_preimage(o.clone(), preimage,));
128
129		#[extrinsic_call]
130		unnote_preimage(o as T::RuntimeOrigin, hash);
131
132		assert!(!Pallet::<T>::have_preimage(&hash));
133	}
134
135	// Expensive request - will unreserve the noter's deposit.
136	#[benchmark]
137	fn request_preimage() {
138		let o = T::ManagerOrigin::try_successful_origin()
139			.expect("ManagerOrigin has no successful origin required for the benchmark");
140		let (preimage, hash) = preimage_and_hash::<T>();
141		let noter = funded_account::<T>();
142		assert_ok!(Pallet::<T>::note_preimage(RawOrigin::Signed(noter.clone()).into(), preimage));
143
144		#[extrinsic_call]
145		_(o as T::RuntimeOrigin, hash);
146
147		let ticket =
148			TicketOf::<T>::new(&noter, Footprint { count: 1, size: MAX_SIZE as u64 }).unwrap();
149		let s = RequestStatus::Requested {
150			maybe_ticket: Some((noter, ticket)),
151			count: 1,
152			maybe_len: Some(MAX_SIZE),
153		};
154		assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
155	}
156
157	// Cheap request - would unreserve the deposit but none was held.
158	#[benchmark]
159	fn request_no_deposit_preimage() {
160		let o = T::ManagerOrigin::try_successful_origin()
161			.expect("ManagerOrigin has no successful origin required for the benchmark");
162		let (preimage, hash) = preimage_and_hash::<T>();
163		assert_ok!(Pallet::<T>::note_preimage(o.clone(), preimage,));
164
165		#[extrinsic_call]
166		request_preimage(o as T::RuntimeOrigin, hash);
167
168		let s =
169			RequestStatus::Requested { maybe_ticket: None, count: 2, maybe_len: Some(MAX_SIZE) };
170		assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
171	}
172
173	// Cheap request - the preimage is not yet noted, so deposit to unreserve.
174	#[benchmark]
175	fn request_unnoted_preimage() {
176		let o = T::ManagerOrigin::try_successful_origin()
177			.expect("ManagerOrigin has no successful origin required for the benchmark");
178		let (_, hash) = preimage_and_hash::<T>();
179
180		#[extrinsic_call]
181		request_preimage(o as T::RuntimeOrigin, hash);
182
183		let s = RequestStatus::Requested { maybe_ticket: None, count: 1, maybe_len: None };
184		assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
185	}
186
187	// Cheap request - the preimage is already requested, so just a counter bump.
188	#[benchmark]
189	fn request_requested_preimage() {
190		let o = T::ManagerOrigin::try_successful_origin()
191			.expect("ManagerOrigin has no successful origin required for the benchmark");
192		let (_, hash) = preimage_and_hash::<T>();
193		assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
194
195		#[extrinsic_call]
196		request_preimage(o as T::RuntimeOrigin, hash);
197
198		let s = RequestStatus::Requested { maybe_ticket: None, count: 2, maybe_len: None };
199		assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
200	}
201
202	// Expensive unrequest - last reference and it's noted, so will destroy the preimage.
203	#[benchmark]
204	fn unrequest_preimage() {
205		let o = T::ManagerOrigin::try_successful_origin()
206			.expect("ManagerOrigin has no successful origin required for the benchmark");
207		let (preimage, hash) = preimage_and_hash::<T>();
208		assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
209		assert_ok!(Pallet::<T>::note_preimage(o.clone(), preimage));
210
211		#[extrinsic_call]
212		_(o as T::RuntimeOrigin, hash);
213
214		assert_eq!(RequestStatusFor::<T>::get(&hash), None);
215	}
216
217	// Cheap unrequest - last reference, but it's not noted.
218	#[benchmark]
219	fn unrequest_unnoted_preimage() {
220		let o = T::ManagerOrigin::try_successful_origin()
221			.expect("ManagerOrigin has no successful origin required for the benchmark");
222		let (_, hash) = preimage_and_hash::<T>();
223		assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
224
225		#[extrinsic_call]
226		unrequest_preimage(o as T::RuntimeOrigin, hash);
227
228		assert_eq!(RequestStatusFor::<T>::get(&hash), None);
229	}
230
231	// Cheap unrequest - not the last reference.
232	#[benchmark]
233	fn unrequest_multi_referenced_preimage() {
234		let o = T::ManagerOrigin::try_successful_origin()
235			.expect("ManagerOrigin has no successful origin required for the benchmark");
236		let (_, hash) = preimage_and_hash::<T>();
237		assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
238		assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
239
240		#[extrinsic_call]
241		unrequest_preimage(o as T::RuntimeOrigin, hash);
242
243		let s = RequestStatus::Requested { maybe_ticket: None, count: 1, maybe_len: None };
244		assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
245	}
246
247	#[benchmark]
248	fn ensure_updated(n: Linear<1, MAX_HASH_UPGRADE_BULK_COUNT>) {
249		let caller = funded_account::<T>();
250		let hashes = (0..n).map(|i| insert_old_unrequested::<T>(i)).collect::<Vec<_>>();
251
252		#[extrinsic_call]
253		_(RawOrigin::Signed(caller), hashes);
254
255		assert_eq!(RequestStatusFor::<T>::iter_keys().count(), n as usize);
256		#[allow(deprecated)]
257		let c = StatusFor::<T>::iter_keys().count();
258		assert_eq!(c, 0);
259	}
260
261	impl_benchmark_test_suite! {
262		Pallet,
263		mock::new_test_ext(),
264		mock::Test
265	}
266}