agave_validator/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2pub use solana_test_validator as test_validator;
3use {
4    console::style,
5    fd_lock::{RwLock, RwLockWriteGuard},
6    indicatif::{ProgressDrawTarget, ProgressStyle},
7    std::{
8        borrow::Cow,
9        fmt::Display,
10        fs::{File, OpenOptions},
11        path::Path,
12        process::exit,
13        time::Duration,
14    },
15};
16
17pub mod admin_rpc_service;
18pub mod bootstrap;
19pub mod cli;
20pub mod commands;
21pub mod dashboard;
22
23pub fn format_name_value(name: &str, value: &str) -> String {
24    format!("{} {}", style(name).bold(), value)
25}
26/// Pretty print a "name value"
27pub fn println_name_value(name: &str, value: &str) {
28    println!("{}", format_name_value(name, value));
29}
30
31/// Creates a new process bar for processing that will take an unknown amount of time
32pub fn new_spinner_progress_bar() -> ProgressBar {
33    let progress_bar = indicatif::ProgressBar::new(42);
34    progress_bar.set_draw_target(ProgressDrawTarget::stdout());
35    progress_bar.set_style(
36        ProgressStyle::default_spinner()
37            .template("{spinner:.green} {wide_msg}")
38            .expect("ProgresStyle::template direct input to be correct"),
39    );
40    progress_bar.enable_steady_tick(Duration::from_millis(100));
41
42    ProgressBar {
43        progress_bar,
44        is_term: console::Term::stdout().is_term(),
45    }
46}
47
48pub struct ProgressBar {
49    progress_bar: indicatif::ProgressBar,
50    is_term: bool,
51}
52
53impl ProgressBar {
54    pub fn set_message<T: Into<Cow<'static, str>> + Display>(&self, msg: T) {
55        if self.is_term {
56            self.progress_bar.set_message(msg);
57        } else {
58            println!("{msg}");
59        }
60    }
61
62    pub fn println<I: AsRef<str>>(&self, msg: I) {
63        self.progress_bar.println(msg);
64    }
65
66    pub fn abandon_with_message<T: Into<Cow<'static, str>> + Display>(&self, msg: T) {
67        if self.is_term {
68            self.progress_bar.abandon_with_message(msg);
69        } else {
70            println!("{msg}");
71        }
72    }
73}
74
75pub fn ledger_lockfile(ledger_path: &Path) -> RwLock<File> {
76    let lockfile = ledger_path.join("ledger.lock");
77    fd_lock::RwLock::new(
78        OpenOptions::new()
79            .write(true)
80            .create(true)
81            .truncate(false)
82            .open(lockfile)
83            .unwrap(),
84    )
85}
86
87pub fn lock_ledger<'lock>(
88    ledger_path: &Path,
89    ledger_lockfile: &'lock mut RwLock<File>,
90) -> RwLockWriteGuard<'lock, File> {
91    ledger_lockfile.try_write().unwrap_or_else(|_| {
92        println!(
93            "Error: Unable to lock {} directory. Check if another validator is running",
94            ledger_path.display()
95        );
96        exit(1);
97    })
98}