diff --git a/crates/analyze/Cargo.toml b/crates/analyze/Cargo.toml index a53b2b583..6b8d4e895 100644 --- a/crates/analyze/Cargo.toml +++ b/crates/analyze/Cargo.toml @@ -15,7 +15,7 @@ include = ["Cargo.toml", "*.rs", "LICENSE"] [dependencies] contract-metadata = { version = "4.0.0-rc", path = "../metadata" } -parity-wasm = { version = "0.45.0", features = ["sign_ext"] } +parity-wasm = { version = "0.45.0" } anyhow = "1.0.75" [dev-dependencies] diff --git a/crates/build/Cargo.toml b/crates/build/Cargo.toml index 6bf90009f..a8051ba26 100644 --- a/crates/build/Cargo.toml +++ b/crates/build/Cargo.toml @@ -28,7 +28,7 @@ rustc_version = "0.4.0" scale = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] } toml = "0.8.8" tracing = "0.1.40" -parity-wasm = { version = "0.45.0", features = ["sign_ext"] } +parity-wasm = { version = "0.45.0"} semver = { version = "1.0.20", features = ["serde"] } serde = { version = "1", default-features = false, features = ["derive"] } serde_json = "1.0.108" diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index 520290bb8..e3fb0ca09 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -731,14 +731,14 @@ fn load_module>(path: P) -> Result { /// Performs required post-processing steps on the Wasm artifact. fn post_process_wasm( - crate_metadata: &CrateMetadata, + optimized_code: &PathBuf, skip_wasm_validation: bool, verbosity: &Verbosity, max_memory_pages: u32, ) -> Result<()> { // Deserialize Wasm module from a file. - let mut module = load_module(&crate_metadata.original_code) - .context("Loading of original wasm failed")?; + let mut module = + load_module(optimized_code).context("Loading of optimized wasm failed")?; strip_exports(&mut module); ensure_maximum_memory_pages(&mut module, max_memory_pages)?; @@ -761,7 +761,7 @@ fn post_process_wasm( "resulting wasm size of post processing must be > 0" ); - parity_wasm::serialize_to_file(&crate_metadata.dest_code, module)?; + parity_wasm::serialize_to_file(optimized_code, module)?; Ok(()) } @@ -1007,17 +1007,14 @@ fn local_build( match target { Target::Wasm => { + let handler = WasmOptHandler::new(*optimization_passes, *keep_debug_symbols)?; + handler.optimize(&crate_metadata.original_code, &crate_metadata.dest_code)?; post_process_wasm( - crate_metadata, + &crate_metadata.dest_code, *skip_wasm_validation, verbosity, *max_memory_pages, )?; - let handler = WasmOptHandler::new(*optimization_passes, *keep_debug_symbols)?; - handler.optimize( - &crate_metadata.dest_code, - &crate_metadata.contract_artifact_name, - )?; } Target::RiscV => { fs::copy(&crate_metadata.original_code, &crate_metadata.dest_code)?; diff --git a/crates/build/src/wasm_opt.rs b/crates/build/src/wasm_opt.rs index afac079ce..d14bad886 100644 --- a/crates/build/src/wasm_opt.rs +++ b/crates/build/src/wasm_opt.rs @@ -53,16 +53,7 @@ impl WasmOptHandler { /// Attempts to perform optional Wasm optimization using Binaryen's `wasm-opt` tool. /// /// If successful, the optimized Wasm binary is written to `dest_wasm`. - pub fn optimize( - &self, - dest_wasm: &PathBuf, - contract_artifact_name: &String, - ) -> Result<()> { - // We'll create a temporary file for our optimized Wasm binary. Note that we'll - // later overwrite this with the original path of the Wasm binary. - let mut dest_optimized = dest_wasm.clone(); - dest_optimized.set_file_name(format!("{contract_artifact_name}-opt.wasm")); - + pub fn optimize(&self, original_wasm: &PathBuf, dest_wasm: &PathBuf) -> Result<()> { tracing::debug!( "Optimization level passed to wasm-opt: {}", self.optimization_level @@ -82,17 +73,15 @@ impl WasmOptHandler { // memory-packing pre-pass. .zero_filled_memory(true) .debug_info(self.keep_debug_symbols) - .run(dest_wasm, &dest_optimized)?; + .run(original_wasm, dest_wasm)?; - if !dest_optimized.exists() { + if !dest_wasm.exists() { return Err(anyhow::anyhow!( "Optimization failed, optimized wasm output file `{}` not found.", - dest_optimized.display() + dest_wasm.display() )) } - // Overwrite existing destination wasm file with the optimised version - std::fs::rename(&dest_optimized, dest_wasm)?; Ok(()) } }