referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Scheduler pallet benchmarking.

use alloc::vec;
use frame_benchmarking::v2::*;
use frame_support::{
	ensure,
	traits::{schedule::Priority, BoundedInline},
	weights::WeightMeter,
};
use frame_system::{EventRecord, RawOrigin};

use crate::*;

type SystemCall<T> = frame_system::Call<T>;
type SystemOrigin<T> = <T as frame_system::Config>::RuntimeOrigin;

const SEED: u32 = 0;
const BLOCK_NUMBER: u32 = 2;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
	let events = frame_system::Pallet::<T>::events();
	let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
	// compare to the last event record
	let EventRecord { event, .. } = &events[events.len() - 1];
	assert_eq!(event, &system_event);
}

/// Add `n` items to the schedule.
///
/// For `resolved`:
/// - `
/// - `None`: aborted (hash without preimage)
/// - `Some(true)`: hash resolves into call if possible, plain call otherwise
/// - `Some(false)`: plain call
fn fill_schedule<T: Config>(
	when: frame_system::pallet_prelude::BlockNumberFor<T>,
	n: u32,
) -> Result<(), &'static str> {
	let t = DispatchTime::At(when);
	let origin: <T as Config>::PalletsOrigin = frame_system::RawOrigin::Root.into();
	for i in 0..n {
		let call = make_call::<T>(None);
		let period = Some(((i + 100).into(), 100));
		let name = u32_to_name(i);
		Pallet::<T>::do_schedule_named(name, t, period, 0, origin.clone(), call)?;
	}
	ensure!(Agenda::<T>::get(when).len() == n as usize, "didn't fill schedule");
	Ok(())
}

fn u32_to_name(i: u32) -> TaskName {
	i.using_encoded(blake2_256)
}

fn make_task<T: Config>(
	periodic: bool,
	named: bool,
	signed: bool,
	maybe_lookup_len: Option<u32>,
	priority: Priority,
) -> ScheduledOf<T> {
	let call = make_call::<T>(maybe_lookup_len);
	let maybe_periodic = match periodic {
		true => Some((100u32.into(), 100)),
		false => None,
	};
	let maybe_id = match named {
		true => Some(u32_to_name(0)),
		false => None,
	};
	let origin = make_origin::<T>(signed);
	Scheduled { maybe_id, priority, call, maybe_periodic, origin, _phantom: PhantomData }
}

fn bounded<T: Config>(len: u32) -> Option<BoundedCallOf<T>> {
	let call =
		<<T as Config>::RuntimeCall>::from(SystemCall::remark { remark: vec![0; len as usize] });
	T::Preimages::bound(call).ok()
}

fn make_call<T: Config>(maybe_lookup_len: Option<u32>) -> BoundedCallOf<T> {
	let bound = BoundedInline::bound() as u32;
	let mut len = match maybe_lookup_len {
		Some(len) => len.min(T::Preimages::MAX_LENGTH as u32 - 2).max(bound) - 3,
		None => bound.saturating_sub(4),
	};

	loop {
		let c = match bounded::<T>(len) {
			Some(x) => x,
			None => {
				len -= 1;
				continue
			},
		};
		if c.lookup_needed() == maybe_lookup_len.is_some() {
			break c
		}
		if maybe_lookup_len.is_some() {
			len += 1;
		} else {
			if len > 0 {
				len -= 1;
			} else {
				break c
			}
		}
	}
}

fn make_origin<T: Config>(signed: bool) -> <T as Config>::PalletsOrigin {
	match signed {
		true => frame_system::RawOrigin::Signed(account("origin", 0, SEED)).into(),
		false => frame_system::RawOrigin::Root.into(),
	}
}

#[benchmarks]
mod benchmarks {
	use super::*;

	// `service_agendas` when no work is done.
	#[benchmark]
	fn service_agendas_base() {
		let now = BLOCK_NUMBER.into();
		IncompleteSince::<T>::put(now - One::one());

		#[block]
		{
			Pallet::<T>::service_agendas(&mut WeightMeter::new(), now, 0);
		}

		assert_eq!(IncompleteSince::<T>::get(), Some(now - One::one()));
	}

	// `service_agenda` when no work is done.
	#[benchmark]
	fn service_agenda_base(
		s: Linear<0, { T::MaxScheduledPerBlock::get() }>,
	) -> Result<(), BenchmarkError> {
		let now = BLOCK_NUMBER.into();
		fill_schedule::<T>(now, s)?;
		let mut executed = 0;

		#[block]
		{
			Pallet::<T>::service_agenda(&mut WeightMeter::new(), &mut executed, now, now, 0);
		}

		assert_eq!(executed, 0);

		Ok(())
	}

	// `service_task` when the task is a non-periodic, non-named, non-fetched call which is not
	// dispatched (e.g. due to being overweight).
	#[benchmark]
	fn service_task_base() {
		let now = BLOCK_NUMBER.into();
		let task = make_task::<T>(false, false, false, None, 0);
		// prevent any tasks from actually being executed as we only want the surrounding weight.
		let mut counter = WeightMeter::with_limit(Weight::zero());
		let _result;

		#[block]
		{
			_result = Pallet::<T>::service_task(&mut counter, now, now, 0, true, task);
		}

		// assert!(_result.is_ok());
	}

	// `service_task` when the task is a non-periodic, non-named, fetched call (with a known
	// preimage length) and which is not dispatched (e.g. due to being overweight).
	#[benchmark(pov_mode = MaxEncodedLen {
		// Use measured PoV size for the Preimages since we pass in a length witness.
		Preimage::PreimageFor: Measured
	})]
	fn service_task_fetched(
		s: Linear<{ BoundedInline::bound() as u32 }, { T::Preimages::MAX_LENGTH as u32 }>,
	) {
		let now = BLOCK_NUMBER.into();
		let task = make_task::<T>(false, false, false, Some(s), 0);
		// prevent any tasks from actually being executed as we only want the surrounding weight.
		let mut counter = WeightMeter::with_limit(Weight::zero());
		let _result;

		#[block]
		{
			_result = Pallet::<T>::service_task(&mut counter, now, now, 0, true, task);
		}

		// assert!(result.is_ok());
	}

	// `service_task` when the task is a non-periodic, named, non-fetched call which is not
	// dispatched (e.g. due to being overweight).
	#[benchmark]
	fn service_task_named() {
		let now = BLOCK_NUMBER.into();
		let task = make_task::<T>(false, true, false, None, 0);
		// prevent any tasks from actually being executed as we only want the surrounding weight.
		let mut counter = WeightMeter::with_limit(Weight::zero());
		let _result;

		#[block]
		{
			_result = Pallet::<T>::service_task(&mut counter, now, now, 0, true, task);
		}

		// assert!(result.is_ok());
	}

	// `service_task` when the task is a periodic, non-named, non-fetched call which is not
	// dispatched (e.g. due to being overweight).
	#[benchmark]
	fn service_task_periodic() {
		let now = BLOCK_NUMBER.into();
		let task = make_task::<T>(true, false, false, None, 0);
		// prevent any tasks from actually being executed as we only want the surrounding weight.
		let mut counter = WeightMeter::with_limit(Weight::zero());
		let _result;

		#[block]
		{
			_result = Pallet::<T>::service_task(&mut counter, now, now, 0, true, task);
		}

		// assert!(result.is_ok());
	}

	// `execute_dispatch` when the origin is `Signed`, not counting the dispatchable's weight.
	#[benchmark]
	fn execute_dispatch_signed() -> Result<(), BenchmarkError> {
		let mut counter = WeightMeter::new();
		let origin = make_origin::<T>(true);
		let call = T::Preimages::realize(&make_call::<T>(None))?.0;
		let result;

		#[block]
		{
			result = Pallet::<T>::execute_dispatch(&mut counter, origin, call);
		}

		assert!(result.is_ok());

		Ok(())
	}

	// `execute_dispatch` when the origin is not `Signed`, not counting the dispatchable's weight.
	#[benchmark]
	fn execute_dispatch_unsigned() -> Result<(), BenchmarkError> {
		let mut counter = WeightMeter::new();
		let origin = make_origin::<T>(false);
		let call = T::Preimages::realize(&make_call::<T>(None))?.0;
		let result;

		#[block]
		{
			result = Pallet::<T>::execute_dispatch(&mut counter, origin, call);
		}

		assert!(result.is_ok());

		Ok(())
	}

	#[benchmark]
	fn schedule(
		s: Linear<0, { T::MaxScheduledPerBlock::get() - 1 }>,
	) -> Result<(), BenchmarkError> {
		let when = BLOCK_NUMBER.into();
		let periodic = Some((BlockNumberFor::<T>::one(), 100));
		let priority = 0;
		// Essentially a no-op call.
		let call = Box::new(SystemCall::set_storage { items: vec![] }.into());

		fill_schedule::<T>(when, s)?;

		#[extrinsic_call]
		_(RawOrigin::Root, when, periodic, priority, call);

		ensure!(Agenda::<T>::get(when).len() == s as usize + 1, "didn't add to schedule");

		Ok(())
	}

	#[benchmark]
	fn cancel(s: Linear<1, { T::MaxScheduledPerBlock::get() }>) -> Result<(), BenchmarkError> {
		let when = BLOCK_NUMBER.into();

		fill_schedule::<T>(when, s)?;
		assert_eq!(Agenda::<T>::get(when).len(), s as usize);
		let schedule_origin =
			T::ScheduleOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

		#[extrinsic_call]
		_(schedule_origin as SystemOrigin<T>, when, 0);

		ensure!(
			s == 1 || Lookup::<T>::get(u32_to_name(0)).is_none(),
			"didn't remove from lookup if more than 1 task scheduled for `when`"
		);
		// Removed schedule is NONE
		ensure!(
			s == 1 || Agenda::<T>::get(when)[0].is_none(),
			"didn't remove from schedule if more than 1 task scheduled for `when`"
		);
		ensure!(
			s > 1 || Agenda::<T>::get(when).len() == 0,
			"remove from schedule if only 1 task scheduled for `when`"
		);

		Ok(())
	}

	#[benchmark]
	fn schedule_named(
		s: Linear<0, { T::MaxScheduledPerBlock::get() - 1 }>,
	) -> Result<(), BenchmarkError> {
		let id = u32_to_name(s);
		let when = BLOCK_NUMBER.into();
		let periodic = Some((BlockNumberFor::<T>::one(), 100));
		let priority = 0;
		// Essentially a no-op call.
		let call = Box::new(SystemCall::set_storage { items: vec![] }.into());

		fill_schedule::<T>(when, s)?;

		#[extrinsic_call]
		_(RawOrigin::Root, id, when, periodic, priority, call);

		ensure!(Agenda::<T>::get(when).len() == s as usize + 1, "didn't add to schedule");

		Ok(())
	}

	#[benchmark]
	fn cancel_named(
		s: Linear<1, { T::MaxScheduledPerBlock::get() }>,
	) -> Result<(), BenchmarkError> {
		let when = BLOCK_NUMBER.into();

		fill_schedule::<T>(when, s)?;

		#[extrinsic_call]
		_(RawOrigin::Root, u32_to_name(0));

		ensure!(
			s == 1 || Lookup::<T>::get(u32_to_name(0)).is_none(),
			"didn't remove from lookup if more than 1 task scheduled for `when`"
		);
		// Removed schedule is NONE
		ensure!(
			s == 1 || Agenda::<T>::get(when)[0].is_none(),
			"didn't remove from schedule if more than 1 task scheduled for `when`"
		);
		ensure!(
			s > 1 || Agenda::<T>::get(when).len() == 0,
			"remove from schedule if only 1 task scheduled for `when`"
		);

		Ok(())
	}

	#[benchmark]
	fn schedule_retry(
		s: Linear<1, { T::MaxScheduledPerBlock::get() }>,
	) -> Result<(), BenchmarkError> {
		let when = BLOCK_NUMBER.into();

		fill_schedule::<T>(when, s)?;
		let name = u32_to_name(s - 1);
		let address = Lookup::<T>::get(name).unwrap();
		let period: BlockNumberFor<T> = 1_u32.into();
		let retry_config = RetryConfig { total_retries: 10, remaining: 10, period };
		Retries::<T>::insert(address, retry_config);
		let (mut when, index) = address;
		let task = Agenda::<T>::get(when)[index as usize].clone().unwrap();
		let mut weight_counter = WeightMeter::with_limit(T::MaximumWeight::get());

		#[block]
		{
			Pallet::<T>::schedule_retry(
				&mut weight_counter,
				when,
				when,
				index,
				&task,
				retry_config,
			);
		}

		when = when + BlockNumberFor::<T>::one();
		assert_eq!(
			Retries::<T>::get((when, 0)),
			Some(RetryConfig { total_retries: 10, remaining: 9, period })
		);

		Ok(())
	}

	#[benchmark]
	fn set_retry() -> Result<(), BenchmarkError> {
		let s = T::MaxScheduledPerBlock::get();
		let when = BLOCK_NUMBER.into();

		fill_schedule::<T>(when, s)?;
		let name = u32_to_name(s - 1);
		let address = Lookup::<T>::get(name).unwrap();
		let (when, index) = address;
		let period = BlockNumberFor::<T>::one();

		#[extrinsic_call]
		_(RawOrigin::Root, (when, index), 10, period);

		assert_eq!(
			Retries::<T>::get((when, index)),
			Some(RetryConfig { total_retries: 10, remaining: 10, period })
		);
		assert_last_event::<T>(
			Event::RetrySet { task: address, id: None, period, retries: 10 }.into(),
		);

		Ok(())
	}

	#[benchmark]
	fn set_retry_named() -> Result<(), BenchmarkError> {
		let s = T::MaxScheduledPerBlock::get();
		let when = BLOCK_NUMBER.into();

		fill_schedule::<T>(when, s)?;
		let name = u32_to_name(s - 1);
		let address = Lookup::<T>::get(name).unwrap();
		let (when, index) = address;
		let period = BlockNumberFor::<T>::one();

		#[extrinsic_call]
		_(RawOrigin::Root, name, 10, period);

		assert_eq!(
			Retries::<T>::get((when, index)),
			Some(RetryConfig { total_retries: 10, remaining: 10, period })
		);
		assert_last_event::<T>(
			Event::RetrySet { task: address, id: Some(name), period, retries: 10 }.into(),
		);

		Ok(())
	}

	#[benchmark]
	fn cancel_retry() -> Result<(), BenchmarkError> {
		let s = T::MaxScheduledPerBlock::get();
		let when = BLOCK_NUMBER.into();

		fill_schedule::<T>(when, s)?;
		let name = u32_to_name(s - 1);
		let address = Lookup::<T>::get(name).unwrap();
		let (when, index) = address;
		let period = BlockNumberFor::<T>::one();
		assert!(Pallet::<T>::set_retry(RawOrigin::Root.into(), (when, index), 10, period).is_ok());

		#[extrinsic_call]
		_(RawOrigin::Root, (when, index));

		assert!(!Retries::<T>::contains_key((when, index)));
		assert_last_event::<T>(Event::RetryCancelled { task: address, id: None }.into());

		Ok(())
	}

	#[benchmark]
	fn cancel_retry_named() -> Result<(), BenchmarkError> {
		let s = T::MaxScheduledPerBlock::get();
		let when = BLOCK_NUMBER.into();

		fill_schedule::<T>(when, s)?;
		let name = u32_to_name(s - 1);
		let address = Lookup::<T>::get(name).unwrap();
		let (when, index) = address;
		let period = BlockNumberFor::<T>::one();
		assert!(Pallet::<T>::set_retry_named(RawOrigin::Root.into(), name, 10, period).is_ok());

		#[extrinsic_call]
		_(RawOrigin::Root, name);

		assert!(!Retries::<T>::contains_key((when, index)));
		assert_last_event::<T>(Event::RetryCancelled { task: address, id: Some(name) }.into());

		Ok(())
	}

	impl_benchmark_test_suite! {
		Pallet,
		mock::new_test_ext(),
		mock::Test
	}
}