Skip to content

Refactor(account) introducing account id key#2495

Open
swaploard wants to merge 10 commits into0xMiden:nextfrom
swaploard:refactor(account)--introducing-AccountIdKey
Open

Refactor(account) introducing account id key#2495
swaploard wants to merge 10 commits into0xMiden:nextfrom
swaploard:refactor(account)--introducing-AccountIdKey

Conversation

@swaploard
Copy link
Contributor

This PR addresses issue (#2443) by introducing a dedicated AccountIdKey type to unify and centralize all AccountId → SMT and advice-map key conversions across the account tree and transaction layers. Previously, this logic was implemented via multiple free/helper functions, leading to duplication and scattered layout assumptions. This change removes those functions and replaces them with a single, strongly-typed abstraction that encapsulates the canonical word representations, leaf index conversion, and advice map key format, improving type safety, maintainability, and clarity.

@PhilippGackstatter
Copy link
Contributor

Hi @swaploard, thanks for the PR! Some larger PRs were merged and now would be a good time to merge this PR. Can you merge next into this one?

@swaploard
Copy link
Contributor Author

Sure

Hi @swaploard, thanks for the PR! Some larger PRs were merged and now would be a good time to merge this PR. Can you merge next into this one?

Sure, I will do that.

@swaploard
Copy link
Contributor Author

@PhilippGackstatter, You can review it now.

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. I left some comments inline - most of them are small nits.

Comment on lines +11 to +12
/// The account ID encoded as a key for use in AccountTree and
/// advice maps in `TransactionAdviceInputs`.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: comment wrapping (we usually don't wrap comments before 100 chars).

}

// SMT WORD REPRESENTATION
// ================================================================================================
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: inside impls, we usually use ----- for separators (instead of ======).

Comment on lines +52 to +53
// LEAF INDEX
// ================================================================================================
Copy link
Contributor

Choose a reason for hiding this comment

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

Same nit as above.

Comment on lines +61 to +62
// ADVICE MAP KEY
// ================================================================================================
Copy link
Contributor

Choose a reason for hiding this comment

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

Same nit as above.

Comment on lines +83 to +84
#[cfg(test)]
mod tests {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: let's add TESTS section separator above this line.

Comment on lines +47 to +50
pub fn from_word(word: Word) -> AccountId {
AccountId::try_from_elements(word[KEY_SUFFIX_IDX], word[KEY_PREFIX_IDX])
.expect("account tree should only contain valid IDs")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be try_from_word() and return an error if the conversion failed.

Comment on lines +64 to +67
/// Returns advice map key:
///
/// `[suffix, prefix, 0, 0]`
pub fn to_advice_map_word(&self) -> Word {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we actually want to have different ways of encoding the ID into a word? Maybe we can align these to make sure we always use [0, 0, suffix, prefix] representation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not need this.

Comment on lines 32 to 34
pub fn account_id_to_smt_index(account_id: AccountId) -> LeafIndex<SMT_DEPTH> {
account_id_to_smt_key(account_id).into()
AccountIdKey::from(account_id).as_word().into()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No we dont need this

pub fn leaf(&self) -> SmtLeaf {
if self.commitment == Word::empty() {
let leaf_idx = LeafIndex::from(account_id_to_smt_key(self.id));
let leaf_idx = LeafIndex::from(AccountIdKey::from(self.id).as_word());
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this not be something like:

let leaf_idx = AccountIdKey::from(self.id).to_left_index();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

make sense.


// Read the account header elements from the advice map.
let account_id_key = TransactionAdviceInputs::account_id_map_key(account_id);
let account_id_key = AccountIdKey::from(account_id).as_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: I'd probably do just:

let account_id_key = AccountIdKey::from(account_id)

And then below, would do:

.get(&account_id_key.as_word())

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