Skip to content

Commit

Permalink
refactor(blockifier): use sierra version in native blockifier
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Dec 19, 2024
1 parent 0b19960 commit 2553415
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
8 changes: 4 additions & 4 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,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<CompiledClassV1, ProgramError> {
pub fn try_from_json_string(
raw_contract_class: &str,
sierra_version: SierraVersion,
) -> Result<CompiledClassV1, ProgramError> {
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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/native_blockifier/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")]
Expand Down
36 changes: 29 additions & 7 deletions crates/native_blockifier/src/state_readers/py_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -119,15 +126,30 @@ pub struct PyRawCompiledClass {
pub version: usize,
}

impl TryFrom<PyRawCompiledClass> for RunnableCompiledClass {
pub struct VersionedPyRawClass {
raw_compiled_class: PyRawCompiledClass,
optional_sierra_version: Option<SierraVersion>,
}

impl TryFrom<VersionedPyRawClass> for RunnableCompiledClass {
type Error = NativeBlockifierError;

fn try_from(raw_compiled_class: PyRawCompiledClass) -> NativeBlockifierResult<Self> {
fn try_from(versioned_raw_compiled_class: VersionedPyRawClass) -> NativeBlockifierResult<Self> {
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,
})?,
Expand Down

0 comments on commit 2553415

Please sign in to comment.