Skip to content

Commit ec4177e

Browse files
authored
Remove redundant version checks (#3907)
commit-id:69a775ca
1 parent afdf47c commit ec4177e

File tree

11 files changed

+19
-148
lines changed

11 files changed

+19
-148
lines changed

crates/forge/src/new.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use std::io::Write;
1313
use std::path::{Path, PathBuf};
1414
use toml_edit::{Array, ArrayOfTables, DocumentMut, Item, Table, Value, value};
1515

16+
const OZ_VERSION: Version = Version::new(1, 0, 0);
17+
1618
static TEMPLATES_DIR: Dir = include_dir!("snforge_templates");
1719

18-
const DEFAULT_ASSERT_MACROS: Version = Version::new(0, 1, 0);
19-
const MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS: Version = Version::new(2, 8, 0);
2020
const SCARB_WITHOUT_CAIRO_TEST_TEMPLATE: Version = Version::new(2, 13, 0);
2121

2222
struct Dependency {
@@ -134,7 +134,7 @@ impl TryFrom<&Template> for TemplateManifestConfig {
134134
},
135135
Dependency {
136136
name: "openzeppelin_token".to_string(),
137-
version: get_oz_version()?.to_string(),
137+
version: OZ_VERSION.to_string(),
138138
dev: false,
139139
},
140140
],
@@ -256,12 +256,7 @@ fn set_cairo_edition(document: &mut DocumentMut, cairo_edition: &str) {
256256
}
257257

258258
fn add_assert_macros(document: &mut DocumentMut) -> Result<()> {
259-
let versions = scarb_version()?;
260-
let version = if versions.scarb < MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS {
261-
DEFAULT_ASSERT_MACROS
262-
} else {
263-
versions.cairo
264-
};
259+
let version = scarb_version()?.cairo;
265260

266261
document
267262
.entry("dev-dependencies")
@@ -464,16 +459,3 @@ fn get_template_dir(template: &Template) -> Result<Dir<'_>> {
464459
.ok_or_else(|| anyhow!("Directory {dir_name} not found"))
465460
.cloned()
466461
}
467-
468-
fn get_oz_version() -> Result<Version> {
469-
let scarb_version = scarb_version()?.scarb;
470-
471-
let oz_version = match scarb_version {
472-
ver if ver >= Version::new(2, 9, 4) => Version::new(1, 0, 0),
473-
ver if ver >= Version::new(2, 9, 1) => Version::new(0, 20, 0),
474-
ver if ver >= Version::new(2, 8, 4) => Version::new(0, 19, 0),
475-
_ => bail!("Minimal Scarb version to create a new project with ERC-20 template is 2.8.4"),
476-
};
477-
478-
Ok(oz_version)
479-
}

crates/forge/src/profile_validation/backtrace.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::profile_validation::{check_cairo_profile_entries, get_manifest};
33
use anyhow::ensure;
44
use indoc::formatdoc;
55
use scarb_metadata::Metadata;
6-
use semver::Version;
76

87
/// Checks if backtrace can be generated based on scarb version, profile settings extracted from
98
/// the provided [`Metadata`] and if native execution is disabled in the provided [`TestArgs`].
@@ -14,7 +13,6 @@ pub fn check_backtrace_compatibility(
1413
) -> anyhow::Result<()> {
1514
#[cfg(feature = "cairo-native")]
1615
check_if_native_disabled(test_args)?;
17-
check_scarb_version(scarb_metadata)?;
1816
check_profile(scarb_metadata)?;
1917
Ok(())
2018
}
@@ -28,15 +26,6 @@ fn check_if_native_disabled(test_args: &TestArgs) -> anyhow::Result<()> {
2826
);
2927
Ok(())
3028
}
31-
/// Checks if the scarb version from the provided [`Metadata`] is greater than or equal to the minimal required version.
32-
fn check_scarb_version(scarb_metadata: &Metadata) -> anyhow::Result<()> {
33-
const MINIMAL_SCARB_VERSION: Version = Version::new(2, 8, 0);
34-
ensure!(
35-
scarb_metadata.app_version_info.version >= MINIMAL_SCARB_VERSION,
36-
"Backtrace generation requires scarb version >= {MINIMAL_SCARB_VERSION}",
37-
);
38-
Ok(())
39-
}
4029

4130
/// Checks if the runtime profile settings in the provided from [`Metadata`] contain the required entries for backtrace generation.
4231
fn check_profile(scarb_metadata: &Metadata) -> anyhow::Result<()> {

crates/forge/src/profile_validation/coverage.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,10 @@ use crate::profile_validation::{check_cairo_profile_entries, get_manifest};
22
use anyhow::ensure;
33
use indoc::formatdoc;
44
use scarb_metadata::Metadata;
5-
use semver::Version;
65

76
/// Checks if coverage can be based on scarb version and profile settings extracted from the provided [`Metadata`].
87
pub fn check_coverage_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-
"Coverage generation requires scarb version >= {MINIMAL_SCARB_VERSION}",
20-
);
21-
Ok(())
8+
check_profile(scarb_metadata)
229
}
2310

2411
/// Checks if the runtime profile settings in the provided from [`Metadata`] contain the required entries for coverage generation.

crates/forge/src/run_tests/package.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ use crate::{
1212
},
1313
scarb::{
1414
config::{ForgeConfigFromScarb, ForkTarget},
15-
load_test_artifacts, should_compile_starknet_contract_target,
15+
load_test_artifacts,
1616
},
1717
shared_cache::FailedTestsCache,
1818
test_filter::{NameFilter, TestsFilter},
19-
warn::{
20-
warn_if_available_gas_used_with_incompatible_scarb_version,
21-
warn_if_incompatible_rpc_version,
22-
},
19+
warn::warn_if_incompatible_rpc_version,
2320
};
2421
use anyhow::Result;
2522
use camino::{Utf8Path, Utf8PathBuf};
@@ -88,10 +85,7 @@ impl RunForPackageArgs {
8885
&package,
8986
ui,
9087
CompilationOpts {
91-
use_test_target_contracts: !should_compile_starknet_contract_target(
92-
&scarb_metadata.app_version_info.version,
93-
args.no_optimization,
94-
),
88+
use_test_target_contracts: !args.no_optimization,
9589
#[cfg(feature = "cairo-native")]
9690
run_native: args.run_native,
9791
},
@@ -193,7 +187,6 @@ pub async fn run_for_package(
193187
tests_filter.filter_tests(&mut test_target.test_cases)?;
194188
}
195189

196-
warn_if_available_gas_used_with_incompatible_scarb_version(&test_targets, &ui)?;
197190
warn_if_incompatible_rpc_version(&test_targets, ui.clone()).await?;
198191

199192
let not_filtered = sum_test_cases(&test_targets);

crates/forge/src/run_tests/workspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ pub async fn run_for_workspace(args: TestArgs, ui: Arc<UI>) -> Result<ExitStatus
8282
filter.clone(),
8383
args.scarb_args.features.clone(),
8484
args.scarb_args.profile.clone(),
85-
&scarb_metadata.app_version_info.version,
8685
args.no_optimization,
8786
)?;
8887

crates/forge/src/scarb.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ use forge_runner::package_tests::raw::TestTargetRaw;
88
use scarb_api::{ScarbCommand, test_targets_by_name};
99
use scarb_metadata::PackageMetadata;
1010
use scarb_ui::args::{FeaturesSpec, PackagesFilter, ProfileSpec};
11-
use semver::Version;
1211
use std::fs;
1312
use std::io::ErrorKind;
1413

1514
pub mod config;
1615

17-
const MINIMAL_SCARB_VERSION_TO_OPTIMIZE_COMPILATION: Version = Version::new(2, 8, 3);
18-
1916
impl PackageConfig for ForgeConfigFromScarb {
2017
fn tool_name() -> &'static str {
2118
"snforge"
@@ -29,23 +26,14 @@ impl PackageConfig for ForgeConfigFromScarb {
2926
}
3027
}
3128

32-
#[must_use]
33-
pub fn should_compile_starknet_contract_target(
34-
scarb_version: &Version,
35-
no_optimization: bool,
36-
) -> bool {
37-
*scarb_version < MINIMAL_SCARB_VERSION_TO_OPTIMIZE_COMPILATION || no_optimization
38-
}
39-
4029
#[tracing::instrument(skip_all, level = "debug")]
4130
pub fn build_artifacts_with_scarb(
4231
filter: PackagesFilter,
4332
features: FeaturesSpec,
4433
profile: ProfileSpec,
45-
scarb_version: &Version,
4634
no_optimization: bool,
4735
) -> Result<()> {
48-
if should_compile_starknet_contract_target(scarb_version, no_optimization) {
36+
if no_optimization {
4937
build_contracts_with_scarb(filter.clone(), features.clone(), profile.clone())?;
5038
}
5139
build_test_artifacts_with_scarb(filter, features, profile)?;

crates/forge/src/warn.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use foundry_ui::UI;
66
use foundry_ui::components::warning::WarningMessage;
77
use indoc::formatdoc;
88
use scarb_api::package_matches_version_requirement;
9-
use scarb_api::version::scarb_version;
109
use scarb_metadata::Metadata;
1110
use semver::{Comparator, Op, Version, VersionReq};
1211
use shared::rpc::create_rpc_client;
@@ -16,28 +15,6 @@ use std::env;
1615
use std::sync::Arc;
1716
use url::Url;
1817

19-
pub(crate) fn warn_if_available_gas_used_with_incompatible_scarb_version(
20-
test_targets: &[TestTargetWithResolvedConfig],
21-
ui: &UI,
22-
) -> Result<()> {
23-
for test_target in test_targets {
24-
for case in &test_target.test_cases {
25-
if case
26-
.config
27-
.available_gas
28-
.as_ref().is_some_and(cheatnet::runtime_extensions::forge_config_extension::config::RawAvailableResourceBoundsConfig::is_zero)
29-
&& scarb_version()?.scarb <= Version::new(2, 4, 3)
30-
{
31-
ui.println(&WarningMessage::new("`available_gas` attribute was probably specified when using Scarb ~2.4.3 \
32-
Make sure to use Scarb >=2.4.4"
33-
));
34-
}
35-
}
36-
}
37-
38-
Ok(())
39-
}
40-
4118
pub(crate) async fn warn_if_incompatible_rpc_version(
4219
test_targets: &[TestTargetWithResolvedConfig],
4320
ui: Arc<UI>,

crates/forge/test_utils/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ use scarb_api::version::scarb_version;
1010
use semver::Version;
1111
use std::str::FromStr;
1212

13-
const DEFAULT_ASSERT_MACROS: Version = Version::new(0, 1, 0);
14-
const MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS: Version = Version::new(2, 8, 0);
15-
1613
pub fn tempdir_with_tool_versions() -> Result<assert_fs::TempDir> {
1714
let project_root = get_project_root()?;
1815
let temp_dir = assert_fs::TempDir::new()?;
@@ -21,14 +18,7 @@ pub fn tempdir_with_tool_versions() -> Result<assert_fs::TempDir> {
2118
}
2219

2320
pub fn get_assert_macros_version() -> Result<Version> {
24-
let scarb_version_output = scarb_version()?;
25-
let assert_macros_version =
26-
if scarb_version_output.scarb < MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS {
27-
DEFAULT_ASSERT_MACROS
28-
} else {
29-
scarb_version_output.cairo
30-
};
31-
Ok(assert_macros_version)
21+
Ok(scarb_version()?.cairo)
3222
}
3323

3424
#[must_use]

crates/forge/tests/e2e/requirements.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
use crate::e2e::common::runner::{runner, setup_package};
2-
use indoc::{formatdoc, indoc};
3-
use scarb_api::version::scarb_version;
4-
use semver::Version;
2+
use indoc::indoc;
53
use shared::test_utils::output_assert::assert_stdout_contains;
64

75
#[test]
86
fn happy_path() {
97
let temp = setup_package("simple_package");
108
let output = runner(&temp).arg("check-requirements").assert();
119

12-
let scarb_version = scarb_version().unwrap().scarb;
13-
14-
let rust_check = if scarb_version < Version::new(2, 10, 0) {
15-
indoc! {"
16-
Checking requirements
17-
18-
✅ Rust [..]
19-
"}
20-
} else {
21-
""
22-
};
23-
2410
assert_stdout_contains(
2511
output,
26-
formatdoc! {r"
12+
indoc! {r"
2713
Checking requirements
2814
2915
✅ Scarb [..]
3016
✅ Universal Sierra Compiler [..]
31-
{}
32-
", rust_check},
17+
18+
"},
3319
);
3420
}
3521

crates/native-api/src/lib.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,23 @@ use cairo_lang_starknet_classes::contract_class::ContractClass;
77
use cairo_native::executor::AotContractExecutor;
88
use starknet_api::contract_class::SierraVersion;
99

10-
#[derive(Debug, thiserror::Error)]
11-
pub enum NativeCompilationError {
12-
#[error("Unsupported Sierra version {0}. cairo-native requires version 1.7.0 or later.")]
13-
UnsupportedSierraVersion(String),
14-
}
15-
1610
/// Compiles a given Sierra [`ContractClass`] into an [`AotContractExecutor`] for `cairo-native` execution.
17-
pub fn compile_contract_class(
18-
contract_class: &ContractClass,
19-
) -> Result<AotContractExecutor, NativeCompilationError> {
11+
#[must_use]
12+
pub fn compile_contract_class(contract_class: &ContractClass) -> AotContractExecutor {
2013
let sierra_version = extract_sierra_version(contract_class);
21-
check_sierra_version(&sierra_version)?;
2214

2315
let sierra_program = contract_class
2416
.extract_sierra_program()
2517
.expect("extraction should succeed");
2618

27-
Ok(AotContractExecutor::new(
19+
AotContractExecutor::new(
2820
&sierra_program,
2921
&contract_class.entry_points_by_type,
3022
sierra_version.clone().into(),
3123
cairo_native::OptLevel::Default,
3224
None,
3325
)
34-
.expect("compilation should succeed"))
26+
.expect("compilation should succeed")
3527
}
3628

3729
/// Extracts the Sierra version from the given [`ContractClass`].
@@ -46,15 +38,3 @@ fn extract_sierra_version(contract_class: &ContractClass) -> SierraVersion {
4638
SierraVersion::extract_from_program(&sierra_version_values)
4739
.expect("version extraction should succeed")
4840
}
49-
50-
/// Checks if the given Sierra version is supported by `cairo-native`.
51-
fn check_sierra_version(sierra_version: &SierraVersion) -> Result<(), NativeCompilationError> {
52-
let minimal_supported_version = SierraVersion::new(1, 7, 0);
53-
if sierra_version < &minimal_supported_version {
54-
Err(NativeCompilationError::UnsupportedSierraVersion(
55-
sierra_version.to_string(),
56-
))
57-
} else {
58-
Ok(())
59-
}
60-
}

0 commit comments

Comments
 (0)