From 0d94643edf8ddc421b603b6ebc196c90381ce7eb Mon Sep 17 00:00:00 2001 From: liorgold2 <38202661+liorgold2@users.noreply.github.com> Date: Tue, 28 Jan 2025 17:55:12 +0200 Subject: [PATCH] feat(blockifier): add ExecutableCallEntryPoint (#3657) commit-id:7ef0c9b2 --- .../deprecated_entry_point_execution.rs | 16 +++--- .../blockifier/src/execution/entry_point.rs | 53 +++++++++++++++++-- .../src/execution/execution_utils.rs | 19 ++++--- scripts/rust_fmt.sh | 0 4 files changed, 68 insertions(+), 20 deletions(-) mode change 100644 => 100755 scripts/rust_fmt.sh diff --git a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs index d836e4cf0f..eb9704cbef 100644 --- a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs +++ b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs @@ -17,9 +17,9 @@ use crate::execution::call_info::{CallExecution, CallInfo}; use crate::execution::contract_class::{CompiledClassV0, TrackedResource}; use crate::execution::deprecated_syscalls::hint_processor::DeprecatedSyscallHintProcessor; use crate::execution::entry_point::{ - CallEntryPoint, EntryPointExecutionContext, EntryPointExecutionResult, + ExecutableCallEntryPoint, }; use crate::execution::errors::{PostExecutionError, PreExecutionError}; use crate::execution::execution_utils::{read_execution_retdata, Args, ReadOnlySegments}; @@ -43,7 +43,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, + call: ExecutableCallEntryPoint, compiled_class: CompiledClassV0, state: &mut dyn State, context: &mut EntryPointExecutionContext, @@ -66,7 +66,7 @@ pub fn execute_entry_point_call( } pub fn initialize_execution_context<'a>( - call: &CallEntryPoint, + call: &ExecutableCallEntryPoint, compiled_class: CompiledClassV0, state: &'a mut dyn State, context: &'a mut EntryPointExecutionContext, @@ -103,14 +103,14 @@ pub fn initialize_execution_context<'a>( initial_syscall_ptr, call.storage_address, call.caller_address, - call.class_hash.expect("Class hash must be resolved prior to call execution."), + call.class_hash, ); Ok(VmExecutionContext { runner, syscall_handler, initial_syscall_ptr, entry_point_pc }) } pub fn resolve_entry_point_pc( - call: &CallEntryPoint, + call: &ExecutableCallEntryPoint, compiled_class: &CompiledClassV0, ) -> Result { if call.entry_point_type == EntryPointType::Constructor @@ -158,7 +158,7 @@ pub fn resolve_entry_point_pc( } pub fn prepare_call_arguments( - call: &CallEntryPoint, + call: &ExecutableCallEntryPoint, runner: &mut CairoRunner, initial_syscall_ptr: Relocatable, read_only_segments: &mut ReadOnlySegments, @@ -219,7 +219,7 @@ pub fn run_entry_point( pub fn finalize_execution( mut runner: CairoRunner, syscall_handler: DeprecatedSyscallHintProcessor<'_>, - call: CallEntryPoint, + call: ExecutableCallEntryPoint, implicit_args: Vec, n_total_args: usize, ) -> Result { @@ -259,7 +259,7 @@ pub fn finalize_execution( + &CallInfo::summarize_vm_resources(syscall_handler.inner_calls.iter()); Ok(CallInfo { - call, + call: call.into(), execution: CallExecution { retdata: read_execution_retdata(&runner, retdata_size, &retdata_ptr)?, events: syscall_handler.events, diff --git a/crates/blockifier/src/execution/entry_point.rs b/crates/blockifier/src/execution/entry_point.rs index 0a2b09791e..f6568be3c4 100644 --- a/crates/blockifier/src/execution/entry_point.rs +++ b/crates/blockifier/src/execution/entry_point.rs @@ -96,10 +96,14 @@ pub enum CallType { /// Represents a call to an entry point of a Starknet contract. #[cfg_attr(feature = "transaction_serde", derive(serde::Deserialize))] #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)] -pub struct CallEntryPoint { - // The class hash is not given if it can be deduced from the storage address. - // It is resolved prior to entry point's execution. - pub class_hash: Option, +pub struct CallEntryPointVariant { + /// The class hash of the entry point. + /// The type is `ClassHash` in the case of [ExecutableCallEntryPoint] and `Option` + /// in the case of [CallEntryPoint]. + /// + /// The class hash is not given if it can be deduced from the storage address. + /// It is resolved prior to entry point's execution. + pub class_hash: TClassHash, // Optional, since there is no address to the code implementation in a library call. // and for outermost calls (triggered by the transaction itself). // TODO(AlonH): BACKWARD-COMPATIBILITY. @@ -114,6 +118,25 @@ pub struct CallEntryPoint { pub initial_gas: u64, } +pub type CallEntryPoint = CallEntryPointVariant>; +pub type ExecutableCallEntryPoint = CallEntryPointVariant; + +impl From for CallEntryPoint { + fn from(call: ExecutableCallEntryPoint) -> Self { + Self { + class_hash: Some(call.class_hash), + code_address: call.code_address, + entry_point_type: call.entry_point_type, + entry_point_selector: call.entry_point_selector, + calldata: call.calldata, + storage_address: call.storage_address, + caller_address: call.caller_address, + call_type: call.call_type, + initial_gas: call.initial_gas, + } + } +} + impl CallEntryPoint { pub fn execute( mut self, @@ -159,7 +182,13 @@ impl CallEntryPoint { )); // This is the last operation of this function. - execute_entry_point_call_wrapper(self, compiled_class, state, context, remaining_gas) + execute_entry_point_call_wrapper( + self.into_executable(class_hash), + compiled_class, + state, + context, + remaining_gas, + ) } /// Similar to `execute`, but returns an error if the outer call is reverted. @@ -200,6 +229,20 @@ impl CallEntryPoint { Ok(()) } } + + fn into_executable(self, class_hash: ClassHash) -> ExecutableCallEntryPoint { + ExecutableCallEntryPoint { + class_hash, + code_address: self.code_address, + entry_point_type: self.entry_point_type, + entry_point_selector: self.entry_point_selector, + calldata: self.calldata, + storage_address: self.storage_address, + caller_address: self.caller_address, + call_type: self.call_type, + initial_gas: self.initial_gas, + } + } } pub struct ConstructorContext { diff --git a/crates/blockifier/src/execution/execution_utils.rs b/crates/blockifier/src/execution/execution_utils.rs index c04054ba46..f3f9a58772 100644 --- a/crates/blockifier/src/execution/execution_utils.rs +++ b/crates/blockifier/src/execution/execution_utils.rs @@ -25,11 +25,11 @@ use crate::execution::call_info::{CallExecution, CallInfo, Retdata}; use crate::execution::contract_class::{RunnableCompiledClass, TrackedResource}; use crate::execution::entry_point::{ execute_constructor_entry_point, - CallEntryPoint, ConstructorContext, ConstructorEntryPointExecutionResult, EntryPointExecutionContext, EntryPointExecutionResult, + ExecutableCallEntryPoint, }; use crate::execution::errors::{ ConstructorEntryPointExecutionError, @@ -51,7 +51,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, + mut call: ExecutableCallEntryPoint, compiled_class: RunnableCompiledClass, state: &mut dyn State, context: &mut EntryPointExecutionContext, @@ -93,7 +93,7 @@ pub fn execute_entry_point_call_wrapper( _ => return Err(err.into()), }; Ok(CallInfo { - call: orig_call, + call: orig_call.into(), execution: CallExecution { retdata: Retdata(vec![Felt::from_hex(error_code).unwrap()]), failed: true, @@ -110,7 +110,7 @@ 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, + call: ExecutableCallEntryPoint, compiled_class: RunnableCompiledClass, state: &mut dyn State, context: &mut EntryPointExecutionContext, @@ -125,7 +125,12 @@ pub fn execute_entry_point_call( ) } RunnableCompiledClass::V1(compiled_class) => { - entry_point_execution::execute_entry_point_call(call, compiled_class, state, context) + entry_point_execution::execute_entry_point_call( + call.into(), + compiled_class, + state, + context, + ) } #[cfg(feature = "cairo_native")] RunnableCompiledClass::V1Native(compiled_class) => { @@ -133,14 +138,14 @@ pub fn execute_entry_point_call( // We cannot run native with cairo steps as the tracked resources (it's a vm // resouorce). entry_point_execution::execute_entry_point_call( - call, + call.into(), compiled_class.casm(), state, context, ) } else { native_entry_point_execution::execute_entry_point_call( - call, + call.into(), compiled_class, state, context, diff --git a/scripts/rust_fmt.sh b/scripts/rust_fmt.sh old mode 100644 new mode 100755