From 7498e29c49985796b302a124a0702c325c9e62ed Mon Sep 17 00:00:00 2001 From: clearloop <26088946+clearloop@users.noreply.github.com> Date: Mon, 18 Dec 2023 05:38:21 +0800 Subject: [PATCH] refactor(zint): make ethers and wasm-opt optional (#196) * chore(zint): rename ethers to optional dependencies * feat(zinkc): make wasm-opt optional * chore(zint): make wasm-opt default features * chore(zint): make wasm-opt default features of zint * chore(zinkc): remove utils in feature cli * chore(zinkc): make feature wasm-opt part of feature utils * docs(RELEASES): append releases note --- Cargo.lock | 4 +--- Cargo.toml | 2 +- RELEASES.md | 2 ++ cli/elko/Cargo.toml | 1 - cli/elko/src/utils/result.rs | 3 --- cli/elko/src/utils/wasm.rs | 7 +------ compiler/Cargo.toml | 6 +++++- compiler/filetests/Cargo.toml | 2 +- compiler/filetests/build.rs | 7 +------ compiler/src/lib.rs | 1 + compiler/src/utils.rs | 15 +++++++++++++++ zint/Cargo.toml | 5 ++--- zint/src/api.rs | 4 +++- zint/src/contract.rs | 18 ++---------------- zint/src/evm.rs | 2 +- zint/src/lib.rs | 5 +++-- zint/src/result.rs | 7 ++++--- 17 files changed, 43 insertions(+), 48 deletions(-) create mode 100644 compiler/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index abdb2aff3..be6d9e045 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -993,7 +993,6 @@ dependencies = [ "thiserror", "toml", "tracing", - "wasm-opt", "zinkc", ] @@ -4778,6 +4777,7 @@ dependencies = [ "thiserror", "tracing", "tracing-subscriber", + "wasm-opt", "wasmparser", "wat", "zabi", @@ -4795,7 +4795,6 @@ dependencies = [ "syn 2.0.41", "tracing", "tracing-subscriber", - "wasm-opt", "wat", "zinkc", ] @@ -4817,7 +4816,6 @@ dependencies = [ "tracing", "tracing-subscriber", "url", - "wasm-opt", "zabi", "zinkc", ] diff --git a/Cargo.toml b/Cargo.toml index e3ca6bf1d..d198ee7e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,5 +100,5 @@ zink-codegen.workspace = true anyhow.workspace = true paste.workspace = true filetests.workspace = true -zint.workspace = true +zint = { workspace = true, features = [ "ethers" ] } tokio = { workspace = true, features = [ "macros" ] } diff --git a/RELEASES.md b/RELEASES.md index dbb4d5ed2..c21228dda 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -3,6 +3,8 @@ ### Changes - Refactor conta with `toml_edit` +- Optional ethers +- Optional exports wasm-opt from zinkc ### FIXED diff --git a/cli/elko/Cargo.toml b/cli/elko/Cargo.toml index 4105dfe4e..06463f044 100644 --- a/cli/elko/Cargo.toml +++ b/cli/elko/Cargo.toml @@ -20,5 +20,4 @@ serde = { workspace = true, features = [ "derive" ] } thiserror.workspace = true toml.workspace = true tracing.workspace = true -wasm-opt.workspace = true zinkc = { workspace = true, features = [ "cli" ] } diff --git a/cli/elko/src/utils/result.rs b/cli/elko/src/utils/result.rs index 231727c47..a92deada9 100644 --- a/cli/elko/src/utils/result.rs +++ b/cli/elko/src/utils/result.rs @@ -15,9 +15,6 @@ pub enum Error { /// IO error #[error(transparent)] Io(#[from] std::io::Error), - /// Serde JSON error - #[error(transparent)] - WasmOpt(#[from] wasm_opt::OptimizationError), } /// Zinkc result diff --git a/cli/elko/src/utils/wasm.rs b/cli/elko/src/utils/wasm.rs index 9be2901bd..3bec37ca9 100644 --- a/cli/elko/src/utils/wasm.rs +++ b/cli/elko/src/utils/wasm.rs @@ -5,7 +5,6 @@ use anyhow::anyhow; use cargo_metadata::{Metadata, MetadataCommand, Package}; use etc::{Etc, FileSystem}; use std::{fs, path::PathBuf, process::Command}; -use wasm_opt::OptimizationOptions; /// WASM Builder pub struct WasmBuilder { @@ -120,11 +119,7 @@ impl WasmBuilder { .with_extension("wasm"); // run the wasm optimizer - OptimizationOptions::new_opt_level_4() - .debug_info(false) - .mvp_features_only() - .set_converge() - .run(src, self.output()?)?; + zinkc::utils::wasm_opt(src, self.output()?)?; Ok(()) } diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index dc963bbe9..59ab87c80 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -20,8 +20,11 @@ tracing.workspace = true wasmparser.workspace = true zabi.workspace = true zingen.workspace = true + +# Optional dependencies ccli = { workspace = true, optional = true } serde_json = { workspace = true, optional = true } +wasm-opt = { workspace = true, optional = true } [dev-dependencies] hex.workspace = true @@ -32,4 +35,5 @@ zabi.workspace = true etc.workspace = true [features] -cli = [ "ccli", "serde_json" ] +cli = [ "ccli", "serde_json", "utils" ] +utils = [ "wasm-opt" ] diff --git a/compiler/filetests/Cargo.toml b/compiler/filetests/Cargo.toml index 442cb1a77..0cc2226fd 100644 --- a/compiler/filetests/Cargo.toml +++ b/compiler/filetests/Cargo.toml @@ -26,7 +26,7 @@ proc-macro2.workspace = true quote.workspace = true syn.workspace = true wat.workspace = true -wasm-opt.workspace = true +zinkc = { workspace = true, features = [ "utils", "wasm-opt" ] } [features] testing = [] diff --git a/compiler/filetests/build.rs b/compiler/filetests/build.rs index 36ee7a2af..954a51d42 100644 --- a/compiler/filetests/build.rs +++ b/compiler/filetests/build.rs @@ -9,7 +9,6 @@ use std::{ path::{Path, PathBuf}, }; use syn::{parse_quote, ExprArray, ExprMatch, Ident, ItemImpl, ItemMod}; -use wasm_opt::OptimizationOptions; fn main() -> Result<()> { println!("cargo:rerun-if-changed=build.rs"); @@ -97,11 +96,7 @@ fn examples() -> Result> { .collect::>(); for wasm in &files { - OptimizationOptions::new_opt_level_4() - .debug_info(false) - .mvp_features_only() - .set_converge() - .run(wasm, wasm)?; + zinkc::utils::wasm_opt(wasm, wasm)?; } Ok(files) diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 6004240cb..f429d8b87 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -12,3 +12,4 @@ mod compiler; mod config; mod parser; mod result; +pub mod utils; diff --git a/compiler/src/utils.rs b/compiler/src/utils.rs new file mode 100644 index 000000000..b1122502d --- /dev/null +++ b/compiler/src/utils.rs @@ -0,0 +1,15 @@ +//! Zink compiler utils +#![cfg(feature = "utils")] + +use std::path::Path; + +/// Run wasm-opt on the given WASM file. +pub fn wasm_opt(input: impl AsRef, output: impl AsRef) -> anyhow::Result<()> { + ::wasm_opt::OptimizationOptions::new_opt_level_4() + .shrink_level(::wasm_opt::ShrinkLevel::Level2) + .debug_info(false) + .mvp_features_only() + .set_converge() + .run(&input, &output) + .map_err(Into::into) +} diff --git a/zint/Cargo.toml b/zint/Cargo.toml index 7410dd7b4..969f06503 100644 --- a/zint/Cargo.toml +++ b/zint/Cargo.toml @@ -13,7 +13,7 @@ repository.workspace = true anyhow.workspace = true cargo_metadata.workspace = true etc.workspace = true -ethers.workspace = true +ethers = { workspace = true, optional = true } hex.workspace = true revm.workspace = true serde = { workspace = true, features = [ "derive" ] } @@ -23,6 +23,5 @@ tracing.workspace = true tracing-subscriber = { workspace = true, features = ["env-filter"]} toml.workspace = true url.workspace = true -wasm-opt.workspace = true zabi.workspace = true -zinkc.workspace = true +zinkc = { workspace = true, features = [ "utils" ] } diff --git a/zint/src/api.rs b/zint/src/api.rs index 1bbc1b795..ccc885f99 100644 --- a/zint/src/api.rs +++ b/zint/src/api.rs @@ -1,6 +1,8 @@ -//! Zink SDK. +//! Zink ethers integration. +#![cfg(feature = "ethers")] use crate::Result; +pub use ethers; use ethers::{ abi::Abi, contract::ContractFactory, diff --git a/zint/src/contract.rs b/zint/src/contract.rs index 21ff43ef9..92d7b1108 100644 --- a/zint/src/contract.rs +++ b/zint/src/contract.rs @@ -3,11 +3,7 @@ use crate::{Bytes32, Info, EVM}; use anyhow::{anyhow, Result}; use serde::Deserialize; -use std::{ - fs, - path::{Path, PathBuf}, -}; -use wasm_opt::OptimizationOptions; +use std::{fs, path::PathBuf}; use zabi::Abi; use zinkc::Compiler; @@ -55,16 +51,6 @@ impl Contract { }) } - /// Run wasm-opt on the given WASM file. - fn wasm_opt(wasm: impl AsRef) -> Result<()> { - OptimizationOptions::new_opt_level_4() - .debug_info(false) - .mvp_features_only() - .set_converge() - .run(&wasm, &wasm) - .map_err(Into::into) - } - /// Create new contract pub fn new(wasm: impl AsRef<[u8]>) -> Self { crate::setup_logger(); @@ -140,7 +126,7 @@ impl Contract { }; let wasm = search("release").or_else(|_| search("debug"))?; - Self::wasm_opt(&wasm)?; + zinkc::utils::wasm_opt(&wasm, &wasm)?; tracing::debug!("loading contract from {}", wasm.display()); Ok(Self::new(fs::read(wasm)?)) diff --git a/zint/src/evm.rs b/zint/src/evm.rs index d5ea1e534..85043634c 100644 --- a/zint/src/evm.rs +++ b/zint/src/evm.rs @@ -80,8 +80,8 @@ impl EVM { pub fn run(btyecode: &[u8], input: &[u8]) -> Info { let mut evm = Self::new(btyecode, input); let info = evm.execute(); - tracing::debug!("{info:?}"); + tracing::debug!("{info:?}"); info } } diff --git a/zint/src/lib.rs b/zint/src/lib.rs index 9e594b15c..a4e781f37 100644 --- a/zint/src/lib.rs +++ b/zint/src/lib.rs @@ -8,15 +8,16 @@ mod evm; mod result; pub use self::{ - api::Ethers, bytes::Bytes32, contract::Contract, evm::{Info, InstructionResult, EVM, U256}, result::Result, }; -pub use ethers; use tracing_subscriber::EnvFilter; +#[cfg(feature = "ethers")] +pub use api::*; + /// Set up the logger. pub fn setup_logger() { tracing_subscriber::fmt() diff --git a/zint/src/result.rs b/zint/src/result.rs index fbf3d1ed9..b53ccbee2 100644 --- a/zint/src/result.rs +++ b/zint/src/result.rs @@ -1,22 +1,23 @@ //! Zink sdk results. -use crate::api::Signer; - /// Zint error. #[derive(thiserror::Error, Debug)] pub enum Error { + #[cfg(feature = "ethers")] /// Ethers abi error. #[error(transparent)] Abi(#[from] ethers::abi::AbiError), /// Anyhow error. #[error(transparent)] Anyhow(#[from] anyhow::Error), + #[cfg(feature = "ethers")] /// Ethers contract error. #[error(transparent)] - Contract(#[from] ethers::middleware::contract::ContractError), + Contract(#[from] ethers::middleware::contract::ContractError), /// Url parser error. #[error(transparent)] Url(#[from] url::ParseError), + #[cfg(feature = "ethers")] /// Ethers wallet error. #[error(transparent)] Wallet(#[from] ethers::signers::WalletError),