diff --git a/CHANGELOG.md b/CHANGELOG.md index dfd38e9eac..60259dc228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ ### Changes +- [BREAKING] Renamed `AccountComponent::get_procedures()` to `procedures()`, returning `impl Iterator` ([#2597](https://github.com/0xMiden/protocol/pull/2597)). - [BREAKING] Removed `NoteAssets::add_asset`; `OutputNoteBuilder` now accumulates assets in a `Vec` and computes the commitment only when `build()` is called, avoiding rehashing on every asset addition. ([#2577](https://github.com/0xMiden/protocol/pull/2577)) - [BREAKING] Made `supported_types` a required parameter of `AccountComponentMetadata::new()`; removed `with_supported_type`, `with_supported_types`, `with_supports_all_types`, and `with_supports_regular_types` builder methods; added `AccountType::all()` and `AccountType::regular()` helpers ([#2554](https://github.com/0xMiden/protocol/pull/2554)). - [BREAKING] Migrated to miden-vm 0.21 and miden-crypto 0.22 ([#2508](https://github.com/0xMiden/miden-base/pull/2508)). diff --git a/crates/miden-protocol/src/account/code/mod.rs b/crates/miden-protocol/src/account/code/mod.rs index c8825b9f3c..74fec4a01c 100644 --- a/crates/miden-protocol/src/account/code/mod.rs +++ b/crates/miden-protocol/src/account/code/mod.rs @@ -338,7 +338,7 @@ impl AccountProcedureBuilder { fn add_auth_component(&mut self, component: &AccountComponent) -> Result<(), AccountError> { let mut auth_proc_count = 0; - for (proc_root, is_auth) in component.get_procedures() { + for (proc_root, is_auth) in component.procedures() { self.add_procedure(proc_root); if is_auth { @@ -358,20 +358,19 @@ impl AccountProcedureBuilder { } fn add_component(&mut self, component: &AccountComponent) -> Result<(), AccountError> { - for (proc_mast_root, is_auth) in component.get_procedures() { + for (proc_root, is_auth) in component.procedures() { if is_auth { return Err(AccountError::AccountCodeMultipleAuthComponents); } - self.add_procedure(proc_mast_root); + self.add_procedure(proc_root); } Ok(()) } - fn add_procedure(&mut self, proc_mast_root: Word) { + fn add_procedure(&mut self, proc_root: AccountProcedureRoot) { // Allow procedures with the same MAST root from different components, but only add them // once. - let proc_root = AccountProcedureRoot::from_raw(proc_mast_root); if !self.procedures.contains(&proc_root) { self.procedures.push(proc_root); } diff --git a/crates/miden-protocol/src/account/component/code.rs b/crates/miden-protocol/src/account/component/code.rs index ee8e752e65..af4517b7d8 100644 --- a/crates/miden-protocol/src/account/component/code.rs +++ b/crates/miden-protocol/src/account/component/code.rs @@ -1,6 +1,8 @@ use miden_assembly::Library; -use miden_processor::mast::MastForest; +use miden_assembly::library::ProcedureExport; +use miden_processor::mast::{MastForest, MastNodeExt}; +use crate::account::AccountProcedureRoot; use crate::vm::AdviceMap; // ACCOUNT COMPONENT CODE @@ -26,6 +28,22 @@ impl AccountComponentCode { self.0 } + /// Returns an iterator over the [`AccountProcedureRoot`]s of this component's exported + /// procedures. + pub fn procedure_roots(&self) -> impl Iterator + '_ { + self.0.exports().filter_map(|export| { + export.as_procedure().map(|proc_export| { + let digest = self.0.mast_forest()[proc_export.node].digest(); + AccountProcedureRoot::from_raw(digest) + }) + }) + } + + /// Returns the procedure exports of this component. + pub fn exports(&self) -> impl Iterator + '_ { + self.0.exports().filter_map(|export| export.as_procedure()) + } + /// Returns a new [AccountComponentCode] with the provided advice map entries merged into the /// underlying [Library]'s [MastForest]. /// diff --git a/crates/miden-protocol/src/account/component/mod.rs b/crates/miden-protocol/src/account/component/mod.rs index b9da72c1f5..4b90cc39e1 100644 --- a/crates/miden-protocol/src/account/component/mod.rs +++ b/crates/miden-protocol/src/account/component/mod.rs @@ -13,7 +13,7 @@ pub use storage::*; mod code; pub use code::AccountComponentCode; -use crate::account::{AccountType, StorageSlot}; +use crate::account::{AccountProcedureRoot, AccountType, StorageSlot}; use crate::assembly::Path; use crate::errors::AccountError; use crate::{MastForest, Word}; @@ -196,25 +196,24 @@ impl AccountComponent { self.metadata.supported_types().contains(&account_type) } - /// Returns a vector of tuples (digest, is_auth) for all procedures in this component. + /// Returns an iterator over ([`AccountProcedureRoot`], is_auth) for all procedures in this + /// component. /// /// A procedure is considered an authentication procedure if it has the `@auth_script` /// attribute. - pub fn get_procedures(&self) -> Vec<(Word, bool)> { + pub fn procedures(&self) -> impl Iterator + '_ { let library = self.code.as_library(); - let mut procedures = Vec::new(); - for export in library.exports() { - if let Some(proc_export) = export.as_procedure() { + library.exports().filter_map(|export| { + export.as_procedure().map(|proc_export| { let digest = library .mast_forest() .get_node_by_id(proc_export.node) .expect("export node not in the forest") .digest(); let is_auth = proc_export.attributes.has(AUTH_SCRIPT_ATTRIBUTE); - procedures.push((digest, is_auth)); - } - } - procedures + (AccountProcedureRoot::from_raw(digest), is_auth) + }) + }) } /// Returns the digest of the procedure with the specified path, or `None` if it was not found diff --git a/crates/miden-standards/src/testing/account_component/incr_nonce.rs b/crates/miden-standards/src/testing/account_component/incr_nonce.rs index 96dc055ba2..95c4158c64 100644 --- a/crates/miden-standards/src/testing/account_component/incr_nonce.rs +++ b/crates/miden-standards/src/testing/account_component/incr_nonce.rs @@ -18,7 +18,7 @@ static INCR_NONCE_AUTH_LIBRARY: LazyLock = LazyLock::new(|| { CodeBuilder::default() .compile_component_code("incr_nonce", INCR_NONCE_AUTH_CODE) .expect("incr nonce code should be valid") - .into_library() + .into() }); /// Creates a mock authentication [`AccountComponent`] for testing purposes under the "incr_nonce" diff --git a/crates/miden-standards/src/testing/mock_account_code.rs b/crates/miden-standards/src/testing/mock_account_code.rs index 6f4b8aa061..48de0e4d32 100644 --- a/crates/miden-standards/src/testing/mock_account_code.rs +++ b/crates/miden-standards/src/testing/mock_account_code.rs @@ -142,14 +142,14 @@ static MOCK_FAUCET_LIBRARY: LazyLock = LazyLock::new(|| { CodeBuilder::default() .compile_component_code("mock::faucet", MOCK_FAUCET_CODE) .expect("mock faucet code should be valid") - .into_library() + .into() }); static MOCK_ACCOUNT_LIBRARY: LazyLock = LazyLock::new(|| { CodeBuilder::default() .compile_component_code("mock::account", MOCK_ACCOUNT_CODE) .expect("mock account code should be valid") - .into_library() + .into() }); // MOCK ACCOUNT CODE EXT