@@ -134,27 +134,25 @@ pub(crate) fn calculate_batch_size(batch_queue: &BatchQueue) -> Result<usize, Ba
134134}
135135
136136/// This function tries to build a batch to be submitted to Aligned.
137- /// Given a copy of the current batch queue, , and applyies an algorithm to find the biggest batch
137+ /// Given the current batch queue applies the following algorithm to find the biggest batch
138138/// of proofs from users that are willing to pay for it:
139139/// 1. Traverse each batch priority queue, starting from the one with minimum max fee.
140140/// 2. Calculate the `fee_per_proof` for the whole batch and compare with the `max_fee` of the entry.
141141/// 3. If `fee_per_proof` is less than the `max_fee` of the current entry, submit the batch. If not, pop this entry
142- /// from the queue and push it to `resulting_priority_queue`, then repeat step 1.
142+ /// from the queue. then repeat step 1.
143143///
144- /// `resulting_priority_queue` will be the batch queue composed of all entries that were not willing to pay for the batch.
145- /// This is outputted in along with the finalized batch.
144+ /// Returns the finalized batch.
146145pub ( crate ) fn try_build_batch (
147146 batch_queue : BatchQueue ,
148147 gas_price : U256 ,
149148 max_batch_byte_size : usize ,
150149 max_batch_proof_qty : usize ,
151- ) -> Result < ( BatchQueue , Vec < BatchQueueEntry > ) , BatcherError > {
152- let mut batch_queue = batch_queue;
153- let mut batch_size = calculate_batch_size ( & batch_queue) ?;
154- let mut resulting_priority_queue = BatchQueue :: new ( ) ;
150+ ) -> Result < Vec < BatchQueueEntry > , BatcherError > {
151+ let mut finalized_batch = batch_queue;
152+ let mut batch_size = calculate_batch_size ( & finalized_batch) ?;
155153
156- while let Some ( ( entry, _) ) = batch_queue . peek ( ) {
157- let batch_len = batch_queue . len ( ) ;
154+ while let Some ( ( entry, _) ) = finalized_batch . peek ( ) {
155+ let batch_len = finalized_batch . len ( ) ;
158156 let fee_per_proof = calculate_fee_per_proof ( batch_len, gas_price) ;
159157
160158 if batch_size > max_batch_byte_size
@@ -173,8 +171,7 @@ pub(crate) fn try_build_batch(
173171 . len ( ) ;
174172 batch_size -= verification_data_size;
175173
176- let ( not_working_entry, not_working_priority) = batch_queue. pop ( ) . unwrap ( ) ;
177- resulting_priority_queue. push ( not_working_entry, not_working_priority) ;
174+ finalized_batch. pop ( ) ;
178175
179176 continue ;
180177 }
@@ -183,16 +180,13 @@ pub(crate) fn try_build_batch(
183180 break ;
184181 }
185182
186- // If `batch_queue_copy ` is empty, this means that all the batch queue was traversed and we didn't find
183+ // If `finalized_batch ` is empty, this means that all the batch queue was traversed and we didn't find
187184 // any user willing to pay fot the fee per proof.
188- if batch_queue . is_empty ( ) {
185+ if finalized_batch . is_empty ( ) {
189186 return Err ( BatcherError :: BatchCostTooHigh ) ;
190187 }
191188
192- Ok ( (
193- resulting_priority_queue,
194- batch_queue. clone ( ) . into_sorted_vec ( ) ,
195- ) )
189+ Ok ( finalized_batch. clone ( ) . into_sorted_vec ( ) )
196190}
197191
198192fn calculate_fee_per_proof ( batch_len : usize , gas_price : U256 ) -> U256 {
@@ -301,14 +295,20 @@ mod test {
301295 batch_queue. push ( entry_3, batch_priority_3) ;
302296
303297 let gas_price = U256 :: from ( 1 ) ;
304- let ( resulting_batch_queue, batch) =
305- try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
298+ let finalized_batch = try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
306299
307- assert ! ( resulting_batch_queue. is_empty( ) ) ;
308-
309- assert_eq ! ( batch[ 0 ] . nonced_verification_data. max_fee, max_fee_3) ;
310- assert_eq ! ( batch[ 1 ] . nonced_verification_data. max_fee, max_fee_2) ;
311- assert_eq ! ( batch[ 2 ] . nonced_verification_data. max_fee, max_fee_1) ;
300+ assert_eq ! (
301+ finalized_batch[ 0 ] . nonced_verification_data. max_fee,
302+ max_fee_3
303+ ) ;
304+ assert_eq ! (
305+ finalized_batch[ 1 ] . nonced_verification_data. max_fee,
306+ max_fee_2
307+ ) ;
308+ assert_eq ! (
309+ finalized_batch[ 2 ] . nonced_verification_data. max_fee,
310+ max_fee_1
311+ ) ;
312312 }
313313
314314 #[ test]
@@ -404,13 +404,11 @@ mod test {
404404 batch_queue. push ( entry_3, batch_priority_3) ;
405405
406406 let gas_price = U256 :: from ( 1 ) ;
407- let ( resulting_batch_queue, finalized_batch) =
408- try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
407+ let finalized_batch = try_build_batch ( batch_queue. clone ( ) , gas_price, 5000000 , 50 ) . unwrap ( ) ;
409408
410- // The resulting batch queue (entries from the old batch queue that were not willing to pay
411- // in this batch), should be empty and hence, all entries from the batch queue should be in
409+ // All entries from the batch queue should be in
412410 // the finalized batch.
413- assert ! ( resulting_batch_queue . is_empty ( ) ) ;
411+ assert_eq ! ( batch_queue . len ( ) , 3 ) ;
414412 assert_eq ! ( finalized_batch. len( ) , 3 ) ;
415413 assert_eq ! (
416414 finalized_batch[ 0 ] . nonced_verification_data. max_fee,
@@ -515,14 +513,10 @@ mod test {
515513 batch_queue. push ( entry_3, batch_priority_3) ;
516514
517515 let gas_price = U256 :: from ( 1 ) ;
518- let ( resulting_batch_queue, finalized_batch) =
519- try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
520-
521- // The resulting batch queue (entries from the old batch queue that were not willing to pay
522- // in this batch), should be empty and hence, all entries from the batch queue should be in
523- // the finalized batch.
516+ let finalized_batch = try_build_batch ( batch_queue. clone ( ) , gas_price, 5000000 , 50 ) . unwrap ( ) ;
524517
525- assert_eq ! ( resulting_batch_queue. len( ) , 1 ) ;
518+ // All but one entries from the batch queue should be in the finalized batch.
519+ assert_eq ! ( batch_queue. len( ) , 3 ) ;
526520 assert_eq ! ( finalized_batch. len( ) , 2 ) ;
527521 assert_eq ! (
528522 finalized_batch[ 0 ] . nonced_verification_data. max_fee,
@@ -628,10 +622,10 @@ mod test {
628622 // The max batch len is 2, so the algorithm should stop at the second entry.
629623 let max_batch_proof_qty = 2 ;
630624
631- let ( resulting_batch_queue , finalized_batch) =
632- try_build_batch ( batch_queue, gas_price, 5000000 , max_batch_proof_qty) . unwrap ( ) ;
625+ let finalized_batch =
626+ try_build_batch ( batch_queue. clone ( ) , gas_price, 5000000 , max_batch_proof_qty) . unwrap ( ) ;
633627
634- assert_eq ! ( resulting_batch_queue . len( ) , 1 ) ;
628+ assert_eq ! ( batch_queue . len( ) , 3 ) ;
635629 assert_eq ! ( finalized_batch. len( ) , 2 ) ;
636630 assert_eq ! (
637631 finalized_batch[ 0 ] . nonced_verification_data. max_fee,
0 commit comments