Skip to content

Commit ef8c9a3

Browse files
committed
refactor: move user update to batch state
1 parent 44c36e6 commit ef8c9a3

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

crates/batcher/src/lib.rs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ impl Batcher {
620620

621621
// if this is locked, then it means that the a batch is being built
622622
// so we need to stop the processing
623-
self.batch_building_mutex.lock().await;
623+
let _ = self.batch_building_mutex.lock().await;
624624

625625
// * ---------------------------------------------------*
626626
// * Perform validations over the message *
@@ -910,11 +910,8 @@ impl Batcher {
910910
// * ---------------------------------------------------------------------*
911911
// * Add message data into the queue and update user state *
912912
// * ---------------------------------------------------------------------*
913-
914-
let batch_state_lock = self.batch_state.lock().await;
915913
if let Err(e) = self
916914
.add_to_batch(
917-
batch_state_lock,
918915
nonced_verification_data,
919916
ws_conn_sink.clone(),
920917
signature,
@@ -928,6 +925,21 @@ impl Batcher {
928925
return Ok(());
929926
};
930927

928+
if let Err(_) = self
929+
.batch_state
930+
.lock()
931+
.await
932+
.update_user_after_adding_proof(addr, msg_nonce, msg_max_fee)
933+
.await
934+
{
935+
send_message(
936+
ws_conn_sink.clone(),
937+
SubmitProofResponseMessage::AddToBatchError,
938+
)
939+
.await;
940+
return Ok(());
941+
}
942+
931943
info!("Verification data message handled");
932944
Ok(())
933945
}
@@ -1137,7 +1149,6 @@ impl Batcher {
11371149
/// Adds verification data to the current batch queue.
11381150
async fn add_to_batch(
11391151
&self,
1140-
mut batch_state_lock: MutexGuard<'_, BatchState>,
11411152
verification_data: NoncedVerificationData,
11421153
ws_conn_sink: WsMessageSink,
11431154
proof_submitter_sig: Signature,
@@ -1149,6 +1160,7 @@ impl Batcher {
11491160

11501161
let max_fee = verification_data.max_fee;
11511162
let nonce = verification_data.nonce;
1163+
let mut batch_state_lock = self.batch_state.lock().await;
11521164
batch_state_lock.batch_queue.push(
11531165
BatchQueueEntry::new(
11541166
verification_data,
@@ -1168,46 +1180,6 @@ impl Batcher {
11681180

11691181
info!("Current batch queue length: {}", queue_len);
11701182

1171-
let Some(user_proof_count) = batch_state_lock
1172-
.get_user_proof_count(&proof_submitter_addr)
1173-
.await
1174-
else {
1175-
error!("User state of address {proof_submitter_addr} was not found when trying to update user state. This user state should have been present");
1176-
std::mem::drop(batch_state_lock);
1177-
return Err(BatcherError::AddressNotFoundInUserStates(
1178-
proof_submitter_addr,
1179-
));
1180-
};
1181-
1182-
let Some(current_total_fees_in_queue) = batch_state_lock
1183-
.get_user_total_fees_in_queue(&proof_submitter_addr)
1184-
.await
1185-
else {
1186-
error!("User state of address {proof_submitter_addr} was not found when trying to update user state. This user state should have been present");
1187-
std::mem::drop(batch_state_lock);
1188-
return Err(BatcherError::AddressNotFoundInUserStates(
1189-
proof_submitter_addr,
1190-
));
1191-
};
1192-
1193-
// User state is updated
1194-
if batch_state_lock
1195-
.update_user_state(
1196-
&proof_submitter_addr,
1197-
nonce + U256::one(),
1198-
max_fee,
1199-
user_proof_count + 1,
1200-
current_total_fees_in_queue + max_fee,
1201-
)
1202-
.is_none()
1203-
{
1204-
error!("User state of address {proof_submitter_addr} was not found when trying to update user state. This user state should have been present");
1205-
std::mem::drop(batch_state_lock);
1206-
return Err(BatcherError::AddressNotFoundInUserStates(
1207-
proof_submitter_addr,
1208-
));
1209-
};
1210-
12111183
Ok(())
12121184
}
12131185

@@ -1231,7 +1203,7 @@ impl Batcher {
12311203
block_number: u64,
12321204
gas_price: U256,
12331205
) -> Option<Vec<BatchQueueEntry>> {
1234-
let batch_building_mutex = self.batch_building_mutex.lock().await;
1206+
let _ = self.batch_building_mutex.lock().await;
12351207
let batch_state_lock = self.batch_state.lock().await;
12361208
// acquire all the user locks to make sure all the ongoing message have been processed
12371209
for user_mutex in self.user_mutexes.lock().await.values() {

crates/batcher/src/types/batch_state.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,38 @@ impl BatchState {
176176
None
177177
}
178178

179+
pub(crate) async fn update_user_after_adding_proof(
180+
&mut self,
181+
addr: Address,
182+
nonce: U256,
183+
max_fee: U256,
184+
) -> Result<(), String> {
185+
let Some(user_proof_count) = self.get_user_proof_count(&addr).await else {
186+
return Err("user proof count".into());
187+
};
188+
189+
let Some(current_total_fees_in_queue) = self.get_user_total_fees_in_queue(&addr).await
190+
else {
191+
return Err("user total fees in queue not found".into());
192+
};
193+
194+
// User state is updated
195+
if self
196+
.update_user_state(
197+
&addr,
198+
nonce + U256::one(),
199+
max_fee,
200+
user_proof_count + 1,
201+
current_total_fees_in_queue + max_fee,
202+
)
203+
.is_none()
204+
{
205+
return Err("user not found".into());
206+
};
207+
208+
Ok(())
209+
}
210+
179211
// LOGIC:
180212

181213
pub(crate) fn calculate_new_user_states_data(&self) -> HashMap<Address, (usize, U256, U256)> {

0 commit comments

Comments
 (0)