Skip to content

Commit 1767475

Browse files
authored
Change default tracked resource to sierra gas (#3801)
<!-- Reference any GitHub issues resolved by this PR --> Related #3660
1 parent 291ef23 commit 1767475

File tree

11 files changed

+41
-46
lines changed

11 files changed

+41
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
- Updated the error message returned when calling a nonexistent method on a contract to better align with the format used by the network
2121
- Oracle support in `snforge` is now stable and no longer requires the `--experimental-oracles` CLI flag
22+
- 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)
23+
- When using the `--detailed-resources` flag, the used Sierra gas key is now shown as `sierra gas` instead of `sierra_gas_consumption`
2224

2325
### Cast
2426

crates/docs/src/validation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ pub fn extract_snippets_from_file(
2828
static GAS_RE: LazyLock<Regex> =
2929
LazyLock::new(|| Regex::new(r"gas: (?:~\d+|\{.+\})").unwrap());
3030
static EXECUTION_RESOURCES_RE: LazyLock<Regex> = LazyLock::new(|| {
31-
Regex::new(r"(steps|memory holes|builtins|syscalls): (\d+|\(.+\))").unwrap()
31+
Regex::new(r"(steps|memory holes|builtins|syscalls|sierra gas): (\d+|\(.+\))")
32+
.unwrap()
3233
});
3334

3435
let output = GAS_RE.replace_all(m.as_str(), "gas: [..]").to_string();

crates/forge-runner/src/forge_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ pub struct ExecutionDataToSave {
4545

4646
#[derive(Clone, Copy, Debug, Default, PartialEq, Deserialize, Eq, ValueEnum)]
4747
pub enum ForgeTrackedResource {
48-
#[default]
4948
CairoSteps,
49+
#[default]
5050
SierraGas,
5151
}
5252

crates/forge-runner/src/messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn format_detailed_resources(
191191
ForgeTrackedResource::SierraGas => {
192192
let mut output = format!(
193193
"
194-
sierra_gas_consumed: {}
194+
sierra gas: {}
195195
syscalls: ({syscalls})",
196196
charged_resources.gas_consumed.0
197197
);

crates/forge/src/scarb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ mod tests {
236236
fuzzer_runs: None,
237237
fuzzer_seed: None,
238238
max_n_steps: None,
239-
tracked_resource: ForgeTrackedResource::CairoSteps,
239+
tracked_resource: ForgeTrackedResource::SierraGas,
240240
detailed_resources: false,
241241
save_trace_data: false,
242242
build_profile: false,
@@ -505,7 +505,7 @@ mod tests {
505505
fuzzer_runs: None,
506506
fuzzer_seed: None,
507507
max_n_steps: None,
508-
tracked_resource: ForgeTrackedResource::CairoSteps,
508+
tracked_resource: ForgeTrackedResource::SierraGas,
509509
detailed_resources: false,
510510
save_trace_data: false,
511511
build_profile: false,

crates/forge/tests/e2e/fuzzing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ fn generate_arg_cheatcode() {
285285
Failure data:
286286
"`generate_arg` cheatcode: `min_value` must be <= `max_value`, provided values after deserialization: 101 and 100"
287287
288-
[PASS] fuzzing_integrationtest::generate_arg::use_generate_arg_outside_fuzzer (l1_gas: ~0, l1_data_gas: ~0, l2_gas: ~40000)
288+
[PASS] fuzzing_integrationtest::generate_arg::use_generate_arg_outside_fuzzer (l1_gas: ~0, l1_data_gas: ~0, l2_gas: ~[..])
289289
Tests: 1 passed, 1 failed, 0 ignored, 23 filtered out
290290
"#},
291291
);

crates/forge/tests/e2e/running.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,12 @@ fn incompatible_snforge_std_version_error() {
986986
}
987987

988988
#[test]
989-
fn detailed_resources_flag() {
989+
fn detailed_resources_flag_cairo_steps() {
990990
let temp = setup_package("erc20_package");
991991
let output = test_runner(&temp)
992992
.arg("--detailed-resources")
993+
.arg("--tracked-resource")
994+
.arg("cairo-steps")
993995
.assert()
994996
.success();
995997

@@ -1014,12 +1016,10 @@ fn detailed_resources_flag() {
10141016
}
10151017

10161018
#[test]
1017-
fn detailed_resources_flag_sierra_gas() {
1019+
fn detailed_resources_flag() {
10181020
let temp = setup_package("erc20_package");
10191021
let output = test_runner(&temp)
10201022
.arg("--detailed-resources")
1021-
.arg("--tracked-resource")
1022-
.arg("sierra-gas")
10231023
.assert()
10241024
.success();
10251025

@@ -1035,7 +1035,7 @@ fn detailed_resources_flag_sierra_gas() {
10351035
Running 0 test(s) from src/
10361036
Running 1 test(s) from tests/
10371037
[PASS] erc20_package_integrationtest::test_complex::complex[..]
1038-
sierra_gas_consumed: [..]
1038+
sierra gas: [..]
10391039
syscalls: ([..])
10401040
Tests: 1 passed, 0 failed, 0 ignored, 0 filtered out
10411041
"},
@@ -1061,7 +1061,7 @@ fn detailed_resources_mixed_resources() {
10611061
Collected 1 test(s) from forking package
10621062
Running 1 test(s) from src/
10631063
[PASS] forking::tests::test_track_resources [..]
1064-
sierra_gas_consumed: [..]
1064+
sierra gas: [..]
10651065
syscalls: ([..])
10661066
steps: [..]
10671067
memory holes: [..]

crates/forge/tests/e2e/trace_resources.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,37 @@ use std::{collections::HashMap, fs};
1515

1616
#[test]
1717
fn trace_resources_call() {
18-
assert_resources_for_test("test_call", check_call);
18+
assert_vm_resources_for_test("test_call", check_call);
1919
}
2020

2121
#[test]
2222
fn trace_resources_deploy() {
23-
assert_resources_for_test("test_deploy", check_deploy);
23+
assert_vm_resources_for_test("test_deploy", check_deploy);
2424
}
2525

2626
#[test]
2727
fn trace_resources_l1_handler() {
28-
assert_resources_for_test("test_l1_handler", check_l1_handler);
28+
assert_vm_resources_for_test("test_l1_handler", check_l1_handler);
2929
}
3030

3131
#[test]
3232
fn trace_resources_lib_call() {
33-
assert_resources_for_test("test_lib_call", check_libcall);
33+
assert_vm_resources_for_test("test_lib_call", check_libcall);
3434
}
3535

3636
#[test]
3737
#[ignore = "TODO(#1657)"]
3838
fn trace_resources_failed_call() {
39-
assert_resources_for_test("test_failed_call", |_| ());
39+
assert_vm_resources_for_test("test_failed_call", |_| ());
4040
}
4141

4242
#[test]
4343
#[ignore = "TODO(#1657)"]
4444
fn trace_resources_failed_lib_call() {
45-
assert_resources_for_test("test_failed_lib_call", |_| ());
45+
assert_vm_resources_for_test("test_failed_lib_call", |_| ());
4646
}
4747

48-
fn assert_resources_for_test(
48+
fn assert_vm_resources_for_test(
4949
test_name: &str,
5050
check_not_easily_unifiable_syscalls: fn(&ProfilerCallTrace),
5151
) {
@@ -54,6 +54,8 @@ fn assert_resources_for_test(
5454
test_runner(&temp)
5555
.arg(test_name)
5656
.arg("--save-trace-data")
57+
.arg("--tracked-resource")
58+
.arg("cairo-steps")
5759
.assert()
5860
.success();
5961

docs/src/appendix/snforge/test.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ Enabling this flag will slow down the compilation process, but the built contrac
118118
## `--tracked-resource`
119119

120120
Set tracked resource for test execution. Impacts overall test gas cost. Valid values:
121-
- `cairo-steps` (default): track cairo steps, uses vm `ExecutionResources` (steps, builtins, memory holes) to describe resources consumed by the test.
122-
- `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.
123-
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)
121+
- `sierra-gas` (default): track sierra gas, uses cairo native `CallExecution` (sierra gas consumption) to describe computation resources consumed by the test.
122+
- `cairo-steps`: track cairo steps, uses vm `ExecutionResources` (steps, builtins, memory holes) to describe resources consumed by the test.
123+
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)
124124

125125
## `-P`, `--profile` `<PROFILE>`
126126
Specify the profile to use by name.

docs/src/testing/gas-and-resource-estimation.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Gas and VM Resources Estimation
22

3-
`snforge` supports gas and other VM resources estimation for each individual test case.
3+
`snforge` supports gas and VM resources estimation for each individual test case.
44

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

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

2020
### Fuzzed Tests
2121

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

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

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

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

57-
[PASS] hello_starknet_integrationtest::test_contract::test_increase_balance (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~480000)
58-
steps: 4535
59-
memory holes: 15
60-
builtins: (range_check: 95, pedersen: 7)
55+
[PASS] hello_starknet_integrationtest::test_contract::test_increase_balance (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~511980)
56+
sierra gas: 511980
6157
syscalls: (CallContract: 3, StorageRead: 3, Deploy: 1, StorageWrite: 1)
6258

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

69-
This displays the resources used by the VM during the test execution.
70-
7165
## Analyzing the results
7266
Normally in transaction receipt (or block explorer transaction details), you would see some additional OS resources
7367
that starknet-foundry does not include for a test (since it's not a normal transaction per-se):

0 commit comments

Comments
 (0)