Skip to content

Commit ccbe090

Browse files
aurexavre-gius
authored andcommitted
Migrate pallet-preimage to benchmark v2 (paritytech#6277)
Part of: - paritytech#6202. --------- Co-authored-by: Giuseppe Re <[email protected]> Co-authored-by: command-bot <>
1 parent cb9ed40 commit ccbe090

File tree

2 files changed

+231
-215
lines changed

2 files changed

+231
-215
lines changed

substrate/frame/preimage/src/benchmarking.rs

Lines changed: 157 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
//! Preimage pallet benchmarking.
1919
20-
use super::*;
2120
use alloc::vec;
22-
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller, BenchmarkError};
21+
use frame_benchmarking::v2::*;
2322
use frame_support::assert_ok;
2423
use frame_system::RawOrigin;
2524
use sp_runtime::traits::Bounded;
2625

27-
use crate::Pallet as Preimage;
26+
use crate::*;
2827

2928
fn funded_account<T: Config>() -> T::AccountId {
3029
let caller: T::AccountId = whitelisted_caller();
@@ -43,206 +42,225 @@ fn sized_preimage_and_hash<T: Config>(size: u32) -> (Vec<u8>, T::Hash) {
4342
(preimage, hash)
4443
}
4544

46-
benchmarks! {
45+
fn 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]
62+
mod benchmarks {
63+
use super::*;
64+
4765
// Expensive note - will reserve.
48-
note_preimage {
49-
let s in 0 .. MAX_SIZE;
66+
#[benchmark]
67+
fn note_preimage(s: Linear<0, MAX_SIZE>) {
5068
let caller = funded_account::<T>();
5169
let (preimage, hash) = sized_preimage_and_hash::<T>(s);
52-
}: _(RawOrigin::Signed(caller), preimage)
53-
verify {
54-
assert!(Preimage::<T>::have_preimage(&hash));
70+
71+
#[extrinsic_call]
72+
_(RawOrigin::Signed(caller), preimage);
73+
74+
assert!(Pallet::<T>::have_preimage(&hash));
5575
}
76+
5677
// Cheap note - will not reserve since it was requested.
57-
note_requested_preimage {
58-
let s in 0 .. MAX_SIZE;
78+
#[benchmark]
79+
fn note_requested_preimage(s: Linear<0, MAX_SIZE>) {
5980
let caller = funded_account::<T>();
6081
let (preimage, hash) = sized_preimage_and_hash::<T>(s);
61-
assert_ok!(Preimage::<T>::request_preimage(
82+
assert_ok!(Pallet::<T>::request_preimage(
6283
T::ManagerOrigin::try_successful_origin()
6384
.expect("ManagerOrigin has no successful origin required for the benchmark"),
6485
hash,
6586
));
66-
}: note_preimage(RawOrigin::Signed(caller), preimage)
67-
verify {
68-
assert!(Preimage::<T>::have_preimage(&hash));
87+
88+
#[extrinsic_call]
89+
note_preimage(RawOrigin::Signed(caller), preimage);
90+
91+
assert!(Pallet::<T>::have_preimage(&hash));
6992
}
93+
7094
// Cheap note - will not reserve since it's the manager.
71-
note_no_deposit_preimage {
72-
let s in 0 .. MAX_SIZE;
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");
7399
let (preimage, hash) = sized_preimage_and_hash::<T>(s);
74-
assert_ok!(Preimage::<T>::request_preimage(
75-
T::ManagerOrigin::try_successful_origin()
76-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
77-
hash,
78-
));
79-
}: note_preimage<T::RuntimeOrigin>(
80-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
81-
preimage
82-
) verify {
83-
assert!(Preimage::<T>::have_preimage(&hash));
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));
84106
}
85107

86108
// Expensive unnote - will unreserve.
87-
unnote_preimage {
109+
#[benchmark]
110+
fn unnote_preimage() {
88111
let caller = funded_account::<T>();
89112
let (preimage, hash) = preimage_and_hash::<T>();
90-
assert_ok!(Preimage::<T>::note_preimage(RawOrigin::Signed(caller.clone()).into(), preimage));
91-
}: _(RawOrigin::Signed(caller), hash)
92-
verify {
93-
assert!(!Preimage::<T>::have_preimage(&hash));
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));
94119
}
120+
95121
// Cheap unnote - will not unreserve since there's no deposit held.
96-
unnote_no_deposit_preimage {
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");
97126
let (preimage, hash) = preimage_and_hash::<T>();
98-
assert_ok!(Preimage::<T>::note_preimage(
99-
T::ManagerOrigin::try_successful_origin()
100-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
101-
preimage,
102-
));
103-
}: unnote_preimage<T::RuntimeOrigin>(
104-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
105-
hash
106-
) verify {
107-
assert!(!Preimage::<T>::have_preimage(&hash));
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));
108133
}
109134

110135
// Expensive request - will unreserve the noter's deposit.
111-
request_preimage {
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");
112140
let (preimage, hash) = preimage_and_hash::<T>();
113141
let noter = funded_account::<T>();
114-
assert_ok!(Preimage::<T>::note_preimage(RawOrigin::Signed(noter.clone()).into(), preimage));
115-
}: _<T::RuntimeOrigin>(
116-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
117-
hash
118-
) verify {
119-
let ticket = TicketOf::<T>::new(&noter, Footprint { count: 1, size: MAX_SIZE as u64 }).unwrap();
120-
let s = RequestStatus::Requested { maybe_ticket: Some((noter, ticket)), count: 1, maybe_len: Some(MAX_SIZE) };
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+
};
121154
assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
122155
}
156+
123157
// Cheap request - would unreserve the deposit but none was held.
124-
request_no_deposit_preimage {
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");
125162
let (preimage, hash) = preimage_and_hash::<T>();
126-
assert_ok!(Preimage::<T>::note_preimage(
127-
T::ManagerOrigin::try_successful_origin()
128-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
129-
preimage,
130-
));
131-
}: request_preimage<T::RuntimeOrigin>(
132-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
133-
hash
134-
) verify {
135-
let s = RequestStatus::Requested { maybe_ticket: None, count: 2, maybe_len: Some(MAX_SIZE) };
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) };
136170
assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
137171
}
172+
138173
// Cheap request - the preimage is not yet noted, so deposit to unreserve.
139-
request_unnoted_preimage {
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");
140178
let (_, hash) = preimage_and_hash::<T>();
141-
}: request_preimage<T::RuntimeOrigin>(
142-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
143-
hash
144-
) verify {
179+
180+
#[extrinsic_call]
181+
request_preimage(o as T::RuntimeOrigin, hash);
182+
145183
let s = RequestStatus::Requested { maybe_ticket: None, count: 1, maybe_len: None };
146184
assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
147185
}
186+
148187
// Cheap request - the preimage is already requested, so just a counter bump.
149-
request_requested_preimage {
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");
150192
let (_, hash) = preimage_and_hash::<T>();
151-
assert_ok!(Preimage::<T>::request_preimage(
152-
T::ManagerOrigin::try_successful_origin()
153-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
154-
hash,
155-
));
156-
}: request_preimage<T::RuntimeOrigin>(
157-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
158-
hash
159-
) verify {
193+
assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
194+
195+
#[extrinsic_call]
196+
request_preimage(o as T::RuntimeOrigin, hash);
197+
160198
let s = RequestStatus::Requested { maybe_ticket: None, count: 2, maybe_len: None };
161199
assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
162200
}
163201

164202
// Expensive unrequest - last reference and it's noted, so will destroy the preimage.
165-
unrequest_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");
166207
let (preimage, hash) = preimage_and_hash::<T>();
167-
assert_ok!(Preimage::<T>::request_preimage(
168-
T::ManagerOrigin::try_successful_origin()
169-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
170-
hash,
171-
));
172-
assert_ok!(Preimage::<T>::note_preimage(
173-
T::ManagerOrigin::try_successful_origin()
174-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
175-
preimage,
176-
));
177-
}: _<T::RuntimeOrigin>(
178-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
179-
hash
180-
) verify {
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+
181214
assert_eq!(RequestStatusFor::<T>::get(&hash), None);
182215
}
216+
183217
// Cheap unrequest - last reference, but it's not noted.
184-
unrequest_unnoted_preimage {
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");
185222
let (_, hash) = preimage_and_hash::<T>();
186-
assert_ok!(Preimage::<T>::request_preimage(
187-
T::ManagerOrigin::try_successful_origin()
188-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
189-
hash,
190-
));
191-
}: unrequest_preimage<T::RuntimeOrigin>(
192-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
193-
hash
194-
) verify {
223+
assert_ok!(Pallet::<T>::request_preimage(o.clone(), hash,));
224+
225+
#[extrinsic_call]
226+
unrequest_preimage(o as T::RuntimeOrigin, hash);
227+
195228
assert_eq!(RequestStatusFor::<T>::get(&hash), None);
196229
}
230+
197231
// Cheap unrequest - not the last reference.
198-
unrequest_multi_referenced_preimage {
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");
199236
let (_, hash) = preimage_and_hash::<T>();
200-
assert_ok!(Preimage::<T>::request_preimage(
201-
T::ManagerOrigin::try_successful_origin()
202-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
203-
hash,
204-
));
205-
assert_ok!(Preimage::<T>::request_preimage(
206-
T::ManagerOrigin::try_successful_origin()
207-
.expect("ManagerOrigin has no successful origin required for the benchmark"),
208-
hash,
209-
));
210-
}: unrequest_preimage<T::RuntimeOrigin>(
211-
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
212-
hash
213-
) verify {
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+
214243
let s = RequestStatus::Requested { maybe_ticket: None, count: 1, maybe_len: None };
215244
assert_eq!(RequestStatusFor::<T>::get(&hash), Some(s));
216245
}
217246

218-
ensure_updated {
219-
let n in 1..MAX_HASH_UPGRADE_BULK_COUNT;
220-
247+
#[benchmark]
248+
fn ensure_updated(n: Linear<1, MAX_HASH_UPGRADE_BULK_COUNT>) {
221249
let caller = funded_account::<T>();
222250
let hashes = (0..n).map(|i| insert_old_unrequested::<T>(i)).collect::<Vec<_>>();
223-
}: _(RawOrigin::Signed(caller), hashes)
224-
verify {
251+
252+
#[extrinsic_call]
253+
_(RawOrigin::Signed(caller), hashes);
254+
225255
assert_eq!(RequestStatusFor::<T>::iter_keys().count(), n as usize);
226256
#[allow(deprecated)]
227257
let c = StatusFor::<T>::iter_keys().count();
228258
assert_eq!(c, 0);
229259
}
230260

231-
impl_benchmark_test_suite!(Preimage, crate::mock::new_test_ext(), crate::mock::Test);
232-
}
233-
234-
fn insert_old_unrequested<T: Config>(s: u32) -> <T as frame_system::Config>::Hash {
235-
let acc = account("old", s, 0);
236-
T::Currency::make_free_balance_be(&acc, BalanceOf::<T>::max_value() / 2u32.into());
237-
238-
// The preimage size does not matter here as it is not touched.
239-
let preimage = s.to_le_bytes();
240-
let hash = <T as frame_system::Config>::Hashing::hash(&preimage[..]);
241-
242-
#[allow(deprecated)]
243-
StatusFor::<T>::insert(
244-
&hash,
245-
OldRequestStatus::Unrequested { deposit: (acc, 123u32.into()), len: preimage.len() as u32 },
246-
);
247-
hash
261+
impl_benchmark_test_suite! {
262+
Pallet,
263+
mock::new_test_ext(),
264+
mock::Test
265+
}
248266
}

0 commit comments

Comments
 (0)