Skip to content

Commit 67014c3

Browse files
committed
Remove cairo-lang-runner dependency from forge runner
commit-id:a1c233ca
1 parent 22e76b6 commit 67014c3

File tree

6 files changed

+115
-16
lines changed

6 files changed

+115
-16
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/conversions/src/felt.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ impl FromShortString<Felt> for Felt {
7979
}
8080
}
8181

82+
#[derive(Debug)]
83+
pub struct ToStrErr;
84+
85+
pub trait ToShortString<T>: Sized {
86+
fn to_short_string(&self) -> Result<String, ToStrErr>;
87+
}
88+
89+
impl ToShortString<Felt> for Felt {
90+
fn to_short_string(&self) -> Result<String, ToStrErr> {
91+
let mut as_string = String::default();
92+
let mut is_end = false;
93+
for byte in self.to_biguint().to_bytes_be() {
94+
if byte == 0 {
95+
is_end = true;
96+
} else if is_end {
97+
return Err(ToStrErr);
98+
} else if byte.is_ascii_graphic() || byte.is_ascii_whitespace() {
99+
as_string.push(byte as char);
100+
} else {
101+
return Err(ToStrErr);
102+
}
103+
}
104+
Ok(as_string)
105+
}
106+
}
107+
82108
pub trait TryInferFormat: Sized {
83109
/// Parses value from `hex string`, `dec string`, `quotted cairo shortstring `and `quotted cairo string`
84110
fn infer_format_and_parse(value: &str) -> Result<Vec<Self>, FromStrError>;
@@ -105,3 +131,80 @@ impl TryInferFormat for Felt {
105131
}
106132
}
107133
}
134+
#[cfg(test)]
135+
mod tests {
136+
use super::*;
137+
138+
#[test]
139+
fn short_string_happy_case() {
140+
let felt = Felt::from_hex("0x616263646566").unwrap();
141+
assert_eq!(felt.to_short_string().unwrap(), "abcdef");
142+
}
143+
144+
#[test]
145+
fn short_string_31_characters() {
146+
let felt =
147+
Felt::from_hex("0x4142434445464748494a4b4c4d4e4f505152535455565758595a3132333435")
148+
.unwrap();
149+
assert_eq!(
150+
felt.to_short_string().unwrap(),
151+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ12345"
152+
);
153+
}
154+
155+
#[test]
156+
fn short_string_too_long() {
157+
let felt =
158+
Felt::from_hex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
159+
.unwrap();
160+
assert!(felt.to_short_string().is_err());
161+
}
162+
163+
#[test]
164+
fn short_string_empty() {
165+
let felt = Felt::from_hex("0x0").unwrap();
166+
assert_eq!(felt.to_short_string().unwrap(), "");
167+
}
168+
169+
#[test]
170+
fn short_string_with_whitespace() {
171+
let felt = Felt::from_hex("0x48656C6C6F20576F726C64").unwrap();
172+
assert_eq!(felt.to_short_string().unwrap(), "Hello World");
173+
}
174+
175+
#[test]
176+
fn short_string_special_chars() {
177+
let felt = Felt::from_hex("0x4021233F2A2B5B5D").unwrap();
178+
assert_eq!(felt.to_short_string().unwrap(), "@!#?*+[]");
179+
}
180+
181+
#[test]
182+
fn short_string_with_numbers() {
183+
let felt = Felt::from_hex("0x313233343536373839").unwrap();
184+
assert_eq!(felt.to_short_string().unwrap(), "123456789");
185+
}
186+
187+
#[test]
188+
fn short_string_non_ascii() {
189+
let felt = Felt::from_hex("0x80").unwrap();
190+
assert!(felt.to_short_string().is_err());
191+
}
192+
193+
#[test]
194+
fn short_string_null_byte() {
195+
let felt = Felt::from_hex("0x00616263").unwrap();
196+
assert_eq!(felt.to_short_string().unwrap(), "abc");
197+
}
198+
199+
#[test]
200+
fn short_string_null_byte_middle() {
201+
let felt = Felt::from_hex("0x61006263").unwrap();
202+
assert!(felt.to_short_string().is_err());
203+
}
204+
205+
#[test]
206+
fn short_string_null_byte_end() {
207+
let felt = Felt::from_hex("0x61626300").unwrap();
208+
assert_eq!(felt.to_short_string().unwrap(), "abc");
209+
}
210+
}

crates/forge-runner/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ edition.workspace = true
77

88
[dependencies]
99
anyhow.workspace = true
10-
cairo-lang-runner.workspace = true
11-
cairo-lang-runnable-utils.workspace = true
1210
cairo-lang-casm.workspace = true
1311
cairo-lang-sierra.workspace = true
1412
cairo-lang-utils.workspace = true

crates/forge-runner/src/running/config_run.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{forge_config::ForgeTrackedResource, package_tests::TestDetails};
88
use anyhow::Result;
99
use blockifier::execution::contract_class::TrackedResource;
1010
use blockifier::state::{cached_state::CachedState, state_api::StateReader};
11-
use cairo_lang_runner::Arg;
1211
use cheatnet::runtime_extensions::forge_config_extension::{
1312
ForgeConfigExtension, config::RawForgeConfig,
1413
};
@@ -107,12 +106,7 @@ pub fn run_config_pass(
107106
},
108107
extended_runtime: StarknetRuntime {
109108
hint_handler: syscall_handler,
110-
// Max gas is no longer set by `create_entry_code_from_params`
111-
// Instead, call to `ExternalHint::WriteRunParam` is added by it, and we need to
112-
// store the gas value to be read by logic handling the hint
113-
// TODO(#2966) we should subtract initial cost of the function from this value to be more exact.
114-
// But as a workaround it should be good enough.
115-
user_args: vec![vec![Arg::Value(Felt::from(i64::MAX))]],
109+
user_args: vec![],
116110
panic_traceback: None,
117111
},
118112
};

crates/forge-runner/src/running/hints.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use cairo_lang_casm::hints::Hint;
2-
use cairo_lang_runner::casm_run::hint_to_hint_params;
3-
use cairo_vm::serde::deserialize_program::HintParams;
2+
use cairo_vm::serde::deserialize_program::{ApTracking, FlowTrackingData, HintParams};
43
use std::collections::HashMap;
54
use universal_sierra_compiler_api::AssembledCairoProgramWithSerde;
65

@@ -27,7 +26,14 @@ pub fn hints_to_params(
2726
*offset,
2827
hints
2928
.iter()
30-
.map(hint_to_hint_params)
29+
.map(|hint| HintParams {
30+
code: hint.representing_string(),
31+
accessible_scopes: vec![],
32+
flow_tracking_data: FlowTrackingData {
33+
ap_tracking: ApTracking::new(),
34+
reference_ids: HashMap::new(),
35+
},
36+
})
3137
.collect::<Vec<HintParams>>(),
3238
)
3339
})

crates/forge-runner/src/test_case_summary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::gas::check_available_gas;
66
use crate::package_tests::with_config_resolved::TestCaseWithResolvedConfig;
77
use crate::running::{RunCompleted, RunStatus};
88
use cairo_annotations::trace_data::VersionedCallTrace as VersionedProfilerCallTrace;
9-
use cairo_lang_runner::short_string::as_cairo_short_string;
109
use camino::Utf8Path;
1110
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::UsedResources;
1211
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
1312
use conversions::byte_array::ByteArray;
13+
use conversions::felt::ToShortString;
1414
use num_traits::Pow;
1515
use shared::utils::build_readable_text;
1616
use starknet_api::execution_resources::GasVector;
@@ -404,7 +404,7 @@ impl TestCaseSummary<Single> {
404404

405405
fn join_short_strings(data: &[Felt]) -> String {
406406
data.iter()
407-
.map(|felt| as_cairo_short_string(felt).unwrap_or_default())
407+
.map(|felt| felt.to_short_string().unwrap_or_default())
408408
.collect::<Vec<String>>()
409409
.join(", ")
410410
}

0 commit comments

Comments
 (0)