Skip to content

Commit

Permalink
refactor(state, blockifier, starknet_api): rename ContractClass to Co…
Browse files Browse the repository at this point in the history
…mpiledClass
  • Loading branch information
Yoni-Starkware committed Nov 27, 2024
1 parent 13693b0 commit 5a75240
Show file tree
Hide file tree
Showing 30 changed files with 167 additions and 166 deletions.
6 changes: 3 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::RunnableContractClass;
use crate::execution::contract_class::RunnableCompiledClass;
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, RunnableContractClass>,
compiled_contract_classes: VersionedStorage<ClassHash, RunnableCompiledClass>,
}

impl<S: StateReader> VersionedState<S> {
Expand Down Expand Up @@ -339,7 +339,7 @@ impl<S: StateReader> StateReader for VersionedStateProxy<S> {
fn get_compiled_contract_class(
&self,
class_hash: ClassHash,
) -> StateResult<RunnableContractClass> {
) -> StateResult<RunnableCompiledClass> {
let mut state = self.state();
match state.compiled_contract_classes.read(self.tx_index, class_hash) {
Some(value) => Ok(value),
Expand Down
53 changes: 27 additions & 26 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::execution::entry_point::CallEntryPoint;
use crate::execution::errors::PreExecutionError;
use crate::execution::execution_utils::{poseidon_hash_many_cost, sn_api_to_cairo_vm_program};
#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;
use crate::execution::native::contract_class::NativeCompiledClassV1;
use crate::transaction::errors::TransactionExecutionError;
use crate::versioned_constants::CompilerVersion;

Expand All @@ -58,16 +58,17 @@ pub enum TrackedResource {
SierraGas, // AKA Sierra mode.
}

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

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

fn try_from(raw_contract_class: ContractClass) -> Result<Self, Self::Error> {
Expand All @@ -80,7 +81,7 @@ impl TryFrom<ContractClass> for RunnableContractClass {
}
}

impl RunnableContractClass {
impl RunnableCompiledClass {
pub fn constructor_selector(&self) -> Option<EntryPointSelector> {
match self {
Self::V0(class) => class.constructor_selector(),
Expand Down Expand Up @@ -156,16 +157,16 @@ impl RunnableContractClass {
// Note: when deserializing from a SN API class JSON string, the ABI field is ignored
// by serde, since it is not required for execution.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct ContractClassV0(pub Arc<ContractClassV0Inner>);
impl Deref for ContractClassV0 {
type Target = ContractClassV0Inner;
pub struct CompiledClassV0(pub Arc<CompiledClassV0Inner>);
impl Deref for CompiledClassV0 {
type Target = CompiledClassV0Inner;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl ContractClassV0 {
impl CompiledClassV0 {
fn constructor_selector(&self) -> Option<EntryPointSelector> {
Some(self.entry_points_by_type[&EntryPointType::Constructor].first()?.selector)
}
Expand Down Expand Up @@ -201,24 +202,24 @@ impl ContractClassV0 {
TrackedResource::CairoSteps
}

pub fn try_from_json_string(raw_contract_class: &str) -> Result<ContractClassV0, ProgramError> {
let contract_class: ContractClassV0Inner = serde_json::from_str(raw_contract_class)?;
Ok(ContractClassV0(Arc::new(contract_class)))
pub fn try_from_json_string(raw_contract_class: &str) -> Result<CompiledClassV0, ProgramError> {
let contract_class: CompiledClassV0Inner = serde_json::from_str(raw_contract_class)?;
Ok(CompiledClassV0(Arc::new(contract_class)))
}
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct ContractClassV0Inner {
pub struct CompiledClassV0Inner {
#[serde(deserialize_with = "deserialize_program")]
pub program: Program,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPointV0>>,
}

impl TryFrom<DeprecatedContractClass> for ContractClassV0 {
impl TryFrom<DeprecatedContractClass> for CompiledClassV0 {
type Error = ProgramError;

fn try_from(class: DeprecatedContractClass) -> Result<Self, Self::Error> {
Ok(Self(Arc::new(ContractClassV0Inner {
Ok(Self(Arc::new(CompiledClassV0Inner {
program: sn_api_to_cairo_vm_program(class.program)?,
entry_points_by_type: class.entry_points_by_type,
})))
Expand All @@ -227,20 +228,20 @@ impl TryFrom<DeprecatedContractClass> for ContractClassV0 {

// V1.

/// Represents a runnable Cario (Cairo 1) Starknet contract class (meaning, the program is runnable
/// Represents a runnable Cario (Cairo 1) Starknet compiled class (meaning, the program is runnable
/// by the VM). We wrap the actual class in an Arc to avoid cloning the program when cloning the
/// class.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContractClassV1(pub Arc<ContractClassV1Inner>);
impl Deref for ContractClassV1 {
pub struct CompiledClassV1(pub Arc<ContractClassV1Inner>);
impl Deref for CompiledClassV1 {
type Target = ContractClassV1Inner;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl ContractClassV1 {
impl CompiledClassV1 {
pub fn constructor_selector(&self) -> Option<EntryPointSelector> {
self.0.entry_points_by_type.constructor.first().map(|ep| ep.selector)
}
Expand Down Expand Up @@ -286,9 +287,9 @@ impl ContractClassV1 {
get_visited_segments(&self.bytecode_segment_lengths, &mut reversed_visited_pcs, &mut 0)
}

pub fn try_from_json_string(raw_contract_class: &str) -> Result<ContractClassV1, ProgramError> {
pub fn try_from_json_string(raw_contract_class: &str) -> Result<CompiledClassV1, ProgramError> {
let casm_contract_class: CasmContractClass = serde_json::from_str(raw_contract_class)?;
let contract_class = ContractClassV1::try_from(casm_contract_class)?;
let contract_class = CompiledClassV1::try_from(casm_contract_class)?;

Ok(contract_class)
}
Expand Down Expand Up @@ -413,7 +414,7 @@ impl HasSelector for EntryPointV1 {
}
}

impl TryFrom<CasmContractClass> for ContractClassV1 {
impl TryFrom<CasmContractClass> for CompiledClassV1 {
type Error = ProgramError;

fn try_from(class: CasmContractClass) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -466,7 +467,7 @@ impl TryFrom<CasmContractClass> for ContractClassV1 {
Version::parse(&class.compiler_version)
.unwrap_or_else(|_| panic!("Invalid version: '{}'", class.compiler_version)),
);
Ok(ContractClassV1(Arc::new(ContractClassV1Inner {
Ok(CompiledClassV1(Arc::new(ContractClassV1Inner {
program,
entry_points_by_type,
hints: string_to_hint,
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/contract_class_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use assert_matches::assert_matches;
use cairo_lang_starknet_classes::NestedIntList;
use rstest::rstest;

use crate::execution::contract_class::{ContractClassV1, ContractClassV1Inner};
use crate::execution::contract_class::{CompiledClassV1, ContractClassV1Inner};
use crate::transaction::errors::TransactionExecutionError;

#[rstest]
fn test_get_visited_segments() {
let test_contract = ContractClassV1(Arc::new(ContractClassV1Inner {
let test_contract = CompiledClassV1(Arc::new(ContractClassV1Inner {
program: Default::default(),
entry_points_by_type: Default::default(),
hints: Default::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use starknet_api::hash::StarkHash;

use super::execution_utils::SEGMENT_ARENA_BUILTIN_SIZE;
use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources};
use crate::execution::contract_class::{ContractClassV0, TrackedResource};
use crate::execution::contract_class::{CompiledClassV0, TrackedResource};
use crate::execution::deprecated_syscalls::hint_processor::DeprecatedSyscallHintProcessor;
use crate::execution::entry_point::{
CallEntryPoint,
Expand Down Expand Up @@ -44,7 +44,7 @@ pub const CAIRO0_BUILTINS_NAMES: [BuiltinName; 6] = [
/// Executes a specific call to a contract entry point and returns its output.
pub fn execute_entry_point_call(
call: CallEntryPoint,
contract_class: ContractClassV0,
contract_class: CompiledClassV0,
state: &mut dyn State,
context: &mut EntryPointExecutionContext,
) -> EntryPointExecutionResult<CallInfo> {
Expand All @@ -67,7 +67,7 @@ pub fn execute_entry_point_call(

pub fn initialize_execution_context<'a>(
call: &CallEntryPoint,
contract_class: ContractClassV0,
contract_class: CompiledClassV0,
state: &'a mut dyn State,
context: &'a mut EntryPointExecutionContext,
) -> Result<VmExecutionContext<'a>, PreExecutionError> {
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn initialize_execution_context<'a>(

pub fn resolve_entry_point_pc(
call: &CallEntryPoint,
contract_class: &ContractClassV0,
contract_class: &CompiledClassV0,
) -> Result<usize, PreExecutionError> {
if call.entry_point_type == EntryPointType::Constructor
&& call.entry_point_selector != selector_from_name(CONSTRUCTOR_ENTRY_POINT_NAME)
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use starknet_api::execution_resources::GasAmount;
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources, Retdata};
use crate::execution::contract_class::{ContractClassV1, EntryPointV1, TrackedResource};
use crate::execution::contract_class::{CompiledClassV1, EntryPointV1, TrackedResource};
use crate::execution::entry_point::{
CallEntryPoint,
EntryPointExecutionContext,
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct CallResult {
/// Executes a specific call to a contract entry point and returns its output.
pub fn execute_entry_point_call(
call: CallEntryPoint,
contract_class: ContractClassV1,
contract_class: CompiledClassV1,
state: &mut dyn State,
context: &mut EntryPointExecutionContext,
) -> EntryPointExecutionResult<CallInfo> {
Expand Down Expand Up @@ -142,7 +142,7 @@ fn register_visited_pcs(

pub fn initialize_execution_context<'a>(
call: CallEntryPoint,
contract_class: &'a ContractClassV1,
contract_class: &'a CompiledClassV1,
state: &'a mut dyn State,
context: &'a mut EntryPointExecutionContext,
) -> Result<VmExecutionContext<'a>, PreExecutionError> {
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn initialize_execution_context<'a>(

fn prepare_program_extra_data(
runner: &mut CairoRunner,
contract_class: &ContractClassV1,
contract_class: &CompiledClassV1,
read_only_segments: &mut ReadOnlySegments,
gas_costs: &GasCosts,
) -> Result<usize, PreExecutionError> {
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 @@ -22,7 +22,7 @@ use starknet_api::transaction::fields::Calldata;
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::{RunnableContractClass, TrackedResource};
use crate::execution::contract_class::{RunnableCompiledClass, TrackedResource};
use crate::execution::entry_point::{
execute_constructor_entry_point,
CallEntryPoint,
Expand Down Expand Up @@ -52,7 +52,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: RunnableContractClass,
contract_class: RunnableCompiledClass,
state: &mut dyn State,
context: &mut EntryPointExecutionContext,
remaining_gas: &mut u64,
Expand Down Expand Up @@ -120,24 +120,24 @@ 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: RunnableContractClass,
contract_class: RunnableCompiledClass,
state: &mut dyn State,
context: &mut EntryPointExecutionContext,
) -> EntryPointExecutionResult<CallInfo> {
match contract_class {
RunnableContractClass::V0(contract_class) => {
RunnableCompiledClass::V0(contract_class) => {
deprecated_entry_point_execution::execute_entry_point_call(
call,
contract_class,
state,
context,
)
}
RunnableContractClass::V1(contract_class) => {
RunnableCompiledClass::V1(contract_class) => {
entry_point_execution::execute_entry_point_call(call, contract_class, state, context)
}
#[cfg(feature = "cairo_native")]
RunnableContractClass::V1Native(contract_class) => {
RunnableCompiledClass::V1Native(contract_class) => {
if context.tracked_resource_stack.last() == Some(&TrackedResource::CairoSteps) {
// We cannot run native with cairo steps as the tracked resources (it's a vm
// resouorce).
Expand Down
30 changes: 15 additions & 15 deletions crates/blockifier/src/execution/native/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ use std::sync::Arc;
use cairo_native::executor::AotContractExecutor;
use starknet_api::core::EntryPointSelector;

use crate::execution::contract_class::{ContractClassV1, EntryPointV1};
use crate::execution::contract_class::{CompiledClassV1, EntryPointV1};
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::errors::PreExecutionError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NativeContractClassV1(pub Arc<NativeContractClassV1Inner>);
impl Deref for NativeContractClassV1 {
type Target = NativeContractClassV1Inner;
pub struct NativeCompiledClassV1(pub Arc<NativeCompiledClassV1Inner>);
impl Deref for NativeCompiledClassV1 {
type Target = NativeCompiledClassV1Inner;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl NativeContractClassV1 {
impl NativeCompiledClassV1 {
pub(crate) fn constructor_selector(&self) -> Option<EntryPointSelector> {
self.casm.constructor_selector()
}
Expand All @@ -26,8 +26,8 @@ impl NativeContractClassV1 {
///
/// executor must be derived from sierra_program which in turn must be derived from
/// sierra_contract_class.
pub fn new(executor: AotContractExecutor, casm: ContractClassV1) -> NativeContractClassV1 {
let contract = NativeContractClassV1Inner::new(executor, casm);
pub fn new(executor: AotContractExecutor, casm: CompiledClassV1) -> NativeCompiledClassV1 {
let contract = NativeCompiledClassV1Inner::new(executor, casm);

Self(Arc::new(contract))
}
Expand All @@ -39,29 +39,29 @@ impl NativeContractClassV1 {
self.casm.get_entry_point(call)
}

pub fn casm(&self) -> ContractClassV1 {
pub fn casm(&self) -> CompiledClassV1 {
self.casm.clone()
}
}

#[derive(Debug)]
pub struct NativeContractClassV1Inner {
pub struct NativeCompiledClassV1Inner {
pub executor: AotContractExecutor,
casm: ContractClassV1,
casm: CompiledClassV1,
}

impl NativeContractClassV1Inner {
fn new(executor: AotContractExecutor, casm: ContractClassV1) -> Self {
NativeContractClassV1Inner { executor, casm }
impl NativeCompiledClassV1Inner {
fn new(executor: AotContractExecutor, casm: CompiledClassV1) -> Self {
NativeCompiledClassV1Inner { executor, casm }
}
}

// The location where the compiled contract is loaded into memory will not
// be the same therefore we exclude it from the comparison.
impl PartialEq for NativeContractClassV1Inner {
impl PartialEq for NativeCompiledClassV1Inner {
fn eq(&self, other: &Self) -> bool {
self.casm == other.casm
}
}

impl Eq for NativeContractClassV1Inner {}
impl Eq for NativeCompiledClassV1Inner {}
Loading

0 comments on commit 5a75240

Please sign in to comment.