osvm 0.8.3

OpenSVM CLI tool for managing SVM nodes and deployments
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
//! Mainnet RPC node deployment and management
//!
//! This module provides functionality to deploy and manage a mainnet RPC node
//! that syncs with the real Solana mainnet-beta blockchain.

use crate::utils::log_monitor::{monitor_log_file, LogMonitorConfig};
use crate::utils::osvm_logger::LogCategory;
use crate::{osvm_error, osvm_info};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};

/// Configuration for mainnet RPC node deployment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MainnetRpcConfig {
    pub ledger_path: String,
    pub rpc_port: u16,
    pub gossip_port: u16,
    pub background: bool,
}

/// Information about a running mainnet RPC node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MainnetRpcInfo {
    pub ledger_path: String,
    pub rpc_port: u16,
    pub gossip_port: u16,
    pub pid: Option<u32>,
}

/// Status information for mainnet RPC node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MainnetRpcStatus {
    pub running: bool,
    pub network: String,
    pub pid: Option<u32>,
    pub rpc_port: Option<u16>,
    pub syncing: bool,
    pub slot_height: Option<u64>,
    pub behind_by: Option<u64>,
}

/// Known mainnet entrypoints
const MAINNET_ENTRYPOINTS: &[&str] = &[
    "entrypoint.mainnet-beta.solana.com:8001",
    "entrypoint2.mainnet-beta.solana.com:8001",
    "entrypoint3.mainnet-beta.solana.com:8001",
    "entrypoint4.mainnet-beta.solana.com:8001",
    "entrypoint5.mainnet-beta.solana.com:8001",
];

/// Start a mainnet RPC node
pub async fn start_mainnet_rpc(config: MainnetRpcConfig) -> Result<MainnetRpcInfo> {
    osvm_info!(
        LogCategory::Rpc,
        "start_mainnet_rpc",
        "Starting mainnet-beta RPC node"
    );
    println!("🚀 Starting Solana Mainnet-Beta RPC Node");
    println!("========================================");
    println!("⚠️  WARNING: Mainnet sync requires:");
    println!("   • 2-3TB of fast NVMe storage");
    println!("   • 256GB+ RAM (128GB minimum)");
    println!("   • High-speed internet (1Gbps+)");
    println!("   • Several days to weeks for initial sync");
    println!();

    // Create ledger directory
    fs::create_dir_all(&config.ledger_path).context("Failed to create ledger directory")?;

    // Check disk space
    let disk_space_output = Command::new("df")
        .arg("-BG")
        .arg(&config.ledger_path)
        .output()
        .context("Failed to check disk space")?;

    if disk_space_output.status.success() {
        let output_str = String::from_utf8_lossy(&disk_space_output.stdout);
        if let Some(line) = output_str.lines().nth(1) {
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() >= 4 {
                if let Ok(available_gb) = parts[3].trim_end_matches('G').parse::<u64>() {
                    if available_gb < 2000 {
                        println!("⚠️  WARNING: Only {}GB available. Mainnet requires 2000GB+ free space!", available_gb);
                        println!("💡 Consider using a dedicated disk for the ledger.");
                    } else {
                        println!("✅ Disk space: {}GB available", available_gb);
                    }
                }
            }
        }
    }

    // Build agave-validator command for mainnet RPC
    let mut cmd = Command::new("agave-validator");

    // Basic mainnet configuration
    cmd.arg("--identity")
        .arg("/home/larp/.config/solana/id.json")
        .arg("--ledger")
        .arg(&config.ledger_path)
        .arg("--rpc-port")
        .arg(config.rpc_port.to_string())
        .arg("--gossip-port")
        .arg(config.gossip_port.to_string())
        .arg("--dynamic-port-range")
        .arg("8002-8020")
        .arg("--log")
        .arg("-"); // Log to stdout

    // Add all mainnet entrypoints
    for entrypoint in MAINNET_ENTRYPOINTS {
        cmd.arg("--entrypoint").arg(entrypoint);
    }

    // Add known mainnet validators for bootstrap
    cmd.arg("--known-validator")
        .arg("7Np41oeYqPefeNQEHSv1UDhYrehxin3NStELsSKCT4K2") // Figment
        .arg("--known-validator")
        .arg("GdnSyH3YtwcxFvQrVVJMm1JhTS4QVX7MFsX56uJLUfiZ") // Figment
        .arg("--known-validator")
        .arg("DE1bawNcRJB9rVm3buyMVfr8mBEoyyu73NBovf2oXJsJ") // Bison Trails
        .arg("--known-validator")
        .arg("CakcnaRDHka2gXyfbEd2d3xsvkJkqsLw2akB3zsN1D2S") // Canonical
        .arg("--expected-genesis-hash")
        .arg("5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d");

    // RPC node specific settings
    cmd.arg("--no-voting") // Don't vote, we're just an RPC node
        .arg("--enable-rpc-transaction-history") // Enable full history
        .arg("--enable-extended-tx-metadata-storage") // Store extended metadata
        .arg("--rpc-bind-address")
        .arg("0.0.0.0") // Bind to all interfaces
        .arg("--private-rpc") // Don't register with public RPC list
        .arg("--full-rpc-api") // Enable all RPC methods
        .arg("--account-index")
        .arg("program-id") // Index by program ID
        .arg("--account-index-exclude-key")
        .arg("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA") // Exclude token accounts
        .arg("--account-index-exclude-key")
        .arg("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"); // Exclude token-2022 accounts

    // Performance settings
    cmd.arg("--limit-ledger-size")
        .arg("500000000") // Limit to ~250GB to start
        .arg("--accounts-db-cache-limit-mb")
        .arg("10000") // 10GB accounts cache
        .arg("--max-genesis-archive-unpacked-size")
        .arg("10737418240") // 10GB max genesis
        .arg("--no-port-check"); // Skip port reachability check for non-voting nodes

    println!("📋 Command: {:?}", cmd);
    println!();

    if config.background {
        // Run in background
        println!("🔧 Starting validator in background mode...");

        // Create log file
        let log_file_path = format!(
            "agave-validator-mainnet-{}.log",
            chrono::Utc::now().timestamp()
        );
        let log_file = fs::File::create(&log_file_path).context("Failed to create log file")?;

        let child = cmd
            .stdout(Stdio::from(log_file.try_clone()?))
            .stderr(Stdio::from(log_file))
            .spawn()
            .context("Failed to start agave-validator")?;

        let pid = child.id();

        println!("✅ Mainnet RPC node started in background");
        println!("🆔 Process ID: {}", pid);
        println!("📁 Ledger: {}", config.ledger_path);
        println!("🌐 RPC Port: {}", config.rpc_port);
        println!("📝 Log file: {}", log_file_path);
        println!();
        println!("⏳ Initial snapshot download may take several hours...");
        println!("🔧 Use 'osvm rpc-manager mainnet --status' to check sync progress");
        println!("📋 Use 'osvm rpc-manager mainnet --logs --follow' to watch logs");
        println!("🛑 Use 'osvm rpc-manager mainnet --stop' to stop the node");

        // Start log monitoring in background
        let log_monitor_config = LogMonitorConfig {
            auto_fix_enabled: true,
            restart_on_critical: false, // Don't auto-restart mainnet nodes
            max_restart_attempts: 0,
            restart_cooldown_seconds: 300,
        };

        let log_path_clone = log_file_path.clone();
        tokio::spawn(async move {
            if let Err(e) = monitor_log_file(&log_path_clone, log_monitor_config, None).await {
                osvm_error!(
                    LogCategory::Rpc,
                    "start_mainnet_rpc",
                    &format!("Log monitor error: {}", e)
                );
            }
        });

        Ok(MainnetRpcInfo {
            ledger_path: config.ledger_path,
            rpc_port: config.rpc_port,
            gossip_port: config.gossip_port,
            pid: Some(pid),
        })
    } else {
        // Run in foreground
        println!("🎯 Starting validator in foreground mode...");
        println!("ℹ️  Press Ctrl+C to stop");
        println!();

        let mut child = cmd
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to start agave-validator")?;

        // Stream stdout
        if let Some(stdout) = child.stdout.take() {
            tokio::task::spawn_blocking(move || {
                let reader = BufReader::new(stdout);
                for line in reader.lines() {
                    if let Ok(line) = line {
                        println!("{}", line);
                    }
                }
            });
        }

        // Stream stderr
        if let Some(stderr) = child.stderr.take() {
            tokio::task::spawn_blocking(move || {
                let reader = BufReader::new(stderr);
                for line in reader.lines() {
                    if let Ok(line) = line {
                        eprintln!("{}", line);
                    }
                }
            });
        }

        // Wait for process to complete
        let status = child.wait().context("Failed to wait for agave-validator")?;

        if status.success() {
            println!("✅ Mainnet RPC node exited normally");
        } else {
            eprintln!("❌ Mainnet RPC node exited with error: {}", status);
        }

        Ok(MainnetRpcInfo {
            ledger_path: config.ledger_path,
            rpc_port: config.rpc_port,
            gossip_port: config.gossip_port,
            pid: None,
        })
    }
}

/// Stop the mainnet RPC node
pub async fn stop_mainnet_rpc() -> Result<()> {
    osvm_info!(
        LogCategory::Rpc,
        "stop_mainnet_rpc",
        "Stopping mainnet RPC node"
    );
    println!("🛑 Stopping mainnet RPC node...");

    // Find agave-validator process
    let pgrep_output = Command::new("pgrep")
        .arg("-f")
        .arg("agave-validator.*mainnet")
        .output()
        .context("Failed to execute pgrep")?;

    if !pgrep_output.status.success() || pgrep_output.stdout.is_empty() {
        println!("⚠️  No mainnet RPC node process found");
        return Ok(());
    }

    // Parse PIDs and kill processes
    let pids = String::from_utf8_lossy(&pgrep_output.stdout);
    for pid_str in pids.lines() {
        if let Ok(pid) = pid_str.trim().parse::<u32>() {
            println!("🔧 Stopping process {}", pid);
            let _ = Command::new("kill")
                .arg("-TERM")
                .arg(pid.to_string())
                .output();
        }
    }

    // Wait a moment for graceful shutdown
    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

    // Force kill if still running
    let check_output = Command::new("pgrep")
        .arg("-f")
        .arg("agave-validator.*mainnet")
        .output()?;

    if check_output.status.success() && !check_output.stdout.is_empty() {
        println!("⚠️  Process still running, force killing...");
        let _ = Command::new("pkill")
            .arg("-9")
            .arg("-f")
            .arg("agave-validator.*mainnet")
            .output();
    }

    println!("✅ Mainnet RPC node stopped successfully");
    Ok(())
}

/// Check the status of mainnet RPC node
pub async fn check_mainnet_rpc_status() -> Result<MainnetRpcStatus> {
    // Check if agave-validator is running
    let pgrep_output = Command::new("pgrep")
        .arg("-f")
        .arg("agave-validator.*mainnet")
        .output()
        .context("Failed to execute pgrep")?;

    if !pgrep_output.status.success() || pgrep_output.stdout.is_empty() {
        return Ok(MainnetRpcStatus {
            running: false,
            network: "mainnet-beta".to_string(),
            pid: None,
            rpc_port: None,
            syncing: false,
            slot_height: None,
            behind_by: None,
        });
    }

    // Parse PID
    let pid_str = String::from_utf8_lossy(&pgrep_output.stdout);
    let pid = pid_str
        .lines()
        .next()
        .and_then(|line| line.trim().parse::<u32>().ok());

    // Try to get RPC status
    let rpc_check = Command::new("curl")
        .arg("-s")
        .arg("-X")
        .arg("POST")
        .arg("-H")
        .arg("Content-Type: application/json")
        .arg("-d")
        .arg(r#"{"jsonrpc":"2.0","id":1,"method":"getHealth"}"#)
        .arg("http://localhost:8899")
        .output();

    let rpc_healthy = rpc_check
        .map(|o| o.status.success() && String::from_utf8_lossy(&o.stdout).contains("\"ok\""))
        .unwrap_or(false);

    // Get slot height if RPC is healthy
    let mut slot_height = None;
    let mut behind_by = None;

    if rpc_healthy {
        // Get current slot
        let slot_output = Command::new("curl")
            .arg("-s")
            .arg("-X")
            .arg("POST")
            .arg("-H")
            .arg("Content-Type: application/json")
            .arg("-d")
            .arg(r#"{"jsonrpc":"2.0","id":1,"method":"getSlot"}"#)
            .arg("http://localhost:8899")
            .output();

        if let Ok(output) = slot_output {
            if output.status.success() {
                let response = String::from_utf8_lossy(&output.stdout);
                // Parse JSON to get slot number
                if let Some(start) = response.find("\"result\":") {
                    let result_part = &response[start + 9..];
                    if let Some(end) = result_part.find(|c: char| !c.is_numeric()) {
                        if let Ok(slot) = result_part[..end].parse::<u64>() {
                            slot_height = Some(slot);
                        }
                    }
                }
            }
        }

        // Check how far behind we are
        if slot_height.is_some() {
            // Get slot from public RPC
            let public_slot_output = Command::new("curl")
                .arg("-s")
                .arg("-X")
                .arg("POST")
                .arg("-H")
                .arg("Content-Type: application/json")
                .arg("-d")
                .arg(r#"{"jsonrpc":"2.0","id":1,"method":"getSlot"}"#)
                .arg("https://api.mainnet-beta.solana.com")
                .output();

            if let Ok(output) = public_slot_output {
                if output.status.success() {
                    let response = String::from_utf8_lossy(&output.stdout);
                    if let Some(start) = response.find("\"result\":") {
                        let result_part = &response[start + 9..];
                        if let Some(end) = result_part.find(|c: char| !c.is_numeric()) {
                            if let Ok(public_slot) = result_part[..end].parse::<u64>() {
                                if let Some(local_slot) = slot_height {
                                    behind_by = Some(public_slot.saturating_sub(local_slot));
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(MainnetRpcStatus {
        running: true,
        network: "mainnet-beta".to_string(),
        pid,
        rpc_port: if rpc_healthy { Some(8899) } else { None },
        syncing: slot_height.is_some(),
        slot_height,
        behind_by,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mainnet_rpc_config() {
        let config = MainnetRpcConfig {
            ledger_path: "mainnet-ledger".to_string(),
            rpc_port: 8899,
            gossip_port: 8001,
            background: true,
        };

        assert_eq!(config.ledger_path, "mainnet-ledger");
        assert_eq!(config.rpc_port, 8899);
        assert_eq!(config.gossip_port, 8001);
        assert!(config.background);
    }
}