Skip to content

refactor: unify account/nullifier SMT backends into shared SmtBackend traits#3009

Merged
mmagician merged 8 commits into
nextfrom
mmagician-claude/unify-smt-backend-traits
Jun 3, 2026
Merged

refactor: unify account/nullifier SMT backends into shared SmtBackend traits#3009
mmagician merged 8 commits into
nextfrom
mmagician-claude/unify-smt-backend-traits

Conversation

@mmagician

@mmagician mmagician commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #2755

Replaces the two near-identical per-tree backend trait pairs (AccountTreeBackend / NullifierTreeBackend and their reader variants from #2755) with a single, value-agnostic pair in a new block/smt_backend.rs:

  • SmtBackendReader (read-only) and SmtBackend: SmtBackendReader (read-write), implemented once for Smt and once for LargeSmt (reader impl bounded on SmtStorageReader, writer impl on SmtStorage).

Design notes

  • The reader trait is the intentional superset of what each tree needs (account uses leaves/num_leaves/get_leaf; nullifier uses entries/num_entries). num_leaves and num_entries are both kept because they differ for the nullifier tree when keys collide into a single SmtLeaf::Multiple - collapsing them would undercount nullifiers.

Open questions

  • Trait naming: SmtBackend / SmtBackendReader vs something more specific?

claude added 5 commits May 29, 2026 13:29
… traits

Replaces the near-identical AccountTreeBackend/NullifierTreeBackend trait
pairs with a single Word-valued SmtBackendReader + SmtBackend pair, impl'd
once for Smt and once for LargeSmt. The NullifierBlock value wrapping moves
from the backend into NullifierTree's own accessors, leaving the backend
layer value-agnostic. Removes both per-tree backend.rs modules.
Addresses pre-push review findings on the SMT backend unification:
- correct the CHANGELOG entry to name the shared SmtBackend/SmtBackendReader
  traits instead of the removed per-tree reader traits
- disambiguate the writer-impl section headers in smt_backend.rs
- document that with_storage_from_entries panics (not errors) on storage
  failures, matching large_smt_error_to_merkle_error behavior
- add reader() tests asserting the read-only view shares root and entries
Removes the redundant AccountTreeError::DuplicateEntries variant and maps
the LargeSmt-backed with_storage_from_entries duplicate case through the same
prefix-extracting helper as the Smt-backed with_entries, so both constructors
surface DuplicateStateCommitments { prefix }. Adds a duplicate-entry test,
documents the read-only reader() invariant, and aligns the leaves()/entries()
lifetime style.
These only asserted that a freshly created reader view equals its source,
which exercises no meaningful logic. The duplicate-entry test (real error
path) is kept.
@mmagician mmagician requested a review from sergerad May 29, 2026 14:35
@mmagician mmagician marked this pull request as ready for review May 29, 2026 14:35
Comment thread crates/miden-protocol/src/block/account_tree/mod.rs Outdated
Comment thread crates/miden-protocol/src/block/smt_backend.rs Outdated
Co-authored-by: Marti <marcin.gorny.94@protonmail.com>
@mmagician mmagician changed the title refactor: unify account/nullifier SMT backends into shared SmtBackend traits refactor: unify account/nullifier SMT backends into shared SmtBackend traits May 29, 2026
Use inline `[`leaves`](Self::leaves)` / `[`entries`](Self::entries)` links
instead of reference-style links, which rustdoc failed to resolve under
`-D warnings`.

@PhilippGackstatter PhilippGackstatter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me!

I think since the backends did not add much type safety to account tree or nullifier tree, the duplication isn't worth it and deduplicating them makes sense to me.

)]
DuplicateStateCommitments { prefix: AccountIdPrefix },
#[error("entries passed to account tree contain duplicate values")]
DuplicateEntries(#[source] MerkleError),

@sergerad sergerad Jun 1, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why don't we use this and instead have duplicate_state_commitment_error()? Understood that this PR only added the fn, not the underlying impl which already existed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

DuplicateEntries described the same condition as DuplicateStateCommitments (two entries colliding on the same account-ID prefix) - but with a vaguer message (no account id included in the error)

@sergerad sergerad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM just wondering about the #[source] question which is somewhat tangential to the PR.

@bobbinth bobbinth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good! Thank you!

@mmagician mmagician added this pull request to the merge queue Jun 3, 2026
Merged via the queue into next with commit 1e7d0b0 Jun 3, 2026
20 checks passed
@mmagician mmagician deleted the mmagician-claude/unify-smt-backend-traits branch June 3, 2026 07:53
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.

5 participants