Skip to content

Commit 4e82105

Browse files
refactor: update memory storage height calculations for consistency with checkpoint sync
- Adjusted absolute height calculations in MemoryStorageManager to be base-inclusive. - Updated comments for clarity on how storage indexing relates to absolute blockchain height. - Ensured consistency in height retrieval methods to align with DiskStorage behavior.
1 parent fab0f61 commit 4e82105

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

dash-spv/src/storage/memory.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ impl StorageManager for MemoryStorageManager {
5858

5959
// Determine absolute height offset (for checkpoint-based sync) once per batch
6060
// If syncing from a checkpoint, storage index 0 corresponds to absolute height
61-
// sync_base_height + 1. Otherwise, absolute height equals storage index.
61+
// sync_base_height (base-inclusive). Otherwise, absolute height equals storage index.
6262
let (sync_base_height, synced_from_checkpoint) = match self.load_sync_state().await {
6363
Ok(Some(state)) => (state.sync_base_height, state.synced_from_checkpoint),
6464
_ => (0u32, false),
6565
};
6666
let abs_offset: u32 = if synced_from_checkpoint && sync_base_height > 0 {
67-
sync_base_height + 1
67+
sync_base_height
6868
} else {
6969
0
7070
};
@@ -323,7 +323,21 @@ impl StorageManager for MemoryStorageManager {
323323
}
324324

325325
async fn get_header_height_by_hash(&self, hash: &BlockHash) -> StorageResult<Option<u32>> {
326-
Ok(self.header_hash_index.get(hash).copied())
326+
// Return ABSOLUTE blockchain height for consistency with DiskStorage.
327+
// memory.header_hash_index stores storage index; convert to absolute height using base.
328+
let storage_index = match self.header_hash_index.get(hash).copied() {
329+
Some(idx) => idx,
330+
None => return Ok(None),
331+
};
332+
333+
let base = match self.load_sync_state().await {
334+
Ok(Some(state)) if state.synced_from_checkpoint && state.sync_base_height > 0 => {
335+
state.sync_base_height
336+
}
337+
_ => 0u32,
338+
};
339+
340+
Ok(Some(base + storage_index))
327341
}
328342

329343
async fn get_headers_batch(

dash-spv/src/sync/filters.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,26 +218,27 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
218218
}
219219

220220
/// Convert absolute blockchain height to block header storage index.
221-
/// In checkpoint sync, headers storage starts at (base + 1).
221+
/// Storage indexing is base-inclusive: at checkpoint base B, storage index 0 == absolute height B.
222222
fn header_abs_to_storage_index(&self, height: u32) -> Option<u32> {
223223
if self.sync_base_height > 0 {
224-
height.checked_sub(self.sync_base_height + 1)
224+
height.checked_sub(self.sync_base_height)
225225
} else {
226226
Some(height)
227227
}
228228
}
229229

230230
/// Convert block header storage index to absolute blockchain height.
231+
/// Storage indexing is base-inclusive: at checkpoint base B, absolute height == B + index.
231232
fn header_storage_to_abs_height(&self, index: u32) -> u32 {
232233
if self.sync_base_height > 0 {
233-
self.sync_base_height + 1 + index
234+
self.sync_base_height + index
234235
} else {
235236
index
236237
}
237238
}
238239

239240
/// Convert absolute blockchain height to filter header storage index.
240-
/// In checkpoint sync, filter headers storage starts at base (checkpoint height).
241+
/// Storage indexing is base-inclusive for filter headers as well.
241242
fn filter_abs_to_storage_index(&self, height: u32) -> Option<u32> {
242243
if self.sync_base_height > 0 {
243244
height.checked_sub(self.sync_base_height)
@@ -2062,18 +2063,16 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
20622063
start_height: u32,
20632064
end_height: u32,
20642065
) -> SyncResult<Option<u32>> {
2065-
// Use the efficient reverse index first. Note: storage returns STORAGE height (index),
2066-
// we must convert it to BLOCKCHAIN (absolute) height when comparing with [start,end].
2067-
if let Some(storage_height) =
2066+
// Use the efficient reverse index first.
2067+
// Contract: StorageManager::get_header_height_by_hash returns ABSOLUTE blockchain height.
2068+
if let Some(abs_height) =
20682069
storage.get_header_height_by_hash(block_hash).await.map_err(|e| {
20692070
SyncError::Storage(format!("Failed to get header height by hash: {}", e))
20702071
})?
20712072
{
2072-
// Convert storage-relative height to blockchain height (accounts for checkpoint base)
2073-
let absolute_height = self.header_storage_to_abs_height(storage_height);
20742073
// Check if the absolute height is within the requested range
2075-
if absolute_height >= start_height && absolute_height <= end_height {
2076-
return Ok(Some(absolute_height));
2074+
if abs_height >= start_height && abs_height <= end_height {
2075+
return Ok(Some(abs_height));
20772076
}
20782077
}
20792078
Ok(None)

0 commit comments

Comments
 (0)