referrerpolicy=no-referrer-when-downgrade

pallet_beefy_mmr/
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//! Beefy pallet benchmarking.
19
20#![cfg(feature = "runtime-benchmarks")]
21
22use super::*;
23use crate::Pallet as BeefyMmr;
24use codec::Encode;
25use frame_benchmarking::v2::*;
26use frame_support::traits::Hooks;
27use frame_system::{Config as SystemConfig, Pallet as System};
28use pallet_mmr::{Nodes, Pallet as Mmr};
29use sp_consensus_beefy::Payload;
30use sp_runtime::traits::One;
31
32pub trait Config:
33	pallet_mmr::Config<Hashing = sp_consensus_beefy::MmrHashing> + crate::Config
34{
35}
36
37impl<T> Config for T where
38	T: pallet_mmr::Config<Hashing = sp_consensus_beefy::MmrHashing> + crate::Config
39{
40}
41
42fn init_block<T: Config>(block_num: u32) {
43	let block_num = block_num.into();
44	System::<T>::initialize(&block_num, &<T as SystemConfig>::Hash::default(), &Default::default());
45	Mmr::<T>::on_initialize(block_num);
46}
47
48#[benchmarks]
49mod benchmarks {
50	use super::*;
51
52	/// Generate ancestry proofs with `n` leafs and benchmark the logic that checks
53	/// if the proof is optimal.
54	#[benchmark]
55	fn n_leafs_proof_is_optimal(n: Linear<2, 512>) {
56		pallet_mmr::UseLocalStorage::<T>::set(true);
57
58		for block_num in 1..=n {
59			init_block::<T>(block_num);
60		}
61		let proof = Mmr::<T>::generate_mock_ancestry_proof().unwrap();
62		assert_eq!(proof.leaf_count, n as u64);
63
64		#[block]
65		{
66			<BeefyMmr<T> as AncestryHelper<HeaderFor<T>>>::is_proof_optimal(&proof);
67		};
68	}
69
70	#[benchmark]
71	fn extract_validation_context() {
72		pallet_mmr::UseLocalStorage::<T>::set(true);
73
74		init_block::<T>(1);
75		let header = System::<T>::finalize();
76		frame_system::BlockHash::<T>::insert(BlockNumberFor::<T>::one(), header.hash());
77
78		let validation_context;
79		#[block]
80		{
81			validation_context =
82				<BeefyMmr<T> as AncestryHelper<HeaderFor<T>>>::extract_validation_context(header);
83		}
84
85		assert!(validation_context.is_some());
86	}
87
88	#[benchmark]
89	fn read_peak() {
90		pallet_mmr::UseLocalStorage::<T>::set(true);
91
92		init_block::<T>(1);
93
94		let peak;
95		#[block]
96		{
97			peak = Nodes::<T>::get(0)
98		}
99
100		assert!(peak.is_some());
101	}
102
103	/// Generate ancestry proofs with `n` nodes and benchmark the verification logic.
104	/// These proofs are inflated, containing all the leafs, so we won't read any peak during
105	/// the verification. We need to account for the peaks separately.
106	#[benchmark]
107	fn n_items_proof_is_non_canonical(n: Linear<2, 512>) {
108		pallet_mmr::UseLocalStorage::<T>::set(true);
109
110		for block_num in 1..=n {
111			init_block::<T>(block_num);
112		}
113		let proof = Mmr::<T>::generate_mock_ancestry_proof().unwrap();
114		assert_eq!(proof.items.len(), n as usize);
115
116		let is_non_canonical;
117		#[block]
118		{
119			is_non_canonical = <BeefyMmr<T> as AncestryHelper<HeaderFor<T>>>::is_non_canonical(
120				&Commitment {
121					payload: Payload::from_single_entry(
122						known_payloads::MMR_ROOT_ID,
123						MerkleRootOf::<T>::default().encode(),
124					),
125					block_number: n.into(),
126					validator_set_id: 0,
127				},
128				proof,
129				Mmr::<T>::mmr_root(),
130			);
131		};
132
133		assert_eq!(is_non_canonical, true);
134	}
135
136	impl_benchmark_test_suite!(
137		Pallet,
138		crate::mock::new_test_ext(Default::default()),
139		crate::mock::Test
140	);
141}