Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Updated the error message returned when calling a nonexistent method on a contract to better align with the format used by the network
- Oracle support in `snforge` is now stable and no longer requires the `--experimental-oracles` CLI flag
- The default tracked resource is now Sierra gas, so gas reporting results may differ compared to previous versions. For more information refer to the [documentation](https://foundry-rs.github.io/starknet-foundry/testing/gas-and-resource-estimation.html)
- When using the `--detailed-resources` flag, the used Sierra gas key is now shown as `sierra gas` instead of `sierra_gas_consumption`

### Cast

Expand Down
3 changes: 2 additions & 1 deletion crates/docs/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub fn extract_snippets_from_file(
static GAS_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"gas: (?:~\d+|\{.+\})").unwrap());
static EXECUTION_RESOURCES_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(steps|memory holes|builtins|syscalls): (\d+|\(.+\))").unwrap()
Regex::new(r"(steps|memory holes|builtins|syscalls|sierra gas): (\d+|\(.+\))")
.unwrap()
});

let output = GAS_RE.replace_all(m.as_str(), "gas: [..]").to_string();
Expand Down
2 changes: 1 addition & 1 deletion crates/forge-runner/src/forge_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub struct ExecutionDataToSave {

#[derive(Clone, Copy, Debug, Default, PartialEq, Deserialize, Eq, ValueEnum)]
pub enum ForgeTrackedResource {
#[default]
CairoSteps,
#[default]
SierraGas,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/forge-runner/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn format_detailed_resources(
ForgeTrackedResource::SierraGas => {
let mut output = format!(
"
sierra_gas_consumed: {}
sierra gas: {}
syscalls: ({syscalls})",
charged_resources.gas_consumed.0
);
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/src/scarb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ mod tests {
fuzzer_runs: None,
fuzzer_seed: None,
max_n_steps: None,
tracked_resource: ForgeTrackedResource::CairoSteps,
tracked_resource: ForgeTrackedResource::SierraGas,
detailed_resources: false,
save_trace_data: false,
build_profile: false,
Expand Down Expand Up @@ -505,7 +505,7 @@ mod tests {
fuzzer_runs: None,
fuzzer_seed: None,
max_n_steps: None,
tracked_resource: ForgeTrackedResource::CairoSteps,
tracked_resource: ForgeTrackedResource::SierraGas,
detailed_resources: false,
save_trace_data: false,
build_profile: false,
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/e2e/fuzzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fn generate_arg_cheatcode() {
Failure data:
"`generate_arg` cheatcode: `min_value` must be <= `max_value`, provided values after deserialization: 101 and 100"

[PASS] fuzzing_integrationtest::generate_arg::use_generate_arg_outside_fuzzer (l1_gas: ~0, l1_data_gas: ~0, l2_gas: ~40000)
[PASS] fuzzing_integrationtest::generate_arg::use_generate_arg_outside_fuzzer (l1_gas: ~0, l1_data_gas: ~0, l2_gas: ~[..])
Tests: 1 passed, 1 failed, 0 ignored, 23 filtered out
"#},
);
Expand Down
12 changes: 6 additions & 6 deletions crates/forge/tests/e2e/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,10 +986,12 @@ fn incompatible_snforge_std_version_error() {
}

#[test]
fn detailed_resources_flag() {
fn detailed_resources_flag_cairo_steps() {
let temp = setup_package("erc20_package");
let output = test_runner(&temp)
.arg("--detailed-resources")
.arg("--tracked-resource")
.arg("cairo-steps")
.assert()
.success();

Expand All @@ -1014,12 +1016,10 @@ fn detailed_resources_flag() {
}

#[test]
fn detailed_resources_flag_sierra_gas() {
fn detailed_resources_flag() {
let temp = setup_package("erc20_package");
let output = test_runner(&temp)
.arg("--detailed-resources")
.arg("--tracked-resource")
.arg("sierra-gas")
.assert()
.success();

Expand All @@ -1035,7 +1035,7 @@ fn detailed_resources_flag_sierra_gas() {
Running 0 test(s) from src/
Running 1 test(s) from tests/
[PASS] erc20_package_integrationtest::test_complex::complex[..]
sierra_gas_consumed: [..]
sierra gas: [..]
syscalls: ([..])
Tests: 1 passed, 0 failed, 0 ignored, 0 filtered out
"},
Expand All @@ -1061,7 +1061,7 @@ fn detailed_resources_mixed_resources() {
Collected 1 test(s) from forking package
Running 1 test(s) from src/
[PASS] forking::tests::test_track_resources [..]
sierra_gas_consumed: [..]
sierra gas: [..]
syscalls: ([..])
steps: [..]
memory holes: [..]
Expand Down
16 changes: 9 additions & 7 deletions crates/forge/tests/e2e/trace_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@ use std::{collections::HashMap, fs};

#[test]
fn trace_resources_call() {
assert_resources_for_test("test_call", check_call);
assert_vm_resources_for_test("test_call", check_call);
}

#[test]
fn trace_resources_deploy() {
assert_resources_for_test("test_deploy", check_deploy);
assert_vm_resources_for_test("test_deploy", check_deploy);
}

#[test]
fn trace_resources_l1_handler() {
assert_resources_for_test("test_l1_handler", check_l1_handler);
assert_vm_resources_for_test("test_l1_handler", check_l1_handler);
}

#[test]
fn trace_resources_lib_call() {
assert_resources_for_test("test_lib_call", check_libcall);
assert_vm_resources_for_test("test_lib_call", check_libcall);
}

#[test]
#[ignore = "TODO(#1657)"]
fn trace_resources_failed_call() {
assert_resources_for_test("test_failed_call", |_| ());
assert_vm_resources_for_test("test_failed_call", |_| ());
}

#[test]
#[ignore = "TODO(#1657)"]
fn trace_resources_failed_lib_call() {
assert_resources_for_test("test_failed_lib_call", |_| ());
assert_vm_resources_for_test("test_failed_lib_call", |_| ());
}

fn assert_resources_for_test(
fn assert_vm_resources_for_test(
test_name: &str,
check_not_easily_unifiable_syscalls: fn(&ProfilerCallTrace),
) {
Expand All @@ -54,6 +54,8 @@ fn assert_resources_for_test(
test_runner(&temp)
.arg(test_name)
.arg("--save-trace-data")
.arg("--tracked-resource")
.arg("cairo-steps")
.assert()
.success();

Expand Down
6 changes: 3 additions & 3 deletions docs/src/appendix/snforge/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ Enabling this flag will slow down the compilation process, but the built contrac
## `--tracked-resource`

Set tracked resource for test execution. Impacts overall test gas cost. Valid values:
- `cairo-steps` (default): track cairo steps, uses vm `ExecutionResources` (steps, builtins, memory holes) to describe resources consumed by the test.
- `sierra-gas` (sierra 1.7.0+ is required): track sierra gas, uses cairo native `CallExecution` (sierra gas consumption) to describe computation resources consumed by the test.
To learn more about fee calculation formula (and an impact of tracking sierra gas on it) please consult [starknet docs](https://docs.starknet.io/learn/protocol/fees#overall-fee)
- `sierra-gas` (default): track sierra gas, uses cairo native `CallExecution` (sierra gas consumption) to describe computation resources consumed by the test.
- `cairo-steps`: track cairo steps, uses vm `ExecutionResources` (steps, builtins, memory holes) to describe resources consumed by the test.
To learn more about fee calculation formula (and the impact of tracking sierra gas on it) please consult [starknet docs](https://docs.starknet.io/learn/protocol/fees#overall-fee)

## `-P`, `--profile` `<PROFILE>`
Specify the profile to use by name.
Expand Down
26 changes: 10 additions & 16 deletions docs/src/testing/gas-and-resource-estimation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Gas and VM Resources Estimation

`snforge` supports gas and other VM resources estimation for each individual test case.
`snforge` supports gas and VM resources estimation for each individual test case.

It does not calculate the final transaction fee, for details on how fees are calculated,
please refer to fee mechanism in [Starknet documentation](https://docs.starknet.io/learn/protocol/fees#overview).
Expand All @@ -14,8 +14,8 @@ When the test passes with no errors, estimated gas is displayed this way:
[PASS] tests::simple_test (l1_gas: ~1, l1_data_gas: ~1, l2_gas: ~1)
```

This gas calculation is based on the estimated VM resources (that you can [display additionally on demand](#usage)),
deployed contracts, storage updates, events and l1 <> l2 messages.
This gas calculation is based on the collected Sierra gas or VM resources (that you can [display additionally on demand](#usage)),
storage updates, events and l1 <> l2 messages.

### Fuzzed Tests

Expand All @@ -32,11 +32,11 @@ While using the fuzzing feature additional gas statistics will be displayed:
## Resources Estimation

It is possible to enable more detailed breakdown of resources, on which the gas calculations are based on.
Depending on `--tracked-resource`, vm resources or sierra gas will be displayed.
To learn more about tracked resource flag, see [--tracked-resource](../appendix/snforge/test.md#--tracked-resource).
Depending on `--tracked-resource`, vm resources or sierra gas will be displayed (by default, Sierra gas is used).
To learn more about the tracked resource flag, see [--tracked-resource](../appendix/snforge/test.md#--tracked-resource).

### Usage
In order to run tests with this feature, run the `test` command with the appropriate flag:
In order to run tests with this feature, run the `test` command with the `--detailed-resources` flag:

```shell
$ snforge test --detailed-resources
Expand All @@ -48,16 +48,12 @@ $ snforge test --detailed-resources
```shell
Collected 2 test(s) from hello_starknet package
Running 2 test(s) from tests/
[PASS] hello_starknet_integrationtest::test_contract::test_cannot_increase_balance_with_zero_value (l1_gas: ~0, l1_data_gas: ~96, l2_gas: ~360000)
steps: 3405
memory holes: 22
builtins: (range_check: 77, pedersen: 7)
[PASS] hello_starknet_integrationtest::test_contract::test_cannot_increase_balance_with_zero_value (l1_gas: ~0, l1_data_gas: ~96, l2_gas: ~406680)
sierra gas: 406680
syscalls: (CallContract: 2, StorageRead: 1, Deploy: 1)

[PASS] hello_starknet_integrationtest::test_contract::test_increase_balance (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~480000)
steps: 4535
memory holes: 15
builtins: (range_check: 95, pedersen: 7)
[PASS] hello_starknet_integrationtest::test_contract::test_increase_balance (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~511980)
sierra gas: 511980
syscalls: (CallContract: 3, StorageRead: 3, Deploy: 1, StorageWrite: 1)

Running 0 test(s) from src/
Expand All @@ -66,8 +62,6 @@ Tests: 2 passed, 0 failed, 0 ignored, 0 filtered out
</details>
<br>

This displays the resources used by the VM during the test execution.

## Analyzing the results
Normally in transaction receipt (or block explorer transaction details), you would see some additional OS resources
that starknet-foundry does not include for a test (since it's not a normal transaction per-se):
Expand Down
12 changes: 4 additions & 8 deletions docs/src/testing/running-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,12 @@ $ snforge test --detailed-resources
```shell
Collected 2 test(s) from hello_starknet package
Running 2 test(s) from tests/
[PASS] hello_starknet_integrationtest::test_contract::test_cannot_increase_balance_with_zero_value (l1_gas: ~0, l1_data_gas: ~96, l2_gas: ~360000)
steps: 3405
memory holes: 22
builtins: (range_check: 77, pedersen: 7)
[PASS] hello_starknet_integrationtest::test_contract::test_cannot_increase_balance_with_zero_value (l1_gas: ~0, l1_data_gas: ~96, l2_gas: ~406680)
sierra gas: 406680
syscalls: (CallContract: 2, StorageRead: 1, Deploy: 1)

[PASS] hello_starknet_integrationtest::test_contract::test_increase_balance (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~480000)
steps: 4535
memory holes: 15
builtins: (range_check: 95, pedersen: 7)
[PASS] hello_starknet_integrationtest::test_contract::test_increase_balance (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~511980)
sierra gas: 511980
syscalls: (CallContract: 3, StorageRead: 3, Deploy: 1, StorageWrite: 1)

Running 0 test(s) from src/
Expand Down
Loading