From fa241a4d4a0b344707d4a1b5a5653a0d08c0a59e Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Fri, 2 Jul 2021 14:14:39 +0200 Subject: [PATCH 1/5] feat: automated node config path args The user previously had to specify the location of our generated config file. This change removes this requirement -- since we generate it, we should also pass on the information to the node. --- README.md | 4 ++-- src/setup/node.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2d28a6c4..4cc94c89 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,11 @@ Ziggurat is configured with a `config.toml` file in the root. ```toml kind = "zebra" path = "path/to/zebra/repo" -start_command = "cargo +stable r -- --config zebra.toml --verbose start" +start_command = "cargo +stable r -- --verbose start" # kind = "zcashd" # path = "path/to/zcash/repo" -# start_command = "./src/zcashd -debug=1 -dnsseed=0 -printtoconsole -logips=1 -listenonion=0 -dns=0 -conf=/path/to/zcash/repo/zcash.conf" +# start_command = "./src/zcashd -debug=1 -dnsseed=0 -printtoconsole -logips=1 -listenonion=0 -dns=0" ``` Information about the node to be tested can be set under the `[node]` table: diff --git a/src/setup/node.rs b/src/setup/node.rs index 9242582f..43ad5ad1 100644 --- a/src/setup/node.rs +++ b/src/setup/node.rs @@ -67,11 +67,35 @@ impl Node { let config = NodeConfig::new(); let meta = NodeMetaData::new()?; - Ok(Self { + let mut node = Self { config, meta, process: None, - }) + }; + + // insert the config file cmd args + match node.meta.kind { + NodeKind::Zebra => { + // Bit more convoluted since we need to insert the args before `start`, which comes last. + let start_arg = node + .meta + .start_args + .pop() + .expect("Expected at least one arg for Zebra (`start`)"); + node.meta.start_args.push("--config".into()); + node.meta + .start_args + .push(node.config_filepath().into_os_string()); + node.meta.start_args.push(start_arg); + } + NodeKind::Zcashd => { + node.meta + .start_args + .push(format!("-conf={}", node.config_filepath().to_str().unwrap()).into()); + } + } + + Ok(node) } /// Returns the (external) address of the node. From fd4f1d98395c4ef15b892fd3a9fd12452fd018dc Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Fri, 2 Jul 2021 15:22:03 +0200 Subject: [PATCH 2/5] feat: simplified zebra config path --- src/setup/node.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/setup/node.rs b/src/setup/node.rs index 43ad5ad1..b0fafef0 100644 --- a/src/setup/node.rs +++ b/src/setup/node.rs @@ -76,17 +76,12 @@ impl Node { // insert the config file cmd args match node.meta.kind { NodeKind::Zebra => { - // Bit more convoluted since we need to insert the args before `start`, which comes last. - let start_arg = node - .meta - .start_args - .pop() - .expect("Expected at least one arg for Zebra (`start`)"); - node.meta.start_args.push("--config".into()); + let n = node.meta.start_args.len(); + assert!(n > 1, "Expected at least one arg for Zebra (`start`)"); + node.meta.start_args.insert(n - 1, "--config".into()); node.meta .start_args - .push(node.config_filepath().into_os_string()); - node.meta.start_args.push(start_arg); + .insert(n, node.config_filepath().into_os_string()); } NodeKind::Zcashd => { node.meta From 14e61451d337ce9e61a9202cfdcbaeefedaec99d Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Fri, 2 Jul 2021 15:22:33 +0200 Subject: [PATCH 3/5] feat: config file no longer pollutes node repo --- src/setup/node.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/setup/node.rs b/src/setup/node.rs index b0fafef0..b0896242 100644 --- a/src/setup/node.rs +++ b/src/setup/node.rs @@ -303,8 +303,8 @@ impl Node { fn config_filepath(&self) -> std::path::PathBuf { match self.meta.kind { - NodeKind::Zebra => self.meta.path.join(ZEBRA_CONFIG), - NodeKind::Zcashd => self.meta.path.join(ZCASHD_CONFIG), + NodeKind::Zebra => std::env::current_dir().unwrap().join(ZEBRA_CONFIG), + NodeKind::Zcashd => std::env::current_dir().unwrap().join(ZCASHD_CONFIG), } } From f40f46327bbecd42eb03a03fba018d0623fd1bf0 Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Mon, 5 Jul 2021 11:57:03 +0200 Subject: [PATCH 4/5] ref: move node config args to NodeMetaData --- src/setup/config.rs | 29 +++++++++++++++++++++++++++++ src/setup/node.rs | 38 ++++---------------------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/setup/config.rs b/src/setup/config.rs index abea3c17..3b3010ef 100644 --- a/src/setup/config.rs +++ b/src/setup/config.rs @@ -11,6 +11,10 @@ use std::{ use crate::setup::node::Action; +// The names of the files the node configurations will be written to. +const ZEBRA_CONFIG: &str = "zebra.toml"; +const ZCASHD_CONFIG: &str = "zcash.conf"; + const CONFIG: &str = "config.toml"; const DEFAULT_PORT: u16 = 8080; @@ -65,6 +69,16 @@ pub(super) enum NodeKind { Zcashd, } +impl NodeKind { + /// Path to the configuration file for this [NodeKind] + pub(super) fn config_filepath(&self) -> std::path::PathBuf { + match self { + NodeKind::Zebra => std::env::current_dir().unwrap().join(ZEBRA_CONFIG), + NodeKind::Zcashd => std::env::current_dir().unwrap().join(ZCASHD_CONFIG), + } + } +} + /// Node configuration read from the `config.toml` file. #[derive(Clone)] pub(super) struct NodeMetaData { @@ -91,6 +105,21 @@ impl NodeMetaData { let mut start_args = args_from(&config_file.start_command); let start_command = start_args.remove(0); + // insert the config file path into start args + let config_path = config_file.kind.config_filepath(); + match config_file.kind { + NodeKind::Zebra => { + // Zebra's final arg must be `start`, so we insert the actual args before it. + let n = start_args.len(); + assert!(n > 1, "Expected at least one arg for Zebra (`start`)"); + start_args.insert(n - 1, "--config".into()); + start_args.insert(n, config_path.into_os_string()); + } + NodeKind::Zcashd => { + start_args.push(format!("-conf={}", config_path.to_str().unwrap()).into()); + } + } + Ok(Self { kind: config_file.kind, path: config_file.path, diff --git a/src/setup/node.rs b/src/setup/node.rs index b0896242..28f23bdc 100644 --- a/src/setup/node.rs +++ b/src/setup/node.rs @@ -17,10 +17,6 @@ use tokio::process::{Child, Command}; use std::{fs, io, net::SocketAddr, process::Stdio, time::Duration}; -// The names of the files the node configurations will be written to. -const ZEBRA_CONFIG: &str = "zebra.toml"; -const ZCASHD_CONFIG: &str = "zcash.conf"; - /// Actions to prepare node state on start. pub enum Action { /// Performs no action @@ -67,30 +63,11 @@ impl Node { let config = NodeConfig::new(); let meta = NodeMetaData::new()?; - let mut node = Self { + Ok(Self { config, meta, process: None, - }; - - // insert the config file cmd args - match node.meta.kind { - NodeKind::Zebra => { - let n = node.meta.start_args.len(); - assert!(n > 1, "Expected at least one arg for Zebra (`start`)"); - node.meta.start_args.insert(n - 1, "--config".into()); - node.meta - .start_args - .insert(n, node.config_filepath().into_os_string()); - } - NodeKind::Zcashd => { - node.meta - .start_args - .push(format!("-conf={}", node.config_filepath().to_str().unwrap()).into()); - } - } - - Ok(node) + }) } /// Returns the (external) address of the node. @@ -291,7 +268,7 @@ impl Node { } fn generate_config_file(&self) -> io::Result<()> { - let path = self.config_filepath(); + let path = self.meta.kind.config_filepath(); let content = match self.meta.kind { NodeKind::Zebra => ZebraConfigFile::generate(&self.config) .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?, @@ -301,20 +278,13 @@ impl Node { fs::write(path, content) } - fn config_filepath(&self) -> std::path::PathBuf { - match self.meta.kind { - NodeKind::Zebra => std::env::current_dir().unwrap().join(ZEBRA_CONFIG), - NodeKind::Zcashd => std::env::current_dir().unwrap().join(ZCASHD_CONFIG), - } - } - fn cleanup(&self) -> io::Result<()> { self.cleanup_config_file()?; self.cleanup_cache() } fn cleanup_config_file(&self) -> io::Result<()> { - let path = self.config_filepath(); + let path = self.meta.kind.config_filepath(); match fs::remove_file(path) { // File may not exist, so we surpress the error. Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e), From 5f8503fb6e4b56e896f4f4590465c259d4958e4f Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Mon, 5 Jul 2021 12:14:39 +0200 Subject: [PATCH 5/5] fix: err instead of panic when too few zebra args --- src/setup/config.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/setup/config.rs b/src/setup/config.rs index 3b3010ef..40983768 100644 --- a/src/setup/config.rs +++ b/src/setup/config.rs @@ -110,10 +110,15 @@ impl NodeMetaData { match config_file.kind { NodeKind::Zebra => { // Zebra's final arg must be `start`, so we insert the actual args before it. - let n = start_args.len(); - assert!(n > 1, "Expected at least one arg for Zebra (`start`)"); - start_args.insert(n - 1, "--config".into()); - start_args.insert(n, config_path.into_os_string()); + let n_args = start_args.len(); + if n_args < 1 { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "Expected at least one start_command arg for Zebra (`start`)", + )); + } + start_args.insert(n_args - 1, "--config".into()); + start_args.insert(n_args, config_path.into_os_string()); } NodeKind::Zcashd => { start_args.push(format!("-conf={}", config_path.to_str().unwrap()).into());