Skip to content

Commit 0083d6f

Browse files
committed
fix: initialize dummy state with correct nonce
1 parent 9b54bfc commit 0083d6f

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

crates/batcher/src/lib.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,26 @@ impl Batcher {
852852
let is_user_in_state = self.user_states.read().await.contains_key(&addr);
853853

854854
if !is_user_in_state {
855+
// If the user state was not present,
856+
// we need to get the nonce from the Ethereum contract
857+
let ethereum_user_nonce = match self.get_user_nonce_from_ethereum(addr).await {
858+
Ok(ethereum_user_nonce) => ethereum_user_nonce,
859+
Err(e) => {
860+
error!(
861+
"Failed to get user nonce from Ethereum for address {addr:?}. Error: {e:?}"
862+
);
863+
send_message(
864+
ws_conn_sink.clone(),
865+
SubmitProofResponseMessage::EthRpcError,
866+
)
867+
.await;
868+
self.metrics.user_error(&["eth_rpc_error", ""]);
869+
return Ok(());
870+
}
871+
};
855872
debug!("User state for address {addr:?} not found, creating a new one");
856873
// We add a dummy user state to grab a lock on the user state
857-
let dummy_user_state = UserState::new(U256::zero());
874+
let dummy_user_state = UserState::new(ethereum_user_nonce);
858875
self.user_states
859876
.write()
860877
.await
@@ -882,27 +899,6 @@ impl Batcher {
882899
return Ok(());
883900
};
884901

885-
// If the user state was not present, we need to get the nonce from the Ethereum contract and update the dummy user state
886-
if !is_user_in_state {
887-
let ethereum_user_nonce = match self.get_user_nonce_from_ethereum(addr).await {
888-
Ok(ethereum_user_nonce) => ethereum_user_nonce,
889-
Err(e) => {
890-
error!(
891-
"Failed to get user nonce from Ethereum for address {addr:?}. Error: {e:?}"
892-
);
893-
send_message(
894-
ws_conn_sink.clone(),
895-
SubmitProofResponseMessage::EthRpcError,
896-
)
897-
.await;
898-
self.metrics.user_error(&["eth_rpc_error", ""]);
899-
return Ok(());
900-
}
901-
};
902-
// Update the dummy user state with the correct nonce
903-
user_state_guard.nonce = ethereum_user_nonce;
904-
}
905-
906902
// * ---------------------------------------------------*
907903
// * Perform validations over user state *
908904
// * ---------------------------------------------------*
@@ -996,6 +992,10 @@ impl Batcher {
996992
.try_batch_lock_with_timeout(self.batch_state.lock())
997993
.await
998994
else {
995+
if !is_user_in_state {
996+
// If the user state was not present before, we remove it since we couldn't process the message
997+
self.user_states.write().await.remove(&addr);
998+
}
999999
send_message(ws_conn_sink.clone(), SubmitProofResponseMessage::ServerBusy).await;
10001000
return Ok(());
10011001
};

0 commit comments

Comments
 (0)