Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ monerod
books/*/book
fast_sync_hashes.bin
/books/user/Cuprated.toml
cuprate.toml
95 changes: 94 additions & 1 deletion binaries/cuprated/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
use std::{
fs::{read_to_string, File},
io,
net::{IpAddr, TcpListener},
path::Path,
str::FromStr,
time::Duration,
};

use args::Args;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: because the is a mod and not an external crate it should be grouped with the mod imports around line 40

use clap::Parser;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -61,7 +63,7 @@ const HEADER: &str = r"## ____ _

/// Reads the args & config file, returning a [`Config`].
pub fn read_config_and_args() -> Config {
let args = args::Args::parse();
let args = Args::parse();
args.do_quick_requests();

let config: Config = if let Some(config_file) = &args.config_file {
Expand Down Expand Up @@ -260,6 +262,62 @@ impl Config {
pub fn block_downloader_config(&self) -> BlockDownloaderConfig {
self.p2p.block_downloader.clone().into()
}

fn check_port(ip: IpAddr, port: u16) -> Result<(), String> {
match TcpListener::bind((ip, port)) {
Ok(_) => Ok(()),
Err(e) => Err(format!("Fail to bind {ip}:{port} : {e}")),
}
}

pub fn dry_run_check(args: &Args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to reduce duplicated code it would be better to call this function in read_config_and_args after the config is loaded there and not in do_quick_requests where it currently is

let config: Self = if let Some(config_file) = &args.config_file {
// If a config file was set in the args try to read it and exit if we can't.
match Self::read_from_path(config_file) {
Ok(config) => config,
Err(e) => {
eprintln_red(&format!("Failed to read config from file: {e}"));
std::process::exit(1);
}
}
} else {
// First attempt to read the config file from the current directory.
std::env::current_dir()
.map(|path| path.join(DEFAULT_CONFIG_FILE_NAME))
.map_err(Into::into)
.and_then(Self::read_from_path)
.inspect_err(|e| tracing::debug!("Failed to read config from current dir: {e}"))
// otherwise try the main config directory.
.or_else(|_| {
let file = CUPRATE_CONFIG_DIR.join(DEFAULT_CONFIG_FILE_NAME);
Self::read_from_path(file)
})
.inspect_err(|e| {
tracing::debug!("Failed to read config from config dir: {e}");
if !args.skip_config_warning {
eprintln_red(DEFAULT_CONFIG_WARNING);
std::thread::sleep(DEFAULT_CONFIG_STARTUP_DELAY);
}
})
.unwrap_or_default()
};
println!("Config file loaded.");

//If the ports are available
let ip = config.p2p.clear_net.listen_on;
let ports = vec![config.p2p.clear_net.general.p2p_port];
for port in ports {
match Self::check_port(ip, port) {
Ok(()) => println!("Port {ip}:{port} available."),
Err(e) => {
eprintln_red(&format!("Config file error: {e}"));
std::process::exit(1);
}
}
}
println!("Dry run done.");
std::process::exit(0);
}
}

#[cfg(test)]
Expand All @@ -275,4 +333,39 @@ mod test {

assert_eq!(conf, Config::default());
}

use std::io::Write;
use std::net::{IpAddr, TcpListener};
use std::path::Path;
use tempfile::NamedTempFile;
use toml::to_string;

#[test]

fn test_check_port() {
let port = 51000;
let ip = IpAddr::from([127, 0, 0, 1]);
let available_port = Config::check_port(ip, port);
assert!(available_port.is_ok());
let _listen = TcpListener::bind((ip, port)).expect("fail to bind to the port for test");
let unavailable_port = Config::check_port(ip, port);
assert!(unavailable_port.is_err());
}

#[test]
fn test_read_from_path() {
let config = Config::default();
let config_str = to_string(&config).unwrap();
let mut file = NamedTempFile::new().unwrap();
writeln!(file, "{config_str}").unwrap();
let path = file.path().to_path_buf();

// Test reading from the file
let read_config = Config::read_from_path(path).unwrap();
assert_eq!(read_config, config);

// Test reading from a non-existent file
let non_existent_path = Path::new("non_existent_file.toml");
assert!(Config::read_from_path(non_existent_path).is_err());
}
}
13 changes: 13 additions & 0 deletions binaries/cuprated/src/config/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ pub struct Args {
/// Print misc version information in JSON.
#[arg(short, long)]
pub version: bool,

/// Test the configuration file and exit.
/// `cuprated` will check the configuration for
/// correct syntax and valid values, then print
/// the configuration and exit successfully without
/// starting. If any errors occur, they will be
/// logged and `cuprated` and exit with an error code.
#[arg(long)]
pub dry_run: bool,
}

impl Args {
Expand All @@ -64,6 +73,10 @@ impl Args {
println!("{}", Config::documented_config());
exit(0);
}

if self.dry_run {
Config::dry_run_check(self);
}
}

/// Apply the [`Args`] to the given [`Config`].
Expand Down