@@ -36,6 +36,8 @@ pub struct Batcher {
3636 current_batch : Mutex < Vec < VerificationData > > ,
3737 max_block_interval : u64 ,
3838 min_batch_size : usize ,
39+ max_proof_size : usize ,
40+ max_batch_size : usize ,
3941 last_uploaded_batch_block : Mutex < u64 > ,
4042}
4143
@@ -78,6 +80,8 @@ impl Batcher {
7880 current_batch : Mutex :: new ( Vec :: new ( ) ) ,
7981 max_block_interval : config. batcher . block_interval ,
8082 min_batch_size : config. batcher . batch_size_interval ,
83+ max_proof_size : config. batcher . max_proof_size ,
84+ max_batch_size : config. batcher . max_batch_size ,
8185 last_uploaded_batch_block : Mutex :: new ( last_uploaded_batch_block) ,
8286 }
8387 }
@@ -155,7 +159,7 @@ impl Batcher {
155159 serde_json:: from_str ( message. to_text ( ) . expect ( "Message is not text" ) )
156160 . expect ( "Failed to deserialize task" ) ;
157161
158- if verification_data. verify ( ) {
162+ if verification_data. proof . len ( ) <= self . max_proof_size && verification_data . verify ( ) {
159163 self . add_to_batch ( verification_data) . await ;
160164 } else {
161165 // FIXME(marian): Handle this error correctly
@@ -207,14 +211,43 @@ impl Batcher {
207211
208212 async fn process_batch_and_update_state ( & self , block_number : u64 ) -> ( Vec < u8 > , [ u8 ; 32 ] ) {
209213 let mut current_batch = self . current_batch . lock ( ) . await ;
210- let batch_commitment = VerificationCommitmentBatch :: from ( & ( * current_batch) ) ;
214+
215+
216+ let mut batch_bytes =
217+ serde_json:: to_vec ( current_batch. as_slice ( ) ) . expect ( "Failed to serialize batch" ) ;
218+
219+ let batch_to_send;
220+ if batch_bytes. len ( ) > self . max_batch_size {
221+ let mut current_batch_end = 0 ; // not inclusive
222+ let mut current_batch_size = 0 ;
223+ for ( i, verification_data) in current_batch. iter ( ) . enumerate ( ) {
224+ let verification_data_bytes = serde_json:: to_vec ( verification_data)
225+ . expect ( "Failed to serialize verification data" ) ;
226+
227+ current_batch_size += verification_data_bytes. len ( ) ;
228+ if current_batch_size > self . max_batch_size {
229+ current_batch_end = i;
230+ break ;
231+ }
232+ }
233+
234+ debug ! ( "Batch size exceeds max batch size, splitting batch at index: {}" , current_batch_end) ;
235+ batch_to_send = current_batch. drain ( ..current_batch_end)
236+ . collect :: < Vec < _ > > ( ) ;
237+
238+ info ! ( "# of Elements remaining for next batch: {}" , current_batch. len( ) ) ;
239+ batch_bytes = serde_json:: to_vec ( & batch_to_send)
240+ . expect ( "Failed to serialize batch" ) ;
241+ } else {
242+ batch_to_send = current_batch. clone ( ) ;
243+ current_batch. clear ( ) ;
244+ }
245+
246+ let batch_commitment = VerificationCommitmentBatch :: from ( & batch_to_send) ;
211247 let batch_merkle_tree: MerkleTree < VerificationCommitmentBatch > =
212248 MerkleTree :: build ( & batch_commitment. 0 ) ;
213- let batch_bytes =
214- serde_json:: to_vec ( current_batch. as_slice ( ) ) . expect ( "Failed to serialize batch" ) ;
215249
216- // update batcher state (clear current batch and update last uploaded batch block)
217- current_batch. clear ( ) ;
250+ // update batcher state (update last uploaded batch block)
218251 * self . last_uploaded_batch_block . lock ( ) . await = block_number;
219252
220253 ( batch_bytes, batch_merkle_tree. root )
0 commit comments