Skip to content

Subgraph Merkle tree depth remains zero after adding members #1108

Open

Description

@zjuncal

Describe the bug

The subgraph initializes MerkleTree.depth to 0 in createGroup(), but neither addMember() nor addMembers() updates it after adding leaves. As a result, indexed trees with two or more leaves continue to report depth 0, even though their size and root are updated.

For example, a tree with three leaves is indexed as {"depth":0,"size":3}, whereas new Group([1n, 2n, 3n]) has depth 2 and size 3.

To Reproduce

  1. At commit dbab5553c486a09d5e1042e5271e3316224cc176, index a GroupCreated event.
  2. Index a MemberAdded event for index 0 in that group. Its tree correctly has size 1 and depth 0.
  3. Index a MembersAdded event for the same group with startIndex = 1 and two additional identity commitments.
  4. Query the group's merkleTree { depth size }. The result has size 3 but depth 0, instead of 2.

The same issue occurs when adding the second and subsequent leaves using only MemberAdded events.

This is also reproducible locally in apps/subgraph/tests/semaphore.test.ts: adding a depth 2 assertion to the existing three-leaf batch-add test fails with Expected field 'depth' to equal '2', but was '0' instead. The new individual-add regression test likewise fails at the second leaf, expecting depth 1 but observing 0.

Expected behavior

Keep the indexed depth consistent with the LeanIMT structure: depth 0 for sizes 0 and 1, and ceil(log2(size)) for larger trees. For example, sizes 2, 3, 4, and 5 should have depths 1, 2, 2, and 3, respectively.

Recompute depth after both individual and batch additions. Updating or removing members should preserve size and depth, since removal replaces a leaf with zero rather than shrinking the tree.

Screenshots

Not applicable; this is an indexed data inconsistency reproduced with Matchstick tests.

Technologies (please complete the following information):

  • Node.js version: v26.12.0 (local reproduction)
  • NPM version: 11.20.0; repository dependencies and scripts use Yarn 4.1.0
  • Solidity version: not applicable to the mapping defect; the repository's Semaphore contract declares >=0.8.23 <0.9.0
  • Subgraph tooling: @graphprotocol/graph-cli 0.95.0, @graphprotocol/graph-ts 0.37.0, matchstick-as 0.6.0, Matchstick binary 0.5.0

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions