bp_header_chain/justification/verification/
strict.rs1use crate::justification::{
20 verification::{Error, JustificationVerifier, PrecommitError},
21 GrandpaJustification,
22};
23
24use crate::justification::verification::{
25 IterationFlow, JustificationVerificationContext, SignedPrecommit,
26};
27use sp_consensus_grandpa::AuthorityId;
28use sp_runtime::traits::Header as HeaderT;
29use sp_std::{collections::btree_set::BTreeSet, vec::Vec};
30
31struct StrictJustificationVerifier {
33 votes: BTreeSet<AuthorityId>,
34}
35
36impl<Header: HeaderT> JustificationVerifier<Header> for StrictJustificationVerifier {
37 fn process_duplicate_votes_ancestries(
38 &mut self,
39 _duplicate_votes_ancestries: Vec<usize>,
40 ) -> Result<(), Error> {
41 Err(Error::DuplicateVotesAncestries)
42 }
43
44 fn process_redundant_vote(
45 &mut self,
46 _precommit_idx: usize,
47 ) -> Result<IterationFlow, PrecommitError> {
48 Err(PrecommitError::RedundantAuthorityVote)
49 }
50
51 fn process_known_authority_vote(
52 &mut self,
53 _precommit_idx: usize,
54 signed: &SignedPrecommit<Header>,
55 ) -> Result<IterationFlow, PrecommitError> {
56 if self.votes.contains(&signed.id) {
57 return Err(PrecommitError::DuplicateAuthorityVote)
62 }
63
64 Ok(IterationFlow::Run)
65 }
66
67 fn process_unknown_authority_vote(
68 &mut self,
69 _precommit_idx: usize,
70 ) -> Result<(), PrecommitError> {
71 Err(PrecommitError::UnknownAuthorityVote)
72 }
73
74 fn process_unrelated_ancestry_vote(
75 &mut self,
76 _precommit_idx: usize,
77 ) -> Result<IterationFlow, PrecommitError> {
78 Err(PrecommitError::UnrelatedAncestryVote)
79 }
80
81 fn process_invalid_signature_vote(
82 &mut self,
83 _precommit_idx: usize,
84 ) -> Result<(), PrecommitError> {
85 Err(PrecommitError::InvalidAuthoritySignature)
86 }
87
88 fn process_valid_vote(&mut self, signed: &SignedPrecommit<Header>) {
89 self.votes.insert(signed.id.clone());
90 }
91
92 fn process_redundant_votes_ancestries(
93 &mut self,
94 _redundant_votes_ancestries: BTreeSet<Header::Hash>,
95 ) -> Result<(), Error> {
96 Err(Error::RedundantVotesAncestries)
97 }
98}
99
100pub fn verify_justification<Header: HeaderT>(
102 finalized_target: (Header::Hash, Header::Number),
103 context: &JustificationVerificationContext,
104 justification: &GrandpaJustification<Header>,
105) -> Result<(), Error> {
106 let mut verifier = StrictJustificationVerifier { votes: BTreeSet::new() };
107 verifier.verify_justification(finalized_target, context, justification)
108}