From 38f5856782b11f4e62c0ab7a32ae0288d5b05dc5 Mon Sep 17 00:00:00 2001 From: Nimrod Weiss Date: Thu, 4 Apr 2024 16:11:12 +0300 Subject: [PATCH] chore: adding CLI interface --- Cargo.lock | 195 +++++++++++++++++++++++++++++-- Cargo.toml | 1 + crates/committer_cli/Cargo.toml | 3 + crates/committer_cli/src/main.rs | 138 +++++++++++++++++++--- 4 files changed, 313 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e46d2d40..931662ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,54 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" +[[package]] +name = "anstream" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.81" @@ -362,6 +410,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.55", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "committer" version = "0.1.0-rc.0" @@ -375,6 +469,7 @@ dependencies = [ name = "committer_cli" version = "0.1.0-rc.0" dependencies = [ + "clap", "pretty_assertions", ] @@ -625,6 +720,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -1019,7 +1120,7 @@ dependencies = [ "libc", "redox_syscall 0.4.1", "smallvec", - "windows-targets", + "windows-targets 0.48.5", ] [[package]] @@ -1571,6 +1672,12 @@ dependencies = [ "precomputed-hash", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "strum" version = "0.24.1" @@ -1717,6 +1824,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" @@ -1824,19 +1937,43 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.4", +] + [[package]] name = "windows-targets" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +dependencies = [ + "windows_aarch64_gnullvm 0.52.4", + "windows_aarch64_msvc 0.52.4", + "windows_i686_gnu 0.52.4", + "windows_i686_msvc 0.52.4", + "windows_x86_64_gnu 0.52.4", + "windows_x86_64_gnullvm 0.52.4", + "windows_x86_64_msvc 0.52.4", ] [[package]] @@ -1845,42 +1982,84 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" + [[package]] name = "winnow" version = "0.5.40" diff --git a/Cargo.toml b/Cargo.toml index a01be854..cf5e3a19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ license-file = "LICENSE" [workspace.dependencies] pretty_assertions = "1.2.1" +clap = { version = "4.5.4", features = ["cargo", "derive"] } [workspace.lints.rust] warnings = "deny" diff --git a/crates/committer_cli/Cargo.toml b/crates/committer_cli/Cargo.toml index f73c7a51..e0355ea0 100644 --- a/crates/committer_cli/Cargo.toml +++ b/crates/committer_cli/Cargo.toml @@ -11,3 +11,6 @@ workspace = true [dev-dependencies] pretty_assertions.workspace = true + +[dependencies] +clap.workspace = true diff --git a/crates/committer_cli/src/main.rs b/crates/committer_cli/src/main.rs index d6a3c5ff..09d86a9f 100644 --- a/crates/committer_cli/src/main.rs +++ b/crates/committer_cli/src/main.rs @@ -1,20 +1,126 @@ -use std::env; -use std::path::Path; +use clap::{Args, Parser, Subcommand}; +use std::{collections::HashSet, path::Path}; + +/// Here's my app! +#[derive(Debug, Parser)] +#[clap(name = "committer-cli", version)] +pub struct CommitterCliArgs { + #[clap(flatten)] + global_opts: GlobalOpts, + + #[clap(subcommand)] + command: Command, +} + +#[derive(Debug, Subcommand)] +enum Command { + /// Given previous state tree skeleton and a state diff, computes the new commitment. + Commit { + /// File path to input. + #[clap(long, short = 'i', default_value = "stdin")] + input_path: String, + + /// File path to output. + #[clap(long, short = 'o', default_value = "stdout")] + output_path: String, + + /// The version of the class hash. + #[clap(long)] + class_hash_version: u8, + + /// The version of the contract state hash. + #[clap(long)] + contract_state_hash_version: u8, + + #[clap(flatten)] + leaf_prefixes: LeafPrefixes, + + #[clap(flatten)] + node_prefixes: NodePrefixes, + }, +} + +#[derive(Debug, Args)] +struct GlobalOpts {} + +#[derive(Debug, Args)] +struct LeafPrefixes { + /// Prefix for storage leaf. + #[clap(long)] + storage: u8, + + /// Prefix for contract state leaf. + #[clap(long)] + contract_state: u8, + + /// Prefix for class leaf. + #[clap(long)] + class: u8, +} + +#[derive(Debug, Args)] +struct NodePrefixes { + /// Prefix for edge node. + #[clap(long)] + edge: u8, + + /// Prefix for sibling node. + #[clap(long)] + sibling: u8, + + /// Prefix for binary node. + #[clap(long)] + binary: u8, + + /// Prefix for empty node. + #[clap(long)] + empty: u8, +} /// Main entry point of the committer CLI. fn main() { - // Open the input file. - let args: Vec = env::args().collect(); - let input_file_name = Path::new(&args[1]); - let output_file_name = Path::new(&args[2]); - assert!( - input_file_name.is_absolute() && output_file_name.is_absolute(), - "Given paths must be absolute" - ); - - // Business logic to be implemented here. - let output = std::fs::read(input_file_name).unwrap(); - - // Output to file. - std::fs::write(output_file_name, output).expect("Failed to write output"); + let args = CommitterCliArgs::parse(); + + match args.command { + Command::Commit { + input_path, + output_path, + class_hash_version: _, + contract_state_hash_version: _, + leaf_prefixes, + node_prefixes, + } => { + // Input verification: prefixes must be unique. + let expected_unique_nodes = 4; + let prefixes = [ + node_prefixes.edge, + node_prefixes.sibling, + node_prefixes.binary, + node_prefixes.empty, + ]; + if HashSet::from(prefixes).len() != expected_unique_nodes { + panic!("Node prefixes must be unique, got {prefixes:?}."); + } + let expected_unique_leaves = 3; + let leaf_prefixes = [ + leaf_prefixes.storage, + leaf_prefixes.contract_state, + leaf_prefixes.class, + ]; + if HashSet::from(leaf_prefixes).len() != expected_unique_leaves { + panic!("Leaf prefixes must be unique, got {leaf_prefixes:?}."); + } + + let input_file_name = Path::new(&input_path); + let output_file_name = Path::new(&output_path); + assert!( + input_file_name.is_absolute() && output_file_name.is_absolute(), + "Given paths must be absolute." + ); + // Business logic to be implemented here. + let output = std::fs::read(input_file_name).unwrap(); + // Output to file. + std::fs::write(output_file_name, output).expect("Failed to write output"); + } + } }