referrerpolicy=no-referrer-when-downgrade

pallet_grandpa/
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//! Benchmarks for the GRANDPA pallet.
19
20use super::*;
21use frame_benchmarking::v2::*;
22use frame_system::RawOrigin;
23use sp_core::H256;
24
25#[benchmarks]
26mod benchmarks {
27	use super::*;
28
29	#[benchmark]
30	fn check_equivocation_proof(x: Linear<0, 1>) {
31		// NOTE: generated with the test below `test_generate_equivocation_report_blob`.
32		// the output should be deterministic since the keys we use are static.
33		// with the current benchmark setup it is not possible to generate this
34		// programmatically from the benchmark setup.
35		const EQUIVOCATION_PROOF_BLOB: [u8; 257] = [
36			1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 136, 220, 52, 23, 213, 5, 142, 196,
37			180, 80, 62, 12, 18, 234, 26, 10, 137, 190, 32, 15, 233, 137, 34, 66, 61, 67, 52, 1,
38			79, 166, 176, 238, 207, 48, 195, 55, 171, 225, 252, 130, 161, 56, 151, 29, 193, 32, 25,
39			157, 249, 39, 80, 193, 214, 96, 167, 147, 25, 130, 45, 42, 64, 208, 182, 164, 10, 0, 0,
40			0, 0, 0, 0, 0, 234, 236, 231, 45, 70, 171, 135, 246, 136, 153, 38, 167, 91, 134, 150,
41			242, 215, 83, 56, 238, 16, 119, 55, 170, 32, 69, 255, 248, 164, 20, 57, 50, 122, 115,
42			135, 96, 80, 203, 131, 232, 73, 23, 149, 86, 174, 59, 193, 92, 121, 76, 154, 211, 44,
43			96, 10, 84, 159, 133, 211, 56, 103, 0, 59, 2, 96, 20, 69, 2, 32, 179, 16, 184, 108, 76,
44			215, 64, 195, 78, 143, 73, 177, 139, 20, 144, 98, 231, 41, 117, 255, 220, 115, 41, 59,
45			27, 75, 56, 10, 0, 0, 0, 0, 0, 0, 0, 128, 179, 250, 48, 211, 76, 10, 70, 74, 230, 219,
46			139, 96, 78, 88, 112, 33, 170, 44, 184, 59, 200, 155, 143, 128, 40, 222, 179, 210, 190,
47			84, 16, 182, 21, 34, 94, 28, 193, 163, 226, 51, 251, 134, 233, 187, 121, 63, 157, 240,
48			165, 203, 92, 16, 146, 120, 190, 229, 251, 129, 29, 45, 32, 29, 6,
49		];
50
51		let equivocation_proof1: sp_consensus_grandpa::EquivocationProof<H256, u64> =
52			Decode::decode(&mut &EQUIVOCATION_PROOF_BLOB[..]).unwrap();
53
54		let equivocation_proof2 = equivocation_proof1.clone();
55
56		#[block]
57		{
58			sp_consensus_grandpa::check_equivocation_proof(equivocation_proof1);
59		}
60
61		assert!(sp_consensus_grandpa::check_equivocation_proof(equivocation_proof2));
62	}
63
64	#[benchmark]
65	fn note_stalled() {
66		let delay = 1000u32.into();
67		let best_finalized_block_number = 1u32.into();
68
69		#[extrinsic_call]
70		_(RawOrigin::Root, delay, best_finalized_block_number);
71
72		assert!(Stalled::<T>::get().is_some());
73	}
74
75	impl_benchmark_test_suite!(
76		Pallet,
77		crate::mock::new_test_ext(vec![(1, 1), (2, 1), (3, 1)]),
78		crate::mock::Test,
79	);
80}
81
82#[cfg(test)]
83mod tests {
84	use super::*;
85	use crate::mock::*;
86
87	#[test]
88	fn test_generate_equivocation_report_blob() {
89		let authorities = crate::tests::test_authorities();
90
91		let equivocation_authority_index = 0;
92		let equivocation_key = &authorities[equivocation_authority_index].0;
93		let equivocation_keyring = extract_keyring(equivocation_key);
94
95		new_test_ext_raw_authorities(authorities).execute_with(|| {
96			start_era(1);
97
98			// generate an equivocation proof, with two votes in the same round for
99			// different block hashes signed by the same key
100			let equivocation_proof = generate_equivocation_proof(
101				1,
102				(1, H256::random(), 10, &equivocation_keyring),
103				(1, H256::random(), 10, &equivocation_keyring),
104			);
105
106			println!("equivocation_proof: {:?}", equivocation_proof);
107			println!("equivocation_proof.encode(): {:?}", equivocation_proof.encode());
108		});
109	}
110}