Skip to content
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

feat: add support for signed integers #2161

Merged
merged 16 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions bin/sozo/src/commands/calldata_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@
}
}

/// Decodes a signed integer into a [`Felt`]
struct SignedIntegerCalldataDecoder;
impl CalldataDecoder for SignedIntegerCalldataDecoder {
fn decode(&self, input: &str) -> DecoderResult<Vec<Felt>> {
if let Ok(value) = input.parse::<i128>() {
Ok(vec![value.into()])

Check warning on line 101 in bin/sozo/src/commands/calldata_decoder.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/calldata_decoder.rs#L101

Added line #L101 was not covered by tests
} else {
Err(CalldataDecoderError::ParseError("Invalid numeric string".to_string()))
}
}
}
Comment on lines +96 to +106
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved: Implementation of SignedIntegerCalldataDecoder and its tests.

The implementation of SignedIntegerCalldataDecoder for decoding signed integers from calldata is correctly done. The addition of comprehensive tests for various signed integer types (i8, i16, i32, i64, i128) ensures the correctness of the decoder.

Consider adopting the suggested refactoring to treat all signed integers as a single Felt regardless of their size, which could simplify the code and improve efficiency.

fn decode(&self, input: &str) -> DecoderResult<Vec<Felt>> {
    if let Ok(value) = input.parse::<i128>() {
        Ok(vec![value.into()])
    } else {
        Err(CalldataDecoderError::ParseError("Invalid numeric string".to_string()))
    }
}

Also applies to: 252-295


/// Decodes a string into a [`Felt`], either from hexadecimal or decimal string.
struct DefaultCalldataDecoder;
impl CalldataDecoder for DefaultCalldataDecoder {
Expand Down Expand Up @@ -150,6 +162,7 @@
"u256" => U256CalldataDecoder.decode(value)?,
"str" => StrCalldataDecoder.decode(value)?,
"sstr" => ShortStrCalldataDecoder.decode(value)?,
"int" => SignedIntegerCalldataDecoder.decode(value)?,

Check warning on line 165 in bin/sozo/src/commands/calldata_decoder.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/calldata_decoder.rs#L165

Added line #L165 was not covered by tests
_ => DefaultCalldataDecoder.decode(item)?,
}
} else {
Expand Down Expand Up @@ -236,6 +249,51 @@
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i8() {
let input = "-64";
let signed_i8: i8 = -64;
let expected = vec![signed_i8.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i16() {
let input = "-12345";
let signed_i16: i16 = -12345;
let expected = vec![signed_i16.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i32() {
let input = "-987654321";
let signed_i32: i32 = -987654321;
let expected = vec![signed_i32.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i64() {
let input = "-1234567890123456789";
let signed_i64: i64 = -1234567890123456789;
let expected = vec![signed_i64.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i128() {
let input = "-123456789012345678901234567890123456";
let signed_i128: i128 = -123456789012345678901234567890123456;
let expected = vec![signed_i128.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_combined_decoders() {
let input = "u256:0x64,str:world,987654,0x123";
Expand All @@ -259,4 +317,12 @@
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_invalid_signed_integer_decoder() {
let input = "-12345abc";
let decoder = SignedIntegerCalldataDecoder;
let result = decoder.decode(input);
assert!(result.is_err());
}
}
1 change: 1 addition & 0 deletions bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct ExecuteArgs {
- u256: A 256-bit unsigned integer.
- sstr: A cairo short string.
- str: A cairo string (ByteArray).
- int: A signed integer.
- no prefix: A cairo felt or any type that fit into one felt.")]
pub calldata: Option<String>,

Expand Down
60 changes: 60 additions & 0 deletions crates/dojo-core/src/database/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,66 @@ impl Introspect_u256 of Introspect<u256> {
}
}

impl Introspect_i8 of Introspect<i8> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i8')
}
}

impl Introspect_i16 of Introspect<i16> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i16')
}
}

impl Introspect_i32 of Introspect<i32> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i32')
}
}

impl Introspect_i64 of Introspect<i64> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i64')
}
}

impl Introspect_i128 of Introspect<i128> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i128')
}
}

impl Introspect_address of Introspect<starknet::ContractAddress> {
fn size() -> Option<usize> {
Option::Some(1)
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use starknet::core::types::Felt;
pub mod event;
pub mod packing;
pub mod primitive;
pub mod primitive_conversion;
pub mod schema;
pub mod storage;
pub mod system;
Expand Down
Loading
Loading