voidcashdocs

The note tree

The append-only Poseidon2 Merkle tree that holds every note commitment - depth, pair insertion, root history, rollover and how wallets rebuild it.

Every note commitment ever created is a leaf of one append-only Merkle tree in PrivateVault, through its PoseidonMerkleTree base contract. A spend proves membership against a root of this tree without saying which leaf.

Parameters

ConstantValueMeaning
TREE_DEPTH242^24 = 16,777,216 leaves per tree, or 2^23 insertions
ROOT_HISTORY_SIZE64Recent roots accepted by isKnownRoot
Empty leaf0zeros[0] = 0, zeros[i+1] = H(zeros[i], zeros[i])
NodeH(left, right)Poseidon2 over BN254, through the hasher contract; the same as the circuit's node
function root() external view returns (uint256);
function isKnownRoot(uint256 root) external view returns (bool);
function nextLeafIndex() external view returns (uint32);
function treeNumber() external view returns (uint32);
function isFinalRoot(uint256 root) external view returns (bool);
event TreeCompleted(uint256 indexed treeNumber, uint256 finalRoot);

The vault sits behind a proxy, so the empty tree is written by initialize (_initTree), not by a constructor: it fills zeros and the stored left siblings level by level and records the empty root as the first root.

A path from leaf cm at index i is checked the same way in the circuit and rebuilt the same way by wallets:

a_0 = cm
a_k+1 = H(a_k, path[k])   if bit k of i is 0
        H(path[k], a_k)   if bit k of i is 1
root  = a_24

Pair insertion

Leaves are always inserted two at a time, at an even index (_insertPair(left, right)):

current = H(left, right)                   level 1
pos     = index / 2
for level 1 .. 23:
    if pos is even:  filledSubtrees[level] = current;  current = H(current, zeros[level])
    else:            current = H(filledSubtrees[level], current)
    pos = pos / 2
push current into the root history
nextLeafIndex = index + 2

That is 1 + (TREE_DEPTH - 1) = 24 Poseidon2 calls per pair, instead of 48 for two single insertions.

OperationLeaves inserted
transact (also through ERC-4337)Two pairs: (cm0, cm1), the main pair's outputs, then (cm2, cm3), the HYPE fee change and the zero-value pad. Four NoteAdded events
shield, shieldToken(cm, 0): the note and an empty leaf

A single note is padded with an empty leaf, which leaves the root exactly as if the note had been inserted alone. The padding leaf emits no event and is never a note. Every insertion therefore starts at an even index: the index in Shielded, in TokenShielded and in the first NoteAdded of each pair is always even.

A transaction's fourth output, the pad, is a real leaf with a zero-value HYPE note behind it, not an empty leaf: it is what lets a transaction fill two whole pairs. Transactions made before the vault's v2 upgrade inserted one pair.

Root history

Each insertion writes one new root into a ring buffer of 64. isKnownRoot(root) is true if the root is:

  • the final root of a full tree (see below), or
  • one of the last 64 roots.

Zero is never a known root. A proof names one root, so it stays valid while that root is one of the last 64: through the next 63 insertions. Every shield and shieldToken is one insertion, and every transact two, so a root stays usable through about 31 later transactions. If 64 or more insertions land between building a proof and its inclusion, the transaction reverts with UnknownRoot and must be proved again against a newer root. Nothing is lost: nothing is written when it reverts.

Rollover when a tree is full

When a pair is inserted and nextLeafIndex has reached 2^24:

The current root becomes a final root: isFinalRoot[root] = true, valid forever.

The vault emits TreeCompleted(treeNumber, finalRoot) and increments treeNumber.

The pair is inserted at index 0 of a new tree. The stored left siblings need no reset, because each one is written before it is read.

Notes in a full tree stay spendable forever against its final root, so filling the tree locks nobody out. Leaf indexes restart at 0 in each tree, and the nullifier uses the index inside its tree (see Notes). The check runs for each pair, so a tree can fill between the two pairs of one transaction: its first pair then closes the old tree and its second starts the new one.

A proof has one root, so all three inputs of one transaction must come from the same tree. The app picks the two largest notes of the asset in the tree where they add up to the most. For a token it looks only at trees that also hold a HYPE note covering the gas fee, and spends the smallest such note in the fee pair.

What the wallet rebuilds

The wallet keeps a full copy of the tree, every level of it, in memory (MerkleTree in packages/sdk/src/merkle.ts), rebuilt from the vault's events by scanNotes (packages/sdk/src/scanner.ts):

Read NoteAdded, Shielded, TokenShielded, NullifierSpent, TreeCompleted, ExitCreated, ExitPaid, TokenExitCreated, TokenExitPaid, ExitStopped and ExitResumed from the vault's deployment block on: from the indexer up to its head, and from the chain after that. Without an indexer, or if it fails, from the chain alone.

Replay the leaves in chain order (block, then log index). At each TreeCompleted, start a new tree at index 0.

Restore the padding: when a leaf arrives at an index one past the tree's size, insert an empty leaf first. Any other gap is an error.

For each leaf, try to own it: decrypt its NoteAdded ciphertext with the viewing key; for a shielded note whose ciphertext carries no amount (or, for a token, no asset), take it from the Shielded or TokenShielded event with the same transaction and leaf index. Accept the note only if the commitment recomputed with its asset matches.

Compute each owned note's nullifier and drop the note if a NullifierSpent event has it.

Check the rebuilt current root with isKnownRoot. If the vault does not know it and the data came partly from the indexer, reread everything from the chain; if it came from the chain alone, stop with an error.

The same pass tries every leaf with the user's own keys and with their private address's keys, so one scan finds both.

Reading the chain

The SDK reads the vault's logs with eth_getLogs, every event kind in one query per range of 5,000 blocks, four ranges at a time. Elysium's RPCs refuse ranges over 10,000 blocks; a range the RPC refuses as too large is split in half until each part fits.

The indexer

The indexer (services/indexer, run at https://indexer.voidcash.io) stores only the vault's events. It confirms each block range with eth_getLogs up to 10 blocks behind the latest, at start and every 30 seconds, and between confirmations stores events as they arrive from an eth_subscribe("logs") subscription; the next confirmation fills anything the subscription missed. It keeps them in Postgres, or in memory when no database is configured.

It serves one endpoint, the same for everyone:

GET /events?from=<block>[&after=<block>:<logIndex>]   → { events, next, head }

Each page holds up to 2,000 events in chain order. next continues the listing, and head is the first block not yet confirmed: the wallet uses the indexer's events before head and reads head onwards from the chain itself. /ready, /health and /status report its state.

The indexer never learns which notes a wallet owns: there are no per-user queries, and every wallet downloads the same events and decrypts locally. A dishonest indexer that drops or alters a leaf produces a root the vault does not know, and the wallet falls back to the chain. The root check does not cover everything: ciphertexts and nullifiers are not in the tree, so an indexer could hide a note or a spend and misreport a balance. It cannot move funds, and a wallet that doubts it can read the chain directly.

On this page