Skip to content

Commit

Permalink
chore(blockifier): unify get entry point for V1 and Native
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Sep 25, 2024
1 parent f922076 commit f2ff7f2
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 55 deletions.
76 changes: 41 additions & 35 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ use serde::{Deserialize, Deserializer, Serialize};
use starknet_api::core::EntryPointSelector;
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass,
EntryPoint,
EntryPointOffset,
EntryPointType,
EntryPointV0,
Program as DeprecatedProgram,
};
use starknet_types_core::felt::Felt;

use crate::abi::constants::{self};
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::entry_point_execution::get_entry_point;
use crate::execution::errors::{ContractClassError, PreExecutionError};
use crate::execution::execution_utils::{poseidon_hash_many_cost, sn_api_to_cairo_vm_program};
use crate::execution::native::utils::contract_entrypoint_to_entrypoint_selector;
Expand All @@ -61,6 +62,21 @@ pub enum TrackingResource {
SierraGas, // AKA Sierra mode.
}

#[derive(Clone)]
pub enum EntryPoint {
V1(EntryPointV1),
Native(NativeEntryPoint),
}

impl EntryPoint {
pub fn selector(&self) -> &EntryPointSelector {
match self {
EntryPoint::V1(ep) => &ep.selector,
EntryPoint::Native(ep) => &ep.selector,
}
}
}

/// Represents a runnable Starknet contract class (meaning, the program is runnable by the VM).
#[derive(Clone, Debug, PartialEq, derive_more::From)]
pub enum ContractClass {
Expand Down Expand Up @@ -99,6 +115,20 @@ impl ContractClass {
}
}

pub fn entry_points_of_same_type(&self, entry_point_type: EntryPointType) -> Vec<EntryPoint> {
match self {
ContractClass::V0(_) => panic!("V0 contracts do not support entry points."),
ContractClass::V1(class) => class.entry_points_by_type[&entry_point_type]
.iter()
.map(|ep| EntryPoint::V1(ep.clone()))
.collect(),
ContractClass::V1Native(class) => class.entry_points_by_type[entry_point_type]
.iter()
.map(|ep| EntryPoint::Native(ep.clone()))
.collect(),
}
}

pub fn get_visited_segments(
&self,
visited_pcs: &HashSet<usize>,
Expand Down Expand Up @@ -199,7 +229,7 @@ impl ContractClassV0 {
pub struct ContractClassV0Inner {
#[serde(deserialize_with = "deserialize_program")]
pub program: Program,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPointV0>>,
}

impl TryFrom<DeprecatedContractClass> for ContractClassV0 {
Expand Down Expand Up @@ -245,21 +275,9 @@ impl ContractClassV1 {
&self,
call: &CallEntryPoint,
) -> Result<EntryPointV1, PreExecutionError> {
call.verify_constructor()?;

let entry_points_of_same_type = &self.0.entry_points_by_type[&call.entry_point_type];
let filtered_entry_points: Vec<_> = entry_points_of_same_type
.iter()
.filter(|ep| ep.selector == call.entry_point_selector)
.collect();

match &filtered_entry_points[..] {
[] => Err(PreExecutionError::EntryPointNotFound(call.entry_point_selector)),
[entry_point] => Ok((*entry_point).clone()),
_ => Err(PreExecutionError::DuplicatedEntryPointSelector {
selector: call.entry_point_selector,
typ: call.entry_point_type,
}),
match get_entry_point(&ContractClass::V1(self.clone()), call)? {
EntryPoint::V1(entry_point) => Ok(entry_point),
EntryPoint::Native(_) => panic!("Unexpected entry point type."),
}
}

Expand Down Expand Up @@ -638,22 +656,10 @@ impl NativeContractClassV1 {
}

/// Returns an entry point into the natively compiled contract.
pub fn get_entry_point(&self, call: &CallEntryPoint) -> Result<&FunctionId, PreExecutionError> {
call.verify_constructor()?;

let entry_points_of_same_type = &self.0.entry_points_by_type[call.entry_point_type];
let filtered_entry_points: Vec<_> = entry_points_of_same_type
.iter()
.filter(|ep| ep.selector == call.entry_point_selector)
.collect();

match &filtered_entry_points[..] {
[] => Err(PreExecutionError::EntryPointNotFound(call.entry_point_selector)),
[entry_point] => Ok(&entry_point.function_id),
_ => Err(PreExecutionError::DuplicatedEntryPointSelector {
selector: call.entry_point_selector,
typ: call.entry_point_type,
}),
pub fn get_entry_point(&self, call: &CallEntryPoint) -> Result<FunctionId, PreExecutionError> {
match get_entry_point(&ContractClass::V1Native(self.clone()), call)? {
EntryPoint::Native(entry_point) => Ok(entry_point.function_id),
EntryPoint::V1(_) => panic!("Unexpected entry point type."),
}
}
}
Expand Down Expand Up @@ -730,9 +736,9 @@ fn sierra_eps_to_native_eps(
sierra_eps.iter().map(|sierra_ep| NativeEntryPoint::from(func_ids, sierra_ep)).collect()
}

#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
/// Provides a relation between a function in a contract and a compiled contract.
struct NativeEntryPoint {
pub struct NativeEntryPoint {
/// The selector is the key to find the function in the contract.
selector: EntryPointSelector,
/// And the function_id is the key to find the function in the compiled contract.
Expand Down
25 changes: 24 additions & 1 deletion 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::felt;
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::{ContractClassV1, EntryPointV1};
use crate::execution::contract_class::{ContractClass, ContractClassV1, EntryPoint, EntryPointV1};
use crate::execution::entry_point::{
CallEntryPoint,
EntryPointExecutionContext,
Expand Down Expand Up @@ -145,6 +145,29 @@ fn register_visited_pcs(
Ok(())
}

pub fn get_entry_point(
contract_class: &ContractClass,
call: &CallEntryPoint,
) -> Result<EntryPoint, PreExecutionError> {
call.verify_constructor()?;

let entry_points_of_same_type =
&contract_class.entry_points_of_same_type(call.entry_point_type);
let filtered_entry_points: Vec<_> = entry_points_of_same_type
.iter()
.filter(|ep| *ep.selector() == call.entry_point_selector)
.collect();

match &filtered_entry_points[..] {
[] => Err(PreExecutionError::EntryPointNotFound(call.entry_point_selector)),
[entry_point] => Ok((**entry_point).clone()),
_ => Err(PreExecutionError::DuplicatedEntryPointSelector {
selector: call.entry_point_selector,
typ: call.entry_point_type,
}),
}
}

pub fn initialize_execution_context<'a>(
call: CallEntryPoint,
contract_class: &'a ContractClassV1,
Expand Down
8 changes: 4 additions & 4 deletions crates/papyrus_protobuf/src/converters/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl From<state::ContractClass> for protobuf::Cairo1Class {
}
}

impl TryFrom<protobuf::EntryPoint> for deprecated_contract_class::EntryPoint {
impl TryFrom<protobuf::EntryPoint> for deprecated_contract_class::EntryPointV0 {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::EntryPoint) -> Result<Self, Self::Error> {
let selector_felt =
Expand All @@ -281,12 +281,12 @@ impl TryFrom<protobuf::EntryPoint> for deprecated_contract_class::EntryPoint {
value.offset.try_into().expect("Failed converting u64 to usize"),
);

Ok(deprecated_contract_class::EntryPoint { selector, offset })
Ok(deprecated_contract_class::EntryPointV0 { selector, offset })
}
}

impl From<deprecated_contract_class::EntryPoint> for protobuf::EntryPoint {
fn from(value: deprecated_contract_class::EntryPoint) -> Self {
impl From<deprecated_contract_class::EntryPointV0> for protobuf::EntryPoint {
fn from(value: deprecated_contract_class::EntryPointV0) -> Self {
protobuf::EntryPoint {
selector: Some(value.selector.0.into()),
offset: u64::try_from(value.offset.0).expect("Failed converting usize to u64"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use starknet_api::core::{CompiledClassHash, ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::deprecated_contract_class::{
ContractClassAbiEntry as DeprecatedContractClassAbiEntry,
EntryPoint as DeprecatedEntryPoint,
EntryPointType as DeprecatedEntryPointType,
EntryPointV0 as DeprecatedEntryPoint,
EventAbiEntry,
FunctionAbiEntry,
StructAbiEntry,
Expand Down
8 changes: 6 additions & 2 deletions crates/papyrus_rpc/src/v0_6/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use std::collections::HashMap;

use papyrus_storage::db::serialization::StorageSerdeError;
use serde::{Deserialize, Serialize};
use starknet_api::deprecated_contract_class::{ContractClassAbiEntry, EntryPoint, EntryPointType};
use starknet_api::deprecated_contract_class::{
ContractClassAbiEntry,
EntryPointType,
EntryPointV0,
};

use crate::compression_utils::compress_and_encode;

Expand All @@ -12,7 +16,7 @@ pub struct ContractClass {
/// A base64 encoding of the gzip-compressed JSON representation of program.
pub program: String,
/// The selector of each entry point is a unique identifier in the program.
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPointV0>>,
}

impl TryFrom<starknet_api::deprecated_contract_class::ContractClass> for ContractClass {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use starknet_api::core::{CompiledClassHash, ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::deprecated_contract_class::{
ContractClassAbiEntry as DeprecatedContractClassAbiEntry,
EntryPoint as DeprecatedEntryPoint,
EntryPointType as DeprecatedEntryPointType,
EntryPointV0 as DeprecatedEntryPoint,
EventAbiEntry,
FunctionAbiEntry,
StructAbiEntry,
Expand Down
8 changes: 6 additions & 2 deletions crates/papyrus_rpc/src/v0_7/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use std::collections::HashMap;

use papyrus_storage::db::serialization::StorageSerdeError;
use serde::{Deserialize, Serialize};
use starknet_api::deprecated_contract_class::{ContractClassAbiEntry, EntryPoint, EntryPointType};
use starknet_api::deprecated_contract_class::{
ContractClassAbiEntry,
EntryPointType,
EntryPointV0,
};

use crate::compression_utils::compress_and_encode;

Expand All @@ -12,7 +16,7 @@ pub struct ContractClass {
/// A base64 encoding of the gzip-compressed JSON representation of program.
pub program: String,
/// The selector of each entry point is a unique identifier in the program.
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPointV0>>,
}

impl TryFrom<starknet_api::deprecated_contract_class::ContractClass> for ContractClass {
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ use starknet_api::deprecated_contract_class::{
ConstructorType,
ContractClass as DeprecatedContractClass,
ContractClassAbiEntry,
EntryPoint as DeprecatedEntryPoint,
EntryPointOffset,
EntryPointType as DeprecatedEntryPointType,
EntryPointV0 as DeprecatedEntryPoint,
EventAbiEntry,
EventType,
FunctionAbiEntry,
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ use starknet_api::deprecated_contract_class::{
ConstructorType,
ContractClass as DeprecatedContractClass,
ContractClassAbiEntry,
EntryPoint as DeprecatedEntryPoint,
EntryPointOffset,
EntryPointType as DeprecatedEntryPointType,
EntryPointV0 as DeprecatedEntryPoint,
EventAbiEntry,
EventType,
FunctionAbiEntry,
Expand Down
8 changes: 4 additions & 4 deletions crates/starknet_api/src/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct ContractClass {
/// The selector of each entry point is a unique identifier in the program.
// TODO: Consider changing to IndexMap, since this is used for computing the
// class hash.
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPointV0>>,
}

/// A [ContractClass](`crate::deprecated_contract_class::ContractClass`) abi entry.
Expand Down Expand Up @@ -184,16 +184,16 @@ pub enum EntryPointType {

/// An entry point of a [ContractClass](`crate::deprecated_contract_class::ContractClass`).
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
pub struct EntryPoint {
pub struct EntryPointV0 {
pub selector: EntryPointSelector,
pub offset: EntryPointOffset,
}

impl TryFrom<CasmContractEntryPoint> for EntryPoint {
impl TryFrom<CasmContractEntryPoint> for EntryPointV0 {
type Error = StarknetApiError;

fn try_from(value: CasmContractEntryPoint) -> Result<Self, Self::Error> {
Ok(EntryPoint {
Ok(EntryPointV0 {
selector: EntryPointSelector(StarkHash::from(value.selector)),
offset: EntryPointOffset(value.offset),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use starknet_api::deprecated_contract_class::{
ConstructorType,
ContractClass as DeprecatedContractClass,
ContractClassAbiEntry,
EntryPoint as DeprecatedEntryPoint,
EntryPointOffset,
EntryPointType as DeprecatedEntryPointType,
EntryPointV0 as DeprecatedEntryPoint,
FunctionAbiEntry,
Program,
TypedParameter,
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_client/src/writer/objects/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use papyrus_test_utils::{auto_impl_get_test_instance, get_number_of_variants, Ge
use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::deprecated_contract_class::{
ContractClassAbiEntry as DeprecatedContractClassAbiEntry,
EntryPoint as DeprecatedEntryPoint,
EntryPointType as DeprecatedEntryPointType,
EntryPointV0 as DeprecatedEntryPoint,
};
use starknet_api::transaction::TransactionHash;

Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_client/src/writer/objects/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use starknet_api::core::{
};
use starknet_api::deprecated_contract_class::{
ContractClassAbiEntry as DeprecatedContractClassAbiEntry,
EntryPoint as DeprecatedEntryPoint,
EntryPointType as DeprecatedEntryPointType,
EntryPointV0 as DeprecatedEntryPoint,
};
use starknet_api::state::{EntryPoint, EntryPointType};
use starknet_api::transaction::{
Expand Down

0 comments on commit f2ff7f2

Please sign in to comment.