pallet_bridge_grandpa/
storage_types.rs1use crate::{Config, Error};
20
21use bp_header_chain::{AuthoritySet, ChainWithGrandpa};
22use codec::{Decode, Encode, MaxEncodedLen};
23use frame_support::{traits::Get, BoundedVec, CloneNoBound, DebugNoBound};
24use scale_info::TypeInfo;
25use sp_consensus_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};
26use sp_std::marker::PhantomData;
27
28pub type StoredAuthorityList<MaxBridgedAuthorities> =
30 BoundedVec<(AuthorityId, AuthorityWeight), MaxBridgedAuthorities>;
31
32pub struct StoredAuthorityListLimit<T, I>(PhantomData<(T, I)>);
34
35impl<T: Config<I>, I: 'static> Get<u32> for StoredAuthorityListLimit<T, I> {
36 fn get() -> u32 {
37 T::BridgedChain::MAX_AUTHORITIES_COUNT
38 }
39}
40
41#[derive(CloneNoBound, Decode, Encode, Eq, TypeInfo, MaxEncodedLen, DebugNoBound)]
43#[scale_info(skip_type_params(T, I))]
44pub struct StoredAuthoritySet<T: Config<I>, I: 'static> {
45 pub authorities: StoredAuthorityList<StoredAuthorityListLimit<T, I>>,
47 pub set_id: SetId,
49}
50
51impl<T: Config<I>, I: 'static> StoredAuthoritySet<T, I> {
52 pub fn try_new(authorities: AuthorityList, set_id: SetId) -> Result<Self, Error<T, I>> {
56 Ok(Self {
57 authorities: TryFrom::try_from(authorities)
58 .map_err(|_| Error::TooManyAuthoritiesInSet)?,
59 set_id,
60 })
61 }
62
63 pub fn unused_proof_size(&self) -> u64 {
71 let single_authority_max_encoded_len =
75 <(AuthorityId, AuthorityWeight)>::max_encoded_len() as u64;
76 let extra_authorities =
77 T::BridgedChain::MAX_AUTHORITIES_COUNT.saturating_sub(self.authorities.len() as _);
78 single_authority_max_encoded_len.saturating_mul(extra_authorities as u64)
79 }
80}
81
82impl<T: Config<I>, I: 'static> PartialEq for StoredAuthoritySet<T, I> {
83 fn eq(&self, other: &Self) -> bool {
84 self.set_id == other.set_id && self.authorities == other.authorities
85 }
86}
87
88impl<T: Config<I>, I: 'static> Default for StoredAuthoritySet<T, I> {
89 fn default() -> Self {
90 StoredAuthoritySet { authorities: BoundedVec::default(), set_id: 0 }
91 }
92}
93
94impl<T: Config<I>, I: 'static> From<StoredAuthoritySet<T, I>> for AuthoritySet {
95 fn from(t: StoredAuthoritySet<T, I>) -> Self {
96 AuthoritySet { authorities: t.authorities.into(), set_id: t.set_id }
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use crate::mock::{TestRuntime, MAX_BRIDGED_AUTHORITIES};
103 use bp_test_utils::authority_list;
104
105 type StoredAuthoritySet = super::StoredAuthoritySet<TestRuntime, ()>;
106
107 #[test]
108 fn unused_proof_size_works() {
109 let authority_entry = authority_list().pop().unwrap();
110
111 assert_eq!(
113 StoredAuthoritySet::try_new(
114 vec![authority_entry.clone(); MAX_BRIDGED_AUTHORITIES as usize],
115 0,
116 )
117 .unwrap()
118 .unused_proof_size(),
119 0,
120 );
121
122 assert_eq!(
124 StoredAuthoritySet::try_new(
125 vec![authority_entry; MAX_BRIDGED_AUTHORITIES as usize - 1],
126 0,
127 )
128 .unwrap()
129 .unused_proof_size(),
130 40,
131 );
132
133 }
136}