Skip to content

Commit 3254ac4

Browse files
authored
refactor: Height based storage (#272)
* removed the sync_base_height from the segmentCache struct * tip height calculation updated to consider the sentinel block existence * rolledback next_height method removal * unit test updated to fit the new logic * updated other tests to fit the new api requirements * clippy warning fixed * updated test to work with teh new api behaviour * removed file * comments improved
1 parent f9234fd commit 3254ac4

File tree

6 files changed

+199
-220
lines changed

6 files changed

+199
-220
lines changed

dash-spv/src/storage/headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl DiskStorageManager {
2020
) -> StorageResult<()> {
2121
let hashes = headers.iter().map(|header| header.block_hash()).collect::<Vec<_>>();
2222

23-
self.block_headers.write().await.store_items_at_height(headers, height, self).await?;
23+
self.block_headers.write().await.store_items(headers, height, self).await?;
2424

2525
// Update reverse index
2626
let mut reverse_index = self.header_hash_index.write().await;

dash-spv/src/storage/manager.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Core DiskStorageManager struct and background worker implementation.
22
33
use std::collections::HashMap;
4-
use std::io::Result;
54
use std::path::PathBuf;
65
use std::sync::Arc;
76
use tokio::sync::{mpsc, RwLock};
@@ -85,34 +84,15 @@ impl DiskStorageManager {
8584
StorageError::WriteFailed(format!("Failed to create state directory: {}", e))
8685
})?;
8786

88-
// Temporary fix to load the sync base height if we have data already persisted
89-
let sync_base_height =
90-
load_sync_base_height_if_persisted(base_path.join("state/chain.json"))
91-
.await
92-
.unwrap_or(0);
93-
94-
async fn load_sync_base_height_if_persisted(path: PathBuf) -> Result<u32> {
95-
let content = tokio::fs::read_to_string(path).await?;
96-
let value: serde_json::Value = serde_json::from_str(&content)?;
97-
98-
Ok(value
99-
.get("sync_base_height")
100-
.and_then(|v| v.as_u64())
101-
.map(|h| h as u32)
102-
.unwrap_or(0))
103-
}
104-
10587
let mut storage = Self {
10688
base_path: base_path.clone(),
10789
block_headers: Arc::new(RwLock::new(
108-
SegmentCache::load_or_new(base_path.clone(), sync_base_height).await?,
90+
SegmentCache::load_or_new(base_path.clone()).await?,
10991
)),
11092
filter_headers: Arc::new(RwLock::new(
111-
SegmentCache::load_or_new(base_path.clone(), sync_base_height).await?,
112-
)),
113-
filters: Arc::new(RwLock::new(
114-
SegmentCache::load_or_new(base_path.clone(), sync_base_height).await?,
93+
SegmentCache::load_or_new(base_path.clone()).await?,
11594
)),
95+
filters: Arc::new(RwLock::new(SegmentCache::load_or_new(base_path.clone()).await?)),
11696
header_hash_index: Arc::new(RwLock::new(HashMap::new())),
11797
worker_tx: None,
11898
worker_handle: None,
@@ -124,9 +104,6 @@ impl DiskStorageManager {
124104

125105
// Load chain state to get sync_base_height
126106
if let Ok(Some(state)) = storage.load_chain_state().await {
127-
storage.filter_headers.write().await.set_sync_base_height(state.sync_base_height);
128-
storage.block_headers.write().await.set_sync_base_height(state.sync_base_height);
129-
storage.filters.write().await.set_sync_base_height(state.sync_base_height);
130107
tracing::debug!("Loaded sync_base_height: {}", state.sync_base_height);
131108
}
132109

0 commit comments

Comments
 (0)