@@ -260,15 +260,15 @@ pub mod pallet {
260260
261261/// Stateless MMR proof verification for batch of leaves.
262262///
263- /// This function can be used to verify received MMR [primitives::Proof ] (`proof`)
263+ /// This function can be used to verify received MMR [primitives::LeafProof ] (`proof`)
264264/// for given leaves set (`leaves`) against a known MMR root hash (`root`).
265265/// Note, the leaves should be sorted such that corresponding leaves and leaf indices have the
266266/// same position in both the `leaves` vector and the `leaf_indices` vector contained in the
267- /// [primitives::Proof ].
267+ /// [primitives::LeafProof ].
268268pub fn verify_leaves_proof < H , L > (
269269 root : H :: Output ,
270270 leaves : Vec < mmr:: Node < H , L > > ,
271- proof : primitives:: Proof < H :: Output > ,
271+ proof : primitives:: LeafProof < H :: Output > ,
272272) -> Result < ( ) , primitives:: Error >
273273where
274274 H : traits:: Hash ,
@@ -342,7 +342,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
342342 pub fn generate_proof (
343343 block_numbers : Vec < BlockNumberFor < T > > ,
344344 best_known_block_number : Option < BlockNumberFor < T > > ,
345- ) -> Result < ( Vec < LeafOf < T , I > > , primitives:: Proof < HashOf < T , I > > ) , primitives:: Error > {
345+ ) -> Result < ( Vec < LeafOf < T , I > > , primitives:: LeafProof < HashOf < T , I > > ) , primitives:: Error > {
346346 // check whether best_known_block_number provided, else use current best block
347347 let best_known_block_number =
348348 best_known_block_number. unwrap_or_else ( || <frame_system:: Pallet < T > >:: block_number ( ) ) ;
@@ -362,11 +362,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
362362 mmr. generate_proof ( leaf_indices)
363363 }
364364
365- /// Return the on-chain MMR root hash.
366- pub fn mmr_root ( ) -> HashOf < T , I > {
367- RootHash :: < T , I > :: get ( )
368- }
369-
370365 /// Verify MMR proof for given `leaves`.
371366 ///
372367 /// This method is safe to use within the runtime code.
@@ -375,7 +370,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
375370 /// or the proof is invalid.
376371 pub fn verify_leaves (
377372 leaves : Vec < LeafOf < T , I > > ,
378- proof : primitives:: Proof < HashOf < T , I > > ,
373+ proof : primitives:: LeafProof < HashOf < T , I > > ,
379374 ) -> Result < ( ) , primitives:: Error > {
380375 if proof. leaf_count > NumberOfLeaves :: < T , I > :: get ( ) ||
381376 proof. leaf_count == 0 ||
@@ -393,4 +388,37 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
393388 Err ( primitives:: Error :: Verify . log_debug ( "The proof is incorrect." ) )
394389 }
395390 }
391+
392+ pub fn generate_ancestry_proof (
393+ prev_block_number : BlockNumberFor < T > ,
394+ best_known_block_number : Option < BlockNumberFor < T > > ,
395+ ) -> Result < primitives:: AncestryProof < HashOf < T , I > > , Error > {
396+ // check whether best_known_block_number provided, else use current best block
397+ let best_known_block_number =
398+ best_known_block_number. unwrap_or_else ( || <frame_system:: Pallet < T > >:: block_number ( ) ) ;
399+
400+ let leaf_count = Self :: block_num_to_leaf_index ( best_known_block_number) ?. saturating_add ( 1 ) ;
401+ let prev_leaf_count = Self :: block_num_to_leaf_index ( prev_block_number) ?. saturating_add ( 1 ) ;
402+
403+ let mmr: ModuleMmr < mmr:: storage:: OffchainStorage , T , I > = mmr:: Mmr :: new ( leaf_count) ;
404+ mmr. generate_ancestry_proof ( prev_leaf_count)
405+ }
406+
407+ pub fn verify_ancestry_proof (
408+ ancestry_proof : primitives:: AncestryProof < HashOf < T , I > > ,
409+ ) -> Result < ( ) , Error > {
410+ let mmr: ModuleMmr < mmr:: storage:: OffchainStorage , T , I > =
411+ mmr:: Mmr :: new ( ancestry_proof. leaf_count ) ;
412+ let is_valid = mmr. verify_ancestry_proof ( ancestry_proof) ?;
413+ if is_valid {
414+ Ok ( ( ) )
415+ } else {
416+ Err ( Error :: Verify . log_debug ( "The ancestry proof is incorrect." ) )
417+ }
418+ }
419+
420+ /// Return the on-chain MMR root hash.
421+ pub fn mmr_root ( ) -> HashOf < T , I > {
422+ RootHash :: < T , I > :: get ( )
423+ }
396424}
0 commit comments