Skip to content

Commit

Permalink
refactor(zint): make ethers and wasm-opt optional (#196)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
clearloop authored Dec 17, 2023
1 parent 0a10536 commit 7498e29
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 48 deletions.
4 changes: 1 addition & 3 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
2 changes: 2 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### Changes

- Refactor conta with `toml_edit`
- Optional ethers
- Optional exports wasm-opt from zinkc

### FIXED

Expand Down
1 change: 0 additions & 1 deletion cli/elko/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
3 changes: 0 additions & 3 deletions cli/elko/src/utils/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions cli/elko/src/utils/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,4 +35,5 @@ zabi.workspace = true
etc.workspace = true

[features]
cli = [ "ccli", "serde_json" ]
cli = [ "ccli", "serde_json", "utils" ]
utils = [ "wasm-opt" ]
2 changes: 1 addition & 1 deletion compiler/filetests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
7 changes: 1 addition & 6 deletions compiler/filetests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -97,11 +96,7 @@ fn examples() -> Result<Vec<PathBuf>> {
.collect::<Vec<_>>();

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)
Expand Down
1 change: 1 addition & 0 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ mod compiler;
mod config;
mod parser;
mod result;
pub mod utils;
15 changes: 15 additions & 0 deletions compiler/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<Path>, output: impl AsRef<Path>) -> 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)
}
5 changes: 2 additions & 3 deletions zint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand All @@ -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" ] }
4 changes: 3 additions & 1 deletion zint/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Zink SDK.
//! Zink ethers integration.
#![cfg(feature = "ethers")]

use crate::Result;
pub use ethers;
use ethers::{
abi::Abi,
contract::ContractFactory,
Expand Down
18 changes: 2 additions & 16 deletions zint/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -55,16 +51,6 @@ impl Contract {
})
}

/// Run wasm-opt on the given WASM file.
fn wasm_opt(wasm: impl AsRef<Path>) -> 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();
Expand Down Expand Up @@ -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)?))
Expand Down
2 changes: 1 addition & 1 deletion zint/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 3 additions & 2 deletions zint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions zint/src/result.rs
Original file line number Diff line number Diff line change
@@ -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<Signer>),
Contract(#[from] ethers::middleware::contract::ContractError<crate::api::Signer>),
/// Url parser error.
#[error(transparent)]
Url(#[from] url::ParseError),
#[cfg(feature = "ethers")]
/// Ethers wallet error.
#[error(transparent)]
Wallet(#[from] ethers::signers::WalletError),
Expand Down

0 comments on commit 7498e29

Please sign in to comment.