@@ -266,6 +266,7 @@ impl<E: RStorage + Clock + Metrics, H: CHasher> Mmr<E, H> {
266266 orphaned_leaf = Some ( item) ;
267267 }
268268 journal. rewind ( last_valid_size) . await ?;
269+ journal. sync ( ) . await ?;
269270 journal_size = last_valid_size
270271 }
271272
@@ -483,7 +484,8 @@ impl<E: RStorage + Clock + Metrics, H: CHasher> Mmr<E, H> {
483484 }
484485
485486 /// Pop the given number of elements from the tip of the MMR assuming they exist, and otherwise
486- /// return Empty or ElementPruned errors.
487+ /// return Empty or ElementPruned errors. The backing journal is synced to disk before
488+ /// returning.
487489 ///
488490 /// # Warning
489491 ///
@@ -522,6 +524,7 @@ impl<E: RStorage + Clock + Metrics, H: CHasher> Mmr<E, H> {
522524 }
523525
524526 self . journal . rewind ( new_size) . await ?;
527+ self . journal . sync ( ) . await ?;
525528 self . journal_size = new_size;
526529
527530 // Reset the mem_mmr to one of the new_size in the "prune_all" state.
@@ -561,10 +564,6 @@ impl<E: RStorage + Clock + Metrics, H: CHasher> Mmr<E, H> {
561564 /// Process all batched updates and sync the MMR to disk. If `pool` is non-null, then it will be
562565 /// used to parallelize the sync.
563566 pub async fn sync ( & mut self , h : & mut impl Hasher < H > ) -> Result < ( ) , Error > {
564- if self . size ( ) == 0 {
565- return Ok ( ( ) ) ;
566- }
567-
568567 // Write the nodes cached in the memory-resident MMR to the journal.
569568 self . mem_mmr . sync ( h) ;
570569
@@ -852,6 +851,20 @@ mod tests {
852851 assert ! ( mmr. prune_to_pos( & mut hasher, 0 ) . await . is_ok( ) ) ;
853852 assert ! ( mmr. sync( & mut hasher) . await . is_ok( ) ) ;
854853 assert ! ( matches!( mmr. pop( 1 ) . await , Err ( Error :: Empty ) ) ) ;
854+
855+ mmr. add ( & mut hasher, & test_digest ( 0 ) ) . await . unwrap ( ) ;
856+ assert_eq ! ( mmr. size( ) , 1 ) ;
857+ mmr. sync ( & mut hasher) . await . unwrap ( ) ;
858+ assert ! ( mmr. get_node( 0 ) . await . is_ok( ) ) ;
859+ assert ! ( mmr. pop( 1 ) . await . is_ok( ) ) ;
860+ assert_eq ! ( mmr. size( ) , 0 ) ;
861+ mmr. sync ( & mut hasher) . await . unwrap ( ) ;
862+
863+ let mmr = Mmr :: init ( context. clone ( ) , & mut hasher, test_config ( ) )
864+ . await
865+ . unwrap ( ) ;
866+ assert_eq ! ( mmr. size( ) , 0 ) ;
867+
855868 mmr. destroy ( ) . await . unwrap ( ) ;
856869 } ) ;
857870 }
0 commit comments