@@ -704,7 +704,7 @@ impl Batcher {
704704 // This is needed because we need to query the user state to make validations and
705705 // finally add the proof to the batch queue.
706706
707- let batch_state_lock = self . batch_state . lock ( ) . await ;
707+ let mut batch_state_lock = self . batch_state . lock ( ) . await ;
708708
709709 let msg_max_fee = nonced_verification_data. max_fee ;
710710 let Some ( user_last_max_fee_limit) =
@@ -800,17 +800,38 @@ impl Batcher {
800800 // * Perform validation over batcher queue *
801801 // * ---------------------------------------------------------------------*
802802
803- // if max batch qty exceded, remove least priority element
804803 if batch_state_lock. batch_queue . len ( ) == self . max_queue_size {
805- info ! ( "Queue limit exceded, removing least priority element" ) ;
806-
807- // if let Some(lowest_priority_entry) = batch_state_lock.batch_queue.pop() {
808- // send_message(
809- // lowest_priority_entry.0.messaging_sink.unwrap(),
810- // SubmitProofResponseMessage::BatchQueueLimitExceededError,
811- // )
812- // .await;
813- // }
804+ // Check if the new proof have more priority than the lowest pirority entry
805+ if let Some ( ( _, lowest_priority_entry_priority) ) = batch_state_lock. batch_queue . peek ( ) {
806+ if * lowest_priority_entry_priority
807+ > BatchQueueEntryPriority :: new (
808+ nonced_verification_data. max_fee ,
809+ nonced_verification_data. nonce ,
810+ )
811+ {
812+ let ( removed_entry, _) = batch_state_lock. batch_queue . pop ( ) . unwrap ( ) ;
813+ info ! (
814+ "Removing proof from entry. Sender {}, Nonce {}." ,
815+ removed_entry. sender, removed_entry. nonced_verification_data. nonce
816+ ) ;
817+
818+ batch_state_lock. remove_entry_from_user_state ( & removed_entry) ;
819+ send_message (
820+ removed_entry. messaging_sink . unwrap ( ) ,
821+ SubmitProofResponseMessage :: BatchQueueLimitExceededError ,
822+ )
823+ . await ;
824+ } else {
825+ // Can't add new entry with less priority to the batch queue
826+ info ! ( "Can't add new entry, the batcher queue is full" ) ;
827+ send_message (
828+ ws_conn_sink. clone ( ) ,
829+ SubmitProofResponseMessage :: BatchQueueLimitExceededError ,
830+ )
831+ . await ;
832+ return Ok ( ( ) ) ;
833+ }
834+ }
814835 }
815836
816837 // * ---------------------------------------------------------------------*
0 commit comments