@@ -21,7 +21,6 @@ use lambdaworks_crypto::merkle_tree::merkle::MerkleTree;
2121use log:: { debug, error, info, warn} ;
2222use tokio:: net:: { TcpListener , TcpStream } ;
2323use tokio:: sync:: { Mutex , RwLock } ;
24- use tokio_tungstenite:: tungstenite:: error:: ProtocolError ;
2524use tokio_tungstenite:: tungstenite:: { Error , Message } ;
2625use tokio_tungstenite:: WebSocketStream ;
2726use 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+ }
0 commit comments