fix: move recompute logic to OutputNoteBuilder::add_asset to avoid repeated hashing#2577
Merged
PhilippGackstatter merged 6 commits into0xMiden:nextfrom Mar 12, 2026
Merged
Conversation
PhilippGackstatter
approved these changes
Mar 12, 2026
Contributor
PhilippGackstatter
left a comment
There was a problem hiding this comment.
TL;DR: Let's merge this PR as-is; we'll need to touch this again in the future anyway.
Long-form thought process
I somehow hadn't realized we would have to move the merge logic to the builder. Ideally we should not have this logic implemented in `OutputNoteBuilder`. It should be part of `NoteAssets` somehow in case we need to reuse it (currently we don't).I can think of two other approaches:
- Keep
NoteAssets::add_assetand remove the cachedcommitmentinside. Instead we'd haveto_commitmentwhich would compute the commitment when called.- We use
NoteAssets::commitmentin a few places, so re-hashing it a few times is not ideal but also not really bad.
- We use
- Remove
NoteAssets::add_assetand introduce aNoteAssetsBuilderthat implements the merge logic internally and only constructsNoteAssetswhenNoteAssetsBuilder::buildis called (which computes the commitment once).- Then we can have a
NoteAssets::into_builderto reconstruct a builder and mutate the note assets easily.
- Then we can have a
The builder seems a bit overkill, but I think it's the best solution to avoid a bunch of unnecessary hashing.
In the future, assets will be able to customize how they are merged, and so this approach to merging assets as we have it now will no longer work. For the purpose of transaction execution, we'll probably need to let the VM do the merging and then extract the merged asset in a transaction event and insert into NoteAssets. In this case, the current approach will work better than what we've had so far, so I think the PR is still a step in the right direction.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #2525
Removed
NoteAssets::add_assetmethodChanged
OutputNoteBuilder::assetsfromNoteAssetstoVec<Asset>Moved the asset merging logic into
OutputNoteBuilder::add_asset