mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-24 09:01:08 +00:00
Refactor Beefy MMR and remove parachain specific implementations (#10664)
* refactor beefy mmr * use plain vector of bytes for leaf extra * update comment * update comments * remove unused vars * Use sp_std::vec::Vec Co-authored-by: Adrian Catangiu <adrian@parity.io> * make extra data generic * fix tests * refactor beefy-mmr * Update frame/beefy-mmr/src/lib.rs * minor fix * fmt * Update frame/beefy-mmr/src/lib.rs Co-authored-by: Adrian Catangiu <adrian@parity.io>
This commit is contained in:
@@ -29,17 +29,16 @@
|
||||
//! The MMR leaf contains:
|
||||
//! 1. Block number and parent block hash.
|
||||
//! 2. Merkle Tree Root Hash of next BEEFY validator set.
|
||||
//! 3. Merkle Tree Root Hash of current parachain heads state.
|
||||
//! 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};
|
||||
use sp_runtime::traits::{Convert, Hash, Member};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
use beefy_primitives::mmr::{BeefyNextAuthoritySet, MmrLeaf, MmrLeafVersion};
|
||||
use beefy_primitives::mmr::{BeefyDataProvider, BeefyNextAuthoritySet, MmrLeaf, MmrLeafVersion};
|
||||
use pallet_mmr::primitives::LeafDataProvider;
|
||||
|
||||
use codec::Encode;
|
||||
use frame_support::traits::Get;
|
||||
|
||||
pub use pallet::*;
|
||||
@@ -90,23 +89,6 @@ impl Convert<beefy_primitives::crypto::AuthorityId, Vec<u8>> for BeefyEcdsaToEth
|
||||
}
|
||||
|
||||
type MerkleRootOf<T> = <T as pallet_mmr::Config>::Hash;
|
||||
type ParaId = u32;
|
||||
type ParaHead = Vec<u8>;
|
||||
|
||||
/// A type that is able to return current list of parachain heads that end up in the MMR leaf.
|
||||
pub trait ParachainHeadsProvider {
|
||||
/// Return a list of tuples containing a `ParaId` and Parachain Header data (ParaHead).
|
||||
///
|
||||
/// The returned data does not have to be sorted.
|
||||
fn parachain_heads() -> Vec<(ParaId, ParaHead)>;
|
||||
}
|
||||
|
||||
/// A default implementation for runtimes without parachains.
|
||||
impl ParachainHeadsProvider for () {
|
||||
fn parachain_heads() -> Vec<(ParaId, ParaHead)> {
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
@@ -138,12 +120,11 @@ pub mod pallet {
|
||||
/// efficiency reasons.
|
||||
type BeefyAuthorityToMerkleLeaf: Convert<<Self as pallet_beefy::Config>::BeefyId, Vec<u8>>;
|
||||
|
||||
/// Retrieve a list of current parachain heads.
|
||||
///
|
||||
/// The trait is implemented for `paras` module, but since not all chains might have
|
||||
/// parachains, and we want to keep the MMR leaf structure uniform, it's possible to use
|
||||
/// `()` as well to simply put dummy data to the leaf.
|
||||
type ParachainHeads: ParachainHeadsProvider;
|
||||
/// 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<Self::LeafExtra>;
|
||||
}
|
||||
|
||||
/// Details of next BEEFY authority set.
|
||||
@@ -163,13 +144,14 @@ where
|
||||
<T as frame_system::Config>::BlockNumber,
|
||||
<T as frame_system::Config>::Hash,
|
||||
MerkleRootOf<T>,
|
||||
T::LeafExtra,
|
||||
>;
|
||||
|
||||
fn leaf_data() -> Self::LeafData {
|
||||
MmrLeaf {
|
||||
version: T::LeafVersion::get(),
|
||||
parent_number_and_hash: frame_system::Pallet::<T>::leaf_data(),
|
||||
parachain_heads: Pallet::<T>::parachain_heads_merkle_root(),
|
||||
leaf_extra: T::BeefyDataProvider::extra_data(),
|
||||
beefy_next_authority_set: Pallet::<T>::update_beefy_next_authority_set(),
|
||||
}
|
||||
}
|
||||
@@ -188,23 +170,6 @@ impl<T: Config> Pallet<T>
|
||||
where
|
||||
MerkleRootOf<T>: From<beefy_merkle_tree::Hash> + Into<beefy_merkle_tree::Hash>,
|
||||
{
|
||||
/// Returns latest root hash of a merkle tree constructed from all active parachain headers.
|
||||
///
|
||||
/// The leafs are sorted by `ParaId` to allow more efficient lookups and non-existence proofs.
|
||||
///
|
||||
/// NOTE this does not include parathreads - only parachains are part of the merkle tree.
|
||||
///
|
||||
/// NOTE This is an initial and inefficient implementation, which re-constructs
|
||||
/// the merkle tree every block. Instead we should update the merkle root in
|
||||
/// [Self::on_initialize] call of this pallet and update the merkle tree efficiently (use
|
||||
/// on-chain storage to persist inner nodes).
|
||||
fn parachain_heads_merkle_root() -> MerkleRootOf<T> {
|
||||
let mut para_heads = T::ParachainHeads::parachain_heads();
|
||||
para_heads.sort();
|
||||
let para_heads = para_heads.into_iter().map(|pair| pair.encode());
|
||||
beefy_merkle_tree::merkle_root::<Self, _, _>(para_heads).into()
|
||||
}
|
||||
|
||||
/// Returns details of the next BEEFY authority set.
|
||||
///
|
||||
/// Details contain authority set id, authority set length and a merkle root,
|
||||
|
||||
Reference in New Issue
Block a user