Skip to content

Commit

Permalink
feat(blockifier): add syscall counting logic for native
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Oct 25, 2024
1 parent 4cf52c8 commit 985a98a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 7 additions & 3 deletions crates/blockifier/src/execution/native/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ fn create_call_info(

let gas_consumed = call.initial_gas - remaining_gas;

// todo(rodrigo): execution resources for native execution are still wip until future
// development on both Cairo lang and the Native compiler
let versioned_constants = syscall_handler.context.versioned_constants();
*syscall_handler.resources +=
&versioned_constants.get_additional_os_syscall_resources(&syscall_handler.syscall_counter);

Ok(CallInfo {
call,
execution: CallExecution {
Expand All @@ -75,9 +81,7 @@ fn create_call_info(
failed: call_result.failure_flag,
gas_consumed,
},
// todo(rodrigo): execution resources rely heavily on how the VM work, therefore
// the dummy values
resources: ExecutionResources::default(),
resources: syscall_handler.resources.clone(),
inner_calls: syscall_handler.inner_calls,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
Expand Down
17 changes: 16 additions & 1 deletion crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use starknet_types_core::felt::Felt;
use crate::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message, Retdata};
use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext};
use crate::execution::native::utils::encode_str_as_felts;
use crate::execution::syscalls::hint_processor::OUT_OF_GAS_ERROR;
use crate::execution::syscalls::hint_processor::{SyscallCounter, OUT_OF_GAS_ERROR};
use crate::execution::syscalls::SyscallSelector;
use crate::state::state_api::State;

pub struct NativeSyscallHandler<'state> {
Expand All @@ -31,6 +32,7 @@ pub struct NativeSyscallHandler<'state> {
pub caller_address: ContractAddress,
pub contract_address: ContractAddress,
pub entry_point_selector: Felt,
pub syscall_counter: SyscallCounter,

// Execution results.
pub events: Vec<OrderedEvent>,
Expand All @@ -56,6 +58,7 @@ impl<'state> NativeSyscallHandler<'state> {
caller_address,
contract_address,
entry_point_selector: entry_point_selector.0,
syscall_counter: SyscallCounter::new(),
resources,
context,
events: Vec::new(),
Expand All @@ -66,6 +69,12 @@ impl<'state> NativeSyscallHandler<'state> {
}
}

#[allow(dead_code)]
fn increment_syscall_count_by(&mut self, selector: &SyscallSelector, n: usize) {
let syscall_count = self.syscall_counter.entry(*selector).or_default();
*syscall_count += n
}

#[allow(dead_code)]
fn execute_inner_call(
&mut self,
Expand Down Expand Up @@ -100,8 +109,14 @@ impl<'state> NativeSyscallHandler<'state> {
fn substract_syscall_gas_cost(
&mut self,
remaining_gas: &mut u128,
syscall_selector: SyscallSelector,
syscall_gas_cost: u64,
) -> SyscallResult<()> {
// Syscall count for Keccak is done differently
if syscall_selector != SyscallSelector::Keccak {
self.increment_syscall_count_by(&syscall_selector, 1);
}

// Refund `SYSCALL_BASE_GAS_COST` as it was pre-charged.
let required_gas =
u128::from(syscall_gas_cost - self.context.gas_costs().syscall_base_gas_cost);
Expand Down

0 comments on commit 985a98a

Please sign in to comment.