From d0f94672587bcf7b698654ee3f979dfc22e9c912 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Thu, 14 Nov 2024 22:06:22 +0000 Subject: [PATCH] refactor(blockifier): remove entry point logic from native runnable class --- .../src/execution/native/contract_class.rs | 89 ++----------------- .../blockifier/src/test_utils/struct_impls.rs | 11 +-- 2 files changed, 11 insertions(+), 89 deletions(-) diff --git a/crates/blockifier/src/execution/native/contract_class.rs b/crates/blockifier/src/execution/native/contract_class.rs index b4ec966e9df..6f83c892cdf 100644 --- a/crates/blockifier/src/execution/native/contract_class.rs +++ b/crates/blockifier/src/execution/native/contract_class.rs @@ -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); impl Deref for NativeContractClassV1 { @@ -25,28 +17,19 @@ impl Deref for NativeContractClassV1 { impl NativeContractClassV1 { pub(crate) fn constructor_selector(&self) -> Option { - 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 { - self.entry_points_by_type.get_entry_point(call).map(|ep| ep.function_id) - } - pub fn casm(&self) -> ContractClassV1 { self.casm.clone() } @@ -55,21 +38,12 @@ impl NativeContractClassV1 { #[derive(Debug)] pub struct NativeContractClassV1Inner { pub executor: AotContractExecutor, - entry_points_by_type: EntryPointsByType, 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 } } } @@ -77,57 +51,8 @@ impl NativeContractClassV1Inner { // 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 { - 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::>(); - - let entry_points_by_type = &sierra_contract_class.entry_points_by_type; - - EntryPointsByType:: { - 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 { - 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 - } -} diff --git a/crates/blockifier/src/test_utils/struct_impls.rs b/crates/blockifier/src/test_utils/struct_impls.rs index 3a93a00d565..5e744b5c9d8 100644 --- a/crates/blockifier/src/test_utils/struct_impls.rs +++ b/crates/blockifier/src/test_utils/struct_impls.rs @@ -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 {