Skip to content

Commit

Permalink
feat (batcher): send batches sequentially (#707)
Browse files Browse the repository at this point in the history
Co-authored-by: Mariano Nicolini <mariano.nicolini.91@gmail.com>
  • Loading branch information
taturosati and entropidelic committed Aug 2, 2024
1 parent 318be6d commit cc6d6a8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion batcher/aligned-batcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub struct Batcher {
last_uploaded_batch_block: Mutex<u64>,
pre_verification_is_enabled: bool,
non_paying_config: Option<NonPayingConfig>,
posting_batch: Mutex<bool>,
}

impl Batcher {
Expand Down Expand Up @@ -166,6 +167,7 @@ impl Batcher {
last_uploaded_batch_block: Mutex::new(last_uploaded_batch_block),
pre_verification_is_enabled: config.batcher.pre_verification_is_enabled,
non_paying_config,
posting_batch: Mutex::new(false),
}
}

Expand Down Expand Up @@ -435,6 +437,18 @@ impl Batcher {
.map(|(vd, _, _, _)| vd.clone())
.collect();

// Check if a batch is currently being posted
let mut batch_posting = self.posting_batch.lock().await;
if *batch_posting {
info!(
"Batch is currently being posted. Waiting for the current batch to be finalized..."
);
return None;
}

// Set the batch posting flag to true
*batch_posting = true;

let current_batch_size = serde_json::to_vec(&batch_verification_data).unwrap().len();

// check if the current batch needs to be splitted into smaller batches
Expand Down Expand Up @@ -570,7 +584,14 @@ impl Batcher {
/// finalizes the batch.
async fn handle_new_block(&self, block_number: u64) -> Result<(), BatcherError> {
while let Some(finalized_batch) = self.is_batch_ready(block_number).await {
self.finalize_batch(block_number, finalized_batch).await?;
let batch_finalization_result =
self.finalize_batch(block_number, finalized_batch).await;

// Resetting this here to avoid doing it on every return path of `finalize_batch` function
let mut batch_posting = self.posting_batch.lock().await;
*batch_posting = false;

batch_finalization_result?;
}
Ok(())
}
Expand Down

0 comments on commit cc6d6a8

Please sign in to comment.