Skip to content

Commit c9b4e3d

Browse files
authored
Save vm trace in trace data separately (#3755)
Related #3744 commit-id:fe6352de --- **Stack**: - #3756 - #3755⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent bf0ade0 commit c9b4e3d

File tree

4 files changed

+27
-60
lines changed

4 files changed

+27
-60
lines changed

crates/cheatnet/src/runtime_extensions/call_to_blockifier_runtime_extension/execution/cairo1_execution.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,12 @@ pub(crate) fn execute_entry_point_call_cairo1(
8585
&args,
8686
program_extra_data_length,
8787
)
88-
.map_err(|source| {
88+
.inspect_err(|_| {
8989
extract_trace_and_register_errors(
90-
source,
9190
class_hash,
9291
&mut runner,
9392
cheatable_runtime.extension.cheatnet_state,
94-
)
93+
);
9594
})?;
9695

9796
let trace = get_relocated_vm_trace(&mut runner);
@@ -128,6 +127,9 @@ pub(crate) fn execute_entry_point_call_cairo1(
128127
.cheatnet_state
129128
.register_error(class_hash, pcs);
130129
}
130+
cheatnet_state
131+
.trace_data
132+
.set_vm_trace_for_current_call(trace);
131133

132134
let (syscall_usage_vm_resources, syscall_usage_sierra_gas) = match tracked_resource {
133135
TrackedResource::CairoSteps => (syscall_usage, SyscallUsageMap::default()),
@@ -138,7 +140,6 @@ pub(crate) fn execute_entry_point_call_cairo1(
138140
call_info,
139141
syscall_usage_vm_resources,
140142
syscall_usage_sierra_gas,
141-
vm_trace: Some(trace),
142143
})
143144
// endregion
144145
}

crates/cheatnet/src/runtime_extensions/call_to_blockifier_runtime_extension/execution/deprecated/cairo0_execution.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ pub(crate) fn execute_entry_point_call_cairo0(
5959
entry_point_pc,
6060
&args,
6161
)
62-
.map_err(|source| {
62+
.inspect_err(|_| {
6363
extract_trace_and_register_errors(
64-
source,
6564
call.class_hash,
6665
&mut runner,
6766
cheatable_syscall_handler.extension.cheatnet_state,
68-
)
67+
);
6968
})?;
7069

7170
let syscall_usage = cheatable_syscall_handler
@@ -86,7 +85,6 @@ pub(crate) fn execute_entry_point_call_cairo0(
8685
call_info: execution_result,
8786
syscall_usage_vm_resources: syscall_usage,
8887
syscall_usage_sierra_gas: SyscallUsageMap::default(),
89-
vm_trace: None,
9088
})
9189
// endregion
9290
}

crates/cheatnet/src/runtime_extensions/call_to_blockifier_runtime_extension/execution/entry_point.rs

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use blockifier::{
2929
state::state_api::State,
3030
};
3131
use cairo_vm::vm::runners::cairo_runner::{CairoRunner, ExecutionResources};
32-
use cairo_vm::vm::trace::trace_entry::RelocatedTraceEntry;
3332
use conversions::FromConv;
3433
use conversions::string::TryFromHexStr;
3534
use shared::vm::VirtualMachineExt;
@@ -40,16 +39,14 @@ use starknet_api::{
4039
};
4140
use starknet_types_core::felt::Felt;
4241
use std::collections::{HashMap, HashSet};
43-
use thiserror::Error;
4442

4543
pub(crate) type ContractClassEntryPointExecutionResult =
46-
Result<CallInfoWithExecutionData, EntryPointExecutionErrorWithTrace>;
44+
Result<CallInfoWithExecutionData, EntryPointExecutionError>;
4745

4846
pub(crate) struct CallInfoWithExecutionData {
4947
pub call_info: CallInfo,
5048
pub syscall_usage_vm_resources: SyscallUsageMap,
5149
pub syscall_usage_sierra_gas: SyscallUsageMap,
52-
pub vm_trace: Option<Vec<RelocatedTraceEntry>>,
5350
}
5451

5552
// blockifier/src/execution/entry_point.rs:180 (CallEntryPoint::execute)
@@ -95,7 +92,6 @@ pub fn execute_call_entry_point(
9592
ret_data: ret_data_f252,
9693
},
9794
&[],
98-
None,
9995
vec![],
10096
vec![],
10197
);
@@ -194,21 +190,19 @@ pub fn execute_call_entry_point(
194190
call_info,
195191
syscall_usage_vm_resources,
196192
syscall_usage_sierra_gas,
197-
vm_trace,
198193
}) => {
199194
exit_non_error_call(
200195
&call_info,
201196
&syscall_usage_vm_resources,
202197
&syscall_usage_sierra_gas,
203198
cheatnet_state,
204-
vm_trace,
205199
cheated_data.tx_info.signature.unwrap_or_default(),
206200
);
207201
update_remaining_gas(remaining_gas, &call_info);
208202
Ok(call_info)
209203
}
210-
Err(EntryPointExecutionErrorWithTrace { source: err, trace }) => {
211-
exit_error_call(&err, cheatnet_state, &entry_point, trace);
204+
Err(err) => {
205+
exit_error_call(&err, cheatnet_state, &entry_point);
212206
Err(err)
213207
}
214208
}
@@ -227,14 +221,11 @@ fn evaluate_execution_result(
227221
Ok(res) => {
228222
if res.call_info.execution.failed && !is_revertable {
229223
clear_handled_errors(&res.call_info, cheatnet_state);
230-
return Err(EntryPointExecutionErrorWithTrace {
231-
source: EntryPointExecutionError::ExecutionFailed {
232-
error_trace: extract_trailing_cairo1_revert_trace(
233-
&res.call_info,
234-
Cairo1RevertHeader::Execution,
235-
),
236-
},
237-
trace: res.vm_trace,
224+
return Err(EntryPointExecutionError::ExecutionFailed {
225+
error_trace: extract_trailing_cairo1_revert_trace(
226+
&res.call_info,
227+
Cairo1RevertHeader::Execution,
228+
),
238229
});
239230
}
240231
Ok(res)
@@ -247,12 +238,12 @@ fn evaluate_execution_result(
247238

248239
#[expect(clippy::result_large_err)]
249240
fn handle_entry_point_execution_error(
250-
err: EntryPointExecutionErrorWithTrace,
241+
err: EntryPointExecutionError,
251242
call: ExecutableCallEntryPoint,
252243
current_tracked_resource: TrackedResource,
253244
is_revertable: bool,
254245
) -> ContractClassEntryPointExecutionResult {
255-
if let EntryPointExecutionError::PreExecutionError(pre_err) = &err.source {
246+
if let EntryPointExecutionError::PreExecutionError(pre_err) = &err {
256247
match pre_err {
257248
PreExecutionError::EntryPointNotFound(_)
258249
| PreExecutionError::NoEntryPointOfTypeFound(_)
@@ -296,7 +287,6 @@ fn call_info_from_pre_execution_error(
296287
},
297288
syscall_usage_vm_resources: SyscallUsageMap::default(),
298289
syscall_usage_sierra_gas: SyscallUsageMap::default(),
299-
vm_trace: None,
300290
}
301291
}
302292

@@ -305,7 +295,6 @@ fn exit_non_error_call(
305295
syscall_usage_vm_resources: &SyscallUsageMap,
306296
syscall_usage_sierra_gas: &SyscallUsageMap,
307297
cheatnet_state: &mut CheatnetState,
308-
vm_trace: Option<Vec<RelocatedTraceEntry>>,
309298
signature: Vec<Felt>,
310299
) {
311300
let nested_syscall_usage_vm_resources =
@@ -327,7 +316,6 @@ fn exit_non_error_call(
327316
syscall_usage_sierra_gas,
328317
CallResult::from_non_error(call_info),
329318
&call_info.execution.l2_to_l1_messages,
330-
vm_trace,
331319
signature,
332320
call_info.execution.events.clone(),
333321
);
@@ -337,7 +325,6 @@ fn exit_error_call(
337325
error: &EntryPointExecutionError,
338326
cheatnet_state: &mut CheatnetState,
339327
entry_point: &ExecutableCallEntryPoint,
340-
vm_trace: Option<Vec<RelocatedTraceEntry>>,
341328
) {
342329
let identifier = match entry_point.call_type {
343330
CallType::Call => AddressOrClassHash::ContractAddress(entry_point.storage_address),
@@ -350,7 +337,6 @@ fn exit_error_call(
350337
SyscallUsageMap::default(),
351338
CallResult::from_err(error, &identifier),
352339
&[],
353-
vm_trace,
354340
vec![],
355341
vec![],
356342
);
@@ -445,39 +431,18 @@ fn mocked_call_info(
445431
}
446432
}
447433

448-
#[derive(Debug, Error)]
449-
#[error("{}", source)]
450-
pub struct EntryPointExecutionErrorWithTrace {
451-
pub source: EntryPointExecutionError,
452-
pub trace: Option<Vec<RelocatedTraceEntry>>,
453-
}
454-
455-
impl<T> From<T> for EntryPointExecutionErrorWithTrace
456-
where
457-
T: Into<EntryPointExecutionError>,
458-
{
459-
fn from(value: T) -> Self {
460-
Self {
461-
source: value.into(),
462-
trace: None,
463-
}
464-
}
465-
}
466-
467434
pub(crate) fn extract_trace_and_register_errors(
468-
source: EntryPointExecutionError,
469435
class_hash: ClassHash,
470436
runner: &mut CairoRunner,
471437
cheatnet_state: &mut CheatnetState,
472-
) -> EntryPointExecutionErrorWithTrace {
438+
) {
473439
let trace = get_relocated_vm_trace(runner);
440+
cheatnet_state
441+
.trace_data
442+
.set_vm_trace_for_current_call(trace);
443+
474444
let pcs = runner.vm.get_reversed_pc_traceback();
475445
cheatnet_state.register_error(class_hash, pcs);
476-
477-
EntryPointExecutionErrorWithTrace {
478-
source,
479-
trace: Some(trace),
480-
}
481446
}
482447

483448
/// This helper function is used for backtrace to avoid displaying errors that were already handled

crates/cheatnet/src/state.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ impl TraceData {
567567
current_call.borrow_mut().entry_point.class_hash = Some(class_hash);
568568
}
569569

570+
pub fn set_vm_trace_for_current_call(&mut self, vm_trace: Vec<RelocatedTraceEntry>) {
571+
let current_call = self.current_call_stack.top();
572+
current_call.borrow_mut().vm_trace = Some(vm_trace);
573+
}
574+
570575
#[expect(clippy::too_many_arguments)]
571576
pub fn exit_nested_call(
572577
&mut self,
@@ -576,7 +581,6 @@ impl TraceData {
576581
used_syscalls_sierra_gas: SyscallUsageMap,
577582
result: CallResult,
578583
l2_to_l1_messages: &[OrderedL2ToL1Message],
579-
vm_trace: Option<Vec<RelocatedTraceEntry>>,
580584
signature: Vec<Felt>,
581585
events: Vec<OrderedEvent>,
582586
) {
@@ -597,7 +601,6 @@ impl TraceData {
597601
.collect();
598602

599603
last_call.result = result;
600-
last_call.vm_trace = vm_trace;
601604
last_call.signature = signature;
602605
last_call.events = events;
603606
}

0 commit comments

Comments
 (0)