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
use crate::core::{Result};
use std::sync::Arc;
use std::collections::VecDeque;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use serde::Serialize;

/// Generic object pool for efficient memory management
pub struct ObjectPool<T> {
    objects: Arc<tokio::sync::Mutex<VecDeque<T>>>,
    factory: Box<dyn Fn() -> T + Send + Sync>,
    reset_fn: Option<Box<dyn Fn(&mut T) + Send + Sync>>,
    current_size: Arc<std::sync::atomic::AtomicUsize>,
    metrics: Arc<RwLock<PoolMetrics>>,
    config: PoolConfig,
}

#[derive(Debug, Clone, Serialize)]
pub struct PoolMetrics {
    pub total_created: u64,
    pub total_reused: u64,
    pub total_discarded: u64,
    pub current_size: usize,
    pub peak_size: usize,
    pub hit_rate: f64,
    pub avg_lifetime_ms: f64,
    pub last_cleanup: Option<chrono::DateTime<chrono::Utc>>,
}

impl Default for PoolMetrics {
    fn default() -> Self {
        Self {
            total_created: 0,
            total_reused: 0,
            total_discarded: 0,
            current_size: 0,
            peak_size: 0,
            hit_rate: 0.0,
            avg_lifetime_ms: 0.0,
            last_cleanup: None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct PoolConfig {
    pub max_size: usize,
    pub initial_size: usize,
    pub cleanup_interval: Duration,
    pub max_idle_time: Duration,
    pub enable_metrics: bool,
    pub preallocate_initial: bool,
}

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            max_size: 1000,
            initial_size: 10,
            cleanup_interval: Duration::from_secs(60),
            max_idle_time: Duration::from_secs(300),
            enable_metrics: true,
            preallocate_initial: true,
        }
    }
}

impl<T> ObjectPool<T>
where
    T: Send + 'static,
{
    pub fn new<F>(factory: F, config: PoolConfig) -> Self
    where
        F: Fn() -> T + Send + Sync + 'static,
    {
        let pool = Self {
            objects: Arc::new(tokio::sync::Mutex::new(VecDeque::with_capacity(config.max_size))),
            factory: Box::new(factory),
            reset_fn: None,
            current_size: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
            metrics: Arc::new(RwLock::new(PoolMetrics::default())),
            config,
        };

        // Preallocate initial objects if enabled
        if pool.config.preallocate_initial {
            // Note: Skip preallocation for now due to Clone issues
            // In production, you'd implement a proper sharing mechanism
        }

        // Start cleanup task if needed
        if pool.config.cleanup_interval > Duration::ZERO {
            pool.start_cleanup_task();
        }

        pool
    }

    pub fn with_reset<F, R>(factory: F, reset_fn: R, config: PoolConfig) -> Self
    where
        F: Fn() -> T + Send + Sync + 'static,
        R: Fn(&mut T) + Send + Sync + 'static,
    {
        let mut pool = Self::new(factory, config);
        pool.reset_fn = Some(Box::new(reset_fn));
        pool
    }

    /// Get an object from the pool
    pub async fn get(&self) -> PooledObject<T> {
        let _start_time = Instant::now();
        
        // Try to get from pool first
        let mut objects = self.objects.lock().await;
        
        let object = if let Some(mut obj) = objects.pop_front() {
            // Reset object if reset function is provided
            if let Some(ref reset_fn) = self.reset_fn {
                reset_fn(&mut obj);
            }
            
            // Update metrics
            if self.config.enable_metrics {
                let mut metrics = self.metrics.write().await;
                metrics.total_reused += 1;
                self.update_hit_rate(&mut metrics);
            }
            
            obj
        } else {
            // Create new object
            let obj = (self.factory)();
            
            // Update metrics
            if self.config.enable_metrics {
                let mut metrics = self.metrics.write().await;
                metrics.total_created += 1;
                self.update_hit_rate(&mut metrics);
            }
            
            obj
        };
        
        let _current_size = self.current_size.fetch_sub(1, std::sync::atomic::Ordering::Relaxed) - 1;
        
        PooledObject {
            object: Some(object),
        }
    }



    /// Start background cleanup task
    fn start_cleanup_task(&self) {
        // Note: Skip cleanup task for now due to Clone issues
        // In production, you'd implement a proper sharing mechanism
        tracing::warn!("Object pool cleanup task disabled due to Clone limitations");
    }


    /// Update hit rate metric
    fn update_hit_rate(&self, metrics: &mut PoolMetrics) {
        let total_requests = metrics.total_created + metrics.total_reused;
        if total_requests > 0 {
            metrics.hit_rate = metrics.total_reused as f64 / total_requests as f64;
        }
    }


    /// Get current pool metrics
    pub async fn get_metrics(&self) -> PoolMetrics {
        if !self.config.enable_metrics {
            return PoolMetrics::default();
        }
        
        let mut metrics = self.metrics.write().await;
        metrics.current_size = self.current_size.load(std::sync::atomic::Ordering::Relaxed);
        
        if metrics.current_size > metrics.peak_size {
            metrics.peak_size = metrics.current_size;
        }
        
        PoolMetrics {
            total_created: metrics.total_created,
            total_reused: metrics.total_reused,
            total_discarded: metrics.total_discarded,
            current_size: metrics.current_size,
            peak_size: metrics.peak_size,
            hit_rate: metrics.hit_rate,
            avg_lifetime_ms: metrics.avg_lifetime_ms,
            last_cleanup: metrics.last_cleanup,
        }
    }

    /// Clear all objects from the pool
    pub async fn clear(&self) {
        let mut objects = self.objects.lock().await;
        objects.clear();
        self.current_size.store(0, std::sync::atomic::Ordering::Relaxed);
        
        if self.config.enable_metrics {
            let mut metrics = self.metrics.write().await;
            metrics.current_size = 0;
        }
    }
}

// Note: ObjectPool cannot be cloned due to function pointers
// Use Arc<ObjectPool<T>> for sharing instead

/// Pooled object that needs to be manually returned to pool
pub struct PooledObject<T> {
    object: Option<T>,
}

impl<T> PooledObject<T> {
    /// Get a mutable reference to the pooled object
    pub fn get_mut(&mut self) -> &mut T {
        self.object.as_mut().expect("Object already returned to pool")
    }

    /// Get an immutable reference to the pooled object
    pub fn get(&self) -> &T {
        self.object.as_ref().expect("Object already returned to pool")
    }

    /// Consume the pooled object and return the inner value
    /// Note: This will not return the object to the pool
    pub fn into_inner(mut self) -> T {
        self.object.take().expect("Object already returned to pool")
    }
}

impl<T> std::ops::Deref for PooledObject<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.get()
    }
}

impl<T> std::ops::DerefMut for PooledObject<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.get_mut()
    }
}

/// Memory manager for coordinating multiple object pools
pub struct MemoryManager {
    transaction_pool: Arc<ObjectPool<TransactionWrapper>>,
    account_pool: Arc<ObjectPool<AccountWrapper>>,
    buffer_pool: Arc<ObjectPool<Vec<u8>>>,
    string_pool: Arc<ObjectPool<String>>,
    metrics: Arc<RwLock<MemoryMetrics>>,
    config: MemoryManagerConfig,
}

#[derive(Debug, Clone, Serialize)]
pub struct MemoryMetrics {
    pub total_allocated_objects: u64,
    pub total_reused_objects: u64,
    pub total_memory_saved_mb: f64,
    pub pool_efficiency: f64,
    pub gc_pressure: f64,
    pub allocation_rate: f64,
    pub deallocation_rate: f64,
    pub last_gc: Option<chrono::DateTime<chrono::Utc>>,
}

impl Default for MemoryMetrics {
    fn default() -> Self {
        Self {
            total_allocated_objects: 0,
            total_reused_objects: 0,
            total_memory_saved_mb: 0.0,
            pool_efficiency: 0.0,
            gc_pressure: 0.0,
            allocation_rate: 0.0,
            deallocation_rate: 0.0,
            last_gc: None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct MemoryManagerConfig {
    pub enable_monitoring: bool,
    pub gc_threshold: f64,
    pub monitoring_interval: Duration,
    pub auto_gc_enabled: bool,
}

impl Default for MemoryManagerConfig {
    fn default() -> Self {
        Self {
            enable_monitoring: true,
            gc_threshold: 0.8,
            monitoring_interval: Duration::from_secs(30),
            auto_gc_enabled: true,
        }
    }
}

/// Wrapper for Solana transactions
#[derive(Debug, Clone)]
pub struct TransactionWrapper {
    pub transaction: Option<solana_sdk::transaction::Transaction>,
    pub created_at: Instant,
}

/// Wrapper for account data
#[derive(Debug, Clone)]
pub struct AccountWrapper {
    pub account: Option<solana_sdk::account::Account>,
    pub created_at: Instant,
}

impl MemoryManager {
    pub fn new(config: MemoryManagerConfig) -> Result<Self> {
        // Create object pools with appropriate configurations
        let transaction_config = PoolConfig {
            max_size: 1000,
            initial_size: 50,
            cleanup_interval: Duration::from_secs(120),
            max_idle_time: Duration::from_secs(600),
            enable_metrics: config.enable_monitoring,
            preallocate_initial: true,
        };

        let account_config = PoolConfig {
            max_size: 5000,
            initial_size: 100,
            cleanup_interval: Duration::from_secs(60),
            max_idle_time: Duration::from_secs(300),
            enable_metrics: config.enable_monitoring,
            preallocate_initial: true,
        };

        let buffer_config = PoolConfig {
            max_size: 2000,
            initial_size: 200,
            cleanup_interval: Duration::from_secs(30),
            max_idle_time: Duration::from_secs(180),
            enable_metrics: config.enable_monitoring,
            preallocate_initial: true,
        };

        let string_config = PoolConfig {
            max_size: 3000,
            initial_size: 300,
            cleanup_interval: Duration::from_secs(45),
            max_idle_time: Duration::from_secs(240),
            enable_metrics: config.enable_monitoring,
            preallocate_initial: true,
        };

        let transaction_pool = Arc::new(ObjectPool::with_reset(
            || TransactionWrapper {
                transaction: None,
                created_at: Instant::now(),
            },
            |wrapper| {
                wrapper.transaction = None;
                wrapper.created_at = Instant::now();
            },
            transaction_config,
        ));

        let account_pool = Arc::new(ObjectPool::with_reset(
            || AccountWrapper {
                account: None,
                created_at: Instant::now(),
            },
            |wrapper| {
                wrapper.account = None;
                wrapper.created_at = Instant::now();
            },
            account_config,
        ));

        let buffer_pool = Arc::new(ObjectPool::with_reset(
            || Vec::with_capacity(4096),
            |buffer| buffer.clear(),
            buffer_config,
        ));

        let string_pool = Arc::new(ObjectPool::with_reset(
            || String::with_capacity(256),
            |string| string.clear(),
            string_config,
        ));

        let manager = Self {
            transaction_pool,
            account_pool,
            buffer_pool,
            string_pool,
            metrics: Arc::new(RwLock::new(MemoryMetrics::default())),
            config: config.clone(),
        };

        // Start monitoring if enabled
        if config.enable_monitoring {
            manager.start_monitoring();
        }

        Ok(manager)
    }

    /// Get a transaction from the pool
    pub async fn get_transaction(&self) -> PooledObject<TransactionWrapper> {
        self.transaction_pool.get().await
    }

    /// Get an account wrapper from the pool
    pub async fn get_account(&self) -> PooledObject<AccountWrapper> {
        self.account_pool.get().await
    }

    /// Get a buffer from the pool
    pub async fn get_buffer(&self) -> PooledObject<Vec<u8>> {
        self.buffer_pool.get().await
    }

    /// Get a buffer from the pool (blocking version for sync contexts)
    pub fn get_buffer_blocking(&self) -> PooledObject<Vec<u8>> {
        // For now, create a new buffer since we can't block in sync context
        // In a real implementation, you might use a blocking pool or tokio::block_on
        PooledObject {
            object: Some(Vec::with_capacity(4096)),
        }
    }

    /// Get a string from the pool
    pub async fn get_string(&self) -> PooledObject<String> {
        self.string_pool.get().await
    }

    /// Get comprehensive memory metrics
    pub async fn get_metrics(&self) -> Result<MemoryMetrics> {
        if !self.config.enable_monitoring {
            return Ok(MemoryMetrics::default());
        }

        let tx_metrics = self.transaction_pool.get_metrics().await;
        let account_metrics = self.account_pool.get_metrics().await;
        let buffer_metrics = self.buffer_pool.get_metrics().await;
        let string_metrics = self.string_pool.get_metrics().await;

        let total_allocated = tx_metrics.total_created + account_metrics.total_created 
            + buffer_metrics.total_created + string_metrics.total_created;
        let total_reused = tx_metrics.total_reused + account_metrics.total_reused 
            + buffer_metrics.total_reused + string_metrics.total_reused;

        // Estimate memory saved (rough calculation)
        let avg_object_size_kb = 4.0; // Average size assumption
        let memory_saved_mb = (total_reused as f64 * avg_object_size_kb) / 1024.0;

        let pool_efficiency = if total_allocated > 0 {
            total_reused as f64 / total_allocated as f64
        } else {
            0.0
        };

        let mut metrics = self.metrics.write().await;
        metrics.total_allocated_objects = total_allocated;
        metrics.total_reused_objects = total_reused;
        metrics.total_memory_saved_mb = memory_saved_mb;
        metrics.pool_efficiency = pool_efficiency;

        Ok(MemoryMetrics {
            total_allocated_objects: metrics.total_allocated_objects,
            total_reused_objects: metrics.total_reused_objects,
            total_memory_saved_mb: metrics.total_memory_saved_mb,
            pool_efficiency: metrics.pool_efficiency,
            gc_pressure: metrics.gc_pressure,
            allocation_rate: metrics.allocation_rate,
            deallocation_rate: metrics.deallocation_rate,
            last_gc: metrics.last_gc,
        })
    }

    /// Perform garbage collection if needed
    pub async fn maybe_gc(&self) -> Result<bool> {
        if !self.config.auto_gc_enabled {
            return Ok(false);
        }

        let metrics = self.get_metrics().await?;
        
        if metrics.pool_efficiency < (1.0 - self.config.gc_threshold) {
            self.perform_gc().await?;
            return Ok(true);
        }

        Ok(false)
    }

    /// Force garbage collection
    pub async fn perform_gc(&self) -> Result<()> {
        tracing::info!("Performing garbage collection on object pools");

        // Clear pools that have low efficiency
        let tx_metrics = self.transaction_pool.get_metrics().await;
        if tx_metrics.hit_rate < 0.3 {
            self.transaction_pool.clear().await;
        }

        let account_metrics = self.account_pool.get_metrics().await;
        if account_metrics.hit_rate < 0.3 {
            self.account_pool.clear().await;
        }

        let buffer_metrics = self.buffer_pool.get_metrics().await;
        if buffer_metrics.hit_rate < 0.3 {
            self.buffer_pool.clear().await;
        }

        let string_metrics = self.string_pool.get_metrics().await;
        if string_metrics.hit_rate < 0.3 {
            self.string_pool.clear().await;
        }

        // Update metrics
        if self.config.enable_monitoring {
            let mut metrics = self.metrics.write().await;
            metrics.last_gc = Some(chrono::Utc::now());
        }

        // Trigger JVM-like GC hint (though Rust doesn't have explicit GC)
        tokio::task::yield_now().await;

        Ok(())
    }

    /// Start memory monitoring
    fn start_monitoring(&self) {
        let manager = self.clone();
        let monitoring_interval = self.config.monitoring_interval;
        
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(monitoring_interval);
            
            loop {
                interval.tick().await;
                
                // Update metrics
                if let Ok(_) = manager.get_metrics().await {
                    // Metrics updated successfully
                }
                
                // Check if GC is needed
                if let Err(e) = manager.maybe_gc().await {
                    tracing::error!("Auto GC failed: {}", e);
                }
            }
        });
    }

    /// Get memory usage statistics
    pub async fn get_memory_usage(&self) -> MemoryUsageStats {
        let tx_metrics = self.transaction_pool.get_metrics().await;
        let account_metrics = self.account_pool.get_metrics().await;
        let buffer_metrics = self.buffer_pool.get_metrics().await;
        let string_metrics = self.string_pool.get_metrics().await;

        MemoryUsageStats {
            transaction_pool_size: tx_metrics.current_size,
            account_pool_size: account_metrics.current_size,
            buffer_pool_size: buffer_metrics.current_size,
            string_pool_size: string_metrics.current_size,
            total_pooled_objects: tx_metrics.current_size + account_metrics.current_size 
                + buffer_metrics.current_size + string_metrics.current_size,
            estimated_memory_mb: (tx_metrics.current_size * 1024 + account_metrics.current_size * 512 
                + buffer_metrics.current_size * 4 + string_metrics.current_size * 256) as f64 / 1024.0 / 1024.0,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct MemoryUsageStats {
    pub transaction_pool_size: usize,
    pub account_pool_size: usize,
    pub buffer_pool_size: usize,
    pub string_pool_size: usize,
    pub total_pooled_objects: usize,
    pub estimated_memory_mb: f64,
}

impl Clone for MemoryManager {
    fn clone(&self) -> Self {
        Self {
            transaction_pool: self.transaction_pool.clone(),
            account_pool: self.account_pool.clone(),
            buffer_pool: self.buffer_pool.clone(),
            string_pool: self.string_pool.clone(),
            metrics: self.metrics.clone(),
            config: self.config.clone(),
        }
    }
}