2929//! The MMR leaf contains: 
3030//! 1. Block number and parent block hash. 
3131//! 2. Merkle Tree Root Hash of next BEEFY validator set. 
32- //! 3. Merkle Tree Root Hash of current parachain heads state . 
32+ //! 3. Arbitrary extra leaf data to be used by downstream pallets to include custom data . 
3333//! 
3434//! and thanks to versioning can be easily updated in the future. 
3535
36- use  sp_runtime:: traits:: { Convert ,  Hash } ; 
36+ use  sp_runtime:: traits:: { Convert ,  Hash ,   Member } ; 
3737use  sp_std:: prelude:: * ; 
3838
39- use  beefy_primitives:: mmr:: { BeefyNextAuthoritySet ,  MmrLeaf ,  MmrLeafVersion } ; 
39+ use  beefy_primitives:: mmr:: { BeefyDataProvider ,   BeefyNextAuthoritySet ,  MmrLeaf ,  MmrLeafVersion } ; 
4040use  pallet_mmr:: primitives:: LeafDataProvider ; 
4141
42- use  codec:: Encode ; 
4342use  frame_support:: traits:: Get ; 
4443
4544pub  use  pallet:: * ; 
@@ -90,23 +89,6 @@ impl Convert<beefy_primitives::crypto::AuthorityId, Vec<u8>> for BeefyEcdsaToEth
9089} 
9190
9291type  MerkleRootOf < T >  = <T  as  pallet_mmr:: Config >:: Hash ; 
93- type  ParaId  = u32 ; 
94- type  ParaHead  = Vec < u8 > ; 
95- 
96- /// A type that is able to return current list of parachain heads that end up in the MMR leaf. 
97- pub  trait  ParachainHeadsProvider  { 
98- 	/// Return a list of tuples containing a `ParaId` and Parachain Header data (ParaHead). 
99-  	/// 
100-  	/// The returned data does not have to be sorted. 
101-  	fn  parachain_heads ( )  -> Vec < ( ParaId ,  ParaHead ) > ; 
102- } 
103- 
104- /// A default implementation for runtimes without parachains. 
105- impl  ParachainHeadsProvider  for  ( )  { 
106- 	fn  parachain_heads ( )  -> Vec < ( ParaId ,  ParaHead ) >  { 
107- 		Default :: default ( ) 
108- 	} 
109- } 
11092
11193#[ frame_support:: pallet]  
11294pub  mod  pallet { 
@@ -138,12 +120,11 @@ pub mod pallet {
138120 		/// efficiency reasons. 
139121 		type  BeefyAuthorityToMerkleLeaf :  Convert < <Self  as  pallet_beefy:: Config >:: BeefyId ,  Vec < u8 > > ; 
140122
141- 		/// Retrieve a list of current parachain heads. 
142-  		/// 
143-  		/// The trait is implemented for `paras` module, but since not all chains might have 
144-  		/// parachains, and we want to keep the MMR leaf structure uniform, it's possible to use 
145-  		/// `()` as well to simply put dummy data to the leaf. 
146-  		type  ParachainHeads :  ParachainHeadsProvider ; 
123+ 		/// The type expected for the leaf extra data 
124+  		type  LeafExtra :  Member  + codec:: FullCodec ; 
125+ 
126+ 		/// Retrieve arbitrary data that should be added to the mmr leaf 
127+  		type  BeefyDataProvider :  BeefyDataProvider < Self :: LeafExtra > ; 
147128	} 
148129
149130	/// Details of next BEEFY authority set. 
@@ -163,13 +144,14 @@ where
163144		<T  as  frame_system:: Config >:: BlockNumber , 
164145		<T  as  frame_system:: Config >:: Hash , 
165146		MerkleRootOf < T > , 
147+ 		T :: LeafExtra , 
166148	> ; 
167149
168150	fn  leaf_data ( )  -> Self :: LeafData  { 
169151		MmrLeaf  { 
170152			version :  T :: LeafVersion :: get ( ) , 
171153			parent_number_and_hash :  frame_system:: Pallet :: < T > :: leaf_data ( ) , 
172- 			parachain_heads :   Pallet :: < T > :: parachain_heads_merkle_root ( ) , 
154+ 			leaf_extra :   T :: BeefyDataProvider :: extra_data ( ) , 
173155			beefy_next_authority_set :  Pallet :: < T > :: update_beefy_next_authority_set ( ) , 
174156		} 
175157	} 
@@ -188,23 +170,6 @@ impl<T: Config> Pallet<T>
188170where 
189171	MerkleRootOf < T > :  From < beefy_merkle_tree:: Hash >  + Into < beefy_merkle_tree:: Hash > , 
190172{ 
191- 	/// Returns latest root hash of a merkle tree constructed from all active parachain headers. 
192-  	/// 
193-  	/// The leafs are sorted by `ParaId` to allow more efficient lookups and non-existence proofs. 
194-  	/// 
195-  	/// NOTE this does not include parathreads - only parachains are part of the merkle tree. 
196-  	/// 
197-  	/// NOTE This is an initial and inefficient implementation, which re-constructs 
198-  	/// the merkle tree every block. Instead we should update the merkle root in 
199-  	/// [Self::on_initialize] call of this pallet and update the merkle tree efficiently (use 
200-  	/// on-chain storage to persist inner nodes). 
201-  	fn  parachain_heads_merkle_root ( )  -> MerkleRootOf < T >  { 
202- 		let  mut  para_heads = T :: ParachainHeads :: parachain_heads ( ) ; 
203- 		para_heads. sort ( ) ; 
204- 		let  para_heads = para_heads. into_iter ( ) . map ( |pair| pair. encode ( ) ) ; 
205- 		beefy_merkle_tree:: merkle_root :: < Self ,  _ ,  _ > ( para_heads) . into ( ) 
206- 	} 
207- 
208173	/// Returns details of the next BEEFY authority set. 
209174 	/// 
210175 	/// Details contain authority set id, authority set length and a merkle root, 
0 commit comments