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