Skip to content

Commit 08d51b6

Browse files
committed
feat: move verification after the message and replamcents validations
1 parent f8b33ac commit 08d51b6

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

crates/batcher/src/lib.rs

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ impl Batcher {
613613
debug!("Received message with nonce: {msg_nonce:?}");
614614
self.metrics.received_proofs.inc();
615615

616+
// TODO: check if the user is already being attended
617+
// TODO: check if a batch is being built
618+
616619
// * ---------------------------------------------------*
617620
// * Perform validations over the message *
618621
// * ---------------------------------------------------*
@@ -662,46 +665,6 @@ impl Batcher {
662665
nonced_verification_data = aux_verification_data
663666
}
664667

665-
// When pre-verification is enabled, batcher will verify proofs for faster feedback with clients
666-
if self.pre_verification_is_enabled {
667-
let verification_data = &nonced_verification_data.verification_data;
668-
if self
669-
.is_verifier_disabled(verification_data.proving_system)
670-
.await
671-
{
672-
warn!(
673-
"Verifier for proving system {} is disabled, skipping verification",
674-
verification_data.proving_system
675-
);
676-
send_message(
677-
ws_conn_sink.clone(),
678-
SubmitProofResponseMessage::InvalidProof(ProofInvalidReason::DisabledVerifier(
679-
verification_data.proving_system,
680-
)),
681-
)
682-
.await;
683-
self.metrics.user_error(&[
684-
"disabled_verifier",
685-
&format!("{}", verification_data.proving_system),
686-
]);
687-
return Ok(());
688-
}
689-
690-
if !zk_utils::verify(verification_data).await {
691-
error!("Invalid proof detected. Verification failed");
692-
send_message(
693-
ws_conn_sink.clone(),
694-
SubmitProofResponseMessage::InvalidProof(ProofInvalidReason::RejectedProof),
695-
)
696-
.await;
697-
self.metrics.user_error(&[
698-
"rejected_proof",
699-
&format!("{}", verification_data.proving_system),
700-
]);
701-
return Ok(());
702-
}
703-
}
704-
705668
info!("Handling message");
706669

707670
// We don't need a batch state lock here, since if the user locks its funds
@@ -715,11 +678,10 @@ impl Batcher {
715678
// If it was not present, then the user nonce is queried to the Aligned contract.
716679
// Lastly, we get a lock of the batch state again and insert the user state if it was still missing.
717680

718-
let is_user_in_state: bool;
719-
{
681+
let is_user_in_state: bool = {
720682
let batch_state_lock = self.batch_state.lock().await;
721-
is_user_in_state = batch_state_lock.user_states.contains_key(&addr);
722-
}
683+
batch_state_lock.user_states.contains_key(&addr)
684+
};
723685

724686
if !is_user_in_state {
725687
let ethereum_user_nonce = match self.get_user_nonce_from_ethereum(addr).await {
@@ -859,6 +821,8 @@ impl Batcher {
859821
return Ok(());
860822
}
861823

824+
self.verify_proof(&nonced_verification_data);
825+
862826
// * ---------------------------------------------------------------------*
863827
// * Perform validation over batcher queue *
864828
// * ---------------------------------------------------------------------*
@@ -1003,6 +967,9 @@ impl Batcher {
1003967
return;
1004968
}
1005969

970+
// if all went well, verify the proof
971+
self.verify_proof(&nonced_verification_data);
972+
1006973
info!("Replacing message for address {addr} with nonce {nonce} and max fee {replacement_max_fee}");
1007974

1008975
// The replacement entry is built from the old entry and validated for then to be replaced
@@ -2040,4 +2007,41 @@ impl Batcher {
20402007

20412008
true
20422009
}
2010+
2011+
async fn verify_proof(
2012+
&self,
2013+
nonced_verification_data: &NoncedVerificationData,
2014+
) -> Result<(), ProofInvalidReason> {
2015+
if !self.pre_verification_is_enabled {
2016+
return Ok(());
2017+
}
2018+
let verification_data = &nonced_verification_data.verification_data;
2019+
if self
2020+
.is_verifier_disabled(verification_data.proving_system)
2021+
.await
2022+
{
2023+
warn!(
2024+
"Verifier for proving system {} is disabled, skipping verification",
2025+
verification_data.proving_system
2026+
);
2027+
self.metrics.user_error(&[
2028+
"disabled_verifier",
2029+
&format!("{}", verification_data.proving_system),
2030+
]);
2031+
return Err(ProofInvalidReason::DisabledVerifier(
2032+
verification_data.proving_system,
2033+
));
2034+
}
2035+
2036+
if !zk_utils::verify(verification_data).await {
2037+
error!("Invalid proof detected. Verification failed");
2038+
self.metrics.user_error(&[
2039+
"rejected_proof",
2040+
&format!("{}", verification_data.proving_system),
2041+
]);
2042+
return Err(ProofInvalidReason::RejectedProof);
2043+
}
2044+
2045+
Ok(())
2046+
}
20432047
}

0 commit comments

Comments
 (0)