1#![cfg_attr(not(feature = "std"), no_std)]
19#![warn(missing_docs)]
20
21extern crate alloc;
37
38use sp_runtime::{
39 generic::OpaqueDigestItemId,
40 traits::{Convert, Header, Member},
41 SaturatedConversion,
42};
43
44use alloc::vec::Vec;
45use codec::Decode;
46use pallet_mmr::{primitives::AncestryProof, LeafDataProvider, NodesUtils, ParentNumberAndHash};
47use sp_consensus_beefy::{
48 known_payloads,
49 mmr::{BeefyAuthoritySet, BeefyDataProvider, BeefyNextAuthoritySet, MmrLeaf, MmrLeafVersion},
50 AncestryHelper, AncestryHelperWeightInfo, Commitment, ConsensusLog,
51 ValidatorSet as BeefyValidatorSet,
52};
53
54use frame_support::{crypto::ecdsa::ECDSAExt, pallet_prelude::Weight, traits::Get};
55use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor};
56
57pub use pallet::*;
58pub use weights::WeightInfo;
59
60mod benchmarking;
61#[cfg(test)]
62mod mock;
63#[cfg(test)]
64mod tests;
65mod weights;
66
67pub struct DepositBeefyDigest<T>(core::marker::PhantomData<T>);
69
70impl<T> pallet_mmr::primitives::OnNewRoot<sp_consensus_beefy::MmrRootHash> for DepositBeefyDigest<T>
71where
72 T: pallet_mmr::Config<Hashing = sp_consensus_beefy::MmrHashing>,
73 T: pallet_beefy::Config,
74{
75 fn on_new_root(root: &sp_consensus_beefy::MmrRootHash) {
76 let digest = sp_runtime::generic::DigestItem::Consensus(
77 sp_consensus_beefy::BEEFY_ENGINE_ID,
78 codec::Encode::encode(&sp_consensus_beefy::ConsensusLog::<
79 <T as pallet_beefy::Config>::BeefyId,
80 >::MmrRoot(*root)),
81 );
82 frame_system::Pallet::<T>::deposit_log(digest);
83 }
84}
85
86pub struct BeefyEcdsaToEthereum;
88impl Convert<sp_consensus_beefy::ecdsa_crypto::AuthorityId, Vec<u8>> for BeefyEcdsaToEthereum {
89 fn convert(beefy_id: sp_consensus_beefy::ecdsa_crypto::AuthorityId) -> Vec<u8> {
90 sp_core::ecdsa::Public::from(beefy_id)
91 .to_eth_address()
92 .map(|v| v.to_vec())
93 .map_err(|_| {
94 log::debug!(target: "runtime::beefy", "Failed to convert BEEFY PublicKey to ETH address!");
95 })
96 .unwrap_or_default()
97 }
98}
99
100type MerkleRootOf<T> = <<T as pallet_mmr::Config>::Hashing as sp_runtime::traits::Hash>::Output;
101
102#[frame_support::pallet]
103pub mod pallet {
104 #![allow(missing_docs)]
105
106 use super::*;
107 use frame_support::pallet_prelude::*;
108
109 #[pallet::pallet]
111 pub struct Pallet<T>(_);
112
113 #[pallet::config]
115 #[pallet::disable_frame_system_supertrait_check]
116 pub trait Config: pallet_mmr::Config + pallet_beefy::Config {
117 type LeafVersion: Get<MmrLeafVersion>;
122
123 type BeefyAuthorityToMerkleLeaf: Convert<<Self as pallet_beefy::Config>::BeefyId, Vec<u8>>;
130
131 type LeafExtra: Member + codec::FullCodec;
133
134 type BeefyDataProvider: BeefyDataProvider<Self::LeafExtra>;
136
137 type WeightInfo: WeightInfo;
138 }
139
140 #[pallet::storage]
142 pub type BeefyAuthorities<T: Config> =
143 StorageValue<_, BeefyAuthoritySet<MerkleRootOf<T>>, ValueQuery>;
144
145 #[pallet::storage]
149 pub type BeefyNextAuthorities<T: Config> =
150 StorageValue<_, BeefyNextAuthoritySet<MerkleRootOf<T>>, ValueQuery>;
151}
152
153impl<T: Config> LeafDataProvider for Pallet<T> {
154 type LeafData = MmrLeaf<
155 BlockNumberFor<T>,
156 <T as frame_system::Config>::Hash,
157 MerkleRootOf<T>,
158 T::LeafExtra,
159 >;
160
161 fn leaf_data() -> Self::LeafData {
162 MmrLeaf {
163 version: T::LeafVersion::get(),
164 parent_number_and_hash: ParentNumberAndHash::<T>::leaf_data(),
165 leaf_extra: T::BeefyDataProvider::extra_data(),
166 beefy_next_authority_set: BeefyNextAuthorities::<T>::get(),
167 }
168 }
169}
170
171impl<T> sp_consensus_beefy::OnNewValidatorSet<<T as pallet_beefy::Config>::BeefyId> for Pallet<T>
172where
173 T: pallet::Config,
174{
175 fn on_new_validator_set(
177 current_set: &BeefyValidatorSet<<T as pallet_beefy::Config>::BeefyId>,
178 next_set: &BeefyValidatorSet<<T as pallet_beefy::Config>::BeefyId>,
179 ) {
180 let current = Pallet::<T>::compute_authority_set(current_set);
181 let next = Pallet::<T>::compute_authority_set(next_set);
182 BeefyAuthorities::<T>::put(¤t);
184 BeefyNextAuthorities::<T>::put(&next);
185 }
186}
187
188impl<T: Config> AncestryHelper<HeaderFor<T>> for Pallet<T>
189where
190 T: pallet_mmr::Config<Hashing = sp_consensus_beefy::MmrHashing>,
191{
192 type Proof = AncestryProof<MerkleRootOf<T>>;
193 type ValidationContext = MerkleRootOf<T>;
194
195 fn is_proof_optimal(proof: &Self::Proof) -> bool {
196 let is_proof_optimal = pallet_mmr::Pallet::<T>::is_ancestry_proof_optimal(proof);
197
198 if cfg!(feature = "runtime-benchmarks") {
201 return true
202 }
203
204 is_proof_optimal
205 }
206
207 fn extract_validation_context(header: HeaderFor<T>) -> Option<Self::ValidationContext> {
208 let expected_hash = frame_system::Pallet::<T>::block_hash(header.number());
210 if expected_hash != header.hash() {
211 return None;
212 }
213
214 header.digest().convert_first(|l| {
216 l.try_to(OpaqueDigestItemId::Consensus(&sp_consensus_beefy::BEEFY_ENGINE_ID))
217 .and_then(|log: ConsensusLog<<T as pallet_beefy::Config>::BeefyId>| match log {
218 ConsensusLog::MmrRoot(mmr_root) => Some(mmr_root),
219 _ => None,
220 })
221 })
222 }
223
224 fn is_non_canonical(
225 commitment: &Commitment<BlockNumberFor<T>>,
226 proof: Self::Proof,
227 context: Self::ValidationContext,
228 ) -> bool {
229 let commitment_leaf_count =
230 match pallet_mmr::Pallet::<T>::block_num_to_leaf_count(commitment.block_number) {
231 Ok(commitment_leaf_count) => commitment_leaf_count,
232 Err(_) => {
233 return false
236 },
237 };
238 if commitment_leaf_count != proof.prev_leaf_count {
239 return false;
242 }
243
244 let canonical_mmr_root = context;
245 let canonical_prev_root =
246 match pallet_mmr::Pallet::<T>::verify_ancestry_proof(canonical_mmr_root, proof) {
247 Ok(canonical_prev_root) => canonical_prev_root,
248 Err(_) => {
249 return false
252 },
253 };
254
255 let mut found_commitment_root = false;
256 let commitment_roots = commitment
257 .payload
258 .get_all_decoded::<MerkleRootOf<T>>(&known_payloads::MMR_ROOT_ID);
259 for maybe_commitment_root in commitment_roots {
260 match maybe_commitment_root {
261 Some(commitment_root) => {
262 found_commitment_root = true;
263 if canonical_prev_root != commitment_root {
264 return true;
267 }
268 },
269 None => {
270 return true;
272 },
273 }
274 }
275 if !found_commitment_root {
276 return true;
279 }
280
281 false
282 }
283}
284
285impl<T: Config> AncestryHelperWeightInfo<HeaderFor<T>> for Pallet<T>
286where
287 T: pallet_mmr::Config<Hashing = sp_consensus_beefy::MmrHashing>,
288{
289 fn is_proof_optimal(proof: &<Self as AncestryHelper<HeaderFor<T>>>::Proof) -> Weight {
290 <T as Config>::WeightInfo::n_leafs_proof_is_optimal(proof.leaf_count.saturated_into())
291 }
292
293 fn extract_validation_context() -> Weight {
294 <T as Config>::WeightInfo::extract_validation_context()
295 }
296
297 fn is_non_canonical(proof: &<Self as AncestryHelper<HeaderFor<T>>>::Proof) -> Weight {
298 let mmr_utils = NodesUtils::new(proof.leaf_count);
299 let num_peaks = mmr_utils.number_of_peaks();
300
301 <T as Config>::WeightInfo::n_items_proof_is_non_canonical(
305 proof.items.len().saturating_add(proof.prev_peaks.len()).saturated_into(),
306 )
307 .saturating_add(<T as Config>::WeightInfo::read_peak().saturating_mul(num_peaks))
310 }
311}
312
313impl<T: Config> Pallet<T> {
314 pub fn authority_set_proof() -> BeefyAuthoritySet<MerkleRootOf<T>> {
316 BeefyAuthorities::<T>::get()
317 }
318
319 pub fn next_authority_set_proof() -> BeefyNextAuthoritySet<MerkleRootOf<T>> {
321 BeefyNextAuthorities::<T>::get()
322 }
323
324 fn compute_authority_set(
330 validator_set: &BeefyValidatorSet<<T as pallet_beefy::Config>::BeefyId>,
331 ) -> BeefyAuthoritySet<MerkleRootOf<T>> {
332 let id = validator_set.id();
333 let beefy_addresses = validator_set
334 .validators()
335 .into_iter()
336 .cloned()
337 .map(T::BeefyAuthorityToMerkleLeaf::convert)
338 .collect::<Vec<_>>();
339 let default_eth_addr = [0u8; 20];
340 let len = beefy_addresses.len() as u32;
341 let uninitialized_addresses = beefy_addresses
342 .iter()
343 .filter(|&addr| addr.as_slice().eq(&default_eth_addr))
344 .count();
345 if uninitialized_addresses > 0 {
346 log::error!(
347 target: "runtime::beefy",
348 "Failed to convert {} out of {} BEEFY PublicKeys to ETH addresses!",
349 uninitialized_addresses,
350 len,
351 );
352 }
353 let keyset_commitment = binary_merkle_tree::merkle_root::<
354 <T as pallet_mmr::Config>::Hashing,
355 _,
356 >(beefy_addresses)
357 .into();
358 BeefyAuthoritySet { id, len, keyset_commitment }
359 }
360}
361
362sp_api::decl_runtime_apis! {
363 pub trait BeefyMmrApi<H>
365 where
366 BeefyAuthoritySet<H>: Decode,
367 {
368 fn authority_set_proof() -> BeefyAuthoritySet<H>;
370
371 fn next_authority_set_proof() -> BeefyNextAuthoritySet<H>;
373 }
374}