Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blockifier): verify cairo-lang version before Cairo0 compilation #182

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/blockifier/src/test_utils/cairo_compile.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::process::Command;
use std::{env, fs};

use cached::proc_macro::cached;
use serde::{Deserialize, Serialize};

const CAIRO0_PIP_REQUIREMENTS_FILE: &str = "tests/requirements.txt";

/// Objects for simple deserialization of Cargo.toml to fetch the Cairo1 compiler version.
/// The compiler itself isn't actually a dependency, so we compile by using the version of the
/// cairo-lang-casm crate.
Expand Down Expand Up @@ -54,6 +57,7 @@ pub fn cairo1_compiler_version() -> String {

/// Compiles a Cairo0 program using the deprecated compiler.
pub fn cairo0_compile(path: String, extra_arg: Option<String>, debug_info: bool) -> Vec<u8> {
verify_cairo0_compiler_deps();
let mut command = Command::new("starknet-compile-deprecated");
command.arg(&path);
if let Some(extra_arg) = extra_arg {
Expand All @@ -72,3 +76,34 @@ pub fn cairo0_compile(path: String, extra_arg: Option<String>, debug_info: bool)
pub fn cairo1_compile(_path: String) -> Vec<u8> {
todo!();
}

/// Verifies that the required dependencies are available before compiling; panics if unavailable.
fn verify_cairo0_compiler_deps() {
// Python compiler. Verify correct version.
let cairo_lang_version_output =
Command::new("sh").arg("-c").arg("pip freeze | grep cairo-lang").output().unwrap().stdout;
let cairo_lang_version_untrimmed = String::from_utf8(cairo_lang_version_output).unwrap();
let cairo_lang_version = cairo_lang_version_untrimmed.trim();
let requirements_contents = fs::read_to_string(CAIRO0_PIP_REQUIREMENTS_FILE).unwrap();
let expected_cairo_lang_version = requirements_contents
.lines()
.nth(1) // Skip docstring.
.expect(
"Expecting requirements file to contain a docstring in the first line, and \
then the required cairo-lang version in the second line."
).trim();

assert_eq!(
cairo_lang_version,
expected_cairo_lang_version,
"cairo-lang version {expected_cairo_lang_version} not found ({}). Please run:\npip3.9 \
install -r {}/{}\nthen rerun the test.",
if cairo_lang_version.is_empty() {
String::from("no installed cairo-lang found")
} else {
format!("installed version: {cairo_lang_version}")
},
env::var("CARGO_MANIFEST_DIR").unwrap(),
CAIRO0_PIP_REQUIREMENTS_FILE
);
}
Loading