Skip to content

Commit 0c26470

Browse files
authored
Add backtrace validation (#3655)
Related #3272 commit-id:93440e6c --- **Stack**: - #3656 - #3655⚠️ *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 d146920 commit 0c26470

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

crates/forge-runner/src/backtrace/data.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use cairo_lang_sierra::program::StatementIdx;
1212
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
1313
use cairo_lang_starknet_classes::contract_class::ContractClass;
1414
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
15-
use indoc::indoc;
1615
use itertools::Itertools;
1716
use rayon::iter::IntoParallelIterator;
1817
use rayon::iter::ParallelIterator;
@@ -96,22 +95,11 @@ impl ContractBacktraceData {
9695
.context("debug info not found")?;
9796

9897
let VersionedCoverageAnnotations::V1(coverage_annotations) =
99-
VersionedCoverageAnnotations::try_from_debug_info(sierra_debug_info).context(indoc! {
100-
"perhaps the contract was compiled without the following entry in Scarb.toml under [profile.dev.cairo]:
101-
unstable-add-statements-code-locations-debug-info = true
102-
103-
or scarb version is less than 2.8.0
104-
"
105-
})?;
98+
VersionedCoverageAnnotations::try_from_debug_info(sierra_debug_info)
99+
.expect("this should not fail, as we are doing validation in the `can_backtrace_be_generated` function");
106100

107101
let VersionedProfilerAnnotations::V1(profiler_annotations) =
108-
VersionedProfilerAnnotations::try_from_debug_info(sierra_debug_info).context(indoc! {
109-
"perhaps the contract was compiled without the following entry in Scarb.toml under [profile.dev.cairo]:
110-
unstable-add-statements-functions-debug-info = true
111-
112-
or scarb version is less than 2.8.0
113-
"
114-
})?;
102+
VersionedProfilerAnnotations::try_from_debug_info(sierra_debug_info).expect("this should not fail, as we are doing validation in the `can_backtrace_be_generated` function");
115103

116104
// Not optimal, but USC doesn't produce debug info for the contract class
117105
let (_, debug_info) = CasmContractClass::from_contract_class_with_debug_info(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::profile_validation::{check_cairo_profile_entries, get_manifest};
2+
use anyhow::ensure;
3+
use indoc::formatdoc;
4+
use scarb_metadata::Metadata;
5+
use semver::Version;
6+
7+
/// Checks if backtrace can be generated based on scarb version and profile settings extracted from the provided [`Metadata`].
8+
pub fn check_backtrace_compatibility(scarb_metadata: &Metadata) -> anyhow::Result<()> {
9+
check_scarb_version(scarb_metadata)?;
10+
check_profile(scarb_metadata)?;
11+
Ok(())
12+
}
13+
14+
/// Checks if the scarb version from the provided [`Metadata`] is greater than or equal to the minimal required version.
15+
fn check_scarb_version(scarb_metadata: &Metadata) -> anyhow::Result<()> {
16+
const MINIMAL_SCARB_VERSION: Version = Version::new(2, 8, 0);
17+
ensure!(
18+
scarb_metadata.app_version_info.version >= MINIMAL_SCARB_VERSION,
19+
"Backtrace generation requires scarb version >= {MINIMAL_SCARB_VERSION}",
20+
);
21+
Ok(())
22+
}
23+
24+
/// Checks if the runtime profile settings in the provided from [`Metadata`] contain the required entries for backtrace generation.
25+
fn check_profile(scarb_metadata: &Metadata) -> anyhow::Result<()> {
26+
const BACKTRACE_REQUIRED_ENTRIES: &[(&str, &str)] = &[
27+
("unstable-add-statements-functions-debug-info", "true"),
28+
("unstable-add-statements-code-locations-debug-info", "true"),
29+
];
30+
31+
let manifest = get_manifest(scarb_metadata)?;
32+
33+
let has_needed_entries =
34+
check_cairo_profile_entries(&manifest, scarb_metadata, BACKTRACE_REQUIRED_ENTRIES);
35+
36+
ensure!(
37+
has_needed_entries,
38+
formatdoc! {
39+
"Scarb.toml must have the following Cairo compiler configuration to run backtrace:
40+
41+
[profile.{profile}.cairo]
42+
unstable-add-statements-functions-debug-info = true
43+
unstable-add-statements-code-locations-debug-info = true
44+
... other entries ...
45+
",
46+
profile = scarb_metadata.current_profile
47+
},
48+
);
49+
50+
Ok(())
51+
}

crates/forge/src/profile_validation/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
mod backtrace;
12
mod coverage;
23

34
use crate::TestArgs;
5+
use crate::profile_validation::backtrace::check_backtrace_compatibility;
46
use crate::profile_validation::coverage::check_coverage_compatibility;
7+
use forge_runner::backtrace::is_backtrace_enabled;
58
use scarb_metadata::Metadata;
69
use std::fs;
710
use toml_edit::{DocumentMut, Table};
@@ -14,6 +17,9 @@ pub fn check_profile_compatibility(
1417
if test_args.coverage {
1518
check_coverage_compatibility(scarb_metadata)?;
1619
}
20+
if is_backtrace_enabled() {
21+
check_backtrace_compatibility(scarb_metadata)?;
22+
}
1723
Ok(())
1824
}
1925

crates/forge/tests/e2e/backtrace.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ fn test_wrong_scarb_toml_configuration() {
160160
assert_stdout_contains(
161161
output,
162162
indoc! {
163-
"Failure data:
164-
Got an exception while executing a hint: Requested contract address 0x0000000000000000000000000000000000000000000000000000000000000123 is not deployed.
165-
failed to create backtrace: perhaps the contract was compiled without the following entry in Scarb.toml under [profile.dev.cairo]:
166-
unstable-add-statements-code-locations-debug-info = true
163+
"[ERROR] Scarb.toml must have the following Cairo compiler configuration to run backtrace:
167164
168-
or scarb version is less than 2.8.0"
165+
[profile.dev.cairo]
166+
unstable-add-statements-functions-debug-info = true
167+
unstable-add-statements-code-locations-debug-info = true
168+
... other entries ..."
169169
},
170170
);
171171
}

0 commit comments

Comments
 (0)