Skip to content

Commit 17b213a

Browse files
authored
Add test for get_available_gas (#3751)
Closes #3499 commit-id:c7694535 --- **Stack**: - #3756 - #3755 - #3752 - #3751⚠️ *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.*
1 parent e342057 commit 17b213a

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

CHANGELOG.md

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

1414
- Minimal supported `Scarb` version is now `2.10.0` (updated from `2.9.1`)
1515

16+
#### Fixed
17+
18+
- [`core::testing::get_available_gas`](https://docs.starknet.io/build/corelib/core-testing-get_available_gas) now works correctly in snforge tests
19+
1620
#### Removed
1721

1822
- Possibility to use `#[available_gas]` with unnamed argument. Use named arguments instead, e.g. `#[available_gas(l2_gas: 5)]`.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use forge_runner::forge_config::ForgeTrackedResource;
2+
use indoc::indoc;
3+
use std::path::Path;
4+
use test_utils::runner::{Contract, assert_passed};
5+
use test_utils::running_tests::run_test_case;
6+
use test_utils::test_case;
7+
8+
#[test]
9+
fn test_get_available_gas() {
10+
let test = test_case!(
11+
indoc!(
12+
r#"
13+
use snforge_std::{declare, DeclareResultTrait, ContractClassTrait};
14+
15+
#[test]
16+
fn check_available_gas() {
17+
let contract = declare("HelloStarknet").unwrap().contract_class().clone();
18+
let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap();
19+
20+
let gas_before = core::testing::get_available_gas();
21+
core::gas::withdraw_gas().unwrap();
22+
23+
starknet::syscalls::call_contract_syscall(
24+
contract_address, selector!("increase_balance"), array![10].span(),
25+
).unwrap();
26+
27+
let gas_after = core::testing::get_available_gas();
28+
core::gas::withdraw_gas().unwrap();
29+
30+
let gas_diff = gas_before - gas_after;
31+
32+
// call_contract syscall: 91_560 gas
33+
// storage_write and storage_read syscalls: 10_000 gas each
34+
let min_expected_gas = 91_560 + 10_000 * 2;
35+
36+
// Check that gas used is above the expected minimum
37+
assert(gas_diff > min_expected_gas, 'Incorrect gas');
38+
39+
// Allow an arbitrary margin of 10_000 gas
40+
assert(min_expected_gas + 10_000 > gas_diff, 'Incorrect gas');
41+
}
42+
"#
43+
),
44+
Contract::from_code_path(
45+
"HelloStarknet".to_string(),
46+
Path::new("tests/data/contracts/hello_starknet.cairo"),
47+
)
48+
.unwrap()
49+
);
50+
51+
let result = run_test_case(&test, ForgeTrackedResource::SierraGas);
52+
53+
assert_passed(&result);
54+
}

crates/forge/tests/integration/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod env;
1616
mod fuzzing;
1717
mod gas;
1818
mod generate_random_felt;
19+
mod get_available_gas;
1920
mod get_class_hash;
2021
mod interact_with_state;
2122
mod l1_handler_executor;

0 commit comments

Comments
 (0)