-
Notifications
You must be signed in to change notification settings - Fork 52
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: Introduce ComponentPackage
and ComponentTemplate
#1015
base: next
Are you sure you want to change the base?
Conversation
0cee24f
to
123e92c
Compare
ComponentPackage
and ComponentConfig
f12c492
to
1f66753
Compare
ComponentPackage
and ComponentConfig
ComponentPackage
and ComponentTemplate
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 just a few comments inline.
/// | ||
/// Each entry can describe: | ||
/// - A value slot (single word or multiple words). | ||
/// - A multi-slot entry (spanning multiple contiguous slots, with multipe values). |
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 point be about storage maps?
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.
Added the storage map entries alongside its functionality
/// The initial value for this slot. | ||
value: WordRepresentation, |
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 be just Word
? I guess the question is whether it makes sense to keep the original representation at this level as well. The alternative would be to convert WordRepresentation
to Word
when we go from RawStorageEntry
to StorageEntry
.
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 debated this as well. I think the only real upside of keeping the representation is to be able to get the original representation when unpacking or inspecting packages. Whether this is valuable, I'm not sure. I think whether the value is defined as elements or as a binary number, for instance, could sometimes give some more semantic context to the entry itself, but obviously not sure it will be very useful versus just parsing as words and keeping that (probably less error prone as well, though this is easily tested).
I was thinking of adding some methods for more simply retrieving Word
from each entry while keeping the original representation, but again maybe this is too much.
266af8f
to
34abed2
Compare
e5291d0
to
9df6689
Compare
#[macro_export] | ||
macro_rules! digest { | ||
($hex:expr) => {{ | ||
let felts: [$crate::Felt; 4] = match $crate::utils::parse_hex_string_as_word($hex) { |
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.
Question: how would this affect the ability to use the digest!
macro from other crates?
if slots.len() != values.len() { | ||
return Err(ComponentMetadataError::InvalidMultiSlotEntry); | ||
} | ||
|
||
Ok(StorageEntry::MultiSlot { | ||
name: name.into(), | ||
description: description.map(Into::<String>::into), | ||
slots, | ||
values: values.into_iter().map(Into::into).collect(), | ||
}) |
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.
The description above says:
- A multi-slot entry (spanning multiple contiguous slots, with multipe values).
Should we check that the slots are contiguous? (I know we also check it at the ComponentPackage
level.
if !slot_present { | ||
return Err(D::Error::missing_field("slot")); | ||
} | ||
if raw.value.is_some() { |
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.
nit: could we use value_present
here?
if slot_present { | ||
return Err(D::Error::custom( | ||
"Fields 'slot' and 'slots' are mutually exclusive.", | ||
)); | ||
} | ||
if value_present { | ||
return Err(D::Error::custom( | ||
"Fields 'value' and 'values' are mutually exclusive.", | ||
)); | ||
} |
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.
These checks for mutual exclusivity can be done before the match to reduce code repetition
slot: raw.slot.expect("was checked to be present"), | ||
value: raw.value.expect("was checked to be present"), |
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.
instead of the if + expect we could do .ok_or(...)?
here
let slots = raw.slots.ok_or_else(|| D::Error::missing_field("slots"))?; | ||
let values = raw.values.ok_or_else(|| D::Error::missing_field("values"))?; |
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.
If we change the match to be match (raw.slots, raw.values)
we could deconstruct in the case to (Some(slots), Some(values))
and remove these ok_or_else
.
impl Default for WordRepresentation { | ||
fn default() -> Self { | ||
WordRepresentation::SingleHex(Default::default()) | ||
} | ||
} |
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 be in the previous section?
} | ||
// Enforce and skip the '0x' prefix. | ||
let hex_bytes = match hex.as_bytes() { | ||
[b'0', b'x', rest @ ..] => rest, |
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 we allow for "0X" prefix like we do for Felts?
// SAFETY: u8 cast to u64 is safe. We cannot use u64::from in const context so we | ||
// are forced to cast. |
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.
We should add this safety comment back to the match case
Part of #550.
Introduces a first draft of
ComponentPackage
andComponentTemplate
.For now it can serialize and deserialize configs such as this one:
Most structs down to the element need to have custom (de)serialization due to the different alternatives discussed in #550, but at least now it's easy if we want to add a new away of encoding things such as felts (for instance, if we want to allow for binary representation for elements).
Planned follow-ups:
{{foo.bar.baz}}
) for matching whole words or single elements