@@ -100,9 +100,16 @@ impl PartialOrd for BatchQueueEntryPriority {
100100
101101impl Ord for BatchQueueEntryPriority {
102102 fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
103- let ord = other. max_fee . cmp ( & self . max_fee ) ;
103+ // Implementation of lowest-first:
104+ let ord: std:: cmp:: Ordering = other. max_fee . cmp ( & self . max_fee ) ;
105+ // This means, less max_fee will go first
106+ // We want this because we will .pop() to remove unwanted elements, low fee submitions.
107+
104108 if ord == std:: cmp:: Ordering :: Equal {
105- self . nonce . cmp ( & other. nonce ) . reverse ( )
109+ // Case of same max_fee:
110+ // Implementation of biggest-first:
111+ // Since we want to .pop() entries with biggest nonce first, because we want to submit low nonce first
112+ self . nonce . cmp ( & other. nonce )
106113 } else {
107114 ord
108115 }
@@ -155,6 +162,7 @@ pub(crate) fn try_build_batch(
155162 let batch_len = finalized_batch. len ( ) ;
156163 let fee_per_proof = calculate_fee_per_proof ( batch_len, gas_price) ;
157164
165+ // if batch is not acceptable:
158166 if batch_size > max_batch_byte_size
159167 || fee_per_proof > entry. nonced_verification_data . max_fee
160168 || batch_len > max_batch_proof_qty
@@ -311,6 +319,209 @@ mod test {
311319 ) ;
312320 }
313321
322+ #[ test]
323+ fn batch_finalization_algorithm_works_from_same_sender_same_fee ( ) {
324+ // The following information will be the same for each entry, it is just some dummy data to see
325+ // algorithm working.
326+
327+ let proof_generator_addr = Address :: random ( ) ;
328+ let payment_service_addr = Address :: random ( ) ;
329+ let sender_addr = Address :: random ( ) ;
330+ let bytes_for_verification_data = vec ! [ 42_u8 ; 10 ] ;
331+ let dummy_signature = Signature {
332+ r : U256 :: from ( 1 ) ,
333+ s : U256 :: from ( 2 ) ,
334+ v : 3 ,
335+ } ;
336+ let verification_data = VerificationData {
337+ proving_system : ProvingSystemId :: Risc0 ,
338+ proof : bytes_for_verification_data. clone ( ) ,
339+ pub_input : Some ( bytes_for_verification_data. clone ( ) ) ,
340+ verification_key : Some ( bytes_for_verification_data. clone ( ) ) ,
341+ vm_program_code : Some ( bytes_for_verification_data) ,
342+ proof_generator_addr,
343+ } ;
344+ let chain_id = U256 :: from ( 42 ) ;
345+
346+ // Here we create different entries for the batch queue.
347+ // All with the same fee
348+
349+ let max_fee = U256 :: from ( 130000000000000u128 ) ;
350+
351+ // Entry 1
352+ let nonce_1 = U256 :: from ( 1 ) ;
353+ let nonced_verification_data_1 = NoncedVerificationData :: new (
354+ verification_data. clone ( ) ,
355+ nonce_1,
356+ max_fee,
357+ chain_id,
358+ payment_service_addr,
359+ ) ;
360+ let vd_commitment_1: VerificationDataCommitment = nonced_verification_data_1. clone ( ) . into ( ) ;
361+ let entry_1 = BatchQueueEntry :: new_for_testing (
362+ nonced_verification_data_1,
363+ vd_commitment_1,
364+ dummy_signature,
365+ sender_addr,
366+ ) ;
367+ let batch_priority_1 = BatchQueueEntryPriority :: new ( max_fee, nonce_1) ;
368+
369+ // Entry 2
370+ let nonce_2 = U256 :: from ( 2 ) ;
371+ let nonced_verification_data_2 = NoncedVerificationData :: new (
372+ verification_data. clone ( ) ,
373+ nonce_2,
374+ max_fee,
375+ chain_id,
376+ payment_service_addr,
377+ ) ;
378+ let vd_commitment_2: VerificationDataCommitment = nonced_verification_data_2. clone ( ) . into ( ) ;
379+ let entry_2 = BatchQueueEntry :: new_for_testing (
380+ nonced_verification_data_2,
381+ vd_commitment_2,
382+ dummy_signature,
383+ sender_addr,
384+ ) ;
385+ let batch_priority_2 = BatchQueueEntryPriority :: new ( max_fee, nonce_2) ;
386+
387+ // Entry 3
388+ let nonce_3 = U256 :: from ( 3 ) ;
389+ let nonced_verification_data_3 = NoncedVerificationData :: new (
390+ verification_data. clone ( ) ,
391+ nonce_3,
392+ max_fee,
393+ chain_id,
394+ payment_service_addr,
395+ ) ;
396+ let vd_commitment_3: VerificationDataCommitment = nonced_verification_data_3. clone ( ) . into ( ) ;
397+ let entry_3 = BatchQueueEntry :: new_for_testing (
398+ nonced_verification_data_3,
399+ vd_commitment_3,
400+ dummy_signature,
401+ sender_addr,
402+ ) ;
403+ let batch_priority_3 = BatchQueueEntryPriority :: new ( max_fee, nonce_3) ;
404+
405+ let mut batch_queue = BatchQueue :: new ( ) ;
406+ batch_queue. push ( entry_1, batch_priority_1) ;
407+ batch_queue. push ( entry_2, batch_priority_2) ;
408+ batch_queue. push ( entry_3, batch_priority_3) ;
409+
410+ let gas_price = U256 :: from ( 1 ) ;
411+ let ( resulting_batch_queue, batch) =
412+ try_build_batch ( batch_queue, gas_price, 5000000 , 50 ) . unwrap ( ) ;
413+
414+ assert ! ( resulting_batch_queue. is_empty( ) ) ;
415+
416+ assert_eq ! ( batch[ 0 ] . nonced_verification_data. nonce, nonce_3) ;
417+ assert_eq ! ( batch[ 1 ] . nonced_verification_data. nonce, nonce_2) ;
418+ assert_eq ! ( batch[ 2 ] . nonced_verification_data. nonce, nonce_1) ;
419+
420+ // sanity check
421+ assert_eq ! ( batch[ 2 ] . nonced_verification_data. max_fee, max_fee) ;
422+ }
423+
424+ #[ test]
425+ fn batch_finalization_algorithm_works_from_same_sender_same_fee_nonempty_resulting_queue ( ) {
426+ // The following information will be the same for each entry, it is just some dummy data to see
427+ // algorithm working.
428+
429+ let proof_generator_addr = Address :: random ( ) ;
430+ let payment_service_addr = Address :: random ( ) ;
431+ let sender_addr = Address :: random ( ) ;
432+ let bytes_for_verification_data = vec ! [ 42_u8 ; 10 ] ;
433+ let dummy_signature = Signature {
434+ r : U256 :: from ( 1 ) ,
435+ s : U256 :: from ( 2 ) ,
436+ v : 3 ,
437+ } ;
438+ let verification_data = VerificationData {
439+ proving_system : ProvingSystemId :: Risc0 ,
440+ proof : bytes_for_verification_data. clone ( ) ,
441+ pub_input : Some ( bytes_for_verification_data. clone ( ) ) ,
442+ verification_key : Some ( bytes_for_verification_data. clone ( ) ) ,
443+ vm_program_code : Some ( bytes_for_verification_data) ,
444+ proof_generator_addr,
445+ } ;
446+ let chain_id = U256 :: from ( 42 ) ;
447+
448+ // Here we create different entries for the batch queue.
449+ // All with the same fee
450+
451+ let max_fee = U256 :: from ( 130000000000000u128 ) ;
452+
453+ // Entry 1
454+ let nonce_1 = U256 :: from ( 1 ) ;
455+ let nonced_verification_data_1 = NoncedVerificationData :: new (
456+ verification_data. clone ( ) ,
457+ nonce_1,
458+ max_fee,
459+ chain_id,
460+ payment_service_addr,
461+ ) ;
462+ let vd_commitment_1: VerificationDataCommitment = nonced_verification_data_1. clone ( ) . into ( ) ;
463+ let entry_1 = BatchQueueEntry :: new_for_testing (
464+ nonced_verification_data_1,
465+ vd_commitment_1,
466+ dummy_signature,
467+ sender_addr,
468+ ) ;
469+ let batch_priority_1 = BatchQueueEntryPriority :: new ( max_fee, nonce_1) ;
470+
471+ // Entry 2
472+ let nonce_2 = U256 :: from ( 2 ) ;
473+ let nonced_verification_data_2 = NoncedVerificationData :: new (
474+ verification_data. clone ( ) ,
475+ nonce_2,
476+ max_fee,
477+ chain_id,
478+ payment_service_addr,
479+ ) ;
480+ let vd_commitment_2: VerificationDataCommitment = nonced_verification_data_2. clone ( ) . into ( ) ;
481+ let entry_2 = BatchQueueEntry :: new_for_testing (
482+ nonced_verification_data_2,
483+ vd_commitment_2,
484+ dummy_signature,
485+ sender_addr,
486+ ) ;
487+ let batch_priority_2 = BatchQueueEntryPriority :: new ( max_fee, nonce_2) ;
488+
489+ // Entry 3
490+ let nonce_3 = U256 :: from ( 3 ) ;
491+ let nonced_verification_data_3 = NoncedVerificationData :: new (
492+ verification_data. clone ( ) ,
493+ nonce_3,
494+ max_fee,
495+ chain_id,
496+ payment_service_addr,
497+ ) ;
498+ let vd_commitment_3: VerificationDataCommitment = nonced_verification_data_3. clone ( ) . into ( ) ;
499+ let entry_3 = BatchQueueEntry :: new_for_testing (
500+ nonced_verification_data_3,
501+ vd_commitment_3,
502+ dummy_signature,
503+ sender_addr,
504+ ) ;
505+ let batch_priority_3 = BatchQueueEntryPriority :: new ( max_fee, nonce_3) ;
506+
507+ let mut batch_queue = BatchQueue :: new ( ) ;
508+ batch_queue. push ( entry_1, batch_priority_1) ;
509+ batch_queue. push ( entry_2, batch_priority_2) ;
510+ batch_queue. push ( entry_3. clone ( ) , batch_priority_3. clone ( ) ) ;
511+
512+ let gas_price = U256 :: from ( 1 ) ;
513+ let ( resulting_batch_queue, batch) =
514+ try_build_batch ( batch_queue, gas_price, 5000000 , 2 ) . unwrap ( ) ;
515+
516+ assert ! ( resulting_batch_queue. len( ) == 1 ) ; //nonce_3
517+ assert ! ( resulting_batch_queue. clone( ) . pop( ) == Some ( ( entry_3. clone( ) , batch_priority_3. clone( ) ) ) ) ; //nonce_3
518+
519+ assert_eq ! ( batch[ 0 ] . nonced_verification_data. nonce, nonce_2) ;
520+ assert_eq ! ( batch[ 1 ] . nonced_verification_data. nonce, nonce_1) ;
521+ }
522+
523+
524+
314525 #[ test]
315526 fn batch_finalization_algorithm_works_from_different_senders ( ) {
316527 // The following information will be the same for each entry, it is just some dummy data to see
0 commit comments