referrerpolicy=no-referrer-when-downgrade

pallet_bridge_grandpa/
benchmarking.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Benchmarks for the GRANDPA Pallet.
18//!
19//! The main dispatchable for the GRANDPA pallet is `submit_finality_proof_ex`. Our benchmarks
20//! are based around `submit_finality_proof`, though - from weight PoV they are the same calls.
21//! There are to main factors which affect finality proof verification:
22//!
23//! 1. The number of `votes-ancestries` in the justification
24//! 2. The number of `pre-commits` in the justification
25//!
26//! Vote ancestries are the headers between (`finality_target`, `head_of_chain`], where
27//! `header_of_chain` is a descendant of `finality_target`.
28//!
29//! Pre-commits are messages which are signed by validators at the head of the chain they think is
30//! the best.
31//!
32//! Consider the following:
33//!
34//!   / B <- C'
35//! A <- B <- C
36//!
37//! The common ancestor of both forks is block A, so this is what GRANDPA will finalize. In order to
38//! verify this we will have vote ancestries of `[B, C, B', C']` and pre-commits `[C, C']`.
39//!
40//! Note that the worst case scenario here would be a justification where each validator has it's
41//! own fork which is `SESSION_LENGTH` blocks long.
42
43use crate::*;
44
45use bp_header_chain::justification::required_justification_precommits;
46use bp_runtime::BasicOperatingMode;
47use bp_test_utils::{
48	accounts, make_justification_for_header, JustificationGeneratorParams, TEST_GRANDPA_ROUND,
49	TEST_GRANDPA_SET_ID,
50};
51use frame_benchmarking::{benchmarks_instance_pallet, whitelisted_caller};
52use frame_system::RawOrigin;
53use sp_consensus_grandpa::AuthorityId;
54use sp_runtime::traits::{One, Zero};
55use sp_std::vec::Vec;
56
57/// The maximum number of vote ancestries to include in a justification.
58///
59/// In practice this would be limited by the session length (number of blocks a single authority set
60/// can produce) of a given chain.
61const MAX_VOTE_ANCESTRIES: u32 = 1000;
62
63// `1..MAX_VOTE_ANCESTRIES` is too large && benchmarks are running for almost 40m (steps=50,
64// repeat=20) on a decent laptop, which is too much. Since we're building linear function here,
65// let's just select some limited subrange for benchmarking.
66const MAX_VOTE_ANCESTRIES_RANGE_BEGIN: u32 = MAX_VOTE_ANCESTRIES / 20;
67const MAX_VOTE_ANCESTRIES_RANGE_END: u32 =
68	MAX_VOTE_ANCESTRIES_RANGE_BEGIN + MAX_VOTE_ANCESTRIES_RANGE_BEGIN;
69
70// the same with validators - if there are too much validators, let's run benchmarks on subrange
71fn precommits_range_end<T: Config<I>, I: 'static>() -> u32 {
72	let max_bridged_authorities = T::BridgedChain::MAX_AUTHORITIES_COUNT;
73	let max_bridged_authorities = if max_bridged_authorities > 128 {
74		sp_std::cmp::max(128, max_bridged_authorities / 5)
75	} else {
76		max_bridged_authorities
77	};
78
79	required_justification_precommits(max_bridged_authorities)
80}
81
82/// Prepare header and its justification to submit using `submit_finality_proof`.
83fn prepare_benchmark_data<T: Config<I>, I: 'static>(
84	precommits: u32,
85	ancestors: u32,
86) -> (BridgedHeader<T, I>, GrandpaJustification<BridgedHeader<T, I>>) {
87	// going from precommits to total authorities count
88	let total_authorities_count = (3 * precommits - 1) / 2;
89
90	let authority_list = accounts(total_authorities_count as u16)
91		.iter()
92		.map(|id| (AuthorityId::from(*id), 1))
93		.collect::<Vec<_>>();
94
95	let genesis_header: BridgedHeader<T, I> = bp_test_utils::test_header(Zero::zero());
96	let genesis_hash = genesis_header.hash();
97	let init_data = InitializationData {
98		header: Box::new(genesis_header),
99		authority_list,
100		set_id: TEST_GRANDPA_SET_ID,
101		operating_mode: BasicOperatingMode::Normal,
102	};
103
104	bootstrap_bridge::<T, I>(init_data);
105	assert!(<ImportedHeaders<T, I>>::contains_key(genesis_hash));
106
107	let header: BridgedHeader<T, I> = bp_test_utils::test_header(One::one());
108	let params = JustificationGeneratorParams {
109		header: header.clone(),
110		round: TEST_GRANDPA_ROUND,
111		set_id: TEST_GRANDPA_SET_ID,
112		authorities: accounts(precommits as u16).iter().map(|k| (*k, 1)).collect::<Vec<_>>(),
113		ancestors,
114		forks: 1,
115	};
116	let justification = make_justification_for_header(params);
117	(header, justification)
118}
119
120benchmarks_instance_pallet! {
121	// This is the "gold standard" benchmark for this extrinsic, and it's what should be used to
122	// annotate the weight in the pallet.
123	submit_finality_proof {
124		let p in 1 .. precommits_range_end::<T, I>();
125		let v in MAX_VOTE_ANCESTRIES_RANGE_BEGIN..MAX_VOTE_ANCESTRIES_RANGE_END;
126		let caller: T::AccountId = whitelisted_caller();
127		let (header, justification) = prepare_benchmark_data::<T, I>(p, v);
128	}: submit_finality_proof(RawOrigin::Signed(caller), Box::new(header), justification)
129	verify {
130		let genesis_header: BridgedHeader<T, I> = bp_test_utils::test_header(Zero::zero());
131		let header: BridgedHeader<T, I> = bp_test_utils::test_header(One::one());
132		let expected_hash = header.hash();
133
134		// check that the header#1 has been inserted
135		assert_eq!(<BestFinalized<T, I>>::get().unwrap().1, expected_hash);
136		assert!(<ImportedHeaders<T, I>>::contains_key(expected_hash));
137
138		// check that the header#0 has been pruned
139		assert!(!<ImportedHeaders<T, I>>::contains_key(genesis_header.hash()));
140	}
141
142	force_set_pallet_state {
143		let set_id = 100;
144		let authorities = accounts(T::BridgedChain::MAX_AUTHORITIES_COUNT as u16)
145			.iter()
146			.map(|id| (AuthorityId::from(*id), 1))
147			.collect::<Vec<_>>();
148		let (header, _) = prepare_benchmark_data::<T, I>(1, 1);
149		let expected_hash = header.hash();
150	}: force_set_pallet_state(RawOrigin::Root, set_id, authorities, Box::new(header))
151	verify {
152		assert_eq!(<BestFinalized<T, I>>::get().unwrap().1, expected_hash);
153		assert_eq!(<CurrentAuthoritySet<T, I>>::get().set_id, set_id);
154	}
155
156	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
157}