Skip to content

Commit 52627ed

Browse files
JuArceNicolasRampoldientropidelictaturosati
authored
feat: send useful error messages in batcher responses to the client (#635)
Co-authored-by: NicolasRampoldi <[email protected]> Co-authored-by: Mariano A. Nicolini <[email protected]> Co-authored-by: Tatu <[email protected]>
1 parent 028a9c1 commit 52627ed

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use lambdaworks_crypto::merkle_tree::merkle::MerkleTree;
2121
use log::{debug, error, info, warn};
2222
use tokio::net::{TcpListener, TcpStream};
2323
use tokio::sync::{Mutex, RwLock};
24-
use tokio_tungstenite::tungstenite::error::ProtocolError;
2524
use tokio_tungstenite::tungstenite::{Error, Message};
2625
use tokio_tungstenite::WebSocketStream;
2726
use types::batch_queue::BatchQueue;
@@ -176,9 +175,6 @@ impl Batcher {
176175
.try_for_each(|msg| self.clone().handle_message(msg, outgoing.clone()))
177176
.await
178177
{
179-
Err(Error::Protocol(ProtocolError::ResetWithoutClosingHandshake)) => {
180-
info!("Client {} reset connection", &addr)
181-
}
182178
Err(e) => error!("Unexpected error: {}", e),
183179
Ok(_) => info!("{} disconnected", &addr),
184180
}
@@ -189,7 +185,7 @@ impl Batcher {
189185
self: Arc<Self>,
190186
message: Message,
191187
ws_conn_sink: Arc<RwLock<SplitSink<WebSocketStream<TcpStream>, Message>>>,
192-
) -> Result<(), tokio_tungstenite::tungstenite::Error> {
188+
) -> Result<(), Error> {
193189
// Deserialize verification data from message
194190
let client_msg: ClientMessage =
195191
serde_json::from_str(message.to_text().expect("Message is not text"))
@@ -216,34 +212,35 @@ impl Batcher {
216212

217213
if user_balance == U256::from(0) {
218214
error!("Insufficient funds for address {:?}", addr);
219-
return Err(tokio_tungstenite::tungstenite::Error::Protocol(
220-
ProtocolError::HandshakeIncomplete,
221-
));
215+
send_error_message(ws_conn_sink.clone(), ResponseMessage::InsufficientBalanceError(addr))
216+
.await;
217+
return Ok(()); // Send error message to the client and return
222218
}
223219

224220
addr
225221
} else {
226222
error!("Signature verification error");
227-
return Err(tokio_tungstenite::tungstenite::Error::Protocol(
228-
ProtocolError::HandshakeIncomplete,
229-
));
223+
send_error_message(ws_conn_sink.clone(), ResponseMessage::SignatureVerificationError())
224+
.await;
225+
return Ok(()); // Send error message to the client and return
230226
};
231227

232228
let verification_data = client_msg.verification_data;
233229
if verification_data.proof.len() <= self.max_proof_size {
234230
// When pre-verification is enabled, batcher will verify proofs for faster feedback with clients
235231
if self.pre_verification_is_enabled && !zk_utils::verify(&verification_data) {
236-
return Err(tokio_tungstenite::tungstenite::Error::Protocol(
237-
ProtocolError::HandshakeIncomplete,
238-
));
232+
error!("Invalid proof detected. Verification failed.");
233+
send_error_message(ws_conn_sink.clone(), ResponseMessage::VerificationError())
234+
.await;
235+
return Ok(()); // Send error message to the client and return
239236
}
240237
self.add_to_batch(verification_data, ws_conn_sink.clone(), submitter_addr)
241238
.await;
242239
} else {
243-
// FIXME(marian): Handle this error correctly
244-
return Err(tokio_tungstenite::tungstenite::Error::Protocol(
245-
ProtocolError::HandshakeIncomplete,
246-
));
240+
error!("Proof is too large");
241+
send_error_message(ws_conn_sink.clone(), ResponseMessage::ProofTooLargeError())
242+
.await;
243+
return Ok(()); // Send error message to the client and return
247244
};
248245

249246
info!("Verification data message handled");
@@ -478,3 +475,19 @@ async fn send_batch_inclusion_data_responses(
478475
})
479476
.await;
480477
}
478+
479+
async fn send_error_message(
480+
ws_conn_sink: Arc<RwLock<SplitSink<WebSocketStream<TcpStream>, Message>>>,
481+
error_message: ResponseMessage,
482+
) {
483+
let serialized_response =
484+
serde_json::to_vec(&error_message).expect("Could not serialize response");
485+
486+
// Send error message
487+
ws_conn_sink
488+
.write()
489+
.await
490+
.send(Message::binary(serialized_response))
491+
.await
492+
.expect("Failed to send error message");
493+
}

batcher/aligned-sdk/src/communication/messaging.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use futures_util::{future, stream::SplitStream, SinkExt, StreamExt, TryStreamExt};
2-
use log::debug;
2+
use log::{debug, error};
33
use std::sync::Arc;
44
use tokio::{net::TcpStream, sync::Mutex};
55

@@ -108,6 +108,21 @@ async fn process_batch_inclusion_data(
108108
.to_string(),
109109
));
110110
}
111+
Ok(ResponseMessage::Error(e)) => {
112+
error!("Batcher responded with error: {}", e);
113+
},
114+
Ok(ResponseMessage::VerificationError()) => {
115+
error!("Invalid proof");
116+
},
117+
Ok(ResponseMessage::ProofTooLargeError()) => {
118+
error!("Proof is too large");
119+
},
120+
Ok(ResponseMessage::InsufficientBalanceError(address)) => {
121+
error!("Insufficient balance for address: {}", address);
122+
},
123+
Ok(ResponseMessage::SignatureVerificationError()) => {
124+
error!("Failed to verify the signature");
125+
},
111126
Err(e) => {
112127
return Err(SubmitError::SerializationError(e));
113128
}

batcher/aligned-sdk/src/core/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ impl AlignedVerificationData {
198198
pub enum ResponseMessage {
199199
BatchInclusionData(BatchInclusionData),
200200
ProtocolVersion(u16),
201+
VerificationError(),
202+
ProofTooLargeError(),
203+
InsufficientBalanceError(Address),
204+
SignatureVerificationError(),
205+
Error(String),
201206
}
202207

203208
#[derive(Debug, Clone)]

batcher/aligned/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ async fn main() -> Result<(), AlignedError> {
291291

292292
if unique_batch_merkle_roots.len() > 1 {
293293
info!("Proofs submitted to aligned. See the batches in the explorer:");
294-
} else {
294+
} else if unique_batch_merkle_roots.len() == 1 {
295295
info!("Proofs submitted to aligned. See the batch in the explorer:");
296296
}
297297

0 commit comments

Comments
 (0)