11use ethers:: signers:: Signer ;
22use ethers:: types:: Address ;
33use futures_util:: { stream:: SplitStream , SinkExt , StreamExt } ;
4- use log:: { debug, error, info} ;
4+ use log:: { debug, error, info, warn } ;
55use std:: sync:: Arc ;
66use tokio:: { net:: TcpStream , sync:: Mutex } ;
77
@@ -67,11 +67,18 @@ pub async fn send_messages(
6767 debug ! ( "{:?} Message sent" , idx) ;
6868
6969 // Save the verification data commitment to read its response later
70- sent_verification_data. push ( verification_data) ;
70+ sent_verification_data. push ( verification_data) ;
7171 }
7272
7373 info ! ( "All messages sent" ) ;
74- Ok ( sent_verification_data)
74+ // This vector is reversed so that while responses are received, removing from the end is cheaper.
75+ let sent_verification_data_rev: Vec < NoncedVerificationData > =
76+ sent_verification_data
77+ . into_iter ( )
78+ . map ( |vd| vd. into ( ) )
79+ . rev ( )
80+ . collect ( ) ;
81+ Ok ( sent_verification_data_rev)
7582}
7683
7784// Receives the array of proofs sent
@@ -80,17 +87,18 @@ pub async fn send_messages(
8087// finishes when the last proof sent receives its response
8188pub async fn receive (
8289 response_stream : Arc < Mutex < ResponseStream > > ,
83- mut sent_verification_data : Vec < NoncedVerificationData > ,
90+ mut sent_verification_data_rev : Vec < NoncedVerificationData > ,
8491) -> Result < Vec < AlignedVerificationData > , SubmitError > {
8592 // Responses are filtered to only admit binary or close messages.
8693 let mut response_stream = response_stream. lock ( ) . await ;
8794 let mut aligned_submitted_data: Vec < AlignedVerificationData > = Vec :: new ( ) ;
88- let last_proof_nonce = get_biggest_nonce ( & sent_verification_data ) ;
95+ let last_proof_nonce = get_biggest_nonce ( & sent_verification_data_rev ) ;
8996
9097 // read from WS
9198 while let Some ( Ok ( msg) ) = response_stream. next ( ) . await {
9299 // unexpected WS close:
93100 if let Message :: Close ( close_frame) = msg {
101+ warn ! ( "Unexpected WS close" ) ;
94102 if let Some ( close_msg) = close_frame {
95103 return Err ( SubmitError :: WebSocketClosedUnexpectedlyError (
96104 close_msg. to_owned ( ) ,
@@ -105,7 +113,7 @@ pub async fn receive(
105113
106114 let related_verification_data = match_batcher_response_with_stored_verification_data (
107115 & batch_inclusion_data_message,
108- & mut sent_verification_data ,
116+ & mut sent_verification_data_rev ,
109117 ) ?;
110118
111119 let aligned_verification_data =
@@ -218,11 +226,11 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
218226// This is used to verify the proof you sent was indeed included in the batch
219227fn match_batcher_response_with_stored_verification_data (
220228 batch_inclusion_data : & BatchInclusionData ,
221- sent_verification_data : & mut Vec < NoncedVerificationData > ,
229+ sent_verification_data_rev : & mut Vec < NoncedVerificationData > ,
222230) -> Result < VerificationDataCommitment , SubmitError > {
223231 debug ! ( "Matching verification data with batcher response ..." ) ;
224232 let mut index = None ;
225- for ( i, sent_nonced_verification_data) in sent_verification_data . iter_mut ( ) . enumerate ( ) {
233+ 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
226234 if sent_nonced_verification_data. nonce == batch_inclusion_data. user_nonce {
227235 debug ! ( "local nonced verification data matched with batcher response" ) ;
228236 index = Some ( i) ;
@@ -231,7 +239,7 @@ fn match_batcher_response_with_stored_verification_data(
231239 }
232240
233241 if let Some ( i) = index {
234- let verification_data = sent_verification_data . swap_remove ( i) ; //TODO maybe only remove?
242+ let verification_data = sent_verification_data_rev . remove ( i) ;
235243 return Ok ( verification_data. verification_data . clone ( ) . into ( ) ) ;
236244 }
237245
0 commit comments