Skip to content

Commit cc6d6a8

Browse files
feat (batcher): send batches sequentially (#707)
Co-authored-by: Mariano Nicolini <[email protected]>
1 parent 318be6d commit cc6d6a8

File tree

1 file changed

+22
-1
lines changed
  • batcher/aligned-batcher/src

1 file changed

+22
-1
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub struct Batcher {
9898
last_uploaded_batch_block: Mutex<u64>,
9999
pre_verification_is_enabled: bool,
100100
non_paying_config: Option<NonPayingConfig>,
101+
posting_batch: Mutex<bool>,
101102
}
102103

103104
impl Batcher {
@@ -166,6 +167,7 @@ impl Batcher {
166167
last_uploaded_batch_block: Mutex::new(last_uploaded_batch_block),
167168
pre_verification_is_enabled: config.batcher.pre_verification_is_enabled,
168169
non_paying_config,
170+
posting_batch: Mutex::new(false),
169171
}
170172
}
171173

@@ -435,6 +437,18 @@ impl Batcher {
435437
.map(|(vd, _, _, _)| vd.clone())
436438
.collect();
437439

440+
// Check if a batch is currently being posted
441+
let mut batch_posting = self.posting_batch.lock().await;
442+
if *batch_posting {
443+
info!(
444+
"Batch is currently being posted. Waiting for the current batch to be finalized..."
445+
);
446+
return None;
447+
}
448+
449+
// Set the batch posting flag to true
450+
*batch_posting = true;
451+
438452
let current_batch_size = serde_json::to_vec(&batch_verification_data).unwrap().len();
439453

440454
// check if the current batch needs to be splitted into smaller batches
@@ -570,7 +584,14 @@ impl Batcher {
570584
/// finalizes the batch.
571585
async fn handle_new_block(&self, block_number: u64) -> Result<(), BatcherError> {
572586
while let Some(finalized_batch) = self.is_batch_ready(block_number).await {
573-
self.finalize_batch(block_number, finalized_batch).await?;
587+
let batch_finalization_result =
588+
self.finalize_batch(block_number, finalized_batch).await;
589+
590+
// Resetting this here to avoid doing it on every return path of `finalize_batch` function
591+
let mut batch_posting = self.posting_batch.lock().await;
592+
*batch_posting = false;
593+
594+
batch_finalization_result?;
574595
}
575596
Ok(())
576597
}

0 commit comments

Comments
 (0)