|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2020 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 BABE Pallet. |
| 19 | +
|
| 20 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 21 | + |
| 22 | +use super::*; |
| 23 | +use frame_benchmarking::benchmarks; |
| 24 | + |
| 25 | +type Header = sp_runtime::generic::Header<u64, sp_runtime::traits::BlakeTwo256>; |
| 26 | + |
| 27 | +benchmarks! { |
| 28 | + _ { } |
| 29 | + |
| 30 | + check_equivocation_proof { |
| 31 | + let x in 0 .. 1; |
| 32 | + |
| 33 | + // NOTE: generated with the test below `test_generate_equivocation_report_blob`. |
| 34 | + // the output is not deterministic since keys are generated randomly (and therefore |
| 35 | + // signature content changes). it should not affect the benchmark. |
| 36 | + // with the current benchmark setup it is not possible to generate this programatically |
| 37 | + // from the benchmark setup. |
| 38 | + const EQUIVOCATION_PROOF_BLOB: [u8; 416] = [ |
| 39 | + 222, 241, 46, 66, 243, 228, 135, 233, 177, 64, 149, 170, 141, 92, 193, 106, 51, 73, 31, |
| 40 | + 27, 80, 218, 220, 248, 129, 29, 20, 128, 243, 250, 134, 39, 11, 0, 0, 0, 0, 0, 0, 0, |
| 41 | + 158, 4, 7, 240, 67, 153, 134, 190, 251, 196, 229, 95, 136, 165, 234, 228, 255, 18, 2, |
| 42 | + 187, 76, 125, 108, 50, 67, 33, 196, 108, 38, 115, 179, 86, 40, 36, 27, 5, 105, 58, 228, |
| 43 | + 94, 198, 65, 212, 218, 213, 61, 170, 21, 51, 249, 182, 121, 101, 91, 204, 25, 31, 87, |
| 44 | + 219, 208, 43, 119, 211, 185, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 45 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 6, 66, 65, 66, 69, 52, 2, 0, 0, 0, 0, 11, |
| 46 | + 0, 0, 0, 0, 0, 0, 0, 5, 66, 65, 66, 69, 1, 1, 188, 192, 217, 91, 138, 78, 217, 80, 8, |
| 47 | + 29, 140, 55, 242, 210, 170, 184, 73, 98, 135, 212, 236, 209, 115, 52, 200, 79, 175, |
| 48 | + 172, 242, 161, 199, 47, 236, 93, 101, 95, 43, 34, 141, 16, 247, 220, 33, 59, 31, 197, |
| 49 | + 27, 7, 196, 62, 12, 238, 236, 124, 136, 191, 29, 36, 22, 238, 242, 202, 57, 139, 0, 0, |
| 50 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 51 | + 0, 40, 23, 175, 153, 83, 6, 33, 65, 123, 51, 80, 223, 126, 186, 226, 225, 240, 105, 28, |
| 52 | + 169, 9, 54, 11, 138, 46, 194, 201, 250, 48, 242, 125, 117, 116, 0, 0, 0, 0, 0, 0, 0, 0, |
| 53 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 6, 66, 65, |
| 54 | + 66, 69, 52, 2, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 5, 66, 65, 66, 69, 1, 1, 142, 12, |
| 55 | + 124, 11, 167, 227, 103, 88, 78, 23, 228, 33, 96, 41, 207, 183, 227, 189, 114, 70, 254, |
| 56 | + 30, 128, 243, 233, 83, 214, 45, 74, 182, 120, 119, 64, 243, 219, 119, 63, 240, 205, |
| 57 | + 123, 231, 82, 205, 174, 143, 70, 2, 86, 182, 20, 16, 141, 145, 91, 116, 195, 58, 223, |
| 58 | + 175, 145, 255, 7, 121, 133 |
| 59 | + ]; |
| 60 | + |
| 61 | + let equivocation_proof1: sp_consensus_babe::EquivocationProof<Header> = |
| 62 | + Decode::decode(&mut &EQUIVOCATION_PROOF_BLOB[..]).unwrap(); |
| 63 | + |
| 64 | + let equivocation_proof2 = equivocation_proof1.clone(); |
| 65 | + }: { |
| 66 | + sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof1); |
| 67 | + } verify { |
| 68 | + assert!(sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof2)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + use crate::mock::*; |
| 76 | + use frame_support::assert_ok; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_benchmarks() { |
| 80 | + new_test_ext(3).execute_with(|| { |
| 81 | + assert_ok!(test_benchmark_check_equivocation_proof::<Test>()); |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_generate_equivocation_report_blob() { |
| 87 | + let (pairs, mut ext) = new_test_ext_with_pairs(3); |
| 88 | + |
| 89 | + let offending_authority_index = 0; |
| 90 | + let offending_authority_pair = &pairs[0]; |
| 91 | + |
| 92 | + ext.execute_with(|| { |
| 93 | + start_era(1); |
| 94 | + |
| 95 | + let equivocation_proof = generate_equivocation_proof( |
| 96 | + offending_authority_index, |
| 97 | + offending_authority_pair, |
| 98 | + CurrentSlot::get() + 1, |
| 99 | + ); |
| 100 | + |
| 101 | + println!("equivocation_proof: {:?}", equivocation_proof); |
| 102 | + println!( |
| 103 | + "equivocation_proof.encode(): {:?}", |
| 104 | + equivocation_proof.encode() |
| 105 | + ); |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
0 commit comments