@@ -147,6 +147,7 @@ pub(crate) fn try_build_batch(
147147 batch_queue : BatchQueue ,
148148 gas_price : U256 ,
149149 max_batch_size : usize ,
150+ max_batch_len : usize ,
150151) -> Result < ( BatchQueue , Vec < BatchQueueEntry > ) , BatcherError > {
151152 let mut batch_queue = batch_queue;
152153 let mut batch_size = calculate_batch_size ( & batch_queue) ?;
@@ -156,7 +157,7 @@ pub(crate) fn try_build_batch(
156157 let batch_len = batch_queue. len ( ) ;
157158 let fee_per_proof = calculate_fee_per_proof ( batch_len, gas_price) ;
158159
159- if batch_size > max_batch_size || fee_per_proof > entry. nonced_verification_data . max_fee {
160+ if batch_size > max_batch_size || fee_per_proof > entry. nonced_verification_data . max_fee || batch_len > max_batch_len {
160161 // Update the state for the next iteration:
161162 // * Subtract this entry size to the size of the batch size.
162163 // * Push the current entry to the resulting batch queue.
@@ -298,7 +299,7 @@ mod test {
298299
299300 let gas_price = U256 :: from ( 1 ) ;
300301 let ( resulting_batch_queue, batch) =
301- try_build_batch ( batch_queue, gas_price, 5000000 ) . unwrap ( ) ;
302+ try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
302303
303304 assert ! ( resulting_batch_queue. is_empty( ) ) ;
304305
@@ -401,7 +402,7 @@ mod test {
401402
402403 let gas_price = U256 :: from ( 1 ) ;
403404 let ( resulting_batch_queue, finalized_batch) =
404- try_build_batch ( batch_queue, gas_price, 5000000 ) . unwrap ( ) ;
405+ try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
405406
406407 // The resulting batch queue (entries from the old batch queue that were not willing to pay
407408 // in this batch), should be empty and hence, all entries from the batch queue should be in
@@ -512,7 +513,7 @@ mod test {
512513
513514 let gas_price = U256 :: from ( 1 ) ;
514515 let ( resulting_batch_queue, finalized_batch) =
515- try_build_batch ( batch_queue, gas_price, 5000000 ) . unwrap ( ) ;
516+ try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
516517
517518 // The resulting batch queue (entries from the old batch queue that were not willing to pay
518519 // in this batch), should be empty and hence, all entries from the batch queue should be in
@@ -529,4 +530,113 @@ mod test {
529530 max_fee_1
530531 ) ;
531532 }
532- }
533+
534+ #[ test]
535+ fn batch_finalization_algorithm_works_not_bigger_than_max_batch_len ( ) {
536+ // The following information will be the same for each entry, it is just some dummy data to see
537+ // algorithm working.
538+
539+ let proof_generator_addr = Address :: random ( ) ;
540+ let payment_service_addr = Address :: random ( ) ;
541+ let sender_addr = Address :: random ( ) ;
542+ let bytes_for_verification_data = vec ! [ 42_u8 ; 10 ] ;
543+ let dummy_signature = Signature {
544+ r : U256 :: from ( 1 ) ,
545+ s : U256 :: from ( 2 ) ,
546+ v : 3 ,
547+ } ;
548+ let verification_data = VerificationData {
549+ proving_system : ProvingSystemId :: Risc0 ,
550+ proof : bytes_for_verification_data. clone ( ) ,
551+ pub_input : Some ( bytes_for_verification_data. clone ( ) ) ,
552+ verification_key : Some ( bytes_for_verification_data. clone ( ) ) ,
553+ vm_program_code : Some ( bytes_for_verification_data) ,
554+ proof_generator_addr,
555+ } ;
556+ let chain_id = U256 :: from ( 42 ) ;
557+
558+ // Here we create different entries for the batch queue.
559+ // Since we are sending with the same address, the low nonces should have higher max fees.
560+
561+ // Entry 1
562+ let nonce_1 = U256 :: from ( 1 ) ;
563+ let max_fee_1 = U256 :: from ( 1_300_000_000_000_002u128 ) ;
564+ let nonced_verification_data_1 = NoncedVerificationData :: new (
565+ verification_data. clone ( ) ,
566+ nonce_1,
567+ max_fee_1,
568+ chain_id,
569+ payment_service_addr,
570+ ) ;
571+ let vd_commitment_1: VerificationDataCommitment = nonced_verification_data_1. clone ( ) . into ( ) ;
572+ let entry_1 = BatchQueueEntry :: new_for_testing (
573+ nonced_verification_data_1,
574+ vd_commitment_1,
575+ dummy_signature,
576+ sender_addr,
577+ ) ;
578+ let batch_priority_1 = BatchQueueEntryPriority :: new ( max_fee_1, nonce_1) ;
579+
580+ // Entry 2
581+ let nonce_2 = U256 :: from ( 2 ) ;
582+ let max_fee_2 = U256 :: from ( 1_300_000_000_000_001u128 ) ;
583+ let nonced_verification_data_2 = NoncedVerificationData :: new (
584+ verification_data. clone ( ) ,
585+ nonce_2,
586+ max_fee_2,
587+ chain_id,
588+ payment_service_addr,
589+ ) ;
590+ let vd_commitment_2: VerificationDataCommitment = nonced_verification_data_2. clone ( ) . into ( ) ;
591+ let entry_2 = BatchQueueEntry :: new_for_testing (
592+ nonced_verification_data_2,
593+ vd_commitment_2,
594+ dummy_signature,
595+ sender_addr,
596+ ) ;
597+ let batch_priority_2 = BatchQueueEntryPriority :: new ( max_fee_2, nonce_2) ;
598+
599+ // Entry 3
600+ let nonce_3 = U256 :: from ( 3 ) ;
601+ let max_fee_3 = U256 :: from ( 1_300_000_000_000_000u128 ) ;
602+ let nonced_verification_data_3 = NoncedVerificationData :: new (
603+ verification_data. clone ( ) ,
604+ nonce_3,
605+ max_fee_3,
606+ chain_id,
607+ payment_service_addr,
608+ ) ;
609+ let vd_commitment_3: VerificationDataCommitment = nonced_verification_data_3. clone ( ) . into ( ) ;
610+ let entry_3 = BatchQueueEntry :: new_for_testing (
611+ nonced_verification_data_3,
612+ vd_commitment_3,
613+ dummy_signature,
614+ sender_addr,
615+ ) ;
616+ let batch_priority_3 = BatchQueueEntryPriority :: new ( max_fee_3, nonce_3) ;
617+
618+ let mut batch_queue = BatchQueue :: new ( ) ;
619+ batch_queue. push ( entry_1, batch_priority_1) ;
620+ batch_queue. push ( entry_2, batch_priority_2) ;
621+ batch_queue. push ( entry_3, batch_priority_3) ;
622+
623+ let gas_price = U256 :: from ( 1 ) ;
624+
625+ let max_batch_len = 2 ;
626+
627+ let ( resulting_batch_queue, finalized_batch) =
628+ try_build_batch ( batch_queue, gas_price, 5000000 , max_batch_len) . unwrap ( ) ;
629+
630+ assert_eq ! ( resulting_batch_queue. len( ) , 1 ) ;
631+ assert_eq ! ( finalized_batch. len( ) , 2 ) ;
632+ assert_eq ! (
633+ finalized_batch[ 0 ] . nonced_verification_data. max_fee,
634+ max_fee_2
635+ ) ;
636+ assert_eq ! (
637+ finalized_batch[ 1 ] . nonced_verification_data. max_fee,
638+ max_fee_1
639+ ) ;
640+ }
641+
642+ }
0 commit comments