From c095163227537a2c77ad2a5cf87b9be7a3458736 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Mon, 29 Jul 2024 17:51:24 +0300 Subject: [PATCH] feat(blockifier): verify cairo-lang version before Cairo0 compilation --- .../src/test_utils/cairo_compile.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/blockifier/src/test_utils/cairo_compile.rs b/crates/blockifier/src/test_utils/cairo_compile.rs index 727cd4ee68..6193b0d2d6 100644 --- a/crates/blockifier/src/test_utils/cairo_compile.rs +++ b/crates/blockifier/src/test_utils/cairo_compile.rs @@ -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. @@ -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, debug_info: bool) -> Vec { + verify_cairo0_compiler_deps(); let mut command = Command::new("starknet-compile-deprecated"); command.arg(&path); if let Some(extra_arg) = extra_arg { @@ -72,3 +76,30 @@ pub fn cairo0_compile(path: String, extra_arg: Option, debug_info: bool) pub fn cairo1_compile(_path: String) -> Vec { 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 = String::from_utf8(cairo_lang_version_output).unwrap(); + + 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.trim(), + expected_cairo_lang_version, + "cairo-lang version {expected_cairo_lang_version} not found (installed version: '{}'). \ + Please run:\npip3.9 install -r {}/{}\nthen rerun the test.", + cairo_lang_version.trim(), + env::var("CARGO_MANIFEST_DIR").unwrap(), + CAIRO0_PIP_REQUIREMENTS_FILE + ); +}