From 9a24c7eb61175f9feeba6e52be8f6ec34103dc69 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Mon, 29 Jul 2024 17:46:32 +0300 Subject: [PATCH] chore(blockifier): fetch compiler repo version --- Cargo.lock | 1 + Cargo.toml | 1 + crates/blockifier/Cargo.toml | 1 + .../src/test_utils/cairo_compile.rs | 52 +++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7bdb1093b7..c0ca4899bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1023,6 +1023,7 @@ dependencies = [ "test-case", "thiserror", "tikv-jemallocator", + "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 207205c202..808760624a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -198,6 +198,7 @@ tokio = { version = "1.37.0", features = ["full"] } tokio-retry = "0.3" tokio-stream = "0.1.8" tokio-test = "0.4.4" +toml = "0.8" tower = "0.4.13" tracing = "0.1.37" tracing-subscriber = "0.3.16" diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index 7536f0e449..8445b74086 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -53,6 +53,7 @@ strum.workspace = true strum_macros.workspace = true thiserror.workspace = true tikv-jemallocator = { workspace = true, optional = 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 61827ee5e0..727cd4ee68 100644 --- a/crates/blockifier/src/test_utils/cairo_compile.rs +++ b/crates/blockifier/src/test_utils/cairo_compile.rs @@ -1,5 +1,57 @@ 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 { + // cairo-lang-casm = "x.y.z". + String(String), + // cairo-lang-casm = { version = "x.y.z", .. }. + 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 (by checking the +/// package version of one of the crates from the compiler in the dependencies). +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");