Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(blockifier_reexecution): compile sierra to versioned runnable class #2679

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions crates/blockifier_reexecution/src/state_reader/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cairo_lang_starknet_classes::contract_class::ContractEntryPoints;
use cairo_lang_utils::bigint::BigUintAsHex;
use flate2::bufread;
use serde::Deserialize;
use starknet_api::contract_class::{ContractClass, EntryPointType};
use starknet_api::contract_class::{ContractClass, EntryPointType, SierraVersion};
use starknet_api::core::EntryPointSelector;
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass,
Expand Down Expand Up @@ -72,8 +72,11 @@ pub fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
Ok(s)
}

/// Compile a FlattenedSierraClass to a ContractClass V1 (casm) using cairo_lang_starknet_classes.
pub fn sierra_to_contact_class_v1(sierra: FlattenedSierraClass) -> StateResult<ContractClass> {
/// Compile a FlattenedSierraClass to a versioned ContractClass V1 (casm) using
/// cairo_lang_starknet_classes.
pub fn sierra_to_versioned_contract_class_v1(
sierra: FlattenedSierraClass,
) -> StateResult<(ContractClass, SierraVersion)> {
let middle_sierra: MiddleSierraContractClass = {
let v = serde_json::to_value(sierra).map_err(serde_err_to_state_err);
serde_json::from_value(v?).map_err(serde_err_to_state_err)?
Expand All @@ -86,6 +89,11 @@ pub fn sierra_to_contact_class_v1(sierra: FlattenedSierraClass) -> StateResult<C
abi: None,
};

let sierra_program_values =
sierra.sierra_program.iter().take(3).map(|felt| felt.value.clone()).collect::<Vec<_>>();

let sierra_version = SierraVersion::extract_from_program(&sierra_program_values).unwrap();

let casm =
cairo_lang_starknet_classes::casm_contract_class::CasmContractClass::from_contract_class(
sierra,
Expand All @@ -94,7 +102,7 @@ pub fn sierra_to_contact_class_v1(sierra: FlattenedSierraClass) -> StateResult<C
)
// TODO(Aviv): Reconsider the unwrap.
.unwrap();
Ok(ContractClass::V1(casm))
Ok((ContractClass::V1(casm), sierra_version))
}

/// Compile a CompressedLegacyContractClass to a ContractClass V0 using cairo_lang_starknet_classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_core::types::ContractClass as StarknetContractClass;
use starknet_types_core::felt::Felt;

use crate::state_reader::compile::{legacy_to_contract_class_v0, sierra_to_contact_class_v1};
use crate::state_reader::compile::{
legacy_to_contract_class_v0,
sierra_to_versioned_contract_class_v1,
};
use crate::state_reader::errors::ReexecutionResult;
use crate::state_reader::reexecution_state_reader::{
ConsecutiveReexecutionStateReaders,
Expand Down Expand Up @@ -157,7 +160,8 @@ impl StateReader for OfflineStateReader {
fn get_compiled_class(&self, class_hash: ClassHash) -> StateResult<RunnableCompiledClass> {
match self.get_contract_class(&class_hash)? {
StarknetContractClass::Sierra(sierra) => {
Ok(sierra_to_contact_class_v1(sierra).unwrap().try_into().unwrap())
let (casm, _) = sierra_to_versioned_contract_class_v1(sierra).unwrap();
Ok(casm.try_into().unwrap())
}
StarknetContractClass::Legacy(legacy) => {
Ok(legacy_to_contract_class_v0(legacy).unwrap().try_into().unwrap())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use starknet_api::core::ClassHash;
use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_core::types::ContractClass as StarknetContractClass;

use crate::state_reader::compile::{legacy_to_contract_class_v0, sierra_to_contact_class_v1};
use crate::state_reader::compile::{
legacy_to_contract_class_v0,
sierra_to_versioned_contract_class_v1,
};
use crate::state_reader::errors::{ReexecutionError, ReexecutionResult};

pub trait ReexecutionStateReader {
Expand All @@ -24,14 +27,10 @@ pub trait ReexecutionStateReader {
let abi_length = sierra.abi.len();
let sierra_length = sierra.sierra_program.len();

let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program)?;
let (contract_class, sierra_version) =
sierra_to_versioned_contract_class_v1(sierra)?;

Ok(ClassInfo::new(
&sierra_to_contact_class_v1(sierra)?,
sierra_length,
abi_length,
sierra_version,
)?)
Ok(ClassInfo::new(&contract_class, sierra_length, abi_length, sierra_version)?)
}
StarknetContractClass::Legacy(legacy) => {
let abi_length =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use starknet_gateway::rpc_state_reader::RpcStateReader;
use starknet_types_core::felt::Felt;

use crate::retry_request;
use crate::state_reader::compile::{legacy_to_contract_class_v0, sierra_to_contact_class_v1};
use crate::state_reader::compile::{
legacy_to_contract_class_v0,
sierra_to_versioned_contract_class_v1,
};
use crate::state_reader::errors::ReexecutionResult;
use crate::state_reader::offline_state_reader::SerializableDataNextBlock;
use crate::state_reader::reexecution_state_reader::{
Expand Down Expand Up @@ -131,7 +134,8 @@ impl StateReader for TestStateReader {

match contract_class {
StarknetContractClass::Sierra(sierra) => {
Ok(sierra_to_contact_class_v1(sierra).unwrap().try_into().unwrap())
let (casm, _) = sierra_to_versioned_contract_class_v1(sierra).unwrap();
Ok(RunnableCompiledClass::try_from(casm).unwrap())
}
StarknetContractClass::Legacy(legacy) => {
Ok(legacy_to_contract_class_v0(legacy).unwrap().try_into().unwrap())
Expand Down