Skip to content

Commit a7cb140

Browse files
refactor: improve error handling in storage height calculations
- Replaced `unwrap_or_default()` with `ok_or_else()` to provide more informative error messages when blockchain heights are at or below the checkpoint base. - Enhanced validation for various height calculations to ensure consistency and clarity in error reporting.
1 parent b2d4d3c commit a7cb140

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

dash-spv/src/sync/filters.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,12 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
500500
// Convert blockchain height to storage height
501501
let storage_height = self
502502
.header_abs_to_storage_index(next_batch_end_height)
503-
.unwrap_or_else(|| {
504-
tracing::warn!(
503+
.ok_or_else(|| {
504+
SyncError::Validation(format!(
505505
"next_batch_end_height {} is at or before checkpoint base {}",
506-
next_batch_end_height,
507-
self.sync_base_height
508-
);
509-
// Fallback to current_sync_height
510-
self.header_abs_to_storage_index(self.current_sync_height)
511-
.unwrap_or_default()
512-
});
506+
next_batch_end_height, self.sync_base_height
507+
))
508+
})?;
513509
match storage.get_header(storage_height).await {
514510
Ok(Some(header)) => header.block_hash(),
515511
Ok(None) => {
@@ -611,8 +607,14 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
611607
} else {
612608
// Special handling for chain tip: if we can't find the exact tip header,
613609
// try the previous header as we might be at the actual chain tip
614-
let tip_storage_height =
615-
self.header_abs_to_storage_index(header_tip_height).unwrap_or_default();
610+
let tip_storage_height = self
611+
.header_abs_to_storage_index(header_tip_height)
612+
.ok_or_else(|| {
613+
SyncError::Validation(format!(
614+
"header_tip_height {} below checkpoint base {}",
615+
header_tip_height, self.sync_base_height
616+
))
617+
})?;
616618
match storage.get_header(tip_storage_height).await {
617619
Ok(Some(header)) => header.block_hash(),
618620
Ok(None) if header_tip_height > 0 => {
@@ -624,7 +626,13 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
624626
// Try previous header when at chain tip
625627
let prev_storage_height = self
626628
.header_abs_to_storage_index(header_tip_height - 1)
627-
.unwrap_or_default();
629+
.ok_or_else(|| {
630+
SyncError::Validation(format!(
631+
"prev header height {} below checkpoint base {}",
632+
header_tip_height - 1,
633+
self.sync_base_height
634+
))
635+
})?;
628636
storage
629637
.get_header(prev_storage_height)
630638
.await
@@ -1567,7 +1575,12 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
15671575
let batch_end = (current_height + batch_size - 1).min(end);
15681576

15691577
// Get stop hash for this batch - convert blockchain height to storage index
1570-
let storage_height = self.header_abs_to_storage_index(batch_end).unwrap_or_default();
1578+
let storage_height = self.header_abs_to_storage_index(batch_end).ok_or_else(|| {
1579+
SyncError::Validation(format!(
1580+
"batch_end {} is at or before checkpoint base {}",
1581+
batch_end, self.sync_base_height
1582+
))
1583+
})?;
15711584
let stop_hash = storage
15721585
.get_header(storage_height)
15731586
.await
@@ -1894,7 +1907,12 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
18941907
}
18951908

18961909
// Calculate stop hash for retry - convert blockchain height to storage index
1897-
let storage_height = self.header_abs_to_storage_index(end).unwrap_or_default();
1910+
let storage_height = self.header_abs_to_storage_index(end).ok_or_else(|| {
1911+
SyncError::Validation(format!(
1912+
"retry end {} is at or before checkpoint base {}",
1913+
end, self.sync_base_height
1914+
))
1915+
})?;
18981916
match storage.get_header(storage_height).await {
18991917
Ok(Some(header)) => {
19001918
let stop_hash = header.block_hash();
@@ -3333,7 +3351,12 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
33333351
}
33343352

33353353
// Calculate stop hash for this range - convert blockchain height to storage index
3336-
let storage_height = self.header_abs_to_storage_index(end).unwrap_or_default();
3354+
let storage_height = self.header_abs_to_storage_index(end).ok_or_else(|| {
3355+
SyncError::Validation(format!(
3356+
"retry range end {} is at or before checkpoint base {}",
3357+
end, self.sync_base_height
3358+
))
3359+
})?;
33373360
match storage.get_header(storage_height).await {
33383361
Ok(Some(header)) => {
33393362
let stop_hash = header.block_hash();
@@ -3370,7 +3393,12 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
33703393

33713394
// Get stop hash for this batch - convert blockchain height to storage index
33723395
let batch_storage_height =
3373-
self.header_abs_to_storage_index(batch_end).unwrap_or_default();
3396+
self.header_abs_to_storage_index(batch_end).ok_or_else(|| {
3397+
SyncError::Validation(format!(
3398+
"retry batch_end {} is at or before checkpoint base {}",
3399+
batch_end, self.sync_base_height
3400+
))
3401+
})?;
33743402
match storage.get_header(batch_storage_height).await {
33753403
Ok(Some(batch_header)) => {
33763404
let batch_stop_hash = batch_header.block_hash();

0 commit comments

Comments
 (0)