Skip to content

Commit 2f8df91

Browse files
committed
Simplify functions
1 parent eeb557d commit 2f8df91

4 files changed

Lines changed: 41 additions & 57 deletions

File tree

batcher/aligned-batcher/src/lib.rs

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use types::batch_state::BatchState;
1717
use types::user_state::UserState;
1818

1919
use batch_queue::calculate_batch_size;
20-
use std::cmp::Ordering;
2120
use std::collections::HashMap;
2221
use std::env;
2322
use 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(());

batcher/aligned-batcher/src/types/batch_state.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::collections::{hash_map::Entry, HashMap};
22

3-
use crate::BatchQueueEntryPriority;
4-
53
use super::{
64
batch_queue::{BatchQueue, BatchQueueEntry},
75
user_state::UserState,
@@ -256,10 +254,4 @@ impl BatchState {
256254
pub(crate) fn is_queue_full(&self) -> bool {
257255
self.batch_queue.len() >= self.max_size
258256
}
259-
260-
pub(crate) fn lowest_entry_priority(&self) -> Option<BatchQueueEntryPriority> {
261-
self.batch_queue
262-
.peek()
263-
.map(|(_, priority_entry)| priority_entry.clone())
264-
}
265257
}

batcher/aligned-sdk/src/communication/messaging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
265265
);
266266
Err(SubmitError::GenericError(e))
267267
}
268-
Ok(SubmitProofResponseMessage::BatchQueueLimitExceededError) => {
268+
Ok(SubmitProofResponseMessage::UnderpricedProof) => {
269269
error!("Batcher responded with error: queue limit has been exceeded. Funds have not been spent.");
270270
Err(SubmitError::BatchQueueLimitExceededError)
271271
}

batcher/aligned-sdk/src/core/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ pub enum SubmitProofResponseMessage {
399399
AddToBatchError,
400400
EthRpcError,
401401
InvalidPaymentServiceAddress(Address, Address),
402-
BatchQueueLimitExceededError,
402+
UnderpricedProof,
403403
}
404404

405405
#[derive(Debug, Clone, Serialize, Deserialize)]

0 commit comments

Comments
 (0)