solana-recover 1.1.3

A comprehensive Solana wallet recovery and account management tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Unified Scanner Architecture
//! 
//! This module consolidates all scanner functionality into a single, cohesive architecture
//! using the strategy pattern to eliminate code duplication and improve maintainability.

use crate::core::{Result, SolanaRecoverError, ScanResult, BatchScanRequest, BatchScanResult};
use crate::rpc::{ConnectionPoolTrait};
use crate::utils::cache::{CacheTrait, MetricsTrait};
use std::sync::Arc;
use std::time::Instant;
use chrono::Utc;
use tracing::{info};
use serde::{Deserialize, Serialize};
use async_trait::async_trait;

/// Performance modes for scanning strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PerformanceMode {
    /// Maximum speed with highest resource usage
    UltraFast,
    /// Balanced performance and resource usage
    Balanced,
    /// Optimized for minimal resource consumption
    ResourceEfficient,
    /// Optimized for high throughput
    Throughput,
    /// Optimized for minimal latency
    Latency,
}

impl Default for PerformanceMode {
    fn default() -> Self {
        PerformanceMode::Balanced
    }
}

/// Configuration for the unified scanner
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnifiedScannerConfig {
    /// Performance mode
    pub performance_mode: PerformanceMode,
    
    /// Maximum concurrent scans
    pub max_concurrent_scans: usize,
    
    /// Scan timeout
    pub scan_timeout: std::time::Duration,
    
    /// Batch size for operations
    pub batch_size: usize,
    
    /// Enable optimizations
    pub enable_optimizations: bool,
    
    /// Enable caching
    pub enable_caching: bool,
    
    /// Enable parallel processing
    pub enable_parallel_processing: bool,
}

impl Default for UnifiedScannerConfig {
    fn default() -> Self {
        Self {
            performance_mode: PerformanceMode::Balanced,
            max_concurrent_scans: 100,
            scan_timeout: std::time::Duration::from_secs(30),
            batch_size: 50,
            enable_optimizations: true,
            enable_caching: true,
            enable_parallel_processing: true,
        }
    }
}

impl UnifiedScannerConfig {
    /// Validate the configuration
    pub fn validate(&self) -> Result<()> {
        if self.max_concurrent_scans == 0 {
            return Err(SolanaRecoverError::ConfigError("max_concurrent_scans must be greater than 0".to_string()));
        }
        
        if self.batch_size == 0 {
            return Err(SolanaRecoverError::ConfigError("batch_size must be greater than 0".to_string()));
        }
        
        if self.scan_timeout.as_secs() == 0 {
            return Err(SolanaRecoverError::ConfigError("scan_timeout must be greater than 0 seconds".to_string()));
        }
        
        Ok(())
    }
}

/// Strategy trait for different scanning approaches
#[async_trait]
pub trait ScanStrategy: Send + Sync {
    /// Scan a single wallet
    async fn scan_wallet(&self, wallet_address: &str, context: &ScanContext) -> Result<ScanResult>;
    
    /// Scan multiple wallets in batch
    async fn scan_batch(&self, request: &BatchScanRequest, context: &ScanContext) -> Result<BatchScanResult>;
    
    /// Strategy name for identification
    fn name(&self) -> &str;
    
    /// Strategy priority (higher = more preferred)
    fn priority(&self) -> u8;
    
    /// Check if strategy supports the given performance mode
    fn supports_mode(&self, mode: &PerformanceMode) -> bool;
}

/// Context provided to scanning strategies
#[derive(Clone)]
pub struct ScanContext {
    /// Connection pool for RPC calls
    pub connection_pool: Arc<dyn ConnectionPoolTrait>,
    
    /// Scanner configuration
    pub config: UnifiedScannerConfig,
    
    /// Cache instance (optional)
    pub cache: Option<Arc<dyn CacheTrait>>,
    
    /// Metrics collector (optional)
    pub metrics: Option<Arc<dyn MetricsTrait>>,
}

/// Ultra-fast scanning strategy implementation
pub struct UltraFastStrategy;

impl UltraFastStrategy {
    pub fn new() -> Self {
        Self
    }
}

#[async_trait]
impl ScanStrategy for UltraFastStrategy {
    async fn scan_wallet(&self, wallet_address: &str, context: &ScanContext) -> Result<ScanResult> {
        use crate::core::scanner::WalletScanner;
        
        let start_time = Instant::now();
        
        info!("Starting ultra-fast scan for wallet: {}", wallet_address);
        
        // Use basic scanner with ultra-fast configuration
        let scanner = WalletScanner::new(context.connection_pool.clone());
        let scan_result = scanner.scan_wallet(wallet_address).await?;
        
        let duration = start_time.elapsed();
        info!("Ultra-fast scan completed in {}ms", duration.as_millis());
        
        Ok(scan_result)
    }
    
    async fn scan_batch(&self, request: &BatchScanRequest, context: &ScanContext) -> Result<BatchScanResult> {
        let start_time = Instant::now();
        
        info!("Starting ultra-fast batch scan for {} wallets", request.wallet_addresses.len());
        
        // Implement ultra-fast batch scanning
        let mut results = Vec::new();
        
        for wallet_address in &request.wallet_addresses {
            let scan_result = self.scan_wallet(wallet_address, context).await?;
            results.push(scan_result);
        }
        
        let duration = start_time.elapsed();
        info!("Ultra-fast batch scan completed in {}ms", duration.as_millis());
        
        Ok(BatchScanResult {
            request_id: request.id,
            batch_id: None,
            results,
            total_wallets: request.wallet_addresses.len(),
            successful_scans: request.wallet_addresses.len(),
            failed_scans: 0,
            completed_wallets: request.wallet_addresses.len(),
            failed_wallets: 0,
            total_recoverable_sol: 0.0,
            estimated_fee_sol: 0.0,
            created_at: Utc::now(),
            completed_at: Some(Utc::now()),
            duration_ms: Some(duration.as_millis() as u64),
            scan_time_ms: duration.as_millis() as u64,
        })
    }
    
    fn name(&self) -> &str {
        "UltraFast"
    }
    
    fn priority(&self) -> u8 {
        100 // Highest priority
    }
    
    fn supports_mode(&self, mode: &PerformanceMode) -> bool {
        matches!(mode, PerformanceMode::UltraFast | PerformanceMode::Throughput)
    }
}

/// Balanced scanning strategy implementation
pub struct BalancedStrategy;

#[async_trait]
impl ScanStrategy for BalancedStrategy {
    async fn scan_wallet(&self, wallet_address: &str, context: &ScanContext) -> Result<ScanResult> {
        use crate::core::scanner::WalletScanner;
        
        let start_time = Instant::now();
        
        info!("Starting balanced scan for wallet: {}", wallet_address);
        
        // Use basic scanner with balanced configuration
        let scanner = WalletScanner::new(context.connection_pool.clone());
        let scan_result = scanner.scan_wallet(wallet_address).await?;
        
        let duration = start_time.elapsed();
        info!("Balanced scan completed in {}ms", duration.as_millis());
        
        Ok(scan_result)
    }
    
    async fn scan_batch(&self, request: &BatchScanRequest, context: &ScanContext) -> Result<BatchScanResult> {
        let start_time = Instant::now();
        
        info!("Starting balanced batch scan for {} wallets", request.wallet_addresses.len());
        
        let mut results = Vec::new();
        
        for wallet_address in &request.wallet_addresses {
            let scan_result = self.scan_wallet(wallet_address, context).await?;
            results.push(scan_result);
        }
        
        let duration = start_time.elapsed();
        info!("Balanced batch scan completed in {}ms", duration.as_millis());
        
        Ok(BatchScanResult {
            request_id: request.id,
            batch_id: None,
            results,
            total_wallets: request.wallet_addresses.len(),
            successful_scans: request.wallet_addresses.len(),
            failed_scans: 0,
            completed_wallets: request.wallet_addresses.len(),
            failed_wallets: 0,
            total_recoverable_sol: 0.0,
            estimated_fee_sol: 0.0,
            created_at: Utc::now(),
            completed_at: Some(Utc::now()),
            duration_ms: Some(duration.as_millis() as u64),
            scan_time_ms: duration.as_millis() as u64,
        })
    }
    
    fn name(&self) -> &str {
        "Balanced"
    }
    
    fn priority(&self) -> u8 {
        50
    }
    
    fn supports_mode(&self, mode: &PerformanceMode) -> bool {
        matches!(mode, PerformanceMode::Balanced)
    }
}

/// Resource-efficient scanning strategy implementation
pub struct ResourceEfficientStrategy;

#[async_trait]
impl ScanStrategy for ResourceEfficientStrategy {
    async fn scan_wallet(&self, wallet_address: &str, context: &ScanContext) -> Result<ScanResult> {
        use crate::core::scanner::WalletScanner;
        
        let start_time = Instant::now();
        
        info!("Starting resource-efficient scan for wallet: {}", wallet_address);
        
        // Use basic scanner for resource efficiency
        let scanner = WalletScanner::new(context.connection_pool.clone());
        let scan_result = scanner.scan_wallet(wallet_address).await?;
        
        let duration = start_time.elapsed();
        info!("Resource-efficient scan completed in {}ms", duration.as_millis());
        
        Ok(scan_result)
    }
    
    async fn scan_batch(&self, request: &BatchScanRequest, context: &ScanContext) -> Result<BatchScanResult> {
        let start_time = Instant::now();
        
        info!("Starting resource-efficient batch scan for {} wallets", request.wallet_addresses.len());
        
        let mut results = Vec::new();
        
        for wallet_address in &request.wallet_addresses {
            let scan_result = self.scan_wallet(wallet_address, context).await?;
            results.push(scan_result);
        }
        
        let duration = start_time.elapsed();
        info!("Resource-efficient batch scan completed in {}ms", duration.as_millis());
        
        Ok(BatchScanResult {
            request_id: request.id,
            batch_id: None,
            results,
            total_wallets: request.wallet_addresses.len(),
            successful_scans: request.wallet_addresses.len(),
            failed_scans: 0,
            completed_wallets: request.wallet_addresses.len(),
            failed_wallets: 0,
            total_recoverable_sol: 0.0,
            estimated_fee_sol: 0.0,
            created_at: Utc::now(),
            completed_at: Some(Utc::now()),
            duration_ms: Some(duration.as_millis() as u64),
            scan_time_ms: duration.as_millis() as u64,
        })
    }
    
    fn name(&self) -> &str {
        "ResourceEfficient"
    }
    
    fn priority(&self) -> u8 {
        25
    }
    
    fn supports_mode(&self, mode: &PerformanceMode) -> bool {
        matches!(mode, PerformanceMode::ResourceEfficient)
    }
}

/// Unified wallet scanner with pluggable strategies
pub struct UnifiedWalletScanner {
    /// Core scanning engine
    core: ScannerCore,
    
    /// Available scanning strategies
    strategies: Vec<Arc<dyn ScanStrategy>>,
    
    /// Scanner configuration
    config: UnifiedScannerConfig,
    
    /// Currently active strategy
    active_strategy: Option<Arc<dyn ScanStrategy>>,
}

/// Core scanning functionality shared across strategies
struct ScannerCore {
    connection_pool: Arc<dyn ConnectionPoolTrait>,
    cache: Option<Arc<dyn CacheTrait>>,
    metrics: Option<Arc<dyn MetricsTrait>>,
}

impl UnifiedWalletScanner {
    /// Create a new unified scanner with default strategies
    pub fn new(connection_pool: Arc<dyn ConnectionPoolTrait>, config: UnifiedScannerConfig) -> Self {
        let core = ScannerCore {
            connection_pool: connection_pool.clone(),
            cache: None,
            metrics: None,
        };
        
        // Initialize default strategies
        let strategies: Vec<Arc<dyn ScanStrategy>> = vec![
            Arc::new(UltraFastStrategy::new()),
            Arc::new(BalancedStrategy),
            Arc::new(ResourceEfficientStrategy),
        ];
        
        // Select active strategy based on performance mode
        let active_strategy = Self::select_strategy_for_mode(&config.performance_mode, &strategies);
        
        Self {
            core,
            strategies,
            config,
            active_strategy,
        }
    }
    
    /// Select the best strategy for the given performance mode
    fn select_strategy_for_mode(mode: &PerformanceMode, strategies: &[Arc<dyn ScanStrategy>]) -> Option<Arc<dyn ScanStrategy>> {
        strategies
            .iter()
            .filter(|strategy| strategy.supports_mode(mode))
            .max_by_key(|strategy| strategy.priority())
            .cloned()
    }
    
    /// Scan a single wallet using the active strategy
    pub async fn scan_wallet(&self, wallet_address: &str) -> Result<ScanResult> {
        let strategy = self.active_strategy.as_ref()
            .ok_or_else(|| SolanaRecoverError::InternalError("No active strategy configured".to_string()))?;
        
        let context = ScanContext {
            connection_pool: self.core.connection_pool.clone(),
            config: self.config.clone(),
            cache: self.core.cache.clone(),
            metrics: self.core.metrics.clone(),
        };
        
        strategy.scan_wallet(wallet_address, &context).await
    }
    
    /// Scan multiple wallets in batch
    pub async fn scan_batch(&self, request: &BatchScanRequest) -> Result<BatchScanResult> {
        let strategy = self.active_strategy.as_ref()
            .ok_or_else(|| SolanaRecoverError::InternalError("No active strategy configured".to_string()))?;
        
        let context = ScanContext {
            connection_pool: self.core.connection_pool.clone(),
            config: self.config.clone(),
            cache: self.core.cache.clone(),
            metrics: self.core.metrics.clone(),
        };
        
        strategy.scan_batch(request, &context).await
    }
    
    /// Change the performance mode and update active strategy
    pub fn set_performance_mode(&mut self, mode: PerformanceMode) -> Result<()> {
        self.config.performance_mode = mode.clone();
        self.active_strategy = Self::select_strategy_for_mode(&mode, &self.strategies);
        
        if self.active_strategy.is_none() {
            return Err(SolanaRecoverError::InternalError(
                format!("No strategy supports performance mode: {:?}", mode)
            ));
        }
        
        Ok(())
    }
    
    /// Get the currently active strategy name
    pub fn active_strategy_name(&self) -> Option<&str> {
        self.active_strategy.as_ref().map(|strategy| strategy.name())
    }
    
    /// List all available strategies
    pub fn available_strategies(&self) -> Vec<&str> {
        self.strategies.iter().map(|strategy| strategy.name()).collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_performance_mode_default() {
        let config = UnifiedScannerConfig::default();
        assert!(matches!(config.performance_mode, PerformanceMode::Balanced));
    }
    
    #[test]
    fn test_strategy_priorities() {
        let ultra_fast = UltraFastStrategy::new();
        let balanced = BalancedStrategy;
        let resource_efficient = ResourceEfficientStrategy;
        
        assert!(ultra_fast.priority() > balanced.priority());
        assert!(balanced.priority() > resource_efficient.priority());
    }
    
    #[test]
    fn test_strategy_mode_support() {
        let ultra_fast = UltraFastStrategy::new();
        let balanced = BalancedStrategy;
        let resource_efficient = ResourceEfficientStrategy;
        
        assert!(ultra_fast.supports_mode(&PerformanceMode::UltraFast));
        assert!(ultra_fast.supports_mode(&PerformanceMode::Throughput));
        assert!(!ultra_fast.supports_mode(&PerformanceMode::Balanced));
        
        assert!(balanced.supports_mode(&PerformanceMode::Balanced));
        assert!(!balanced.supports_mode(&PerformanceMode::UltraFast));
        
        assert!(resource_efficient.supports_mode(&PerformanceMode::ResourceEfficient));
        assert!(!resource_efficient.supports_mode(&PerformanceMode::Balanced));
    }
}