-
Notifications
You must be signed in to change notification settings - Fork 240
Add get_current_vm_step
#3868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
franciszekjob
wants to merge
37
commits into
master
Choose a base branch
from
3824-get-current-step
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+312
−1
Open
Add get_current_vm_step
#3868
Changes from 24 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
b55a950
Add `get_current_step`
franciszekjob 8a8a80f
Minor refactor
franciszekjob 073c95f
Fix scarb formatting
franciszekjob 77dd8b9
Add test
franciszekjob 5672903
Update changelog
franciszekjob 53494da
Ignore cheats test of `meta_tx_v0` on `cairo-native`
MKowalski8 5dd5177
Additional check native test
MKowalski8 ab287a4
Enable test on vm
MKowalski8 d3ceb43
Merge branch 'fix-native-meta-tx-v0-tests' into 3824-get-current-step
franciszekjob 7af1256
Merge branch 'master' of https://github.com/foundry-rs/starknet-found…
franciszekjob feed6d5
Apply code review suggestions
franciszekjob 2afa790
Fix linting
franciszekjob 9b7e7a2
Merge branch '3824-get-current-step' of https://github.com/foundry-rs…
franciszekjob 30d1404
Fix lock
franciszekjob b5c1de0
WIP
franciszekjob 57deb3b
Fix resources calculation
franciszekjob a674c9d
Remove print
franciszekjob 8b22af2
Refactor
franciszekjob 7254799
Little refactor
franciszekjob 86b73b0
Remove unused file
franciszekjob 660ec59
Refactor names
franciszekjob 65c6297
Rename `vm_steps_total` -> `total_steps`
franciszekjob c691cb1
Apply code review suggestions
franciszekjob 113bc5f
Apply code review suggestions
franciszekjob cbdd37e
Apply code review suggestions
franciszekjob d84fe26
Rename test
franciszekjob e4b5c13
Merge branch 'master' of https://github.com/foundry-rs/starknet-found…
franciszekjob b94c072
Fix imports
franciszekjob 3a999a8
Remove comments
franciszekjob 48c9e68
Add comment
franciszekjob 7f6a893
Change variable name
franciszekjob 195544a
Add assertions for specific step values in `test_get_current_vm_step`
franciszekjob ec48ca9
Fix import
franciszekjob 4aab58a
Fix test
franciszekjob d17f252
Improve test for `get_current_vm_step`
franciszekjob d37a05b
Fix test
franciszekjob 13f1af4
Reduce explanation
franciszekjob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| use forge_runner::forge_config::ForgeTrackedResource; | ||
| use indoc::indoc; | ||
| use std::path::Path; | ||
| use test_utils::runner::{Contract, assert_passed}; | ||
| use test_utils::running_tests::run_test_case; | ||
| use test_utils::test_case; | ||
|
|
||
| #[test] | ||
| fn test_get_current_step() { | ||
| let test = test_case!( | ||
| indoc!( | ||
| r#" | ||
| use snforge_std::testing::get_current_step; | ||
| use snforge_std::{ContractClassTrait, DeclareResultTrait, declare}; | ||
|
|
||
| #[test] | ||
| fn check_current_step() { | ||
| let step_start = get_current_step(); | ||
|
|
||
| let contract = declare("HelloStarknet").unwrap().contract_class().clone(); | ||
| let _ = contract.deploy(@ArrayTrait::new()).unwrap(); | ||
|
|
||
| let step_end = get_current_step(); | ||
|
|
||
| assert!(step_end > step_start); | ||
| } | ||
| "# | ||
| ), | ||
| Contract::from_code_path( | ||
| "HelloStarknet".to_string(), | ||
| Path::new("tests/data/contracts/hello_starknet.cairo"), | ||
| ) | ||
| .unwrap() | ||
| ); | ||
|
|
||
| let result = run_test_case(&test, ForgeTrackedResource::SierraGas); | ||
|
|
||
| assert_passed(&result); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [package] | ||
| name = "testing_reference" | ||
| version = "0.1.0" | ||
| edition = "2024_07" | ||
|
|
||
| [dependencies] | ||
| starknet = "2.12.0" | ||
|
|
||
| [dev-dependencies] | ||
| snforge_std = { path = "../../../snforge_std" } | ||
|
|
||
| [[target.starknet-contract]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #[starknet::interface] | ||
| pub trait ICounter<TContractState> { | ||
| fn increment(ref self: TContractState); | ||
| } | ||
|
|
||
| #[starknet::contract] | ||
| pub mod Counter { | ||
| use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; | ||
|
|
||
| #[storage] | ||
| struct Storage { | ||
| i: felt252, | ||
| } | ||
|
|
||
| #[constructor] | ||
| fn constructor(ref self: ContractState) { | ||
| self.i.write(0); | ||
| } | ||
|
|
||
| #[abi(embed_v0)] | ||
| impl CounterImpl of super::ICounter<ContractState> { | ||
| fn increment(ref self: ContractState) { | ||
| let current_value = self.i.read(); | ||
| self.i.write(current_value + 1); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use snforge_std::testing::get_current_step; | ||
| use snforge_std::{ContractClassTrait, DeclareResultTrait, declare}; | ||
| use testing_reference::{ICounterSafeDispatcher, ICounterSafeDispatcherTrait}; | ||
|
|
||
| #[feature("safe_dispatcher")] | ||
| fn setup() { | ||
| // Deploy contract | ||
| let (contract_address, _) = declare("Counter") | ||
| .unwrap() | ||
| .contract_class() | ||
| .deploy(@array![]) | ||
| .unwrap(); | ||
|
|
||
| let dispatcher = ICounterSafeDispatcher { contract_address }; | ||
|
|
||
| // Increment counter a few times | ||
| dispatcher.increment(); | ||
| dispatcher.increment(); | ||
| dispatcher.increment(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_setup_steps() { | ||
| let steps_start = get_current_step(); | ||
| setup(); | ||
| let steps_end = get_current_step(); | ||
|
|
||
| // Assert that setup used no more than 100 steps | ||
| assert!(steps_end - steps_start <= 100); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # `testing` Module | ||
|
|
||
| Module containing functions useful for testing. | ||
|
|
||
| ## Functions | ||
|
|
||
| * [`get_current_step`](./testing/get_current_step.md) |
49 changes: 49 additions & 0 deletions
49
docs/src/appendix/snforge-library/testing/get_current_step.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # `get_current_step` | ||
|
|
||
| Gets the current step from Cairo VM during test execution. | ||
|
|
||
| ```rust | ||
| fn get_current_step() -> u32; | ||
| ``` | ||
|
|
||
| ## Example | ||
|
|
||
| Let's consider a simple counter contract that increments a value stored in its storage. | ||
|
|
||
| ```rust | ||
| {{#include ../../../../listings/testing_reference/src/lib.cairo}} | ||
| ``` | ||
|
|
||
| Now, let's define `setup` function which deploys this contract and increments the counter a few times and assert that `setup` function does not exceed a certain number of steps during its execution. This is particularly useful for performance testing and ensuring that our setup logic remains efficient. | ||
|
|
||
| ```rust | ||
| {{#include ../../../../listings/testing_reference/tests/tests.cairo}} | ||
| ``` | ||
|
|
||
| <!-- { "package_name": "testing_reference" } --> | ||
| Let's run the test: | ||
| ```shell | ||
| $ snforge test test_setup_steps | ||
| ``` | ||
|
|
||
| <details> | ||
| <summary>Output:</summary> | ||
|
|
||
| ```shell | ||
| Collected 1 test(s) from testing_reference package | ||
| Running 0 test(s) from src/ | ||
| Running 1 test(s) from tests/ | ||
| [FAIL] testing_reference_integrationtest::tests::test_setup_steps | ||
|
|
||
| Failure data: | ||
| "assertion failed: `steps_end - steps_start <= 100`." | ||
|
|
||
| Tests: 0 passed, 1 failed, 0 ignored, 0 filtered out | ||
|
|
||
| Failures: | ||
| testing_reference_integrationtest::tests::test_setup_steps | ||
| ``` | ||
| </details> | ||
| <br> | ||
|
|
||
| As we can see, the test fails because the `setup` function exceeded the allowed number of steps (100 in this case). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,8 @@ pub mod fuzzable; | |
|
|
||
| pub mod signature; | ||
|
|
||
| pub mod testing; | ||
|
|
||
| pub mod trace; | ||
|
|
||
| #[doc(hidden)] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| use crate::cheatcode::execute_cheatcode_and_deserialize; | ||
|
|
||
| /// Gets the current step from Cairo VM during test execution | ||
| pub fn get_current_step() -> u32 { | ||
| execute_cheatcode_and_deserialize::<'get_current_step', u32>(array![].span()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.