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

feat: ComponentMetadata templating #1027

Merged
merged 15 commits into from
Jan 2, 2025
Merged

Conversation

igamigo
Copy link
Collaborator

@igamigo igamigo commented Dec 18, 2024

No description provided.

@igamigo igamigo force-pushed the igamigo-pkg-template branch from ba8cc33 to c73a3d2 Compare December 23, 2024 15:16
@igamigo igamigo marked this pull request as ready for review December 23, 2024 15:37
Copy link
Contributor

@bobbinth bobbinth left a 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! Not a full review yet, but I left some comments inline.

pub enum TemplateValue {
Felt(Felt),
Word(Word),
Map(Vec<(Digest, Word)>),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would Entries be a better name here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Instead of Map? Sounds a bit more generic IMO but I can rename it if preferred

objects/src/accounts/package/storage_entry/template.rs Outdated Show resolved Hide resolved
objects/src/accounts/package/storage_entry/template.rs Outdated Show resolved Hide resolved
objects/src/accounts/package/mod.rs Outdated Show resolved Hide resolved
objects/src/accounts/package/mod.rs Outdated Show resolved Hide resolved
objects/src/accounts/package/mod.rs Outdated Show resolved Hide resolved
objects/src/accounts/package/mod.rs Outdated Show resolved Hide resolved
objects/src/accounts/package/mod.rs Outdated Show resolved Hide resolved
Comment on lines 184 to 193
// Check for duplicates
let mut seen = BTreeSet::new();
let duplicate =
all_slots
.iter()
.find_map(|&slot| if !seen.insert(slot) { Some(slot) } else { None });

if let Some(dup) = duplicate {
return Err(ComponentPackageError::DuplicateSlots(dup));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This check can be done inside the all_slots.windows(2). We should check that slots[1] == slots[0] doesn't happen.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point! Changed.

Comment on lines 258 to 259
#[error("error creating AccountComponent: {0}")]
AccountComponentError(AccountError),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use #[from] here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Did you mean #[source]? I added #[from] in the serde error and #[source] here. I'd rather avoid automatically adding the conversion since there might be places where we benefit from double-checking the context

objects/src/accounts/package/mod.rs Outdated Show resolved Hide resolved
@@ -160,6 +164,59 @@ impl StorageEntry {
StorageEntry::MultiSlot { .. } => &[],
}
}

/// Returns an iterator over all of the storage entries's template keys.
// TODO: Should template keys be typed?
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do we mean by "typed" here? Could keys be of different types other than strings?

EDIT: Oh, maybe we mean they key having the storage entry variant where it came from. I think this can be checked by the caller if necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was referring to whether we want keys to have what type of value they expect, basically.

objects/src/accounts/package/storage_entry/mod.rs Outdated Show resolved Hide resolved
}

/// Returns `Ok(&Vec<(Digest, Word)>>` if the variant is `Map`, otherwise errors.
pub fn as_map(&self) -> Result<&Vec<(Digest, Word)>, ComponentPackageError> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is not used, will this be used in a later PR?

/// they are not of a valid type)
pub fn instantiate_component(
&self,
template_keys: &BTreeMap<String, TemplateValue>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
template_keys: &BTreeMap<String, TemplateValue>,
template_keys: &BTreeMap<TemplateKey, TemplateValue>,

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same in the try_into_... methods that have this map as a parameter

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll probably leave refactoring initialization data for future PRs as explained here: #1027 (comment)

Comment on lines 63 to 70
FeltRepresentation::Dynamic(template_key) => *template_values
.get(template_key.key())
.ok_or_else(|| {
ComponentPackageError::TemplateValueNotProvided(
template_key.key().to_string(),
)
})?
.as_felt()?,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this should be moved into a FeltRepresentation::try_into_felt(template_values) method, so it is more in line with the other objects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Factored out the code

Comment on lines 138 to 141
// Attempt to deserialize as TemplateKey first
if let Ok(tk) = TemplateKey::try_deserialize(value) {
return Ok(WordRepresentation::Dynamic(tk));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of trying one then the other, we could check the first two chars, if they are 0x the we use the parse_hex_string_as_word. This way the error message could be different in both cases because we know which variant we were expecting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

While implementing this I realized it is still valuable to output valid formats for string-based values. For example: maybe someone writes "123" expecting decimals to work but then it won't match with the "0x" and so we would assume a dynamic value when that's not really the case.

@bobbinth
Copy link
Contributor

I think maybe the way to proceed with this and #1015 PRs is:

  1. Address some small comments in this PR and merge it into #1015 .
  2. Review #1015 comprehensively and merge it.

Otherwise, I find myself flipping back-and-forth between the two PRs to see where a given comment may be more appropriate.

@igamigo
Copy link
Collaborator Author

igamigo commented Dec 30, 2024

I think maybe the way to proceed with this and #1015 PRs is:

  1. Address some small comments in this PR and merge it into #1015 .
  2. Review #1015 comprehensively and merge it.

Otherwise, I find myself flipping back-and-forth between the two PRs to see where a given comment may be more appropriate.

Sounds fair. @bobbinth if it's OK by you, I'll merge this and continue on the base branch

@bobbinth bobbinth merged commit 3095071 into igamigo-acc-pkg Jan 2, 2025
9 checks passed
@bobbinth bobbinth deleted the igamigo-pkg-template branch January 2, 2025 08:59
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.

3 participants