Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blockifier): add syscall counting for native syscall handler #1555

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ 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),
Expand Down
25 changes: 20 additions & 5 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -32,6 +33,8 @@ pub struct NativeSyscallHandler<'state> {
pub l2_to_l1_messages: Vec<OrderedL2ToL1Message>,
pub inner_calls: Vec<CallInfo>,

pub syscall_counter: SyscallCounter,

// Additional information gathered during execution.
pub read_values: Vec<Felt>,
pub accessed_keys: HashSet<StorageKey, RandomState>,
Expand All @@ -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,
Expand Down Expand Up @@ -84,15 +94,20 @@ impl<'state> NativeSyscallHandler<'state> {
Ok(retdata)
}

// Handles gas related logic when executing a syscall. Required because Native calls the
// syscalls directly unlike the VM where the `execute_syscall` method perform this operation
// first.
/// Handles all gas-related logics and additional metadata such as `SyscallCounter`. In native,
/// we need to explicitly call this method at the beginning of each syscall.
#[allow(dead_code)]
fn substract_syscall_gas_cost(
fn pre_execute_syscall(
&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
Loading