@aztec/merkle-tree 0.22.0 → 0.24.0

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