Skip to content

Commit

Permalink
refactor(blockifier): remove unnecessary wrapper for library call (#2258
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Yoni-Starkware authored Nov 25, 2024
1 parent ade351d commit d9f7c62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 49 deletions.
39 changes: 1 addition & 38 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use cairo_vm::vm::errors::memory_errors::MemoryError;
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources};
use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::state::StorageKey;
use starknet_api::transaction::fields::{
Expand All @@ -29,7 +28,7 @@ use thiserror::Error;
use crate::abi::sierra_types::SierraTypeError;
use crate::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::common_hints::{ExecutionMode, HintExecutionResult};
use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext};
use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext};
use crate::execution::errors::{ConstructorEntryPointExecutionError, EntryPointExecutionError};
use crate::execution::execution_utils::{
felt_from_ptr,
Expand Down Expand Up @@ -845,42 +844,6 @@ pub fn create_retdata_segment(
Ok(ReadOnlySegment { start_ptr: retdata_segment_start_ptr, length: raw_retdata.len() })
}

pub fn execute_library_call(
syscall_handler: &mut SyscallHintProcessor<'_>,
vm: &mut VirtualMachine,
class_hash: ClassHash,
call_to_external: bool,
entry_point_selector: EntryPointSelector,
calldata: Calldata,
remaining_gas: &mut u64,
) -> SyscallResult<ReadOnlySegment> {
let entry_point_type =
if call_to_external { EntryPointType::External } else { EntryPointType::L1Handler };
let entry_point = CallEntryPoint {
class_hash: Some(class_hash),
code_address: None,
entry_point_type,
entry_point_selector,
calldata,
// The call context remains the same in a library call.
storage_address: syscall_handler.storage_address(),
caller_address: syscall_handler.caller_address(),
call_type: CallType::Delegate,
// NOTE: this value might be overridden later on.
initial_gas: *remaining_gas,
};

execute_inner_call(entry_point, vm, syscall_handler, remaining_gas).map_err(|error| match error
{
SyscallExecutionError::SyscallError { .. } => error,
_ => error.as_lib_call_execution_error(
class_hash,
syscall_handler.storage_address(),
entry_point_selector,
),
})
}

pub fn read_felt_array<TErr>(vm: &VirtualMachine, ptr: &mut Relocatable) -> Result<Vec<Felt>, TErr>
where
TErr: From<StarknetApiError> + From<VirtualMachineError> + From<MemoryError> + From<MathError>,
Expand Down
34 changes: 23 additions & 11 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use starknet_types_core::felt::Felt;
use self::hint_processor::{
create_retdata_segment,
execute_inner_call,
execute_library_call,
felt_to_bool,
read_call_params,
read_calldata,
Expand Down Expand Up @@ -450,16 +449,29 @@ pub fn library_call(
syscall_handler: &mut SyscallHintProcessor<'_>,
remaining_gas: &mut u64,
) -> SyscallResult<LibraryCallResponse> {
let call_to_external = true;
let retdata_segment = execute_library_call(
syscall_handler,
vm,
request.class_hash,
call_to_external,
request.function_selector,
request.calldata,
remaining_gas,
)?;
let entry_point = CallEntryPoint {
class_hash: Some(request.class_hash),
code_address: None,
entry_point_type: EntryPointType::External,
entry_point_selector: request.function_selector,
calldata: request.calldata,
// The call context remains the same in a library call.
storage_address: syscall_handler.storage_address(),
caller_address: syscall_handler.caller_address(),
call_type: CallType::Delegate,
// NOTE: this value might be overridden later on.
initial_gas: *remaining_gas,
};

let retdata_segment = execute_inner_call(entry_point, vm, syscall_handler, remaining_gas)
.map_err(|error| match error {
SyscallExecutionError::SyscallError { .. } => error,
_ => error.as_lib_call_execution_error(
request.class_hash,
syscall_handler.storage_address(),
request.function_selector,
),
})?;

Ok(LibraryCallResponse { segment: retdata_segment })
}
Expand Down

0 comments on commit d9f7c62

Please sign in to comment.