Skip to content

Commit

Permalink
Merge pull request #1048 from matter-labs/slumber-547-clap-to-structopt
Browse files Browse the repository at this point in the history
Replace clap with structopt
  • Loading branch information
popzxc authored Oct 22, 2020
2 parents dbb2dc2 + 95cf852 commit e61160b
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 196 deletions.
187 changes: 93 additions & 94 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/bin/data_restore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ log = "0.4"
serde_json = "1.0.0"
env_logger = "0.6"
anyhow = "1.0"
clap = "2.33.0"
structopt = "0.3.20"
chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
tokio = { version = "0.2", features = ["full"] }

Expand Down
61 changes: 31 additions & 30 deletions core/bin/data_restore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod storage_interactor;
pub mod tree_state;

use crate::data_restore_driver::DataRestoreDriver;
use clap::{App, Arg};
use structopt::StructOpt;
use web3::transports::Http;
use zksync_config::ConfigurationOptions;
use zksync_crypto::convert::FeConvert;
Expand Down Expand Up @@ -49,48 +49,49 @@ async fn add_tokens_to_db(pool: &ConnectionPool, eth_network: &str) {
}
}

#[derive(StructOpt)]
#[structopt(
name = "Data restore driver",
author = "Matter Labs",
rename_all = "snake_case"
)]
struct Opt {
/// Restores data with provided genesis (zero) block
#[structopt(long)]
genesis: bool,

/// Continues data restoring
#[structopt(long, name = "continue")]
continue_mode: bool,

/// Restore data until the last verified block and exit
#[structopt(long)]
finite: bool,

/// Expected tree root hash after restoring. This argument is ignored if mode is not `finite`
#[structopt(long)]
final_hash: Option<String>,
}

#[tokio::main]
async fn main() {
log::info!("Restoring zkSync state from the contract");
env_logger::init();
let connection_pool = ConnectionPool::new(Some(1)).await;
let config_opts = ConfigurationOptions::from_env();

let cli = App::new("Data restore driver")
.author("Matter Labs")
.arg(
Arg::with_name("genesis")
.long("genesis")
.help("Restores data with provided genesis (zero) block"),
)
.arg(
Arg::with_name("continue")
.long("continue")
.help("Continues data restoring"),
)
.arg(
Arg::with_name("finite")
.long("finite")
.help("Restore data until the last verified block and exit"),
)
.arg(
Arg::with_name("final_hash")
.long("final_hash")
.takes_value(true)
.help("Expected tree root hash after restoring. This argument is ignored if mode is not `finite`")
)
.get_matches();
let opt = Opt::from_args();

let transport = Http::new(&config_opts.web3_url).expect("failed to start web3 transport");
let governance_addr = config_opts.governance_eth_addr;
let genesis_tx_hash = config_opts.genesis_tx_hash;
let contract_addr = config_opts.contract_eth_addr;
let available_block_chunk_sizes = config_opts.available_block_chunk_sizes;

let finite_mode = cli.is_present("finite");
let finite_mode = opt.finite;
let final_hash = if finite_mode {
cli.value_of("final_hash")
.map(|value| FeConvert::from_hex(value).expect("Can't parse the final hash"))
opt.final_hash
.map(|value| FeConvert::from_hex(&value).expect("Can't parse the final hash"))
} else {
None
};
Expand All @@ -108,15 +109,15 @@ async fn main() {
);

// If genesis is argument is present - there will be fetching contracts creation transactions to get first eth block and genesis acc address
if cli.is_present("genesis") {
if opt.genesis {
// We have to load pre-defined tokens into the database before restoring state,
// since these tokens do not have a corresponding Ethereum events.
add_tokens_to_db(&driver.connection_pool, &config_opts.eth_network).await;

driver.set_genesis_state(genesis_tx_hash).await;
}

if cli.is_present("continue") {
if opt.continue_mode {
driver.load_state_from_storage().await;
}

Expand Down
2 changes: 1 addition & 1 deletion core/bin/key_generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ hex = "0.4"
rust-crypto = "0.2"
log = "0.4"
env_logger = "0.6"
clap = "2.33.0"
structopt = "0.3.20"
handlebars = "3.0.1"
serde_json = "1.0.0"
43 changes: 27 additions & 16 deletions core/bin/key_generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This is Verification key generator for PLONK prover.
//! Verification keys depends on universal setup and circuit,
//! jthat is why for each version of circuit they can be generated only once.
//! that is why for each version of circuit they can be generated only once.
//! Process of generation of this keys is CPU and memory consuming,
//! so developers are expected to download verification keys from public space.
//! After Verification keys are generated for all of our circuits
Expand All @@ -13,29 +13,40 @@
mod verifier_contract_generator;
mod zksync_key;

use clap::{App, SubCommand};
use structopt::StructOpt;

use crate::verifier_contract_generator::create_verifier_contract;
use crate::zksync_key::{make_plonk_blocks_verify_keys, make_plonk_exodus_verify_key};
use zksync_config::AvailableBlockSizesConfig;

#[derive(StructOpt)]
enum Command {
/// Generate zkSync main circuit(for various block sizes), and exodus circuit verification keys
Keys,
/// Generate verifier contract based on verification keys
Contract,
}

#[derive(StructOpt)]
#[structopt(name = "ZkSync keys generator", author = "Matter Labs")]
struct Opt {
#[structopt(subcommand)]
command: Command,
}

fn main() {
env_logger::init();

let cli = App::new("ZkSync keys generator")
.author("Matter Labs")
.subcommand(
SubCommand::with_name("keys").about("Generate zkSync main circuit(for various block sizes), and exodus circuit verification keys"),
)
.subcommand(SubCommand::with_name("contract").about("Generate verifier contract based on verification keys"))
.get_matches();

let opt = Opt::from_args();
let config = AvailableBlockSizesConfig::from_env();
let (cmd, _) = cli.subcommand();
if cmd == "keys" {
make_plonk_exodus_verify_key();
make_plonk_blocks_verify_keys(config);
} else if cmd == "contract" {
create_verifier_contract(config);

match opt.command {
Command::Keys => {
make_plonk_exodus_verify_key();
make_plonk_blocks_verify_keys(config);
}
Command::Contract => {
create_verifier_contract(config);
}
}
}
2 changes: 1 addition & 1 deletion core/bin/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ env_logger = "0.6"
reqwest = { version = "0.10", features = ["blocking", "json"] }
anyhow = "1.0"
backoff = "0.1.6"
clap = "2.33.0"
structopt = "0.3.20"
ctrlc = { version = "3.1", features = ["termination"] }
26 changes: 15 additions & 11 deletions core/bin/prover/src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
time::Duration,
};
// External deps
use clap::{App, Arg};
use structopt::StructOpt;
// Workspace deps
use zksync_config::ProverOptions;
use zksync_utils::parse_env;
Expand All @@ -21,17 +21,21 @@ fn api_client_from_env(worker_name: &str) -> client::ApiClient {
client::ApiClient::new(&server_api_url, worker_name, request_timout)
}

#[derive(StructOpt)]
#[structopt(
name = "zkSync operator node",
author = "Matter Labs",
rename_all = "snake_case"
)]
struct Opt {
/// Name of the worker. Must be unique!
#[structopt(index = 1)]
worker_name: String,
}

pub fn main_for_prover_impl<P: ProverImpl<client::ApiClient> + 'static + Send + Sync>() {
let cli = App::new("Plonk step by step prover")
.author("Matter Labs")
.arg(
Arg::with_name("worker_name")
.help("Name of the worker. Must be unique!")
.required(true)
.index(1),
)
.get_matches();
let worker_name = cli.value_of("worker_name").unwrap();
let opt = Opt::from_args();
let worker_name = opt.worker_name;

// used env
let heartbeat_interval = ProverOptions::from_env().heartbeat_interval;
Expand Down
2 changes: 1 addition & 1 deletion core/bin/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ zksync_config = { path = "../../lib/config", version = "1.0" }
zksync_storage = { path = "../../lib/storage", version = "1.0" }

anyhow = "1.0"
clap = "2.33.0"
structopt = "0.3.20"
log = "0.4"
env_logger = "0.6"
ctrlc = { version = "3.1", features = ["termination"] }
Expand Down
45 changes: 20 additions & 25 deletions core/bin/server/examples/generate_exit_proof.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Generate exit proof for exodus mode given account and token
//! correct verified state should be present in the db (could be restored using `data-restore` module)
use clap::{App, Arg};
use num::BigUint;
use serde::Serialize;
use std::time::Instant;
use structopt::StructOpt;
use zksync_crypto::proof::EncodedProofPlonk;
use zksync_storage::ConnectionPool;
use zksync_types::{AccountId, Address, TokenId, TokenLike};
Expand All @@ -18,36 +18,31 @@ struct ExitProofData {
proof: EncodedProofPlonk,
}

#[derive(StructOpt)]
#[structopt(
name = "zkSync operator node",
author = "Matter Labs",
rename_all = "snake_case"
)]
struct Opt {
/// Account id of the account
#[structopt(long)]
account_id: String,

/// Token to withdraw - "ETH" or address of the ERC20 token
#[structopt(long)]
token: String,
}

#[tokio::main]
async fn main() {
env_logger::init();

let cli = App::new("ZkSync operator node")
.author("Matter Labs")
.arg(
Arg::with_name("Account id")
.long("accound_id")
.takes_value(true)
.required(true)
.help("Account id of the account"),
)
.arg(
Arg::with_name("Token")
.long("token")
.takes_value(true)
.required(true)
.help("Token to withdraw - \"ETH\" or address of the ERC20 token"),
)
.get_matches();

let account_id = cli
.value_of("Account id")
.expect("required argument")
.parse::<AccountId>()
.unwrap();
let opt = Opt::from_args();

let account_id = opt.account_id.parse::<AccountId>().unwrap();
let token = {
let token = cli.value_of("Token").expect("required argument");
let token = &opt.token;
serde_json::from_str::<TokenLike>(token).expect("invalid token argument")
};

Expand Down
30 changes: 14 additions & 16 deletions core/bin/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use futures::{channel::mpsc, executor::block_on, SinkExt, StreamExt};
use std::cell::RefCell;
use structopt::StructOpt;
use zksync_api::run_api;
use zksync_config::{ConfigurationOptions, ProverOptions};
use zksync_core::{genesis_init, run_core, wait_for_tasks};
Expand All @@ -15,27 +16,24 @@ pub enum ServerCommand {
Launch,
}

fn read_cli() -> ServerCommand {
let cli = clap::App::new("zkSync operator node")
.author("Matter Labs")
.arg(
clap::Arg::with_name("genesis")
.long("genesis")
.help("Generate genesis block for the first contract deployment"),
)
.get_matches();

if cli.is_present("genesis") {
ServerCommand::Genesis
} else {
ServerCommand::Launch
}
#[derive(StructOpt)]
#[structopt(name = "zkSync operator node", author = "Matter Labs")]
struct Opt {
/// Generate genesis block for the first contract deployment
#[structopt(long)]
genesis: bool,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
let server_mode = read_cli();
let opt = Opt::from_args();

let server_mode = if opt.genesis {
ServerCommand::Genesis
} else {
ServerCommand::Launch
};

if let ServerCommand::Genesis = server_mode {
log::info!("Performing the server genesis initialization");
Expand Down

0 comments on commit e61160b

Please sign in to comment.