@aztec/merkle-tree 0.16.4 → 0.16.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,48 +0,0 @@
1
- import { UpdateOnlyTree } from '../interfaces/update_only_tree.js';
2
- import { FullTreeSnapshotBuilder } from '../snapshots/full_snapshot.js';
3
- import { TreeSnapshot } from '../snapshots/snapshot_builder.js';
4
- import { INITIAL_LEAF, TreeBase } from '../tree_base.js';
5
-
6
- /**
7
- * A Merkle tree implementation that uses a LevelDB database to store the tree.
8
- */
9
- export class SparseTree extends TreeBase implements UpdateOnlyTree {
10
- #snapshotBuilder = new FullTreeSnapshotBuilder(this.db, this);
11
-
12
- /**
13
- * Updates a leaf in the tree.
14
- * @param leaf - New contents of the leaf.
15
- * @param index - Index of the leaf to be updated.
16
- */
17
- public async updateLeaf(leaf: Buffer, index: bigint): Promise<void> {
18
- if (index > this.maxIndex) {
19
- throw Error(`Index out of bounds. Index ${index}, max index: ${this.maxIndex}.`);
20
- }
21
-
22
- const insertingZeroElement = leaf.equals(INITIAL_LEAF);
23
- const originallyZeroElement = (await this.getLeafValue(index, true))?.equals(INITIAL_LEAF);
24
- if (insertingZeroElement && originallyZeroElement) {
25
- return;
26
- }
27
- await this.addLeafToCacheAndHashToRoot(leaf, index);
28
- if (insertingZeroElement) {
29
- // Deleting element (originally non-zero and new value is zero)
30
- this.cachedSize = (this.cachedSize ?? this.size) - 1n;
31
- } else if (originallyZeroElement) {
32
- // Inserting new element (originally zero and new value is non-zero)
33
- this.cachedSize = (this.cachedSize ?? this.size) + 1n;
34
- }
35
- }
36
-
37
- public snapshot(block: number): Promise<TreeSnapshot> {
38
- return this.#snapshotBuilder.snapshot(block);
39
- }
40
-
41
- public getSnapshot(block: number): Promise<TreeSnapshot> {
42
- return this.#snapshotBuilder.getSnapshot(block);
43
- }
44
-
45
- public findLeafIndex(_value: Buffer, _includeUncommitted: boolean): Promise<bigint | undefined> {
46
- throw new Error('Finding leaf index is not supported for sparse trees');
47
- }
48
- }
@@ -1,612 +0,0 @@
1
- import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer';
2
- import { createDebugLogger } from '@aztec/foundation/log';
3
- import { IndexedTreeLeaf, IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
4
- import { Hasher, SiblingPath } from '@aztec/types';
5
-
6
- import { LevelUp } from 'levelup';
7
-
8
- import {
9
- BatchInsertionResult,
10
- IndexedTree,
11
- IndexedTreeSnapshot,
12
- IndexedTreeSnapshotBuilder,
13
- LowLeafWitnessData,
14
- } from '../index.js';
15
- import { TreeBase } from '../tree_base.js';
16
-
17
- const log = createDebugLogger('aztec:standard-indexed-tree');
18
-
19
- /**
20
- * Factory for creating leaf preimages.
21
- */
22
- export interface PreimageFactory {
23
- /**
24
- * Creates a new preimage from a leaf.
25
- * @param leaf - Leaf to create a preimage from.
26
- * @param nextKey - Next key of the leaf.
27
- * @param nextIndex - Next index of the leaf.
28
- */
29
- fromLeaf(leaf: IndexedTreeLeaf, nextKey: bigint, nextIndex: bigint): IndexedTreeLeafPreimage;
30
- /**
31
- * Creates a new preimage from a buffer.
32
- * @param buffer - Buffer to create a preimage from.
33
- */
34
- fromBuffer(buffer: Buffer): IndexedTreeLeafPreimage;
35
- /**
36
- * Creates an empty preimage.
37
- */
38
- empty(): IndexedTreeLeafPreimage;
39
- /**
40
- * Creates a copy of a preimage.
41
- * @param preimage - Preimage to be cloned.
42
- */
43
- clone(preimage: IndexedTreeLeafPreimage): IndexedTreeLeafPreimage;
44
- }
45
-
46
- /**
47
- * Factory for creating leaves.
48
- */
49
- export interface LeafFactory {
50
- /**
51
- * Creates a new leaf from a buffer.
52
- * @param key - Key of the leaf.
53
- */
54
- buildDummy(key: bigint): IndexedTreeLeaf;
55
- /**
56
- * Creates a new leaf from a buffer.
57
- * @param buffer - Buffer to create a leaf from.
58
- */
59
- fromBuffer(buffer: Buffer): IndexedTreeLeaf;
60
- }
61
-
62
- export const buildDbKeyForPreimage = (name: string, index: bigint) => {
63
- return `${name}:leaf_by_index:${toBufferBE(index, 32).toString('hex')}`;
64
- };
65
-
66
- export const buildDbKeyForLeafIndex = (name: string, key: bigint) => {
67
- return `${name}:leaf_index_by_leaf_key:${toBufferBE(key, 32).toString('hex')}`;
68
- };
69
-
70
- /**
71
- * Pre-compute empty witness.
72
- * @param treeHeight - Height of tree for sibling path.
73
- * @returns An empty witness.
74
- */
75
- function getEmptyLowLeafWitness<N extends number>(
76
- treeHeight: N,
77
- leafPreimageFactory: PreimageFactory,
78
- ): LowLeafWitnessData<N> {
79
- return {
80
- leafPreimage: leafPreimageFactory.empty(),
81
- index: 0n,
82
- siblingPath: new SiblingPath(treeHeight, Array(treeHeight).fill(toBufferBE(0n, 32))),
83
- };
84
- }
85
-
86
- /**
87
- * Standard implementation of an indexed tree.
88
- */
89
- export class StandardIndexedTree extends TreeBase implements IndexedTree {
90
- #snapshotBuilder = new IndexedTreeSnapshotBuilder(this.db, this, this.leafPreimageFactory);
91
- protected cachedLeafPreimages: { [key: string]: IndexedTreeLeafPreimage } = {};
92
-
93
- public constructor(
94
- db: LevelUp,
95
- hasher: Hasher,
96
- name: string,
97
- depth: number,
98
- size: bigint = 0n,
99
- protected leafPreimageFactory: PreimageFactory,
100
- protected leafFactory: LeafFactory,
101
- root?: Buffer,
102
- ) {
103
- super(db, hasher, name, depth, size, root);
104
- }
105
-
106
- /**
107
- * Appends the given leaves to the tree.
108
- * @param _leaves - The leaves to append.
109
- * @returns Empty promise.
110
- * @remarks Use batchInsert method instead.
111
- */
112
- appendLeaves(_leaves: Buffer[]): Promise<void> {
113
- throw new Error('Not implemented');
114
- }
115
-
116
- /**
117
- * Commits the changes to the database.
118
- * @returns Empty promise.
119
- */
120
- public async commit(): Promise<void> {
121
- await super.commit();
122
- await this.commitLeaves();
123
- }
124
-
125
- /**
126
- * Rolls back the not-yet-committed changes.
127
- * @returns Empty promise.
128
- */
129
- public async rollback(): Promise<void> {
130
- await super.rollback();
131
- this.clearCachedLeaves();
132
- }
133
-
134
- /**
135
- * Gets the value of the leaf at the given index.
136
- * @param index - Index of the leaf of which to obtain the value.
137
- * @param includeUncommitted - Indicates whether to include uncommitted leaves in the computation.
138
- * @returns The value of the leaf at the given index or undefined if the leaf is empty.
139
- */
140
- public async getLeafValue(index: bigint, includeUncommitted: boolean): Promise<Buffer | undefined> {
141
- const preimage = await this.getLatestLeafPreimageCopy(index, includeUncommitted);
142
- return preimage && preimage.toBuffer();
143
- }
144
-
145
- /**
146
- * Finds the index of the largest leaf whose value is less than or equal to the provided value.
147
- * @param newKey - The new key to be inserted into the tree.
148
- * @param includeUncommitted - If true, the uncommitted changes are included in the search.
149
- * @returns The found leaf index and a flag indicating if the corresponding leaf's value is equal to `newValue`.
150
- */
151
- async findIndexOfPreviousKey(
152
- newKey: bigint,
153
- includeUncommitted: boolean,
154
- ): Promise<
155
- | {
156
- /**
157
- * The index of the found leaf.
158
- */
159
- index: bigint;
160
- /**
161
- * A flag indicating if the corresponding leaf's value is equal to `newValue`.
162
- */
163
- alreadyPresent: boolean;
164
- }
165
- | undefined
166
- > {
167
- let lowLeafIndex = await this.getDbLowLeafIndex(newKey);
168
- let lowLeafPreimage = lowLeafIndex !== undefined ? await this.getDbPreimage(lowLeafIndex) : undefined;
169
-
170
- if (includeUncommitted) {
171
- const cachedLowLeafIndex = this.getCachedLowLeafIndex(newKey);
172
- if (cachedLowLeafIndex !== undefined) {
173
- const cachedLowLeafPreimage = this.getCachedPreimage(cachedLowLeafIndex)!;
174
- if (!lowLeafPreimage || cachedLowLeafPreimage.getKey() > lowLeafPreimage.getKey()) {
175
- lowLeafIndex = cachedLowLeafIndex;
176
- lowLeafPreimage = cachedLowLeafPreimage;
177
- }
178
- }
179
- }
180
-
181
- if (lowLeafIndex === undefined || !lowLeafPreimage) {
182
- return undefined;
183
- }
184
-
185
- return {
186
- index: lowLeafIndex,
187
- alreadyPresent: lowLeafPreimage.getKey() === newKey,
188
- };
189
- }
190
-
191
- private getCachedLowLeafIndex(key: bigint): bigint | undefined {
192
- const indexes = Object.getOwnPropertyNames(this.cachedLeafPreimages);
193
- const lowLeafIndexes = indexes
194
- .map(index => ({
195
- index: BigInt(index),
196
- key: this.cachedLeafPreimages[index].getKey(),
197
- }))
198
- .filter(({ key: candidateKey }) => candidateKey <= key)
199
- .sort((a, b) => Number(b.key - a.key));
200
- return lowLeafIndexes[0]?.index;
201
- }
202
-
203
- private getCachedLeafIndex(key: bigint): bigint | undefined {
204
- const index = Object.keys(this.cachedLeafPreimages).find(index => {
205
- return this.cachedLeafPreimages[index].getKey() === key;
206
- });
207
- if (index) {
208
- return BigInt(index);
209
- }
210
- return undefined;
211
- }
212
-
213
- private async getDbLowLeafIndex(key: bigint): Promise<bigint | undefined> {
214
- return await new Promise<bigint | undefined>((resolve, reject) => {
215
- let lowLeafIndex: bigint | undefined;
216
- this.db
217
- .createReadStream({
218
- gte: buildDbKeyForLeafIndex(this.getName(), 0n),
219
- lte: buildDbKeyForLeafIndex(this.getName(), key),
220
- limit: 1,
221
- reverse: true,
222
- })
223
- .on('data', data => {
224
- lowLeafIndex = toBigIntBE(data.value);
225
- })
226
- .on('close', function () {})
227
- .on('end', function () {
228
- resolve(lowLeafIndex);
229
- })
230
- .on('error', function () {
231
- log.error('stream error');
232
- reject();
233
- });
234
- });
235
- }
236
-
237
- private async getDbPreimage(index: bigint): Promise<IndexedTreeLeafPreimage | undefined> {
238
- const dbPreimage = await this.db
239
- .get(buildDbKeyForPreimage(this.getName(), index))
240
- .then(data => this.leafPreimageFactory.fromBuffer(data))
241
- .catch(() => undefined);
242
- return dbPreimage;
243
- }
244
-
245
- private getCachedPreimage(index: bigint): IndexedTreeLeafPreimage | undefined {
246
- return this.cachedLeafPreimages[index.toString()];
247
- }
248
-
249
- /**
250
- * Gets the latest LeafPreimage copy.
251
- * @param index - Index of the leaf of which to obtain the LeafPreimage copy.
252
- * @param includeUncommitted - If true, the uncommitted changes are included in the search.
253
- * @returns A copy of the leaf preimage at the given index or undefined if the leaf was not found.
254
- */
255
- public async getLatestLeafPreimageCopy(
256
- index: bigint,
257
- includeUncommitted: boolean,
258
- ): Promise<IndexedTreeLeafPreimage | undefined> {
259
- const preimage = !includeUncommitted
260
- ? await this.getDbPreimage(index)
261
- : this.getCachedPreimage(index) ?? (await this.getDbPreimage(index));
262
- return preimage && this.leafPreimageFactory.clone(preimage);
263
- }
264
-
265
- /**
266
- * Returns the index of a leaf given its value, or undefined if no leaf with that value is found.
267
- * @param value - The leaf value to look for.
268
- * @param includeUncommitted - Indicates whether to include uncommitted data.
269
- * @returns The index of the first leaf found with a given value (undefined if not found).
270
- */
271
- public async findLeafIndex(value: Buffer, includeUncommitted: boolean): Promise<bigint | undefined> {
272
- const leaf = this.leafFactory.fromBuffer(value);
273
- let index = await this.db
274
- .get(buildDbKeyForLeafIndex(this.getName(), leaf.getKey()))
275
- .then(data => toBigIntBE(data))
276
- .catch(() => undefined);
277
-
278
- if (includeUncommitted && index === undefined) {
279
- const cachedIndex = this.getCachedLeafIndex(leaf.getKey());
280
- index = cachedIndex;
281
- }
282
- return index;
283
- }
284
-
285
- /**
286
- * Initializes the tree.
287
- * @param prefilledSize - A number of leaves that are prefilled with values.
288
- * @returns Empty promise.
289
- *
290
- * @remarks Explanation of pre-filling:
291
- * There needs to be an initial (0,0,0) leaf in the tree, so that when we insert the first 'proper' leaf, we can
292
- * prove that any value greater than 0 doesn't exist in the tree yet. We prefill/pad the tree with "the number of
293
- * leaves that are added by one block" so that the first 'proper' block can insert a full subtree.
294
- *
295
- * Without this padding, there would be a leaf (0,0,0) at leaf index 0, making it really difficult to insert e.g.
296
- * 1024 leaves for the first block, because there's only neat space for 1023 leaves after 0. By padding with 1023
297
- * more leaves, we can then insert the first block of 1024 leaves into indices 1024:2047.
298
- */
299
- public async init(prefilledSize: number): Promise<void> {
300
- if (prefilledSize < 1) {
301
- throw new Error(`Prefilled size must be at least 1!`);
302
- }
303
-
304
- const leaves: IndexedTreeLeafPreimage[] = [];
305
- for (let i = 0n; i < prefilledSize; i++) {
306
- const newLeaf = this.leafFactory.buildDummy(i);
307
- const newLeafPreimage = this.leafPreimageFactory.fromLeaf(newLeaf, i + 1n, i + 1n);
308
- leaves.push(newLeafPreimage);
309
- }
310
-
311
- // Make the last leaf point to the first leaf
312
- leaves[prefilledSize - 1] = this.leafPreimageFactory.fromLeaf(leaves[prefilledSize - 1].asLeaf(), 0n, 0n);
313
-
314
- await this.encodeAndAppendLeaves(leaves, true);
315
- await this.commit();
316
- }
317
-
318
- /**
319
- * Commits all the leaves to the database and removes them from a cache.
320
- */
321
- private async commitLeaves(): Promise<void> {
322
- const batch = this.db.batch();
323
- const keys = Object.getOwnPropertyNames(this.cachedLeafPreimages);
324
- for (const key of keys) {
325
- const leaf = this.cachedLeafPreimages[key];
326
- const index = BigInt(key);
327
- batch.put(buildDbKeyForPreimage(this.getName(), index), leaf.toBuffer());
328
- batch.put(buildDbKeyForLeafIndex(this.getName(), leaf.getKey()), toBufferBE(index, 32));
329
- }
330
- await batch.write();
331
- this.clearCachedLeaves();
332
- }
333
-
334
- /**
335
- * Clears the cache.
336
- */
337
- private clearCachedLeaves() {
338
- this.cachedLeafPreimages = {};
339
- }
340
-
341
- /**
342
- * Updates a leaf in the tree.
343
- * @param preimage - New contents of the leaf.
344
- * @param index - Index of the leaf to be updated.
345
- */
346
- protected async updateLeaf(preimage: IndexedTreeLeafPreimage, index: bigint) {
347
- if (index > this.maxIndex) {
348
- throw Error(`Index out of bounds. Index ${index}, max index: ${this.maxIndex}.`);
349
- }
350
-
351
- this.cachedLeafPreimages[index.toString()] = preimage;
352
- const encodedLeaf = this.encodeLeaf(preimage, true);
353
- await this.addLeafToCacheAndHashToRoot(encodedLeaf, index);
354
- const numLeaves = this.getNumLeaves(true);
355
- if (index >= numLeaves) {
356
- this.cachedSize = index + 1n;
357
- }
358
- }
359
-
360
- /* eslint-disable jsdoc/require-description-complete-sentence */
361
- /* The following doc block messes up with complete-sentence, so we just disable it */
362
-
363
- /**
364
- *
365
- * Each base rollup needs to provide non membership / inclusion proofs for each of the nullifier.
366
- * This method will return membership proofs and perform partial node updates that will
367
- * allow the circuit to incrementally update the tree and perform a batch insertion.
368
- *
369
- * This offers massive circuit performance savings over doing incremental insertions.
370
- *
371
- * WARNING: This function has side effects, it will insert values into the tree.
372
- *
373
- * Assumptions:
374
- * 1. There are 8 nullifiers provided and they are either unique or empty. (denoted as 0)
375
- * 2. If kc 0 has 1 nullifier, and kc 1 has 3 nullifiers the layout will assume to be the sparse
376
- * nullifier layout: [kc0-0, 0, 0, 0, kc1-0, kc1-1, kc1-2, 0]
377
- *
378
- * Algorithm overview
379
- *
380
- * In general, if we want to batch insert items, we first need to update their low nullifier to point to them,
381
- * then batch insert all of the values at once in the final step.
382
- * To update a low nullifier, we provide an insertion proof that the low nullifier currently exists to the
383
- * circuit, then update the low nullifier.
384
- * Updating this low nullifier will in turn change the root of the tree. Therefore future low nullifier insertion proofs
385
- * must be given against this new root.
386
- * As a result, each low nullifier membership proof will be provided against an intermediate tree state, each with differing
387
- * roots.
388
- *
389
- * This become tricky when two items that are being batch inserted need to update the same low nullifier, or need to use
390
- * a value that is part of the same batch insertion as their low nullifier. What we do to avoid this case is to
391
- * update the existing leaves in the tree with the nullifiers in high to low order, ensuring that this case never occurs.
392
- * The circuit has to sort the nullifiers (or take a hint of the sorted nullifiers and prove that it's a valid permutation).
393
- * Then we just batch insert the new nullifiers in the original order.
394
- *
395
- * The following example will illustrate attempting to insert 2,3,20,19 into a tree already containing 0,5,10,15
396
- *
397
- * The example will explore two cases. In each case the values low nullifier will exist within the batch insertion,
398
- * One where the low nullifier comes before the item in the set (2,3), and one where it comes after (20,19).
399
- *
400
- * First, we sort the nullifiers high to low, that's 20,19,3,2
401
- *
402
- * The original tree: Pending insertion subtree
403
- *
404
- * index 0 1 2 3 - - - -
405
- * ------------------------------------- ----------------------------
406
- * val 0 5 10 15 - - - -
407
- * nextIdx 1 2 3 0 - - - -
408
- * nextVal 5 10 15 0 - - - -
409
- *
410
- *
411
- * Inserting 20:
412
- * 1. Find the low nullifier (3) - provide inclusion proof
413
- * 2. Update its pointers
414
- * 3. Insert 20 into the pending subtree
415
- *
416
- * index 0 1 2 3 - - 6 -
417
- * ------------------------------------- ----------------------------
418
- * val 0 5 10 15 - - 20 -
419
- * nextIdx 1 2 3 6 - - 0 -
420
- * nextVal 5 10 15 20 - - 0 -
421
- *
422
- * Inserting 19:
423
- * 1. Find the low nullifier (3) - provide inclusion proof
424
- * 2. Update its pointers
425
- * 3. Insert 19 into the pending subtree
426
- *
427
- * index 0 1 2 3 - - 6 7
428
- * ------------------------------------- ----------------------------
429
- * val 0 5 10 15 - - 20 19
430
- * nextIdx 1 2 3 7 - - 0 6
431
- * nextVal 5 10 15 19 - - 0 20
432
- *
433
- * Inserting 3:
434
- * 1. Find the low nullifier (0) - provide inclusion proof
435
- * 2. Update its pointers
436
- * 3. Insert 3 into the pending subtree
437
- *
438
- * index 0 1 2 3 - 5 6 7
439
- * ------------------------------------- ----------------------------
440
- * val 0 5 10 15 - 3 20 19
441
- * nextIdx 5 2 3 7 - 1 0 6
442
- * nextVal 3 10 15 19 - 5 0 20
443
- *
444
- * Inserting 2:
445
- * 1. Find the low nullifier (0) - provide inclusion proof
446
- * 2. Update its pointers
447
- * 3. Insert 2 into the pending subtree
448
- *
449
- * index 0 1 2 3 4 5 6 7
450
- * ------------------------------------- ----------------------------
451
- * val 0 5 10 15 2 3 20 19
452
- * nextIdx 4 2 3 7 5 1 0 6
453
- * nextVal 2 10 15 19 3 5 0 20
454
- *
455
- * Perform subtree insertion
456
- *
457
- * index 0 1 2 3 4 5 6 7
458
- * ---------------------------------------------------------------------
459
- * val 0 5 10 15 2 3 20 19
460
- * nextIdx 4 2 3 7 5 1 0 6
461
- * nextVal 2 10 15 19 3 5 0 20
462
- *
463
- * TODO: this implementation will change once the zero value is changed from h(0,0,0). Changes incoming over the next sprint
464
- * @param leaves - Values to insert into the tree.
465
- * @param subtreeHeight - Height of the subtree.
466
- * @returns The data for the leaves to be updated when inserting the new ones.
467
- */
468
- public async batchInsert<
469
- TreeHeight extends number,
470
- SubtreeHeight extends number,
471
- SubtreeSiblingPathHeight extends number,
472
- >(
473
- leaves: Buffer[],
474
- subtreeHeight: SubtreeHeight,
475
- ): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>> {
476
- const emptyLowLeafWitness = getEmptyLowLeafWitness(this.getDepth() as TreeHeight, this.leafPreimageFactory);
477
- // Accumulators
478
- const lowLeavesWitnesses: LowLeafWitnessData<TreeHeight>[] = leaves.map(() => emptyLowLeafWitness);
479
- const pendingInsertionSubtree: IndexedTreeLeafPreimage[] = leaves.map(() => this.leafPreimageFactory.empty());
480
-
481
- // Start info
482
- const startInsertionIndex = this.getNumLeaves(true);
483
-
484
- const leavesToInsert = leaves.map(leaf => this.leafFactory.fromBuffer(leaf));
485
- const sortedDescendingLeafTuples = leavesToInsert
486
- .map((leaf, index) => ({ leaf, index }))
487
- .sort((a, b) => Number(b.leaf.getKey() - a.leaf.getKey()));
488
- const sortedDescendingLeaves = sortedDescendingLeafTuples.map(leafTuple => leafTuple.leaf);
489
-
490
- // Get insertion path for each leaf
491
- for (let i = 0; i < leavesToInsert.length; i++) {
492
- const newLeaf = sortedDescendingLeaves[i];
493
- const originalIndex = leavesToInsert.indexOf(newLeaf);
494
-
495
- if (newLeaf.isEmpty()) {
496
- continue;
497
- }
498
-
499
- const indexOfPrevious = await this.findIndexOfPreviousKey(newLeaf.getKey(), true);
500
- if (indexOfPrevious === undefined) {
501
- return {
502
- lowLeavesWitnessData: undefined,
503
- sortedNewLeaves: sortedDescendingLeafTuples.map(leafTuple => leafTuple.leaf.toBuffer()),
504
- sortedNewLeavesIndexes: sortedDescendingLeafTuples.map(leafTuple => leafTuple.index),
505
- newSubtreeSiblingPath: await this.getSubtreeSiblingPath(subtreeHeight, true),
506
- };
507
- }
508
-
509
- // get the low leaf (existence checked in getting index)
510
- const lowLeafPreimage = (await this.getLatestLeafPreimageCopy(indexOfPrevious.index, true))!;
511
- const siblingPath = await this.getSiblingPath<TreeHeight>(BigInt(indexOfPrevious.index), true);
512
-
513
- const witness: LowLeafWitnessData<TreeHeight> = {
514
- leafPreimage: lowLeafPreimage,
515
- index: BigInt(indexOfPrevious.index),
516
- siblingPath,
517
- };
518
-
519
- // Update the running paths
520
- lowLeavesWitnesses[i] = witness;
521
-
522
- const currentPendingPreimageLeaf = this.leafPreimageFactory.fromLeaf(
523
- newLeaf,
524
- lowLeafPreimage.getNextKey(),
525
- lowLeafPreimage.getNextIndex(),
526
- );
527
-
528
- pendingInsertionSubtree[originalIndex] = currentPendingPreimageLeaf;
529
-
530
- const newLowLeafPreimage = this.leafPreimageFactory.fromLeaf(
531
- lowLeafPreimage.asLeaf(),
532
- newLeaf.getKey(),
533
- startInsertionIndex + BigInt(originalIndex),
534
- );
535
-
536
- const lowLeafIndex = indexOfPrevious.index;
537
- this.cachedLeafPreimages[lowLeafIndex.toString()] = newLowLeafPreimage;
538
- await this.updateLeaf(newLowLeafPreimage, lowLeafIndex);
539
- }
540
-
541
- const newSubtreeSiblingPath = await this.getSubtreeSiblingPath<SubtreeHeight, SubtreeSiblingPathHeight>(
542
- subtreeHeight,
543
- true,
544
- );
545
-
546
- // Perform batch insertion of new pending values
547
- // Note: In this case we set `hash0Leaf` param to false because batch insertion algorithm use forced null leaf
548
- // inclusion. See {@link encodeLeaf} for a more through param explanation.
549
- await this.encodeAndAppendLeaves(pendingInsertionSubtree, false);
550
-
551
- return {
552
- lowLeavesWitnessData: lowLeavesWitnesses,
553
- sortedNewLeaves: sortedDescendingLeafTuples.map(leafTuple => leafTuple.leaf.toBuffer()),
554
- sortedNewLeavesIndexes: sortedDescendingLeafTuples.map(leafTuple => leafTuple.index),
555
- newSubtreeSiblingPath,
556
- };
557
- }
558
-
559
- async getSubtreeSiblingPath<SubtreeHeight extends number, SubtreeSiblingPathHeight extends number>(
560
- subtreeHeight: SubtreeHeight,
561
- includeUncommitted: boolean,
562
- ): Promise<SiblingPath<SubtreeSiblingPathHeight>> {
563
- const nextAvailableLeafIndex = this.getNumLeaves(includeUncommitted);
564
- const fullSiblingPath = await this.getSiblingPath(nextAvailableLeafIndex, includeUncommitted);
565
-
566
- // Drop the first subtreeHeight items since we only care about the path to the subtree root
567
- return fullSiblingPath.getSubtreeSiblingPath(subtreeHeight);
568
- }
569
-
570
- snapshot(blockNumber: number): Promise<IndexedTreeSnapshot> {
571
- return this.#snapshotBuilder.snapshot(blockNumber);
572
- }
573
-
574
- getSnapshot(block: number): Promise<IndexedTreeSnapshot> {
575
- return this.#snapshotBuilder.getSnapshot(block);
576
- }
577
-
578
- /**
579
- * Encodes leaves and appends them to a tree.
580
- * @param preimages - Leaves to encode.
581
- * @param hash0Leaf - Indicates whether 0 value leaf should be hashed. See {@link encodeLeaf}.
582
- * @returns Empty promise
583
- */
584
- private async encodeAndAppendLeaves(preimages: IndexedTreeLeafPreimage[], hash0Leaf: boolean): Promise<void> {
585
- const startInsertionIndex = this.getNumLeaves(true);
586
-
587
- const hashedLeaves = preimages.map((preimage, i) => {
588
- this.cachedLeafPreimages[(startInsertionIndex + BigInt(i)).toString()] = preimage;
589
- return this.encodeLeaf(preimage, hash0Leaf);
590
- });
591
-
592
- await super.appendLeaves(hashedLeaves);
593
- }
594
-
595
- /**
596
- * Encode a leaf into a buffer.
597
- * @param leaf - Leaf to encode.
598
- * @param hash0Leaf - Indicates whether 0 value leaf should be hashed. Not hashing 0 value can represent a forced
599
- * null leaf insertion. Detecting this case by checking for 0 value is safe as in the case of
600
- * nullifier it is improbable that a valid nullifier would be 0.
601
- * @returns Leaf encoded in a buffer.
602
- */
603
- private encodeLeaf(leaf: IndexedTreeLeafPreimage, hash0Leaf: boolean): Buffer {
604
- let encodedLeaf;
605
- if (!hash0Leaf && leaf.getKey() == 0n) {
606
- encodedLeaf = toBufferBE(0n, 32);
607
- } else {
608
- encodedLeaf = this.hasher.hashInputs(leaf.toHashInputs());
609
- }
610
- return encodedLeaf;
611
- }
612
- }