@@ -318,26 +318,38 @@ impl CombinedDatabase {
318318 /// we leave it to the caller to decide how to bring them up to date.
319319 /// We don't rollback the on-chain database as it is the source of truth.
320320 /// The target height of the rollback is the latest height of the on-chain database.
321- pub fn sync_aux_db_heights ( & self ) -> anyhow:: Result < ( ) > {
322- let on_chain_height = match self . on_chain ( ) . latest_height_from_metadata ( ) ? {
323- Some ( height) => height,
324- None => return Ok ( ( ) ) , // Exit loop if on-chain height is None
325- } ;
321+ pub fn sync_aux_db_heights < S > ( & self , shutdown_listener : & mut S ) -> anyhow:: Result < ( ) >
322+ where
323+ S : ShutdownListener ,
324+ {
325+ while !shutdown_listener. is_cancelled ( ) {
326+ let on_chain_height = match self . on_chain ( ) . latest_height_from_metadata ( ) ? {
327+ Some ( height) => height,
328+ None => break , // Exit loop if on-chain height is None
329+ } ;
330+
331+ let off_chain_height = self . off_chain ( ) . latest_height_from_metadata ( ) ?;
332+ let gas_price_height = self . gas_price ( ) . latest_height_from_metadata ( ) ?;
326333
327- let off_chain_height = self . off_chain ( ) . latest_height_from_metadata ( ) ?;
328- let gas_price_height = self . gas_price ( ) . latest_height_from_metadata ( ) ?;
334+ // Handle off-chain rollback if necessary
335+ if let Some ( off_height) = off_chain_height {
336+ if off_height > on_chain_height {
337+ self . off_chain ( ) . rollback_last_block ( ) ?;
338+ }
339+ }
329340
330- // Handle off-chain rollback if necessary
331- if let Some ( off_height) = off_chain_height {
332- if off_height > on_chain_height {
333- self . off_chain ( ) . rollback_to ( on_chain_height) ?;
341+ // Handle gas price rollback if necessary
342+ if let Some ( gas_height) = gas_price_height {
343+ if gas_height > on_chain_height {
344+ self . gas_price ( ) . rollback_last_block ( ) ?;
345+ }
334346 }
335- }
336347
337- // Handle gas price rollback if necessary
338- if let Some ( gas_height) = gas_price_height {
339- if gas_height > on_chain_height {
340- self . gas_price ( ) . rollback_to ( on_chain_height) ?;
348+ // If both off-chain and gas price heights are synced, break
349+ if off_chain_height. map_or ( true , |h| h <= on_chain_height)
350+ && gas_price_height. map_or ( true , |h| h <= on_chain_height)
351+ {
352+ break ;
341353 }
342354 }
343355
0 commit comments