@@ -39,10 +39,10 @@ pub async fn send_messages(
3939 max_fees : & [ U256 ] ,
4040 wallet : Wallet < SigningKey > ,
4141 mut nonce : U256 ,
42- ) -> Result < Vec < NoncedVerificationData > , SubmitError > {
42+ ) -> Vec < Result < NoncedVerificationData , SubmitError > > {
4343 let chain_id = U256 :: from ( wallet. chain_id ( ) ) ;
4444 let mut ws_write = ws_write. lock ( ) . await ;
45- let mut sent_verification_data: Vec < NoncedVerificationData > = Vec :: new ( ) ;
45+ let mut sent_verification_data: Vec < Result < NoncedVerificationData , SubmitError > > = Vec :: new ( ) ;
4646
4747 for ( idx, verification_data) in verification_data. iter ( ) . enumerate ( ) {
4848 // Build each message to send
@@ -56,29 +56,37 @@ pub async fn send_messages(
5656
5757 nonce += U256 :: one ( ) ;
5858 let msg = ClientMessage :: new ( verification_data. clone ( ) , wallet. clone ( ) ) . await ;
59- let msg_bin = cbor_serialize ( & msg) . map_err ( SubmitError :: SerializationError ) ?;
60-
59+ let msg_bin = match cbor_serialize ( & msg) . map_err ( SubmitError :: SerializationError ) {
60+ Ok ( bin) => bin,
61+ Err ( e) => {
62+ error ! ( "Error while serializing message: {:?}" , e) ;
63+ sent_verification_data. push ( Err ( e) ) ;
64+ return sent_verification_data;
65+ }
66+ } ;
67+
6168 // Send the message
62- ws_write
63- . send ( Message :: Binary ( msg_bin. clone ( ) ) )
64- . await
65- . map_err ( SubmitError :: WebSocketConnectionError ) ?;
69+ if let Err ( e) = ws_write. send ( Message :: Binary ( msg_bin. clone ( ) ) ) . await {
70+ error ! ( "Error while sending message: {:?}" , e) ;
71+ sent_verification_data. push ( Err ( SubmitError :: WebSocketConnectionError ( e) ) ) ;
72+ return sent_verification_data;
73+ }
6674
6775 debug ! ( "{:?} Message sent" , idx) ;
6876
6977 // Save the verification data commitment to read its response later
70- sent_verification_data. push ( verification_data) ;
78+ sent_verification_data. push ( Ok ( verification_data) ) ;
7179 }
7280
7381 info ! ( "All proofs sent" ) ;
7482 // This vector is reversed so that while responses are received, removing from the end is cheaper.
75- let sent_verification_data_rev: Vec < NoncedVerificationData > =
83+ let sent_verification_data_rev: Vec < Result < NoncedVerificationData , SubmitError > > =
7684 sent_verification_data
7785 . into_iter ( )
7886 . map ( |vd| vd. into ( ) )
7987 . rev ( )
8088 . collect ( ) ;
81- Ok ( sent_verification_data_rev)
89+ sent_verification_data_rev
8290}
8391
8492// Receives the array of proofs sent
@@ -87,11 +95,11 @@ pub async fn send_messages(
8795// finishes when the last proof sent receives its response
8896pub async fn receive (
8997 response_stream : Arc < Mutex < ResponseStream > > ,
90- mut sent_verification_data_rev : Vec < NoncedVerificationData > ,
91- ) -> Result < Vec < AlignedVerificationData > , SubmitError > {
98+ mut sent_verification_data_rev : Vec < Result < NoncedVerificationData , SubmitError > > ,
99+ ) -> Vec < Result < AlignedVerificationData , SubmitError > > {
92100 // Responses are filtered to only admit binary or close messages.
93101 let mut response_stream = response_stream. lock ( ) . await ;
94- let mut aligned_submitted_data: Vec < AlignedVerificationData > = Vec :: new ( ) ;
102+ let mut aligned_submitted_data: Vec < Result < AlignedVerificationData , SubmitError > > = Vec :: new ( ) ;
95103 let last_proof_nonce = get_biggest_nonce ( & sent_verification_data_rev) ;
96104
97105 // read from WS
@@ -100,35 +108,51 @@ pub async fn receive(
100108 if let Message :: Close ( close_frame) = msg {
101109 warn ! ( "Unexpected WS close" ) ;
102110 if let Some ( close_msg) = close_frame {
103- return Err ( SubmitError :: WebSocketClosedUnexpectedlyError (
104- close_msg. to_owned ( ) ,
105- ) ) ;
111+ aligned_submitted_data. push ( Err ( SubmitError :: WebSocketClosedUnexpectedlyError ( close_msg. to_owned ( ) ) ) ) ;
112+ break ;
106113 }
107- return Err ( SubmitError :: GenericError (
108- "Connection was closed before receive() processed all sent messages " . to_string ( ) ,
109- ) ) ;
114+ aligned_submitted_data. push ( Err ( SubmitError :: GenericError ( "Connection was closed before receive() processed all sent messages " . to_string ( ) ) ) ) ;
115+ break ;
110116 }
111117
112- let batch_inclusion_data_message = handle_batcher_response ( msg) . await ? ;
118+ // first error msg from batcher will drop the rest of the messages in the burst
113119
114- let related_verification_data = match_batcher_response_with_stored_verification_data (
115- & batch_inclusion_data_message,
116- & mut sent_verification_data_rev,
117- ) ?;
120+ let batch_inclusion_data_message = match handle_batcher_response ( msg) . await {
121+ Ok ( data) => data,
122+ Err ( e) => {
123+ warn ! ( "Error while handling batcher response: {:?}" , e) ;
124+ aligned_submitted_data. push ( Err ( e) ) ;
125+ break ;
126+ }
127+ } ;
118128
119- let aligned_verification_data =
120- process_batcher_response ( & batch_inclusion_data_message, & related_verification_data) ?;
129+ let related_verification_data = match match_batcher_response_with_stored_verification_data ( & batch_inclusion_data_message, & mut sent_verification_data_rev) {
130+ Ok ( data) => data,
131+ Err ( e) => {
132+ warn ! ( "Error while matching batcher response with sent data: {:?}" , e) ;
133+ aligned_submitted_data. push ( Err ( e) ) ;
134+ break ;
135+ }
136+ } ;
121137
122- aligned_submitted_data. push ( aligned_verification_data) ;
138+ let aligned_verification_data = match process_batcher_response ( & batch_inclusion_data_message, & related_verification_data) {
139+ Ok ( data) => data,
140+ Err ( e) => {
141+ warn ! ( "Error while processing batcher response: {:?}" , e) ;
142+ aligned_submitted_data. push ( Err ( e) ) ;
143+ break ;
144+ }
145+ } ;
146+
147+ aligned_submitted_data. push ( Ok ( aligned_verification_data) ) ;
123148 debug ! ( "Message response handled successfully" ) ;
124149
125150 if batch_inclusion_data_message. user_nonce == last_proof_nonce {
126151 break ;
127152 }
128153 }
129154
130- debug ! ( "All proof responses handled successfully" ) ;
131- Ok ( aligned_submitted_data)
155+ aligned_submitted_data
132156}
133157
134158async fn handle_batcher_response ( msg : Message ) -> Result < BatchInclusionData , SubmitError > {
@@ -226,20 +250,23 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
226250// This is used to verify the proof you sent was indeed included in the batch
227251fn match_batcher_response_with_stored_verification_data (
228252 batch_inclusion_data : & BatchInclusionData ,
229- sent_verification_data_rev : & mut Vec < NoncedVerificationData > ,
253+ sent_verification_data_rev : & mut Vec < Result < NoncedVerificationData , SubmitError > > ,
230254) -> Result < VerificationDataCommitment , SubmitError > {
231255 debug ! ( "Matching verification data with batcher response ..." ) ;
232256 let mut index = None ;
233257 for ( i, sent_nonced_verification_data) in sent_verification_data_rev. iter_mut ( ) . enumerate ( ) . rev ( ) { // iterate in reverse since the last element is the most probable to match
234- if sent_nonced_verification_data. nonce == batch_inclusion_data. user_nonce {
235- debug ! ( "local nonced verification data matched with batcher response" ) ;
236- index = Some ( i) ;
237- break ;
258+ if let Ok ( sent_nonced_verification_data) = sent_nonced_verification_data {
259+ if sent_nonced_verification_data. nonce == batch_inclusion_data. user_nonce {
260+ debug ! ( "local nonced verification data matched with batcher response" ) ;
261+ index = Some ( i) ;
262+ break ;
263+ }
238264 }
239265 }
240266
267+ // cant remove an element while iterating, so we remove it here
241268 if let Some ( i) = index {
242- let verification_data = sent_verification_data_rev. remove ( i) ;
269+ let verification_data = sent_verification_data_rev. remove ( i) . unwrap ( ) ;
243270 return Ok ( verification_data. verification_data . clone ( ) . into ( ) ) ;
244271 }
245272
@@ -249,11 +276,13 @@ fn match_batcher_response_with_stored_verification_data(
249276// Returns the biggest nonce from the sent verification data
250277// Used to know which is the last proof sent to the Batcher,
251278// to know when to stop reading the WS for responses
252- fn get_biggest_nonce ( sent_verification_data : & [ NoncedVerificationData ] ) -> U256 {
279+ fn get_biggest_nonce ( sent_verification_data : & [ Result < NoncedVerificationData , SubmitError > ] ) -> U256 {
253280 let mut biggest_nonce = U256 :: zero ( ) ;
254281 for verification_data in sent_verification_data. iter ( ) {
255- if verification_data. nonce > biggest_nonce {
256- biggest_nonce = verification_data. nonce ;
282+ if let Ok ( verification_data) = verification_data {
283+ if verification_data. nonce > biggest_nonce {
284+ biggest_nonce = verification_data. nonce ;
285+ }
257286 }
258287 }
259288 biggest_nonce
0 commit comments