From facff3654155d86423a1ee7c9bfc7f2fe16ad3be Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Thu, 19 Dec 2024 10:39:29 +0200 Subject: [PATCH] refactor(blockifier): use sierra version in native blockifier --- .../src/execution/contract_class.rs | 8 ++--- crates/native_blockifier/src/errors.rs | 2 ++ .../src/state_readers/py_state_reader.rs | 36 +++++++++++++++---- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index f39a578872..385709a243 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -294,12 +294,12 @@ impl CompiledClassV1 { get_visited_segments(&self.bytecode_segment_lengths, &mut reversed_visited_pcs, &mut 0) } - pub fn try_from_json_string(raw_contract_class: &str) -> Result { + pub fn try_from_json_string( + raw_contract_class: &str, + sierra_version: SierraVersion, + ) -> Result { let casm_contract_class: CasmContractClass = serde_json::from_str(raw_contract_class)?; - // TODO(Aviv): Use Sierra version received from Python state reader. - let sierra_version = SierraVersion::DEPRECATED; let contract_class = CompiledClassV1::try_from((casm_contract_class, sierra_version))?; - Ok(contract_class) } } diff --git a/crates/native_blockifier/src/errors.rs b/crates/native_blockifier/src/errors.rs index 2128f41748..77d4c03248 100644 --- a/crates/native_blockifier/src/errors.rs +++ b/crates/native_blockifier/src/errors.rs @@ -89,6 +89,8 @@ pub enum NativeBlockifierInputError { ProgramError(#[from] ProgramError), #[error(transparent)] PyFeltParseError(#[from] FromStrError), + #[error("Sierra version is missing.")] + MissingSierraVersion, #[error(transparent)] StarknetApiError(#[from] StarknetApiError), #[error("Unknown builtin: {0}.")] diff --git a/crates/native_blockifier/src/state_readers/py_state_reader.rs b/crates/native_blockifier/src/state_readers/py_state_reader.rs index 7f1f8a8fe3..037a6f4b72 100644 --- a/crates/native_blockifier/src/state_readers/py_state_reader.rs +++ b/crates/native_blockifier/src/state_readers/py_state_reader.rs @@ -82,13 +82,20 @@ impl StateReader for PyStateReader { // Extract the raw compiled class let py_raw_compiled_class: PyRawCompiledClass = py_versioned_raw_compiled_class.get_item(0)?.extract()?; - let runnable_compiled_class = RunnableCompiledClass::try_from(py_raw_compiled_class)?; // Extract and process the Sierra version let (minor, major, patch): (u64, u64, u64) = py_versioned_raw_compiled_class.get_item(1)?.extract()?; - // TODO(Aviv): Return it in the next PR after the change in the StateReader API. - let _sierra_version = SierraVersion::new(major, minor, patch); + + let sierra_version = SierraVersion::new(major, minor, patch); + + let versioned_py_raw_compiled_class = VersionedPyRawClass { + raw_compiled_class: py_raw_compiled_class, + optional_sierra_version: Some(sierra_version), + }; + + let runnable_compiled_class = + RunnableCompiledClass::try_from(versioned_py_raw_compiled_class)?; Ok(runnable_compiled_class) }) .map_err(|err| { @@ -119,15 +126,30 @@ pub struct PyRawCompiledClass { pub version: usize, } -impl TryFrom for RunnableCompiledClass { +pub struct VersionedPyRawClass { + raw_compiled_class: PyRawCompiledClass, + optional_sierra_version: Option, +} + +impl TryFrom for RunnableCompiledClass { type Error = NativeBlockifierError; - fn try_from(raw_compiled_class: PyRawCompiledClass) -> NativeBlockifierResult { + fn try_from(versioned_raw_compiled_class: VersionedPyRawClass) -> NativeBlockifierResult { + let raw_compiled_class = versioned_raw_compiled_class.raw_compiled_class; + match raw_compiled_class.version { 0 => Ok(CompiledClassV0::try_from_json_string(&raw_compiled_class.raw_compiled_class)? .into()), - 1 => Ok(CompiledClassV1::try_from_json_string(&raw_compiled_class.raw_compiled_class)? - .into()), + 1 => { + let sierra_version = versioned_raw_compiled_class + .optional_sierra_version + .ok_or(NativeBlockifierInputError::MissingSierraVersion)?; + Ok(CompiledClassV1::try_from_json_string( + &raw_compiled_class.raw_compiled_class, + sierra_version, + )? + .into()) + } _ => Err(NativeBlockifierInputError::UnsupportedContractClassVersion { version: raw_compiled_class.version, })?,