-
Notifications
You must be signed in to change notification settings - Fork 106
feat: SchemaCommitment component
#2253
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
98c040f
feat: Extend InitStorageData and allow passing native structs
igamigo 636f727
Merge branch 'next' into igamigo-init-refactor
igamigo c1c7f8f
chore: re-add refactored comments
igamigo b5ea229
feat: commit to AccountStorageSchema
igamigo f4b161d
feat: component
igamigo a4250a7
review: refactor and simplify structs
igamigo 9aa118c
feat: commit to StorageSchema
igamigo 9a79fbf
Merge branch 'igamigo-schema-commit' into igamigo-schema-component
igamigo 6a032a3
chore: rename
igamigo 031297c
chore: changelog
igamigo 935e521
chore: lints
igamigo c8eea61
chore: merge conflicts
igamigo 012de90
chore: move serde code
igamigo a5a5fce
chore: more merges
igamigo 786ce75
Merge branch 'igamigo-schema-commit' of https://github.com/0xPolygonM…
igamigo b2d295e
chore: doc lints
igamigo 7fca82f
reviews: copilot comments
igamigo 7b373f0
chore: rename file
igamigo 3db87a8
reviews: factor MASM out and simplify commitment
igamigo ddf1efe
chore: make slot private
igamigo f930f05
reviews: error on conflicting entries, section separator
igamigo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
crates/miden-standards/asm/account_components/metadata/schema_commitment.masm
bobbinth marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # The MASM code of the Storage Commitment metadata Component. | ||
| # | ||
| # See the `AccountSchemaCommitment` Rust type's documentation for more details. | ||
|
|
||
| pub use ::miden::standards::metadata::storage_schema::get_schema_commitment |
19 changes: 19 additions & 0 deletions
19
crates/miden-standards/asm/standards/metadata/storage_schema.masm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # This module defines the storage schema functionality for accounts. It exposes the storage | ||
| # slot at which an account stores a commitment to its storage schema, and provides a helper | ||
| # procedure to load that commitment from the active account's storage. | ||
|
|
||
| use miden::protocol::active_account | ||
|
|
||
| # CONSTANTS | ||
| # ================================================================================================= | ||
|
|
||
| # The slot in this component's storage layout where the account storage schema commitment is stored | ||
| const SCHEMA_COMMITMENT_SLOT = word("miden::standards::metadata::storage_schema") | ||
|
|
||
| pub proc get_schema_commitment | ||
| dropw | ||
| # => [pad(16)] | ||
|
|
||
| push.SCHEMA_COMMITMENT_SLOT[0..2] exec.active_account::get_item | ||
| # => [SCHEMA_COMMITMENT, pad(16)] | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| use alloc::collections::BTreeMap; | ||
|
|
||
| use miden_protocol::Word; | ||
| use miden_protocol::account::component::StorageSchema; | ||
| use miden_protocol::account::{AccountComponent, StorageSlot, StorageSlotName}; | ||
| use miden_protocol::errors::AccountComponentTemplateError; | ||
| use miden_protocol::utils::sync::LazyLock; | ||
|
|
||
| use crate::account::components::storage_schema_library; | ||
|
|
||
| pub static SCHEMA_COMMITMENT_SLOT_NAME: LazyLock<StorageSlotName> = LazyLock::new(|| { | ||
| StorageSlotName::new("miden::standards::metadata::storage_schema") | ||
| .expect("storage slot name should be valid") | ||
| }); | ||
|
|
||
| /// An [`AccountComponent`] exposing the account storage schema commitment. | ||
| /// | ||
| /// The [`AccountSchemaCommitment`] component can be constructed from a list of [`StorageSchema`], | ||
| /// from which a commitment is computed and then inserted into the [`SCHEMA_COMMITMENT_SLOT_NAME`] | ||
| /// slot. | ||
| /// | ||
| /// It reexports the `get_schema_commitment` procedure from | ||
| /// `miden::standards::metadata::storage_schema`. | ||
| /// | ||
| /// ## Storage Layout | ||
| /// | ||
| /// - [`Self::schema_commitment_slot`]: Storage schema commitment. | ||
| pub struct AccountSchemaCommitment { | ||
| schema_commitment: Word, | ||
| } | ||
|
|
||
| impl AccountSchemaCommitment { | ||
| /// Creates a new [`AccountSchemaCommitment`] component from a list of storage schemas. | ||
| /// | ||
| /// The input schemas are merged into a single schema before the final commitment is computed. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if the schemas contain conflicting definitions for the same slot name. | ||
| pub fn new(schemas: &[StorageSchema]) -> Result<Self, AccountComponentTemplateError> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if some other error type would make more sense to return from here instead of |
||
| Ok(Self { | ||
| schema_commitment: compute_schema_commitment(schemas)?, | ||
| }) | ||
| } | ||
|
|
||
| /// Creates a new [`AccountSchemaCommitment`] component from a [`StorageSchema`]. | ||
| pub fn from_schema( | ||
| storage_schema: &StorageSchema, | ||
| ) -> Result<Self, AccountComponentTemplateError> { | ||
| Self::new(core::slice::from_ref(storage_schema)) | ||
| } | ||
|
|
||
| /// Returns the [`StorageSlotName`] where the schema commitment is stored. | ||
| pub fn schema_commitment_slot() -> &'static StorageSlotName { | ||
| &SCHEMA_COMMITMENT_SLOT_NAME | ||
| } | ||
| } | ||
|
|
||
| impl From<AccountSchemaCommitment> for AccountComponent { | ||
| fn from(schema_commitment: AccountSchemaCommitment) -> Self { | ||
| AccountComponent::new( | ||
| storage_schema_library(), | ||
| vec![StorageSlot::with_value( | ||
| AccountSchemaCommitment::schema_commitment_slot().clone(), | ||
| schema_commitment.schema_commitment, | ||
| )], | ||
| ) | ||
| .expect( | ||
| "AccountSchemaCommitment component should satisfy the requirements of a valid account component", | ||
| ) | ||
| .with_supports_all_types() | ||
| } | ||
| } | ||
|
|
||
| /// Computes the schema commitment. | ||
| /// | ||
| /// The account schema commitment is computed from the merged schema commitment. | ||
| /// If the passed list of schemas is empty, [`Word::empty()`] is returned. | ||
| fn compute_schema_commitment( | ||
| schemas: &[StorageSchema], | ||
| ) -> Result<Word, AccountComponentTemplateError> { | ||
| if schemas.is_empty() { | ||
| return Ok(Word::empty()); | ||
| } | ||
|
|
||
| let mut merged_slots = BTreeMap::new(); | ||
| for schema in schemas { | ||
| for (slot_name, slot_schema) in schema.iter() { | ||
| match merged_slots.get(slot_name) { | ||
| None => { | ||
| merged_slots.insert(slot_name.clone(), slot_schema.clone()); | ||
| }, | ||
| // Slot exists, check if the schema is the same before erroring | ||
| // TODO: If we wanted to not error, we would have to decide on a winning schema | ||
| // for the StorageSlotName | ||
igamigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Some(existing) => { | ||
| if existing != slot_schema { | ||
| return Err(AccountComponentTemplateError::InvalidSchema(format!( | ||
| "conflicting definitions for storage slot `{slot_name}`", | ||
| ))); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let merged_schema = StorageSchema::new(merged_slots)?; | ||
|
|
||
| Ok(merged_schema.commitment()) | ||
| } | ||
|
|
||
| // TESTS | ||
| // ================================================================================================ | ||
|
|
||
| #[cfg(test)] | ||
igamigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mod tests { | ||
| use miden_protocol::Word; | ||
| use miden_protocol::account::AccountBuilder; | ||
| use miden_protocol::account::component::AccountComponentMetadata; | ||
|
|
||
| use super::AccountSchemaCommitment; | ||
| use crate::account::auth::NoAuth; | ||
|
|
||
| #[test] | ||
| fn storage_schema_commitment_is_order_independent() { | ||
| let toml_a = r#" | ||
| name = "Component A" | ||
| description = "Component A schema" | ||
| version = "0.1.0" | ||
| supported-types = [] | ||
|
|
||
| [[storage.slots]] | ||
| name = "test::slot_a" | ||
| type = "word" | ||
| "#; | ||
|
|
||
| let toml_b = r#" | ||
| name = "Component B" | ||
| description = "Component B schema" | ||
| version = "0.1.0" | ||
| supported-types = [] | ||
|
|
||
| [[storage.slots]] | ||
| name = "test::slot_b" | ||
| description = "description is committed to" | ||
| type = "word" | ||
| "#; | ||
|
|
||
| let metadata_a = AccountComponentMetadata::from_toml(toml_a).unwrap(); | ||
| let metadata_b = AccountComponentMetadata::from_toml(toml_b).unwrap(); | ||
|
|
||
| let schema_a = metadata_a.storage_schema().clone(); | ||
| let schema_b = metadata_b.storage_schema().clone(); | ||
|
|
||
| // Create one component for each of two different accounts, but switch orderings | ||
| let component_a = | ||
| AccountSchemaCommitment::new(&[schema_a.clone(), schema_b.clone()]).unwrap(); | ||
| let component_b = AccountSchemaCommitment::new(&[schema_b, schema_a]).unwrap(); | ||
|
|
||
| let account_a = AccountBuilder::new([1u8; 32]) | ||
| .with_auth_component(NoAuth) | ||
| .with_component(component_a) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let account_b = AccountBuilder::new([2u8; 32]) | ||
| .with_auth_component(NoAuth) | ||
| .with_component(component_b) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let slot_name = AccountSchemaCommitment::schema_commitment_slot(); | ||
| let commitment_a = account_a.storage().get_item(slot_name).unwrap(); | ||
| let commitment_b = account_b.storage().get_item(slot_name).unwrap(); | ||
|
|
||
| assert_eq!(commitment_a, commitment_b); | ||
| } | ||
|
|
||
| #[test] | ||
| fn storage_schema_commitment_is_empty_for_no_schemas() { | ||
| let component = AccountSchemaCommitment::new(&[]).unwrap(); | ||
|
|
||
| assert_eq!(component.schema_commitment, Word::empty()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.