Skip to content

Commit

Permalink
Cairo run fix argument deserialization for numbers larger than u64
Browse files Browse the repository at this point in the history
commit-id:6d3c27da
  • Loading branch information
maciektr committed Jul 4, 2024
1 parent 3ea1add commit 34b4973
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions extensions/scarb-cairo-run/src/deserialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum ArgsError {
#[error("failed to convert slice to array: {0}")]
ArrayFromSlice(#[from] std::array::TryFromSliceError),
#[error("number out of range")]
NumberOutOfRange,
NumberOutOfRange(#[from] starknet_types_core::felt::FromStrError),
#[error("failed to parse arguments: {0}")]
ParseError(#[from] serde_json::Error),
}
Expand Down Expand Up @@ -91,8 +91,8 @@ impl Args {
for arg in iterator {
match arg {
Value::Number(n) => {
let n = n.as_u64().ok_or(ArgsError::NumberOutOfRange)?;
args.push(Arg::Value(Felt252::from(n)));
let n = Felt252::from_str(n.to_string().as_str())?;
args.push(Arg::Value(n));
}
Value::String(n) => {
let n = num_bigint::BigUint::from_str(n)?;
Expand Down
29 changes: 29 additions & 0 deletions extensions/scarb-cairo-run/tests/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ fn valid_number_of_args() {
"#});
}

#[test]
fn can_deserialize_big_number() {
let t = TempDir::new().unwrap();

ProjectBuilder::start()
.name("hello")
.version("0.1.0")
.lib_cairo(indoc! {r#"
fn main(n: felt252) -> felt252 {
n
}
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("cairo-run")
.arg("--")
.arg(r#"[1129815197211541481934112806673325772687763881719835256646064516195041515616]"#)
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
Compiling hello v0.1.0 ([..]/Scarb.toml)
Finished release target(s) in [..]
Running hello
Run completed successfully, returning [1129815197211541481934112806673325772687763881719835256646064516195041515616]
"#});
}

#[test]
fn invalid_number_of_args() {
let t = TempDir::new().unwrap();
Expand Down

0 comments on commit 34b4973

Please sign in to comment.