diff --git a/crates/blockifier/tests/feature_contracts_compatibility_test.rs b/crates/blockifier/tests/feature_contracts_compatibility_test.rs index e9f27809958..d3411dec194 100644 --- a/crates/blockifier/tests/feature_contracts_compatibility_test.rs +++ b/crates/blockifier/tests/feature_contracts_compatibility_test.rs @@ -15,6 +15,44 @@ const FIX_COMMAND: &str = "FIX_FEATURE_TEST=1 cargo test -p blockifier --test \ feature_contracts_compatibility_test --features testing -- \ --include-ignored"; +pub enum FeatureContractMetadata { + Cairo0(DeprecatedFeatureContractMetadata), + Cairo1(InnerFeatureContractMetadata), + #[cfg(feature = "cairo_native")] + Native(InnerFeatureContractMetadata), +} + +impl FeatureContractMetadata { + pub fn compiled_path(&self) -> &str { + match self { + FeatureContractMetadata::Cairo0(data) => &data.compiled_path, + FeatureContractMetadata::Cairo1(data) => &data.compiled_path, + #[cfg(feature = "cairo_native")] + FeatureContractMetadata::Native(data) => &data.compiled_path, + } + } + + pub fn sierra_path(&self) -> &str { + match self { + FeatureContractMetadata::Cairo0(_) => panic!("No sierra path for Cairo0 contracts."), + FeatureContractMetadata::Cairo1(data) => &data.sierra_path, + #[cfg(feature = "cairo_native")] + FeatureContractMetadata::Native(data) => &data.sierra_path, + } + } +} +pub struct DeprecatedFeatureContractMetadata { + pub source_path: String, + pub base_filename: String, + pub compiled_path: String, +} +pub struct InnerFeatureContractMetadata { + pub source_path: String, + pub base_filename: String, + pub compiled_path: String, + pub sierra_path: String, +} + // To fix Cairo0 feature contracts, first enter a python venv and install the requirements: // ``` // python -m venv tmp_venv @@ -104,11 +142,10 @@ fn verify_and_get_files(cairo_version: CairoVersion) -> Vec<(String, String, Str // Verify `TEST_CONTRACTS` file and directory structure. if !path.is_file() { if let Some(dir_name) = path.file_name() { - assert_eq!( - dir_name, - COMPILED_CONTRACTS_SUBDIR, + assert!( + dir_name == COMPILED_CONTRACTS_SUBDIR || dir_name == SIERRA_CONTRACTS_SUBDIR, "Found directory '{}' in `{directory}`, which should contain only the \ - `{COMPILED_CONTRACTS_SUBDIR}` directory.", + `{COMPILED_CONTRACTS_SUBDIR}` or `{SIERRA_CONTRACTS_SUBDIR}` directory.", dir_name.to_string_lossy() ); continue; @@ -125,7 +162,36 @@ fn verify_and_get_files(cairo_version: CairoVersion) -> Vec<(String, String, Str let existing_compiled_path = format!("{directory}/{COMPILED_CONTRACTS_SUBDIR}/{file_name}{compiled_extension}"); - paths.push((path_str.to_string(), file_name.to_string(), existing_compiled_path)); + match cairo_version { + CairoVersion::Cairo0 => { + paths.push(FeatureContractMetadata::Cairo0(DeprecatedFeatureContractMetadata { + source_path: path_str.to_string(), + base_filename: file_name.to_string(), + compiled_path: existing_compiled_path, + })) + } + CairoVersion::Cairo1 => { + let existing_sierra_path = + format!("{directory}/{SIERRA_CONTRACTS_SUBDIR}/{file_name}.sierra.json"); + paths.push(FeatureContractMetadata::Cairo1(InnerFeatureContractMetadata { + source_path: path_str.to_string(), + base_filename: file_name.to_string(), + compiled_path: existing_compiled_path, + sierra_path: existing_sierra_path, + })); + } + #[cfg(feature = "cairo_native")] + CairoVersion::Native => { + let existing_sierra_path = + format!("{directory}/{SIERRA_CONTRACTS_SUBDIR}/{file_name}.sierra.json"); + paths.push(FeatureContractMetadata::Native(InnerFeatureContractMetadata { + source_path: path_str.to_string(), + base_filename: file_name.to_string(), + compiled_path: existing_compiled_path, + sierra_path: existing_sierra_path, + })); + } + } } paths @@ -136,14 +202,33 @@ fn verify_feature_contracts_match_enum() { let mut compiled_paths_from_enum: Vec = FeatureContract::all_feature_contracts() .map(|contract| contract.get_compiled_path()) .collect(); - let mut compiled_paths_on_filesystem: Vec = verify_and_get_files(CairoVersion::Cairo0) + let (mut compiled_paths_on_filesystem, mut sierra_paths_on_filesystem): ( + Vec, + Vec, + ) = verify_and_get_files(CairoVersion::Cairo0) .into_iter() .chain(verify_and_get_files(CairoVersion::Cairo1)) - .map(|(_, _, compiled_path)| compiled_path) - .collect(); + .map(|metadata| match metadata { + FeatureContractMetadata::Cairo0(data) => (data.compiled_path, String::new()), + FeatureContractMetadata::Cairo1(data) => (data.compiled_path, data.sierra_path), + #[cfg(feature = "cairo_native")] + FeatureContractMetadata::Native(data) => (data.compiled_path, data.sierra_path), + }) + .unzip(); compiled_paths_from_enum.sort(); compiled_paths_on_filesystem.sort(); assert_eq!(compiled_paths_from_enum, compiled_paths_on_filesystem); + + let mut sierra_paths_from_enum: Vec = FeatureContract::all_feature_contracts() + .filter(|contract| contract.cairo_version() == CairoVersion::Cairo1) + .map(|contract| contract.get_sierra_path()) + .collect(); + sierra_paths_from_enum.sort(); + sierra_paths_on_filesystem = + sierra_paths_on_filesystem.iter().filter(|s| !s.is_empty()).cloned().collect(); + sierra_paths_on_filesystem.sort(); + + assert_eq!(sierra_paths_from_enum, sierra_paths_on_filesystem); } // todo(rdr): find the right way to feature verify native contracts as well