Skip to content

Commit

Permalink
chore(cairo_native): add native to stack trace test (#2633)
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware authored Dec 25, 2024
1 parent e27f8ff commit 93c3ef6
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 109 deletions.
78 changes: 62 additions & 16 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use crate::state::state_api::State;
use crate::transaction::objects::TransactionInfo;
use crate::versioned_constants::GasCosts;

pub const CALL_CONTRACT_SELECTOR_NAME: &str = "call_contract";
pub const LIBRARY_CALL_SELECTOR_NAME: &str = "library_call";
pub struct NativeSyscallHandler<'state> {
pub base: Box<SyscallHandlerBase<'state>>,

Expand All @@ -55,19 +57,6 @@ impl<'state> NativeSyscallHandler<'state> {
}
}

fn execute_inner_call(
&mut self,
entry_point: CallEntryPoint,
remaining_gas: &mut u64,
) -> SyscallResult<Retdata> {
let raw_retdata = self
.base
.execute_inner_call(entry_point, remaining_gas)
.map_err(|e| self.handle_error(remaining_gas, e))?;

Ok(Retdata(raw_retdata))
}

pub fn gas_costs(&self) -> &GasCosts {
self.base.context.gas_costs()
}
Expand Down Expand Up @@ -129,6 +118,36 @@ impl<'state> NativeSyscallHandler<'state> {
}
}

fn execute_inner_call(
&mut self,
entry_point: CallEntryPoint,
remaining_gas: &mut u64,
class_hash: ClassHash,
error_wrapper_fn: impl Fn(
SyscallExecutionError,
ClassHash,
ContractAddress,
EntryPointSelector,
) -> SyscallExecutionError,
) -> SyscallResult<Retdata> {
let entry_point_clone = entry_point.clone();
let raw_data = self.base.execute_inner_call(entry_point, remaining_gas).map_err(|e| {
self.handle_error(
remaining_gas,
match e {
SyscallExecutionError::Revert { .. } => e,
_ => error_wrapper_fn(
e,
class_hash,
entry_point_clone.storage_address,
entry_point_clone.entry_point_selector,
),
},
)
})?;
Ok(Retdata(raw_data))
}

fn get_tx_info_v1(&self) -> TxInfo {
let tx_info = &self.base.context.tx_context.tx_info;
TxInfo {
Expand Down Expand Up @@ -295,11 +314,13 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {

let wrapper_calldata = Calldata(Arc::new(calldata.to_vec()));

let selector = EntryPointSelector(function_selector);

let entry_point = CallEntryPoint {
class_hash: Some(class_hash),
code_address: None,
entry_point_type: EntryPointType::External,
entry_point_selector: EntryPointSelector(function_selector),
entry_point_selector: selector,
calldata: wrapper_calldata,
// The call context remains the same in a library call.
storage_address: self.base.call.storage_address,
Expand All @@ -308,7 +329,17 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
initial_gas: *remaining_gas,
};

Ok(self.execute_inner_call(entry_point, remaining_gas)?.0)
let error_wrapper_function =
|e: SyscallExecutionError,
class_hash: ClassHash,
storage_address: ContractAddress,
selector: EntryPointSelector| {
e.as_lib_call_execution_error(class_hash, storage_address, selector)
};

Ok(self
.execute_inner_call(entry_point, remaining_gas, class_hash, error_wrapper_function)?
.0)
}

fn call_contract(
Expand All @@ -322,6 +353,11 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {

let contract_address = ContractAddress::try_from(address)
.map_err(|error| self.handle_error(remaining_gas, error.into()))?;

let class_hash = self
.base
.get_class_hash_at(contract_address)
.map_err(|e| self.handle_error(remaining_gas, e))?;
if self.base.context.execution_mode == ExecutionMode::Validate
&& self.base.call.storage_address != contract_address
{
Expand All @@ -346,7 +382,17 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
initial_gas: *remaining_gas,
};

Ok(self.execute_inner_call(entry_point, remaining_gas)?.0)
let error_wrapper_function =
|e: SyscallExecutionError,
class_hash: ClassHash,
storage_address: ContractAddress,
selector: EntryPointSelector| {
e.as_call_contract_execution_error(class_hash, storage_address, selector)
};

Ok(self
.execute_inner_call(entry_point, remaining_gas, class_hash, error_wrapper_function)?
.0)
}

fn storage_read(
Expand Down
4 changes: 4 additions & 0 deletions crates/blockifier/src/execution/stack_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ fn extract_entry_point_execution_error_into_stack_trace(
EntryPointExecutionError::CairoRunError(cairo_run_error) => {
extract_cairo_run_error_into_stack_trace(error_stack, depth, cairo_run_error)
}
#[cfg(feature = "cairo_native")]
EntryPointExecutionError::NativeUnrecoverableError(error) => {
extract_syscall_execution_error_into_stack_trace(error_stack, depth, error)
}
EntryPointExecutionError::ExecutionFailed { error_trace } => {
error_stack.push(error_trace.clone().into())
}
Expand Down
Loading

0 comments on commit 93c3ef6

Please sign in to comment.