@@ -2,7 +2,7 @@ use crate::metrics;
22use alloy:: {
33 consensus:: BlockHeader ,
44 eips:: NumHash ,
5- primitives:: { B256 , b256} ,
5+ primitives:: { B256 , BlockNumber , b256} ,
66} ;
77use eyre:: Context ;
88use futures_util:: StreamExt ;
@@ -25,7 +25,7 @@ use signet_node_config::SignetNodeConfig;
2525use signet_node_types:: { NodeStatus , NodeTypesDbTrait , SignetNodeTypes } ;
2626use signet_rpc:: RpcServerGuard ;
2727use signet_types:: { PairedHeights , constants:: SignetSystemConstants } ;
28- use std:: { fmt, sync:: Arc } ;
28+ use std:: { fmt, mem :: MaybeUninit , sync:: Arc } ;
2929use tokio:: sync:: watch;
3030use tracing:: { debug, info, instrument} ;
3131
@@ -179,7 +179,9 @@ where
179179 /// errors.
180180 #[ instrument( skip( self ) , fields( host = ?self . host. config. chain. chain( ) ) ) ]
181181 pub async fn start ( mut self ) -> eyre:: Result < ( ) > {
182- self . ru_provider . ru_check_consistency ( ) ?;
182+ if let Some ( height) = self . ru_provider . ru_check_consistency ( ) ? {
183+ self . unwind_to ( height) . wrap_err ( "failed to unwind RU database to consistent state" ) ?;
184+ }
183185
184186 // This exists only to bypass the `tracing::instrument(err)` macro to
185187 // ensure that full sources get reported.
@@ -299,8 +301,7 @@ where
299301 // NB: REVERTS MUST RUN FIRST
300302 let mut reverted = None ;
301303 if let Some ( chain) = notification. reverted_chain ( ) {
302- reverted =
303- self . on_host_revert ( & chain) . await . wrap_err ( "error encountered during revert" ) ?;
304+ reverted = self . on_host_revert ( & chain) . wrap_err ( "error encountered during revert" ) ?;
304305 }
305306
306307 let mut committed = None ;
@@ -614,9 +615,23 @@ where
614615 }
615616 }
616617
618+ /// Unwind the RU chain DB to the target block number.
619+ fn unwind_to ( & self , target : BlockNumber ) -> eyre:: Result < RuChain > {
620+ let mut reverted = MaybeUninit :: uninit ( ) ;
621+ self . ru_provider
622+ . provider_rw ( ) ?
623+ . update ( |writer| {
624+ reverted. write ( writer. ru_take_blocks_and_execution_above ( target) ?) ;
625+ Ok ( ( ) )
626+ } )
627+ // SAFETY: if the closure above returns Ok, reverted is initialized.
628+ . map ( |_| unsafe { reverted. assume_init ( ) } )
629+ . map_err ( Into :: into)
630+ }
631+
617632 /// Called when the host chain has reverted a block or set of blocks.
618633 #[ instrument( skip_all, fields( first = chain. first( ) . number( ) , tip = chain. tip( ) . number( ) ) ) ]
619- pub async fn on_host_revert ( & self , chain : & Arc < Chain < Host > > ) -> eyre:: Result < Option < RuChain > > {
634+ pub fn on_host_revert ( & self , chain : & Arc < Chain < Host > > ) -> eyre:: Result < Option < RuChain > > {
620635 // If the end is before the RU genesis, we don't need to do anything at
621636 // all.
622637 if chain. tip ( ) . number ( ) <= self . constants . host_deploy_height ( ) {
@@ -632,12 +647,6 @@ where
632647 . unwrap_or_default ( ) // 0 if the block is before the deploy height
633648 . saturating_sub ( 1 ) ; // still 0 if 0, otherwise the block BEFORE.
634649
635- let mut reverted = None ;
636- self . ru_provider . provider_rw ( ) ?. update ( |writer| {
637- reverted = Some ( writer. ru_take_blocks_and_execution_above ( target) ?) ;
638- Ok ( ( ) )
639- } ) ?;
640-
641- Ok ( reverted)
650+ self . unwind_to ( target) . map ( Some )
642651 }
643652}
0 commit comments