referrerpolicy=no-referrer-when-downgrade

pallet_election_provider_multi_block/unsigned/
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
18use crate::{
19	unsigned::{miner::OffchainWorkerMiner, Call, Config, Pallet},
20	verifier::Verifier,
21	CurrentPhase, Phase,
22};
23use frame_benchmarking::v2::*;
24use frame_election_provider_support::ElectionProvider;
25use frame_support::{assert_ok, pallet_prelude::*};
26use frame_system::RawOrigin;
27use sp_std::boxed::Box;
28
29#[benchmarks(where T: crate::Config + crate::signed::Config + crate::verifier::Config)]
30mod benchmarks {
31	use super::*;
32
33	#[benchmark(pov_mode = Measured)]
34	fn validate_unsigned() -> Result<(), BenchmarkError> {
35		#[cfg(test)]
36		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
37		crate::Pallet::<T>::start().unwrap();
38
39		crate::Pallet::<T>::roll_until_matches(|| {
40			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
41		});
42		let call: Call<T> = OffchainWorkerMiner::<T>::mine_solution(T::MinerPages::get(), false)
43			.map(|solution| Call::submit_unsigned { paged_solution: Box::new(solution) })
44			.unwrap();
45
46		#[block]
47		{
48			assert_ok!(Pallet::<T>::validate_unsigned(TransactionSource::Local, &call));
49		}
50
51		Ok(())
52	}
53
54	#[benchmark(pov_mode = Measured)]
55	fn submit_unsigned() -> Result<(), BenchmarkError> {
56		#[cfg(test)]
57		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
58		crate::Pallet::<T>::start().unwrap();
59
60		// roll to unsigned phase open
61		crate::Pallet::<T>::roll_until_matches(|| {
62			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
63		});
64		// TODO: we need to better ensure that this is actually worst case
65		let solution =
66			OffchainWorkerMiner::<T>::mine_solution(T::MinerPages::get(), false).unwrap();
67
68		// nothing is queued
69		assert!(T::Verifier::queued_score().is_none());
70		#[block]
71		{
72			assert_ok!(Pallet::<T>::submit_unsigned(RawOrigin::None.into(), Box::new(solution)));
73		}
74
75		// something is queued
76		assert!(T::Verifier::queued_score().is_some());
77		Ok(())
78	}
79
80	#[benchmark(extra, pov_mode = Measured)]
81	fn mine_solution(p: Linear<1, { T::Pages::get() }>) -> Result<(), BenchmarkError> {
82		#[cfg(test)]
83		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
84		crate::Pallet::<T>::start().unwrap();
85
86		// roll to unsigned phase open
87		crate::Pallet::<T>::roll_until_matches(|| {
88			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
89		});
90
91		#[block]
92		{
93			OffchainWorkerMiner::<T>::mine_solution(p, true).unwrap();
94		}
95
96		Ok(())
97	}
98
99	impl_benchmark_test_suite!(
100		Pallet,
101		crate::mock::ExtBuilder::full().build_unchecked(),
102		crate::mock::Runtime
103	);
104}