@@ -108,33 +108,54 @@ mod test {
108108 }
109109
110110 #[ tokio:: test]
111- async fn test_database_finalize_batch_commit ( ) {
111+ async fn test_database_finalize_batch_commits ( ) {
112112 // Set up the test database.
113113 let db = setup_test_db ( ) . await ;
114114
115115 // Generate unstructured bytes.
116- let mut bytes = [ 0u8 ; 1024 ] ;
116+ let mut bytes = [ 0u8 ; 2048 ] ;
117117 rand:: rng ( ) . fill ( bytes. as_mut_slice ( ) ) ;
118118 let mut u = Unstructured :: new ( & bytes) ;
119119
120- // Generate a random BatchCommitData.
121- let batch_commit = BatchCommitData :: arbitrary ( & mut u) . unwrap ( ) ;
120+ // Generate 10 finalized batches at L1 block 100.
121+ for i in 0 ..10 {
122+ let batch_commit = BatchCommitData {
123+ index : i,
124+ calldata : Arc :: new ( vec ! [ ] . into ( ) ) ,
125+ finalized_block_number : Some ( 100 ) ,
126+ ..Arbitrary :: arbitrary ( & mut u) . unwrap ( )
127+ } ;
128+ db. insert_batch ( batch_commit. clone ( ) ) . await . unwrap ( ) ;
129+ }
130+ // Finalize all batches below batch index 10.
131+ db. finalize_batches_up_to_index ( 10 , 100 ) . await . unwrap ( ) ;
122132
123- // Store the batch and finalize it.
124- let finalized_block_number = u64:: arbitrary ( & mut u) . unwrap ( ) ;
125- db. insert_batch ( batch_commit. clone ( ) ) . await . unwrap ( ) ;
126- db. finalize_batch ( batch_commit. hash , finalized_block_number) . await . unwrap ( ) ;
133+ // Generate 10 commit batches not finalized.
134+ for i in 10 ..20 {
135+ let batch_commit = BatchCommitData {
136+ index : i,
137+ calldata : Arc :: new ( vec ! [ ] . into ( ) ) ,
138+ finalized_block_number : None ,
139+ ..Arbitrary :: arbitrary ( & mut u) . unwrap ( )
140+ } ;
141+ db. insert_batch ( batch_commit. clone ( ) ) . await . unwrap ( ) ;
142+ }
143+
144+ // Finalize all batches below batch index 15.
145+ db. finalize_batches_up_to_index ( 15 , 200 ) . await . unwrap ( ) ;
127146
128147 // Verify the finalized_block_number is correctly updated.
129- let finalized_block_number_from_db = models:: batch_commit:: Entity :: find ( )
130- . filter ( models:: batch_commit:: Column :: Hash . eq ( batch_commit. hash . to_vec ( ) ) )
131- . one ( db. get_connection ( ) )
132- . await
133- . unwrap ( )
134- . unwrap ( )
135- . finalized_block_number
136- . unwrap ( ) ;
137- assert_eq ! ( finalized_block_number, finalized_block_number_from_db as u64 ) ;
148+ let batches = db. get_batches ( ) . await . unwrap ( ) . collect :: < Vec < Result < _ , _ > > > ( ) . await ;
149+ for batch in batches {
150+ let batch = batch. unwrap ( ) ;
151+ if batch. index < 10 {
152+ assert_eq ! ( batch. finalized_block_number, Some ( 100 ) ) ;
153+ } else if batch. index <= 15 {
154+ assert_eq ! ( batch. finalized_block_number, Some ( 200 ) ) ;
155+ } else {
156+ assert_eq ! ( batch. finalized_block_number, None ) ;
157+ }
158+ }
138159 }
139160
140161 #[ tokio:: test]
@@ -281,7 +302,7 @@ mod test {
281302 // Finalize batch up to block number 110.
282303 if block_number <= 110 {
283304 finalized_batches_hashes. push ( hash) ;
284- db. finalize_batch ( hash , block_number) . await . unwrap ( ) ;
305+ db. finalize_batches_up_to_index ( batch_index , block_number) . await . unwrap ( ) ;
285306 }
286307
287308 block_number += 1 ;
0 commit comments