-
Notifications
You must be signed in to change notification settings - Fork 236
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(AztecMacro): Autogenerate constants for struct field slots for better note-getter options #3011
Comments
Is this the only use case for this feature @spalladino? I was discussing it with @sirasistant and @rahul-kothari and we found a pretty problematic footgun if implemented naively. Since we allow users to do things like this: struct TrickyNote {
a_bool: bool,
another_bool: bool
}
impl Serialize<1> for TrickyNote {
fn serialize(self) -> [Field; 1] {
[self.a_bool as Field + (self.another_bool as Field) * 10]
}
}
impl Deserialize<1> for TrickyNote {
fn deserialize(fields: [Field; 1]) -> Self {
Self {
a_bool: fields[0] == 1,
another_bool: fields[0] / 10 == 1
}
}
} And struct Storage {
notes: Set<TrickyNote>
}
fn main() {
// oh noes
let what_to_look_for = "i_dont_know";
let options = NoteGetterOptions::new().select(
TrickyNote::get_index_for("what?"),
what_to_look_for,
Option::none()
);
} Granted it's a convoluted case, but it shows the disconnect between the struct shape and the serialized form. If this is the only usage of such a feature, I think we need a better solution. |
Yep, this is the only use case for this feature (that I know of). Agree that with your scenario this is a potential footgun, but the current way of doing things is not good either. Maybe this is a hint that note serialization should be handled automatically by the system..? |
It would be pretty easy to create a canonical (de)serialization impl for notes via macro that ensures 1 struct field -> 1 serialization field, but it could be both less efficient and add a significant amount of "magic" to the platform... |
I'm not too familiar with Rust, but maybe this could be solved if we had a Still, this doesn't solve the |
As a new user this doesn't feel like magic: on the contrary, I'd greatly appreciate not having to deal with all of these details (until I absolutely have to). I'd compare this to how Solidity doesn't have you worry about how structs are passed in external calls, received, hashed, emitted in logs, etc. |
Partially addresses: #4519 (moved autogeneration to the macro, even if not incremental) Closes: #3011 Added the `#[aztec(note)]` attribute, which automatically implements most of the `NoteInterface` trait in a struct marked as such, plus several utilities. Even if this adds a fair share of "magic" to the note implementation logic, it is structured in a way that it's hopefully easy to follow, including meaningful errors attached to the correct span during the process. ![Screenshot 2024-03-14 at 14 59 07](https://github.com/AztecProtocol/aztec-packages/assets/5404052/84a3d6e4-e346-4cfe-93eb-ec317632f344) Hey you! Implement the trait! ![Screenshot 2024-03-14 at 14 46 39](https://github.com/AztecProtocol/aztec-packages/assets/5404052/bebfb3dd-c178-44d0-b9bc-005b5c9f0f38) But only the meat and potatoes though. As long as the user doesn't want to do any custom stuff, `get_header`, `set_header`, `compute_note_content_hash`, `get_note_type_id`, `serialize_content` and `deserialize_content` get automatically implemented. Any combination of them can be overridden by the developer though. A metadata struct is also added, which takes the following form: ```rust struct CardNote { points: FieldSelector, randomness: FieldSelector, owner: FieldSelector, } ``` This is used to implement a `properties()` function, which in turn can be used in conjunction with the `NoteGetterOptions.select` and `.sort` <img width="697" alt="Screenshot 2024-03-18 at 15 27 27" src="https://github.com/AztecProtocol/aztec-packages/assets/5404052/5da531b3-0b7f-4cf9-9908-300ff8d98c6d">
…ecProtocol/aztec-packages#4508) Partially addresses: AztecProtocol/aztec-packages#4519 (moved autogeneration to the macro, even if not incremental) Closes: AztecProtocol/aztec-packages#3011 Added the `#[aztec(note)]` attribute, which automatically implements most of the `NoteInterface` trait in a struct marked as such, plus several utilities. Even if this adds a fair share of "magic" to the note implementation logic, it is structured in a way that it's hopefully easy to follow, including meaningful errors attached to the correct span during the process. ![Screenshot 2024-03-14 at 14 59 07](https://github.com/AztecProtocol/aztec-packages/assets/5404052/84a3d6e4-e346-4cfe-93eb-ec317632f344) Hey you! Implement the trait! ![Screenshot 2024-03-14 at 14 46 39](https://github.com/AztecProtocol/aztec-packages/assets/5404052/bebfb3dd-c178-44d0-b9bc-005b5c9f0f38) But only the meat and potatoes though. As long as the user doesn't want to do any custom stuff, `get_header`, `set_header`, `compute_note_content_hash`, `get_note_type_id`, `serialize_content` and `deserialize_content` get automatically implemented. Any combination of them can be overridden by the developer though. A metadata struct is also added, which takes the following form: ```rust struct CardNote { points: FieldSelector, randomness: FieldSelector, owner: FieldSelector, } ``` This is used to implement a `properties()` function, which in turn can be used in conjunction with the `NoteGetterOptions.select` and `.sort` <img width="697" alt="Screenshot 2024-03-18 at 15 27 27" src="https://github.com/AztecProtocol/aztec-packages/assets/5404052/5da531b3-0b7f-4cf9-9908-300ff8d98c6d">
Partially addresses: AztecProtocol/aztec-packages#4519 (moved autogeneration to the macro, even if not incremental) Closes: AztecProtocol/aztec-packages#3011 Added the `#[aztec(note)]` attribute, which automatically implements most of the `NoteInterface` trait in a struct marked as such, plus several utilities. Even if this adds a fair share of "magic" to the note implementation logic, it is structured in a way that it's hopefully easy to follow, including meaningful errors attached to the correct span during the process. ![Screenshot 2024-03-14 at 14 59 07](https://github.com/AztecProtocol/aztec-packages/assets/5404052/84a3d6e4-e346-4cfe-93eb-ec317632f344) Hey you! Implement the trait! ![Screenshot 2024-03-14 at 14 46 39](https://github.com/AztecProtocol/aztec-packages/assets/5404052/bebfb3dd-c178-44d0-b9bc-005b5c9f0f38) But only the meat and potatoes though. As long as the user doesn't want to do any custom stuff, `get_header`, `set_header`, `compute_note_content_hash`, `get_note_type_id`, `serialize_content` and `deserialize_content` get automatically implemented. Any combination of them can be overridden by the developer though. A metadata struct is also added, which takes the following form: ```rust struct CardNote { points: FieldSelector, randomness: FieldSelector, owner: FieldSelector, } ``` This is used to implement a `properties()` function, which in turn can be used in conjunction with the `NoteGetterOptions.select` and `.sort` <img width="697" alt="Screenshot 2024-03-18 at 15 27 27" src="https://github.com/AztecProtocol/aztec-packages/assets/5404052/5da531b3-0b7f-4cf9-9908-300ff8d98c6d">
Partially addresses: AztecProtocol/aztec-packages#4519 (moved autogeneration to the macro, even if not incremental) Closes: AztecProtocol/aztec-packages#3011 Added the `#[aztec(note)]` attribute, which automatically implements most of the `NoteInterface` trait in a struct marked as such, plus several utilities. Even if this adds a fair share of "magic" to the note implementation logic, it is structured in a way that it's hopefully easy to follow, including meaningful errors attached to the correct span during the process. ![Screenshot 2024-03-14 at 14 59 07](https://github.com/AztecProtocol/aztec-packages/assets/5404052/84a3d6e4-e346-4cfe-93eb-ec317632f344) Hey you! Implement the trait! ![Screenshot 2024-03-14 at 14 46 39](https://github.com/AztecProtocol/aztec-packages/assets/5404052/bebfb3dd-c178-44d0-b9bc-005b5c9f0f38) But only the meat and potatoes though. As long as the user doesn't want to do any custom stuff, `get_header`, `set_header`, `compute_note_content_hash`, `get_note_type_id`, `serialize_content` and `deserialize_content` get automatically implemented. Any combination of them can be overridden by the developer though. A metadata struct is also added, which takes the following form: ```rust struct CardNote { points: FieldSelector, randomness: FieldSelector, owner: FieldSelector, } ``` This is used to implement a `properties()` function, which in turn can be used in conjunction with the `NoteGetterOptions.select` and `.sort` <img width="697" alt="Screenshot 2024-03-18 at 15 27 27" src="https://github.com/AztecProtocol/aztec-packages/assets/5404052/5da531b3-0b7f-4cf9-9908-300ff8d98c6d">
When writing a note getter, the dev needs to specify how to filter or sort based on the indices of each field. This is error prone, as a small change in a struct automatically breaks all note getters that depend on it if it changes the position of the fields.
We should somehow automatically generate constants (macros? new language feature?) for getting the index of a field within a struct, to be used in note getters.
Requested by @0xShaito from Wonderland
The text was updated successfully, but these errors were encountered: