From d5ca4fdbadbc5ca4f9e7c04924c9762d38a57958 Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Sun, 15 Dec 2024 17:11:41 +0200 Subject: [PATCH] chore(papyrus_execution): get versioned contract class --- .../papyrus_execution/src/execution_utils.rs | 22 +++++++++++------ crates/papyrus_execution/src/state_reader.rs | 24 ++++++++++++++----- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/crates/papyrus_execution/src/execution_utils.rs b/crates/papyrus_execution/src/execution_utils.rs index 9a6f34ffb79..543c2baf8b7 100644 --- a/crates/papyrus_execution/src/execution_utils.rs +++ b/crates/papyrus_execution/src/execution_utils.rs @@ -6,6 +6,7 @@ use blockifier::execution::contract_class::{ CompiledClassV0, CompiledClassV1, RunnableCompiledClass, + VersionedRunnableCompiledClass, }; use blockifier::state::cached_state::{CachedState, CommitmentStateDiff, MutRefState}; use blockifier::state::state_api::StateReader; @@ -19,6 +20,7 @@ use papyrus_storage::state::StateStorageReader; use papyrus_storage::{StorageError, StorageResult, StorageTxn}; // Expose the tool for creating entry point selectors from function names. pub use starknet_api::abi::abi_utils::selector_from_name; +use starknet_api::contract_class::SierraVersion; use starknet_api::core::{ClassHash, ContractAddress, Nonce}; use starknet_api::state::{StateNumber, StorageKey, ThinStateDiff}; use starknet_types_core::felt::Felt; @@ -55,20 +57,26 @@ impl TryFrom for ExecutionConfig { } } -pub(crate) fn get_contract_class( +pub(crate) fn get_versioned_contract_class( txn: &StorageTxn<'_, RO>, class_hash: &ClassHash, state_number: StateNumber, -) -> Result, ExecutionUtilsError> { +) -> Result, ExecutionUtilsError> { match txn.get_state_reader()?.get_class_definition_block_number(class_hash)? { Some(block_number) if state_number.is_before(block_number) => return Ok(None), Some(_block_number) => { - let Some(casm) = txn.get_casm(class_hash)? else { + let (Some(casm), Some(sierra)) = txn.get_casm_and_sierra(class_hash)? else { return Err(ExecutionUtilsError::CasmTableNotSynced); }; - return Ok(Some(RunnableCompiledClass::V1( + let runnable_compiled_class = RunnableCompiledClass::V1( CompiledClassV1::try_from(casm).map_err(ExecutionUtilsError::ProgramError)?, - ))); + ); + let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program) + .map_err(|_| ExecutionUtilsError::CasmTableNotSynced)?; + return Ok(Some(VersionedRunnableCompiledClass::Cairo1(( + runnable_compiled_class, + sierra_version, + )))); } None => {} }; @@ -78,9 +86,9 @@ pub(crate) fn get_contract_class( else { return Ok(None); }; - Ok(Some(RunnableCompiledClass::V0( + Ok(Some(VersionedRunnableCompiledClass::Cairo0(RunnableCompiledClass::V0( CompiledClassV0::try_from(deprecated_class).map_err(ExecutionUtilsError::ProgramError)?, - ))) + )))) } /// Given an ExecutableTransactionInput, returns a function that will convert the corresponding diff --git a/crates/papyrus_execution/src/state_reader.rs b/crates/papyrus_execution/src/state_reader.rs index b67aaa170e3..2195ec015c5 100644 --- a/crates/papyrus_execution/src/state_reader.rs +++ b/crates/papyrus_execution/src/state_reader.rs @@ -8,6 +8,7 @@ use blockifier::execution::contract_class::{ CompiledClassV0, CompiledClassV1, RunnableCompiledClass, + VersionedRunnableCompiledClass, }; use blockifier::state::errors::StateError; use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; @@ -15,12 +16,13 @@ use papyrus_common::pending_classes::{ApiContractClass, PendingClassesTrait}; use papyrus_common::state::DeclaredClassHashEntry; use papyrus_storage::state::StateStorageReader; use papyrus_storage::{StorageError, StorageReader}; +use starknet_api::contract_class::SierraVersion; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::state::{StateNumber, StorageKey}; use starknet_types_core::felt::Felt; use crate::execution_utils; -use crate::execution_utils::{get_contract_class, ExecutionUtilsError}; +use crate::execution_utils::{get_versioned_contract_class, ExecutionUtilsError}; use crate::objects::PendingData; /// A view into the state at a specific state number. @@ -81,10 +83,20 @@ impl BlockifierStateReader for ExecutionStateReader { .as_ref() .and_then(|pending_data| pending_data.classes.get_compiled_class(class_hash)) { - return Ok(RunnableCompiledClass::V1( - CompiledClassV1::try_from(pending_casm).map_err(StateError::ProgramError)?, - )); + if let Some(ApiContractClass::ContractClass(sierra)) = self + .maybe_pending_data + .as_ref() + .and_then(|pending_data| pending_data.classes.get_class(class_hash)) + { + let runnable_compiled_class = RunnableCompiledClass::V1( + CompiledClassV1::try_from(pending_casm).map_err(StateError::ProgramError)?, + ); + let _sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program)?; + // TODO(AVIV): Use the sierra version when the return type is updated. + return Ok(runnable_compiled_class); + } } + if let Some(ApiContractClass::DeprecatedContractClass(pending_deprecated_class)) = self .maybe_pending_data .as_ref() @@ -95,12 +107,12 @@ impl BlockifierStateReader for ExecutionStateReader { .map_err(StateError::ProgramError)?, )); } - match get_contract_class( + match get_versioned_contract_class( &self.storage_reader.begin_ro_txn().map_err(storage_err_to_state_err)?, &class_hash, self.state_number, ) { - Ok(Some(contract_class)) => Ok(contract_class), + Ok(Some(versioned_contract_class)) => Ok(versioned_contract_class.into()), Ok(None) => Err(StateError::UndeclaredClassHash(class_hash)), Err(ExecutionUtilsError::CasmTableNotSynced) => { self.missing_compiled_class.set(Some(class_hash));