From bbb52f60d9d4e36632c218cd305a8c2f8e752025 Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Thu, 19 Dec 2024 11:50:06 +0200 Subject: [PATCH] refactor(blockifier): remove versioned runnable contract class --- .../src/execution/contract_class.rs | 21 ---------------- .../src/state/contract_class_manager.rs | 14 +++-------- crates/blockifier/src/state/global_cache.rs | 8 +++---- crates/blockifier/src/test_utils/contracts.rs | 14 +---------- .../papyrus_state_reader/src/papyrus_state.rs | 24 +++++++++---------- 5 files changed, 19 insertions(+), 62 deletions(-) diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index ceecbfcd410..cf307b3a6ce 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -65,27 +65,6 @@ pub enum RunnableCompiledClass { V1Native(NativeCompiledClassV1), } -/// Represents a runnable compiled class for Cairo, with the Sierra version (for Cairo 1). -#[derive(Clone)] -pub enum VersionedRunnableCompiledClass { - // TODO(Aviv): Delete this enum. - Cairo0(RunnableCompiledClass), - Cairo1((RunnableCompiledClass, SierraVersion)), -} - -impl From for RunnableCompiledClass { - fn from( - versioned_runnable_compiled_class: VersionedRunnableCompiledClass, - ) -> RunnableCompiledClass { - match versioned_runnable_compiled_class { - VersionedRunnableCompiledClass::Cairo0(runnable_compiled_class) - | VersionedRunnableCompiledClass::Cairo1((runnable_compiled_class, _)) => { - runnable_compiled_class - } - } - } -} - impl TryFrom for RunnableCompiledClass { type Error = ProgramError; diff --git a/crates/blockifier/src/state/contract_class_manager.rs b/crates/blockifier/src/state/contract_class_manager.rs index 4c0f901c8cb..2e98de6e1c9 100644 --- a/crates/blockifier/src/state/contract_class_manager.rs +++ b/crates/blockifier/src/state/contract_class_manager.rs @@ -7,8 +7,6 @@ use std::sync::Arc; use cached::Cached; #[cfg(feature = "cairo_native")] use log::{error, info}; -#[cfg(feature = "cairo_native")] -use starknet_api::contract_class::SierraVersion; use starknet_api::core::ClassHash; #[cfg(feature = "cairo_native")] use starknet_api::state::SierraContractClass; @@ -22,7 +20,6 @@ use starknet_sierra_compile::utils::into_contract_class_for_compilation; use starknet_sierra_compile::SierraToNativeCompiler; use crate::blockifier::config::ContractClassManagerConfig; -use crate::execution::contract_class::VersionedRunnableCompiledClass; #[cfg(feature = "cairo_native")] use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass}; #[cfg(feature = "cairo_native")] @@ -122,12 +119,12 @@ impl ContractClassManager { } /// Returns the casm compiled class for the given class hash, if it exists in cache. - pub fn get_casm(&self, class_hash: &ClassHash) -> Option { + pub fn get_casm(&self, class_hash: &ClassHash) -> Option { self.contract_caches.get_casm(class_hash) } /// Sets the casm compiled class for the given class hash in the cache. - pub fn set_casm(&self, class_hash: ClassHash, compiled_class: VersionedRunnableCompiledClass) { + pub fn set_casm(&self, class_hash: ClassHash, compiled_class: RunnableCompiledClass) { self.contract_caches.set_casm(class_hash, compiled_class); } @@ -140,13 +137,8 @@ impl ContractClassManager { #[cfg(feature = "cairo_native")] fn cache_request_contracts(&self, request: &CompilationRequest) { let (class_hash, sierra, casm) = request.clone(); - let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program).unwrap(); self.contract_caches.set_sierra(class_hash, sierra); - let cached_casm = VersionedRunnableCompiledClass::Cairo1(( - RunnableCompiledClass::from(casm), - sierra_version, - )); - self.contract_caches.set_casm(class_hash, cached_casm); + self.contract_caches.set_casm(class_hash, RunnableCompiledClass::V1(casm)); } #[cfg(any(feature = "testing", test))] diff --git a/crates/blockifier/src/state/global_cache.rs b/crates/blockifier/src/state/global_cache.rs index abc105d839c..abf4e6c7802 100644 --- a/crates/blockifier/src/state/global_cache.rs +++ b/crates/blockifier/src/state/global_cache.rs @@ -5,7 +5,7 @@ use starknet_api::core::ClassHash; #[cfg(feature = "cairo_native")] use starknet_api::state::SierraContractClass; -use crate::execution::contract_class::VersionedRunnableCompiledClass; +use crate::execution::contract_class::RunnableCompiledClass; #[cfg(feature = "cairo_native")] use crate::execution::native::contract_class::NativeCompiledClassV1; @@ -52,7 +52,7 @@ impl GlobalContractCache { #[derive(Clone)] pub struct ContractCaches { - pub casm_cache: GlobalContractCache, + pub casm_cache: GlobalContractCache, #[cfg(feature = "cairo_native")] pub native_cache: GlobalContractCache, #[cfg(feature = "cairo_native")] @@ -60,11 +60,11 @@ pub struct ContractCaches { } impl ContractCaches { - pub fn get_casm(&self, class_hash: &ClassHash) -> Option { + pub fn get_casm(&self, class_hash: &ClassHash) -> Option { self.casm_cache.get(class_hash) } - pub fn set_casm(&self, class_hash: ClassHash, compiled_class: VersionedRunnableCompiledClass) { + pub fn set_casm(&self, class_hash: ClassHash, compiled_class: RunnableCompiledClass) { self.casm_cache.set(class_hash, compiled_class); } diff --git a/crates/blockifier/src/test_utils/contracts.rs b/crates/blockifier/src/test_utils/contracts.rs index b9e24b0b846..6bec15ea0f1 100644 --- a/crates/blockifier/src/test_utils/contracts.rs +++ b/crates/blockifier/src/test_utils/contracts.rs @@ -17,7 +17,7 @@ use starknet_types_core::felt::Felt; use strum::IntoEnumIterator; use strum_macros::EnumIter; -use crate::execution::contract_class::{RunnableCompiledClass, VersionedRunnableCompiledClass}; +use crate::execution::contract_class::RunnableCompiledClass; use crate::execution::entry_point::CallEntryPoint; #[cfg(feature = "cairo_native")] use crate::execution::native::contract_class::NativeCompiledClassV1; @@ -191,18 +191,6 @@ impl FeatureContract { self.get_class().try_into().unwrap() } - #[allow(dead_code)] - pub fn get_versioned_runnable_class(&self) -> VersionedRunnableCompiledClass { - let runnable_class = self.get_runnable_class(); - match self.cairo_version() { - CairoVersion::Cairo0 => VersionedRunnableCompiledClass::Cairo0(runnable_class), - - CairoVersion::Cairo1(_) => { - VersionedRunnableCompiledClass::Cairo1((runnable_class, self.get_sierra_version())) - } - } - } - pub fn get_raw_sierra(&self) -> String { if self.cairo_version() == CairoVersion::Cairo0 { panic!("The sierra contract is only available for Cairo1."); diff --git a/crates/papyrus_state_reader/src/papyrus_state.rs b/crates/papyrus_state_reader/src/papyrus_state.rs index 15d45d477b7..5e8bb401935 100644 --- a/crates/papyrus_state_reader/src/papyrus_state.rs +++ b/crates/papyrus_state_reader/src/papyrus_state.rs @@ -2,7 +2,6 @@ use blockifier::execution::contract_class::{ CompiledClassV0, CompiledClassV1, RunnableCompiledClass, - VersionedRunnableCompiledClass, }; use blockifier::state::contract_class_manager::ContractClassManager; use blockifier::state::errors::{couple_casm_and_sierra, StateError}; @@ -48,7 +47,7 @@ impl PapyrusReader { fn get_compiled_class_inner( &self, class_hash: ClassHash, - ) -> StateResult { + ) -> StateResult { // TODO(AVIV): Return RunnableCompiledClass. let state_number = StateNumber(self.latest_block); let class_declaration_block_number = self @@ -72,9 +71,9 @@ impl PapyrusReader { let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program)?; let runnable_compiled = RunnableCompiledClass::V1(CompiledClassV1::try_from(( casm_compiled_class, - sierra_version.clone(), + sierra_version, ))?); - return Ok(VersionedRunnableCompiledClass::Cairo1((runnable_compiled, sierra_version))); + return Ok(runnable_compiled); } let v0_compiled_class = self @@ -84,8 +83,8 @@ impl PapyrusReader { .map_err(|err| StateError::StateReadError(err.to_string()))?; match v0_compiled_class { - Some(starknet_api_contract_class) => Ok(VersionedRunnableCompiledClass::Cairo0( - CompiledClassV0::try_from(starknet_api_contract_class)?.into(), + Some(starknet_api_contract_class) => Ok(RunnableCompiledClass::V0( + CompiledClassV0::try_from(starknet_api_contract_class)?, )), None => Err(StateError::UndeclaredClassHash(class_hash)), } @@ -134,16 +133,15 @@ impl StateReader for PapyrusReader { fn get_compiled_class(&self, class_hash: ClassHash) -> StateResult { // Assumption: the global cache is cleared upon reverted blocks. - let versioned_contract_class = self.contract_class_manager.get_casm(&class_hash); + let contract_class = self.contract_class_manager.get_casm(&class_hash); - match versioned_contract_class { - Some(contract_class) => Ok(RunnableCompiledClass::from(contract_class)), + match contract_class { + Some(contract_class) => Ok(contract_class), None => { - let versioned_contract_class_from_db = self.get_compiled_class_inner(class_hash)?; + let contract_class_from_db = self.get_compiled_class_inner(class_hash)?; // The class was declared in a previous (finalized) state; update the global cache. - self.contract_class_manager - .set_casm(class_hash, versioned_contract_class_from_db.clone()); - Ok(RunnableCompiledClass::from(versioned_contract_class_from_db)) + self.contract_class_manager.set_casm(class_hash, contract_class_from_db.clone()); + Ok(contract_class_from_db) } } }