Skip to content

Build Merkle trees directly from sorted leaves - #550

Open
robknight wants to merge 1 commit into
mainfrom
bulk_merkle_tree_build
Open

Build Merkle trees directly from sorted leaves#550
robknight wants to merge 1 commit into
mainfrom
bulk_merkle_tree_build

Conversation

@robknight

Copy link
Copy Markdown
Collaborator

This PR changes how Merkle trees are built from HashMaps. Previously, we would iterate over the HashMap, inserting each node into a full Merkle tree, repeatedly traversing existing paths and writing intermediate nodes that were superseded by later insertions. This is expensive when we have to build a Merkle tree containing thousands of nodes, e.g. when building a large custom predicate module.

Building from sorted leaves performs one pass over the final tree structure and stores each leaf and intermediate node exactly once, substantially reducing constructor work and database traffic.

  • Replace repeated insertion in MerkleTree::new_with_db with direct bulk construction.
  • Sort leaves by path and recursively construct the canonical tree.
  • Store only nodes reachable from the final root, avoiding transient nodes created by incremental insertion.
  • Add tests comparing roots, iteration results, and proofs with incremental construction.
  • Cover empty trees, randomized input order, maximum shared prefixes, and stored-node counts.

@robknight
robknight requested a review from ed255 August 31, 2026 12:18
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.

1 participant