diff --git a/Cargo.lock b/Cargo.lock index 83b3b597..b9956840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -688,6 +688,7 @@ dependencies = [ "anyhow", "cdk-config", "clap", + "colored", "dotenvy", "execute", "reqwest 0.12.8", @@ -851,6 +852,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "combine" version = "4.6.7" diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index 964d8f83..0c1f8274 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -14,6 +14,7 @@ toml = "0.8.14" tracing.workspace = true tracing-subscriber = { workspace = true, features = ["env-filter", "json"] } url = { workspace = true, features = ["serde"] } +colored = "2.0" cdk-config = { path = "../cdk-config" } diff --git a/crates/cdk/src/cli.rs b/crates/cdk/src/cli.rs index 12acc8a8..5b61bd9e 100644 --- a/crates/cdk/src/cli.rs +++ b/crates/cdk/src/cli.rs @@ -5,25 +5,8 @@ use clap::{Parser, Subcommand, ValueHint}; /// Command line interface. #[derive(Parser)] +#[command(author, version, about, long_about = None)] pub(crate) struct Cli { - /// The path to the configuration file. - #[arg( - long, - short, - value_hint = ValueHint::FilePath, - env = "CDK_CONFIG_PATH" - )] - pub(crate) config: PathBuf, - - /// The path to a chain specification file. - #[arg( - long, - short = 'g', - value_hint = ValueHint::FilePath, - env = "CDK_GENESIS_PATH" - )] - pub(crate) chain: PathBuf, - #[command(subcommand)] pub(crate) cmd: Commands, } @@ -31,6 +14,15 @@ pub(crate) struct Cli { #[derive(Subcommand)] pub(crate) enum Commands { Node { + /// The path to the configuration file. + #[arg( + long, + short, + value_hint = ValueHint::FilePath, + env = "CDK_CONFIG_PATH" + )] + config: PathBuf, + /// Components to run. #[arg( long, @@ -40,5 +32,24 @@ pub(crate) enum Commands { )] components: Option, }, - Erigon, + Erigon { + /// The path to the configuration file. + #[arg( + long, + short, + value_hint = ValueHint::FilePath, + env = "CDK_CONFIG_PATH" + )] + config: PathBuf, + + /// The path to a chain specification file. + #[arg( + long, + short = 'g', + value_hint = ValueHint::FilePath, + env = "CDK_GENESIS_PATH" + )] + chain: PathBuf, + }, + Versions, } diff --git a/crates/cdk/src/helpers.rs b/crates/cdk/src/helpers.rs new file mode 100644 index 00000000..4fac948b --- /dev/null +++ b/crates/cdk/src/helpers.rs @@ -0,0 +1,13 @@ +use std::env; + +const CDK_CLIENT_BIN: &str = "cdk-node"; + +pub(crate) fn get_bin_path() -> String { + // This is to find the binary when running in development mode + // otherwise it will use system path + let mut bin_path = env::var("CARGO_MANIFEST_DIR").unwrap_or(CDK_CLIENT_BIN.into()); + if bin_path != CDK_CLIENT_BIN { + bin_path = format!("{}/../../target/{}", bin_path, CDK_CLIENT_BIN); + } + bin_path +} diff --git a/crates/cdk/src/main.rs b/crates/cdk/src/main.rs index 7f7f3991..61e66eab 100644 --- a/crates/cdk/src/main.rs +++ b/crates/cdk/src/main.rs @@ -4,8 +4,8 @@ use alloy_rpc_client::ReqwestClient; use cdk_config::Config; use clap::Parser; use cli::Cli; +use colored::*; use execute::Execute; -use std::env; use std::path::PathBuf; use std::process::Command; use url::Url; @@ -13,9 +13,10 @@ use url::Url; pub mod allocs_render; mod cli; mod config_render; +mod helpers; mod logging; +mod versions; -const CDK_CLIENT_BIN: &str = "cdk-node"; const CDK_ERIGON_BIN: &str = "cdk-erigon"; #[tokio::main] @@ -24,13 +25,8 @@ async fn main() -> anyhow::Result<()> { let cli = Cli::parse(); - // Read the config - let config = read_config(cli.config.clone())?; - - // Initialize the logger - logging::tracing(&config.log); - println!( + "{}", r#"🐼 _____ _ _____ _____ _ __ | __ \ | | / ____| __ \| |/ / @@ -41,12 +37,13 @@ async fn main() -> anyhow::Result<()> { __/ | __/ | |___/ |___/ "# + .purple() ); match cli.cmd { - cli::Commands::Node { components } => node(cli.config, components)?, - cli::Commands::Erigon {} => erigon(config, cli.chain).await?, - // _ => forward()?, + cli::Commands::Node { config, components } => node(config, components)?, + cli::Commands::Erigon { config, chain } => erigon(config, chain).await?, + cli::Commands::Versions {} => versions::versions(), } Ok(()) @@ -70,12 +67,15 @@ fn read_config(config_path: PathBuf) -> anyhow::Result { /// This function returns on fatal error or after graceful shutdown has /// completed. pub fn node(config_path: PathBuf, components: Option) -> anyhow::Result<()> { - // This is to find the erigon binary when running in development mode + // Read the config + let config = read_config(config_path.clone())?; + + // Initialize the logger + logging::tracing(&config.log); + + // This is to find the binary when running in development mode // otherwise it will use system path - let mut bin_path = env::var("CARGO_MANIFEST_DIR").unwrap_or(CDK_CLIENT_BIN.into()); - if bin_path != CDK_CLIENT_BIN { - bin_path = format!("{}/../../target/{}", bin_path, CDK_CLIENT_BIN); - } + let bin_path = helpers::get_bin_path(); let components_param = match components { Some(components) => format!("-components={}", components), @@ -118,7 +118,13 @@ pub fn node(config_path: PathBuf, components: Option) -> anyhow::Result< /// This is the main erigon entrypoint. /// This function starts everything needed to run an Erigon node. -pub async fn erigon(config: Config, genesis_file: PathBuf) -> anyhow::Result<()> { +pub async fn erigon(config_path: PathBuf, genesis_file: PathBuf) -> anyhow::Result<()> { + // Read the config + let config = read_config(config_path.clone())?; + + // Initialize the logger + logging::tracing(&config.log); + // Render configuration files let chain_id = config.aggregator.chain_id.clone(); let rpc_url = Url::parse(&config.sequence_sender.rpc_url).unwrap(); diff --git a/crates/cdk/src/versions.rs b/crates/cdk/src/versions.rs new file mode 100644 index 00000000..77581452 --- /dev/null +++ b/crates/cdk/src/versions.rs @@ -0,0 +1,47 @@ +use colored::*; +use execute::Execute; +use std::io; +use std::process::{Command, Output}; + +fn version() -> Result { + let bin_path = crate::helpers::get_bin_path(); + + // Run the node passing the config file path as argument + let mut command = Command::new(bin_path.clone()); + command.args(&["version"]); + + command.execute_output() +} + +pub(crate) fn versions() { + // Get the version of the cdk-node binary. + let output = version().unwrap(); + let version = String::from_utf8(output.stdout).unwrap(); + + println!("{}", format!("{}", version.trim()).green()); + + let versions = vec![ + ( + "zkEVM Contracts", + "https://github.com/0xPolygonHermez/zkevm-contracts/releases/tag/v8.0.0-rc.4-fork.12", + ), + ("zkEVM Prover", "v8.0.0-RC12"), + ("CDK Erigon", "hermeznetwork/cdk-erigon:0948e33"), + ( + "zkEVM Pool Manager", + "hermeznetwork/zkevm-pool-manager:v0.1.1", + ), + ( + "CDK Data Availability Node", + "0xpolygon/cdk-data-availability:0.0.10", + ), + ]; + + // Multi-line string to print the versions with colors. + let formatted_versions: Vec = versions + .iter() + .map(|(key, value)| format!("{}: {}", key.green(), value.blue())) + .collect(); + + println!("{}", formatted_versions.join("\n")); +}