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
use crate::core::{Result, SolanaRecoverError};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};
use std::sync::Arc;
use moka::future::Cache as MokaCache;
use parking_lot::RwLock;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CacheConfig {
    pub ttl_seconds: u64,
    pub max_size: usize,
    pub cleanup_interval_seconds: u64,
    pub enable_hierarchical_cache: bool,
    pub l1_cache_size: usize,
    pub l2_cache_size: usize,
    pub compression_threshold: usize,
    pub enable_metrics: bool,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            ttl_seconds: 300, // 5 minutes
            max_size: 100000, // Increased for better performance
            cleanup_interval_seconds: 60, // 1 minute
            enable_hierarchical_cache: true,
            l1_cache_size: 10000,  // Hot data
            l2_cache_size: 50000,  // Warm data
            compression_threshold: 1024, // Compress entries > 1KB
            enable_metrics: true,
        }
    }
}

#[derive(Debug, Clone)]
struct CacheEntry<T> {
    value: T,
    created_at: Instant,
    ttl: Duration,
    access_count: u64,
    last_accessed: Instant,
    size_bytes: usize,
    compressed: bool,
}

impl<T> CacheEntry<T> {
    fn new(value: T, ttl: Duration) -> Self {
        let size_bytes = std::mem::size_of_val(&value);
        Self {
            value,
            created_at: Instant::now(),
            ttl,
            access_count: 0,
            last_accessed: Instant::now(),
            size_bytes,
            compressed: false,
        }
    }
    
    fn new_compressed(value: T, ttl: Duration) -> Self {
        let size_bytes = std::mem::size_of_val(&value);
        Self {
            value,
            created_at: Instant::now(),
            ttl,
            access_count: 0,
            last_accessed: Instant::now(),
            size_bytes,
            compressed: true,
        }
    }
    
    fn is_compressed(&self) -> bool {
        self.compressed
    }
    
    fn set_compressed(&mut self, compressed: bool) {
        self.compressed = compressed;
    }

    fn is_expired(&self) -> bool {
        self.created_at.elapsed() > self.ttl
    }
    
    fn touch(&mut self) {
        self.access_count += 1;
        self.last_accessed = Instant::now();
    }
    
    fn priority_score(&self) -> f64 {
        let age_factor = self.last_accessed.elapsed().as_secs_f64();
        let access_factor = self.access_count as f64;
        access_factor / (1.0 + age_factor)
    }
}

pub struct CacheManager {
    // Hierarchical cache: L1 (hot) + L2 (warm)
    l1_cache: Arc<MokaCache<String, CacheEntry<serde_json::Value>>>,
    l2_cache: Arc<DashMap<String, CacheEntry<serde_json::Value>>>,
    config: CacheConfig,
    metrics: Arc<RwLock<CacheMetrics>>,
    compression_enabled: bool,
}

#[derive(Debug, Default, Clone, serde::Serialize)]
pub struct CacheMetrics {
    pub hits: u64,
    pub misses: u64,
    pub sets: u64,
    pub evictions: u64,
    pub compressions: u64,
    pub decompressions: u64,
    pub total_size_bytes: u64,
    pub avg_access_time_ns: u64,
    pub l1_hits: u64,
    pub l2_hits: u64,
}

impl CacheManager {
    pub fn new(config: CacheConfig) -> Self {
        let l1_cache = Arc::new(
            MokaCache::builder()
                .max_capacity(config.l1_cache_size as u64)
                .time_to_live(Duration::from_secs(config.ttl_seconds))
                .build()
        );
        
        let cache = Self {
            l1_cache,
            l2_cache: Arc::new(DashMap::new()),
            config: config.clone(),
            metrics: Arc::new(RwLock::new(CacheMetrics::default())),
            compression_enabled: config.compression_threshold > 0,
        };

        // Start cleanup task for L2 cache
        if config.enable_hierarchical_cache {
            let l2_cache = cache.l2_cache.clone();
            let cleanup_interval = Duration::from_secs(config.cleanup_interval_seconds);
            let metrics = cache.metrics.clone();
            
            tokio::spawn(async move {
                let mut interval = tokio::time::interval(cleanup_interval);
                loop {
                    interval.tick().await;
                    Self::cleanup_expired_l2(&l2_cache, &metrics);
                }
            });
        }

        cache
    }

    fn cleanup_expired_l2(
        l2_cache: &DashMap<String, CacheEntry<serde_json::Value>>,
        metrics: &Arc<RwLock<CacheMetrics>>
    ) {
        let expired_keys: Vec<String> = l2_cache
            .iter()
            .filter(|entry| entry.value().is_expired())
            .map(|entry| entry.key().clone())
            .collect();

        let mut total_size_freed = 0usize;
        for key in &expired_keys {
            if let Some((_, entry)) = l2_cache.remove(key) {
                total_size_freed += entry.size_bytes;
            }
        }
        
        // Update metrics
        {
            let mut m = metrics.write();
            m.total_size_bytes = m.total_size_bytes.saturating_sub(total_size_freed as u64);
        }
    }

    pub async fn get<T>(&self, key: &str) -> Result<Option<T>>
    where
        T: for<'de> Deserialize<'de>,
    {
        let start = Instant::now();
        
        // Try L1 cache first (hot data)
        if let Some(mut entry) = self.l1_cache.get(key).await {
            entry.touch();
            
            // Track compression metrics
            if entry.is_compressed() {
                let mut m = self.metrics.write();
                m.decompressions += 1;
            }
            
            let value: T = serde_json::from_value(entry.value.clone())
                .map_err(|e| SolanaRecoverError::StorageError(
                    format!("Failed to deserialize cache value: {}", e)
                ))?;
            
            // Update metrics
            {
                let mut m = self.metrics.write();
                m.hits += 1;
                m.l1_hits += 1;
                let elapsed = start.elapsed().as_nanos() as u64;
                m.avg_access_time_ns = (m.avg_access_time_ns + elapsed) / 2;
            }
            
            return Ok(Some(value));
        }
        
        // Try L2 cache (warm data)
        if self.config.enable_hierarchical_cache {
            if let Some(mut entry) = self.l2_cache.get_mut(key) {
                if entry.is_expired() {
                    drop(entry);
                    self.l2_cache.remove(key);
                } else {
                    entry.touch();
                    
                    // Track compression metrics
                    if entry.is_compressed() {
                        let mut m = self.metrics.write();
                        m.decompressions += 1;
                    }
                    
                    let value: T = serde_json::from_value(entry.value.clone())
                        .map_err(|e| SolanaRecoverError::StorageError(
                            format!("Failed to deserialize cache value: {}", e)
                        ))?;
                    
                    // Promote to L1 if accessed frequently
                    if entry.access_count > 3 {
                        let entry_clone = entry.clone();
                        drop(entry);
                        let _ = self.l1_cache.insert(key.to_string(), entry_clone).await;
                    } else {
                        drop(entry);
                    }
                    
                    // Update metrics
                    {
                        let mut m = self.metrics.write();
                        m.hits += 1;
                        m.l2_hits += 1;
                        let elapsed = start.elapsed().as_nanos() as u64;
                        m.avg_access_time_ns = (m.avg_access_time_ns + elapsed) / 2;
                    }
                    
                    return Ok(Some(value));
                }
            }
        }
        
        // Cache miss
        {
            let mut m = self.metrics.write();
            m.misses += 1;
            let elapsed = start.elapsed().as_nanos() as u64;
            m.avg_access_time_ns = (m.avg_access_time_ns + elapsed) / 2;
        }
        
        Ok(None)
    }

    pub async fn set<T>(&self, key: &str, value: &T) -> Result<()>
    where
        T: Serialize,
    {
        let json_value = serde_json::to_value(value)
            .map_err(|e| SolanaRecoverError::StorageError(
                format!("Failed to serialize cache value: {}", e)
            ))?;
        
        let size_bytes = json_value.to_string().len();
        let should_compress = self.compression_enabled && size_bytes > self.config.compression_threshold;
        
        let entry = if should_compress {
            CacheEntry::new_compressed(
                json_value,
                Duration::from_secs(self.config.ttl_seconds)
            )
        } else {
            CacheEntry::new(
                json_value,
                Duration::from_secs(self.config.ttl_seconds)
            )
        };
        
        // Store in L1 cache (hot data)
        let _ = self.l1_cache.insert(key.to_string(), entry.clone()).await;
        
        // Also store in L2 if hierarchical caching is enabled
        if self.config.enable_hierarchical_cache {
            self.l2_cache.insert(key.to_string(), entry);
        }
        
        // Update metrics
        {
            let mut m = self.metrics.write();
            m.sets += 1;
            m.total_size_bytes += size_bytes as u64;
            if should_compress {
                m.compressions += 1;
            }
        }
        
        // Check if we need to evict entries
        if self.l1_cache.weighted_size() >= self.config.l1_cache_size as u64 {
            self.evict_lru_l1();
        }
        
        if self.config.enable_hierarchical_cache && self.l2_cache.len() >= self.config.l2_cache_size {
            self.evict_smart_l2();
        }
        
        Ok(())
    }

    pub async fn delete(&self, key: &str) -> bool {
        let l1_removed = self.l1_cache.remove(key).await.is_some();
        let l2_removed = if self.config.enable_hierarchical_cache {
            self.l2_cache.remove(key).is_some()
        } else {
            false
        };
        l1_removed || l2_removed
    }

    pub fn clear(&self) {
        self.l1_cache.invalidate_all();
        if self.config.enable_hierarchical_cache {
            self.l2_cache.clear();
        }
    }

    pub fn size(&self) -> u64 {
        let l1_size = self.l1_cache.entry_count();
        let l2_size = if self.config.enable_hierarchical_cache {
            self.l2_cache.len()
        } else {
            0
        };
        
        l1_size as u64 + l2_size as u64
    }

    pub fn stats(&self) -> CacheStats {
        let l1_size = self.l1_cache.entry_count();
        let l2_size = if self.config.enable_hierarchical_cache {
            self.l2_cache.len()
        } else {
            0
        };
        
        let expired_l2 = if self.config.enable_hierarchical_cache {
            self.l2_cache
                .iter()
                .filter(|entry| entry.value().is_expired())
                .count()
        } else {
            0
        };
        
        CacheStats {
            total_entries: l1_size as u64 + l2_size as u64,
            l1_entries: l1_size as u64,
            l2_entries: l2_size as u64,
            expired_entries: expired_l2 as u64,
            max_size: self.config.max_size as u64,
            l1_max_size: self.config.l1_cache_size as u64,
            l2_max_size: self.config.l2_cache_size as u64,
            ttl_seconds: self.config.ttl_seconds,
            metrics: self.metrics.read().clone(),
        }
    }

    fn evict_lru_l1(&self) {
        // Moka cache handles LRU eviction automatically
        // This is a no-op as Moka manages its own eviction
    }
    
    fn evict_smart_l2(&self) {
        // Smart eviction based on priority score (access frequency + recency)
        let mut entries: Vec<_> = self.l2_cache
            .iter()
            .map(|entry| {
                let (key, value) = entry.pair();
                (key.clone(), value.priority_score())
            })
            .collect();

        entries.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        // Remove lowest priority entries (bottom 20%)
        let remove_count = (self.config.l2_cache_size / 5).max(1);
        let mut total_size_freed = 0usize;
        
        for (key, _) in entries.iter().take(remove_count) {
            if let Some((_, entry)) = self.l2_cache.remove(key) {
                total_size_freed += entry.size_bytes;
            }
        }
        
        // Update metrics
        {
            let mut m = self.metrics.write();
            m.evictions += remove_count as u64;
            m.total_size_bytes = m.total_size_bytes.saturating_sub(total_size_freed as u64);
        }
    }
    
    pub fn get_metrics(&self) -> CacheMetrics {
        self.metrics.read().clone()
    }
    
    pub async fn cleanup_expired(&self) -> Result<u64> {
        if self.config.enable_hierarchical_cache {
            let initial_size = self.l2_cache.len();
            Self::cleanup_expired_l2(&self.l2_cache, &self.metrics);
            let cleaned_count = initial_size - self.l2_cache.len();
            Ok(cleaned_count as u64)
        } else {
            Ok(0)
        }
    }

    pub async fn recompress_entries(&self, new_threshold: usize) -> Result<()> {
        // Recompress entries based on new threshold
        if self.config.enable_hierarchical_cache {
            let mut entries_to_update = Vec::new();
            
            for entry in self.l2_cache.iter() {
                let (key, cache_entry) = entry.pair();
                let should_compress = cache_entry.size_bytes > new_threshold;
                
                if cache_entry.is_compressed() != should_compress {
                    let mut updated_entry = cache_entry.clone();
                    updated_entry.set_compressed(should_compress);
                    entries_to_update.push((key.clone(), updated_entry));
                }
            }
            
            // Update entries with new compression status
            for (key, entry) in entries_to_update {
                let _ = self.l1_cache.insert(key, entry).await;
            }
        }
        Ok(())
    }
    
    pub async fn warm_up<T>(&self, entries: Vec<(String, T)>) -> Result<()>
    where
        T: Serialize + for<'de> Deserialize<'de>,
    {
        for (key, value) in entries {
            self.set(&key, &value).await?;
        }
        Ok(())
    }
    
    pub async fn prefetch_batch<T>(&self, keys: Vec<String>) -> Vec<(String, Option<T>)>
    where
        T: for<'de> Deserialize<'de>,
    {
        use futures::future::join_all;
        
        let results = join_all(
            keys.into_iter().map(|key| async move {
                let result = self.get(&key).await.unwrap_or(None);
                (key, result)
            })
        ).await;
        
        results
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct CacheStats {
    pub total_entries: u64,
    pub l1_entries: u64,
    pub l2_entries: u64,
    pub expired_entries: u64,
    pub max_size: u64,
    pub l1_max_size: u64,
    pub l2_max_size: u64,
    pub ttl_seconds: u64,
    pub metrics: CacheMetrics,
}

impl Default for CacheManager {
    fn default() -> Self {
        Self::new(CacheConfig::default())
    }
}