@@ -17,7 +17,6 @@ use types::batch_state::BatchState;
1717use types:: user_state:: UserState ;
1818
1919use batch_queue:: calculate_batch_size;
20- use std:: cmp:: Ordering ;
2120use std:: collections:: HashMap ;
2221use std:: env;
2322use std:: net:: SocketAddr ;
@@ -801,57 +800,50 @@ impl Batcher {
801800 // * ---------------------------------------------------------------------*
802801
803802 if batch_state_lock. is_queue_full ( ) {
804- info ! ( "Batch queue is full. Evaluating if the incoming proof can replace a lower-priority entry." ) ;
803+ debug ! ( "Batch queue is full. Evaluating if the incoming proof can replace a lower-priority entry." ) ;
805804
806- let msg_entry_priority = BatchQueueEntryPriority :: new (
807- nonced_verification_data. max_fee ,
808- nonced_verification_data. nonce ,
809- ) ;
805+ // This cannot panic, if the batch queue is full it has at least one item
806+ let ( lowest_priority_entry, _) = batch_state_lock. batch_queue . peek ( ) . expect ( "Batch queue was expected to be full, but somehow no item was inside" ) ;
810807
811- if let Some ( lowest_entry_priority) = batch_state_lock. lowest_entry_priority ( ) {
812- // If the new proof has more priority than the lowest one in the queue, discard the latter one and push the new one
813- if lowest_entry_priority. cmp ( & msg_entry_priority) == Ordering :: Greater {
814- let Some ( ( removed_entry, _) ) = batch_state_lock. batch_queue . pop ( ) else {
815- warn ! ( "Failed to remove lowest-priority proof despite queue being full." ) ;
816- std:: mem:: drop ( batch_state_lock) ;
817- send_message (
818- ws_conn_sink. clone ( ) ,
819- SubmitProofResponseMessage :: BatchQueueLimitExceededError ,
820- )
821- . await ;
822- return Ok ( ( ) ) ;
823- } ;
824-
825- info ! (
826- "Incoming proof (nonce: {}, fee: {}) has higher priority. Replacing lowest priority proof from sender {} with nonce {}." ,
827- nonced_verification_data. nonce,
828- nonced_verification_data. max_fee,
829- removed_entry. sender,
830- removed_entry. nonced_verification_data. nonce
831- ) ;
808+ let lowest_fee_in_queue = lowest_priority_entry. nonced_verification_data . max_fee ;
832809
833- batch_state_lock. update_user_state_on_entry_removal ( & removed_entry) ;
834- if let Some ( removed_entry_ws) = removed_entry. messaging_sink {
835- send_message (
836- removed_entry_ws,
837- SubmitProofResponseMessage :: BatchQueueLimitExceededError ,
838- )
839- . await ;
840- } ;
841- } else {
842- warn ! (
843- "Incoming proof (nonce: {}, fee: {}) has lower priority than all entries in the full queue. Rejecting submission." ,
844- nonced_verification_data. nonce,
845- nonced_verification_data. max_fee
846- ) ;
847- std:: mem:: drop ( batch_state_lock) ;
810+ let new_proof_fee = nonced_verification_data. max_fee ;
811+
812+ if new_proof_fee > lowest_fee_in_queue {
813+
814+ // This cannot panic, if the batch queue is full it has at least one item
815+ let ( removed_entry, _) = batch_state_lock. batch_queue . pop ( ) . expect ( "Batch queue was expected to be full, but somehow no item was inside" ) ;
816+
817+ info ! (
818+ "Incoming proof (nonce: {}, fee: {}) has higher fee. Replacing lowest fee proof from sender {} with nonce {}." ,
819+ nonced_verification_data. nonce,
820+ nonced_verification_data. max_fee,
821+ removed_entry. sender,
822+ removed_entry. nonced_verification_data. nonce
823+ ) ;
824+
825+ batch_state_lock. update_user_state_on_entry_removal ( & removed_entry) ;
826+
827+ if let Some ( removed_entry_ws) = removed_entry. messaging_sink {
848828 send_message (
849- ws_conn_sink . clone ( ) ,
850- SubmitProofResponseMessage :: BatchQueueLimitExceededError ,
829+ removed_entry_ws ,
830+ SubmitProofResponseMessage :: UnderpricedProof ,
851831 )
852832 . await ;
853- return Ok ( ( ) ) ;
854- }
833+ } ;
834+ } else {
835+ info ! (
836+ "Incoming proof (nonce: {}, fee: {}) has lower priority than all entries in the full queue. Rejecting submission." ,
837+ nonced_verification_data. nonce,
838+ nonced_verification_data. max_fee
839+ ) ;
840+ std:: mem:: drop ( batch_state_lock) ;
841+ send_message (
842+ ws_conn_sink. clone ( ) ,
843+ SubmitProofResponseMessage :: UnderpricedProof ,
844+ )
845+ . await ;
846+ return Ok ( ( ) ) ;
855847 }
856848 }
857849
@@ -1792,7 +1784,7 @@ impl Batcher {
17921784 error ! ( "Can't add new entry, the batcher queue is full" ) ;
17931785 send_message (
17941786 ws_sink. clone ( ) ,
1795- SubmitProofResponseMessage :: BatchQueueLimitExceededError ,
1787+ SubmitProofResponseMessage :: UnderpricedProof ,
17961788 )
17971789 . await ;
17981790 return Ok ( ( ) ) ;
0 commit comments