// This file is part of Substrate. // Copyright (C) 2021-2022 Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] //! A BEEFY+MMR pallet combo. //! //! While both BEEFY and Merkle Mountain Range (MMR) can be used separately, //! these tools were designed to work together in unison. //! //! The pallet provides a standardized MMR Leaf format that is can be used //! to bridge BEEFY+MMR-based networks (both standalone and polkadot-like). //! //! The MMR leaf contains: //! 1. Block number and parent block hash. //! 2. Merkle Tree Root Hash of next BEEFY validator set. //! 3. Arbitrary extra leaf data to be used by downstream pallets to include custom data. //! //! and thanks to versioning can be easily updated in the future. use sp_runtime::traits::{Convert, Hash, Member}; use sp_std::prelude::*; use beefy_primitives::mmr::{BeefyDataProvider, BeefyNextAuthoritySet, MmrLeaf, MmrLeafVersion}; use pallet_mmr::{LeafDataProvider, ParentNumberAndHash}; use frame_support::{crypto::ecdsa::ECDSAExt, traits::Get}; pub use pallet::*; #[cfg(test)] mod mock; #[cfg(test)] mod tests; /// A BEEFY consensus digest item with MMR root hash. pub struct DepositBeefyDigest(sp_std::marker::PhantomData); impl pallet_mmr::primitives::OnNewRoot for DepositBeefyDigest where T: pallet_mmr::Config, T: pallet_beefy::Config, { fn on_new_root(root: &::Hash) { let digest = sp_runtime::generic::DigestItem::Consensus( beefy_primitives::BEEFY_ENGINE_ID, codec::Encode::encode(&beefy_primitives::ConsensusLog::< ::BeefyId, >::MmrRoot(*root)), ); >::deposit_log(digest); } } /// Convert BEEFY secp256k1 public keys into Ethereum addresses pub struct BeefyEcdsaToEthereum; impl Convert> for BeefyEcdsaToEthereum { fn convert(a: beefy_primitives::crypto::AuthorityId) -> Vec { sp_core::ecdsa::Public::try_from(a.as_ref()) .map_err(|_| { log::error!(target: "runtime::beefy", "Invalid BEEFY PublicKey format!"); }) .unwrap_or(sp_core::ecdsa::Public::from_raw([0u8; 33])) .to_eth_address() .map(|v| v.to_vec()) .map_err(|_| { log::error!(target: "runtime::beefy", "Failed to convert BEEFY PublicKey to ETH address!"); }) .unwrap_or_default() } } type MerkleRootOf = ::Hash; #[frame_support::pallet] pub mod pallet { #![allow(missing_docs)] use super::*; use frame_support::pallet_prelude::*; /// BEEFY-MMR pallet. #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] pub struct Pallet(_); /// The module's configuration trait. #[pallet::config] #[pallet::disable_frame_system_supertrait_check] pub trait Config: pallet_mmr::Config + pallet_beefy::Config { /// Current leaf version. /// /// Specifies the version number added to every leaf that get's appended to the MMR. /// Read more in [`MmrLeafVersion`] docs about versioning leaves. type LeafVersion: Get; /// Convert BEEFY AuthorityId to a form that would end up in the Merkle Tree. /// /// For instance for ECDSA (secp256k1) we want to store uncompressed public keys (65 bytes) /// and later to Ethereum Addresses (160 bits) to simplify using them on Ethereum chain, /// but the rest of the Substrate codebase is storing them compressed (33 bytes) for /// efficiency reasons. type BeefyAuthorityToMerkleLeaf: Convert<::BeefyId, Vec>; /// The type expected for the leaf extra data type LeafExtra: Member + codec::FullCodec; /// Retrieve arbitrary data that should be added to the mmr leaf type BeefyDataProvider: BeefyDataProvider; } /// Details of next BEEFY authority set. /// /// This storage entry is used as cache for calls to `update_beefy_next_authority_set`. #[pallet::storage] #[pallet::getter(fn beefy_next_authorities)] pub type BeefyNextAuthorities = StorageValue<_, BeefyNextAuthoritySet>, ValueQuery>; } impl LeafDataProvider for Pallet where MerkleRootOf: From + Into, { type LeafData = MmrLeaf< ::BlockNumber, ::Hash, MerkleRootOf, T::LeafExtra, >; fn leaf_data() -> Self::LeafData { MmrLeaf { version: T::LeafVersion::get(), parent_number_and_hash: ParentNumberAndHash::::leaf_data(), leaf_extra: T::BeefyDataProvider::extra_data(), beefy_next_authority_set: Pallet::::update_beefy_next_authority_set(), } } } impl beefy_merkle_tree::Hasher for Pallet where MerkleRootOf: Into, { fn hash(data: &[u8]) -> beefy_merkle_tree::Hash { ::Hashing::hash(data).into() } } impl Pallet where MerkleRootOf: From + Into, { /// Returns details of the next BEEFY authority set. /// /// Details contain authority set id, authority set length and a merkle root, /// constructed from uncompressed secp256k1 public keys converted to Ethereum addresses /// of the next BEEFY authority set. /// /// This function will use a storage-cached entry in case the set didn't change, or compute and /// cache new one in case it did. fn update_beefy_next_authority_set() -> BeefyNextAuthoritySet> { let id = pallet_beefy::Pallet::::validator_set_id() + 1; let current_next = Self::beefy_next_authorities(); // avoid computing the merkle tree if validator set id didn't change. if id == current_next.id { return current_next } let beefy_addresses = pallet_beefy::Pallet::::next_authorities() .into_iter() .map(T::BeefyAuthorityToMerkleLeaf::convert) .collect::>(); let len = beefy_addresses.len() as u32; let root = beefy_merkle_tree::merkle_root::(beefy_addresses).into(); let next_set = BeefyNextAuthoritySet { id, len, root }; // cache the result BeefyNextAuthorities::::put(&next_set); next_set } }