Skip to content

Commit 598ca66

Browse files
authored
refactor: removed all use of dyn (#274)
1 parent a86c5b6 commit 598ca66

File tree

11 files changed

+37
-152
lines changed

11 files changed

+37
-152
lines changed

dash-spv/src/chain/fork_detector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ impl ForkDetector {
2929
}
3030

3131
/// Check if a header creates or extends a fork
32-
pub fn check_header(
32+
pub fn check_header<CS: ChainStorage>(
3333
&mut self,
3434
header: &BlockHeader,
3535
chain_state: &ChainState,
36-
storage: &dyn ChainStorage,
36+
storage: &CS,
3737
) -> ForkDetectionResult {
3838
let header_hash = header.block_hash();
3939
let prev_hash = header.prev_blockhash;

dash-spv/src/chain/reorg.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,22 @@ impl ReorgManager {
7878
}
7979

8080
/// Check if a fork has more work than the current chain and should trigger a reorg
81-
pub fn should_reorganize(
81+
pub fn should_reorganize<CS: ChainStorage>(
8282
&self,
8383
current_tip: &ChainTip,
8484
fork: &Fork,
85-
storage: &dyn ChainStorage,
85+
storage: &CS,
8686
) -> Result<bool, String> {
8787
self.should_reorganize_with_chain_state(current_tip, fork, storage, None)
8888
}
8989

9090
/// Check if a fork has more work than the current chain and should trigger a reorg
9191
/// This version is checkpoint-aware when chain_state is provided
92-
pub fn should_reorganize_with_chain_state(
92+
pub fn should_reorganize_with_chain_state<CS: ChainStorage>(
9393
&self,
9494
current_tip: &ChainTip,
9595
fork: &Fork,
96-
storage: &dyn ChainStorage,
96+
storage: &CS,
9797
chain_state: Option<&ChainState>,
9898
) -> Result<bool, String> {
9999
// Check if fork has more work
@@ -178,10 +178,10 @@ impl ReorgManager {
178178
}
179179

180180
/// Check if a block is chain-locked
181-
pub fn is_chain_locked(
181+
pub fn is_chain_locked<CS: ChainStorage>(
182182
&self,
183183
header: &BlockHeader,
184-
storage: &dyn ChainStorage,
184+
storage: &CS,
185185
) -> Result<bool, String> {
186186
if let Some(ref chain_lock_mgr) = self.chain_lock_manager {
187187
// Get the height of this header

dash-spv/src/network/tests.rs

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,5 @@
11
//! Unit tests for network module
22
3-
#[cfg(test)]
4-
mod peer_network_manager_tests {
5-
use crate::client::ClientConfig;
6-
use crate::network::manager::PeerNetworkManager;
7-
use crate::network::NetworkManager;
8-
use dashcore::Network;
9-
use std::time::Duration;
10-
use tempfile::TempDir;
11-
12-
fn create_test_config() -> ClientConfig {
13-
let temp_dir = TempDir::new().unwrap();
14-
ClientConfig {
15-
network: Network::Regtest,
16-
peers: vec!["127.0.0.1:19899".parse().unwrap()],
17-
restrict_to_configured_peers: false,
18-
storage_path: Some(temp_dir.path().to_path_buf()),
19-
validation_mode: crate::types::ValidationMode::Basic,
20-
filter_checkpoint_interval: 1000,
21-
max_headers_per_message: 2000,
22-
connection_timeout: Duration::from_secs(5),
23-
message_timeout: Duration::from_secs(30),
24-
sync_timeout: Duration::from_secs(60),
25-
enable_filters: false,
26-
enable_masternodes: false,
27-
max_peers: 3,
28-
enable_persistence: false,
29-
log_level: "info".to_string(),
30-
filter_request_delay_ms: 0,
31-
max_concurrent_filter_requests: 50,
32-
max_concurrent_cfheaders_requests_parallel: 50,
33-
cfheaders_request_timeout_secs: 30,
34-
max_cfheaders_retries: 3,
35-
// Mempool fields
36-
enable_mempool_tracking: false,
37-
mempool_strategy: crate::client::config::MempoolStrategy::BloomFilter,
38-
max_mempool_transactions: 1000,
39-
mempool_timeout_secs: 3600,
40-
fetch_mempool_transactions: true,
41-
persist_mempool: false,
42-
// Request control fields
43-
max_concurrent_headers_requests: None,
44-
max_concurrent_mnlist_requests: None,
45-
max_concurrent_cfheaders_requests: None,
46-
max_concurrent_block_requests: None,
47-
headers_request_rate_limit: None,
48-
mnlist_request_rate_limit: None,
49-
cfheaders_request_rate_limit: None,
50-
filters_request_rate_limit: None,
51-
blocks_request_rate_limit: None,
52-
start_from_height: None,
53-
wallet_creation_time: None,
54-
// QRInfo fields
55-
qr_info_extra_share: true,
56-
qr_info_timeout: Duration::from_secs(30),
57-
user_agent: None,
58-
}
59-
}
60-
61-
#[tokio::test]
62-
async fn test_as_any_downcast() {
63-
let config = create_test_config();
64-
let manager = PeerNetworkManager::new(&config).await.unwrap();
65-
66-
// Test that we can downcast through the trait
67-
let network_manager: &dyn NetworkManager = &manager;
68-
let downcasted = network_manager.as_any().downcast_ref::<PeerNetworkManager>();
69-
70-
assert!(downcasted.is_some());
71-
}
72-
}
73-
743
#[cfg(test)]
754
mod peer_tests {
765
use crate::network::peer::Peer;

dash-spv/src/storage/disk/state.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,6 @@ impl DiskStorageManager {
535535

536536
#[async_trait]
537537
impl StorageManager for DiskStorageManager {
538-
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
539-
self
540-
}
541-
542538
async fn store_headers(&mut self, headers: &[BlockHeader]) -> StorageResult<()> {
543539
self.store_headers(headers).await
544540
}

dash-spv/src/storage/memory.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ impl MemoryStorageManager {
5252

5353
#[async_trait]
5454
impl StorageManager for MemoryStorageManager {
55-
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
56-
self
57-
}
58-
5955
async fn store_headers(&mut self, headers: &[BlockHeader]) -> StorageResult<()> {
6056
let initial_count = self.headers.len();
6157
tracing::debug!(

dash-spv/src/storage/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub mod sync_storage;
77
pub mod types;
88

99
use async_trait::async_trait;
10-
use std::any::Any;
1110
use std::collections::HashMap;
1211
use std::ops::Range;
1312

@@ -101,8 +100,6 @@ pub trait ChainStorage: Send + Sync {
101100
/// at a time when using external synchronization, which naturally provides consistency.
102101
#[async_trait]
103102
pub trait StorageManager: Send + Sync {
104-
/// Convert to Any for downcasting
105-
fn as_any_mut(&mut self) -> &mut dyn Any;
106103
/// Store block headers.
107104
async fn store_headers(&mut self, headers: &[BlockHeader]) -> StorageResult<()>;
108105

@@ -242,14 +239,3 @@ pub trait StorageManager: Send + Sync {
242239
/// Shutdown the storage manager
243240
async fn shutdown(&mut self) -> StorageResult<()>;
244241
}
245-
246-
/// Helper trait to provide as_any_mut for all StorageManager implementations
247-
pub trait AsAnyMut {
248-
fn as_any_mut(&mut self) -> &mut dyn Any;
249-
}
250-
251-
impl<T: 'static> AsAnyMut for T {
252-
fn as_any_mut(&mut self) -> &mut dyn Any {
253-
self
254-
}
255-
}

dash-spv/src/sync/filters/retry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
264264
async fn handle_request_timeout(
265265
&mut self,
266266
range: (u32, u32),
267-
_network: &mut dyn NetworkManager,
267+
_network: &mut N,
268268
storage: &S,
269269
) -> SyncResult<()> {
270270
let (start, end) = range;

dash-spv/src/sync/headers/manager.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,10 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
256256
}
257257
}
258258

259-
// Use the internal storage method if available (DiskStorageManager optimization)
260-
if let Some(disk_storage) =
261-
storage.as_any_mut().downcast_mut::<crate::storage::disk::DiskStorageManager>()
262-
{
263-
disk_storage
264-
.store_headers(headers)
265-
.await
266-
.map_err(|e| SyncError::Storage(format!("Failed to store headers batch: {}", e)))?;
267-
} else {
268-
// Fallback to standard store_headers for other storage backends
269-
storage
270-
.store_headers(headers)
271-
.await
272-
.map_err(|e| SyncError::Storage(format!("Failed to store headers batch: {}", e)))?;
273-
}
259+
storage
260+
.store_headers(headers)
261+
.await
262+
.map_err(|e| SyncError::Storage(format!("Failed to store headers batch: {}", e)))?;
274263

275264
// Update Sync Progress
276265
let batch_size = headers.len() as u32;

dash-spv/src/sync/masternodes/manager.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
126126
/// Request QRInfo - simplified non-blocking implementation
127127
pub async fn request_qrinfo(
128128
&mut self,
129-
network: &mut dyn NetworkManager,
129+
network: &mut N,
130130
base_block_hash: BlockHash,
131131
block_hash: BlockHash,
132132
) -> Result<(), String> {
@@ -215,7 +215,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
215215
/// Make QRInfo P2P request (simplified non-blocking)
216216
async fn request_qr_info(
217217
&mut self,
218-
network: &mut dyn NetworkManager,
218+
network: &mut N,
219219
known_block_hashes: Vec<BlockHash>,
220220
block_request_hash: BlockHash,
221221
) -> Result<(), String> {
@@ -381,11 +381,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
381381
}
382382

383383
/// Start masternode synchronization
384-
pub async fn start_sync(
385-
&mut self,
386-
network: &mut dyn NetworkManager,
387-
storage: &mut S,
388-
) -> SyncResult<bool> {
384+
pub async fn start_sync(&mut self, network: &mut N, storage: &mut S) -> SyncResult<bool> {
389385
if self.sync_in_progress {
390386
return Err(SyncError::SyncInProgress);
391387
}
@@ -445,7 +441,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
445441
&mut self,
446442
diff: &MnListDiff,
447443
storage: &mut S,
448-
_network: &mut dyn NetworkManager,
444+
_network: &mut N,
449445
) -> SyncResult<bool> {
450446
self.insert_mn_list_diff(diff, storage).await;
451447

@@ -500,11 +496,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
500496
}
501497

502498
/// Check for sync timeout
503-
pub async fn check_sync_timeout(
504-
&mut self,
505-
storage: &mut dyn StorageManager,
506-
network: &mut dyn NetworkManager,
507-
) -> SyncResult<()> {
499+
pub async fn check_sync_timeout(&mut self, storage: &mut S, network: &mut N) -> SyncResult<()> {
508500
// Check if we're waiting for MnListDiff responses and have timed out
509501
if self.pending_mnlistdiff_requests > 0 {
510502
if let Some(wait_start) = self.mnlistdiff_wait_start {
@@ -625,7 +617,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
625617
&mut self,
626618
qr_info: QRInfo,
627619
storage: &mut S,
628-
network: &mut dyn NetworkManager,
620+
network: &mut N,
629621
) {
630622
self.log_qrinfo_details(&qr_info, "📋 Masternode sync processing QRInfo (unified path)");
631623

@@ -726,7 +718,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
726718
&mut self,
727719
qr_info: &QRInfo,
728720
storage: &mut S,
729-
network: &mut dyn NetworkManager,
721+
network: &mut N,
730722
) -> Result<(), String> {
731723
tracing::info!(
732724
"🔗 Feeding QRInfo to engine and getting additional diffs for quorum validation"
@@ -807,7 +799,7 @@ impl<S: StorageManager + Send + Sync + 'static, N: NetworkManager + Send + Sync
807799
&mut self,
808800
quorum_hashes: &std::collections::BTreeSet<QuorumHash>,
809801
storage: &mut S,
810-
network: &mut dyn NetworkManager,
802+
network: &mut N,
811803
) -> Result<(), String> {
812804
use dashcore::network::message::NetworkMessage;
813805
use dashcore::network::message_sml::GetMnListDiff;

dash-spv/src/sync/transitions.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ impl TransitionManager {
2323
}
2424

2525
/// Check if we can transition from current phase to target phase
26-
pub async fn can_transition_to(
26+
pub async fn can_transition_to<S: StorageManager>(
2727
&self,
2828
current_phase: &SyncPhase,
2929
target_phase: &SyncPhase,
30-
storage: &dyn StorageManager,
30+
storage: &S,
3131
) -> SyncResult<bool> {
3232
// Can't transition to the same phase
3333
if std::mem::discriminant(current_phase) == std::mem::discriminant(target_phase) {
@@ -168,11 +168,11 @@ impl TransitionManager {
168168
}
169169

170170
/// Get the next phase based on current phase and configuration
171-
pub async fn get_next_phase(
171+
pub async fn get_next_phase<S: StorageManager, N: NetworkManager>(
172172
&self,
173173
current_phase: &SyncPhase,
174-
storage: &dyn StorageManager,
175-
network: &dyn NetworkManager,
174+
storage: &S,
175+
network: &N,
176176
) -> SyncResult<Option<SyncPhase>> {
177177
match current_phase {
178178
SyncPhase::Idle => {
@@ -320,10 +320,10 @@ impl TransitionManager {
320320

321321
// Helper methods for checking phase completion
322322

323-
async fn are_headers_complete(
323+
async fn are_headers_complete<S: StorageManager>(
324324
&self,
325325
phase: &SyncPhase,
326-
_storage: &dyn StorageManager,
326+
_storage: &S,
327327
) -> SyncResult<bool> {
328328
if let SyncPhase::DownloadingHeaders {
329329
received_empty_response,
@@ -337,10 +337,10 @@ impl TransitionManager {
337337
}
338338
}
339339

340-
async fn are_masternodes_complete(
340+
async fn are_masternodes_complete<S: StorageManager>(
341341
&self,
342342
phase: &SyncPhase,
343-
storage: &dyn StorageManager,
343+
storage: &S,
344344
) -> SyncResult<bool> {
345345
if let SyncPhase::DownloadingMnList {
346346
current_height,
@@ -364,10 +364,10 @@ impl TransitionManager {
364364
}
365365
}
366366

367-
async fn are_cfheaders_complete(
367+
async fn are_cfheaders_complete<S: StorageManager>(
368368
&self,
369369
phase: &SyncPhase,
370-
_storage: &dyn StorageManager,
370+
_storage: &S,
371371
) -> SyncResult<bool> {
372372
if let SyncPhase::DownloadingCFHeaders {
373373
current_height,
@@ -413,9 +413,9 @@ impl TransitionManager {
413413
false
414414
}
415415

416-
async fn create_cfheaders_phase(
416+
async fn create_cfheaders_phase<S: StorageManager>(
417417
&self,
418-
storage: &dyn StorageManager,
418+
storage: &S,
419419
) -> SyncResult<Option<SyncPhase>> {
420420
let header_tip = storage
421421
.get_tip_height()
@@ -440,9 +440,9 @@ impl TransitionManager {
440440
}))
441441
}
442442

443-
async fn create_fully_synced_phase(
443+
async fn create_fully_synced_phase<S: StorageManager>(
444444
&self,
445-
_storage: &dyn StorageManager,
445+
_storage: &S,
446446
) -> SyncResult<Option<SyncPhase>> {
447447
Ok(Some(SyncPhase::FullySynced {
448448
sync_completed_at: Instant::now(),

0 commit comments

Comments
 (0)