@@ -22,6 +22,7 @@ use common::{
2222} ;
2323use config:: BatchBuilderConfig ;
2424use std:: sync:: Arc ;
25+ use tokio_util:: sync:: CancellationToken ;
2526use tracing:: { debug, error, info, warn} ;
2627
2728// Temporary struct while we don't have forced inclusion flag in extra data
@@ -40,6 +41,7 @@ pub struct BatchManager {
4041 forced_inclusion : Arc < ForcedInclusion > ,
4142 cached_forced_inclusion_txs : CachedForcedInclusion ,
4243 metrics : Arc < Metrics > ,
44+ cancel_token : CancellationToken ,
4345}
4446
4547impl BatchManager {
@@ -49,6 +51,7 @@ impl BatchManager {
4951 ethereum_l1 : Arc < EthereumL1 < ExecutionLayer > > ,
5052 taiko : Arc < Taiko < ExecutionLayer > > ,
5153 metrics : Arc < Metrics > ,
54+ cancel_token : CancellationToken ,
5255 ) -> Self {
5356 info ! (
5457 "Batch builder config:\n \
@@ -76,6 +79,7 @@ impl BatchManager {
7679 forced_inclusion,
7780 cached_forced_inclusion_txs : CachedForcedInclusion :: Empty ,
7881 metrics,
82+ cancel_token,
7983 }
8084 }
8185
@@ -570,6 +574,16 @@ impl BatchManager {
570574 operation_type : OperationType ,
571575 anchor_block_id : u64 ,
572576 ) -> Result < Option < BuildPreconfBlockResponse > , Error > {
577+ if self . batch_builder . has_current_forced_inclusion ( ) {
578+ // we should not enter this function if we already have a forced inclusion
579+ // if we see this error in logs that means something is wrong in the code logic
580+ error ! (
581+ "Bug in code: Try to add new l2 block with forced inclusion when we already have one"
582+ ) ;
583+ return Err ( anyhow:: anyhow!(
584+ "Bug in code: Try to add new l2 block with forced inclusion when we already have one"
585+ ) ) ;
586+ }
573587 // get current forced inclusion
574588 let start = std:: time:: Instant :: now ( ) ;
575589 let forced_inclusion = self . forced_inclusion . consume_forced_inclusion ( ) . await ?;
@@ -622,18 +636,25 @@ impl BatchManager {
622636 "Failed to advance head to new forced inclusion L2 block: {}" ,
623637 err
624638 ) ;
639+ self . forced_inclusion . release_forced_inclusion ( ) . await ;
625640 self . batch_builder . remove_current_batch ( ) ;
626- return Ok ( None ) ; // TODO: why not return error here?
641+ return Err ( anyhow:: anyhow!(
642+ "Failed to advance head to new forced inclusion L2 block: {}" ,
643+ err
644+ ) ) ;
627645 }
628646 } ;
629647 // set it to batch builder
630648 if !self
631649 . batch_builder
632650 . set_forced_inclusion ( Some ( forced_inclusion_batch) )
633651 {
652+ // We should never enter here because it means we already have a forced inclusion
653+ // but we didn't set it yet. And at the beginning of the function we checked if
654+ // the forced inclusion is empty. This is a bug in the code logic
634655 error ! ( "Failed to set forced inclusion to batch" ) ;
635- self . batch_builder . remove_current_batch ( ) ;
636- return Ok ( None ) ; // TODO: why not return error here?
656+ self . cancel_token . cancel ( ) ;
657+ return Err ( anyhow :: anyhow! ( "Failed to set forced inclusion to batch" ) ) ;
637658 }
638659 return Ok ( forced_inclusion_block_response) ;
639660 }
@@ -821,6 +842,7 @@ impl BatchManager {
821842 forced_inclusion : self . forced_inclusion . clone ( ) ,
822843 cached_forced_inclusion_txs : CachedForcedInclusion :: Empty ,
823844 metrics : self . metrics . clone ( ) ,
845+ cancel_token : self . cancel_token . clone ( ) ,
824846 }
825847 }
826848
0 commit comments