Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi committed Mar 10, 2021
1 parent d2720c1 commit defe20d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
43 changes: 30 additions & 13 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ use structopt::StructOpt;
/// This is the maximum number of pages available for a contract to allocate.
const MAX_MEMORY_PAGES: u32 = 16;

/// Result of executing the build.
pub struct ExecutionResult {
pub dest_wasm: PathBuf,
pub optimization_result: OptimizationResult,
}

/// Executes build of the smart-contract which produces a wasm binary that is ready for deploying.
///
/// It does so by invoking `cargo build` and then post processing the final binary.
Expand Down Expand Up @@ -114,6 +120,8 @@ impl CheckCommand {

/// Builds the project in the specified directory, defaults to the current directory.
///
/// Returns the path to the generated contract artifact, if one was generated.
///
/// Uses the unstable cargo feature [`build-std`](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std)
/// to build the standard library with [`panic_immediate_abort`](https://github.com/johnthagen/min-sized-rust#remove-panic-string-formatting-with-panic_immediate_abort)
/// which reduces the size of the Wasm binary by not including panic strings and formatting code.
Expand All @@ -132,7 +140,7 @@ fn build_cargo_project(
build_artifact: BuildArtifacts,
verbosity: Verbosity,
unstable_flags: UnstableFlags,
) -> Result<()> {
) -> Result<Option<PathBuf>> {
util::assert_channel()?;

// set linker args via RUSTFLAGS.
Expand Down Expand Up @@ -184,7 +192,11 @@ fn build_cargo_project(
// clear RUSTFLAGS
std::env::remove_var("RUSTFLAGS");

Ok(())
if build_artifact == BuildArtifacts::CheckOnly {
Ok(Some(crate_metadata.dest_wasm.clone()))
} else {
Ok(None)
}
}

/// Ensures the wasm memory import of a given module has the maximum number of pages.
Expand Down Expand Up @@ -384,13 +396,19 @@ fn execute(
) -> Result<BuildResult> {
if build_artifact == BuildArtifacts::CodeOnly || build_artifact == BuildArtifacts::CheckOnly {
let crate_metadata = CrateMetadata::collect(manifest_path)?;
let (maybe_dest_wasm, maybe_optimization_result) = execute_with_crate_metadata(
let maybe_execution_result = execute_with_crate_metadata(
&crate_metadata,
verbosity,
optimize_contract,
build_artifact,
unstable_flags,
)?;

let (maybe_dest_wasm, maybe_optimization_result) = maybe_execution_result
.map_or((None, None), |r| {
(Some(r.dest_wasm), Some(r.optimization_result))
});

let res = BuildResult {
dest_wasm: maybe_dest_wasm,
dest_metadata: None,
Expand All @@ -415,23 +433,22 @@ fn execute(
///
/// Uses the supplied `CrateMetadata`. If an instance is not available use [`execute_build`]
///
/// Returns a tuple of `(maybe_optimized_wasm_path, maybe_optimization_result)`.
/// Returns an `ExecutionResult` if the build resulted in any artifacts.
pub(crate) fn execute_with_crate_metadata(
crate_metadata: &CrateMetadata,
verbosity: Verbosity,
optimize_contract: bool,
build_artifact: BuildArtifacts,
unstable_flags: UnstableFlags,
) -> Result<(Option<PathBuf>, Option<OptimizationResult>)> {
) -> Result<Option<ExecutionResult>> {
maybe_println!(
verbosity,
" {} {}",
format!("[1/{}]", build_artifact.steps()).bold(),
"Building cargo project".bright_green().bold()
);
build_cargo_project(&crate_metadata, build_artifact, verbosity, unstable_flags)?;
if build_artifact == BuildArtifacts::CheckOnly {
return Ok((None, None));
if build_cargo_project(&crate_metadata, build_artifact, verbosity, unstable_flags)?.is_none() {
return Ok(None);
}
maybe_println!(
verbosity,
Expand All @@ -441,7 +458,7 @@ pub(crate) fn execute_with_crate_metadata(
);
post_process_wasm(&crate_metadata)?;
if !optimize_contract {
return Ok((None, None));
return Ok(None);
}
maybe_println!(
verbosity,
Expand All @@ -450,10 +467,10 @@ pub(crate) fn execute_with_crate_metadata(
"Optimizing wasm file".bright_green().bold()
);
let optimization_result = optimize_wasm(&crate_metadata)?;
Ok((
Some(crate_metadata.dest_wasm.clone()),
Some(optimization_result),
))
Ok(Some(ExecutionResult {
dest_wasm: crate_metadata.dest_wasm.clone(),
optimization_result,
}))
}

#[cfg(feature = "test-ci-only")]
Expand Down
13 changes: 8 additions & 5 deletions src/cmd/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,21 @@ impl GenerateMetadataCommand {
///
/// Return a tuple of `(dest_wasm, hash, optimization_result)`.
fn wasm_hash(&self) -> Result<(PathBuf, CodeHash, OptimizationResult)> {
let (maybe_dest_wasm, maybe_optimization_res) = super::build::execute_with_crate_metadata(
let res = super::build::execute_with_crate_metadata(
&self.crate_metadata,
self.verbosity,
true, // for the hash we always use the optimized version of the contract
self.build_artifact,
self.unstable_options.clone(),
)?;
)?
.expect("result must exist");

let wasm = fs::read(&self.crate_metadata.dest_wasm)?;
let dest_wasm = maybe_dest_wasm.expect("dest wasm must exist");
let optimization_res = maybe_optimization_res.expect("optimization result must exist");
Ok((dest_wasm, blake2_hash(wasm.as_slice()), optimization_res))
Ok((
res.dest_wasm,
blake2_hash(wasm.as_slice()),
res.optimization_result,
))
}
}

Expand Down

0 comments on commit defe20d

Please sign in to comment.