Repository navigation
Description
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
- At commit
dbab5553c486a09d5e1042e5271e3316224cc176, index aGroupCreatedevent. - Index a
MemberAddedevent for index0in that group. Its tree correctly has size1and depth0. - Index a
MembersAddedevent for the same group withstartIndex = 1and two additional identity commitments. - Query the group's
merkleTree { depth size }. The result has size3but depth0, instead of2.
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-cli0.95.0,@graphprotocol/graph-ts0.37.0,matchstick-as0.6.0, Matchstick binary 0.5.0
Additional context
- Mapping: https://github.com/semaphore-protocol/semaphore/blob/dbab5553c486a09d5e1042e5271e3316224cc176/apps/subgraph/src/semaphore.ts
- Group behavior: https://github.com/semaphore-protocol/semaphore/blob/dbab5553c486a09d5e1042e5271e3316224cc176/packages/group/tests/index.test.ts
- Suggested fix: share an integer-based depth calculation between the two member-add handlers.
- This report is based on source inspection and local mapping tests, not a query against a deployed subgraph.
- The fix does not retroactively repair stored entities. A subsequent member-add event can correct that group's current depth, but correcting all affected groups and historical results requires reindexing/replaying events with the corrected mapping.
Activity
fix(subgraph): fix(subgraph): update Merkle tree depth when adding me…