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

Incorrect Serialization of Struct Return Values in Cairo1 Run #1564

Closed
raphaelDkhn opened this issue Jan 12, 2024 · 2 comments · Fixed by #1575
Closed

Incorrect Serialization of Struct Return Values in Cairo1 Run #1564

raphaelDkhn opened this issue Jan 12, 2024 · 2 comments · Fixed by #1575
Labels
bug Something isn't working cairo1run

Comments

@raphaelDkhn
Copy link

raphaelDkhn commented Jan 12, 2024

Describe the bug

I'm encountering an issue with struct serialization when running a Cairo1 program with cairo1-run. When a struct is returned from the program, the serialized return does not match the expected output. Here's the Cairo program I'm testing:

Here is the cairo program I'm testing:

use core::array::SpanTrait;

#[derive(Copy, Drop)]
struct Tensor {
    shape: Span<u32>,
    data: Span<u32>
}

fn main() -> Tensor {
    let tensor = Tensor {
    shape: array![2, 2].span(),
    data: array![1, 2, 3, 4].span(),
    };

    tensor
}

After running cargo run ../cairo_programs/cairo-1-programs/tensor.cairo --layout all_cairo --args '[2 2] [1 2 3 4]' it returns:

Return values : [5:0, 5:2, 6:0, 6:4]

However, I expected it to return:

Return values : 
[
    2, // Total number of elements in 'shape'
    2, // First element of 'shape'
    2, // Second element of 'shape'
    4, // Total number of elements in 'data'
    1, // First element of 'data'
    2, // Second element of 'data'
    3, // Third element of 'data'
    4  // Fourth element of 'data'
]

The discrepancy suggests an issue with how the struct is being serialized upon return.

What version/commit are you on?
Current Version/Commit: Branch cairo-1-input-args
From this PR: #1551

@raphaelDkhn raphaelDkhn added the bug Something isn't working label Jan 12, 2024
@igaray igaray added this to Starknet Jan 15, 2024
@igaray igaray moved this to Todo in Starknet Jan 15, 2024
@fmoletta
Copy link
Contributor

Hello, we can definitely fetch the arrays from memory when fetching the return values to show the full arrays instead of their start and end addresses, but I find the expected format a bit confusing. I would suggest serializing it as [2 2] [1 2 3 4] instead to match the convention used for input args and we don't mix up the amount of elements on the array with the actual array elements

@raphaelDkhn
Copy link
Author

raphaelDkhn commented Jan 15, 2024

Hello, we can definitely fetch the arrays from memory when fetching the return values to show the full arrays instead of their start and end addresses, but I find the expected format a bit confusing. I would suggest serializing it as [2 2] [1 2 3 4] instead to match the convention used for input args and we don't mix up the amount of elements on the array with the actual array elements

I was following the same serialization than Cairo (if I'm not mistaken). But your suggestion sounds good as well.

@fmoletta fmoletta moved this from Todo to In Progress in Starknet Jan 18, 2024
@fmoletta fmoletta moved this from In Progress to In Review in Starknet Jan 18, 2024
@fmoletta fmoletta linked a pull request Jan 18, 2024 that will close this issue
@github-project-automation github-project-automation bot moved this from In Review to Done in Starknet Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cairo1run
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants