Skip to main content

snarkos_cli/helpers/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod bech32m;
17pub use bech32m::*;
18
19mod log_writer;
20use log_writer::*;
21
22mod dynamic_format;
23use dynamic_format::*;
24
25pub(crate) mod args;
26
27pub mod logger;
28pub use logger::*;
29
30pub mod dev;
31
32pub mod updater;
33pub use updater::*;
34
35use snarkos_node::network::NodeType;
36
37use anyhow::Result;
38use colored::*;
39#[cfg(target_family = "unix")]
40use nix::sys::resource::{Resource, getrlimit};
41
42/// Check if process's open files limit is above minimum and warn if not.
43#[cfg(target_family = "unix")]
44pub fn check_open_files_limit(minimum: u64) {
45    // Acquire current limits.
46    match getrlimit(Resource::RLIMIT_NOFILE) {
47        Ok((soft_limit, _)) => {
48            // Check if requirements are met.
49            if soft_limit < minimum {
50                // Warn about too low limit.
51                let warning = [
52                    format!("⚠️  The open files limit ({soft_limit}) for this process is lower than recommended."),
53                    format!("  • To ensure correct behavior of the node, please raise it to at least {minimum}."),
54                    "  • See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
55                ]
56                .join("\n")
57                .yellow()
58                .bold();
59                eprintln!("{warning}\n");
60            }
61        }
62        Err(err) => {
63            // Warn about unknown limit.
64            let warning = [
65                format!("⚠️  Unable to check the open files limit for this process due to {err}."),
66                format!("  • To ensure correct behavior of the node, please ensure it is at least {minimum}."),
67                "  • See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
68            ]
69            .join("\n")
70            .yellow()
71            .bold();
72            eprintln!("{warning}\n");
73        }
74    };
75}
76
77/// Returns the RAM memory in GiB.
78pub(crate) fn detect_ram_memory() -> Result<u64, sys_info::Error> {
79    let ram_kib = sys_info::mem_info()?.total;
80    let ram_mib = ram_kib / 1024;
81    Ok(ram_mib / 1024)
82}
83
84/// Ensures the current system meets the minimum requirements for a validator.
85/// Note: Some of the checks in this method are overly-permissive, in order to ensure
86/// future hardware architecture changes do not prevent validators from running a node.
87#[rustfmt::skip]
88pub(crate) fn check_validator_machine(node_type: NodeType) {
89    // If the node is a validator, ensure it meets the minimum requirements.
90    if node_type.is_validator() {
91        // Ensure the system is a Linux-based system.
92        // Note: While macOS is not officially supported, we allow it for development purposes.
93        if !cfg!(target_os = "linux") && !cfg!(target_os = "macos") {
94            let message = "⚠️  The operating system of this machine is not supported for a validator (Ubuntu required)\n".to_string();
95            println!("{}", message.yellow().bold());
96        }
97        // Retrieve the number of cores.
98        let num_cores = num_cpus::get();
99        // Enforce the minimum number of cores.
100        let min_num_cores = 64;
101        if num_cores < min_num_cores {
102            let message = format!("⚠️  The number of cores ({num_cores} cores) on this machine is insufficient for a validator (minimum {min_num_cores} cores)\n");
103            println!("{}", message.yellow().bold());
104        }
105        // Enforce the minimum amount of RAM.
106        if let Ok(ram) = crate::helpers::detect_ram_memory() {
107            let min_ram = 256;
108            if ram < min_ram {
109                let message = format!("⚠️  The amount of RAM ({ram} GiB) on this machine is insufficient for a validator (minimum {min_ram} GiB)\n");
110                println!("{}", message.yellow().bold());
111            }
112        }
113    }
114}