From 62d0c0ee0848579102bbdc4e07b3592001c777a8 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Mon, 29 Jul 2024 17:35:04 +0300 Subject: [PATCH] chore(blockifier): refactor feature contract path getters for different paths --- crates/blockifier/src/test_utils/contracts.rs | 89 +++++++++++++------ 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/crates/blockifier/src/test_utils/contracts.rs b/crates/blockifier/src/test_utils/contracts.rs index 1f19b43c77..1a2acfbda2 100644 --- a/crates/blockifier/src/test_utils/contracts.rs +++ b/crates/blockifier/src/test_utils/contracts.rs @@ -1,3 +1,5 @@ +use std::path::Path; + use starknet_api::core::{ ClassHash, CompiledClassHash, @@ -63,8 +65,11 @@ const SECURITY_TEST_CONTRACT_NAME: &str = "security_tests_contract"; const TEST_CONTRACT_NAME: &str = "test_contract"; // ERC20 contract is in a unique location. +const ERC20_CAIRO0_CONTRACT_SOURCE_PATH: &str = + "./ERC20/ERC20_Cairo0/ERC20_without_some_syscalls/ERC20/ERC20.cairo"; const ERC20_CAIRO0_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo0/ERC20_without_some_syscalls/ERC20/\ erc20_contract_without_some_syscalls_compiled.json"; +const ERC20_CAIRO1_CONTRACT_SOURCE_PATH: &str = "./ERC20/ERC20_Cairo1/ERC20.cairo"; const ERC20_CAIRO1_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo1/erc20.casm.json"; /// Enum representing all feature contracts. @@ -186,36 +191,66 @@ impl FeatureContract { } } - pub fn get_compiled_path(&self) -> String { - let cairo_version = self.cairo_version(); - let contract_name = match self { - Self::AccountWithLongValidate(_) => ACCOUNT_LONG_VALIDATE_NAME, - Self::AccountWithoutValidations(_) => ACCOUNT_WITHOUT_VALIDATIONS_NAME, - Self::Empty(_) => EMPTY_CONTRACT_NAME, - Self::FaultyAccount(_) => FAULTY_ACCOUNT_NAME, - Self::LegacyTestContract => LEGACY_CONTRACT_NAME, - Self::SecurityTests => SECURITY_TEST_CONTRACT_NAME, - Self::TestContract(_) => TEST_CONTRACT_NAME, - // ERC20 is a special case - not in the feature_contracts directory. - Self::ERC20(_) => { - return match cairo_version { - CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_PATH.into(), - CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_PATH.into(), - }; - } - }; - format!( - "feature_contracts/cairo{}/compiled/{}{}.json", + pub fn get_source_path(&self) -> String { + // Special case: ERC20 contract in a different location. + if let Self::ERC20(cairo_version) = self { match cairo_version { - CairoVersion::Cairo0 => "0", - CairoVersion::Cairo1 => "1", - }, - contract_name, + CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_SOURCE_PATH, + CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_SOURCE_PATH, + } + .into() + } else { + format!( + "feature_contracts/cairo{}/{}.cairo", + match self.cairo_version() { + CairoVersion::Cairo0 => "0", + CairoVersion::Cairo1 => "1", + }, + match self { + Self::AccountWithLongValidate(_) => ACCOUNT_LONG_VALIDATE_NAME, + Self::AccountWithoutValidations(_) => ACCOUNT_WITHOUT_VALIDATIONS_NAME, + Self::Empty(_) => EMPTY_CONTRACT_NAME, + Self::FaultyAccount(_) => FAULTY_ACCOUNT_NAME, + Self::LegacyTestContract => LEGACY_CONTRACT_NAME, + Self::SecurityTests => SECURITY_TEST_CONTRACT_NAME, + Self::TestContract(_) => TEST_CONTRACT_NAME, + Self::ERC20(_) => unreachable!(), + } + ) + } + } + + pub fn get_compiled_path(&self) -> String { + // ERC20 is a special case - not in the feature_contracts directory. + if let Self::ERC20(cairo_version) = self { match cairo_version { - CairoVersion::Cairo0 => "_compiled", - CairoVersion::Cairo1 => ".casm", + CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_PATH, + CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_PATH, } - ) + .into() + } else { + let cairo_version = self.cairo_version(); + let source_path = self.get_source_path(); + let base_name = Path::new(&source_path) + .file_name() + .unwrap() + .to_str() + .unwrap() + .strip_suffix(".cairo") + .unwrap(); + format!( + "feature_contracts/cairo{}/compiled/{}{}.json", + match cairo_version { + CairoVersion::Cairo0 => "0", + CairoVersion::Cairo1 => "1", + }, + base_name, + match cairo_version { + CairoVersion::Cairo0 => "_compiled", + CairoVersion::Cairo1 => ".casm", + } + ) + } } /// Fetch PC locations from the compiled contract to compute the expected PC locations in the