-
Notifications
You must be signed in to change notification settings - Fork 106
feat: extend InitStorageData and allow passing native structs
#2230
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
Conversation
0fe55a6 to
537805b
Compare
537805b to
98c040f
Compare
InitStorageData and allow passing native structs
mmagician
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure my understanding is correct, the title "allow passing native structs" refers to allowing native Miden value types (Word, Felt) to be passed directly into InitStorageData, instead of requiring parsed WordValues? (basically 1. from @partylikeits1983's proposal)
I haven't done a full review of the fine details, but on a high level there might be a chance of simplifying the structs a bit, see my comment below
crates/miden-protocol/src/account/component/storage/init_storage_data.rs
Outdated
Show resolved
Hide resolved
mmagician
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering when exactly do we utilize InitStorageData::new() in practice?
I imagine that typically the init storage data will be provided via a TOML file, i.e. using InitStorageData::from_toml constructor.
cc @partylikeits1983 as this PR addresses #1860
Other than this question, the changes LGTM ✅
I added a few unit tests to cover the missing error variants.
| /// Inserts a single map entry. | ||
| /// | ||
| /// See [`Self::insert_value`] for examples of supported types for `key` and `value`. | ||
| pub fn insert_map_entry( | ||
| &mut self, | ||
| slot_name: StorageSlotName, | ||
| key: impl Into<WordValue>, | ||
| value: impl Into<WordValue>, | ||
| ) { | ||
| self.map_entries.entry(slot_name).or_default().push((key.into(), value.into())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this return a Result instead, and reject if duplicates are inserted?
|
Regarding my previous question, I was just genuinely wondering about the different constructors. Because by removing the |
I had thought of this as well. Before this PR, the main advantage of having something like
So I agree with the fact that it maybe decreases the value of converting from native structs, but also the mutators added in this PR are hopefully ergonomic enough that it's not a nuisance to do this pattern instead of preparing a list of values to pass to |
bobbinth
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thank you! I left some comments inline - most of them can be addressed in a follow-up.
crates/miden-protocol/src/account/component/storage/init_storage_data.rs
Outdated
Show resolved
Hide resolved
| pub fn insert_value( | ||
| &mut self, | ||
| name: StorageValueName, | ||
| value: impl Into<WordValue>, | ||
| ) -> Result<(), InitStorageDataError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not for this PR, but would it make sense to also define the type for the name parameter as impl TryInto<StorageValueName>? This way, it may be possible to write something like:
data.insert_value("foo::bar.baz", Felt::from(42_u32));Also, maybe we should implement more conversions for WordValue - e.g., From<u32>, From<[u32; 4]> etc. Or if there is a way somehow to do a blanket implementation so that anything that can be converted into word can be converted into WordValue, that would be even better - but not sure that's possible.
| pub fn insert_map_entry( | ||
| &mut self, | ||
| slot_name: StorageSlotName, | ||
| key: impl Into<WordValue>, | ||
| value: impl Into<WordValue>, | ||
| ) -> Result<(), InitStorageDataError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment re potentially making slot_name be impl TryInto<StorageSlotName>.
| pub fn set_map_values( | ||
| &mut self, | ||
| slot_name: StorageSlotName, | ||
| entries: Vec<(WordValue, WordValue)>, | ||
| ) -> Result<(), InitStorageDataError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment re slot_name as above.
crates/miden-protocol/src/account/component/storage/init_storage_data.rs
Outdated
Show resolved
Hide resolved
crates/miden-protocol/src/account/component/storage/toml/init_storage_data.rs
Outdated
Show resolved
Hide resolved
| // TESTS | ||
| // ================================================================================================ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not for this PR, but I'd move the tests into a separate file as this file is already quite large.
Or another option could be to split the file up into logical components (e.g., map slot schema, value slot schema) under src/account/component/storage/schema directory.
bobbinth
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thank you! I'll merge this as soon as CI passes. Could you create an issue with the remaining follow-ups?
Closes #1860
Also simplifies/removes some code