Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `snforge` now supports [oracles](https://docs.swmansion.com/cairo-oracle/) with `--experimental-oracles` flag.
- `--trace-components` flag to allow selecting which components of the trace to do display. Read more [here](https://foundry-rs.github.io/starknet-foundry/snforge-advanced-features/debugging.html#trace-components)

#### Removed

- Possibility to use `#[available_gas]` with unnamed argument. Use named arguments instead, e.g. `#[available_gas(l2_gas: 5)]`.

### Cast

#### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ use std::{fmt, num::NonZeroU32};
use url::Url;
// available gas

#[derive(Debug, Clone, Copy, CairoDeserialize, PartialEq)]
pub enum RawAvailableGasConfig {
MaxGas(usize),
MaxResourceBounds(RawAvailableResourceBoundsConfig),
}

impl RawAvailableGasConfig {
#[must_use]
pub fn is_zero(&self) -> bool {
match self {
RawAvailableGasConfig::MaxGas(amount) => *amount == 0,
RawAvailableGasConfig::MaxResourceBounds(bounds) => {
bounds.to_gas_vector() == GasVector::ZERO
}
}
}
}

#[derive(Debug, Clone, Copy, CairoDeserialize, PartialEq)]
pub struct RawAvailableResourceBoundsConfig {
pub l1_gas: usize,
Expand All @@ -44,6 +26,11 @@ impl RawAvailableResourceBoundsConfig {
l2_gas: GasAmount(self.l2_gas as u64),
}
}

#[must_use]
pub fn is_zero(&self) -> bool {
self.to_gas_vector() == GasVector::ZERO
}
}

// fork
Expand Down Expand Up @@ -179,7 +166,7 @@ pub struct RawPredeployedContractsConfig {
#[derive(Debug, Default, Clone)]
pub struct RawForgeConfig {
pub fork: Option<RawForkConfig>,
pub available_gas: Option<RawAvailableGasConfig>,
pub available_gas: Option<RawAvailableResourceBoundsConfig>,
pub ignore: Option<RawIgnoreConfig>,
pub should_panic: Option<RawShouldPanicConfig>,
pub fuzzer: Option<RawFuzzerConfig>,
Expand Down
31 changes: 7 additions & 24 deletions crates/forge-runner/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ use blockifier::transaction::objects::HasRelatedFeeType;
use blockifier::utils::u64_from_usize;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::UsedResources;
use cheatnet::runtime_extensions::forge_config_extension::config::RawAvailableGasConfig;
use cheatnet::runtime_extensions::forge_config_extension::config::RawAvailableResourceBoundsConfig;
use cheatnet::state::ExtendedStateReader;
use foundry_ui::UI;
use foundry_ui::components::warning::WarningMessage;
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::transaction::EventContent;
use starknet_api::transaction::fields::GasVectorComputationMode;
Expand Down Expand Up @@ -144,35 +142,20 @@ fn get_state_resources(
}

pub fn check_available_gas(
available_gas: Option<RawAvailableGasConfig>,
available_gas: Option<RawAvailableResourceBoundsConfig>,
summary: TestCaseSummary<Single>,
ui: &UI,
) -> TestCaseSummary<Single> {
match summary {
TestCaseSummary::Passed {
name,
gas_info,
debugging_trace,
..
} if available_gas.is_some_and(|available_gas| match available_gas {
RawAvailableGasConfig::MaxGas(gas) => {
// todo(3109): remove uunnamed argument in available_gas
ui.println(&WarningMessage::new(
"Setting available_gas with unnamed argument is deprecated. \
Consider setting resource bounds (l1_gas, l1_data_gas and l2_gas) explicitly.",
));
// convert resource bounds to classic l1_gas using formula
// l1_gas + l1_data_gas + (l2_gas / 40000)
// because 100 l2_gas = 0.0025 l1_gas
(gas_info.l1_gas + gas_info.l1_data_gas + (gas_info.l2_gas / 40000))
> GasAmount(gas as u64)
}
RawAvailableGasConfig::MaxResourceBounds(bounds) => {
let av_gas = bounds.to_gas_vector();
gas_info.l1_gas > av_gas.l1_gas
|| gas_info.l1_data_gas > av_gas.l1_data_gas
|| gas_info.l2_gas > av_gas.l2_gas
}
} if available_gas.is_some_and(|available_gas| {
let av_gas = available_gas.to_gas_vector();
gas_info.l1_gas > av_gas.l1_gas
|| gas_info.l1_data_gas > av_gas.l1_data_gas
|| gas_info.l2_gas > av_gas.l2_gas
}) =>
{
TestCaseSummary::Failed {
Expand Down
8 changes: 0 additions & 8 deletions crates/forge-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,27 @@ pub fn run_for_test_case(
forge_config: Arc<ForgeConfig>,
versioned_program_path: Arc<Utf8PathBuf>,
send: Sender<()>,
ui: &Arc<UI>,
) -> JoinHandle<Result<AnyTestCaseSummary>> {
if case.config.fuzzer_config.is_none() {
let ui = ui.clone();
tokio::task::spawn(async move {
let res = run_test(
case,
casm_program,
forge_config,
versioned_program_path,
send,
ui,
)
.await?;
Ok(AnyTestCaseSummary::Single(res))
})
} else {
let ui = ui.clone();
tokio::task::spawn(async move {
let res = run_with_fuzzing(
case,
casm_program,
forge_config.clone(),
versioned_program_path,
send,
ui,
)
.await??;
Ok(AnyTestCaseSummary::Fuzzing(res))
Expand All @@ -148,7 +143,6 @@ fn run_with_fuzzing(
forge_config: Arc<ForgeConfig>,
versioned_program_path: Arc<Utf8PathBuf>,
send: Sender<()>,
ui: Arc<UI>,
) -> JoinHandle<Result<TestCaseSummary<Fuzzing>>> {
tokio::task::spawn(async move {
let test_runner_config = &forge_config.test_runner_config;
Expand All @@ -174,7 +168,6 @@ fn run_with_fuzzing(
let mut tasks = FuturesUnordered::new();

for _ in 1..=fuzzer_runs.get() {
let ui = ui.clone();
tasks.push(run_fuzz_test(
case.clone(),
casm_program.clone(),
Expand All @@ -183,7 +176,6 @@ fn run_with_fuzzing(
send.clone(),
fuzzing_send.clone(),
rng.clone(),
ui,
));
}

Expand Down
4 changes: 2 additions & 2 deletions crates/forge-runner/src/package_tests/with_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{TestCase, TestTarget};
use crate::expected_result::{ExpectedPanicValue, ExpectedTestResult};
use cheatnet::runtime_extensions::forge_config_extension::config::{
Expected, RawAvailableGasConfig, RawForgeConfig, RawForkConfig, RawFuzzerConfig,
Expected, RawAvailableResourceBoundsConfig, RawForgeConfig, RawForkConfig, RawFuzzerConfig,
RawShouldPanicConfig,
};
use conversions::serde::serialize::SerializeToFeltVec;
Expand All @@ -14,7 +14,7 @@ pub type TestCaseWithConfig = TestCase<TestCaseConfig>;
/// see [`super::with_config_resolved::TestCaseResolvedConfig`] for more info
#[derive(Debug, Clone)]
pub struct TestCaseConfig {
pub available_gas: Option<RawAvailableGasConfig>,
pub available_gas: Option<RawAvailableResourceBoundsConfig>,
pub ignored: bool,
pub expected_result: ExpectedTestResult,
pub fork_config: Option<RawForkConfig>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::expected_result::ExpectedTestResult;
use anyhow::Result;
use cairo_vm::types::program::Program;
use cheatnet::runtime_extensions::forge_config_extension::config::{
RawAvailableGasConfig, RawFuzzerConfig,
RawAvailableResourceBoundsConfig, RawFuzzerConfig,
};
use starknet_api::block::BlockNumber;
use universal_sierra_compiler_api::AssembledProgramWithDebugInfo;
Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct ResolvedForkConfig {
/// fetches block number
#[derive(Debug, Clone, PartialEq)]
pub struct TestCaseResolvedConfig {
pub available_gas: Option<RawAvailableGasConfig>,
pub available_gas: Option<RawAvailableResourceBoundsConfig>,
pub ignored: bool,
pub expected_result: ExpectedTestResult,
pub fork_config: Option<ResolvedForkConfig>,
Expand Down
22 changes: 2 additions & 20 deletions crates/forge-runner/src/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use cheatnet::state::{
BlockInfoReader, CallTrace, CheatnetState, EncounteredErrors, ExtendedStateReader,
};
use execution::finalize_execution;
use foundry_ui::UI;
use hints::hints_by_representation;
use rand::prelude::StdRng;
use runtime::starknet::context::{build_context, set_max_steps};
Expand Down Expand Up @@ -65,7 +64,6 @@ pub fn run_test(
forge_config: Arc<ForgeConfig>,
versioned_program_path: Arc<Utf8PathBuf>,
send: Sender<()>,
ui: Arc<UI>,
) -> JoinHandle<TestCaseSummary<Single>> {
tokio::task::spawn_blocking(move || {
// Due to the inability of spawn_blocking to be abruptly cancelled,
Expand All @@ -85,17 +83,10 @@ pub fn run_test(
return TestCaseSummary::Interrupted {};
}

extract_test_case_summary(
run_result,
&case,
&forge_config,
&versioned_program_path,
&ui,
)
extract_test_case_summary(run_result, &case, &forge_config, &versioned_program_path)
})
}

#[expect(clippy::too_many_arguments)]
pub(crate) fn run_fuzz_test(
case: Arc<TestCaseWithResolvedConfig>,
casm_program: Arc<AssembledProgramWithDebugInfo>,
Expand All @@ -104,7 +95,6 @@ pub(crate) fn run_fuzz_test(
send: Sender<()>,
fuzzing_send: Sender<()>,
rng: Arc<Mutex<StdRng>>,
ui: Arc<UI>,
) -> JoinHandle<TestCaseSummary<Single>> {
tokio::task::spawn_blocking(move || {
// Due to the inability of spawn_blocking to be abruptly cancelled,
Expand All @@ -128,13 +118,7 @@ pub(crate) fn run_fuzz_test(
return TestCaseSummary::Interrupted {};
}

extract_test_case_summary(
run_result,
&case,
&forge_config,
&versioned_program_path,
&ui,
)
extract_test_case_summary(run_result, &case, &forge_config, &versioned_program_path)
})
}

Expand Down Expand Up @@ -391,7 +375,6 @@ fn extract_test_case_summary(
case: &TestCaseWithResolvedConfig,
forge_config: &ForgeConfig,
versioned_program_path: &Utf8Path,
ui: &UI,
) -> TestCaseSummary<Single> {
let contracts_data = &forge_config.test_runner_config.contracts_data;
let trace_args = &forge_config.output_config.trace_args;
Expand All @@ -403,7 +386,6 @@ fn extract_test_case_summary(
contracts_data,
versioned_program_path,
trace_args,
ui,
),
RunResult::Error(run_error) => {
let mut message = format!(
Expand Down
4 changes: 1 addition & 3 deletions crates/forge-runner/src/test_case_summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::Use
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
use conversions::byte_array::ByteArray;
use conversions::felt::ToShortString;
use foundry_ui::UI;
use num_traits::Pow;
use shared::utils::build_readable_text;
use starknet_api::execution_resources::GasVector;
Expand Down Expand Up @@ -325,7 +324,6 @@ impl TestCaseSummary<Single> {
contracts_data: &ContractsData,
versioned_program_path: &Utf8Path,
trace_args: &TraceArgs,
ui: &UI,
) -> Self {
let name = test_case.name.clone();

Expand Down Expand Up @@ -354,7 +352,7 @@ impl TestCaseSummary<Single> {
)),
debugging_trace,
};
check_available_gas(test_case.config.available_gas, summary, ui)
check_available_gas(test_case.config.available_gas, summary)
}
ExpectedTestResult::Panics(expected_panic_value) => TestCaseSummary::Failed {
name,
Expand Down
1 change: 0 additions & 1 deletion crates/forge/src/run_tests/test_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub async fn run_for_test_target(
forge_config.clone(),
tests.sierra_program_path.clone(),
send.clone(),
&ui.clone(),
));
}

Expand Down
2 changes: 1 addition & 1 deletion crates/forge/src/warn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn warn_if_available_gas_used_with_incompatible_scarb_version(
if case
.config
.available_gas
.as_ref().is_some_and(cheatnet::runtime_extensions::forge_config_extension::config::RawAvailableGasConfig::is_zero)
.as_ref().is_some_and(cheatnet::runtime_extensions::forge_config_extension::config::RawAvailableResourceBoundsConfig::is_zero)
&& ScarbCommand::version().run()?.scarb <= Version::new(2, 4, 3)
{
ui.println(&WarningMessage::new("`available_gas` attribute was probably specified when using Scarb ~2.4.3 \
Expand Down
39 changes: 0 additions & 39 deletions crates/forge/tests/integration/available_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,6 @@ fn correct_available_gas() {
assert_passed(&result);
}

#[test]
fn correct_available_gas_with_unnamed_argument() {
let test = test_utils::test_case!(indoc!(
r"
#[test]
#[available_gas(11)]
fn keccak_cost() {
keccak::keccak_u256s_le_inputs(array![1].span());
}
"
));

let result = run_test_case(&test, ForgeTrackedResource::CairoSteps);

assert_passed(&result);
}

#[test]
fn available_gas_exceeded() {
let test = test_utils::test_case!(indoc!(
Expand All @@ -59,28 +42,6 @@ fn available_gas_exceeded() {
);
}

#[test]
fn available_gas_exceeded_with_unnamed_argument() {
let test = test_utils::test_case!(indoc!(
r"
#[test]
#[available_gas(5)]
fn keccak_cost() {
keccak::keccak_u256s_le_inputs(array![1].span());
}
"
));

let result = run_test_case(&test, ForgeTrackedResource::CairoSteps);

assert_failed(&result);
assert_case_output_contains(
&result,
"keccak_cost",
"Test cost exceeded the available gas. Consumed l1_gas: ~0, l1_data_gas: ~0, l2_gas: ~240000",
);
}

#[test]
fn available_gas_fuzzing() {
let test = test_utils::test_case!(indoc!(
Expand Down
Loading
Loading