Skip to content

Commit

Permalink
refactor(zkstack_cli): Replace curl and wget with reqwest (#3052)
Browse files Browse the repository at this point in the history
## What ❔
Replace curl and wget with reqwest
  • Loading branch information
matias-gonz authored Oct 10, 2024
1 parent e5b5a3b commit db133e6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 40 deletions.
11 changes: 6 additions & 5 deletions zk_toolbox/Cargo.lock

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

2 changes: 1 addition & 1 deletion zk_toolbox/crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod yaml;

pub use prerequisites::{
check_general_prerequisites, check_prerequisites, GCLOUD_PREREQUISITE, GPU_PREREQUISITES,
PROVER_CLI_PREREQUISITE, WGET_PREREQUISITE,
PROVER_CLI_PREREQUISITE,
};
pub use prompt::{init_prompt_theme, Prompt, PromptConfirm, PromptSelect};
pub use term::{error, logger, spinner};
5 changes: 0 additions & 5 deletions zk_toolbox/crates/common/src/prerequisites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ pub const GPU_PREREQUISITES: [Prerequisite; 3] = [
}, // CUDA GPU driver
];

pub const WGET_PREREQUISITE: [Prerequisite; 1] = [Prerequisite {
name: "wget",
download_link: "https://www.gnu.org/software/wget/",
}];

pub const GCLOUD_PREREQUISITE: [Prerequisite; 1] = [Prerequisite {
name: "gcloud",
download_link: "https://cloud.google.com/sdk/docs/install",
Expand Down
1 change: 1 addition & 0 deletions zk_toolbox/crates/zk_inception/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ zksync_protobuf.workspace = true
zksync_protobuf_config.workspace = true
prost.workspace = true
secrecy.workspace = true
reqwest = "0.12.8"

[dev-dependencies]
rand.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::str::FromStr;

use common::{cmd::Cmd, spinner::Spinner};
use common::spinner::Spinner;
use serde::Deserialize;
use xshell::{cmd, Shell};
use xshell::Shell;

use crate::messages::{MSG_INVALID_ARCH_ERR, MSG_NO_RELEASES_FOUND_ERR};

Expand Down Expand Up @@ -76,22 +76,19 @@ fn get_compatible_archs(asset_name: &str) -> anyhow::Result<Vec<Arch>> {

fn get_releases(shell: &Shell, repo: &str, arch: Arch) -> anyhow::Result<Vec<Version>> {
if repo == "ethereum/solc-bin" {
return get_solc_releases(shell, arch);
return get_solc_releases(arch);
}

let mut cmd = cmd!(
shell,
"curl -f https://api.github.com/repos/{repo}/releases"
);
let client = reqwest::blocking::Client::new();
let mut request = client
.get(format!("https://api.github.com/repos/{repo}/releases"))
.header("User-Agent", "zkstack");

if let Ok(token) = shell.var("GITHUB_TOKEN") {
cmd = cmd.args(vec![
"-H".to_string(),
format!("Authorization: Bearer {}", token),
]);
request = request.header("Authorization", format!("Bearer {}", token));
}

let response = String::from_utf8(Cmd::new(cmd).run_with_output()?.stdout)?;
let response = request.send()?.text()?;
let releases: Vec<GitHubRelease> = serde_json::from_str(&response)?;

let mut versions = vec![];
Expand All @@ -115,21 +112,23 @@ fn get_releases(shell: &Shell, repo: &str, arch: Arch) -> anyhow::Result<Vec<Ver
Ok(versions)
}

fn get_solc_releases(shell: &Shell, arch: Arch) -> anyhow::Result<Vec<Version>> {
fn get_solc_releases(arch: Arch) -> anyhow::Result<Vec<Version>> {
let (arch_str, compatible_archs) = match arch {
Arch::LinuxAmd => ("linux-amd64", vec![Arch::LinuxAmd, Arch::LinuxArm]),
Arch::LinuxArm => ("linux-amd64", vec![Arch::LinuxAmd, Arch::LinuxArm]),
Arch::MacosAmd => ("macosx-amd64", vec![Arch::MacosAmd, Arch::MacosArm]),
Arch::MacosArm => ("macosx-amd64", vec![Arch::MacosAmd, Arch::MacosArm]),
};

let response: std::process::Output = Cmd::new(cmd!(
shell,
"curl https://raw.githubusercontent.com/ethereum/solc-bin/gh-pages/{arch_str}/list.json"
))
.run_with_output()?;
let client = reqwest::blocking::Client::new();
let response = client
.get(format!(
"https://raw.githubusercontent.com/ethereum/solc-bin/gh-pages/{arch_str}/list.json"
))
.header("User-Agent", "zkstack")
.send()?
.text()?;

let response = String::from_utf8(response.stdout)?;
let solc_list: SolcList = serde_json::from_str(&response)?;

let mut versions = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ fn download_binary(

let spinner = Spinner::new(&msg_downloading_binary_spinner(name, version));
Cmd::new(cmd!(shell, "mkdir -p {path}")).run()?;
Cmd::new(cmd!(shell, "wget {url} -O {binary_path}")).run()?;
let response = reqwest::blocking::get(url)?.bytes()?;
shell.write_file(binary_path.clone(), &response)?;
Cmd::new(cmd!(shell, "chmod +x {binary_path}")).run()?;
spinner.finish();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Context;
use common::{check_prerequisites, cmd::Cmd, spinner::Spinner, WGET_PREREQUISITE};
use common::spinner::Spinner;
use config::{get_link_to_prover, EcosystemConfig, GeneralConfig};
use xshell::{cmd, Shell};
use xshell::Shell;

use super::args::compressor_keys::CompressorKeysArgs;
use crate::messages::{
Expand Down Expand Up @@ -35,7 +35,6 @@ pub(crate) fn download_compressor_key(
general_config: &mut GeneralConfig,
path: &str,
) -> anyhow::Result<()> {
check_prerequisites(shell, &WGET_PREREQUISITE, false);
let spinner = Spinner::new(MSG_DOWNLOADING_SETUP_COMPRESSOR_KEY_SPINNER);
let mut compressor_config: zksync_config::configs::FriProofCompressorConfig = general_config
.proof_compressor_config
Expand All @@ -47,14 +46,13 @@ pub(crate) fn download_compressor_key(

let url = compressor_config.universal_setup_download_url;
let path = std::path::Path::new(path);
let parent = path.parent().expect(MSG_SETUP_KEY_PATH_ERROR);
let file_name = path.file_name().expect(MSG_SETUP_KEY_PATH_ERROR);

Cmd::new(cmd!(shell, "wget {url} -P {parent}")).run()?;
let client = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(600))
.build()?;

if file_name != "setup_2^24.key" {
Cmd::new(cmd!(shell, "mv {parent}/setup_2^24.key {path}")).run()?;
}
let response = client.get(url).send()?.bytes()?;
shell.write_file(path, &response)?;

spinner.finish();
Ok(())
Expand Down

0 comments on commit db133e6

Please sign in to comment.