snarkos_cli/helpers/
mod.rs

1// Copyright 2024 Aleo Network Foundation
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 mod logger;
26pub use logger::*;
27
28pub mod updater;
29pub use updater::*;
30
31use snarkos_node::router::messages::NodeType;
32
33use colored::*;
34#[cfg(target_family = "unix")]
35use nix::sys::resource::{Resource, getrlimit};
36
37/// Check if process's open files limit is above minimum and warn if not.
38#[cfg(target_family = "unix")]
39pub fn check_open_files_limit(minimum: u64) {
40    // Acquire current limits.
41    match getrlimit(Resource::RLIMIT_NOFILE) {
42        Ok((soft_limit, _)) => {
43            // Check if requirements are met.
44            if soft_limit < minimum {
45                // Warn about too low limit.
46                let warning = [
47                    format!("⚠️  The open files limit ({soft_limit}) for this process is lower than recommended."),
48                    format!("  • To ensure correct behavior of the node, please raise it to at least {minimum}."),
49                    "  • See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
50                ]
51                .join("\n")
52                .yellow()
53                .bold();
54                eprintln!("{warning}\n");
55            }
56        }
57        Err(err) => {
58            // Warn about unknown limit.
59            let warning = [
60                format!("⚠️  Unable to check the open files limit for this process due to {err}."),
61                format!("  • To ensure correct behavior of the node, please ensure it is at least {minimum}."),
62                "  • See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
63            ]
64            .join("\n")
65            .yellow()
66            .bold();
67            eprintln!("{warning}\n");
68        }
69    };
70}
71
72/// Returns the RAM memory in GiB.
73pub(crate) fn detect_ram_memory() -> Result<u64, sys_info::Error> {
74    let ram_kib = sys_info::mem_info()?.total;
75    let ram_mib = ram_kib / 1024;
76    Ok(ram_mib / 1024)
77}
78
79/// Ensures the current system meets the minimum requirements for a validator.
80/// Note: Some of the checks in this method are overly-permissive, in order to ensure
81/// future hardware architecture changes do not prevent validators from running a node.
82#[rustfmt::skip]
83pub(crate) fn check_validator_machine(node_type: NodeType) {
84    // If the node is a validator, ensure it meets the minimum requirements.
85    if node_type.is_validator() {
86        // Ensure the system is a Linux-based system.
87        // Note: While macOS is not officially supported, we allow it for development purposes.
88        if !cfg!(target_os = "linux") && !cfg!(target_os = "macos") {
89            let message = "⚠️  The operating system of this machine is not supported for a validator (Ubuntu required)\n".to_string();
90            println!("{}", message.yellow().bold());
91        }
92        // Retrieve the number of cores.
93        let num_cores = num_cpus::get();
94        // Enforce the minimum number of cores.
95        let min_num_cores = 64;
96        if num_cores < min_num_cores {
97            let message = format!("⚠️  The number of cores ({num_cores} cores) on this machine is insufficient for a validator (minimum {min_num_cores} cores)\n");
98            println!("{}", message.yellow().bold());
99        }
100        // Enforce the minimum amount of RAM.
101        if let Ok(ram) = crate::helpers::detect_ram_memory() {
102            let min_ram = 256;
103            if ram < min_ram {
104                let message = format!("⚠️  The amount of RAM ({ram} GiB) on this machine is insufficient for a validator (minimum {min_ram} GiB)\n");
105                println!("{}", message.yellow().bold());
106            }
107        }
108    }
109}