Skip to content

fix: move recompute logic to OutputNoteBuilder::add_asset to avoid repeated hashing#2577

Merged
PhilippGackstatter merged 6 commits into0xMiden:nextfrom
PoulavBhowmick03:refactor_add_asset
Mar 12, 2026
Merged

fix: move recompute logic to OutputNoteBuilder::add_asset to avoid repeated hashing#2577
PhilippGackstatter merged 6 commits into0xMiden:nextfrom
PoulavBhowmick03:refactor_add_asset

Conversation

@PoulavBhowmick03
Copy link
Contributor

@PoulavBhowmick03 PoulavBhowmick03 commented Mar 10, 2026

Closes #2525

  • Removed NoteAssets::add_asset method

  • Changed OutputNoteBuilder::assets from NoteAssets to Vec<Asset>

  • Moved the asset merging logic into OutputNoteBuilder::add_asset

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!

Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

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_asset and remove the cached commitment inside. Instead we'd have to_commitment which would compute the commitment when called.
    • We use NoteAssets::commitment in a few places, so re-hashing it a few times is not ideal but also not really bad.
  • Remove NoteAssets::add_asset and introduce a NoteAssetsBuilder that implements the merge logic internally and only constructs NoteAssets when NoteAssetsBuilder::build is called (which computes the commitment once).
    • Then we can have a NoteAssets::into_builder to reconstruct a builder and mutate the note assets easily.

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.

@PhilippGackstatter PhilippGackstatter enabled auto-merge (squash) March 12, 2026 07:39
@PhilippGackstatter PhilippGackstatter merged commit ab4f4ef into 0xMiden:next Mar 12, 2026
15 checks passed
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.

Consider removing NoteAssets::add_asset

3 participants