From cb5749f921b29f585bed446a9bc7f02b138abb9e Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sat, 5 Apr 2025 13:27:13 +0300 Subject: [PATCH 1/3] chore(blockifier_test_utils): implement verify_cairo1_package --- .../src/cairo_compile.rs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/blockifier_test_utils/src/cairo_compile.rs b/crates/blockifier_test_utils/src/cairo_compile.rs index 8759c6e75b64..d902aba4c471 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,18 @@ async fn download_cairo_package(version: &String) { info!("Done."); } +/// Verifies that the Cairo1 package (of the given version) is available. +#[allow(dead_code)] +async fn verify_cairo1_package(version: &String, download_if_missing: bool) { + let cairo_compiler_path = starknet_compile_binary_path(version); + let sierra_compiler_path = starknet_sierra_compile_binary_path(version); + if download_if_missing && (!cairo_compiler_path.exists() || !sierra_compiler_path.exists()) { + download_cairo_package(version).await; + } + assert!(cairo_compiler_path.exists(), "Cairo compiler not found at {cairo_compiler_path:?}"); + assert!(sierra_compiler_path.exists(), "Sierra compiler not found at {sierra_compiler_path:?}"); +} + /// 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 { From 15e617ada5d213fb0b288917e7163bf8fdaf946e Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sat, 5 Apr 2025 14:16:05 +0300 Subject: [PATCH 2/3] chore(blockifier_test_utils): extract logic into starknet_sierra_compile --- .../src/cairo_compile.rs | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/crates/blockifier_test_utils/src/cairo_compile.rs b/crates/blockifier_test_utils/src/cairo_compile.rs index d902aba4c471..f8c03f414557 100644 --- a/crates/blockifier_test_utils/src/cairo_compile.rs +++ b/crates/blockifier_test_utils/src/cairo_compile.rs @@ -130,36 +130,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(); @@ -175,9 +145,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", "--", @@ -191,6 +179,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. From fc851041fafcb02f82f273c3a13d64e51ee04318 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sat, 5 Apr 2025 22:38:31 +0300 Subject: [PATCH 3/3] chore(blockifier_test_utils): split check from verify of cairo1 package --- .../blockifier_test_utils/src/cairo_compile.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/blockifier_test_utils/src/cairo_compile.rs b/crates/blockifier_test_utils/src/cairo_compile.rs index f8c03f414557..5965b4738f4a 100644 --- a/crates/blockifier_test_utils/src/cairo_compile.rs +++ b/crates/blockifier_test_utils/src/cairo_compile.rs @@ -79,16 +79,20 @@ async fn download_cairo_package(version: &String) { info!("Done."); } -/// Verifies that the Cairo1 package (of the given version) is available. -#[allow(dead_code)] -async fn verify_cairo1_package(version: &String, download_if_missing: bool) { +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); - if download_if_missing && (!cairo_compiler_path.exists() || !sierra_compiler_path.exists()) { + 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!(cairo_compiler_path.exists(), "Cairo compiler not found at {cairo_compiler_path:?}"); - assert!(sierra_compiler_path.exists(), "Sierra compiler not found at {sierra_compiler_path:?}"); + assert!(cairo1_package_exists(version)); } /// Runs a command. If it has succeeded, it returns the command's output; otherwise, it panics with