-
Notifications
You must be signed in to change notification settings - Fork 123
refactor: move storage schema component into a separate file #2603
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
Open
bobbinth
wants to merge
3
commits into
next
Choose a base branch
from
bobbin-account-schema
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+292
−272
Open
Changes from 1 commit
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,258 +1,3 @@ | ||
| use alloc::collections::BTreeMap; | ||
| mod schema_commitment; | ||
|
|
||
| use miden_protocol::Word; | ||
| use miden_protocol::account::component::{AccountComponentMetadata, StorageSchema}; | ||
| use miden_protocol::account::{ | ||
| Account, | ||
| AccountBuilder, | ||
| AccountComponent, | ||
| AccountType, | ||
| StorageSlot, | ||
| StorageSlotName, | ||
| }; | ||
| use miden_protocol::errors::{AccountError, ComponentMetadataError}; | ||
| 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 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<'a>( | ||
| schemas: impl IntoIterator<Item = &'a StorageSchema>, | ||
| ) -> Result<Self, ComponentMetadataError> { | ||
| 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, ComponentMetadataError> { | ||
| 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 | ||
| } | ||
|
|
||
| /// Returns the [`AccountComponentMetadata`] for this component. | ||
| pub fn component_metadata() -> AccountComponentMetadata { | ||
| AccountComponentMetadata::new("miden::metadata::schema_commitment", AccountType::all()) | ||
| .with_description("Component exposing the account storage schema commitment") | ||
| } | ||
| } | ||
|
|
||
| impl From<AccountSchemaCommitment> for AccountComponent { | ||
| fn from(schema_commitment: AccountSchemaCommitment) -> Self { | ||
| let metadata = AccountSchemaCommitment::component_metadata(); | ||
|
|
||
| AccountComponent::new( | ||
| storage_schema_library(), | ||
| vec![StorageSlot::with_value( | ||
| AccountSchemaCommitment::schema_commitment_slot().clone(), | ||
| schema_commitment.schema_commitment, | ||
| )], | ||
| metadata, | ||
| ) | ||
| .expect( | ||
| "AccountSchemaCommitment component should satisfy the requirements of a valid account component", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // ACCOUNT BUILDER EXTENSION | ||
| // ================================================================================================ | ||
|
|
||
| /// An extension trait for [`AccountBuilder`] that provides a convenience method for building an | ||
| /// account with an [`AccountSchemaCommitment`] component. | ||
| pub trait AccountBuilderSchemaCommitmentExt { | ||
| /// Builds an [`Account`] out of the configured builder after computing the storage schema | ||
| /// commitment from all components currently in the builder and adding an | ||
| /// [`AccountSchemaCommitment`] component. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// - The components' storage schemas contain conflicting definitions for the same slot name. | ||
| /// - [`AccountBuilder::build`] fails. | ||
| fn build_with_schema_commitment(self) -> Result<Account, AccountError>; | ||
| } | ||
|
|
||
| impl AccountBuilderSchemaCommitmentExt for AccountBuilder { | ||
| fn build_with_schema_commitment(self) -> Result<Account, AccountError> { | ||
| let schema_commitment = | ||
| AccountSchemaCommitment::new(self.storage_schemas()).map_err(|err| { | ||
| AccountError::other_with_source("failed to compute account schema commitment", err) | ||
| })?; | ||
|
|
||
| self.with_component(schema_commitment).build() | ||
| } | ||
| } | ||
|
|
||
| // HELPERS | ||
| // ================================================================================================ | ||
|
|
||
| /// 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<'a>( | ||
| schemas: impl IntoIterator<Item = &'a StorageSchema>, | ||
| ) -> Result<Word, ComponentMetadataError> { | ||
| let mut schemas = schemas.into_iter().peekable(); | ||
| if schemas.peek().is_none() { | ||
| 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 | ||
| Some(existing) => { | ||
| if existing != slot_schema { | ||
| return Err(ComponentMetadataError::InvalidSchema(format!( | ||
| "conflicting definitions for storage slot `{slot_name}`", | ||
| ))); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let merged_schema = StorageSchema::new(merged_slots)?; | ||
|
|
||
| Ok(merged_schema.commitment()) | ||
| } | ||
|
|
||
| // TESTS | ||
| // ================================================================================================ | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use miden_protocol::Word; | ||
| use miden_protocol::account::AccountBuilder; | ||
| use miden_protocol::account::auth::{AuthScheme, PublicKeyCommitment}; | ||
| use miden_protocol::account::component::AccountComponentMetadata; | ||
|
|
||
| use super::{AccountBuilderSchemaCommitmentExt, AccountSchemaCommitment}; | ||
| use crate::account::auth::{AuthSingleSig, 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()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn build_with_schema_commitment_adds_schema_commitment_component() { | ||
| let auth_component = AuthSingleSig::new( | ||
| PublicKeyCommitment::from(Word::empty()), | ||
| AuthScheme::EcdsaK256Keccak, | ||
| ); | ||
|
|
||
| let account = AccountBuilder::new([1u8; 32]) | ||
| .with_auth_component(auth_component) | ||
| .build_with_schema_commitment() | ||
| .unwrap(); | ||
|
|
||
| // The auth component has 2 slots (public key and scheme ID) and the schema commitment adds | ||
| // 1 more. | ||
| assert_eq!(account.storage().num_slots(), 3); | ||
|
|
||
| // The auth component's public key slot should be accessible. | ||
| assert!(account.storage().get_item(AuthSingleSig::public_key_slot()).is_ok()); | ||
|
|
||
| // The schema commitment slot should be non-empty since we have a component with a schema. | ||
| let slot_name = AccountSchemaCommitment::schema_commitment_slot(); | ||
| let commitment = account.storage().get_item(slot_name).unwrap(); | ||
| assert_ne!(commitment, Word::empty()); | ||
| } | ||
| } | ||
| pub use schema_commitment::{AccountBuilderSchemaCommitmentExt, AccountSchemaCommitment}; |
Oops, something went wrong.
Oops, something went wrong.
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.
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 think we should start moving these into their corresponding module, and eventually, try to get rid of the
StandardAccountComponent(I may be forgetting something, but it is not clear to me why we need it).