Skip to content

@lelantos-org/sdk / advanced / TreeStore

Class: TreeStore ​

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:111

Constructors ​

Constructor ​

ts
new TreeStore(
   P, 
   fmd, 
   treeDepth?
): TreeStore;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:126

treeDepth must equal the depth the spend path gives the circuit (WalletConfig.treeDepth). A mismatched depth raises no local error, but the proof fails to verify on-chain.

Parameters ​

ParameterType
PPoseidon
fmdCommitmentFeed
treeDepth?number

Returns ​

TreeStore

Methods ​

getPath() ​

ts
getPath(leafIndex): MerkleProof & object;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:141

Parameters ​

ParameterType
leafIndexnumber

Returns ​

MerkleProof & object


loadState() ​

ts
loadState(state): void;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:129

Parameters ​

ParameterType
stateTreeStoreState

Returns ​

void


reset() ​

ts
reset(): Promise<void>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:186

Discard the local tree and its persisted state so the next sync() rebuilds from leaf 0.

For trees that syncing cannot repair: sync() only appends from chunkOf(syncedCount), so a prefix that diverged from the chain (server re-index, pool redeployed under the same chain id, partially written restore) stays wrong, as does a local tree with more leaves than the chain. Every leaf is re-fetched and re-hashed, so use it only after an ordinary resync fails, as syncVerified does.

Returns ​

Promise<void>


root() ​

ts
root(): bigint;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:144

Returns ​

bigint


saveState() ​

ts
saveState(): TreeStoreState;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:130

Returns ​

TreeStoreState


sync() ​

ts
sync(opts?): Promise<TreeSyncSummary>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:137

Fetch chunks since the last sync, insert their leaves, then persist. Idempotent: the tail chunk is re-fetched every sync and existing entries are skipped by leaf index.

Runs after any sync, reset or verification already in progress.

Parameters ​

ParameterType
opts?TreeSyncOpts

Returns ​

Promise<TreeSyncSummary>


syncVerified() ​

ts
syncVerified(opts?): Promise<RootCheck>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:164

Sync, then repair until the local tree matches the chain. Passes, in increasing cost:

  1. Sync and check.
  2. If the mirror has more leaves, sync again to append the rest. Otherwise re-read the tree state: an equal count with a differing root also results from /v1/tree-state and the chunk feed being read at different times.
  3. If isKnownRoot is supplied, ask the pool, which is authoritative. If it accepts the local root, the mirror is at fault and the tree is spendable.
  4. Rebuild from leaf 0, since syncing cannot repair a diverged tree; see reset.

Parameters ​

ParameterType
opts?TreeVerifyOpts

Returns ​

Promise<RootCheck>


syncVerifiedSnapshot() ​

ts
syncVerifiedSnapshot<T>(opts, read): Promise<{
  check: RootCheck;
  value: T | undefined;
}>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:172

syncVerified, then read against the verified tree, with no sync or reset in between. read is skipped when the tree is not spendable.

A spend needs its Merkle root and every input's path from one tree; read separately, a concurrent sync or reset could change the root between them.

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterType
optsTreeVerifyOpts
read(check) => T | Promise<T>

Returns ​

Promise<{ check: RootCheck; value: T | undefined; }>


verifyRoot() ​

ts
verifyRoot(): Promise<RootCheck>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:152

Check the locally built root against the mirrored chain root. This makes trusting the server's leafHash sound: a wrong leaf produces a wrong root, which surfaces here rather than as a rejected transaction.

Reports observations only; syncVerified acts on them.

Returns ​

Promise<RootCheck>


withPersistence() ​

ts
static withPersistence(
   P, 
   fmd, 
   persistence, 
   treeDepth?
): Promise<TreeStore>;

Defined in: @lelantos-org/sdk/dist/sync/tree-store.d.ts:128

Build a TreeStore and restore any previously persisted state.

Parameters ​

ParameterType
PPoseidon
fmdCommitmentFeed
persistenceTreePersistence
treeDepth?number

Returns ​

Promise<TreeStore>