Skip to content

Commit

Permalink
feat: add versions command (#120)
Browse files Browse the repository at this point in the history
* refactor: not all commands need the same params
* feat: versions command
  • Loading branch information
vcastellm authored Oct 10, 2024
1 parent 999b7b6 commit 72ed025
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 36 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
49 changes: 30 additions & 19 deletions crates/cdk/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,24 @@ 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,
}

#[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,
Expand All @@ -40,5 +32,24 @@ pub(crate) enum Commands {
)]
components: Option<String>,
},
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,
}
13 changes: 13 additions & 0 deletions crates/cdk/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 23 additions & 17 deletions crates/cdk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ 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;

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]
Expand All @@ -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#"🐼
_____ _ _____ _____ _ __
| __ \ | | / ____| __ \| |/ /
Expand All @@ -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(())
Expand All @@ -70,12 +67,15 @@ fn read_config(config_path: PathBuf) -> anyhow::Result<Config> {
/// This function returns on fatal error or after graceful shutdown has
/// completed.
pub fn node(config_path: PathBuf, components: Option<String>) -> 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),
Expand Down Expand Up @@ -118,7 +118,13 @@ pub fn node(config_path: PathBuf, components: Option<String>) -> 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();
Expand Down
47 changes: 47 additions & 0 deletions crates/cdk/src/versions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use colored::*;
use execute::Execute;
use std::io;
use std::process::{Command, Output};

fn version() -> Result<Output, io::Error> {
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<String> = versions
.iter()
.map(|(key, value)| format!("{}: {}", key.green(), value.blue()))
.collect();

println!("{}", formatted_versions.join("\n"));
}

0 comments on commit 72ed025

Please sign in to comment.