Skip to content

Commit

Permalink
refactor(blockifier): rename ContractClass to CompiledClass
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Nov 27, 2024
1 parent f8826d7 commit 9133792
Showing 44 changed files with 229 additions and 282 deletions.
2 changes: 1 addition & 1 deletion crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
@@ -161,7 +161,7 @@ impl<S: StateReader> TransactionExecutor<S> {
.block_state
.as_ref()
.expect(BLOCK_STATE_ACCESS_ERR)
.get_compiled_contract_class(*class_hash)?;
.get_compiled_class(*class_hash)?;
Ok((*class_hash, contract_class.get_visited_segments(class_visited_pcs)?))
})
.collect::<TransactionExecutorResult<_>>()?;
2 changes: 1 addition & 1 deletion crates/blockifier/src/bouncer.rs
Original file line number Diff line number Diff line change
@@ -551,7 +551,7 @@ pub fn get_casm_hash_calculation_resources<S: StateReader>(
let mut casm_hash_computation_resources = ExecutionResources::default();

for class_hash in executed_class_hashes {
let class = state_reader.get_compiled_contract_class(*class_hash)?;
let class = state_reader.get_compiled_class(*class_hash)?;
casm_hash_computation_resources += &class.estimate_casm_hash_computation_resources();
}

11 changes: 4 additions & 7 deletions crates/blockifier/src/concurrency/versioned_state.rs
Original file line number Diff line number Diff line change
@@ -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};
@@ -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> {
@@ -336,14 +336,11 @@ impl<S: StateReader> StateReader for VersionedStateProxy<S> {
}
}

fn get_compiled_contract_class(
&self,
class_hash: ClassHash,
) -> StateResult<RunnableContractClass> {
fn get_compiled_class(&self, class_hash: ClassHash) -> StateResult<RunnableCompiledClass> {
let mut state = self.state();
match state.compiled_contract_classes.read(self.tx_index, class_hash) {
Some(value) => Ok(value),
None => match state.initial_state.get_compiled_contract_class(class_hash) {
None => match state.initial_state.get_compiled_class(class_hash) {
Ok(initial_value) => {
state.declared_contracts.set_initial_value(class_hash, true);
state
20 changes: 6 additions & 14 deletions crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
@@ -97,12 +97,9 @@ fn test_versioned_state_proxy() {
versioned_state_proxys[5].get_compiled_class_hash(class_hash).unwrap(),
compiled_class_hash
);
assert_eq!(
versioned_state_proxys[7].get_compiled_contract_class(class_hash).unwrap(),
contract_class
);
assert_eq!(versioned_state_proxys[7].get_compiled_class(class_hash).unwrap(), contract_class);
assert_matches!(
versioned_state_proxys[7].get_compiled_contract_class(another_class_hash).unwrap_err(),
versioned_state_proxys[7].get_compiled_class(another_class_hash).unwrap_err(),
StateError::UndeclaredClassHash(class_hash) if
another_class_hash == class_hash
);
@@ -197,7 +194,7 @@ fn test_versioned_state_proxy() {
compiled_class_hash_v18
);
assert_eq!(
versioned_state_proxys[15].get_compiled_contract_class(class_hash).unwrap(),
versioned_state_proxys[15].get_compiled_class(class_hash).unwrap(),
contract_class_v11
);
}
@@ -321,7 +318,7 @@ fn test_validate_reads(

assert!(transactional_state.cache.borrow().initial_reads.declared_contracts.is_empty());
assert_matches!(
transactional_state.get_compiled_contract_class(class_hash),
transactional_state.get_compiled_class(class_hash),
Err(StateError::UndeclaredClassHash(err_class_hash)) if
err_class_hash == class_hash
);
@@ -440,10 +437,7 @@ fn test_apply_writes(
&HashMap::default(),
);
assert!(transactional_states[1].get_class_hash_at(contract_address).unwrap() == class_hash_0);
assert!(
transactional_states[1].get_compiled_contract_class(class_hash).unwrap()
== contract_class_0
);
assert!(transactional_states[1].get_compiled_class(class_hash).unwrap() == contract_class_0);
}

#[rstest]
@@ -659,7 +653,5 @@ fn test_versioned_proxy_state_flow(
.commit_chunk_and_recover_block_state(4, HashMap::new());

assert!(modified_block_state.get_class_hash_at(contract_address).unwrap() == class_hash_3);
assert!(
modified_block_state.get_compiled_contract_class(class_hash).unwrap() == contract_class_2
);
assert!(modified_block_state.get_compiled_class(class_hash).unwrap() == contract_class_2);
}
53 changes: 27 additions & 26 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
@@ -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;

@@ -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> {
@@ -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(),
@@ -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)
}
@@ -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,
})))
@@ -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)
}
@@ -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)
}
@@ -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> {
@@ -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,
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/contract_class_test.rs
Original file line number Diff line number Diff line change
@@ -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(),
Original file line number Diff line number Diff line change
@@ -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,
@@ -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> {
@@ -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> {
@@ -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)
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/deprecated_syscalls/mod.rs
Original file line number Diff line number Diff line change
@@ -653,7 +653,7 @@ pub fn replace_class(
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<ReplaceClassResponse> {
// Ensure the class is declared (by reading it).
syscall_handler.state.get_compiled_contract_class(request.class_hash)?;
syscall_handler.state.get_compiled_class(request.class_hash)?;
syscall_handler.state.set_class_hash_at(syscall_handler.storage_address, request.class_hash)?;

Ok(ReplaceClassResponse {})
9 changes: 4 additions & 5 deletions crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
@@ -147,7 +147,7 @@ impl CallEntryPoint {
}
// Add class hash to the call, that will appear in the output (call info).
self.class_hash = Some(class_hash);
let contract_class = state.get_compiled_contract_class(class_hash)?;
let contract_class = state.get_compiled_class(class_hash)?;

context.revert_infos.0.push(EntryPointRevertInfo::new(
self.storage_address,
@@ -406,10 +406,9 @@ pub fn execute_constructor_entry_point(
remaining_gas: &mut u64,
) -> ConstructorEntryPointExecutionResult<CallInfo> {
// Ensure the class is declared (by reading it).
let contract_class =
state.get_compiled_contract_class(ctor_context.class_hash).map_err(|error| {
ConstructorEntryPointExecutionError::new(error.into(), &ctor_context, None)
})?;
let contract_class = state.get_compiled_class(ctor_context.class_hash).map_err(|error| {
ConstructorEntryPointExecutionError::new(error.into(), &ctor_context, None)
})?;
let Some(constructor_selector) = contract_class.constructor_selector() else {
// Contract has no constructor.
return handle_empty_constructor(&ctor_context, calldata, *remaining_gas)
8 changes: 4 additions & 4 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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> {
@@ -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> {
@@ -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> {
Loading

0 comments on commit 9133792

Please sign in to comment.