Skip to content

Commit 7cf6bec

Browse files
ksew1cptartur
andcommitted
Bump blockifier, change deps, make compile (#3713)
**Stack**: - #3711 - #3710 - #3709 - #3713⚠️ *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.* --------- Co-authored-by: Artur Michalek <[email protected]>
1 parent e705594 commit 7cf6bec

File tree

19 files changed

+288
-93
lines changed

19 files changed

+288
-93
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ members = [
1717
"crates/debugging",
1818
"crates/testing/packages_validation",
1919
"crates/foundry-ui",
20+
"crates/native-api",
2021
]
2122

2223
exclude = ["crates/snforge-scarb-plugin", "crates/snforge-scarb-plugin-deprecated"]

crates/cheatnet/src/runtime_extensions/forge_runtime_extension/cheatcodes/declare.rs

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ use anyhow::{Context, Result};
77
use blockifier::execution::contract_class::{CompiledClassV1, RunnableCompiledClass};
88
use blockifier::execution::native::contract_class::NativeCompiledClassV1;
99
use blockifier::state::{errors::StateError, state_api::State};
10-
use cairo_lang_starknet_classes::contract_class::ContractClass;
11-
use cairo_native::executor::AotContractExecutor;
1210
use conversions::IntoConv;
1311
use conversions::serde::serialize::CairoSerialize;
12+
use scarb_api::StarknetContractArtifacts;
1413
use starknet::core::types::contract::SierraClass;
15-
use starknet_api::contract_class::SierraVersion;
1614
use starknet_api::core::{ClassHash, CompiledClassHash};
1715

1816
#[derive(CairoSerialize)]
@@ -31,41 +29,7 @@ pub fn declare(
3129
.with_context(|| format!("Failed to get contract artifact for name = {contract_name}."))
3230
.map_err(EnhancedHintError::from)?;
3331

34-
let sierra_contract_class: ContractClass =
35-
serde_json::from_str(&contract_artifact.sierra).unwrap();
36-
37-
let sierra_program = sierra_contract_class
38-
.extract_sierra_program()
39-
.expect("Cannot extract sierra program from sierra contract class");
40-
41-
let sierra_version_values = sierra_contract_class
42-
.sierra_program
43-
.iter()
44-
.take(3)
45-
.map(|x| x.value.clone())
46-
.collect::<Vec<_>>();
47-
48-
let sierra_version = SierraVersion::extract_from_program(&sierra_version_values)
49-
.expect("Cannot extract sierra version from sierra program");
50-
51-
let executor = AotContractExecutor::new(
52-
&sierra_program,
53-
&sierra_contract_class.entry_points_by_type,
54-
sierra_version.clone().into(),
55-
cairo_native::OptLevel::Default,
56-
// `stats` - Passing a [cairo_native::statistics::Statistics] object enables collecting
57-
// compilation statistics.
58-
None,
59-
)
60-
.expect("Cannot compile sierra into native");
61-
62-
let contract_class = CompiledClassV1::try_from_json_string(
63-
&contract_artifact.casm,
64-
get_current_sierra_version(),
65-
)
66-
.expect("Failed to read contract class from json");
67-
let contract_class = NativeCompiledClassV1::new(executor, contract_class);
68-
let contract_class = RunnableCompiledClass::V1Native(contract_class);
32+
let contract_class = get_contract_class(contract_artifact);
6933

7034
let class_hash = *contracts_data
7135
.get_class_hash(contract_name)
@@ -101,3 +65,19 @@ pub fn declare(
10165
pub fn get_class_hash(sierra_class: &SierraClass) -> Result<ClassHash> {
10266
Ok(sierra_class.class_hash()?.into_())
10367
}
68+
69+
fn get_contract_class(contract_artifact: &StarknetContractArtifacts) -> RunnableCompiledClass {
70+
let contract_class = CompiledClassV1::try_from_json_string(
71+
&contract_artifact.casm,
72+
get_current_sierra_version(),
73+
)
74+
.expect("Failed to read contract class from json");
75+
76+
match &contract_artifact.executor {
77+
None => RunnableCompiledClass::V1(contract_class),
78+
Some(executor) => RunnableCompiledClass::V1Native(NativeCompiledClassV1::new(
79+
executor.clone(),
80+
contract_class,
81+
)),
82+
}
83+
}

crates/cheatnet/tests/common/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use runtime::starknet::constants::TEST_ADDRESS;
2929
use runtime::starknet::context::build_context;
3030
use scarb_api::metadata::MetadataCommandExt;
3131
use scarb_api::{
32-
ScarbCommand, get_contracts_artifacts_and_source_sierra_paths, target_dir_for_workspace,
32+
CompilationOpts, ScarbCommand, get_contracts_artifacts_and_source_sierra_paths,
33+
target_dir_for_workspace,
3334
};
3435
use starknet::core::utils::get_selector_from_name;
3536
use starknet_api::contract_class::EntryPointType;
@@ -85,8 +86,13 @@ pub fn get_contracts() -> ContractsData {
8586
let package = scarb_metadata.packages.first().unwrap();
8687

8788
let ui = UI::default();
88-
let contracts =
89-
get_contracts_artifacts_and_source_sierra_paths(&target_dir, package, false, &ui).unwrap();
89+
let contracts = get_contracts_artifacts_and_source_sierra_paths(
90+
&target_dir,
91+
package,
92+
&ui,
93+
CompilationOpts::default(),
94+
)
95+
.unwrap();
9096
ContractsData::try_from(contracts).unwrap()
9197
}
9298

crates/forge/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ pub struct TestArgs {
142142
#[command(flatten)]
143143
trace_args: TraceArgs,
144144

145+
/// Run contracts on `cairo-native` instead of the default `cairo-vm`.
146+
///
147+
/// Note: Only contracts execution through native is supported, test code itself will still run on `cairo-vm`.
148+
#[arg(long)]
149+
run_native: bool,
150+
145151
/// Use exact matches for `test_filter`
146152
#[arg(short, long)]
147153
exact: bool,
@@ -169,7 +175,7 @@ pub struct TestArgs {
169175
include_ignored: bool,
170176

171177
/// Display more detailed info about used resources
172-
#[arg(long)]
178+
#[arg(long, conflicts_with = "run_native")]
173179
detailed_resources: bool,
174180

175181
/// Control when colored output is used
@@ -181,15 +187,15 @@ pub struct TestArgs {
181187
rerun_failed: bool,
182188

183189
/// Save execution traces of all test which have passed and are not fuzz tests
184-
#[arg(long)]
190+
#[arg(long, conflicts_with = "run_native")]
185191
save_trace_data: bool,
186192

187193
/// Build profiles of all tests which have passed and are not fuzz tests using the cairo-profiler
188-
#[arg(long, conflicts_with = "coverage")]
194+
#[arg(long, conflicts_with_all = ["run_native", "coverage"])]
189195
build_profile: bool,
190196

191197
/// Generate a coverage report for the executed tests which have passed and are not fuzz tests using the cairo-coverage
192-
#[arg(long, conflicts_with = "build_profile")]
198+
#[arg(long, conflicts_with_all = ["run_native", "build_profile"])]
193199
coverage: bool,
194200

195201
/// Number of maximum steps during a single test. For fuzz tests this value is applied to each subtest separately.

crates/forge/src/profile_validation/backtrace.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1+
use crate::TestArgs;
12
use crate::profile_validation::{check_cairo_profile_entries, get_manifest};
23
use anyhow::ensure;
34
use indoc::formatdoc;
45
use scarb_metadata::Metadata;
56
use semver::Version;
67

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<()> {
8+
/// Checks if backtrace can be generated based on scarb version, profile settings extracted from
9+
/// the provided [`Metadata`] and if native execution is disabled in the provided [`TestArgs`].
10+
pub fn check_backtrace_compatibility(
11+
test_args: &TestArgs,
12+
scarb_metadata: &Metadata,
13+
) -> anyhow::Result<()> {
14+
check_if_native_disabled(test_args)?;
915
check_scarb_version(scarb_metadata)?;
1016
check_profile(scarb_metadata)?;
1117
Ok(())
1218
}
1319

20+
/// Checks if native execution is disabled in the provided [`TestArgs`].
21+
fn check_if_native_disabled(test_args: &TestArgs) -> anyhow::Result<()> {
22+
ensure!(
23+
!test_args.run_native,
24+
"Backtrace generation is not supported with `cairo-native` execution",
25+
);
26+
Ok(())
27+
}
1428
/// Checks if the scarb version from the provided [`Metadata`] is greater than or equal to the minimal required version.
1529
fn check_scarb_version(scarb_metadata: &Metadata) -> anyhow::Result<()> {
1630
const MINIMAL_SCARB_VERSION: Version = Version::new(2, 8, 0);

crates/forge/src/profile_validation/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn check_profile_compatibility(
1818
check_coverage_compatibility(scarb_metadata)?;
1919
}
2020
if is_backtrace_enabled() {
21-
check_backtrace_compatibility(scarb_metadata)?;
21+
check_backtrace_compatibility(test_args, scarb_metadata)?;
2222
}
2323
Ok(())
2424
}

crates/forge/src/run_tests/package.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use forge_runner::{
3131
test_target_summary::TestTargetSummary,
3232
};
3333
use foundry_ui::{UI, components::labeled::LabeledMessage};
34-
use scarb_api::get_contracts_artifacts_and_source_sierra_paths;
34+
use scarb_api::{CompilationOpts, get_contracts_artifacts_and_source_sierra_paths};
3535
use scarb_metadata::{Metadata, PackageMetadata};
3636
use std::sync::Arc;
3737

@@ -82,11 +82,14 @@ impl RunForPackageArgs {
8282
let contracts = get_contracts_artifacts_and_source_sierra_paths(
8383
artifacts_dir,
8484
&package,
85-
!should_compile_starknet_contract_target(
86-
&scarb_metadata.app_version_info.version,
87-
args.no_optimization,
88-
),
8985
ui,
86+
CompilationOpts {
87+
use_test_target_contracts: !should_compile_starknet_contract_target(
88+
&scarb_metadata.app_version_info.version,
89+
args.no_optimization,
90+
),
91+
run_native: args.run_native,
92+
},
9093
)?;
9194
let contracts_data = ContractsData::try_from(contracts)?;
9295

crates/forge/test_utils/src/runner.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use forge_runner::{
1717
use foundry_ui::UI;
1818
use indoc::formatdoc;
1919
use scarb_api::{
20-
ScarbCommand, StarknetContractArtifacts, get_contracts_artifacts_and_source_sierra_paths,
21-
metadata::MetadataCommandExt, target_dir_for_workspace,
20+
CompilationOpts, ScarbCommand, StarknetContractArtifacts,
21+
get_contracts_artifacts_and_source_sierra_paths, metadata::MetadataCommandExt,
22+
target_dir_for_workspace,
2223
};
2324
use shared::command::CommandExt;
2425
use starknet_api::execution_resources::{GasAmount, GasVector};
@@ -104,12 +105,16 @@ impl Contract {
104105
.unwrap();
105106
let artifacts_dir = target_dir_for_workspace(&scarb_metadata).join("dev");
106107

107-
let contract =
108-
get_contracts_artifacts_and_source_sierra_paths(&artifacts_dir, package, false, ui)
109-
.unwrap()
110-
.remove(&self.name)
111-
.ok_or(anyhow!("there is no contract with name {}", self.name))?
112-
.0;
108+
let contract = get_contracts_artifacts_and_source_sierra_paths(
109+
&artifacts_dir,
110+
package,
111+
ui,
112+
CompilationOpts::default(),
113+
)
114+
.unwrap()
115+
.remove(&self.name)
116+
.ok_or(anyhow!("there is no contract with name {}", self.name))?
117+
.0;
113118

114119
Ok((contract.sierra, contract.casm))
115120
}
@@ -221,7 +226,11 @@ impl<'a> TestCase {
221226
Ok((
222227
name,
223228
(
224-
StarknetContractArtifacts { sierra, casm },
229+
StarknetContractArtifacts {
230+
sierra,
231+
casm,
232+
executor: None,
233+
},
225234
Utf8PathBuf::default(),
226235
),
227236
))

crates/forge/tests/e2e/backtrace.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ fn test_backtrace_missing_env() {
2323
);
2424
}
2525

26+
#[test]
27+
fn test_backtrace_native_execution() {
28+
let temp = setup_package("backtrace_vm_error");
29+
30+
test_runner(&temp)
31+
.arg("--run-native")
32+
.env("SNFORGE_BACKTRACE", "1")
33+
.assert()
34+
.code(2)
35+
.stdout_eq("[ERROR] Backtrace generation is not supported with `cairo-native` execution\n");
36+
}
37+
2638
#[test]
2739
fn test_backtrace() {
2840
let temp = setup_package("backtrace_vm_error");

0 commit comments

Comments
 (0)