@@ -11,7 +11,7 @@ use reth_network_p2p::{BlockClient, BodiesClient};
1111use reth_scroll_primitives:: ScrollBlock ;
1212use rollup_node_primitives:: {
1313 BatchCommitData , BatchInfo , BlockInfo , BoundedVec , ChainImport , L1MessageEnvelope ,
14- L2BlockInfoWithL1Messages ,
14+ L2BlockInfoWithL1Messages , WithBlockNumber ,
1515} ;
1616use rollup_node_watcher:: L1Notification ;
1717use scroll_alloy_consensus:: TxL1Message ;
@@ -21,6 +21,7 @@ use scroll_db::{Database, DatabaseError, DatabaseOperations, L1MessageStart, Unw
2121use scroll_network:: NewBlockWithPeer ;
2222use std:: {
2323 collections:: { HashMap , VecDeque } ,
24+ ops:: Add ,
2425 pin:: Pin ,
2526 sync:: {
2627 atomic:: { AtomicU64 , Ordering } ,
@@ -545,12 +546,13 @@ impl<
545546 ) ) ,
546547 ) )
547548 }
548- L1Notification :: BatchFinalization { hash, block_number , .. } => {
549+ L1Notification :: BatchFinalization { hash, index , block_number } => {
549550 ChainOrchestratorFuture :: HandleBatchFinalization ( self . handle_metered (
550551 ChainOrchestratorItem :: BatchFinalization ,
551552 Box :: pin ( Self :: handle_batch_finalization (
552553 self . database . clone ( ) ,
553554 hash,
555+ index,
554556 block_number,
555557 self . l1_finalized_block_number . clone ( ) ,
556558 self . l2_finalized_block_number . clone ( ) ,
@@ -602,8 +604,8 @@ impl<
602604 } ) )
603605 }
604606
605- /// Handles a finalized event by updating the chain orchestrator L1 finalized block and
606- /// returning the new finalized L2 chain block.
607+ /// Handles a finalized event by updating the chain orchestrator L1 finalized block, returning
608+ /// the new finalized L2 chain block and the list of finalized batches .
607609 async fn handle_finalized (
608610 database : Arc < Database > ,
609611 block_number : u64 ,
@@ -613,20 +615,30 @@ impl<
613615 // Set the latest finalized L1 block in the database.
614616 database. set_latest_finalized_l1_block_number ( block_number) . await ?;
615617
616- // get the newest finalized batch.
617- let batch_hash = database. get_finalized_batch_hash_at_height ( block_number) . await ?;
618+ // get the finalized batch infos.
619+ // we add 1 to the low finalized l1 block number to avoid fetching the last finalized batch
620+ // a second time.
621+ let low_finalized_l1_block_number =
622+ l1_block_number. load ( Ordering :: Relaxed ) . add ( 1 ) . max ( block_number) ;
623+ let finalized_batches = database
624+ . get_batches_by_finalized_block_range ( low_finalized_l1_block_number, block_number)
625+ . await ?;
618626
619627 // get the finalized block for the batch.
620- let finalized_block = if let Some ( hash ) = batch_hash {
621- Self :: fetch_highest_finalized_block ( database, hash, l2_block_number) . await ?
628+ let finalized_block = if let Some ( info ) = finalized_batches . last ( ) {
629+ Self :: fetch_highest_finalized_block ( database, info . hash , l2_block_number) . await ?
622630 } else {
623631 None
624632 } ;
625633
626634 // update the chain orchestrator l1 block number.
627635 l1_block_number. store ( block_number, Ordering :: Relaxed ) ;
628636
629- Ok ( Some ( ChainOrchestratorEvent :: L1BlockFinalized ( block_number, finalized_block) ) )
637+ Ok ( Some ( ChainOrchestratorEvent :: L1BlockFinalized (
638+ block_number,
639+ finalized_batches,
640+ finalized_block,
641+ ) ) )
630642 }
631643
632644 /// Handles an L1 message by inserting it into the database.
@@ -699,23 +711,30 @@ impl<
699711 async fn handle_batch_finalization (
700712 database : Arc < Database > ,
701713 batch_hash : B256 ,
714+ batch_index : u64 ,
702715 block_number : u64 ,
703716 l1_block_number : Arc < AtomicU64 > ,
704717 l2_block_number : Arc < AtomicU64 > ,
705718 ) -> Result < Option < ChainOrchestratorEvent > , ChainOrchestratorError > {
706719 // finalized the batch.
707720 database. finalize_batch ( batch_hash, block_number) . await ?;
708721
709- // check if the block where the batch was finalized is finalized on L1.
710722 let mut finalized_block = None ;
723+ let mut finalized_batch = None ;
724+
725+ // check if the block where the batch was finalized is finalized on L1.
711726 let l1_block_number_value = l1_block_number. load ( Ordering :: Relaxed ) ;
712- if l1_block_number_value > block_number {
727+ if l1_block_number_value >= block_number {
713728 // fetch the finalized block.
714729 finalized_block =
715730 Self :: fetch_highest_finalized_block ( database, batch_hash, l2_block_number) . await ?;
731+
732+ // set the finalized batch info.
733+ finalized_batch =
734+ Some ( WithBlockNumber :: new ( block_number, BatchInfo :: new ( batch_index, batch_hash) ) ) ;
716735 }
717736
718- let event = ChainOrchestratorEvent :: BatchFinalized ( batch_hash , finalized_block) ;
737+ let event = ChainOrchestratorEvent :: BatchFinalized ( finalized_batch , finalized_block) ;
719738 Ok ( Some ( event) )
720739 }
721740
0 commit comments