Skip to content

Commit dc670d7

Browse files
committed
refactor: revert changes on handle_get_nonce_for_address_msg
1 parent a6941d5 commit dc670d7

1 file changed

Lines changed: 56 additions & 39 deletions

File tree

  • batcher/aligned-batcher/src

batcher/aligned-batcher/src/lib.rs

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,28 @@ impl Batcher {
496496

497497
async fn handle_get_nonce_for_address_msg(
498498
self: Arc<Self>,
499-
address: Address,
499+
mut address: Address,
500500
ws_conn_sink: WsMessageSink,
501501
) -> Result<(), Error> {
502+
if !self.has_to_pay(&address) {
503+
info!("Handling nonpaying message");
504+
let Some(non_paying_config) = self.non_paying_config.as_ref() else {
505+
warn!(
506+
"There isn't a non-paying configuration loaded. This message will be ignored"
507+
);
508+
send_message(
509+
ws_conn_sink.clone(),
510+
GetNonceResponseMessage::InvalidRequest(
511+
"There isn't a non-paying configuration loaded.".to_string(),
512+
),
513+
)
514+
.await;
515+
return Ok(());
516+
};
517+
let replacement_addr = non_paying_config.replacement.address();
518+
address = replacement_addr;
519+
}
520+
502521
let cached_user_nonce = {
503522
let batch_state_lock = self.batch_state.lock().await;
504523
batch_state_lock.get_user_nonce(&address).await
@@ -740,48 +759,46 @@ impl Batcher {
740759
return Ok(());
741760
}
742761

743-
if self.has_to_pay(&addr_in_msg) {
744-
let cached_user_nonce = batch_state_lock.get_user_nonce(&addr).await;
762+
let cached_user_nonce = batch_state_lock.get_user_nonce(&addr).await;
745763

746-
let Some(expected_nonce) = cached_user_nonce else {
747-
error!("Failed to get cached user nonce: User not found in user states, but it should have been already inserted");
748-
std::mem::drop(batch_state_lock);
749-
send_message(
750-
ws_conn_sink.clone(),
751-
SubmitProofResponseMessage::AddToBatchError,
752-
)
753-
.await;
754-
self.metrics.user_error(&["batcher_state_error", ""]);
755-
return Ok(());
756-
};
764+
let Some(expected_nonce) = cached_user_nonce else {
765+
error!("Failed to get cached user nonce: User not found in user states, but it should have been already inserted");
766+
std::mem::drop(batch_state_lock);
767+
send_message(
768+
ws_conn_sink.clone(),
769+
SubmitProofResponseMessage::AddToBatchError,
770+
)
771+
.await;
772+
self.metrics.user_error(&["batcher_state_error", ""]);
773+
return Ok(());
774+
};
757775

758-
if expected_nonce < msg_nonce {
759-
std::mem::drop(batch_state_lock);
760-
warn!("Invalid nonce for address {addr}, expected nonce: {expected_nonce:?}, received nonce: {msg_nonce:?}");
761-
send_message(
762-
ws_conn_sink.clone(),
763-
SubmitProofResponseMessage::InvalidNonce,
764-
)
765-
.await;
766-
self.metrics.user_error(&["invalid_nonce", ""]);
767-
return Ok(());
768-
}
776+
if expected_nonce < msg_nonce {
777+
std::mem::drop(batch_state_lock);
778+
warn!("Invalid nonce for address {addr}, expected nonce: {expected_nonce:?}, received nonce: {msg_nonce:?}");
779+
send_message(
780+
ws_conn_sink.clone(),
781+
SubmitProofResponseMessage::InvalidNonce,
782+
)
783+
.await;
784+
self.metrics.user_error(&["invalid_nonce", ""]);
785+
return Ok(());
786+
}
769787

770-
// In this case, the message might be a replacement one. If it is valid,
771-
// we replace the old entry with the new from the replacement message.
772-
if expected_nonce > msg_nonce {
773-
info!("Possible replacement message received: Expected nonce {expected_nonce:?} - message nonce: {msg_nonce:?}");
774-
self.handle_replacement_message(
775-
batch_state_lock,
776-
nonced_verification_data,
777-
ws_conn_sink.clone(),
778-
client_msg.signature,
779-
addr,
780-
)
781-
.await;
788+
// In this case, the message might be a replacement one. If it is valid,
789+
// we replace the old entry with the new from the replacement message.
790+
if expected_nonce > msg_nonce {
791+
info!("Possible replacement message received: Expected nonce {expected_nonce:?} - message nonce: {msg_nonce:?}");
792+
self.handle_replacement_message(
793+
batch_state_lock,
794+
nonced_verification_data,
795+
ws_conn_sink.clone(),
796+
client_msg.signature,
797+
addr,
798+
)
799+
.await;
782800

783-
return Ok(());
784-
}
801+
return Ok(());
785802
}
786803

787804
// We check this after replacement logic because if user wants to replace a proof, their

0 commit comments

Comments
 (0)