@@ -11,7 +11,7 @@ use reth_network_p2p::{BlockClient, BodiesClient};
11
11
use reth_scroll_primitives:: ScrollBlock ;
12
12
use rollup_node_primitives:: {
13
13
BatchCommitData , BatchInfo , BlockInfo , BoundedVec , ChainImport , L1MessageEnvelope ,
14
- L2BlockInfoWithL1Messages , WithBlockNumber ,
14
+ L2BlockInfoWithL1Messages ,
15
15
} ;
16
16
use rollup_node_watcher:: L1Notification ;
17
17
use scroll_alloy_consensus:: TxL1Message ;
@@ -21,7 +21,6 @@ use scroll_db::{Database, DatabaseError, DatabaseOperations, L1MessageStart, Unw
21
21
use scroll_network:: NewBlockWithPeer ;
22
22
use std:: {
23
23
collections:: { HashMap , VecDeque } ,
24
- ops:: Add ,
25
24
pin:: Pin ,
26
25
sync:: {
27
26
atomic:: { AtomicU64 , Ordering } ,
@@ -67,8 +66,6 @@ pub struct ChainOrchestrator<ChainSpec, BC, P> {
67
66
pending_futures : VecDeque < ChainOrchestratorFuture > ,
68
67
/// The block number of the L1 finalized block.
69
68
l1_finalized_block_number : Arc < AtomicU64 > ,
70
- /// The block number of the L2 finalized block.
71
- l2_finalized_block_number : Arc < AtomicU64 > ,
72
69
/// The chain specification for the chain orchestrator.
73
70
chain_spec : Arc < ChainSpec > ,
74
71
/// The metrics for the chain orchestrator.
@@ -109,7 +106,6 @@ impl<
109
106
database,
110
107
pending_futures : Default :: default ( ) ,
111
108
l1_finalized_block_number : Arc :: new ( AtomicU64 :: new ( 0 ) ) ,
112
- l2_finalized_block_number : Arc :: new ( AtomicU64 :: new ( 0 ) ) ,
113
109
chain_spec,
114
110
metrics : ChainOrchestratorItem :: iter ( )
115
111
. map ( |i| {
@@ -529,7 +525,6 @@ impl<
529
525
self . database . clone ( ) ,
530
526
block_number,
531
527
self . l1_finalized_block_number . clone ( ) ,
532
- self . l2_finalized_block_number . clone ( ) ,
533
528
) ) ,
534
529
) )
535
530
}
@@ -551,16 +546,13 @@ impl<
551
546
) ) ,
552
547
) )
553
548
}
554
- L1Notification :: BatchFinalization { hash, index, block_number } => {
549
+ L1Notification :: BatchFinalization { hash : _hash , index, block_number } => {
555
550
ChainOrchestratorFuture :: HandleBatchFinalization ( self . handle_metered (
556
551
ChainOrchestratorItem :: BatchFinalization ,
557
552
Box :: pin ( Self :: handle_batch_finalization (
558
553
self . database . clone ( ) ,
559
- hash,
560
554
index,
561
555
block_number,
562
- self . l1_finalized_block_number . clone ( ) ,
563
- self . l2_finalized_block_number . clone ( ) ,
564
556
) ) ,
565
557
) )
566
558
}
@@ -615,35 +607,18 @@ impl<
615
607
database : Arc < Database > ,
616
608
block_number : u64 ,
617
609
l1_block_number : Arc < AtomicU64 > ,
618
- l2_block_number : Arc < AtomicU64 > ,
619
610
) -> Result < Option < ChainOrchestratorEvent > , ChainOrchestratorError > {
620
611
// Set the latest finalized L1 block in the database.
621
612
database. set_latest_finalized_l1_block_number ( block_number) . await ?;
622
613
623
- // get the finalized batch infos.
624
- // we add 1 to the low finalized l1 block number to avoid fetching the last finalized batch
625
- // a second time.
626
- let low_finalized_l1_block_number =
627
- l1_block_number. load ( Ordering :: Relaxed ) . add ( 1 ) . max ( block_number) ;
628
- let finalized_batches = database
629
- . get_batches_by_finalized_block_range ( low_finalized_l1_block_number, block_number)
630
- . await ?;
631
-
632
- // get the finalized block for the batch.
633
- let finalized_block = if let Some ( info) = finalized_batches. last ( ) {
634
- Self :: fetch_highest_finalized_block ( database, info. hash , l2_block_number) . await ?
635
- } else {
636
- None
637
- } ;
614
+ // Get all unprocessed batches that have been finalized by this L1 block finalization.
615
+ let finalized_batches =
616
+ database. fetch_and_update_unprocessed_finalized_batches ( block_number) . await ?;
638
617
639
- // update the chain orchestrator l1 block number.
618
+ // Update the chain orchestrator L1 block number.
640
619
l1_block_number. store ( block_number, Ordering :: Relaxed ) ;
641
620
642
- Ok ( Some ( ChainOrchestratorEvent :: L1BlockFinalized (
643
- block_number,
644
- finalized_batches,
645
- finalized_block,
646
- ) ) )
621
+ Ok ( Some ( ChainOrchestratorEvent :: L1BlockFinalized ( block_number, finalized_batches) ) )
647
622
}
648
623
649
624
/// Handles an L1 message by inserting it into the database.
@@ -715,54 +690,13 @@ impl<
715
690
/// Handles a batch finalization event by updating the batch input in the database.
716
691
async fn handle_batch_finalization (
717
692
database : Arc < Database > ,
718
- batch_hash : B256 ,
719
693
batch_index : u64 ,
720
694
block_number : u64 ,
721
- l1_block_number : Arc < AtomicU64 > ,
722
- l2_block_number : Arc < AtomicU64 > ,
723
695
) -> Result < Option < ChainOrchestratorEvent > , ChainOrchestratorError > {
724
696
// finalize all batches up to `batch_index`.
725
697
database. finalize_batches_up_to_index ( batch_index, block_number) . await ?;
726
698
727
- let mut finalized_block = None ;
728
- let mut finalized_batch = None ;
729
-
730
- // check if the block where the batch was finalized is finalized on L1.
731
- let l1_block_number_value = l1_block_number. load ( Ordering :: Relaxed ) ;
732
- if l1_block_number_value >= block_number {
733
- // fetch the finalized block.
734
- finalized_block =
735
- Self :: fetch_highest_finalized_block ( database, batch_hash, l2_block_number) . await ?;
736
-
737
- // set the finalized batch info.
738
- finalized_batch =
739
- Some ( WithBlockNumber :: new ( block_number, BatchInfo :: new ( batch_index, batch_hash) ) ) ;
740
- }
741
-
742
- let event = ChainOrchestratorEvent :: BatchFinalized ( finalized_batch, finalized_block) ;
743
- Ok ( Some ( event) )
744
- }
745
-
746
- /// Returns the highest finalized block for the provided batch hash. Will return [`None`] if the
747
- /// block number has already been seen by the chain orchestrator.
748
- async fn fetch_highest_finalized_block (
749
- database : Arc < Database > ,
750
- batch_hash : B256 ,
751
- l2_block_number : Arc < AtomicU64 > ,
752
- ) -> Result < Option < BlockInfo > , ChainOrchestratorError > {
753
- let finalized_block = database. get_highest_block_for_batch_hash ( batch_hash) . await ?;
754
-
755
- // only return the block if the chain orchestrator hasn't seen it.
756
- // in which case also update the `l2_finalized_block_number` value.
757
- Ok ( finalized_block. filter ( |info| {
758
- let current_l2_block_number = l2_block_number. load ( Ordering :: Relaxed ) ;
759
- if info. number > current_l2_block_number {
760
- l2_block_number. store ( info. number , Ordering :: Relaxed ) ;
761
- true
762
- } else {
763
- false
764
- }
765
- } ) )
699
+ Ok ( None )
766
700
}
767
701
}
768
702
0 commit comments