@@ -17,7 +17,8 @@ use crate::{
17
17
pub struct Metadata {
18
18
pub block_heights : RangeInclusive < u32 > ,
19
19
pub known_to_be_optimal : bool ,
20
- pub optimization_attempts : u64 ,
20
+ pub block_num_upper_limit : NonZeroUsize ,
21
+ pub optimization_attempts : usize ,
21
22
pub gas_usage : u64 ,
22
23
pub compressed_data_size : NonZeroUsize ,
23
24
pub uncompressed_data_size : NonZeroUsize ,
@@ -155,7 +156,9 @@ pub struct Bundler<FragmentEncoder> {
155
156
best_valid_proposal : Option < Proposal > ,
156
157
smallest_invalid_proposal : Option < Proposal > ,
157
158
bundle_encoder : bundle:: Encoder ,
158
- attempts : VecDeque < NonZeroUsize > ,
159
+ sizes_to_try : VecDeque < NonZeroUsize > ,
160
+ attempts_made : usize ,
161
+ max_blocks_in_bundle : NonZeroUsize ,
159
162
bundle_id : NonNegative < i32 > ,
160
163
target_num_fragments : NonZeroUsize ,
161
164
}
@@ -180,9 +183,11 @@ impl<T> Bundler<T> {
180
183
best_valid_proposal : None ,
181
184
smallest_invalid_proposal : None ,
182
185
bundle_encoder,
183
- attempts,
186
+ sizes_to_try : attempts,
184
187
bundle_id,
185
188
target_num_fragments,
189
+ attempts_made : 0 ,
190
+ max_blocks_in_bundle : max_blocks,
186
191
}
187
192
}
188
193
@@ -214,8 +219,12 @@ impl<T> Bundler<T> {
214
219
_ => { }
215
220
}
216
221
217
- // Adding more blocks will likely increase the overall bundle size.
218
- self . attempts . retain ( |a| a <= & block_count_failed) ;
222
+ let min_blocks_in_bundle = NonZeroUsize :: try_from ( 1 ) . unwrap ( ) ;
223
+ let max_blocks_in_bundle =
224
+ NonZeroUsize :: try_from ( block_count_failed. get ( ) . saturating_sub ( 1 ) )
225
+ . unwrap_or ( min_blocks_in_bundle) ;
226
+
227
+ self . max_blocks_in_bundle = min ( self . max_blocks_in_bundle , max_blocks_in_bundle) ;
219
228
}
220
229
}
221
230
@@ -234,8 +243,8 @@ impl<T> Bundler<T> {
234
243
) -> Vec < NonEmpty < CompressedFuelBlock > > {
235
244
let mut blocks_for_attempts = vec ! [ ] ;
236
245
237
- while !self . attempts . is_empty ( ) && blocks_for_attempts. len ( ) < num_concurrent. get ( ) {
238
- let block_count = self . attempts . pop_front ( ) . expect ( "not empty" ) ;
246
+ while !self . sizes_to_try . is_empty ( ) && blocks_for_attempts. len ( ) < num_concurrent. get ( ) {
247
+ let block_count = self . sizes_to_try . pop_front ( ) . expect ( "not empty" ) ;
239
248
let blocks = self . blocks_for_new_proposal ( block_count) ;
240
249
blocks_for_attempts. push ( blocks) ;
241
250
}
@@ -272,15 +281,20 @@ where
272
281
T : crate :: block_bundler:: port:: l1:: FragmentEncoder + Send + Sync + Clone + ' static ,
273
282
{
274
283
async fn advance ( & mut self , optimization_runs : NonZeroUsize ) -> Result < bool > {
275
- if self . attempts . is_empty ( ) {
284
+ self . sizes_to_try
285
+ . retain ( |size| size <= & self . max_blocks_in_bundle ) ;
286
+
287
+ if self . sizes_to_try . is_empty ( ) {
276
288
return Ok ( false ) ;
277
289
}
290
+ let proposals = self . analyze ( optimization_runs) . await ?;
291
+ self . attempts_made += proposals. len ( ) ;
278
292
279
- for proposal in self . analyze ( optimization_runs ) . await ? {
293
+ for proposal in proposals {
280
294
self . save_if_best_so_far ( proposal) ;
281
295
}
282
296
283
- Ok ( !self . attempts . is_empty ( ) )
297
+ Ok ( !self . sizes_to_try . is_empty ( ) )
284
298
}
285
299
286
300
async fn finish ( mut self ) -> Result < BundleProposal > {
@@ -299,22 +313,16 @@ where
299
313
. fragment_encoder
300
314
. encode ( best_proposal. compressed_data , self . bundle_id ) ?;
301
315
302
- let num_attempts = self
303
- . blocks
304
- . len ( )
305
- . saturating_sub ( self . attempts . len ( ) )
306
- . try_into ( )
307
- . map_err ( |_| crate :: Error :: Other ( "too many attempts" . to_string ( ) ) ) ?;
308
-
309
316
Ok ( BundleProposal {
310
317
metadata : Metadata {
311
318
block_heights : best_proposal. block_heights ,
312
- known_to_be_optimal : self . attempts . is_empty ( ) ,
319
+ known_to_be_optimal : self . sizes_to_try . is_empty ( ) ,
313
320
uncompressed_data_size : best_proposal. uncompressed_data_size ,
314
321
compressed_data_size,
315
322
gas_usage : best_proposal. gas_usage ,
316
- optimization_attempts : num_attempts ,
323
+ optimization_attempts : self . attempts_made ,
317
324
num_fragments : fragments. len_nonzero ( ) ,
325
+ block_num_upper_limit : self . max_blocks_in_bundle ,
318
326
} ,
319
327
fragments,
320
328
} )
0 commit comments