@@ -56,8 +56,7 @@ pub struct HeaderSyncManager<S: StorageManager, N: NetworkManager> {
5656 syncing_headers : bool ,
5757 last_sync_progress : std:: time:: Instant ,
5858 headers2_failed : bool ,
59- // Cached flags for quick access without locking
60- cached_synced_from_checkpoint : bool ,
59+ // Cached flag for quick access without locking
6160 cached_sync_base_height : u32 ,
6261}
6362
@@ -94,7 +93,6 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
9493 syncing_headers : false ,
9594 last_sync_progress : std:: time:: Instant :: now ( ) ,
9695 headers2_failed : false ,
97- cached_synced_from_checkpoint : false ,
9896 cached_sync_base_height : 0 ,
9997 _phantom_s : std:: marker:: PhantomData ,
10098 _phantom_n : std:: marker:: PhantomData ,
@@ -106,13 +104,11 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
106104 // First, try to load the persisted chain state which may contain sync_base_height
107105 if let Ok ( Some ( stored_chain_state) ) = storage. load_chain_state ( ) . await {
108106 tracing:: info!(
109- "Loaded chain state from storage with sync_base_height: {}, synced_from_checkpoint: {} " ,
107+ "Loaded chain state from storage with sync_base_height: {}" ,
110108 stored_chain_state. sync_base_height,
111- stored_chain_state. synced_from_checkpoint
112109 ) ;
113110 // Update our chain state with the loaded one to preserve sync_base_height
114111 {
115- self . cached_synced_from_checkpoint = stored_chain_state. synced_from_checkpoint ;
116112 self . cached_sync_base_height = stored_chain_state. sync_base_height ;
117113 let mut cs = self . chain_state . write ( ) . await ;
118114 * cs = stored_chain_state;
@@ -128,7 +124,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
128124 let Some ( tip_height) = tip_height else {
129125 tracing:: debug!( "No headers found in storage" ) ;
130126 // If we're syncing from a checkpoint, this is expected
131- if self . is_synced_from_checkpoint ( ) && self . get_sync_base_height ( ) > 0 {
127+ if self . is_synced_from_checkpoint ( ) {
132128 tracing:: info!( "No headers in storage for checkpoint sync - this is expected" ) ;
133129 return Ok ( 0 ) ;
134130 }
@@ -147,15 +143,8 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
147143 const BATCH_SIZE : u32 = 10_000 ;
148144 let mut loaded_count = 0u32 ;
149145
150- // Determine the first blockchain height we need to load.
151146 // For checkpoint syncs we start at the checkpoint base; otherwise we skip genesis (already present).
152- let base_height = if self . is_synced_from_checkpoint ( ) && self . get_sync_base_height ( ) > 0 {
153- self . get_sync_base_height ( )
154- } else {
155- 1
156- } ;
157-
158- let mut current_height = base_height;
147+ let mut current_height = self . get_sync_base_height ( ) . max ( 1 ) ;
159148
160149 while current_height <= tip_height {
161150 let end_height = ( current_height + BATCH_SIZE - 1 ) . min ( tip_height) ;
@@ -406,7 +395,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
406395 Some ( hash) => {
407396 // When syncing from a checkpoint, we need to create a proper locator
408397 // that helps the peer understand we want headers AFTER this point
409- if self . is_synced_from_checkpoint ( ) && self . get_sync_base_height ( ) > 0 {
398+ if self . is_synced_from_checkpoint ( ) {
410399 // For checkpoint sync, only include the checkpoint hash
411400 // Including genesis would allow peers to fall back to sending headers from genesis
412401 // if they don't recognize the checkpoint, which is exactly what we want to avoid
@@ -651,9 +640,8 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
651640
652641 tracing:: info!( "Preparing header synchronization" ) ;
653642 tracing:: info!(
654- "Chain state before prepare_sync: sync_base_height={}, synced_from_checkpoint={}, headers_count={}" ,
643+ "Chain state before prepare_sync: sync_base_height={}, headers_count={}" ,
655644 self . get_sync_base_height( ) ,
656- self . is_synced_from_checkpoint( ) ,
657645 self . chain_state. read( ) . await . headers. len( )
658646 ) ;
659647
@@ -679,8 +667,8 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
679667 }
680668 } else {
681669 tracing:: info!(
682- "Not syncing from checkpoint or no tip height. synced_from_checkpoint ={}, current_tip_height={:?}" ,
683- self . is_synced_from_checkpoint ( ) ,
670+ "Not syncing from checkpoint or no tip height. sync_base_height ={}, current_tip_height={:?}" ,
671+ self . get_sync_base_height ( ) ,
684672 current_tip_height
685673 ) ;
686674 current_tip_height
@@ -769,12 +757,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
769757 } else {
770758 // Get the current tip hash from storage
771759 // When syncing from checkpoint, the storage height is different from effective height
772- let storage_height = if self . is_synced_from_checkpoint ( ) {
773- // The actual storage height is effective_height - sync_base_height
774- height. saturating_sub ( self . get_sync_base_height ( ) )
775- } else {
776- height
777- } ;
760+ let storage_height = height. saturating_sub ( self . get_sync_base_height ( ) ) ;
778761
779762 let tip_header = storage. get_header ( storage_height) . await . map_err ( |e| {
780763 SyncError :: Storage ( format ! (
@@ -855,7 +838,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
855838 let recovery_base_hash = match current_tip_height {
856839 None => {
857840 // No headers in storage - check if we're syncing from a checkpoint
858- if self . is_synced_from_checkpoint ( ) && self . get_sync_base_height ( ) > 0 {
841+ if self . is_synced_from_checkpoint ( ) {
859842 // Use the checkpoint hash from chain state
860843 if !self . chain_state . read ( ) . await . headers . is_empty ( ) {
861844 let checkpoint_hash =
@@ -1038,27 +1021,13 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
10381021
10391022 /// Whether we're syncing from a checkpoint
10401023 pub fn is_synced_from_checkpoint ( & self ) -> bool {
1041- self . cached_synced_from_checkpoint
1024+ self . cached_sync_base_height > 0
10421025 }
10431026
10441027 /// Update cached flags and totals based on an external state snapshot
1045- pub fn update_cached_from_state_snapshot (
1046- & mut self ,
1047- synced_from_checkpoint : bool ,
1048- sync_base_height : u32 ,
1049- headers_len : u32 ,
1050- ) {
1051- self . cached_synced_from_checkpoint = synced_from_checkpoint;
1028+ pub fn update_cached_from_state_snapshot ( & mut self , sync_base_height : u32 , headers_len : u32 ) {
10521029 self . cached_sync_base_height = sync_base_height;
10531030 // Absolute blockchain tip height = base + headers_len - 1 (if any headers exist)
1054- self . total_headers_synced = if headers_len == 0 {
1055- if synced_from_checkpoint {
1056- sync_base_height
1057- } else {
1058- 0
1059- }
1060- } else {
1061- sync_base_height. saturating_add ( headers_len) . saturating_sub ( 1 )
1062- } ;
1031+ self . total_headers_synced = sync_base_height. saturating_add ( headers_len) . saturating_sub ( 1 ) ;
10631032 }
10641033}
0 commit comments