Skip to content

Commit c4c864b

Browse files
committed
refactor: entry removal
the function was making lots of assumptions in its usage
1 parent 7b92058 commit c4c864b

2 files changed

Lines changed: 23 additions & 29 deletions

File tree

batcher/aligned-batcher/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ impl Batcher {
829829
removed_entry.nonced_verification_data.nonce
830830
);
831831

832-
batch_state_lock.remove_entry_from_user_state(&removed_entry);
832+
batch_state_lock.update_user_state_on_entry_removal(&removed_entry);
833833
if let Some(removed_entry_ws) = removed_entry.messaging_sink {
834834
send_message(
835835
removed_entry_ws,

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -223,39 +223,33 @@ impl BatchState {
223223
})
224224
}
225225

226-
// removes a proof from a user state
227-
pub(crate) fn remove_entry_from_user_state(&mut self, entry: &BatchQueueEntry) {
228-
let addr = entry.sender;
229-
230-
if entry.nonced_verification_data.nonce == U256::zero() {
231-
self.user_states.remove(&addr);
232-
return;
233-
}
234-
let nonce = entry.nonced_verification_data.nonce - U256::one();
235-
236-
match self
226+
/// Updates or removes a user's state when their latest proof entry is removed from the batch queue.
227+
///
228+
/// If the user has no other proofs remaining in the queue, their state is removed entirely.
229+
/// Otherwise, the user's state is updated to reflect the next most recent entry in the queue.
230+
///
231+
/// Note: The given `removed_entry` must be the most recent (latest or highest nonce) entry for the user in the queue.
232+
pub(crate) fn update_user_state_on_entry_removal(&mut self, removed_entry: &BatchQueueEntry) {
233+
let addr = removed_entry.sender;
234+
235+
let last_max_fee_limit = match self
237236
.batch_queue
238237
.iter()
239-
.map(|(e, _)| e)
240-
.find(|e| e.sender == addr && e.nonced_verification_data.nonce == nonce)
238+
.filter(|(e, _)| e.sender == addr)
239+
.last()
241240
{
242-
Some(last_entry) => {
243-
if let Entry::Occupied(mut user_state) = self.user_states.entry(addr) {
244-
user_state.get_mut().proofs_in_batch -= 1;
245-
user_state.get_mut().nonce -= U256::one();
246-
user_state.get_mut().total_fees_in_queue -= U256::one();
247-
user_state.get_mut().last_max_fee_limit =
248-
last_entry.nonced_verification_data.max_fee;
249-
}
250-
}
241+
Some((last_entry, _)) => last_entry.nonced_verification_data.max_fee,
251242
None => {
252-
if let Entry::Occupied(mut user_state) = self.user_states.entry(addr) {
253-
user_state.get_mut().proofs_in_batch = 0;
254-
user_state.get_mut().nonce -= U256::one();
255-
user_state.get_mut().total_fees_in_queue = U256::zero();
256-
user_state.get_mut().last_max_fee_limit = U256::max_value();
257-
}
243+
self.user_states.remove(&addr);
244+
return;
258245
}
246+
};
247+
248+
if let Entry::Occupied(mut user_state) = self.user_states.entry(addr) {
249+
user_state.get_mut().proofs_in_batch -= 1;
250+
user_state.get_mut().nonce -= U256::one();
251+
user_state.get_mut().total_fees_in_queue -= U256::one();
252+
user_state.get_mut().last_max_fee_limit = last_max_fee_limit;
259253
}
260254
}
261255

0 commit comments

Comments
 (0)