Skip to content

Commit

Permalink
Raise steps limit (#1735)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #1652

## Introduced changes

<!-- A brief description of the changes -->

- Added option `--max-n-steps` that allow changing steps limit
(3_000_000, now default)

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
Draggu authored Feb 21, 2024
1 parent 07c3ef8 commit 9f90914
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Added

- contract names to call trace
- `--max-n-steps` argument that allows setting own steps limit

#### Changed

Expand Down
26 changes: 13 additions & 13 deletions crates/forge-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use contracts_data::ContractsData;
use futures::stream::FuturesUnordered;
use futures::StreamExt;

use once_cell::sync::Lazy;
use smol_str::SmolStr;
use trace_data::save_trace_data;

Expand All @@ -42,18 +41,16 @@ mod running;

pub const CACHE_DIR: &str = ".snfoundry_cache";

pub static BUILTINS: Lazy<Vec<&str>> = Lazy::new(|| {
vec![
"Pedersen",
"RangeCheck",
"Bitwise",
"EcOp",
"Poseidon",
"SegmentArena",
"GasBuiltin",
"System",
]
});
pub const BUILTINS: [&str; 8] = [
"Pedersen",
"RangeCheck",
"Bitwise",
"EcOp",
"Poseidon",
"SegmentArena",
"GasBuiltin",
"System",
];

/// Configuration of the test runner
#[derive(Debug, PartialEq)]
Expand All @@ -65,6 +62,7 @@ pub struct RunnerConfig {
pub fuzzer_seed: u64,
pub detailed_resources: bool,
pub save_trace_data: bool,
pub max_n_steps: Option<u32>,
}

impl RunnerConfig {
Expand All @@ -77,6 +75,7 @@ impl RunnerConfig {
fuzzer_seed: u64,
detailed_resources: bool,
save_trace_data: bool,
max_n_steps: Option<u32>,
) -> Self {
Self {
workspace_root,
Expand All @@ -85,6 +84,7 @@ impl RunnerConfig {
fuzzer_seed,
detailed_resources,
save_trace_data,
max_n_steps,
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion crates/forge-runner/src/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use cheatnet::runtime_extensions::forge_runtime_extension::{
};
use cheatnet::state::{BlockInfoReader, CallTrace, CheatnetState, ExtendedStateReader};
use itertools::chain;
use runtime::starknet::context::build_context;
use runtime::starknet::context::{build_context, set_max_steps};
use runtime::{ExtendedRuntime, StarknetRuntime};
use tokio::sync::mpsc::Sender;
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -188,6 +188,11 @@ pub fn run_test_case(
let block_info = state_reader.get_block_info()?;

let mut context = build_context(block_info);

if let Some(max_n_steps) = runner_config.max_n_steps {
set_max_steps(&mut context, max_n_steps);
}

let mut execution_resources = ExecutionResources::default();
let mut cached_state = CachedState::from(state_reader);
let syscall_handler = build_syscall_handler(
Expand Down
20 changes: 18 additions & 2 deletions crates/forge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ struct TestArgs {
/// Save execution traces of all test which have passed and are not fuzz tests
#[arg(long)]
save_trace_data: bool,

/// Number of maximum steps during a single test. For fuzz tests this value is applied to each subtest separately.
#[arg(long)]
max_n_steps: Option<u32>,
}

fn validate_fuzzer_runs_value(val: &str) -> Result<u32> {
Expand All @@ -134,13 +138,15 @@ fn extract_failed_tests(tests_summaries: Vec<TestCrateSummary>) -> Vec<AnyTestCa
.collect()
}

#[allow(clippy::too_many_arguments)]
fn combine_configs(
workspace_root: &Utf8Path,
exit_first: bool,
fuzzer_runs: Option<u32>,
fuzzer_seed: Option<u64>,
detailed_resources: bool,
save_trace_data: bool,
max_n_steps: Option<u32>,
forge_config: &ForgeConfig,
) -> RunnerConfig {
RunnerConfig::new(
Expand All @@ -154,6 +160,7 @@ fn combine_configs(
.unwrap_or_else(|| thread_rng().next_u64()),
detailed_resources || forge_config.detailed_resources,
save_trace_data || forge_config.save_trace_data,
max_n_steps.or(forge_config.max_n_steps),
)
}

Expand Down Expand Up @@ -241,6 +248,7 @@ fn test_workspace(args: TestArgs) -> Result<bool> {
args.fuzzer_seed,
args.detailed_resources,
args.save_trace_data,
args.max_n_steps,
&forge_config,
));
let runner_params =
Expand Down Expand Up @@ -326,6 +334,7 @@ mod tests {
None,
false,
false,
None,
&Default::default(),
);
let config2 = combine_configs(
Expand All @@ -335,6 +344,7 @@ mod tests {
None,
false,
false,
None,
&Default::default(),
);

Expand All @@ -353,6 +363,7 @@ mod tests {
None,
false,
false,
None,
&Default::default(),
);
assert_eq!(
Expand All @@ -364,6 +375,7 @@ mod tests {
config.fuzzer_seed,
false,
false,
None
)
);
}
Expand All @@ -377,6 +389,7 @@ mod tests {
fuzzer_seed: Some(500),
detailed_resources: true,
save_trace_data: true,
max_n_steps: Some(1_000_000),
};
let workspace_root: Utf8PathBuf = Default::default();

Expand All @@ -387,11 +400,12 @@ mod tests {
None,
false,
false,
None,
&config_from_scarb,
);
assert_eq!(
config,
RunnerConfig::new(workspace_root, true, 1234, 500, true, true)
RunnerConfig::new(workspace_root, true, 1234, 500, true, true, Some(1_000_000))
);
}

Expand All @@ -406,6 +420,7 @@ mod tests {
fuzzer_seed: Some(1000),
detailed_resources: false,
save_trace_data: false,
max_n_steps: Some(1234),
};
let config = combine_configs(
&workspace_root,
Expand All @@ -414,12 +429,13 @@ mod tests {
Some(32),
true,
true,
Some(1_000_000),
&config_from_scarb,
);

assert_eq!(
config,
RunnerConfig::new(workspace_root, true, 100, 32, true, true)
RunnerConfig::new(workspace_root, true, 100, 32, true, true, Some(1_000_000))
);
}
}
1 change: 1 addition & 0 deletions crates/forge/src/scarb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ mod tests {
],
fuzzer_runs: None,
fuzzer_seed: None,
max_n_steps: None,
detailed_resources: false,
save_trace_data: false
}
Expand Down
5 changes: 5 additions & 0 deletions crates/forge/src/scarb/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub struct ForgeConfig {
pub save_trace_data: bool,
/// Fork configuration profiles
pub fork: Vec<ForkTarget>,
/// Limit of steps
pub max_n_steps: Option<u32>,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -63,6 +65,8 @@ pub(crate) struct RawForgeConfig {
#[serde(default)]
/// Fork configuration profiles
pub fork: Vec<RawForkTarget>,
/// Limit of steps
pub max_n_steps: Option<u32>,
}

#[derive(Deserialize, Debug, PartialEq, Default, Clone)]
Expand Down Expand Up @@ -130,6 +134,7 @@ impl TryFrom<RawForgeConfig> for ForgeConfig {
detailed_resources: value.detailed_resources,
save_trace_data: value.save_trace_data,
fork: fork_targets,
max_n_steps: value.max_n_steps,
})
}
}
1 change: 1 addition & 0 deletions crates/forge/test_utils/src/running_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn run_test_case(test: &TestCase) -> Vec<TestCrateSummary> {
12345,
false,
false,
None,
)),
Arc::new(RunnerParams::new(
ContractsData::try_from(test.contracts().unwrap()).unwrap(),
Expand Down
12 changes: 12 additions & 0 deletions crates/forge/tests/data/steps/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "steps"
version = "0.1.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet = "2.4.0"
snforge_std = { path = "../../../../../snforge_std" }

[[target.starknet-contract]]
sierra = true
63 changes: 63 additions & 0 deletions crates/forge/tests/data/steps/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#[cfg(test)]
mod tests {
#[test]
// requires 570031 steps
fn steps_570031() {
let mut i = 0;

loop {
if i == 30_000 {
break;
}

i = i + 1;

assert(1 + 1 == 2, 'who knows?');
}
}

#[test]
fn steps_5700031() {
let mut i = 0;

loop {
if i == 300_000 {
break;
}

i = i + 1;

assert(1 + 1 == 2, 'who knows?');
}
}

#[test]
fn steps_2999998() {
let mut i = 0;

loop {
if i == 157_893 {
break;
}

i = i + 1;

assert(1 + 1 == 2, 'who knows?');
}
}

#[test]
fn steps_3000017() {
let mut i = 0;

loop {
if i == 157_894 {
break;
}

i = i + 1;

assert(1 + 1 == 2, 'who knows?');
}
}
}
1 change: 1 addition & 0 deletions crates/forge/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod forking;
mod fuzzing;
mod io_operations;
mod running;
mod steps;
mod trace_data;
mod trace_print;
mod trace_resources;
Expand Down
Loading

0 comments on commit 9f90914

Please sign in to comment.