referrerpolicy=no-referrer-when-downgrade

pallet_im_online/
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//! I'm Online pallet benchmarking.
19
20#![cfg(feature = "runtime-benchmarks")]
21
22use frame_benchmarking::v2::*;
23use frame_support::{traits::UnfilteredDispatchable, WeakBoundedVec};
24use frame_system::RawOrigin;
25use sp_runtime::{traits::Zero, transaction_validity::TransactionSource};
26
27use crate::*;
28
29pub fn create_heartbeat<T: Config>(
30	k: u32,
31) -> Result<
32	(
33		crate::Heartbeat<frame_system::pallet_prelude::BlockNumberFor<T>>,
34		<T::AuthorityId as RuntimeAppPublic>::Signature,
35	),
36	&'static str,
37> {
38	let mut keys = Vec::new();
39	for _ in 0..k {
40		keys.push(T::AuthorityId::generate_pair(None));
41	}
42	let bounded_keys = WeakBoundedVec::<_, T::MaxKeys>::try_from(keys.clone())
43		.map_err(|()| "More than the maximum number of keys provided")?;
44	Keys::<T>::put(bounded_keys);
45
46	let input_heartbeat = Heartbeat {
47		block_number: frame_system::pallet_prelude::BlockNumberFor::<T>::zero(),
48		session_index: 0,
49		authority_index: k - 1,
50		validators_len: keys.len() as u32,
51	};
52
53	let encoded_heartbeat = input_heartbeat.encode();
54	let authority_id = keys.get((k - 1) as usize).ok_or("out of range")?;
55	let signature = authority_id.sign(&encoded_heartbeat).ok_or("couldn't make signature")?;
56
57	Ok((input_heartbeat, signature))
58}
59
60#[benchmarks]
61mod benchmarks {
62	use super::*;
63
64	#[benchmark(extra)]
65	fn heartbeat(k: Linear<1, { <T as Config>::MaxKeys::get() }>) -> Result<(), BenchmarkError> {
66		let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
67
68		#[extrinsic_call]
69		_(RawOrigin::None, input_heartbeat, signature);
70
71		Ok(())
72	}
73
74	#[benchmark(extra)]
75	fn validate_unsigned(
76		k: Linear<1, { <T as Config>::MaxKeys::get() }>,
77	) -> Result<(), BenchmarkError> {
78		let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
79		let call = Call::heartbeat { heartbeat: input_heartbeat, signature };
80
81		#[block]
82		{
83			#[allow(deprecated)]
84			Pallet::<T>::validate_unsigned(TransactionSource::InBlock, &call)
85				.map_err(<&str>::from)?;
86		}
87
88		Ok(())
89	}
90
91	#[benchmark]
92	fn validate_unsigned_and_then_heartbeat(
93		k: Linear<1, { <T as Config>::MaxKeys::get() }>,
94	) -> Result<(), BenchmarkError> {
95		let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
96		let call = Call::heartbeat { heartbeat: input_heartbeat, signature };
97		let call_enc = call.encode();
98
99		#[block]
100		{
101			#[allow(deprecated)]
102			Pallet::<T>::validate_unsigned(TransactionSource::InBlock, &call)
103				.map_err(<&str>::from)?;
104			<Call<T> as Decode>::decode(&mut &*call_enc)
105				.expect("call is encoded above, encoding must be correct")
106				.dispatch_bypass_filter(RawOrigin::None.into())?;
107		}
108
109		Ok(())
110	}
111
112	impl_benchmark_test_suite! {
113		Pallet,
114		mock::new_test_ext(),
115		mock::Runtime
116	}
117}