Skip to content

Commit ade5648

Browse files
committed
Fmt
1 parent 7cb5982 commit ade5648

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

crates/batcher/src/lib.rs

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

1919
use batch_queue::calculate_batch_size;
20+
use dashmap::DashMap;
2021
use std::collections::HashMap;
2122
use std::env;
2223
use std::net::SocketAddr;
2324
use std::sync::Arc;
2425
use std::time::Duration;
25-
use dashmap::DashMap;
2626

2727
use aligned_sdk::common::constants::{
2828
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF, BATCHER_SUBMISSION_BASE_GAS_COST,
@@ -282,8 +282,11 @@ impl Batcher {
282282
}
283283
}
284284

285-
286-
fn get_user_min_fee_in_batch(&self, addr: &Address, batch_queue: &types::batch_queue::BatchQueue) -> U256 {
285+
fn get_user_min_fee_in_batch(
286+
&self,
287+
addr: &Address,
288+
batch_queue: &types::batch_queue::BatchQueue,
289+
) -> U256 {
287290
batch_queue
288291
.iter()
289292
.filter(|(e, _)| &e.sender == addr)
@@ -292,7 +295,11 @@ impl Batcher {
292295
.unwrap_or(U256::max_value())
293296
}
294297

295-
async fn update_user_state_on_entry_removal(&self, removed_entry: &types::batch_queue::BatchQueueEntry, batch_queue: &types::batch_queue::BatchQueue) -> Option<()> {
298+
async fn update_user_state_on_entry_removal(
299+
&self,
300+
removed_entry: &types::batch_queue::BatchQueueEntry,
301+
batch_queue: &types::batch_queue::BatchQueue,
302+
) -> Option<()> {
296303
let addr = removed_entry.sender;
297304

298305
let new_last_max_fee_limit = match batch_queue
@@ -316,7 +323,10 @@ impl Batcher {
316323
Some(())
317324
}
318325

319-
fn calculate_new_user_states_data(&self, batch_queue: &types::batch_queue::BatchQueue) -> HashMap<Address, (usize, U256, U256)> {
326+
fn calculate_new_user_states_data(
327+
&self,
328+
batch_queue: &types::batch_queue::BatchQueue,
329+
) -> HashMap<Address, (usize, U256, U256)> {
320330
let mut updated_user_states = HashMap::new();
321331
for (entry, _) in batch_queue.iter() {
322332
let addr = entry.sender;
@@ -762,7 +772,6 @@ impl Batcher {
762772
}
763773
}
764774

765-
766775
// We don't need a batch state lock here, since if the user locks its funds
767776
// after the check, some blocks should pass until he can withdraw.
768777
// It is safe to do just do this here.
@@ -772,20 +781,19 @@ impl Batcher {
772781

773782
info!("Handling message, locking user state");
774783

775-
776-
777784
// We acquire the lock first only to query if the user is already present and the lock is dropped.
778785
// If it was not present, then the user nonce is queried to the Aligned contract.
779786
// Lastly, we get a lock of the batch state again and insert the user state if it was still missing.
780787

781788
let is_user_in_state = self.user_states.contains_key(&addr);
782-
789+
783790
if !is_user_in_state {
784791
// We add a dummy user state to grab a lock on the user state
785792
let dummy_user_state = UserState::new(U256::zero());
786-
self.user_states.insert(addr, Arc::new(Mutex::new(dummy_user_state)));
793+
self.user_states
794+
.insert(addr, Arc::new(Mutex::new(dummy_user_state)));
787795
}
788-
796+
789797
let Some(user_state_ref) = self.user_states.get(&addr) else {
790798
error!("This should never happen, user state has previously been inserted if it didn't exist");
791799
send_message(
@@ -818,7 +826,9 @@ impl Batcher {
818826
}
819827
};
820828
let user_state = UserState::new(ethereum_user_nonce);
821-
self.user_states.entry(addr).or_insert(Arc::new(Mutex::new(user_state)));
829+
self.user_states
830+
.entry(addr)
831+
.or_insert(Arc::new(Mutex::new(user_state)));
822832
}
823833

824834
// * ---------------------------------------------------*
@@ -986,7 +996,11 @@ impl Batcher {
986996
removed_entry.nonced_verification_data.nonce
987997
);
988998

989-
self.update_user_state_on_entry_removal(&removed_entry, &batch_state_lock.batch_queue).await;
999+
self.update_user_state_on_entry_removal(
1000+
&removed_entry,
1001+
&batch_state_lock.batch_queue,
1002+
)
1003+
.await;
9901004

9911005
if let Some(removed_entry_ws) = removed_entry.messaging_sink {
9921006
send_message(
@@ -1150,7 +1164,8 @@ impl Batcher {
11501164
);
11511165

11521166
// update max_fee_limit
1153-
let updated_max_fee_limit_in_batch = self.get_user_min_fee_in_batch(&addr, &batch_state_lock.batch_queue);
1167+
let updated_max_fee_limit_in_batch =
1168+
self.get_user_min_fee_in_batch(&addr, &batch_state_lock.batch_queue);
11541169
{
11551170
let user_state = self.user_states.get(&addr);
11561171
match user_state {
@@ -1381,7 +1396,8 @@ impl Batcher {
13811396
// now we calculate the new user_states
13821397
let new_user_states = self.calculate_new_user_states_data(&batch_state_lock.batch_queue);
13831398

1384-
let user_addresses: Vec<Address> = self.user_states.iter().map(|entry| *entry.key()).collect();
1399+
let user_addresses: Vec<Address> =
1400+
self.user_states.iter().map(|entry| *entry.key()).collect();
13851401
let default_value = (0, U256::MAX, U256::zero());
13861402
for addr in user_addresses.iter() {
13871403
let (proof_count, max_fee_limit, total_fees_in_queue) =
@@ -1562,8 +1578,10 @@ impl Batcher {
15621578
batch_state_lock.batch_queue.clear();
15631579
self.user_states.clear();
15641580
let nonpaying_user_state = UserState::new(nonpaying_replacement_addr_nonce);
1565-
self.user_states
1566-
.insert(nonpaying_replacement_addr, Arc::new(Mutex::new(nonpaying_user_state)));
1581+
self.user_states.insert(
1582+
nonpaying_replacement_addr,
1583+
Arc::new(Mutex::new(nonpaying_user_state)),
1584+
);
15671585

15681586
self.metrics.update_queue_metrics(0, 0);
15691587
}

crates/batcher/src/types/batch_state.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use super::{
2-
batch_queue::{BatchQueue, BatchQueueEntry},
3-
};
1+
use super::batch_queue::{BatchQueue, BatchQueueEntry};
42
use ethers::types::{Address, U256};
53
use log::debug;
64

@@ -19,7 +17,6 @@ impl BatchState {
1917
}
2018
}
2119

22-
2320
// GETTERS:
2421

2522
pub(crate) fn get_entry(&self, sender: Address, nonce: U256) -> Option<&BatchQueueEntry> {
@@ -29,7 +26,6 @@ impl BatchState {
2926
.find(|entry| entry.sender == sender && entry.nonced_verification_data.nonce == nonce)
3027
}
3128

32-
3329
pub(crate) fn get_user_min_fee_in_batch(&self, addr: &Address) -> U256 {
3430
self.batch_queue
3531
.iter()
@@ -39,10 +35,8 @@ impl BatchState {
3935
.unwrap_or(U256::max_value())
4036
}
4137

42-
4338
// LOGIC:
4439

45-
4640
/// Checks if the entry is valid
4741
/// An entry is valid if there is no entry with the same sender, lower nonce and a lower fee
4842
pub(crate) fn replacement_entry_is_valid(
@@ -66,7 +60,6 @@ impl BatchState {
6660
})
6761
}
6862

69-
7063
pub(crate) fn is_queue_full(&self) -> bool {
7164
self.batch_queue.len() >= self.max_size
7265
}

0 commit comments

Comments
 (0)