diff --git a/crates/blockifier_test_utils/src/cairo_compile.rs b/crates/blockifier_test_utils/src/cairo_compile.rs index 8759c6e75b6..5965b4738f4 100644 --- a/crates/blockifier_test_utils/src/cairo_compile.rs +++ b/crates/blockifier_test_utils/src/cairo_compile.rs @@ -42,9 +42,18 @@ fn cairo1_package_dir(version: &String) -> PathBuf { project_path().unwrap().join(format!("target/bin/cairo_package__{version}")) } +/// Path to starknet-compile binary, of the specified version. +fn starknet_compile_binary_path(version: &String) -> PathBuf { + cairo1_package_dir(version).join("cairo/bin/starknet-compile") +} + +/// Path to starknet-sierra-compile binary, of the specified version. +fn starknet_sierra_compile_binary_path(version: &String) -> PathBuf { + cairo1_package_dir(version).join("cairo/bin/starknet-sierra-compile") +} + /// Downloads the cairo package to the local directory. /// Creates the directory if it does not exist. -#[allow(dead_code)] async fn download_cairo_package(version: &String) { let directory = cairo1_package_dir(version); info!("Downloading Cairo package to {directory:?}."); @@ -70,6 +79,22 @@ async fn download_cairo_package(version: &String) { info!("Done."); } +fn cairo1_package_exists(version: &String) -> bool { + let cairo_compiler_path = starknet_compile_binary_path(version); + let sierra_compiler_path = starknet_sierra_compile_binary_path(version); + cairo_compiler_path.exists() && sierra_compiler_path.exists() +} + +/// Verifies that the Cairo1 package (of the given version) is available. +/// Attempts to download it if not. +#[allow(dead_code)] +async fn verify_cairo1_package(version: &String) { + if !cairo1_package_exists(version) { + download_cairo_package(version).await; + } + assert!(cairo1_package_exists(version)); +} + /// Runs a command. If it has succeeded, it returns the command's output; otherwise, it panics with /// stderr output. fn run_and_verify_output(command: &mut Command) -> Output { @@ -109,36 +134,6 @@ pub fn cairo1_compile( cargo_nightly_arg: Option, ) -> CompilationArtifacts { let mut base_compile_args = vec![]; - - let sierra_output = - starknet_compile(path, git_tag_override, cargo_nightly_arg, &mut base_compile_args); - - let mut temp_file = NamedTempFile::new().unwrap(); - temp_file.write_all(&sierra_output).unwrap(); - let temp_path_str = temp_file.into_temp_path(); - - // Sierra -> CASM. - let mut sierra_compile_command = Command::new("cargo"); - sierra_compile_command.args(base_compile_args); - sierra_compile_command.args([ - "starknet-sierra-compile", - temp_path_str.to_str().unwrap(), - "--allowed-libfuncs-list-name", - "all", - ]); - let casm_output = run_and_verify_output(&mut sierra_compile_command); - - CompilationArtifacts::Cairo1 { casm: casm_output.stdout, sierra: sierra_output } -} - -/// Compile Cairo1 Contract into their Sierra version using the compiler version set in the -/// Cargo.toml -pub fn starknet_compile( - path: String, - git_tag_override: Option, - cargo_nightly_arg: Option, - base_compile_args: &mut Vec, -) -> Vec { verify_cairo1_compiler_deps(git_tag_override); let cairo1_compiler_path = local_cairo1_compiler_repo_path(); @@ -154,9 +149,27 @@ pub fn starknet_compile( base_compile_args.insert(0, format!("+nightly-{nightly_version}")); } + let sierra_output = starknet_compile(path, &mut base_compile_args); + + let mut temp_file = NamedTempFile::new().unwrap(); + temp_file.write_all(&sierra_output).unwrap(); + let temp_path_str = temp_file.into_temp_path(); + + // Sierra -> CASM. + let casm_output = starknet_sierra_compile( + temp_path_str.to_str().unwrap().to_string(), + &mut base_compile_args, + ); + + CompilationArtifacts::Cairo1 { casm: casm_output, sierra: sierra_output } +} + +/// Compile Cairo1 Contract into their Sierra version using the compiler version set in the +/// Cargo.toml +pub fn starknet_compile(path: String, base_compile_args: &mut [String]) -> Vec { // Cairo -> Sierra. let mut starknet_compile_commmand = Command::new("cargo"); - starknet_compile_commmand.args(base_compile_args.clone()); + starknet_compile_commmand.args(base_compile_args.to_owned()); starknet_compile_commmand.args([ "starknet-compile", "--", @@ -170,6 +183,20 @@ pub fn starknet_compile( sierra_output.stdout } +/// Compile Sierra code into CASM. +fn starknet_sierra_compile(path: String, base_compile_args: &mut Vec) -> Vec { + let mut sierra_compile_command = Command::new("cargo"); + sierra_compile_command.args(base_compile_args); + sierra_compile_command.args([ + "starknet-sierra-compile", + &path, + "--allowed-libfuncs-list-name", + "all", + ]); + let casm_output = run_and_verify_output(&mut sierra_compile_command); + casm_output.stdout +} + /// Verifies that the required dependencies are available before compiling; panics if unavailable. fn verify_cairo0_compiler_deps() { // Python compiler. Verify correct version.