11use super :: { models, DatabaseError } ;
22use crate :: DatabaseConnectionProvider ;
3+
34use alloy_primitives:: B256 ;
45use futures:: { Stream , StreamExt } ;
56use rollup_node_primitives:: {
@@ -26,6 +27,8 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
2627 models:: batch_commit:: Column :: Hash ,
2728 models:: batch_commit:: Column :: BlockNumber ,
2829 models:: batch_commit:: Column :: BlockTimestamp ,
30+ models:: batch_commit:: Column :: Calldata ,
31+ models:: batch_commit:: Column :: BlobHash ,
2932 models:: batch_commit:: Column :: FinalizedBlockNumber ,
3033 ] )
3134 . to_owned ( ) ,
@@ -132,7 +135,10 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
132135 }
133136
134137 /// Delete all [`BatchCommitData`]s with a block number greater than the provided block number.
135- async fn delete_batches_gt ( & self , block_number : u64 ) -> Result < u64 , DatabaseError > {
138+ async fn delete_batches_gt_block_number (
139+ & self ,
140+ block_number : u64 ,
141+ ) -> Result < u64 , DatabaseError > {
136142 tracing:: trace!( target: "scroll::db" , block_number, "Deleting batch inputs greater than block number." ) ;
137143 Ok ( models:: batch_commit:: Entity :: delete_many ( )
138144 . filter ( models:: batch_commit:: Column :: BlockNumber . gt ( block_number as i64 ) )
@@ -141,6 +147,16 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
141147 . map ( |x| x. rows_affected ) ?)
142148 }
143149
150+ /// Delete all [`BatchCommitData`]s with a batch index greater than the provided index.
151+ async fn delete_batches_gt_batch_index ( & self , batch_index : u64 ) -> Result < u64 , DatabaseError > {
152+ tracing:: trace!( target: "scroll::db" , batch_index, "Deleting batch inputs greater than batch index." ) ;
153+ Ok ( models:: batch_commit:: Entity :: delete_many ( )
154+ . filter ( models:: batch_commit:: Column :: Index . gt ( batch_index as i64 ) )
155+ . exec ( self . get_connection ( ) )
156+ . await
157+ . map ( |x| x. rows_affected ) ?)
158+ }
159+
144160 /// Get an iterator over all [`BatchCommitData`]s in the database.
145161 async fn get_batches < ' a > (
146162 & ' a self ,
@@ -327,7 +343,7 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
327343 . get_batch_by_index ( batch_info. index - 1 )
328344 . await ?
329345 . expect ( "Batch info must be present due to database query arguments" ) ;
330- let l2_block = self . get_highest_block_for_batch ( previous_batch. hash ) . await ?;
346+ let l2_block = self . get_highest_block_for_batch_hash ( previous_batch. hash ) . await ?;
331347 ( l2_block, Some ( batch. block_number ) )
332348 } else {
333349 ( None , None )
@@ -337,7 +353,10 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
337353 }
338354
339355 /// Delete all L2 blocks with a block number greater than the provided block number.
340- async fn delete_l2_blocks_gt ( & self , block_number : u64 ) -> Result < u64 , DatabaseError > {
356+ async fn delete_l2_blocks_gt_block_number (
357+ & self ,
358+ block_number : u64 ,
359+ ) -> Result < u64 , DatabaseError > {
341360 tracing:: trace!( target: "scroll::db" , block_number, "Deleting L2 blocks greater than provided block number." ) ;
342361 Ok ( models:: l2_block:: Entity :: delete_many ( )
343362 . filter ( models:: l2_block:: Column :: BlockNumber . gt ( block_number as i64 ) )
@@ -346,6 +365,23 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
346365 . map ( |x| x. rows_affected ) ?)
347366 }
348367
368+ /// Delete all L2 blocks with a batch index greater than the batch index.
369+ async fn delete_l2_blocks_gt_batch_index (
370+ & self ,
371+ batch_index : u64 ,
372+ ) -> Result < u64 , DatabaseError > {
373+ tracing:: trace!( target: "scroll::db" , batch_index, "Deleting L2 blocks greater than provided batch index." ) ;
374+ Ok ( models:: l2_block:: Entity :: delete_many ( )
375+ . filter (
376+ Condition :: all ( )
377+ . add ( models:: l2_block:: Column :: BatchIndex . is_not_null ( ) )
378+ . add ( models:: l2_block:: Column :: BatchIndex . gt ( batch_index as i64 ) ) ,
379+ )
380+ . exec ( self . get_connection ( ) )
381+ . await
382+ . map ( |x| x. rows_affected ) ?)
383+ }
384+
349385 /// Insert a new block in the database.
350386 async fn insert_block (
351387 & self ,
@@ -365,6 +401,7 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
365401 . on_conflict (
366402 OnConflict :: column ( models:: l2_block:: Column :: BlockNumber )
367403 . update_columns ( [
404+ models:: l2_block:: Column :: BlockHash ,
368405 models:: l2_block:: Column :: BatchHash ,
369406 models:: l2_block:: Column :: BatchIndex ,
370407 ] )
@@ -396,7 +433,7 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
396433
397434 /// Returns the highest L2 block originating from the provided `batch_hash` or the highest block
398435 /// for the batch's index.
399- async fn get_highest_block_for_batch (
436+ async fn get_highest_block_for_batch_hash (
400437 & self ,
401438 batch_hash : B256 ,
402439 ) -> Result < Option < BlockInfo > , DatabaseError > {
@@ -420,14 +457,28 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
420457 }
421458 }
422459
460+ /// Returns the highest L2 block originating from the provided `batch_index` or the highest
461+ /// block for the batch's index.
462+ async fn get_highest_block_for_batch_index (
463+ & self ,
464+ batch_index : u64 ,
465+ ) -> Result < Option < BlockInfo > , DatabaseError > {
466+ Ok ( models:: l2_block:: Entity :: find ( )
467+ . filter ( models:: l2_block:: Column :: BatchIndex . lte ( batch_index) )
468+ . order_by_desc ( models:: l2_block:: Column :: BlockNumber )
469+ . one ( self . get_connection ( ) )
470+ . await ?
471+ . map ( |model| model. block_info ( ) ) )
472+ }
473+
423474 /// Unwinds the indexer by deleting all indexed data greater than the provided L1 block number.
424475 async fn unwind (
425476 & self ,
426477 genesis_hash : B256 ,
427478 l1_block_number : u64 ,
428479 ) -> Result < UnwindResult , DatabaseError > {
429480 // delete batch inputs and l1 messages
430- let batches_removed = self . delete_batches_gt ( l1_block_number) . await ?;
481+ let batches_removed = self . delete_batches_gt_block_number ( l1_block_number) . await ?;
431482 let deleted_messages = self . delete_l1_messages_gt ( l1_block_number) . await ?;
432483
433484 // filter and sort the executed L1 messages
@@ -441,10 +492,10 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
441492 if let Some ( msg) = removed_executed_l1_messages. first ( ) {
442493 let l2_reorg_block_number = msg
443494 . l2_block_number
444- . expect ( "we guarantee that this is Some(u64) due to the filter above" ) -
445- 1 ;
495+ . expect ( "we guarantee that this is Some(u64) due to the filter above" )
496+ . saturating_sub ( 1 ) ;
446497 let l2_block_info = self . get_l2_block_info_by_number ( l2_reorg_block_number) . await ?;
447- self . delete_l2_blocks_gt ( l2_reorg_block_number) . await ?;
498+ self . delete_l2_blocks_gt_block_number ( l2_reorg_block_number) . await ?;
448499 ( Some ( msg. transaction . queue_index ) , l2_block_info)
449500 } else {
450501 ( None , None )
0 commit comments