Skip to content

Commit

Permalink
refactor: native syscall handler new method
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Oct 16, 2024
1 parent 67c4e9c commit 8be2077
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
26 changes: 10 additions & 16 deletions crates/blockifier/src/execution/native/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@ pub fn execute_entry_point_call(
) -> EntryPointExecutionResult<CallInfo> {
let function_id = contract_class.get_entry_point(&call)?;

let mut syscall_handler: NativeSyscallHandler<'_> = NativeSyscallHandler::new(
state,
call.caller_address,
call.storage_address,
call.entry_point_selector,
resources,
context,
);
let mut syscall_handler: NativeSyscallHandler<'_> =
NativeSyscallHandler::new(call, state, resources, context);

let execution_result = contract_class.executor.invoke_contract_dynamic(
&function_id,
&call.calldata.0,
Some(call.initial_gas.into()),
&syscall_handler.call.calldata.0.clone(),
Some(syscall_handler.call.initial_gas.into()),
&mut syscall_handler,
);

Expand All @@ -49,11 +43,10 @@ pub fn execute_entry_point_call(
Ok(res) => Ok(res),
}?;

create_callinfo(call, call_result, syscall_handler)
create_callinfo(call_result, syscall_handler)
}

fn create_callinfo(
call: CallEntryPoint,
call_result: ContractExecutionResult,
syscall_handler: NativeSyscallHandler<'_>,
) -> Result<CallInfo, EntryPointExecutionError> {
Expand All @@ -64,20 +57,21 @@ fn create_callinfo(
call_result.remaining_gas
),
})?;
if remaining_gas > call.initial_gas {

if remaining_gas > syscall_handler.call.initial_gas {
return Err(PostExecutionError::MalformedReturnData {
error_message: format!(
"Unexpected remaining gas. Used gas is greater than initial gas: {} > {}",
remaining_gas, call.initial_gas
remaining_gas, syscall_handler.call.initial_gas
),
}
.into());
}

let gas_consumed = call.initial_gas - remaining_gas;
let gas_consumed = syscall_handler.call.initial_gas - remaining_gas;

Ok(CallInfo {
call,
call: syscall_handler.call,
execution: CallExecution {
retdata: Retdata(call_result.return_values),
events: syscall_handler.events,
Expand Down
15 changes: 3 additions & 12 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use cairo_native::starknet::{
U256,
};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::{ContractAddress, EntryPointSelector};
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;

Expand All @@ -27,11 +26,7 @@ pub struct NativeSyscallHandler<'state> {
pub state: &'state mut dyn State,
pub resources: &'state mut ExecutionResources,
pub context: &'state mut EntryPointExecutionContext,

// Call information.
pub caller_address: ContractAddress,
pub contract_address: ContractAddress,
pub entry_point_selector: Felt,
pub call: CallEntryPoint,

// Execution results.
pub events: Vec<OrderedEvent>,
Expand All @@ -45,18 +40,14 @@ pub struct NativeSyscallHandler<'state> {

impl<'state> NativeSyscallHandler<'state> {
pub fn new(
call: CallEntryPoint,
state: &'state mut dyn State,
caller_address: ContractAddress,
contract_address: ContractAddress,
entry_point_selector: EntryPointSelector,
resources: &'state mut ExecutionResources,
context: &'state mut EntryPointExecutionContext,
) -> NativeSyscallHandler<'state> {
NativeSyscallHandler {
state,
caller_address,
contract_address,
entry_point_selector: entry_point_selector.0,
call,
resources,
context,
events: Vec::new(),
Expand Down

0 comments on commit 8be2077

Please sign in to comment.