Skip to content

Commit

Permalink
refactor(blockifier): remove entry point logic from native runnable c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
rodrigo-pino committed Nov 14, 2024
1 parent 58dd5c3 commit d0f9467
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 89 deletions.
89 changes: 7 additions & 82 deletions crates/blockifier/src/execution/native/contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
use std::ops::Deref;
use std::sync::Arc;

use cairo_lang_sierra::ids::FunctionId;
use cairo_lang_starknet_classes::contract_class::{
ContractClass as SierraContractClass,
ContractEntryPoint as SierraContractEntryPoint,
};
use cairo_native::executor::AotContractExecutor;
use starknet_api::core::EntryPointSelector;

use crate::execution::contract_class::{ContractClassV1, EntryPointsByType, HasSelector};
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::errors::PreExecutionError;
use crate::execution::native::utils::contract_entrypoint_to_entrypoint_selector;
use crate::execution::contract_class::ContractClassV1;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NativeContractClassV1(pub Arc<NativeContractClassV1Inner>);
impl Deref for NativeContractClassV1 {
Expand All @@ -25,28 +17,19 @@ impl Deref for NativeContractClassV1 {

impl NativeContractClassV1 {
pub(crate) fn constructor_selector(&self) -> Option<EntryPointSelector> {
self.entry_points_by_type.constructor.first().map(|ep| ep.selector)
self.casm.entry_points_by_type.constructor.first().map(|ep| ep.selector)
}

/// Initialize a compiled contract class for native.
///
/// executor must be derived from sierra_program which in turn must be derived from
/// sierra_contract_class.
pub fn new(
executor: AotContractExecutor,
sierra_contract_class: SierraContractClass,
casm: ContractClassV1,
) -> NativeContractClassV1 {
let contract = NativeContractClassV1Inner::new(executor, sierra_contract_class, casm);
pub fn new(executor: AotContractExecutor, casm: ContractClassV1) -> NativeContractClassV1 {
let contract = NativeContractClassV1Inner::new(executor, casm);

Self(Arc::new(contract))
}

/// Returns an entry point into the natively compiled contract.
pub fn get_entry_point(&self, call: &CallEntryPoint) -> Result<FunctionId, PreExecutionError> {
self.entry_points_by_type.get_entry_point(call).map(|ep| ep.function_id)
}

pub fn casm(&self) -> ContractClassV1 {
self.casm.clone()
}
Expand All @@ -55,79 +38,21 @@ impl NativeContractClassV1 {
#[derive(Debug)]
pub struct NativeContractClassV1Inner {
pub executor: AotContractExecutor,
entry_points_by_type: EntryPointsByType<NativeEntryPoint>,
casm: ContractClassV1,
}

impl NativeContractClassV1Inner {
fn new(
executor: AotContractExecutor,
sierra_contract_class: SierraContractClass,
casm: ContractClassV1,
) -> Self {
NativeContractClassV1Inner {
executor,
entry_points_by_type: EntryPointsByType::from(&sierra_contract_class),
casm,
}
fn new(executor: AotContractExecutor, casm: ContractClassV1) -> Self {
NativeContractClassV1Inner { 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 {
fn eq(&self, other: &Self) -> bool {
self.entry_points_by_type == other.entry_points_by_type && self.casm == other.casm
self.casm == other.casm
}
}

impl Eq for NativeContractClassV1Inner {}

impl From<&SierraContractClass> for EntryPointsByType<NativeEntryPoint> {
fn from(sierra_contract_class: &SierraContractClass) -> Self {
let program =
sierra_contract_class.extract_sierra_program().expect("Can't get sierra program.");

let func_ids = program.funcs.iter().map(|func| &func.id).collect::<Vec<&FunctionId>>();

let entry_points_by_type = &sierra_contract_class.entry_points_by_type;

EntryPointsByType::<NativeEntryPoint> {
constructor: sierra_eps_to_native_eps(&func_ids, &entry_points_by_type.constructor),
external: sierra_eps_to_native_eps(&func_ids, &entry_points_by_type.external),
l1_handler: sierra_eps_to_native_eps(&func_ids, &entry_points_by_type.l1_handler),
}
}
}

fn sierra_eps_to_native_eps(
func_ids: &[&FunctionId],
sierra_eps: &[SierraContractEntryPoint],
) -> Vec<NativeEntryPoint> {
sierra_eps.iter().map(|sierra_ep| NativeEntryPoint::from(func_ids, sierra_ep)).collect()
}

#[derive(Clone, Debug, PartialEq)]
/// Provides a relation between a function in a contract and a compiled contract.
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.
function_id: FunctionId,
}

impl NativeEntryPoint {
fn from(func_ids: &[&FunctionId], sierra_ep: &SierraContractEntryPoint) -> NativeEntryPoint {
let &function_id = func_ids.get(sierra_ep.function_idx).expect("Can't find function id.");
NativeEntryPoint {
selector: contract_entrypoint_to_entrypoint_selector(sierra_ep),
function_id: function_id.clone(),
}
}
}

impl HasSelector for NativeEntryPoint {
fn selector(&self) -> &EntryPointSelector {
&self.selector
}
}
11 changes: 4 additions & 7 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,13 @@ impl NativeContractClassV1 {
.expect("Cannot compile sierra into native");

// Compile the sierra contract class into casm
let casm_contract_class = CasmContractClass::from_contract_class(
sierra_contract_class.clone(),
false,
usize::MAX,
)
.expect("Cannot compile sierra contract class into casm contract class");
let casm_contract_class =
CasmContractClass::from_contract_class(sierra_contract_class, false, usize::MAX)
.expect("Cannot compile sierra contract class into casm contract class");
let casm = ContractClassV1::try_from(casm_contract_class)
.expect("Cannot get ContractClassV1 from CasmContractClass");

NativeContractClassV1::new(executor, sierra_contract_class, casm)
NativeContractClassV1::new(executor, casm)
}

pub fn from_file(contract_path: &str) -> Self {
Expand Down

0 comments on commit d0f9467

Please sign in to comment.