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

Introduce AccountIdBuilder #1045

Merged
merged 11 commits into from
Jan 8, 2025
Merged

Conversation

PhilippGackstatter
Copy link
Contributor

@PhilippGackstatter PhilippGackstatter commented Jan 3, 2025

Couple of small fixes following from the account ID refactor (#982).

The biggest addition is the account_id_dummy! macro, hence the PR title and changelog entry.

The changes are:

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 a couple of small comments inline.

Comment on lines 793 to 795
#[cfg(any(feature = "testing", test))]
#[macro_export]
macro_rules! account_id_dummy {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit (feel free to ignore): dummy_account_id feels a bit more natural as the name here.

miden-lib/src/transaction/mod.rs Show resolved Hide resolved
Copy link
Contributor

@Mirko-von-Leipzig Mirko-von-Leipzig left a comment

Choose a reason for hiding this comment

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

Feels a bit strange that its a macro. We could implement rand's Distribution but that wouldn't handle type and storage mode variations.

Alternatively we could also have a builder but that is significantly more implementation code. (just thinking out loud, not recommending this).

@PhilippGackstatter
Copy link
Contributor Author

We could implement rand's Distribution

I think this would make sense for AccountType and AccountStorageMode to get rid of the sample_ functions.

Alternatively we could also have a builder but that is significantly more implementation code. (just thinking out loud, not recommending this).

Thanks for the suggestion. I considered that as well, but the builder seemed like too much boilerplate for just this functionality. Originally I also wanted to get rid of AccountId::dummy and enable calling account_id_dummy! with [u8; 15] and an impl Rng, but it didn't work as nicely as I had hoped.

Maybe the builder would make more sense ultimately. If we introduce another AccountIdVersion, then we'd likely also want to include that in the macro, and then it takes three parameters. And you would be forced to supply all three type, mode and version even if you just want a specific version. With a builder, one can flexibly pick and choose.

I imagine something simple like:

AccountIdBuilder::new(&mut rng).storage_mode(AccountStorageMode::Private).build();
AccountIdBuilder::new(&mut rng).build();

Seems short enough to easily read in tests. Alternatively we can also pass the rng to build I think, though not seeing a significant advantage for one or the other.

@Mirko-von-Leipzig
Copy link
Contributor

Maybe the builder would make more sense ultimately. If we introduce another AccountIdVersion, then we'd likely also want to include that in the macro, and then it takes three parameters. And you would be forced to supply all three type, mode and version even if you just want a specific version. With a builder, one can flexibly pick and choose.

I imagine something simple like:

AccountIdBuilder::new(&mut rng).storage_mode(AccountStorageMode::Private).build();
AccountIdBuilder::new(&mut rng).build();

Seems short enough to easily read in tests. Alternatively we can also pass the rng to build I think, though not seeing a significant advantage for one or the other.

Passing it to build does separate the "configuration" from the generation/seed selection. You could have

.build_with_rng(&mut rng)
.build_with_seed(..)
.build_with_thread_rng(..)

Though there are several APIs one could shuffle these into. Likely this isn't the only place we might want this style of thing 🤷

@PhilippGackstatter
Copy link
Contributor Author

Went with the AccountIdBuilder and it does seem nicer than the macro. Can you have another look, @Mirko-von-Leipzig? Thank you!

I did not add a AccountId::builder method because the builder is only for testing purposes, so keeping it somewhat separate seems like a good idea.

objects/src/accounts/account_id.rs Outdated Show resolved Hide resolved
objects/src/testing/account_id.rs Show resolved Hide resolved
objects/src/testing/account_id.rs Outdated Show resolved Hide resolved
@PhilippGackstatter PhilippGackstatter changed the title Introduce account_id_dummy! macro Introduce AccountIdBuilder Jan 7, 2025
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!

@PhilippGackstatter PhilippGackstatter merged commit 966d98f into next Jan 8, 2025
9 checks passed
@PhilippGackstatter PhilippGackstatter deleted the pgackst-account-id-fixes branch January 8, 2025 09:48
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