@aztec/merkle-tree 0.16.1 → 0.16.3

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.
Files changed (68) hide show
  1. package/dest/index.d.ts +5 -1
  2. package/dest/index.d.ts.map +1 -1
  3. package/dest/index.js +6 -2
  4. package/dest/interfaces/append_only_tree.d.ts +2 -1
  5. package/dest/interfaces/append_only_tree.d.ts.map +1 -1
  6. package/dest/interfaces/indexed_tree.d.ts +38 -17
  7. package/dest/interfaces/indexed_tree.d.ts.map +1 -1
  8. package/dest/interfaces/merkle_tree.d.ts +7 -0
  9. package/dest/interfaces/merkle_tree.d.ts.map +1 -1
  10. package/dest/interfaces/update_only_tree.d.ts +3 -3
  11. package/dest/interfaces/update_only_tree.d.ts.map +1 -1
  12. package/dest/load_tree.d.ts +2 -1
  13. package/dest/load_tree.d.ts.map +1 -1
  14. package/dest/load_tree.js +1 -2
  15. package/dest/new_tree.d.ts +1 -1
  16. package/dest/new_tree.d.ts.map +1 -1
  17. package/dest/new_tree.js +2 -2
  18. package/dest/snapshots/append_only_snapshot.d.ts +30 -0
  19. package/dest/snapshots/append_only_snapshot.d.ts.map +1 -0
  20. package/dest/snapshots/append_only_snapshot.js +200 -0
  21. package/dest/snapshots/base_full_snapshot.d.ts +50 -0
  22. package/dest/snapshots/base_full_snapshot.d.ts.map +1 -0
  23. package/dest/snapshots/base_full_snapshot.js +179 -0
  24. package/dest/snapshots/full_snapshot.d.ts +22 -0
  25. package/dest/snapshots/full_snapshot.d.ts.map +1 -0
  26. package/dest/snapshots/full_snapshot.js +21 -0
  27. package/dest/snapshots/indexed_tree_snapshot.d.ts +15 -0
  28. package/dest/snapshots/indexed_tree_snapshot.d.ts.map +1 -0
  29. package/dest/snapshots/indexed_tree_snapshot.js +75 -0
  30. package/dest/snapshots/snapshot_builder.d.ts +76 -0
  31. package/dest/snapshots/snapshot_builder.d.ts.map +1 -0
  32. package/dest/snapshots/snapshot_builder.js +2 -0
  33. package/dest/snapshots/snapshot_builder_test_suite.d.ts +5 -0
  34. package/dest/snapshots/snapshot_builder_test_suite.d.ts.map +1 -0
  35. package/dest/snapshots/snapshot_builder_test_suite.js +163 -0
  36. package/dest/sparse_tree/sparse_tree.d.ts +5 -0
  37. package/dest/sparse_tree/sparse_tree.d.ts.map +1 -1
  38. package/dest/sparse_tree/sparse_tree.js +18 -1
  39. package/dest/standard_indexed_tree/standard_indexed_tree.d.ts +111 -81
  40. package/dest/standard_indexed_tree/standard_indexed_tree.d.ts.map +1 -1
  41. package/dest/standard_indexed_tree/standard_indexed_tree.js +225 -259
  42. package/dest/standard_indexed_tree/test/standard_indexed_tree_with_append.d.ts.map +1 -1
  43. package/dest/standard_indexed_tree/test/standard_indexed_tree_with_append.js +13 -19
  44. package/dest/standard_tree/standard_tree.d.ts +5 -0
  45. package/dest/standard_tree/standard_tree.d.ts.map +1 -1
  46. package/dest/standard_tree/standard_tree.js +24 -1
  47. package/dest/tree_base.d.ts +9 -4
  48. package/dest/tree_base.d.ts.map +1 -1
  49. package/dest/tree_base.js +16 -7
  50. package/package.json +4 -3
  51. package/src/index.ts +5 -1
  52. package/src/interfaces/append_only_tree.ts +2 -1
  53. package/src/interfaces/indexed_tree.ts +50 -28
  54. package/src/interfaces/merkle_tree.ts +8 -0
  55. package/src/interfaces/update_only_tree.ts +3 -4
  56. package/src/load_tree.ts +2 -2
  57. package/src/new_tree.ts +2 -2
  58. package/src/snapshots/append_only_snapshot.ts +243 -0
  59. package/src/snapshots/base_full_snapshot.ts +232 -0
  60. package/src/snapshots/full_snapshot.ts +26 -0
  61. package/src/snapshots/indexed_tree_snapshot.ts +108 -0
  62. package/src/snapshots/snapshot_builder.ts +84 -0
  63. package/src/snapshots/snapshot_builder_test_suite.ts +218 -0
  64. package/src/sparse_tree/sparse_tree.ts +16 -0
  65. package/src/standard_indexed_tree/standard_indexed_tree.ts +325 -304
  66. package/src/standard_indexed_tree/test/standard_indexed_tree_with_append.ts +23 -21
  67. package/src/standard_tree/standard_tree.ts +21 -0
  68. package/src/tree_base.ts +28 -7
@@ -1 +1 @@
1
- {"version":3,"file":"sparse_tree.d.ts","sourceRoot":"","sources":["../../src/sparse_tree/sparse_tree.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAgB,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEzD;;GAEG;AACH,qBAAa,UAAW,SAAQ,QAAS,YAAW,cAAc;IAChE;;;;OAIG;IACU,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAmBpE"}
1
+ {"version":3,"file":"sparse_tree.d.ts","sourceRoot":"","sources":["../../src/sparse_tree/sparse_tree.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AAEnE,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAgB,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEzD;;GAEG;AACH,qBAAa,UAAW,SAAQ,QAAS,YAAW,cAAc;;IAGhE;;;;OAIG;IACU,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoB5D,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAI9C,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAIjD,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,mBAAmB,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;CAGhG"}
@@ -1,8 +1,15 @@
1
+ var _SparseTree_snapshotBuilder;
2
+ import { __classPrivateFieldGet } from "tslib";
3
+ import { FullTreeSnapshotBuilder } from '../snapshots/full_snapshot.js';
1
4
  import { INITIAL_LEAF, TreeBase } from '../tree_base.js';
2
5
  /**
3
6
  * A Merkle tree implementation that uses a LevelDB database to store the tree.
4
7
  */
5
8
  export class SparseTree extends TreeBase {
9
+ constructor() {
10
+ super(...arguments);
11
+ _SparseTree_snapshotBuilder.set(this, new FullTreeSnapshotBuilder(this.db, this));
12
+ }
6
13
  /**
7
14
  * Updates a leaf in the tree.
8
15
  * @param leaf - New contents of the leaf.
@@ -27,5 +34,15 @@ export class SparseTree extends TreeBase {
27
34
  this.cachedSize = (this.cachedSize ?? this.size) + 1n;
28
35
  }
29
36
  }
37
+ snapshot(block) {
38
+ return __classPrivateFieldGet(this, _SparseTree_snapshotBuilder, "f").snapshot(block);
39
+ }
40
+ getSnapshot(block) {
41
+ return __classPrivateFieldGet(this, _SparseTree_snapshotBuilder, "f").getSnapshot(block);
42
+ }
43
+ findLeafIndex(_value, _includeUncommitted) {
44
+ throw new Error('Finding leaf index is not supported for sparse trees');
45
+ }
30
46
  }
31
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3BhcnNlX3RyZWUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3BhcnNlX3RyZWUvc3BhcnNlX3RyZWUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLFlBQVksRUFBRSxRQUFRLEVBQUUsTUFBTSxpQkFBaUIsQ0FBQztBQUV6RDs7R0FFRztBQUNILE1BQU0sT0FBTyxVQUFXLFNBQVEsUUFBUTtJQUN0Qzs7OztPQUlHO0lBQ0ksS0FBSyxDQUFDLFVBQVUsQ0FBQyxJQUFZLEVBQUUsS0FBYTtRQUNqRCxJQUFJLEtBQUssR0FBRyxJQUFJLENBQUMsUUFBUSxFQUFFO1lBQ3pCLE1BQU0sS0FBSyxDQUFDLDhCQUE4QixLQUFLLGdCQUFnQixJQUFJLENBQUMsUUFBUSxHQUFHLENBQUMsQ0FBQztTQUNsRj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xJQUFJLENBQUMsVUFBVSxJQUFJLElBQUksQ0FBQyxJQUFJLENBQUMsR0FBRyxFQUFFLENBQUM7U0FDdkQ7SUFDSCxDQUFDO0lBRU0sUUFBUSxDQUFDLEtBQWE7UUFDM0IsT0FBTyx1QkFBQSxJQUFJLG1DQUFpQixDQUFDLFFBQVEsQ0FBQyxLQUFLLENBQUMsQ0FBQztJQUMvQyxDQUFDO0lBRU0sV0FBVyxDQUFDLEtBQWE7UUFDOUIsT0FBTyx1QkFBQSxJQUFJLG1DQUFpQixDQUFDLFdBQVcsQ0FBQyxLQUFLLENBQUMsQ0FBQztJQUNsRCxDQUFDO0lBRU0sYUFBYSxDQUFDLE1BQWMsRUFBRSxtQkFBNEI7UUFDL0QsTUFBTSxJQUFJLEtBQUssQ0FBQyxzREFBc0QsQ0FBQyxDQUFDO0lBQzFFLENBQUM7Q0FDRiJ9
@@ -1,32 +1,63 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- import { SiblingPath } from '@aztec/types';
3
- import { IndexedTree, LeafData } from '../interfaces/indexed_tree.js';
2
+ import { IndexedTreeLeaf, IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
3
+ import { Hasher, SiblingPath } from '@aztec/types';
4
+ import { LevelUp } from 'levelup';
5
+ import { BatchInsertionResult, IndexedTree, IndexedTreeSnapshot } from '../index.js';
4
6
  import { TreeBase } from '../tree_base.js';
5
7
  /**
6
- * All of the data to be return during batch insertion.
8
+ * Factory for creating leaf preimages.
7
9
  */
8
- export interface LowLeafWitnessData<N extends number> {
10
+ export interface PreimageFactory {
9
11
  /**
10
- * Preimage of the low nullifier that proves non membership.
12
+ * Creates a new preimage from a leaf.
13
+ * @param leaf - Leaf to create a preimage from.
14
+ * @param nextKey - Next key of the leaf.
15
+ * @param nextIndex - Next index of the leaf.
11
16
  */
12
- leafData: LeafData;
17
+ fromLeaf(leaf: IndexedTreeLeaf, nextKey: bigint, nextIndex: bigint): IndexedTreeLeafPreimage;
13
18
  /**
14
- * Sibling path to prove membership of low nullifier.
19
+ * Creates a new preimage from a buffer.
20
+ * @param buffer - Buffer to create a preimage from.
15
21
  */
16
- siblingPath: SiblingPath<N>;
22
+ fromBuffer(buffer: Buffer): IndexedTreeLeafPreimage;
17
23
  /**
18
- * The index of low nullifier.
24
+ * Creates an empty preimage.
19
25
  */
20
- index: bigint;
26
+ empty(): IndexedTreeLeafPreimage;
27
+ /**
28
+ * Creates a copy of a preimage.
29
+ * @param preimage - Preimage to be cloned.
30
+ */
31
+ clone(preimage: IndexedTreeLeafPreimage): IndexedTreeLeafPreimage;
32
+ }
33
+ /**
34
+ * Factory for creating leaves.
35
+ */
36
+ export interface LeafFactory {
37
+ /**
38
+ * Creates a new leaf from a buffer.
39
+ * @param key - Key of the leaf.
40
+ */
41
+ buildDummy(key: bigint): IndexedTreeLeaf;
42
+ /**
43
+ * Creates a new leaf from a buffer.
44
+ * @param buffer - Buffer to create a leaf from.
45
+ */
46
+ fromBuffer(buffer: Buffer): IndexedTreeLeaf;
21
47
  }
48
+ export declare const buildDbKeyForPreimage: (name: string, index: bigint) => string;
49
+ export declare const buildDbKeyForLeafIndex: (name: string, key: bigint) => string;
22
50
  /**
23
- * Indexed merkle tree.
51
+ * Standard implementation of an indexed tree.
24
52
  */
25
53
  export declare class StandardIndexedTree extends TreeBase implements IndexedTree {
26
- protected leaves: LeafData[];
27
- protected cachedLeaves: {
28
- [key: number]: LeafData;
54
+ #private;
55
+ protected leafPreimageFactory: PreimageFactory;
56
+ protected leafFactory: LeafFactory;
57
+ protected cachedLeafPreimages: {
58
+ [key: string]: IndexedTreeLeafPreimage;
29
59
  };
60
+ constructor(db: LevelUp, hasher: Hasher, name: string, depth: number, size: bigint | undefined, leafPreimageFactory: PreimageFactory, leafFactory: LeafFactory, root?: Buffer);
30
61
  /**
31
62
  * Appends the given leaves to the tree.
32
63
  * @param _leaves - The leaves to append.
@@ -53,33 +84,39 @@ export declare class StandardIndexedTree extends TreeBase implements IndexedTree
53
84
  getLeafValue(index: bigint, includeUncommitted: boolean): Promise<Buffer | undefined>;
54
85
  /**
55
86
  * Finds the index of the largest leaf whose value is less than or equal to the provided value.
56
- * @param newValue - The new value to be inserted into the tree.
87
+ * @param newKey - The new key to be inserted into the tree.
57
88
  * @param includeUncommitted - If true, the uncommitted changes are included in the search.
58
89
  * @returns The found leaf index and a flag indicating if the corresponding leaf's value is equal to `newValue`.
59
90
  */
60
- findIndexOfPreviousValue(newValue: bigint, includeUncommitted: boolean): {
91
+ findIndexOfPreviousKey(newKey: bigint, includeUncommitted: boolean): Promise<{
61
92
  /**
62
93
  * The index of the found leaf.
63
94
  */
64
- index: number;
95
+ index: bigint;
65
96
  /**
66
97
  * A flag indicating if the corresponding leaf's value is equal to `newValue`.
67
98
  */
68
99
  alreadyPresent: boolean;
69
- };
100
+ } | undefined>;
101
+ private getCachedLowLeafIndex;
102
+ private getCachedLeafIndex;
103
+ private getDbLowLeafIndex;
104
+ private getDbPreimage;
105
+ private getCachedPreimage;
70
106
  /**
71
- * Gets the latest LeafData copy.
72
- * @param index - Index of the leaf of which to obtain the LeafData copy.
107
+ * Gets the latest LeafPreimage copy.
108
+ * @param index - Index of the leaf of which to obtain the LeafPreimage copy.
73
109
  * @param includeUncommitted - If true, the uncommitted changes are included in the search.
74
- * @returns A copy of the leaf data at the given index or undefined if the leaf was not found.
110
+ * @returns A copy of the leaf preimage at the given index or undefined if the leaf was not found.
75
111
  */
76
- getLatestLeafDataCopy(index: number, includeUncommitted: boolean): LeafData | undefined;
112
+ getLatestLeafPreimageCopy(index: bigint, includeUncommitted: boolean): Promise<IndexedTreeLeafPreimage | undefined>;
77
113
  /**
78
- * Finds the index of the minimum value in an array.
79
- * @param values - The collection of values to be searched.
80
- * @returns The index of the minimum value in the array.
114
+ * Returns the index of a leaf given its value, or undefined if no leaf with that value is found.
115
+ * @param value - The leaf value to look for.
116
+ * @param includeUncommitted - Indicates whether to include uncommitted data.
117
+ * @returns The index of the first leaf found with a given value (undefined if not found).
81
118
  */
82
- private findMinIndex;
119
+ findLeafIndex(value: Buffer, includeUncommitted: boolean): Promise<bigint | undefined>;
83
120
  /**
84
121
  * Initializes the tree.
85
122
  * @param prefilledSize - A number of leaves that are prefilled with values.
@@ -95,10 +132,6 @@ export declare class StandardIndexedTree extends TreeBase implements IndexedTree
95
132
  * more leaves, we can then insert the first block of 1024 leaves into indices 1024:2047.
96
133
  */
97
134
  init(prefilledSize: number): Promise<void>;
98
- /**
99
- * Loads Merkle tree data from a database and assigns them to this object.
100
- */
101
- initFromDb(): Promise<void>;
102
135
  /**
103
136
  * Commits all the leaves to the database and removes them from a cache.
104
137
  */
@@ -109,10 +142,10 @@ export declare class StandardIndexedTree extends TreeBase implements IndexedTree
109
142
  private clearCachedLeaves;
110
143
  /**
111
144
  * Updates a leaf in the tree.
112
- * @param leaf - New contents of the leaf.
145
+ * @param preimage - New contents of the leaf.
113
146
  * @param index - Index of the leaf to be updated.
114
147
  */
115
- protected updateLeaf(leaf: LeafData, index: bigint): Promise<void>;
148
+ protected updateLeaf(preimage: IndexedTreeLeafPreimage, index: bigint): Promise<void>;
116
149
  /**
117
150
  *
118
151
  * Each base rollup needs to provide non membership / inclusion proofs for each of the nullifier.
@@ -121,8 +154,6 @@ export declare class StandardIndexedTree extends TreeBase implements IndexedTree
121
154
  *
122
155
  * This offers massive circuit performance savings over doing incremental insertions.
123
156
  *
124
- * A description of the algorithm can be found here: https://colab.research.google.com/drive/1A0gizduSi4FIiIJZ8OylwIpO9-OTqV-R
125
- *
126
157
  * WARNING: This function has side effects, it will insert values into the tree.
127
158
  *
128
159
  * Assumptions:
@@ -142,92 +173,91 @@ export declare class StandardIndexedTree extends TreeBase implements IndexedTree
142
173
  * roots.
143
174
  *
144
175
  * This become tricky when two items that are being batch inserted need to update the same low nullifier, or need to use
145
- * a value that is part of the same batch insertion as their low nullifier. In this case a zero low nullifier path is given
146
- * to the circuit, and it must determine from the set of batch inserted values if the insertion is valid.
176
+ * a value that is part of the same batch insertion as their low nullifier. What we do to avoid this case is to
177
+ * update the existing leaves in the tree with the nullifiers in high to low order, ensuring that this case never occurs.
178
+ * The circuit has to sort the nullifiers (or take a hint of the sorted nullifiers and prove that it's a valid permutation).
179
+ * Then we just batch insert the new nullifiers in the original order.
147
180
  *
148
181
  * The following example will illustrate attempting to insert 2,3,20,19 into a tree already containing 0,5,10,15
149
182
  *
150
183
  * The example will explore two cases. In each case the values low nullifier will exist within the batch insertion,
151
184
  * One where the low nullifier comes before the item in the set (2,3), and one where it comes after (20,19).
152
185
  *
186
+ * First, we sort the nullifiers high to low, that's 20,19,3,2
187
+ *
153
188
  * The original tree: Pending insertion subtree
154
189
  *
155
- * index 0 2 3 4 - - - -
190
+ * index 0 1 2 3 - - - -
156
191
  * ------------------------------------- ----------------------------
157
192
  * val 0 5 10 15 - - - -
158
193
  * nextIdx 1 2 3 0 - - - -
159
194
  * nextVal 5 10 15 0 - - - -
160
195
  *
161
196
  *
162
- * Inserting 2: (happy path)
163
- * 1. Find the low nullifier (0) - provide inclusion proof
197
+ * Inserting 20:
198
+ * 1. Find the low nullifier (3) - provide inclusion proof
164
199
  * 2. Update its pointers
165
- * 3. Insert 2 into the pending subtree
200
+ * 3. Insert 20 into the pending subtree
166
201
  *
167
- * index 0 2 3 4 5 - - -
202
+ * index 0 1 2 3 - - 6 -
168
203
  * ------------------------------------- ----------------------------
169
- * val 0 5 10 15 2 - - -
170
- * nextIdx 5 2 3 0 2 - - -
171
- * nextVal 2 10 15 0 5 - - -
172
- *
173
- * Inserting 3: The low nullifier exists within the insertion current subtree
174
- * 1. When looking for the low nullifier for 3, we will receive 0 again as we have not inserted 2 into the main tree
175
- * This is problematic, as we cannot use either 0 or 2 as our inclusion proof.
176
- * Why cant we?
177
- * - Index 0 has a val 0 and nextVal of 2. This is NOT enough to prove non inclusion of 2.
178
- * - Our existing tree is in a state where we cannot prove non inclusion of 3.
179
- * We do not provide a non inclusion proof to out circuit, but prompt it to look within the insertion subtree.
180
- * 2. Update pending insertion subtree
181
- * 3. Insert 3 into pending subtree
182
- *
183
- * (no inclusion proof provided)
184
- * index 0 2 3 4 5 6 - -
204
+ * val 0 5 10 15 - - 20 -
205
+ * nextIdx 1 2 3 6 - - 0 -
206
+ * nextVal 5 10 15 20 - - 0 -
207
+ *
208
+ * Inserting 19:
209
+ * 1. Find the low nullifier (3) - provide inclusion proof
210
+ * 2. Update its pointers
211
+ * 3. Insert 19 into the pending subtree
212
+ *
213
+ * index 0 1 2 3 - - 6 7
185
214
  * ------------------------------------- ----------------------------
186
- * val 0 5 10 15 2 3 - -
187
- * nextIdx 5 2 3 0 6 2 - -
188
- * nextVal 2 10 15 0 3 5 - -
215
+ * val 0 5 10 15 - - 20 19
216
+ * nextIdx 1 2 3 7 - - 0 6
217
+ * nextVal 5 10 15 19 - - 0 20
189
218
  *
190
- * Inserting 20: (happy path)
191
- * 1. Find the low nullifier (15) - provide inclusion proof
219
+ * Inserting 3:
220
+ * 1. Find the low nullifier (0) - provide inclusion proof
192
221
  * 2. Update its pointers
193
- * 3. Insert 20 into the pending subtree
222
+ * 3. Insert 3 into the pending subtree
194
223
  *
195
- * index 0 2 3 4 5 6 7 -
224
+ * index 0 1 2 3 - 5 6 7
196
225
  * ------------------------------------- ----------------------------
197
- * val 0 5 10 15 2 3 20 -
198
- * nextIdx 5 2 3 7 6 2 0 -
199
- * nextVal 2 10 15 20 3 5 0 -
226
+ * val 0 5 10 15 - 3 20 19
227
+ * nextIdx 5 2 3 7 - 1 0 6
228
+ * nextVal 3 10 15 19 - 5 0 20
200
229
  *
201
- * Inserting 19:
202
- * 1. In this case we can find a low nullifier, but we are updating a low nullifier that has already been updated
203
- * We can provide an inclusion proof of this intermediate tree state.
230
+ * Inserting 2:
231
+ * 1. Find the low nullifier (0) - provide inclusion proof
204
232
  * 2. Update its pointers
205
- * 3. Insert 19 into the pending subtree
233
+ * 3. Insert 2 into the pending subtree
206
234
  *
207
- * index 0 2 3 4 5 6 7 8
235
+ * index 0 1 2 3 4 5 6 7
208
236
  * ------------------------------------- ----------------------------
209
- * val 0 5 10 15 2 3 20 19
210
- * nextIdx 5 2 3 8 6 2 0 7
211
- * nextVal 2 10 15 19 3 5 0 20
237
+ * val 0 5 10 15 2 3 20 19
238
+ * nextIdx 4 2 3 7 5 1 0 6
239
+ * nextVal 2 10 15 19 3 5 0 20
212
240
  *
213
241
  * Perform subtree insertion
214
242
  *
215
- * index 0 2 3 4 5 6 7 8
243
+ * index 0 1 2 3 4 5 6 7
216
244
  * ---------------------------------------------------------------------
217
- * val 0 5 10 15 2 3 20 19
218
- * nextIdx 5 2 3 8 6 2 0 7
219
- * nextVal 2 10 15 19 3 5 0 20
245
+ * val 0 5 10 15 2 3 20 19
246
+ * nextIdx 4 2 3 7 5 1 0 6
247
+ * nextVal 2 10 15 19 3 5 0 20
220
248
  *
221
249
  * TODO: this implementation will change once the zero value is changed from h(0,0,0). Changes incoming over the next sprint
222
250
  * @param leaves - Values to insert into the tree.
223
251
  * @param subtreeHeight - Height of the subtree.
224
252
  * @returns The data for the leaves to be updated when inserting the new ones.
225
253
  */
226
- batchInsert<TreeHeight extends number, SubtreeHeight extends number, SubtreeSiblingPathHeight extends number>(leaves: Buffer[], subtreeHeight: SubtreeHeight): Promise<[LowLeafWitnessData<TreeHeight>[], SiblingPath<SubtreeSiblingPathHeight>] | [undefined, SiblingPath<SubtreeSiblingPathHeight>]>;
254
+ batchInsert<TreeHeight extends number, SubtreeHeight extends number, SubtreeSiblingPathHeight extends number>(leaves: Buffer[], subtreeHeight: SubtreeHeight): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>>;
227
255
  getSubtreeSiblingPath<SubtreeHeight extends number, SubtreeSiblingPathHeight extends number>(subtreeHeight: SubtreeHeight, includeUncommitted: boolean): Promise<SiblingPath<SubtreeSiblingPathHeight>>;
256
+ snapshot(blockNumber: number): Promise<IndexedTreeSnapshot>;
257
+ getSnapshot(block: number): Promise<IndexedTreeSnapshot>;
228
258
  /**
229
259
  * Encodes leaves and appends them to a tree.
230
- * @param leaves - Leaves to encode.
260
+ * @param preimages - Leaves to encode.
231
261
  * @param hash0Leaf - Indicates whether 0 value leaf should be hashed. See {@link encodeLeaf}.
232
262
  * @returns Empty promise
233
263
  */
@@ -1 +1 @@
1
- {"version":3,"file":"standard_indexed_tree.d.ts","sourceRoot":"","sources":["../../src/standard_indexed_tree/standard_indexed_tree.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAmB3C;;GAEG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC,SAAS,MAAM;IAClD;;OAEG;IACH,QAAQ,EAAE,QAAQ,CAAC;IACnB;;OAEG;IACH,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAC5B;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAkCD;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,QAAS,YAAW,WAAW;IACtE,SAAS,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAM;IAClC,SAAS,CAAC,YAAY,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAAA;KAAE,CAAM;IAEzD;;;;;OAKG;IACI,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrD;;;OAGG;IACU,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAKpC;;;OAGG;IACU,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAKtC;;;;;OAKG;IACI,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAQ5F;;;;;OAKG;IACH,wBAAwB,CACtB,QAAQ,EAAE,MAAM,EAChB,kBAA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