Skip to content

Commit 408d4c7

Browse files
Merge pull request #95 from dashpay/fix-sync-status-genesis
Fix sync status genesis
2 parents 295d76f + 153fc69 commit 408d4c7

File tree

1 file changed

+23
-66
lines changed

1 file changed

+23
-66
lines changed

dash-spv/src/client/status_display.rs

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -43,51 +43,23 @@ impl<'a> StatusDisplay<'a> {
4343
state: &ChainState,
4444
with_logging: bool,
4545
) -> u32 {
46-
if state.synced_from_checkpoint && state.sync_base_height > 0 {
47-
// Get the actual number of headers in storage
48-
if let Ok(Some(storage_tip)) = self.storage.get_tip_height().await {
49-
// The blockchain height is sync_base_height + storage_tip
50-
let blockchain_height = state.sync_base_height + storage_tip;
51-
if with_logging {
52-
tracing::debug!(
53-
"Status display (checkpoint sync): storage_tip={}, sync_base={}, blockchain_height={}",
54-
storage_tip, state.sync_base_height, blockchain_height
55-
);
56-
}
57-
blockchain_height
58-
} else {
59-
// No headers in storage yet, use the checkpoint height
60-
state.sync_base_height
46+
// Unified formula for both checkpoint and genesis sync:
47+
// For genesis sync: sync_base_height = 0, so height = 0 + storage_count
48+
// For checkpoint sync: height = checkpoint_height + storage_count
49+
if let Ok(Some(storage_tip)) = self.storage.get_tip_height().await {
50+
let blockchain_height = state.sync_base_height + storage_tip;
51+
if with_logging {
52+
tracing::debug!(
53+
"Status display: storage_tip={}, sync_base={}, blockchain_height={}",
54+
storage_tip,
55+
state.sync_base_height,
56+
blockchain_height
57+
);
6158
}
59+
blockchain_height
6260
} else {
63-
// Normal sync from genesis
64-
// Check if headers are in storage but not loaded into memory yet
65-
if state.headers.is_empty() {
66-
// Headers might be in storage but not loaded into ChainState yet
67-
if let Ok(Some(storage_tip)) = self.storage.get_tip_height().await {
68-
if with_logging {
69-
tracing::debug!(
70-
"Status display (normal sync): ChainState empty but storage has {} headers",
71-
storage_tip
72-
);
73-
}
74-
storage_tip
75-
} else {
76-
// No headers in storage or ChainState
77-
0
78-
}
79-
} else {
80-
// Headers are loaded in ChainState, use tip_height()
81-
let tip = state.tip_height();
82-
if with_logging {
83-
tracing::debug!(
84-
"Status display (normal sync): chain state has {} headers, tip_height={}",
85-
state.headers.len(),
86-
tip
87-
);
88-
}
89-
tip
90-
}
61+
// No headers in storage yet
62+
state.sync_base_height
9163
}
9264
}
9365

@@ -242,30 +214,15 @@ impl<'a> StatusDisplay<'a> {
242214
/// This helper method encapsulates the logic for determining the current filter header height,
243215
/// taking into account whether we're syncing from a checkpoint or from genesis.
244216
async fn calculate_filter_header_height(&self, state: &ChainState) -> u32 {
245-
if state.synced_from_checkpoint && state.sync_base_height > 0 {
246-
// Get the actual number of filter headers in storage
247-
if let Ok(Some(storage_height)) = self.storage.get_filter_tip_height().await {
248-
// The blockchain height is sync_base_height + storage_height
249-
state.sync_base_height + storage_height
250-
} else {
251-
// No filter headers in storage yet, use the checkpoint height
252-
state.sync_base_height
253-
}
217+
// Unified formula for both checkpoint and genesis sync:
218+
// For genesis sync: sync_base_height = 0, so height = 0 + storage_count
219+
// For checkpoint sync: height = checkpoint_height + storage_count
220+
if let Ok(Some(storage_height)) = self.storage.get_filter_tip_height().await {
221+
// The blockchain height is sync_base_height + storage_height
222+
state.sync_base_height + storage_height
254223
} else {
255-
// Normal sync from genesis
256-
// Check if filter headers are in storage but not loaded into memory yet
257-
if state.filter_headers.is_empty() {
258-
// Filter headers might be in storage but not loaded into ChainState yet
259-
if let Ok(Some(storage_height)) = self.storage.get_filter_tip_height().await {
260-
storage_height
261-
} else {
262-
// No filter headers in storage or ChainState
263-
0
264-
}
265-
} else {
266-
// Filter headers are loaded in ChainState
267-
state.filter_headers.len().saturating_sub(1) as u32
268-
}
224+
// No filter headers in storage yet
225+
state.sync_base_height
269226
}
270227
}
271228
}

0 commit comments

Comments
 (0)