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

Add encode/decode functionality to hints. #3289

Merged
merged 1 commit into from
Jun 6, 2023
Merged

Conversation

yair-starkware
Copy link
Contributor

@yair-starkware yair-starkware commented Jun 1, 2023

Issues I found:

  1. BigIntAsHex doesn’t implement Encode/Decode and is defined in another crate, so I couldn't implement it myself. Because of this, structs that contain BigIntAsHex also can’t derive Encode/Decode, so I had to implement it manually.
  2. Scale encoding ignores field names. For example, in the following code A & B will have the same encoding:
struct A(u8);
struct B{x: 8};

It is dangerous since we won’t be able to detect things like changing fields order, removing + adding fields, etc.
3. Adding option fields breaks the decoding, so we can't rely on it for adding data without migration:

struct A(u8);
struct B(u8, Option<u8>);
let a = A(1).encode();
B::decode(&mut a.as_slice()).unwrap(); // Panics

This change is Reviewable

Copy link
Contributor

@spapinistarkware spapinistarkware left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 6 files at r1.
Reviewable status: 2 of 6 files reviewed, 3 unresolved discussions (waiting on @dan-starkware and @yair-starkware)


Cargo.toml line 71 at r1 (raw file):

num-traits = "0.2"
once_cell = "1.17.1"
parity-scale-codec = "3.5.0"

Depending on scale is one thing, but are we sure we want to depend on partiy code? Can you explain what exactly this crate has that we need? Regular SCALE doesn't have derive?

Code quote:

parity-scale-codec

crates/cairo-lang-casm/src/operand.rs line 63 at r1 (raw file):

    }
}
// Can't derive the trait because BigIntAsHex doesn't implement Encode.

Why not implement it for BigIntAsHex insteaD?


crates/cairo-lang-casm/src/hints/mod.rs line 14 at r1 (raw file):

// Represents a cairo hint.
// Note: some projects store the encoding of hints. Be mindful about making changes as they are hard
// to detect and may require storage migration.

.

Suggestion:

// Note: Hint encoding should be backwards-compatible. This is an API guarantee.
// For example, new variants should haev new `index`.

Copy link
Contributor Author

@yair-starkware yair-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 2 of 9 files reviewed, 3 unresolved discussions (waiting on @dan-starkware and @spapinistarkware)


Cargo.toml line 71 at r1 (raw file):

Previously, spapinistarkware (Shahar Papini) wrote…

Depending on scale is one thing, but are we sure we want to depend on partiy code? Can you explain what exactly this crate has that we need? Regular SCALE doesn't have derive?

WDYM by regular scale?
There are multiple scale related crates, all depending on parity-scale-codec in their basis, if I understand correctly.


crates/cairo-lang-casm/src/operand.rs line 63 at r1 (raw file):

Previously, spapinistarkware (Shahar Papini) wrote…

Why not implement it for BigIntAsHex insteaD?

Done, thanks.


crates/cairo-lang-casm/src/hints/mod.rs line 14 at r1 (raw file):

Previously, spapinistarkware (Shahar Papini) wrote…

.

Done.

Copy link
Contributor

@spapinistarkware spapinistarkware left a comment

Choose a reason for hiding this comment

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

Reviewed 6 of 7 files at r2, 1 of 1 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @dan-starkware)

@yair-starkware yair-starkware marked this pull request as ready for review June 6, 2023 07:08
@yair-starkware yair-starkware enabled auto-merge June 6, 2023 07:08
@yair-starkware yair-starkware requested a review from orizi June 6, 2023 07:24
@yair-starkware yair-starkware added this pull request to the merge queue Jun 6, 2023
Copy link
Contributor

@spapinistarkware spapinistarkware left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @dan-starkware and @orizi)

Merged via the queue into main with commit d448693 Jun 6, 2023
@orizi orizi deleted the yair/scale_codec branch June 28, 2023 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants