Skip to content

Commit 165c578

Browse files
clear filters
1 parent 97ff4a6 commit 165c578

File tree

9 files changed

+92
-9
lines changed

9 files changed

+92
-9
lines changed

dash-spv/src/client/block_processor.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
180180
let matches =
181181
wallet.check_compact_filter(&filter, &block_hash, self.network).await;
182182

183-
184183
if matches {
185184
tracing::info!("🎯 Compact filter matched for block {}", block_hash);
186185
drop(wallet);
@@ -189,7 +188,11 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
189188
hash: block_hash.to_string(),
190189
});
191190
} else {
192-
tracing::debug!("Compact filter did not match for block {}, {}", block_hash, wallet.describe(self.network).await);
191+
tracing::debug!(
192+
"Compact filter did not match for block {}, {}",
193+
block_hash,
194+
wallet.describe(self.network).await
195+
);
193196
drop(wallet);
194197
}
195198

dash-spv/src/client/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,42 @@ impl<
119119
storage.clear_sync_state().await.map_err(SpvError::Storage)
120120
}
121121

122+
/// Clear all stored filter headers and compact filters while keeping other data intact.
123+
pub async fn clear_filters(&mut self) -> Result<()> {
124+
{
125+
let mut storage = self.storage.lock().await;
126+
storage.clear_filters().await.map_err(SpvError::Storage)?;
127+
}
128+
129+
// Reset in-memory chain state for filters
130+
{
131+
let mut state = self.state.write().await;
132+
state.filter_headers.clear();
133+
state.current_filter_tip = None;
134+
}
135+
136+
// Reset filter sync manager tracking
137+
self.sync_manager.filter_sync_mut().clear_filter_state().await;
138+
139+
// Reset filter-related statistics
140+
let received_heights = {
141+
let stats = self.stats.read().await;
142+
stats.received_filter_heights.clone()
143+
};
144+
145+
{
146+
let mut stats = self.stats.write().await;
147+
stats.filter_headers_downloaded = 0;
148+
stats.filter_height = 0;
149+
stats.filters_downloaded = 0;
150+
stats.filters_received = 0;
151+
}
152+
153+
received_heights.lock().await.clear();
154+
155+
Ok(())
156+
}
157+
122158
/// Take the progress receiver for external consumption.
123159
pub fn take_progress_receiver(
124160
&mut self,

dash-spv/src/storage/disk.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,27 @@ impl StorageManager for DiskStorageManager {
16311631
Ok(())
16321632
}
16331633

1634+
async fn clear_filters(&mut self) -> StorageResult<()> {
1635+
// Stop worker to prevent concurrent writes to filter directories
1636+
self.stop_worker().await;
1637+
1638+
// Clear in-memory filter state
1639+
self.active_filter_segments.write().await.clear();
1640+
*self.cached_filter_tip_height.write().await = None;
1641+
1642+
// Remove filter headers and compact filter files
1643+
let filters_dir = self.base_path.join("filters");
1644+
if filters_dir.exists() {
1645+
tokio::fs::remove_dir_all(&filters_dir).await?;
1646+
}
1647+
tokio::fs::create_dir_all(&filters_dir).await?;
1648+
1649+
// Restart background worker for future operations
1650+
self.start_worker().await;
1651+
1652+
Ok(())
1653+
}
1654+
16341655
async fn stats(&self) -> StorageResult<StorageStats> {
16351656
let mut component_sizes = HashMap::new();
16361657
let mut total_size = 0u64;

dash-spv/src/storage/memory.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ impl StorageManager for MemoryStorageManager {
307307
Ok(())
308308
}
309309

310+
async fn clear_filters(&mut self) -> StorageResult<()> {
311+
self.filter_headers.clear();
312+
self.filters.clear();
313+
Ok(())
314+
}
315+
310316
async fn stats(&self) -> StorageResult<StorageStats> {
311317
let mut component_sizes = HashMap::new();
312318

dash-spv/src/storage/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ pub trait StorageManager: Send + Sync {
154154
/// Clear all data.
155155
async fn clear(&mut self) -> StorageResult<()>;
156156

157+
/// Clear all filter headers and compact filters.
158+
async fn clear_filters(&mut self) -> StorageResult<()>;
159+
157160
/// Get storage statistics.
158161
async fn stats(&self) -> StorageResult<StorageStats>;
159162

dash-spv/src/sync/filters.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,4 +3466,12 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
34663466
self.last_sync_progress = std::time::Instant::now();
34673467
tracing::debug!("Reset filter sync pending requests");
34683468
}
3469+
3470+
/// Fully clear filter tracking state, including received heights.
3471+
pub async fn clear_filter_state(&mut self) {
3472+
self.reset_pending_requests();
3473+
let mut heights = self.received_filter_heights.lock().await;
3474+
heights.clear();
3475+
tracing::info!("Cleared filter sync state and received heights");
3476+
}
34693477
}

dash-spv/tests/error_handling_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ impl StorageManager for MockStorageManager {
380380
Ok(())
381381
}
382382

383+
async fn clear_filters(&mut self) -> StorageResult<()> {
384+
if self.fail_on_write {
385+
return Err(StorageError::WriteFailed("Mock write failure".to_string()));
386+
}
387+
Ok(())
388+
}
389+
383390
async fn stats(&self) -> StorageResult<dash_spv::storage::StorageStats> {
384391
Ok(dash_spv::storage::StorageStats {
385392
header_count: 0,

dash-spv/tests/error_recovery_integration_test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ impl StorageManager for MockStorageManager {
655655
Ok(())
656656
}
657657

658+
async fn clear_filters(&mut self) -> dash_spv::error::StorageResult<()> {
659+
Ok(())
660+
}
661+
658662
async fn stats(&self) -> dash_spv::error::StorageResult<dash_spv::storage::StorageStats> {
659663
Ok(dash_spv::storage::StorageStats {
660664
header_count: 0,

key-wallet-manager/src/wallet_manager/process_block.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::wallet_interface::WalletInterface;
22
use crate::{Network, WalletManager};
33
use alloc::string::String;
44
use alloc::vec::Vec;
5-
use core::fmt::Write as _;
65
use async_trait::async_trait;
6+
use core::fmt::Write as _;
77
use dashcore::bip158::BlockFilter;
88
use dashcore::prelude::CoreBlockHeight;
99
use dashcore::{Block, BlockHash, Transaction, Txid};
@@ -161,11 +161,6 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletInterface for WalletM
161161
details.push(format!("{} ({}): {}", name, wallet_id_hex, summary));
162162
}
163163

164-
format!(
165-
"WalletManager: {} wallet(s) on {}\n{}",
166-
wallet_count,
167-
network,
168-
details.join("\n")
169-
)
164+
format!("WalletManager: {} wallet(s) on {}\n{}", wallet_count, network, details.join("\n"))
170165
}
171166
}

0 commit comments

Comments
 (0)