Skip to content

Commit

Permalink
refactor(blockifier): rename contract class to runnable contract class
Browse files Browse the repository at this point in the history
  • Loading branch information
noaov1 committed Oct 31, 2024
1 parent 34599b9 commit d449235
Show file tree
Hide file tree
Showing 27 changed files with 117 additions and 107 deletions.
8 changes: 4 additions & 4 deletions crates/batcher/src/papyrus_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO(yael 22/9/2024): This module is copied from native_blockifier, need to how to share it
// between the crates.
use blockifier::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1};
use blockifier::execution::contract_class::{RunnableContractClass, ContractClassV0, ContractClassV1};
use blockifier::state::errors::StateError;
use blockifier::state::global_cache::GlobalContractCache;
use blockifier::state::state_api::{StateReader, StateResult};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl PapyrusReader {
fn get_compiled_contract_class_inner(
&self,
class_hash: ClassHash,
) -> StateResult<ContractClass> {
) -> StateResult<RunnableContractClass> {
let state_number = StateNumber(self.latest_block);
let class_declaration_block_number = self
.reader()?
Expand All @@ -60,7 +60,7 @@ impl PapyrusReader {
inconsistent.",
);

return Ok(ContractClass::V1(ContractClassV1::try_from(casm_contract_class)?));
return Ok(RunnableContractClass::V1(ContractClassV1::try_from(casm_contract_class)?));
}

let v0_contract_class = self
Expand Down Expand Up @@ -118,7 +118,7 @@ impl StateReader for PapyrusReader {
}
}

fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<ContractClass> {
fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<RunnableContractClass> {
// Assumption: the global cache is cleared upon reverted blocks.
let contract_class = self.global_class_hash_to_class.get(&class_hash);

Expand Down
9 changes: 6 additions & 3 deletions crates/blockifier/src/concurrency/versioned_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_types_core::felt::Felt;

use crate::concurrency::versioned_storage::VersionedStorage;
use crate::concurrency::TxIndex;
use crate::execution::contract_class::ContractClass;
use crate::execution::contract_class::RunnableContractClass;
use crate::state::cached_state::{ContractClassMapping, StateMaps};
use crate::state::errors::StateError;
use crate::state::state_api::{StateReader, StateResult, UpdatableState};
Expand All @@ -34,7 +34,7 @@ pub struct VersionedState<S: StateReader> {
// the compiled contract classes mapping. Each key with value false, sohuld not apprear
// in the compiled contract classes mapping.
declared_contracts: VersionedStorage<ClassHash, bool>,
compiled_contract_classes: VersionedStorage<ClassHash, ContractClass>,
compiled_contract_classes: VersionedStorage<ClassHash, RunnableContractClass>,
}

impl<S: StateReader> VersionedState<S> {
Expand Down Expand Up @@ -336,7 +336,10 @@ impl<S: StateReader> StateReader for VersionedStateProxy<S> {
}
}

fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<ContractClass> {
fn get_compiled_contract_class(
&self,
class_hash: ClassHash,
) -> StateResult<RunnableContractClass> {
let mut state = self.state();
match state.compiled_contract_classes.read(self.tx_index, class_hash) {
Some(value) => Ok(value),
Expand Down
54 changes: 27 additions & 27 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,46 +62,46 @@ pub enum TrackedResource {

/// Represents a runnable Starknet contract class (meaning, the program is runnable by the VM).
#[derive(Clone, Debug, Eq, PartialEq, derive_more::From)]
pub enum ContractClass {
pub enum RunnableContractClass {
V0(ContractClassV0),
V1(ContractClassV1),
#[cfg(feature = "cairo_native")]
V1Native(NativeContractClassV1),
}

impl TryFrom<RawContractClass> for ContractClass {
impl TryFrom<RawContractClass> for RunnableContractClass {
type Error = ProgramError;

fn try_from(raw_contract_class: RawContractClass) -> Result<Self, Self::Error> {
let contract_class: ContractClass = match raw_contract_class {
let contract_class: RunnableContractClass = match raw_contract_class {
RawContractClass::V0(raw_contract_class) => {
ContractClass::V0(raw_contract_class.try_into()?)
RunnableContractClass::V0(raw_contract_class.try_into()?)
}
RawContractClass::V1(raw_contract_class) => {
ContractClass::V1(raw_contract_class.try_into()?)
RunnableContractClass::V1(raw_contract_class.try_into()?)
}
};

Ok(contract_class)
}
}

impl ContractClass {
impl RunnableContractClass {
pub fn constructor_selector(&self) -> Option<EntryPointSelector> {
match self {
ContractClass::V0(class) => class.constructor_selector(),
ContractClass::V1(class) => class.constructor_selector(),
RunnableContractClass::V0(class) => class.constructor_selector(),
RunnableContractClass::V1(class) => class.constructor_selector(),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native(class) => class.constructor_selector(),
RunnableContractClass::V1Native(class) => class.constructor_selector(),
}
}

pub fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources {
match self {
ContractClass::V0(class) => class.estimate_casm_hash_computation_resources(),
ContractClass::V1(class) => class.estimate_casm_hash_computation_resources(),
RunnableContractClass::V0(class) => class.estimate_casm_hash_computation_resources(),
RunnableContractClass::V1(class) => class.estimate_casm_hash_computation_resources(),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native(_) => {
RunnableContractClass::V1Native(_) => {
todo!("Use casm to estimate casm hash computation resources")
}
}
Expand All @@ -112,23 +112,23 @@ impl ContractClass {
visited_pcs: &HashSet<usize>,
) -> Result<Vec<usize>, TransactionExecutionError> {
match self {
ContractClass::V0(_) => {
RunnableContractClass::V0(_) => {
panic!("get_visited_segments is not supported for v0 contracts.")
}
ContractClass::V1(class) => class.get_visited_segments(visited_pcs),
RunnableContractClass::V1(class) => class.get_visited_segments(visited_pcs),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native(_) => {
RunnableContractClass::V1Native(_) => {
panic!("get_visited_segments is not supported for native contracts.")
}
}
}

pub fn bytecode_length(&self) -> usize {
match self {
ContractClass::V0(class) => class.bytecode_length(),
ContractClass::V1(class) => class.bytecode_length(),
RunnableContractClass::V0(class) => class.bytecode_length(),
RunnableContractClass::V1(class) => class.bytecode_length(),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native(_) => {
RunnableContractClass::V1Native(_) => {
todo!("implement bytecode_length for native contracts.")
}
}
Expand All @@ -137,12 +137,12 @@ impl ContractClass {
/// Returns whether this contract should run using Cairo steps or Sierra gas.
pub fn tracked_resource(&self, min_sierra_version: &CompilerVersion) -> TrackedResource {
match self {
ContractClass::V0(_) => TrackedResource::CairoSteps,
ContractClass::V1(contract_class) => {
RunnableContractClass::V0(_) => TrackedResource::CairoSteps,
RunnableContractClass::V1(contract_class) => {
contract_class.tracked_resource(min_sierra_version)
}
#[cfg(feature = "cairo_native")]
ContractClass::V1Native(_) => TrackedResource::SierraGas,
RunnableContractClass::V1Native(_) => TrackedResource::SierraGas,
}
}
}
Expand Down Expand Up @@ -530,7 +530,7 @@ fn convert_entry_points_v1(external: &[CasmContractEntryPoint]) -> Vec<EntryPoin
#[derive(Clone, Debug)]
// TODO(Ayelet,10/02/2024): Change to bytes.
pub struct ClassInfo {
contract_class: ContractClass,
contract_class: RunnableContractClass,
sierra_program_length: usize,
abi_length: usize,
}
Expand All @@ -554,7 +554,7 @@ impl ClassInfo {
self.contract_class.bytecode_length()
}

pub fn contract_class(&self) -> ContractClass {
pub fn contract_class(&self) -> RunnableContractClass {
self.contract_class.clone()
}

Expand All @@ -574,15 +574,15 @@ impl ClassInfo {
}

pub fn new(
contract_class: &ContractClass,
contract_class: &RunnableContractClass,
sierra_program_length: usize,
abi_length: usize,
) -> ContractClassResult<Self> {
let (contract_class_version, condition) = match contract_class {
ContractClass::V0(_) => (0, sierra_program_length == 0),
ContractClass::V1(_) => (1, sierra_program_length > 0),
RunnableContractClass::V0(_) => (0, sierra_program_length == 0),
RunnableContractClass::V1(_) => (1, sierra_program_length > 0),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native(_) => (1, sierra_program_length > 0),
RunnableContractClass::V1Native(_) => (1, sierra_program_length > 0),
};

if condition {
Expand Down
12 changes: 6 additions & 6 deletions crates/blockifier/src/execution/execution_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::errors::{
};
use super::syscalls::hint_processor::ENTRYPOINT_NOT_FOUND_ERROR;
use crate::execution::call_info::{CallInfo, Retdata};
use crate::execution::contract_class::{ContractClass, TrackedResource};
use crate::execution::contract_class::{RunnableContractClass, TrackedResource};
use crate::execution::entry_point::{
execute_constructor_entry_point,
CallEntryPoint,
Expand All @@ -54,7 +54,7 @@ pub const SEGMENT_ARENA_BUILTIN_SIZE: usize = 3;
/// A wrapper for execute_entry_point_call that performs pre and post-processing.
pub fn execute_entry_point_call_wrapper(
mut call: CallEntryPoint,
contract_class: ContractClass,
contract_class: RunnableContractClass,
state: &mut dyn State,
resources: &mut ExecutionResources,
context: &mut EntryPointExecutionContext,
Expand Down Expand Up @@ -118,13 +118,13 @@ pub fn execute_entry_point_call_wrapper(
/// Executes a specific call to a contract entry point and returns its output.
pub fn execute_entry_point_call(
call: CallEntryPoint,
contract_class: ContractClass,
contract_class: RunnableContractClass,
state: &mut dyn State,
resources: &mut ExecutionResources,
context: &mut EntryPointExecutionContext,
) -> EntryPointExecutionResult<CallInfo> {
match contract_class {
ContractClass::V0(contract_class) => {
RunnableContractClass::V0(contract_class) => {
deprecated_entry_point_execution::execute_entry_point_call(
call,
contract_class,
Expand All @@ -133,15 +133,15 @@ pub fn execute_entry_point_call(
context,
)
}
ContractClass::V1(contract_class) => entry_point_execution::execute_entry_point_call(
RunnableContractClass::V1(contract_class) => entry_point_execution::execute_entry_point_call(
call,
contract_class,
state,
resources,
context,
),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native(contract_class) => {
RunnableContractClass::V1Native(contract_class) => {
native_entry_point_execution::execute_entry_point_call(
call,
contract_class,
Expand Down
10 changes: 5 additions & 5 deletions crates/blockifier/src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet_types_core::felt::Felt;

use crate::abi::abi_utils::get_fee_token_var_address;
use crate::context::TransactionContext;
use crate::execution::contract_class::ContractClass;
use crate::execution::contract_class::RunnableContractClass;
use crate::state::errors::StateError;
use crate::state::state_api::{State, StateReader, StateResult, UpdatableState};
use crate::transaction::objects::TransactionExecutionInfo;
Expand All @@ -18,7 +18,7 @@ use crate::utils::{strict_subtract_mappings, subtract_mappings};
#[path = "cached_state_test.rs"]
mod test;

pub type ContractClassMapping = HashMap<ClassHash, ContractClass>;
pub type ContractClassMapping = HashMap<ClassHash, RunnableContractClass>;

/// Caches read and write requests.
///
Expand Down Expand Up @@ -174,7 +174,7 @@ impl<S: StateReader> StateReader for CachedState<S> {
Ok(*class_hash)
}

fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<ContractClass> {
fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<RunnableContractClass> {
let mut cache = self.cache.borrow_mut();
let class_hash_to_class = &mut *self.class_hash_to_class.borrow_mut();

Expand Down Expand Up @@ -257,7 +257,7 @@ impl<S: StateReader> State for CachedState<S> {
fn set_contract_class(
&mut self,
class_hash: ClassHash,
contract_class: ContractClass,
contract_class: RunnableContractClass,
) -> StateResult<()> {
self.class_hash_to_class.get_mut().insert(class_hash, contract_class);
let mut cache = self.cache.borrow_mut();
Expand Down Expand Up @@ -493,7 +493,7 @@ impl<'a, S: StateReader + ?Sized> StateReader for MutRefState<'a, S> {
self.0.get_class_hash_at(contract_address)
}

fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<ContractClass> {
fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<RunnableContractClass> {
self.0.get_compiled_contract_class(class_hash)
}

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 @@ -3,10 +3,10 @@ use std::sync::{Arc, Mutex, MutexGuard};
use cached::{Cached, SizedCache};
use starknet_api::core::ClassHash;

use crate::execution::contract_class::ContractClass;
use crate::execution::contract_class::RunnableContractClass;

// Note: `ContractClassLRUCache` key-value types must align with `ContractClassMapping`.
type ContractClassLRUCache = SizedCache<ClassHash, ContractClass>;
type ContractClassLRUCache = SizedCache<ClassHash, RunnableContractClass>;
pub type LockedContractClassCache<'a> = MutexGuard<'a, ContractClassLRUCache>;
#[derive(Debug, Clone)]
// Thread-safe LRU cache for contract classes, optimized for inter-language sharing when
Expand All @@ -23,11 +23,11 @@ impl GlobalContractCache {
self.0.lock().expect("Global contract cache is poisoned.")
}

pub fn get(&self, class_hash: &ClassHash) -> Option<ContractClass> {
pub fn get(&self, class_hash: &ClassHash) -> Option<RunnableContractClass> {
self.lock().cache_get(class_hash).cloned()
}

pub fn set(&self, class_hash: ClassHash, contract_class: ContractClass) {
pub fn set(&self, class_hash: ClassHash, contract_class: RunnableContractClass) {
self.lock().cache_set(class_hash, contract_class);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/state/state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_types_core::felt::Felt;
use super::cached_state::{ContractClassMapping, StateMaps};
use crate::abi::abi_utils::get_fee_token_var_address;
use crate::abi::sierra_types::next_storage_key;
use crate::execution::contract_class::ContractClass;
use crate::execution::contract_class::RunnableContractClass;
use crate::state::errors::StateError;

pub type StateResult<T> = Result<T, StateError>;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub trait StateReader {
fn get_class_hash_at(&self, contract_address: ContractAddress) -> StateResult<ClassHash>;

/// Returns the contract class of the given class hash.
fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<ContractClass>;
fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<RunnableContractClass>;

/// Returns the compiled class hash of the given class hash.
fn get_compiled_class_hash(&self, class_hash: ClassHash) -> StateResult<CompiledClassHash>;
Expand Down Expand Up @@ -94,7 +94,7 @@ pub trait State: StateReader {
fn set_contract_class(
&mut self,
class_hash: ClassHash,
contract_class: ContractClass,
contract_class: RunnableContractClass,
) -> StateResult<()>;

/// Sets the given compiled class hash under the given class hash.
Expand Down
Loading

0 comments on commit d449235

Please sign in to comment.