Skip to content

Commit 325202e

Browse files
authored
update batch finalization logic (#308)
1 parent a8b95a3 commit 325202e

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

crates/chain-orchestrator/src/event.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use alloy_consensus::Header;
22
use alloy_primitives::{Signature, B256};
33
use reth_network_peers::PeerId;
44
use reth_scroll_primitives::ScrollBlock;
5-
use rollup_node_primitives::{
6-
BatchInfo, BlockInfo, ChainImport, L2BlockInfoWithL1Messages, WithFinalizedBlockNumber,
7-
};
5+
use rollup_node_primitives::{BatchInfo, BlockInfo, ChainImport, L2BlockInfoWithL1Messages};
86

97
/// An event emitted by the `ChainOrchestrator`.
108
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -40,9 +38,8 @@ pub enum ChainOrchestratorEvent {
4038
/// The safe L2 block info.
4139
safe_head: Option<BlockInfo>,
4240
},
43-
/// A batch has been finalized returning an optional finalized L2 block. Also returns a
44-
/// [`BatchInfo`] if the finalized event occurred in a finalized L1 block.
45-
BatchFinalized(Option<WithFinalizedBlockNumber<BatchInfo>>, Option<BlockInfo>),
41+
/// A batch has been finalized returning a list of finalized batches.
42+
BatchFinalized(u64, Vec<BatchInfo>),
4643
/// An L1 block has been finalized returning the L1 block number and the list of finalized
4744
/// batches.
4845
L1BlockFinalized(u64, Vec<BatchInfo>),

crates/chain-orchestrator/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ impl<
556556
self.database.clone(),
557557
index,
558558
block_number,
559+
self.l1_finalized_block_number.clone(),
559560
)),
560561
))
561562
}
@@ -695,10 +696,20 @@ impl<
695696
database: Arc<Database>,
696697
batch_index: u64,
697698
block_number: u64,
699+
finalized_block_number: Arc<AtomicU64>,
698700
) -> Result<Option<ChainOrchestratorEvent>, ChainOrchestratorError> {
699701
// finalize all batches up to `batch_index`.
700702
database.finalize_batches_up_to_index(batch_index, block_number).await?;
701703

704+
// Get all unprocessed batches that have been finalized by this L1 block finalization.
705+
let finalized_block_number = finalized_block_number.load(Ordering::Relaxed);
706+
if finalized_block_number >= block_number {
707+
let finalized_batches = database
708+
.fetch_and_update_unprocessed_finalized_batches(finalized_block_number)
709+
.await?;
710+
return Ok(Some(ChainOrchestratorEvent::BatchFinalized(block_number, finalized_batches)))
711+
}
712+
702713
Ok(None)
703714
}
704715
}

crates/manager/src/manager/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,16 @@ where
274274
// // push the batch info into the derivation pipeline.
275275
// self.derivation_pipeline.push_batch(batch_info, l1_block_number);
276276
}
277-
ChainOrchestratorEvent::BatchFinalized(batch_info, ..) => {
277+
ChainOrchestratorEvent::BatchFinalized(block_number, finalized_batches) => {
278278
// Uncomment once we implement issue #273.
279279
// // update the fcs on new finalized block.
280280
// if let Some(finalized_block) = finalized_block {
281281
// self.engine.set_finalized_block_info(finalized_block);
282282
// }
283283
// Remove once we implement issue #273.
284284
// Update the derivation pipeline on new finalized batch.
285-
#[allow(clippy::collapsible_match)]
286-
if let Some(batch_info) = batch_info {
287-
self.derivation_pipeline.push_batch(batch_info.inner, batch_info.number);
285+
for batch_info in finalized_batches {
286+
self.derivation_pipeline.push_batch(batch_info, block_number);
288287
}
289288
}
290289
ChainOrchestratorEvent::L1BlockFinalized(l1_block_number, finalized_batches, ..) => {

0 commit comments

Comments
 (0)