Skip to content

Commit cb5c84e

Browse files
authored
fix(batcher): initialize dummy state with correct nonce (#2057)
1 parent f846b69 commit cb5c84e

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

crates/batcher/src/lib.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,25 @@ impl Batcher {
877877
let is_user_in_state = self.user_states.read().await.contains_key(&addr);
878878

879879
if !is_user_in_state {
880+
// If the user state was not present, we need to get the nonce from the Ethereum contract
881+
let ethereum_user_nonce = match self.get_user_nonce_from_ethereum(addr).await {
882+
Ok(ethereum_user_nonce) => ethereum_user_nonce,
883+
Err(e) => {
884+
error!(
885+
"Failed to get user nonce from Ethereum for address {addr:?}. Error: {e:?}"
886+
);
887+
send_message(
888+
ws_conn_sink.clone(),
889+
SubmitProofResponseMessage::EthRpcError,
890+
)
891+
.await;
892+
self.metrics.user_error(&["eth_rpc_error", ""]);
893+
return Ok(());
894+
}
895+
};
880896
debug!("User state for address {addr:?} not found, creating a new one");
881897
// We add a dummy user state to grab a lock on the user state
882-
let dummy_user_state = UserState::new(U256::zero());
898+
let dummy_user_state = UserState::new(ethereum_user_nonce);
883899
self.user_states
884900
.write()
885901
.await
@@ -907,27 +923,6 @@ impl Batcher {
907923
return Ok(());
908924
};
909925

910-
// If the user state was not present, we need to get the nonce from the Ethereum contract and update the dummy user state
911-
if !is_user_in_state {
912-
let ethereum_user_nonce = match self.get_user_nonce_from_ethereum(addr).await {
913-
Ok(ethereum_user_nonce) => ethereum_user_nonce,
914-
Err(e) => {
915-
error!(
916-
"Failed to get user nonce from Ethereum for address {addr:?}. Error: {e:?}"
917-
);
918-
send_message(
919-
ws_conn_sink.clone(),
920-
SubmitProofResponseMessage::EthRpcError,
921-
)
922-
.await;
923-
self.metrics.user_error(&["eth_rpc_error", ""]);
924-
return Ok(());
925-
}
926-
};
927-
// Update the dummy user state with the correct nonce
928-
user_state_guard.nonce = ethereum_user_nonce;
929-
}
930-
931926
// * ---------------------------------------------------*
932927
// * Perform validations over user state *
933928
// * ---------------------------------------------------*

0 commit comments

Comments
 (0)