Skip to content

Commit 206098b

Browse files
committed
Add validation and update user_state
1 parent 085d0db commit 206098b

2 files changed

Lines changed: 69 additions & 11 deletions

File tree

batcher/aligned-batcher/src/lib.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
// * ---------------------------------------------------------------------*

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::{
66
};
77
use ethers::types::{Address, U256};
88
use log::debug;
9+
use log::{error, info, warn};
910

1011
pub(crate) struct BatchState {
1112
pub(crate) batch_queue: BatchQueue,
@@ -214,4 +215,40 @@ impl BatchState {
214215
&& entry.nonced_verification_data.max_fee < replacement_max_fee
215216
})
216217
}
218+
219+
// removes a proof from a user state
220+
pub(crate) fn remove_entry_from_user_state(&mut self, entry: &BatchQueueEntry) {
221+
let addr = entry.sender;
222+
223+
if entry.nonced_verification_data.nonce == U256::zero() {
224+
self.user_states.remove(&addr);
225+
return;
226+
}
227+
let nonce = entry.nonced_verification_data.nonce - U256::one();
228+
229+
match self
230+
.batch_queue
231+
.iter()
232+
.map(|(e, _)| e)
233+
.find(|e| e.sender == addr && e.nonced_verification_data.nonce == nonce)
234+
{
235+
Some(last_entry) => {
236+
if let Entry::Occupied(mut user_state) = self.user_states.entry(addr) {
237+
user_state.get_mut().proofs_in_batch -= 1;
238+
user_state.get_mut().nonce -= U256::one();
239+
user_state.get_mut().total_fees_in_queue -= U256::one();
240+
user_state.get_mut().last_max_fee_limit =
241+
last_entry.nonced_verification_data.max_fee;
242+
}
243+
}
244+
None => {
245+
if let Entry::Occupied(mut user_state) = self.user_states.entry(addr) {
246+
user_state.get_mut().proofs_in_batch = 0;
247+
user_state.get_mut().nonce -= U256::one();
248+
user_state.get_mut().total_fees_in_queue = U256::zero();
249+
user_state.get_mut().last_max_fee_limit = U256::max_value();
250+
}
251+
}
252+
}
253+
}
217254
}

0 commit comments

Comments
 (0)