From 538df34d79103f8ef2f0f42562ca99beebc2ec62 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Thu, 24 Oct 2024 22:53:18 +0100 Subject: [PATCH] feat(blockifier): add syscall counting logic for native --- .../execution/native/entry_point_execution.rs | 16 +++++++++------- .../src/execution/native/syscall_handler.rs | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/crates/blockifier/src/execution/native/entry_point_execution.rs b/crates/blockifier/src/execution/native/entry_point_execution.rs index 84f4c3b4d4..fbedfd23fc 100644 --- a/crates/blockifier/src/execution/native/entry_point_execution.rs +++ b/crates/blockifier/src/execution/native/entry_point_execution.rs @@ -1,7 +1,6 @@ use cairo_native::execution_result::ContractExecutionResult; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use num_traits::ToPrimitive; -use starknet_api::execution_resources::GasAmount; use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources, Retdata}; use crate::execution::contract_class::TrackedResource; @@ -62,6 +61,12 @@ fn create_callinfo( let gas_consumed = syscall_handler.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: syscall_handler.call, execution: CallExecution { @@ -71,12 +76,9 @@ fn create_callinfo( failed: call_result.failure_flag, gas_consumed, }, - // todo(rodrigo): execution resources rely heavily on how the VM work, therefore - // the dummy values - charged_resources: ChargedResources { - vm_resources: ExecutionResources::default(), - gas_for_fee: GasAmount(gas_consumed), - }, + charged_resources: ChargedResources::from_execution_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, diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 1b11df2e30..8dde577c95 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -17,7 +17,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> { @@ -32,6 +33,8 @@ pub struct NativeSyscallHandler<'state> { pub l2_to_l1_messages: Vec, pub inner_calls: Vec, + pub syscall_counter: SyscallCounter, + // Additional information gathered during execution. pub read_values: Vec, pub accessed_keys: HashSet, @@ -52,11 +55,18 @@ impl<'state> NativeSyscallHandler<'state> { events: Vec::new(), l2_to_l1_messages: Vec::new(), inner_calls: Vec::new(), + syscall_counter: SyscallCounter::new(), read_values: Vec::new(), accessed_keys: HashSet::new(), } } + #[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, @@ -91,8 +101,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);