Skip to content

Commit

Permalink
Migrate profiler trace-data to cairo-annotations trace data (#2583)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Towards #2468

## Introduced changes

<!-- A brief description of the changes -->

- Migrate profiler trace-data to cairo-annotations trace data

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
ksew1 authored Oct 16, 2024
1 parent 9ea9d3a commit 4472f34
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 48 deletions.
55 changes: 41 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ cairo-lang-starknet-classes = "2.7.0"
cairo-lang-parser = "2.7.0"
cairo-lang-macro = "0.1.0"
cairo-vm = "1.0.0-rc3"
cairo-annotations = "0.1.0"
starknet-types-core = { version = "0.1.6", features = ["hash", "prime-bigint"] }
anyhow = "1.0.89"
assert_fs = "1.1.2"
Expand All @@ -66,7 +67,6 @@ serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.127"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "660a732" }
starknet-crypto = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "660a732" }
trace-data = { git = "https://github.com/software-mansion/cairo-profiler/", rev = "e61a4a7" }
tempfile = "3.13.0"
thiserror = "1.0.64"
ctor = "0.2.8"
Expand Down
2 changes: 1 addition & 1 deletion crates/cheatnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cairo-lang-utils.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-vm.workspace = true
cairo-annotations.workspace = true
regex.workspace = true
indoc.workspace = true
starknet.workspace = true
Expand All @@ -33,7 +34,6 @@ tokio.workspace = true
num-bigint.workspace = true
conversions.workspace = true
scarb-metadata.workspace = true
trace-data.workspace = true
fs2.workspace = true
flate2.workspace = true
scarb-api = { path = "../scarb-api" }
Expand Down
2 changes: 1 addition & 1 deletion crates/cheatnet/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use blockifier::{
execution::contract_class::ContractClass,
state::state_api::{StateReader, StateResult},
};
use cairo_annotations::trace_data::L1Resources;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use cairo_vm::vm::trace::trace_entry::RelocatedTraceEntry;
use cairo_vm::Felt252;
Expand All @@ -32,7 +33,6 @@ use starknet_api::{
use std::cell::{Ref, RefCell};
use std::collections::HashMap;
use std::rc::Rc;
use trace_data::L1Resources;

// Specifies the duration of the cheat
#[derive(CairoDeserialize, Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/forge-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cairo-lang-sierra-type-size.workspace = true
cairo-lang-sierra-gas.workspace = true
cairo-lang-sierra-ap-change.workspace = true
cairo-lang-test-plugin.workspace = true
cairo-annotations.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
starknet.workspace = true
Expand All @@ -43,7 +44,6 @@ semver.workspace = true
console.workspace = true
serde.workspace = true
bimap.workspace = true
trace-data.workspace = true
flatten-serde-json.workspace = true
rayon.workspace = true
cheatnet = { path = "../cheatnet" }
Expand Down
24 changes: 14 additions & 10 deletions crates/forge-runner/src/build_trace_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ use crate::build_trace_data::test_sierra_program_path::VersionedProgramPath;
use blockifier::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use blockifier::execution::entry_point::{CallEntryPoint, CallType};
use blockifier::execution::syscalls::hint_processor::SyscallCounter;
use cairo_annotations::trace_data::{
CairoExecutionInfo, CallEntryPoint as ProfilerCallEntryPoint,
CallTraceNode as ProfilerCallTraceNode, CallTraceV1 as ProfilerCallTrace,
CallType as ProfilerCallType, CasmLevelInfo, ContractAddress,
DeprecatedSyscallSelector as ProfilerDeprecatedSyscallSelector,
EntryPointSelector as ProfilerEntryPointSelector, EntryPointType as ProfilerEntryPointType,
ExecutionResources as ProfilerExecutionResources, TraceEntry as ProfilerTraceEntry,
VersionedCallTrace as VersionedProfilerCallTrace, VmExecutionResources,
};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use cairo_vm::vm::trace::trace_entry::RelocatedTraceEntry;
use camino::Utf8PathBuf;
Expand All @@ -20,14 +29,6 @@ use conversions::IntoConv;
use starknet::core::utils::get_selector_from_name;
use starknet_api::core::{ClassHash, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use trace_data::{
CairoExecutionInfo, CallEntryPoint as ProfilerCallEntryPoint, CallTrace as ProfilerCallTrace,
CallTraceNode as ProfilerCallTraceNode, CallType as ProfilerCallType, CasmLevelInfo,
ContractAddress, DeprecatedSyscallSelector as ProfilerDeprecatedSyscallSelector,
EntryPointSelector as ProfilerEntryPointSelector, EntryPointType as ProfilerEntryPointType,
ExecutionResources as ProfilerExecutionResources, TraceEntry as ProfilerTraceEntry,
VmExecutionResources,
};

pub mod test_sierra_program_path;

Expand Down Expand Up @@ -173,7 +174,7 @@ pub fn build_profiler_call_entry_point(
let function_name = get_function_name(&entry_point_selector, contracts_data);

ProfilerCallEntryPoint {
class_hash: class_hash.map(|ch| trace_data::ClassHash(ch.to_string())),
class_hash: class_hash.map(|ch| cairo_annotations::trace_data::ClassHash(ch.to_string())),
entry_point_type: build_profiler_entry_point_type(entry_point_type),
entry_point_selector: ProfilerEntryPointSelector(format!("{}", entry_point_selector.0)),
contract_address: ContractAddress(format!("{}", storage_address.0.key())),
Expand Down Expand Up @@ -306,7 +307,10 @@ fn build_profiler_trace_entry(value: &RelocatedTraceEntry) -> ProfilerTraceEntry
}
}

pub fn save_trace_data(test_name: &String, trace_data: &ProfilerCallTrace) -> Result<PathBuf> {
pub fn save_trace_data(
test_name: &String,
trace_data: &VersionedProfilerCallTrace,
) -> Result<PathBuf> {
let serialized_trace =
serde_json::to_string(trace_data).expect("Failed to serialize call trace");
let dir_to_save_trace = PathBuf::from(TRACE_DIR);
Expand Down
12 changes: 6 additions & 6 deletions crates/forge-runner/src/test_case_summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::build_trace_data::test_sierra_program_path::VersionedProgramPath;
use crate::expected_result::{ExpectedPanicValue, ExpectedTestResult};
use crate::gas::check_available_gas;
use crate::package_tests::with_config_resolved::TestCaseWithResolvedConfig;
use cairo_annotations::trace_data::VersionedCallTrace as VersionedProfilerCallTrace;
use cairo_lang_runner::short_string::as_cairo_short_string;
use cairo_lang_runner::{RunResult, RunResultValue};
use cairo_vm::Felt252;
Expand All @@ -15,7 +16,6 @@ use shared::utils::build_readable_text;
use std::cell::RefCell;
use std::option::Option;
use std::rc::Rc;
use trace_data::CallTrace as ProfilerCallTrace;

#[derive(Debug, PartialEq, Clone, Default)]
pub struct GasStatistics {
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct Single;
impl TestType for Single {
type GasInfo = u128;
type TestStatistics = ();
type TraceData = ProfilerCallTrace;
type TraceData = VersionedProfilerCallTrace;
}

/// Summary of running a single test case
Expand Down Expand Up @@ -231,11 +231,11 @@ impl TestCaseSummary<Single> {
test_statistics: (),
gas_info: gas,
used_resources,
trace_data: build_profiler_call_trace(
trace_data: VersionedProfilerCallTrace::V1(build_profiler_call_trace(
call_trace,
contracts_data,
maybe_versioned_program_path,
),
)),
};
check_available_gas(&test_case.config.available_gas, summary)
}
Expand Down Expand Up @@ -269,11 +269,11 @@ impl TestCaseSummary<Single> {
test_statistics: (),
gas_info: gas,
used_resources,
trace_data: build_profiler_call_trace(
trace_data: VersionedProfilerCallTrace::V1(build_profiler_call_trace(
call_trace,
contracts_data,
maybe_versioned_program_path,
),
)),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cairo-lang-starknet.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-test-plugin.workspace = true
cairo-annotations.workspace = true
starknet-types-core.workspace = true
itertools.workspace = true
regex.workspace = true
Expand Down Expand Up @@ -63,7 +64,6 @@ tokio-util.workspace = true
futures.workspace = true
num-integer.workspace = true
url.workspace = true
trace-data.workspace = true
fs_extra.workspace = true
project-root.workspace = true
indoc.workspace = true
Expand Down
13 changes: 8 additions & 5 deletions crates/forge/tests/e2e/build_trace_data.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use super::common::runner::{setup_package, test_runner};
use crate::e2e::common::get_trace_from_trace_node;
use cairo_annotations::trace_data::{
CallTraceNode as ProfilerCallTraceNode, CallTraceV1 as ProfilerCallTrace,
VersionedCallTrace as VersionedProfilerCallTrace,
};
use cairo_lang_sierra::program::VersionedProgram;
use cairo_lang_starknet_classes::contract_class::ContractClass;
use forge_runner::build_trace_data::{TEST_CODE_CONTRACT_NAME, TEST_CODE_FUNCTION_NAME, TRACE_DIR};
use std::fs;
use trace_data::{CallTrace as ProfilerCallTrace, CallTraceNode as ProfilerCallTraceNode};

#[test]
fn simple_package_save_trace() {
Expand Down Expand Up @@ -34,7 +37,7 @@ fn simple_package_save_trace() {
)
.unwrap();

let call_trace: ProfilerCallTrace =
let VersionedProfilerCallTrace::V1(call_trace) =
serde_json::from_str(&trace_data).expect("Failed to parse call_trace");

assert!(call_trace.nested_calls.is_empty());
Expand Down Expand Up @@ -152,14 +155,14 @@ fn trace_has_deploy_with_no_constructor_phantom_nodes() {
// 3 first calls are deploys with empty constructors
matches!(
call_trace.nested_calls[0],
trace_data::CallTraceNode::DeployWithoutConstructor
cairo_annotations::trace_data::CallTraceNode::DeployWithoutConstructor
);
matches!(
call_trace.nested_calls[1],
trace_data::CallTraceNode::DeployWithoutConstructor
cairo_annotations::trace_data::CallTraceNode::DeployWithoutConstructor
);
matches!(
call_trace.nested_calls[2],
trace_data::CallTraceNode::DeployWithoutConstructor
cairo_annotations::trace_data::CallTraceNode::DeployWithoutConstructor
);
}
4 changes: 3 additions & 1 deletion crates/forge/tests/e2e/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use trace_data::{CallTrace as ProfilerCallTrace, CallTraceNode as ProfilerCallTraceNode};
use cairo_annotations::trace_data::{
CallTraceNode as ProfilerCallTraceNode, CallTraceV1 as ProfilerCallTrace,
};

pub mod runner;

Expand Down
Loading

0 comments on commit 4472f34

Please sign in to comment.