diff --git a/Cargo.lock b/Cargo.lock index 79aae00c76..6f4edbd788 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -436,6 +436,7 @@ dependencies = [ "strum_macros 0.24.3", "test-case", "thiserror", + "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 217ddbdcae..0fece228b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ strum = "0.24.1" strum_macros = "0.24.3" tempfile = "3.7.0" test-case = "2.2.2" +toml = "0.8" thiserror = "1.0.37" [workspace.lints.rust] diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index e63f33baef..8140763d96 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -48,6 +48,7 @@ starknet_api = { workspace = true, features = ["testing"] } strum.workspace = true strum_macros.workspace = true thiserror.workspace = true +toml.workspace = true [dev-dependencies] assert_matches.workspace = true diff --git a/crates/blockifier/src/test_utils/cairo_compile.rs b/crates/blockifier/src/test_utils/cairo_compile.rs index ad9568da37..74a62b4a64 100644 --- a/crates/blockifier/src/test_utils/cairo_compile.rs +++ b/crates/blockifier/src/test_utils/cairo_compile.rs @@ -1,5 +1,54 @@ use std::process::Command; +use cached::proc_macro::cached; +use serde::{Deserialize, Serialize}; + +/// 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. +/// The choice of cairo-lang-casm is arbitrary, as all compiler crate dependencies should have the +/// same version. +/// Deserializes: +/// """ +/// ... +/// [workspace.dependencies] +/// ... +/// cairo-lang-casm = VERSION +/// ... +/// """ +/// where `VERSION` can be a simple "x.y.z" version string or an object with a "version" field. +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +enum DependencyValue { + String(String), + Object { version: String }, +} + +#[derive(Debug, Serialize, Deserialize)] +struct CairoLangCasmDependency { + #[serde(rename = "cairo-lang-casm")] + cairo_lang_casm: DependencyValue, +} + +#[derive(Debug, Serialize, Deserialize)] +struct WorkspaceFields { + dependencies: CairoLangCasmDependency, +} + +#[derive(Debug, Serialize, Deserialize)] +struct CargoToml { + workspace: WorkspaceFields, +} + +#[cached] +/// Returns the version of the Cairo1 compiler* defined in the root Cargo.toml. +pub fn cairo1_compiler_version() -> String { + let cargo_toml: CargoToml = toml::from_str(include_str!("../../../../Cargo.toml")).unwrap(); + match cargo_toml.workspace.dependencies.cairo_lang_casm { + DependencyValue::String(version) | DependencyValue::Object { version } => version.clone(), + } +} + /// Compiles a Cairo0 program using the deprecated compiler. pub fn cairo0_compile(path: String, extra_arg: Option, debug_info: bool) -> Vec { let mut command = Command::new("starknet-compile-deprecated");