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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
//! Configuration Management System
//! 
//! This module provides comprehensive configuration management with support for
//! environment-specific configs, validation, hot-reload, and external configuration sources.

use crate::core::{Result, SolanaRecoverError};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::fs;
use std::env;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{info, error};
use std::time::{Duration, Instant, SystemTime};
use std::collections::HashMap;

/// Environment types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Environment {
    Development,
    Testing,
    Staging,
    Production,
}

impl Default for Environment {
    fn default() -> Self {
        Environment::Development
    }
}

impl std::fmt::Display for Environment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Environment::Development => write!(f, "development"),
            Environment::Testing => write!(f, "testing"),
            Environment::Staging => write!(f, "staging"),
            Environment::Production => write!(f, "production"),
        }
    }
}

/// RPC configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcConfig {
    /// Default RPC endpoints
    pub endpoints: Vec<RpcEndpointConfig>,
    
    /// Connection pool settings
    pub pool_size: usize,
    
    /// Request timeout
    pub timeout_ms: u64,
    
    /// Rate limiting
    pub rate_limit_rps: u32,
    
    /// Retry settings
    pub max_retries: usize,
    pub retry_delay_ms: u64,
}

impl Default for RpcConfig {
    fn default() -> Self {
        Self {
            endpoints: vec![
                RpcEndpointConfig {
                    url: "https://api.mainnet-beta.solana.com".to_string(),
                    priority: 1,
                    rate_limit_rps: 100,
                    timeout_ms: 30000,
                    enabled: true,
                }
            ],
            pool_size: 8,
            timeout_ms: 30000,
            rate_limit_rps: 100,
            max_retries: 3,
            retry_delay_ms: 1000,
        }
    }
}

/// Individual RPC endpoint configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RpcEndpointConfig {
    pub url: String,
    pub priority: u8,
    pub rate_limit_rps: u32,
    pub timeout_ms: u64,
    pub enabled: bool,
}

/// Scanner configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScannerConfig {
    /// Performance mode
    pub performance_mode: String,
    
    /// Maximum concurrent scans
    pub max_concurrent_scans: usize,
    
    /// Scan timeout
    pub scan_timeout_seconds: u64,
    
    /// Batch size
    pub batch_size: usize,
    
    /// Optimization settings
    pub enable_optimizations: bool,
    pub enable_caching: bool,
    pub enable_parallel_processing: bool,
    
    /// Ultra-fast specific settings
    pub ultra_fast: UltraFastConfig,
}

impl Default for ScannerConfig {
    fn default() -> Self {
        Self {
            performance_mode: "balanced".to_string(),
            max_concurrent_scans: 100,
            scan_timeout_seconds: 30,
            batch_size: 50,
            enable_optimizations: true,
            enable_caching: true,
            enable_parallel_processing: true,
            ultra_fast: UltraFastConfig::default(),
        }
    }
}

/// Ultra-fast scanner specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UltraFastConfig {
    pub prefetch_window_size: usize,
    pub batch_size_multiplier: f64,
    pub enable_predictive_prefetch: bool,
    pub enable_connection_multiplexing: bool,
    pub enable_smart_batching: bool,
    pub enable_fast_path: bool,
}

impl Default for UltraFastConfig {
    fn default() -> Self {
        Self {
            prefetch_window_size: 50,
            batch_size_multiplier: 2.0,
            enable_predictive_prefetch: true,
            enable_connection_multiplexing: true,
            enable_smart_batching: true,
            enable_fast_path: true,
        }
    }
}

/// Cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
    /// Cache type (memory, redis, etc.)
    pub cache_type: String,
    
    /// Memory cache settings
    pub memory: MemoryCacheConfig,
    
    /// Redis cache settings (if applicable)
    pub redis: Option<RedisCacheConfig>,
    
    /// Default TTL
    pub default_ttl_seconds: u64,
    
    /// Maximum cache size
    pub max_size_mb: usize,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            cache_type: "memory".to_string(),
            memory: MemoryCacheConfig::default(),
            redis: None,
            default_ttl_seconds: 300, // 5 minutes
            max_size_mb: 100,
        }
    }
}

/// Memory cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryCacheConfig {
    pub max_items: usize,
    pub ttl_seconds: u64,
    pub enable_compression: bool,
}

impl Default for MemoryCacheConfig {
    fn default() -> Self {
        Self {
            max_items: 10000,
            ttl_seconds: 300,
            enable_compression: false,
        }
    }
}

/// Redis cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisCacheConfig {
    pub url: String,
    pub pool_size: usize,
    pub connection_timeout_ms: u64,
    pub command_timeout_ms: u64,
}

/// Security configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// Rate limiting
    pub rate_limiting: RateLimitConfig,
    
    /// Audit logging
    pub audit: AuditConfig,
    
    /// Access control
    pub access_control: AccessControlConfig,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            rate_limiting: RateLimitConfig::default(),
            audit: AuditConfig::default(),
            access_control: AccessControlConfig::default(),
        }
    }
}

/// Rate limiting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    pub enabled: bool,
    pub requests_per_minute: u32,
    pub burst_size: u32,
    pub per_wallet_limits: bool,
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            requests_per_minute: 60,
            burst_size: 10,
            per_wallet_limits: true,
        }
    }
}

/// Audit configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditConfig {
    pub enabled: bool,
    pub log_level: String,
    pub retention_days: u32,
    pub include_sensitive_data: bool,
}

impl Default for AuditConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            log_level: "info".to_string(),
            retention_days: 30,
            include_sensitive_data: false,
        }
    }
}

/// Access control configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessControlConfig {
    pub allowed_destinations: Vec<String>,
    pub require_whitelist: bool,
    pub max_transaction_amount_lamports: u64,
}

impl Default for AccessControlConfig {
    fn default() -> Self {
        Self {
            allowed_destinations: vec![],
            require_whitelist: false,
            max_transaction_amount_lamports: 1_000_000_000_000, // 1000 SOL
        }
    }
}

/// Performance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Metrics collection
    pub metrics: MetricsConfig,
    
    /// Memory management
    pub memory: MemoryConfig,
    
    /// Parallel processing
    pub parallel: ParallelConfig,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            metrics: MetricsConfig::default(),
            memory: MemoryConfig::default(),
            parallel: ParallelConfig::default(),
        }
    }
}

/// Metrics configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
    pub enabled: bool,
    pub export_interval_seconds: u64,
    pub prometheus_port: u16,
    pub include_detailed_metrics: bool,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            export_interval_seconds: 30,
            prometheus_port: 9090,
            include_detailed_metrics: false,
        }
    }
}

/// Memory configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    pub max_heap_size_mb: usize,
    pub gc_threshold_percentage: f64,
    pub enable_memory_pooling: bool,
    pub pool_size_mb: usize,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            max_heap_size_mb: 1024,
            gc_threshold_percentage: 80.0,
            enable_memory_pooling: true,
            pool_size_mb: 100,
        }
    }
}

/// Parallel processing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParallelConfig {
    pub max_workers: usize,
    pub work_stealing: bool,
    pub queue_size: usize,
    pub timeout_seconds: u64,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            max_workers: num_cpus::get(),
            work_stealing: true,
            queue_size: 1000,
            timeout_seconds: 60,
        }
    }
}

/// Main application configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    /// Environment
    pub environment: Environment,
    
    /// Application metadata
    pub app_name: String,
    pub version: String,
    
    /// Component configurations
    pub rpc: RpcConfig,
    pub scanner: ScannerConfig,
    pub cache: CacheConfig,
    pub security: SecurityConfig,
    pub performance: PerformanceConfig,
    
    /// Feature flags
    pub features: HashMap<String, bool>,
}

impl Default for AppConfig {
    fn default() -> Self {
        let mut features = HashMap::new();
        features.insert("scanner".to_string(), true);
        features.insert("api".to_string(), true);
        features.insert("cache".to_string(), true);
        features.insert("metrics".to_string(), true);
        
        Self {
            environment: Environment::Development,
            app_name: "solana-recover".to_string(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            rpc: RpcConfig::default(),
            scanner: ScannerConfig::default(),
            cache: CacheConfig::default(),
            security: SecurityConfig::default(),
            performance: PerformanceConfig::default(),
            features,
        }
    }
}

impl AppConfig {
    /// Load configuration from file
    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let content = fs::read_to_string(path)
            .map_err(|e| SolanaRecoverError::InternalError(
                format!("Failed to read config file: {}", e)
            ))?;
        
        let config: AppConfig = toml::from_str(&content)
            .map_err(|e| SolanaRecoverError::InternalError(
                format!("Failed to parse config file: {}", e)
            ))?;
        
        config.validate()?;
        
        Ok(config)
    }
    
    /// Load configuration from environment variables
    pub fn load_from_env() -> Result<Self> {
        let mut config = AppConfig::default();
        
        // Environment
        if let Ok(env_str) = env::var("ENVIRONMENT") {
            config.environment = match env_str.to_lowercase().as_str() {
                "development" => Environment::Development,
                "testing" => Environment::Testing,
                "staging" => Environment::Staging,
                "production" => Environment::Production,
                _ => Environment::Development,
            };
        }
        
        // RPC settings
        if let Ok(endpoints) = env::var("RPC_ENDPOINTS") {
            config.rpc.endpoints = endpoints
                .split(',')
                .map(|url| RpcEndpointConfig {
                    url: url.trim().to_string(),
                    priority: 1,
                    rate_limit_rps: 100,
                    timeout_ms: 30000,
                    enabled: true,
                })
                .collect();
        }
        
        if let Ok(pool_size) = env::var("RPC_POOL_SIZE") {
            config.rpc.pool_size = pool_size.parse().unwrap_or(8);
        }
        
        // Scanner settings
        if let Ok(performance_mode) = env::var("SCANNER_PERFORMANCE_MODE") {
            config.scanner.performance_mode = performance_mode;
        }
        
        if let Ok(max_concurrent) = env::var("SCANNER_MAX_CONCURRENT") {
            config.scanner.max_concurrent_scans = max_concurrent.parse().unwrap_or(100);
        }
        
        // Cache settings
        if let Ok(cache_type) = env::var("CACHE_TYPE") {
            config.cache.cache_type = cache_type;
        }
        
        // Security settings
        if let Ok(rate_limit_enabled) = env::var("RATE_LIMIT_ENABLED") {
            config.security.rate_limiting.enabled = rate_limit_enabled.parse().unwrap_or(true);
        }
        
        config.validate()?;
        
        Ok(config)
    }
    
    /// Load configuration with fallback chain
    pub fn load() -> Result<Self> {
        // Try environment-specific config file first
        let env = env::var("ENVIRONMENT").unwrap_or_else(|_| "development".to_string());
        let env_config_path = format!("config-{}.toml", env);
        
        if Path::new(&env_config_path).exists() {
            info!("Loading environment-specific config from {}", env_config_path);
            return Self::load_from_file(env_config_path);
        }
        
        // Try default config file
        if Path::new("config.toml").exists() {
            info!("Loading default config from config.toml");
            return Self::load_from_file("config.toml");
        }
        
        // Fall back to environment variables
        info!("Loading config from environment variables");
        Self::load_from_env()
    }
    
    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        // Validate RPC endpoints
        if self.rpc.endpoints.is_empty() {
            return Err(SolanaRecoverError::InternalError(
                "At least one RPC endpoint must be configured".to_string()
            ));
        }
        
        for endpoint in &self.rpc.endpoints {
            if endpoint.url.is_empty() {
                return Err(SolanaRecoverError::InternalError(
                    "RPC endpoint URL cannot be empty".to_string()
                ));
            }
        }
        
        // Validate scanner settings
        if self.scanner.max_concurrent_scans == 0 {
            return Err(SolanaRecoverError::InternalError(
                "Max concurrent scans must be greater than 0".to_string()
            ));
        }
        
        if self.scanner.batch_size == 0 {
            return Err(SolanaRecoverError::InternalError(
                "Batch size must be greater than 0".to_string()
            ));
        }
        
        // Validate performance mode
        if !["ultra_fast", "balanced", "resource_efficient", "throughput", "latency"]
            .contains(&self.scanner.performance_mode.as_str()) {
            return Err(SolanaRecoverError::InternalError(
                format!("Invalid performance mode: {}", self.scanner.performance_mode)
            ));
        }
        
        // Validate cache settings
        if self.cache.cache_type.is_empty() {
            return Err(SolanaRecoverError::InternalError(
                "Cache type cannot be empty".to_string()
            ));
        }
        
        info!("Configuration validation passed");
        Ok(())
    }
    
    /// Save configuration to file
    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let content = toml::to_string_pretty(self)
            .map_err(|e| SolanaRecoverError::InternalError(
                format!("Failed to serialize config: {}", e)
            ))?;
        
        fs::write(path, content)
            .map_err(|e| SolanaRecoverError::InternalError(
                format!("Failed to write config file: {}", e)
            ))?;
        
        info!("Configuration saved successfully");
        Ok(())
    }
    
    /// Get configuration for specific environment
    pub fn for_environment(env: Environment) -> Self {
        let mut config = Self::default();
        config.environment = env.clone();
        
        match env {
            Environment::Production => {
                config.rpc.rate_limit_rps = 200;
                config.scanner.max_concurrent_scans = 500;
                config.security.audit.include_sensitive_data = false;
                config.performance.metrics.include_detailed_metrics = false;
            }
            Environment::Staging => {
                config.rpc.rate_limit_rps = 150;
                config.scanner.max_concurrent_scans = 200;
                config.security.audit.include_sensitive_data = true;
            }
            Environment::Testing => {
                config.rpc.rate_limit_rps = 50;
                config.scanner.max_concurrent_scans = 10;
                config.security.rate_limiting.enabled = false;
                config.performance.metrics.enabled = false;
            }
            Environment::Development => {
                config.rpc.rate_limit_rps = 100;
                config.scanner.max_concurrent_scans = 50;
                config.security.audit.include_sensitive_data = true;
                config.performance.metrics.include_detailed_metrics = true;
            }
        }
        
        config
    }
}

/// Configuration manager with hot-reload support
pub struct ConfigManager {
    config: Arc<RwLock<AppConfig>>,
    config_path: Option<PathBuf>,
    last_modified: Arc<RwLock<Option<Instant>>>,
    reload_interval: Duration,
}

impl ConfigManager {
    /// Create a new configuration manager
    pub fn new(config: AppConfig) -> Self {
        Self {
            config: Arc::new(RwLock::new(config)),
            config_path: None,
            last_modified: Arc::new(RwLock::new(None)),
            reload_interval: Duration::from_secs(30),
        }
    }
    
    /// Create with file path for hot-reload
    pub async fn with_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let config = AppConfig::load_from_file(&path)?;
        let path_buf = path.as_ref().to_path_buf();
        
        let mut manager = Self::new(config);
        manager.config_path = Some(path_buf);
        
        // Set initial modification time
        if let Some(ref path) = manager.config_path {
            if let Ok(metadata) = fs::metadata(path) {
                if let Ok(modified) = metadata.modified() {
                    let now = SystemTime::now();
                    let duration_since_modified = now.duration_since(modified).unwrap_or_default();
                    let modified_instant = Instant::now() - duration_since_modified;
                    let mut last_modified = manager.last_modified.write().await;
                    *last_modified = Some(modified_instant);
                }
            }
        }
        
        Ok(manager)
    }
    
    /// Get current configuration
    pub async fn get_config(&self) -> AppConfig {
        self.config.read().await.clone()
    }
    
    /// Update configuration
    pub async fn update_config(&self, new_config: AppConfig) -> Result<()> {
        new_config.validate()?;
        
        let mut config = self.config.write().await;
        *config = new_config;
        
        info!("Configuration updated successfully");
        Ok(())
    }
    
    /// Check for configuration changes and reload if needed
    pub async fn check_for_reload(&self) -> Result<bool> {
        if let Some(ref path) = self.config_path {
            if let Ok(metadata) = fs::metadata(path) {
                if let Ok(modified) = metadata.modified() {
                    let now = SystemTime::now();
                    let duration_since_modified = now.duration_since(modified).unwrap_or_default();
                    let modified_instant = Instant::now() - duration_since_modified;
                    
                    let last_modified = *self.last_modified.read().await;
                    
                    if let Some(last) = last_modified {
                        if modified_instant > last {
                            info!("Configuration file changed, reloading...");
                            self.reload().await?;
                            return Ok(true);
                        }
                    }
                }
            }
        }
        
        Ok(false)
    }
    
    /// Force reload configuration from file
    pub async fn reload(&self) -> Result<()> {
        if let Some(ref path) = self.config_path {
            let new_config = AppConfig::load_from_file(path)?;
            self.update_config(new_config).await?;
            
            // Update last modified time
            if let Ok(metadata) = fs::metadata(path) {
                if let Ok(modified) = metadata.modified() {
                    let now = SystemTime::now();
                    let duration_since_modified = now.duration_since(modified).unwrap_or_default();
                    let modified_instant = Instant::now() - duration_since_modified;
                    let mut last_modified = self.last_modified.write().await;
                    *last_modified = Some(modified_instant);
                }
            }
        }
        
        Ok(())
    }
    
    /// Start background hot-reload task
    pub async fn start_hot_reload(&self) -> tokio::task::JoinHandle<()> {
        let config_manager = self.clone();
        
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(config_manager.reload_interval);
            
            loop {
                interval.tick().await;
                
                if let Err(e) = config_manager.check_for_reload().await {
                    error!("Failed to check for configuration reload: {:?}", e);
                }
            }
        })
    }
}

impl Clone for ConfigManager {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            config_path: self.config_path.clone(),
            last_modified: self.last_modified.clone(),
            reload_interval: self.reload_interval,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;
        
    #[test]
    fn test_default_config() {
        let config = AppConfig::default();
        assert!(config.validate().is_ok());
        assert_eq!(config.environment, Environment::Development);
        assert!(!config.rpc.endpoints.is_empty());
    }
    
    #[test]
    fn test_config_validation() {
        let mut config = AppConfig::default();
        
        // Invalid: no RPC endpoints
        config.rpc.endpoints.clear();
        assert!(config.validate().is_err());
        
        // Valid: add endpoint back
        config.rpc.endpoints.push(RpcEndpointConfig {
            url: "https://api.mainnet-beta.solana.com".to_string(),
            priority: 1,
            rate_limit_rps: 100,
            timeout_ms: 30000,
            enabled: true,
        });
        assert!(config.validate().is_ok());
    }
    
    #[test]
    fn test_environment_configs() {
        let dev_config = AppConfig::for_environment(Environment::Development);
        let prod_config = AppConfig::for_environment(Environment::Production);
        
        assert_eq!(dev_config.environment, Environment::Development);
        assert_eq!(prod_config.environment, Environment::Production);
        
        // Production should have higher limits
        assert!(prod_config.rpc.rate_limit_rps > dev_config.rpc.rate_limit_rps);
        assert!(prod_config.scanner.max_concurrent_scans > dev_config.scanner.max_concurrent_scans);
    }
    
    #[test]
    fn test_config_save_and_load() -> Result<()> {
        let original_config = AppConfig::default();
        
        // Create temporary file
        let temp_file = NamedTempFile::new()?;
        let temp_path = temp_file.path();
        
        // Save config
        original_config.save_to_file(temp_path)?;
        
        // Load config
        let loaded_config = AppConfig::load_from_file(temp_path)?;
        
        // Compare
        assert_eq!(original_config.environment, loaded_config.environment);
        assert_eq!(original_config.rpc.endpoints.len(), loaded_config.rpc.endpoints.len());
        
        Ok(())
    }
    
    #[test]
    fn test_env_config_loading() {
        // Set environment variables
        env::set_var("ENVIRONMENT", "production");
        env::set_var("RPC_POOL_SIZE", "16");
        env::set_var("SCANNER_PERFORMANCE_MODE", "ultra_fast");
        
        let config = AppConfig::load_from_env().unwrap();
        
        assert_eq!(config.environment, Environment::Production);
        assert_eq!(config.rpc.pool_size, 16);
        assert_eq!(config.scanner.performance_mode, "ultra_fast");
        
        // Clean up
        env::remove_var("ENVIRONMENT");
        env::remove_var("RPC_POOL_SIZE");
        env::remove_var("SCANNER_PERFORMANCE_MODE");
    }
}