@@ -11,7 +11,8 @@ use mp_rpc::v0_9_0::{
1111 ClassAndTxnHash , ContractAndTxnHash ,
1212} ;
1313use mp_transactions:: { L1HandlerTransactionResult , L1HandlerTransactionWithFee } ;
14- use mp_utils:: service:: MadaraServiceId ;
14+ use mp_utils:: service:: { MadaraServiceId , MadaraServiceStatus } ;
15+ use std:: time:: Duration ;
1516#[ async_trait]
1617impl MadaraWriteRpcApiV0_1_0Server for Starknet {
1718 /// Submit a new class v0 declaration transaction, bypassing mempool and all validation.
@@ -126,7 +127,26 @@ impl MadaraWriteRpcApiV0_1_0Server for Starknet {
126127 }
127128 }
128129
130+ // Check if block production is currently running (sequencer mode)
131+ let bp_was_running = matches ! (
132+ self . ctx. service_status( MadaraServiceId :: BlockProduction ) ,
133+ MadaraServiceStatus :: On
134+ ) ;
135+
136+ // If running, shut down block production cleanly before reorg
137+ if bp_was_running {
138+ tracing:: info!( "🔌 Stopping block production service for reorg..." ) ;
139+ self . ctx . service_remove ( MadaraServiceId :: BlockProduction ) ;
140+ // Allow time for clean shutdown of executor threads and channels
141+ tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
142+ tracing:: info!( "✅ Block production service stopped" ) ;
143+ }
144+
145+ // Perform the blockchain reorg
146+ tracing:: info!( "🔄 Reverting blockchain to block {}" , target_block_n) ;
129147 self . backend . revert_to ( & block_hash) . map_err ( StarknetRpcApiError :: from) ?;
148+
149+ // Update chain tip cache
130150 let fresh_chain_tip = self
131151 . backend
132152 . db
@@ -135,19 +155,13 @@ impl MadaraWriteRpcApiV0_1_0Server for Starknet {
135155 . map_err ( StarknetRpcApiError :: from) ?;
136156 let backend_chain_tip = mc_db:: ChainTip :: from_storage ( fresh_chain_tip) ;
137157 self . backend . chain_tip . send_replace ( backend_chain_tip) ;
158+ tracing:: info!( "✅ Blockchain reverted successfully" ) ;
138159
139- // Reset block production executor state if block production is enabled
140- let bp_status = self . ctx . service_status ( MadaraServiceId :: BlockProduction ) ;
141- if matches ! ( bp_status, mp_utils:: service:: MadaraServiceStatus :: On ) {
142- if let Some ( block_prod_handle) = & self . block_prod_handle {
143- tracing:: debug!( "revertTo: resetting block production state" ) ;
144- block_prod_handle
145- . reset_state ( )
146- . await
147- . context ( "Resetting block production executor state after reorg" )
148- . map_err ( StarknetRpcApiError :: from) ?;
149- tracing:: debug!( "revertTo: block production state reset complete" ) ;
150- }
160+ // Restart block production if it was running before
161+ if bp_was_running {
162+ tracing:: info!( "🔌 Restarting block production service after reorg..." ) ;
163+ self . ctx . service_add ( MadaraServiceId :: BlockProduction ) ;
164+ tracing:: info!( "✅ Block production service restarted with fresh state" ) ;
151165 }
152166
153167 Ok ( ( ) )
0 commit comments