Skip to content

Commit 54d0151

Browse files
correct counting of attempts, log the max upper block limit
1 parent 8d6182d commit 54d0151

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

packages/services/src/block_bundler/bundler.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use crate::{
1717
pub struct Metadata {
1818
pub block_heights: RangeInclusive<u32>,
1919
pub known_to_be_optimal: bool,
20-
pub optimization_attempts: u64,
20+
pub block_num_upper_limit: NonZeroUsize,
21+
pub optimization_attempts: usize,
2122
pub gas_usage: u64,
2223
pub compressed_data_size: NonZeroUsize,
2324
pub uncompressed_data_size: NonZeroUsize,
@@ -155,7 +156,9 @@ pub struct Bundler<FragmentEncoder> {
155156
best_valid_proposal: Option<Proposal>,
156157
smallest_invalid_proposal: Option<Proposal>,
157158
bundle_encoder: bundle::Encoder,
158-
attempts: VecDeque<NonZeroUsize>,
159+
sizes_to_try: VecDeque<NonZeroUsize>,
160+
attempts_made: usize,
161+
max_blocks_in_bundle: NonZeroUsize,
159162
bundle_id: NonNegative<i32>,
160163
target_num_fragments: NonZeroUsize,
161164
}
@@ -180,9 +183,11 @@ impl<T> Bundler<T> {
180183
best_valid_proposal: None,
181184
smallest_invalid_proposal: None,
182185
bundle_encoder,
183-
attempts,
186+
sizes_to_try: attempts,
184187
bundle_id,
185188
target_num_fragments,
189+
attempts_made: 0,
190+
max_blocks_in_bundle: max_blocks,
186191
}
187192
}
188193

@@ -214,8 +219,12 @@ impl<T> Bundler<T> {
214219
_ => {}
215220
}
216221

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);
219228
}
220229
}
221230

@@ -234,8 +243,8 @@ impl<T> Bundler<T> {
234243
) -> Vec<NonEmpty<CompressedFuelBlock>> {
235244
let mut blocks_for_attempts = vec![];
236245

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");
239248
let blocks = self.blocks_for_new_proposal(block_count);
240249
blocks_for_attempts.push(blocks);
241250
}
@@ -272,15 +281,20 @@ where
272281
T: crate::block_bundler::port::l1::FragmentEncoder + Send + Sync + Clone + 'static,
273282
{
274283
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() {
276288
return Ok(false);
277289
}
290+
let proposals = self.analyze(optimization_runs).await?;
291+
self.attempts_made += proposals.len();
278292

279-
for proposal in self.analyze(optimization_runs).await? {
293+
for proposal in proposals {
280294
self.save_if_best_so_far(proposal);
281295
}
282296

283-
Ok(!self.attempts.is_empty())
297+
Ok(!self.sizes_to_try.is_empty())
284298
}
285299

286300
async fn finish(mut self) -> Result<BundleProposal> {
@@ -299,22 +313,16 @@ where
299313
.fragment_encoder
300314
.encode(best_proposal.compressed_data, self.bundle_id)?;
301315

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-
309316
Ok(BundleProposal {
310317
metadata: Metadata {
311318
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(),
313320
uncompressed_data_size: best_proposal.uncompressed_data_size,
314321
compressed_data_size,
315322
gas_usage: best_proposal.gas_usage,
316-
optimization_attempts: num_attempts,
323+
optimization_attempts: self.attempts_made,
317324
num_fragments: fragments.len_nonzero(),
325+
block_num_upper_limit: self.max_blocks_in_bundle,
318326
},
319327
fragments,
320328
})

packages/services/tests/block_bundler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ async fn stops_advancing_if_optimization_time_ran_out() -> Result<()> {
484484
compressed_data_size: 100.try_into().unwrap(),
485485
uncompressed_data_size: 1000.try_into().unwrap(),
486486
num_fragments: 1.try_into().unwrap(),
487+
block_num_upper_limit: 1.try_into().unwrap(),
487488
},
488489
};
489490

0 commit comments

Comments
 (0)