bp_header_chain/justification/
mod.rs1mod verification;
23
24use crate::ChainWithGrandpa;
25pub use verification::{
26 equivocation::{EquivocationsCollector, GrandpaEquivocationsFinder},
27 optimizer::verify_and_optimize_justification,
28 strict::verify_justification,
29 AncestryChain, Error as JustificationVerificationError, JustificationVerificationContext,
30 PrecommitError,
31};
32
33use bp_runtime::{BlockNumberOf, Chain, HashOf, HeaderId};
34use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
35use scale_info::TypeInfo;
36use sp_consensus_grandpa::{AuthorityId, AuthoritySignature};
37use sp_runtime::{traits::Header as HeaderT, RuntimeDebug, SaturatedConversion};
38use sp_std::prelude::*;
39
40#[derive(Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, TypeInfo)]
46#[cfg_attr(feature = "std", derive(Debug))]
47pub struct GrandpaJustification<Header: HeaderT> {
48 pub round: u64,
50 pub commit:
52 finality_grandpa::Commit<Header::Hash, Header::Number, AuthoritySignature, AuthorityId>,
53 pub votes_ancestries: Vec<Header>,
55}
56
57#[cfg(not(feature = "std"))]
62impl<H: HeaderT> core::fmt::Debug for GrandpaJustification<H> {
63 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
64 write!(f, "GrandpaJustification {{ round: {:?}, commit: <wasm:stripped>, votes_ancestries: {:?} }}", self.round, self.votes_ancestries)
65 }
66}
67
68impl<H: HeaderT> GrandpaJustification<H> {
69 pub fn max_reasonable_size<C>(required_precommits: u32) -> u32
74 where
75 C: Chain + ChainWithGrandpa,
76 {
77 let signed_precommit_size: u32 = BlockNumberOf::<C>::max_encoded_len()
87 .saturating_add(HashOf::<C>::max_encoded_len().saturated_into())
88 .saturating_add(64)
89 .saturating_add(AuthorityId::max_encoded_len().saturated_into())
90 .saturated_into();
91 let max_expected_signed_commit_size = signed_precommit_size
92 .saturating_mul(required_precommits)
93 .saturating_add(BlockNumberOf::<C>::max_encoded_len().saturated_into())
94 .saturating_add(HashOf::<C>::max_encoded_len().saturated_into());
95
96 let max_expected_votes_ancestries_size =
97 C::REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY.saturating_mul(C::AVERAGE_HEADER_SIZE);
98
99 8u32.saturating_add(max_expected_signed_commit_size)
102 .saturating_add(max_expected_votes_ancestries_size)
103 }
104
105 pub fn commit_target_id(&self) -> HeaderId<H::Hash, H::Number> {
107 HeaderId(self.commit.target_number, self.commit.target_hash)
108 }
109}
110
111impl<H: HeaderT> crate::FinalityProof<H::Hash, H::Number> for GrandpaJustification<H> {
112 fn target_header_hash(&self) -> H::Hash {
113 self.commit.target_hash
114 }
115
116 fn target_header_number(&self) -> H::Number {
117 self.commit.target_number
118 }
119}
120
121#[derive(Eq, RuntimeDebug, PartialEq)]
123pub enum Error {
124 JustificationDecode,
126}
127
128pub fn required_justification_precommits(authorities_set_length: u32) -> u32 {
133 authorities_set_length - authorities_set_length.saturating_sub(1) / 3
134}
135
136pub fn decode_justification_target<Header: HeaderT>(
138 raw_justification: &[u8],
139) -> Result<(Header::Hash, Header::Number), Error> {
140 GrandpaJustification::<Header>::decode(&mut &*raw_justification)
141 .map(|justification| (justification.commit.target_hash, justification.commit.target_number))
142 .map_err(|_| Error::JustificationDecode)
143}