From 9ba84231b3cb24a3add6dc5540cf039f8f4a18a3 Mon Sep 17 00:00:00 2001 From: Avi Cohen Date: Wed, 5 Feb 2025 14:31:25 +0200 Subject: [PATCH] test(cairo_native): compare the required starknet-native-compile version to workspace version --- Cargo.lock | 12 +++++- Cargo.toml | 2 + .../starknet_sierra_multicompile/Cargo.toml | 1 + crates/starknet_sierra_multicompile/build.rs | 7 +--- .../src/command_line_compiler.rs | 4 +- .../src/compile_test.rs | 1 - .../src/constants.rs | 5 ++- .../src/constants_test.rs | 10 +++++ .../starknet_sierra_multicompile/src/lib.rs | 4 ++ toml_test_utils/Cargo.toml | 14 +++++++ .../src/lib.rs | 42 +++++++++---------- workspace_tests/Cargo.toml | 3 +- workspace_tests/lints_test.rs | 2 +- workspace_tests/main.rs | 1 - workspace_tests/package_integrity_test.rs | 2 +- workspace_tests/version_integrity_test.rs | 2 +- 16 files changed, 74 insertions(+), 38 deletions(-) create mode 100644 crates/starknet_sierra_multicompile/src/constants_test.rs create mode 100644 toml_test_utils/Cargo.toml rename workspace_tests/toml_utils.rs => toml_test_utils/src/lib.rs (71%) diff --git a/Cargo.lock b/Cargo.lock index 1e96e6a31bc..e9c8312620f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11090,6 +11090,7 @@ dependencies = [ "starknet_infra_utils", "tempfile", "thiserror 1.0.69", + "toml_test_utils", "validator", ] @@ -11818,6 +11819,14 @@ dependencies = [ "winnow 0.6.22", ] +[[package]] +name = "toml_test_utils" +version = "0.14.0-rc.0" +dependencies = [ + "serde", + "toml", +] + [[package]] name = "tower" version = "0.4.13" @@ -12828,8 +12837,7 @@ dependencies = [ name = "workspace_tests" version = "0.14.0-rc.0" dependencies = [ - "serde", - "toml", + "toml_test_utils", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 01d60686716..41c005cf9a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ members = [ "crates/starknet_state_sync", "crates/starknet_state_sync_types", "crates/starknet_task_executor", + "toml_test_utils", "workspace_tests", ] @@ -255,6 +256,7 @@ tokio-stream = "0.1.8" tokio-test = "0.4.4" tokio-util = "0.7.13" toml = "0.8" +toml_test_utils = { path = "toml_test_utils", version = "0.14.0-rc.0" } tower = "0.4.13" tracing = "0.1.37" tracing-subscriber = "0.3.16" diff --git a/crates/starknet_sierra_multicompile/Cargo.toml b/crates/starknet_sierra_multicompile/Cargo.toml index 253aaf7b815..6b07335433a 100644 --- a/crates/starknet_sierra_multicompile/Cargo.toml +++ b/crates/starknet_sierra_multicompile/Cargo.toml @@ -32,6 +32,7 @@ assert_matches.workspace = true mempool_test_utils.path = "../mempool_test_utils" rstest.workspace = true starknet_infra_utils.path = "../starknet_infra_utils" +toml_test_utils.path = "../../toml_test_utils" [build-dependencies] # NOTE: This dependency is not necessary for the code to work. It is added to make the implicit diff --git a/crates/starknet_sierra_multicompile/build.rs b/crates/starknet_sierra_multicompile/build.rs index d6c73f21a64..61a5e5419b4 100644 --- a/crates/starknet_sierra_multicompile/build.rs +++ b/crates/starknet_sierra_multicompile/build.rs @@ -16,9 +16,6 @@ fn main() { } const REQUIRED_CAIRO_LANG_VERSION: &str = "2.10.0-rc.1"; -#[cfg(feature = "cairo_native")] -// TODO(Avi, 15/2/2025): Add test that checks the version of this constant is the workspace version. -const REQUIRED_CAIRO_NATIVE_VERSION: &str = "v0.14.0-rc.0"; /// Downloads the Cairo crate from StarkWare's release page and extracts its contents into the /// `target` directory. This crate includes the `starknet-sierra-compile` binary, which is used to @@ -38,8 +35,8 @@ fn install_starknet_sierra_compile() { /// compilation is required. #[cfg(feature = "cairo_native")] fn install_starknet_native_compile() { - let binary_name = CAIRO_NATIVE_BINARY_NAME; - let required_version = REQUIRED_CAIRO_NATIVE_VERSION; + let binary_name = NATIVE_COMPILE_BINARY_NAME; + let required_version = REQUIRED_NATIVE_COMPILE_VERSION; let repo_root_dir = starknet_infra_utils::path::project_path().expect("Should be able to get the project path"); diff --git a/crates/starknet_sierra_multicompile/src/command_line_compiler.rs b/crates/starknet_sierra_multicompile/src/command_line_compiler.rs index 638547845e6..79de56af628 100644 --- a/crates/starknet_sierra_multicompile/src/command_line_compiler.rs +++ b/crates/starknet_sierra_multicompile/src/command_line_compiler.rs @@ -11,7 +11,7 @@ use tempfile::NamedTempFile; use crate::config::SierraCompilationConfig; use crate::constants::CAIRO_LANG_BINARY_NAME; #[cfg(feature = "cairo_native")] -use crate::constants::CAIRO_NATIVE_BINARY_NAME; +use crate::constants::NATIVE_COMPILE_BINARY_NAME; use crate::errors::CompilationUtilError; use crate::paths::binary_path; use crate::resource_limits::ResourceLimits; @@ -33,7 +33,7 @@ impl CommandLineCompiler { #[cfg(feature = "cairo_native")] let path_to_starknet_native_compile_binary = match &config.sierra_to_native_compiler_path { Some(path) => path.clone(), - None => binary_path(out_dir(), CAIRO_NATIVE_BINARY_NAME), + None => binary_path(out_dir(), NATIVE_COMPILE_BINARY_NAME), }; Self { config, diff --git a/crates/starknet_sierra_multicompile/src/compile_test.rs b/crates/starknet_sierra_multicompile/src/compile_test.rs index ff5ce154450..88681be0fad 100644 --- a/crates/starknet_sierra_multicompile/src/compile_test.rs +++ b/crates/starknet_sierra_multicompile/src/compile_test.rs @@ -74,7 +74,6 @@ fn test_compile_sierra_to_native() { let compiler = command_line_compiler(); let contract_class = get_test_contract(); - // TODO(Avi, 1/1/2025): Check size/memory/time limits. let _native_contract_executor = compiler.compile_to_native(contract_class).unwrap(); } diff --git a/crates/starknet_sierra_multicompile/src/constants.rs b/crates/starknet_sierra_multicompile/src/constants.rs index dee6a4daba3..0d52cf034e2 100644 --- a/crates/starknet_sierra_multicompile/src/constants.rs +++ b/crates/starknet_sierra_multicompile/src/constants.rs @@ -4,4 +4,7 @@ pub(crate) const CAIRO_LANG_BINARY_NAME: &str = "starknet-sierra-compile"; #[cfg(feature = "cairo_native")] -pub(crate) const CAIRO_NATIVE_BINARY_NAME: &str = "starknet-native-compile"; +pub(crate) const NATIVE_COMPILE_BINARY_NAME: &str = "starknet-native-compile"; + +#[cfg(feature = "cairo_native")] +pub const REQUIRED_NATIVE_COMPILE_VERSION: &str = "0.14.0-rc.0"; diff --git a/crates/starknet_sierra_multicompile/src/constants_test.rs b/crates/starknet_sierra_multicompile/src/constants_test.rs new file mode 100644 index 00000000000..1627cd53570 --- /dev/null +++ b/crates/starknet_sierra_multicompile/src/constants_test.rs @@ -0,0 +1,10 @@ +#![cfg(feature = "cairo_native")] +use toml_test_utils::ROOT_TOML; + +use crate::constants::REQUIRED_NATIVE_COMPILE_VERSION; + +#[test] +fn native_compile_version_test() { + let workspace_version = ROOT_TOML.workspace_version(); + assert_eq!(REQUIRED_NATIVE_COMPILE_VERSION, workspace_version); +} diff --git a/crates/starknet_sierra_multicompile/src/lib.rs b/crates/starknet_sierra_multicompile/src/lib.rs index 11773f51f5d..55eba15062d 100644 --- a/crates/starknet_sierra_multicompile/src/lib.rs +++ b/crates/starknet_sierra_multicompile/src/lib.rs @@ -21,6 +21,10 @@ pub mod test_utils; #[path = "compile_test.rs"] pub mod compile_test; +#[cfg(test)] +#[path = "constants_test.rs"] +pub mod constants_test; + pub trait SierraToCasmCompiler: Send + Sync { fn compile( &self, diff --git a/toml_test_utils/Cargo.toml b/toml_test_utils/Cargo.toml new file mode 100644 index 00000000000..91ac30899e0 --- /dev/null +++ b/toml_test_utils/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "toml_test_utils" +version.workspace = true +edition.workspace = true +repository.workspace = true +license.workspace = true +description = "Utilities for working with TOML files in tests." + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true, features = ["derive"] } +toml.workspace = true diff --git a/workspace_tests/toml_utils.rs b/toml_test_utils/src/lib.rs similarity index 71% rename from workspace_tests/toml_utils.rs rename to toml_test_utils/src/lib.rs index fc10a9b9ac7..737c4057dee 100644 --- a/workspace_tests/toml_utils.rs +++ b/toml_test_utils/src/lib.rs @@ -7,53 +7,53 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[serde(untagged)] -pub(crate) enum LintValue { +pub enum LintValue { Bool(bool), } #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(untagged)] -pub(crate) enum DependencyValue { +pub enum DependencyValue { String(String), Object { version: Option, path: Option, features: Option> }, } #[derive(Clone, Debug, Serialize, Deserialize)] -pub(crate) struct Package { +pub struct Package { version: String, } #[derive(Clone, Debug, Serialize, Deserialize)] -pub(crate) struct WorkspaceFields { +pub struct WorkspaceFields { package: Package, members: Vec, dependencies: HashMap, } #[derive(Clone, Debug, Serialize, Deserialize)] -pub(crate) struct CargoToml { +pub struct CargoToml { workspace: WorkspaceFields, } #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(untagged)] -pub(crate) enum PackageEntryValue { +pub enum PackageEntryValue { String(String), Object { workspace: bool }, Other(toml::Value), } #[derive(Clone, Debug, Serialize, Deserialize)] -pub(crate) struct CrateCargoToml { - pub(crate) package: HashMap, +pub struct CrateCargoToml { + pub package: HashMap, dependencies: Option>, #[serde(rename = "dev-dependencies")] - pub(crate) dev_dependencies: Option>, - pub(crate) lints: Option>, + pub dev_dependencies: Option>, + pub lints: Option>, } impl CrateCargoToml { - pub(crate) fn package_name(&self) -> &String { + pub fn package_name(&self) -> &String { match self.package.get("name") { Some(PackageEntryValue::String(name)) => name, _ => panic!("No name found in crate toml {self:?}."), @@ -62,12 +62,12 @@ impl CrateCargoToml { } #[derive(Debug)] -pub(crate) struct LocalCrate { - pub(crate) path: String, - pub(crate) version: String, +pub struct LocalCrate { + pub path: String, + pub version: String, } -pub(crate) static ROOT_TOML: LazyLock = LazyLock::new(|| { +pub static ROOT_TOML: LazyLock = LazyLock::new(|| { let root_toml: CargoToml = toml::from_str(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../Cargo.toml"))) .unwrap(); @@ -75,19 +75,19 @@ pub(crate) static ROOT_TOML: LazyLock = LazyLock::new(|| { }); impl CargoToml { - pub(crate) fn members(&self) -> &Vec { + pub fn members(&self) -> &Vec { &self.workspace.members } - pub(crate) fn workspace_version(&self) -> &str { + pub fn workspace_version(&self) -> &str { &self.workspace.package.version } - pub(crate) fn dependencies(&self) -> impl Iterator + '_ { + pub fn dependencies(&self) -> impl Iterator + '_ { self.workspace.dependencies.iter() } - pub(crate) fn path_dependencies(&self) -> impl Iterator + '_ { + pub fn path_dependencies(&self) -> impl Iterator + '_ { self.dependencies().filter_map(|(_name, value)| { if let DependencyValue::Object { path: Some(path), version: Some(version), .. } = value { @@ -98,7 +98,7 @@ impl CargoToml { }) } - pub(crate) fn member_cargo_tomls(&self) -> HashMap { + pub fn member_cargo_tomls(&self) -> HashMap { let crates_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../")); self.members() .iter() @@ -116,7 +116,7 @@ impl CargoToml { } impl CrateCargoToml { - pub(crate) fn path_dependencies(&self) -> impl Iterator + '_ { + pub fn path_dependencies(&self) -> impl Iterator + '_ { self.dependencies.iter().flatten().filter_map(|(_name, value)| { if let DependencyValue::Object { path: Some(path), .. } = value { Some(path.to_string()) diff --git a/workspace_tests/Cargo.toml b/workspace_tests/Cargo.toml index 18a57a14546..c99a59bad8f 100644 --- a/workspace_tests/Cargo.toml +++ b/workspace_tests/Cargo.toml @@ -7,8 +7,7 @@ license-file.workspace = true description = "Workspace-level tests." [dev-dependencies] -serde = { workspace = true, features = ["derive"] } -toml.workspace = true +toml_test_utils = { path = "../toml_test_utils" } [[test]] name = "workspace_tests" diff --git a/workspace_tests/lints_test.rs b/workspace_tests/lints_test.rs index c42243dce58..39ee03f9950 100644 --- a/workspace_tests/lints_test.rs +++ b/workspace_tests/lints_test.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::toml_utils::{CrateCargoToml, LintValue, ROOT_TOML}; +use toml_test_utils::{CrateCargoToml, LintValue, ROOT_TOML}; #[test] fn test_lints_section_exists() { diff --git a/workspace_tests/main.rs b/workspace_tests/main.rs index dce1c1efde0..499364c01df 100644 --- a/workspace_tests/main.rs +++ b/workspace_tests/main.rs @@ -2,5 +2,4 @@ pub mod lints_test; pub mod package_integrity_test; -pub mod toml_utils; pub mod version_integrity_test; diff --git a/workspace_tests/package_integrity_test.rs b/workspace_tests/package_integrity_test.rs index 7707e93c64f..9ab4daa410a 100644 --- a/workspace_tests/package_integrity_test.rs +++ b/workspace_tests/package_integrity_test.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use crate::toml_utils::{CrateCargoToml, DependencyValue, PackageEntryValue, ROOT_TOML}; +use toml_test_utils::{CrateCargoToml, DependencyValue, PackageEntryValue, ROOT_TOML}; #[test] fn test_package_names_match_directory() { diff --git a/workspace_tests/version_integrity_test.rs b/workspace_tests/version_integrity_test.rs index 2f30127a686..86366c7a1b8 100644 --- a/workspace_tests/version_integrity_test.rs +++ b/workspace_tests/version_integrity_test.rs @@ -1,4 +1,4 @@ -use crate::toml_utils::{DependencyValue, LocalCrate, PackageEntryValue, ROOT_TOML}; +use toml_test_utils::{DependencyValue, LocalCrate, PackageEntryValue, ROOT_TOML}; #[test] fn test_path_dependencies_are_members() {