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

refactor(blockifier): remove versioned runnable contract class #2811

Draft
wants to merge 1 commit into
base: aviv/use_sierra_version_in_native_blockifier
Choose a base branch
from
Draft
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
21 changes: 0 additions & 21 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<VersionedRunnableCompiledClass> 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<ContractClass> for RunnableCompiledClass {
type Error = ProgramError;

Expand Down
14 changes: 3 additions & 11 deletions crates/blockifier/src/state/contract_class_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")]
Expand Down Expand Up @@ -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<VersionedRunnableCompiledClass> {
pub fn get_casm(&self, class_hash: &ClassHash) -> Option<RunnableCompiledClass> {
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);
}

Expand All @@ -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))]
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/src/state/global_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,19 +52,19 @@ impl<T: Clone> GlobalContractCache<T> {

#[derive(Clone)]
pub struct ContractCaches {
pub casm_cache: GlobalContractCache<VersionedRunnableCompiledClass>,
pub casm_cache: GlobalContractCache<RunnableCompiledClass>,
#[cfg(feature = "cairo_native")]
pub native_cache: GlobalContractCache<CachedCairoNative>,
#[cfg(feature = "cairo_native")]
pub sierra_cache: GlobalContractCache<Arc<SierraContractClass>>,
}

impl ContractCaches {
pub fn get_casm(&self, class_hash: &ClassHash) -> Option<VersionedRunnableCompiledClass> {
pub fn get_casm(&self, class_hash: &ClassHash) -> Option<RunnableCompiledClass> {
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);
}

Expand Down
14 changes: 1 addition & 13 deletions crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Expand Down
24 changes: 11 additions & 13 deletions crates/papyrus_state_reader/src/papyrus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -48,7 +47,7 @@ impl PapyrusReader {
fn get_compiled_class_inner(
&self,
class_hash: ClassHash,
) -> StateResult<VersionedRunnableCompiledClass> {
) -> StateResult<RunnableCompiledClass> {
// TODO(AVIV): Return RunnableCompiledClass.
let state_number = StateNumber(self.latest_block);
let class_declaration_block_number = self
Expand All @@ -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
Expand All @@ -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)),
}
Expand Down Expand Up @@ -134,16 +133,15 @@ impl StateReader for PapyrusReader {

fn get_compiled_class(&self, class_hash: ClassHash) -> StateResult<RunnableCompiledClass> {
// 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)
}
}
}
Expand Down
Loading