@aztec/world-state 0.1.0-alpha11
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.
- package/.eslintrc.cjs +1 -0
- package/.tsbuildinfo +1 -0
- package/README.md +40 -0
- package/dest/index.d.ts +4 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +4 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.d.ts +117 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.d.ts.map +1 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.js +133 -0
- package/dest/synchroniser/config.d.ts +19 -0
- package/dest/synchroniser/config.d.ts.map +1 -0
- package/dest/synchroniser/config.js +13 -0
- package/dest/synchroniser/index.d.ts +3 -0
- package/dest/synchroniser/index.d.ts.map +1 -0
- package/dest/synchroniser/index.js +3 -0
- package/dest/synchroniser/server_world_state_synchroniser.d.ts +62 -0
- package/dest/synchroniser/server_world_state_synchroniser.d.ts.map +1 -0
- package/dest/synchroniser/server_world_state_synchroniser.js +134 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.d.ts +2 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.d.ts.map +1 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.js +215 -0
- package/dest/synchroniser/world_state_synchroniser.d.ts +34 -0
- package/dest/synchroniser/world_state_synchroniser.d.ts.map +1 -0
- package/dest/synchroniser/world_state_synchroniser.js +11 -0
- package/dest/utils.d.ts +12 -0
- package/dest/utils.d.ts.map +1 -0
- package/dest/utils.js +14 -0
- package/dest/world-state-db/index.d.ts +165 -0
- package/dest/world-state-db/index.d.ts.map +1 -0
- package/dest/world-state-db/index.js +21 -0
- package/dest/world-state-db/merkle_trees.d.ts +200 -0
- package/dest/world-state-db/merkle_trees.d.ts.map +1 -0
- package/dest/world-state-db/merkle_trees.js +373 -0
- package/package.json +15 -0
- package/src/index.ts +3 -0
- package/src/merkle-tree/merkle_tree_operations_facade.ts +164 -0
- package/src/synchroniser/config.ts +27 -0
- package/src/synchroniser/index.ts +2 -0
- package/src/synchroniser/server_world_state_synchroniser.test.ts +281 -0
- package/src/synchroniser/server_world_state_synchroniser.ts +151 -0
- package/src/synchroniser/world_state_synchroniser.ts +36 -0
- package/src/utils.ts +24 -0
- package/src/world-state-db/index.ts +215 -0
- package/src/world-state-db/merkle_trees.ts +547 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONTRACT_TREE_HEIGHT,
|
|
3
|
+
CONTRACT_TREE_ROOTS_TREE_HEIGHT,
|
|
4
|
+
CircuitsWasm,
|
|
5
|
+
Fr,
|
|
6
|
+
L1_TO_L2_MESSAGES_ROOTS_TREE_HEIGHT,
|
|
7
|
+
L1_TO_L2_MESSAGES_TREE_HEIGHT,
|
|
8
|
+
NULLIFIER_TREE_HEIGHT,
|
|
9
|
+
PRIVATE_DATA_TREE_HEIGHT,
|
|
10
|
+
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
|
|
11
|
+
PUBLIC_DATA_TREE_HEIGHT,
|
|
12
|
+
} from '@aztec/circuits.js';
|
|
13
|
+
import {
|
|
14
|
+
AppendOnlyTree,
|
|
15
|
+
IndexedTree,
|
|
16
|
+
LeafData,
|
|
17
|
+
LowLeafWitnessData,
|
|
18
|
+
Pedersen,
|
|
19
|
+
SiblingPath,
|
|
20
|
+
SparseTree,
|
|
21
|
+
StandardIndexedTree,
|
|
22
|
+
StandardTree,
|
|
23
|
+
UpdateOnlyTree,
|
|
24
|
+
newTree,
|
|
25
|
+
} from '@aztec/merkle-tree';
|
|
26
|
+
import { default as levelup } from 'levelup';
|
|
27
|
+
import {
|
|
28
|
+
CurrentCommitmentTreeRoots,
|
|
29
|
+
INITIAL_NULLIFIER_TREE_SIZE,
|
|
30
|
+
IndexedTreeId,
|
|
31
|
+
MerkleTreeDb,
|
|
32
|
+
MerkleTreeOperations,
|
|
33
|
+
PublicTreeId,
|
|
34
|
+
TreeInfo,
|
|
35
|
+
} from './index.js';
|
|
36
|
+
import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js';
|
|
37
|
+
import { L2Block, MerkleTreeId, merkleTreeIds } from '@aztec/types';
|
|
38
|
+
import { SerialQueue } from '@aztec/foundation/fifo';
|
|
39
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
40
|
+
import { IWasmModule } from '@aztec/foundation/wasm';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A convenience class for managing multiple merkle trees.
|
|
44
|
+
*/
|
|
45
|
+
export class MerkleTrees implements MerkleTreeDb {
|
|
46
|
+
private trees: (AppendOnlyTree | UpdateOnlyTree)[] = [];
|
|
47
|
+
private jobQueue = new SerialQueue();
|
|
48
|
+
|
|
49
|
+
constructor(private db: levelup.LevelUp, private log = createDebugLogger('aztec:merkle_trees')) {}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Initialises the collection of Merkle Trees.
|
|
53
|
+
* @param optionalWasm - WASM instance to use for hashing (if not provided PrimitivesWasm will be used).
|
|
54
|
+
*/
|
|
55
|
+
public async init(optionalWasm?: IWasmModule) {
|
|
56
|
+
const wasm = optionalWasm ?? (await CircuitsWasm.get());
|
|
57
|
+
const hasher = new Pedersen(wasm);
|
|
58
|
+
const contractTree: AppendOnlyTree = await newTree(
|
|
59
|
+
StandardTree,
|
|
60
|
+
this.db,
|
|
61
|
+
hasher,
|
|
62
|
+
`${MerkleTreeId[MerkleTreeId.CONTRACT_TREE]}`,
|
|
63
|
+
CONTRACT_TREE_HEIGHT,
|
|
64
|
+
);
|
|
65
|
+
const contractTreeRootsTree: AppendOnlyTree = await newTree(
|
|
66
|
+
StandardTree,
|
|
67
|
+
this.db,
|
|
68
|
+
hasher,
|
|
69
|
+
`${MerkleTreeId[MerkleTreeId.CONTRACT_TREE_ROOTS_TREE]}`,
|
|
70
|
+
CONTRACT_TREE_ROOTS_TREE_HEIGHT,
|
|
71
|
+
);
|
|
72
|
+
const nullifierTree = await newTree(
|
|
73
|
+
StandardIndexedTree,
|
|
74
|
+
this.db,
|
|
75
|
+
hasher,
|
|
76
|
+
`${MerkleTreeId[MerkleTreeId.NULLIFIER_TREE]}`,
|
|
77
|
+
NULLIFIER_TREE_HEIGHT,
|
|
78
|
+
INITIAL_NULLIFIER_TREE_SIZE,
|
|
79
|
+
);
|
|
80
|
+
const privateDataTree: AppendOnlyTree = await newTree(
|
|
81
|
+
StandardTree,
|
|
82
|
+
this.db,
|
|
83
|
+
hasher,
|
|
84
|
+
`${MerkleTreeId[MerkleTreeId.PRIVATE_DATA_TREE]}`,
|
|
85
|
+
PRIVATE_DATA_TREE_HEIGHT,
|
|
86
|
+
);
|
|
87
|
+
const privateDataTreeRootsTree: AppendOnlyTree = await newTree(
|
|
88
|
+
StandardTree,
|
|
89
|
+
this.db,
|
|
90
|
+
hasher,
|
|
91
|
+
`${MerkleTreeId[MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE]}`,
|
|
92
|
+
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
|
|
93
|
+
);
|
|
94
|
+
const publicDataTree: UpdateOnlyTree = await newTree(
|
|
95
|
+
SparseTree,
|
|
96
|
+
this.db,
|
|
97
|
+
hasher,
|
|
98
|
+
`${MerkleTreeId[MerkleTreeId.PUBLIC_DATA_TREE]}`,
|
|
99
|
+
PUBLIC_DATA_TREE_HEIGHT,
|
|
100
|
+
);
|
|
101
|
+
const l1Tol2MessagesTree: AppendOnlyTree = await newTree(
|
|
102
|
+
StandardTree,
|
|
103
|
+
this.db,
|
|
104
|
+
hasher,
|
|
105
|
+
`${MerkleTreeId[MerkleTreeId.L1_TO_L2_MESSAGES_TREE]}`,
|
|
106
|
+
L1_TO_L2_MESSAGES_TREE_HEIGHT,
|
|
107
|
+
);
|
|
108
|
+
const l1Tol2MessagesRootsTree: AppendOnlyTree = await newTree(
|
|
109
|
+
StandardTree,
|
|
110
|
+
this.db,
|
|
111
|
+
hasher,
|
|
112
|
+
`${MerkleTreeId[MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE]}`,
|
|
113
|
+
L1_TO_L2_MESSAGES_ROOTS_TREE_HEIGHT,
|
|
114
|
+
);
|
|
115
|
+
this.trees = [
|
|
116
|
+
contractTree,
|
|
117
|
+
contractTreeRootsTree,
|
|
118
|
+
nullifierTree,
|
|
119
|
+
privateDataTree,
|
|
120
|
+
privateDataTreeRootsTree,
|
|
121
|
+
publicDataTree,
|
|
122
|
+
l1Tol2MessagesTree,
|
|
123
|
+
l1Tol2MessagesRootsTree,
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
this.jobQueue.start();
|
|
127
|
+
|
|
128
|
+
// The roots trees must contain the empty roots of their data trees
|
|
129
|
+
await this.updateHistoricRootsTrees(true);
|
|
130
|
+
const historicRootsTrees = [contractTreeRootsTree, privateDataTreeRootsTree, l1Tol2MessagesRootsTree];
|
|
131
|
+
await Promise.all(historicRootsTrees.map(tree => tree.commit()));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Method to asynchronously create and initialise a MerkleTrees instance.
|
|
136
|
+
* @param db - The db instance to use for data persistance.
|
|
137
|
+
* @param wasm - WASM instance to use for hashing (if not provided PrimitivesWasm will be used).
|
|
138
|
+
* @returns - A fully initialised MerkleTrees instance.
|
|
139
|
+
*/
|
|
140
|
+
public static async new(db: levelup.LevelUp, wasm?: IWasmModule) {
|
|
141
|
+
const merkleTrees = new MerkleTrees(db);
|
|
142
|
+
await merkleTrees.init(wasm);
|
|
143
|
+
return merkleTrees;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Stops the job queue (waits for all jobs to finish).
|
|
148
|
+
*/
|
|
149
|
+
public async stop() {
|
|
150
|
+
await this.jobQueue.end();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Gets a view of this db that returns uncommitted data.
|
|
155
|
+
* @returns - A facade for this instance.
|
|
156
|
+
*/
|
|
157
|
+
public asLatest(): MerkleTreeOperations {
|
|
158
|
+
return new MerkleTreeOperationsFacade(this, true);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Gets a view of this db that returns committed data only.
|
|
163
|
+
* @returns - A facade for this instance.
|
|
164
|
+
*/
|
|
165
|
+
public asCommitted(): MerkleTreeOperations {
|
|
166
|
+
return new MerkleTreeOperationsFacade(this, false);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Inserts into the roots trees (CONTRACT_TREE_ROOTS_TREE, PRIVATE_DATA_TREE_ROOTS_TREE, L1_TO_L2_MESSAGES_TREE_ROOTS_TREE)
|
|
171
|
+
* the current roots of the corresponding trees (CONTRACT_TREE, PRIVATE_DATA_TREE, L1_TO_L2_MESSAGES_TREE).
|
|
172
|
+
* @param includeUncommitted - Indicates whether to include uncommitted data.
|
|
173
|
+
*/
|
|
174
|
+
public async updateHistoricRootsTrees(includeUncommitted: boolean) {
|
|
175
|
+
for (const [newTree, rootTree] of [
|
|
176
|
+
[MerkleTreeId.PRIVATE_DATA_TREE, MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE],
|
|
177
|
+
[MerkleTreeId.CONTRACT_TREE, MerkleTreeId.CONTRACT_TREE_ROOTS_TREE],
|
|
178
|
+
[MerkleTreeId.L1_TO_L2_MESSAGES_TREE, MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE],
|
|
179
|
+
] as const) {
|
|
180
|
+
const newTreeInfo = await this.getTreeInfo(newTree, includeUncommitted);
|
|
181
|
+
await this.appendLeaves(rootTree, [newTreeInfo.root]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Gets the tree info for the specified tree.
|
|
187
|
+
* @param treeId - Id of the tree to get information from.
|
|
188
|
+
* @param includeUncommitted - Indicates whether to include uncommitted data.
|
|
189
|
+
* @returns The tree info for the specified tree.
|
|
190
|
+
*/
|
|
191
|
+
public async getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise<TreeInfo> {
|
|
192
|
+
return await this.synchronise(() => this._getTreeInfo(treeId, includeUncommitted));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Get the current roots of the commitment trees.
|
|
197
|
+
* @param includeUncommitted - Indicates whether to include uncommitted data.
|
|
198
|
+
* @returns The current roots of the trees.
|
|
199
|
+
*/
|
|
200
|
+
public getCommitmentTreeRoots(includeUncommitted: boolean): CurrentCommitmentTreeRoots {
|
|
201
|
+
const roots = [
|
|
202
|
+
MerkleTreeId.PRIVATE_DATA_TREE,
|
|
203
|
+
MerkleTreeId.CONTRACT_TREE,
|
|
204
|
+
MerkleTreeId.L1_TO_L2_MESSAGES_TREE,
|
|
205
|
+
MerkleTreeId.NULLIFIER_TREE,
|
|
206
|
+
].map(tree => this.trees[tree].getRoot(includeUncommitted));
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
privateDataTreeRoot: roots[0],
|
|
210
|
+
contractDataTreeRoot: roots[1],
|
|
211
|
+
l1Tol2MessagesTreeRoot: roots[2],
|
|
212
|
+
nullifierTreeRoot: roots[3],
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Gets the value at the given index.
|
|
218
|
+
* @param treeId - The ID of the tree to get the leaf value from.
|
|
219
|
+
* @param index - The index of the leaf.
|
|
220
|
+
* @param includeUncommitted - Indicates whether to include uncommitted changes.
|
|
221
|
+
* @returns Leaf value at the given index (undefined if not found).
|
|
222
|
+
*/
|
|
223
|
+
public async getLeafValue(
|
|
224
|
+
treeId: MerkleTreeId,
|
|
225
|
+
index: bigint,
|
|
226
|
+
includeUncommitted: boolean,
|
|
227
|
+
): Promise<Buffer | undefined> {
|
|
228
|
+
return await this.synchronise(() => this.trees[treeId].getLeafValue(index, includeUncommitted));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Gets the sibling path for a leaf in a tree.
|
|
233
|
+
* @param treeId - The ID of the tree.
|
|
234
|
+
* @param index - The index of the leaf.
|
|
235
|
+
* @param includeUncommitted - Indicates whether the sibling path should incro include uncommitted data.
|
|
236
|
+
* @returns The sibling path for the leaf.
|
|
237
|
+
*/
|
|
238
|
+
public async getSiblingPath<N extends number>(
|
|
239
|
+
treeId: MerkleTreeId,
|
|
240
|
+
index: bigint,
|
|
241
|
+
includeUncommitted: boolean,
|
|
242
|
+
): Promise<SiblingPath<N>> {
|
|
243
|
+
return await this.synchronise(() => this._getSiblingPath(treeId, index, includeUncommitted));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Appends leaves to a tree.
|
|
248
|
+
* @param treeId - The ID of the tree.
|
|
249
|
+
* @param leaves - The leaves to append.
|
|
250
|
+
* @returns Empty promise.
|
|
251
|
+
*/
|
|
252
|
+
public async appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise<void> {
|
|
253
|
+
return await this.synchronise(() => this._appendLeaves(treeId, leaves));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Commits all pending updates.
|
|
258
|
+
* @returns Empty promise.
|
|
259
|
+
*/
|
|
260
|
+
public async commit(): Promise<void> {
|
|
261
|
+
return await this.synchronise(() => this._commit());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Rolls back all pending updates.
|
|
266
|
+
* @returns Empty promise.
|
|
267
|
+
*/
|
|
268
|
+
public async rollback(): Promise<void> {
|
|
269
|
+
return await this.synchronise(() => this._rollback());
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Finds the index of the largest leaf whose value is less than or equal to the provided value.
|
|
274
|
+
* @param treeId - The ID of the tree to search.
|
|
275
|
+
* @param value - The value to be inserted into the tree.
|
|
276
|
+
* @param includeUncommitted - If true, the uncommitted changes are included in the search.
|
|
277
|
+
* @returns The found leaf index and a flag indicating if the corresponding leaf's value is equal to `newValue`.
|
|
278
|
+
*/
|
|
279
|
+
public async getPreviousValueIndex(
|
|
280
|
+
treeId: IndexedTreeId,
|
|
281
|
+
value: bigint,
|
|
282
|
+
includeUncommitted: boolean,
|
|
283
|
+
): Promise<{
|
|
284
|
+
/**
|
|
285
|
+
* The index of the found leaf.
|
|
286
|
+
*/
|
|
287
|
+
index: number;
|
|
288
|
+
/**
|
|
289
|
+
* A flag indicating if the corresponding leaf's value is equal to `newValue`.
|
|
290
|
+
*/
|
|
291
|
+
alreadyPresent: boolean;
|
|
292
|
+
}> {
|
|
293
|
+
return await this.synchronise(() =>
|
|
294
|
+
Promise.resolve(this._getIndexedTree(treeId).findIndexOfPreviousValue(value, includeUncommitted)),
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Gets the leaf data at a given index and tree.
|
|
300
|
+
* @param treeId - The ID of the tree get the leaf from.
|
|
301
|
+
* @param index - The index of the leaf to get.
|
|
302
|
+
* @param includeUncommitted - Indicates whether to include uncommitted data.
|
|
303
|
+
* @returns Leaf data.
|
|
304
|
+
*/
|
|
305
|
+
public async getLeafData(
|
|
306
|
+
treeId: IndexedTreeId,
|
|
307
|
+
index: number,
|
|
308
|
+
includeUncommitted: boolean,
|
|
309
|
+
): Promise<LeafData | undefined> {
|
|
310
|
+
return await this.synchronise(() =>
|
|
311
|
+
Promise.resolve(this._getIndexedTree(treeId).getLatestLeafDataCopy(index, includeUncommitted)),
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Returns the index of a leaf given its value, or undefined if no leaf with that value is found.
|
|
317
|
+
* @param treeId - The ID of the tree.
|
|
318
|
+
* @param value - The leaf value to look for.
|
|
319
|
+
* @param includeUncommitted - Indicates whether to include uncommitted data.
|
|
320
|
+
* @returns The index of the first leaf found with a given value (undefined if not found).
|
|
321
|
+
*/
|
|
322
|
+
public async findLeafIndex(
|
|
323
|
+
treeId: MerkleTreeId,
|
|
324
|
+
value: Buffer,
|
|
325
|
+
includeUncommitted: boolean,
|
|
326
|
+
): Promise<bigint | undefined> {
|
|
327
|
+
return await this.synchronise(async () => {
|
|
328
|
+
const tree = this.trees[treeId];
|
|
329
|
+
for (let i = 0n; i < tree.getNumLeaves(includeUncommitted); i++) {
|
|
330
|
+
const currentValue = await tree.getLeafValue(i, includeUncommitted);
|
|
331
|
+
if (currentValue && currentValue.equals(value)) {
|
|
332
|
+
return i;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return undefined;
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Updates a leaf in a tree at a given index.
|
|
341
|
+
* @param treeId - The ID of the tree.
|
|
342
|
+
* @param leaf - The new leaf value.
|
|
343
|
+
* @param index - The index to insert into.
|
|
344
|
+
* @returns Empty promise.
|
|
345
|
+
*/
|
|
346
|
+
public async updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: LeafData | Buffer, index: bigint): Promise<void> {
|
|
347
|
+
return await this.synchronise(() => this._updateLeaf(treeId, leaf, index));
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
|
|
352
|
+
* @param block - The L2 block to handle.
|
|
353
|
+
*/
|
|
354
|
+
public async handleL2Block(block: L2Block): Promise<void> {
|
|
355
|
+
await this.synchronise(() => this._handleL2Block(block));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Batch insert multiple leaves into the tree.
|
|
360
|
+
* @param treeId - The ID of the tree.
|
|
361
|
+
* @param leaves - Leaves to insert into the tree.
|
|
362
|
+
* @param treeHeight - Height of the tree.
|
|
363
|
+
* @param subtreeHeight - Height of the subtree.
|
|
364
|
+
* @returns The data for the leaves to be updated when inserting the new ones.
|
|
365
|
+
*/
|
|
366
|
+
public async batchInsert<
|
|
367
|
+
TreeHeight extends number,
|
|
368
|
+
SubtreeHeight extends number,
|
|
369
|
+
SubtreeSiblingPathHeight extends number,
|
|
370
|
+
>(
|
|
371
|
+
treeId: MerkleTreeId,
|
|
372
|
+
leaves: Buffer[],
|
|
373
|
+
treeHeight: TreeHeight,
|
|
374
|
+
subtreeHeight: SubtreeHeight,
|
|
375
|
+
): Promise<
|
|
376
|
+
| [LowLeafWitnessData<TreeHeight>[], SiblingPath<SubtreeSiblingPathHeight>]
|
|
377
|
+
| [undefined, SiblingPath<SubtreeSiblingPathHeight>]
|
|
378
|
+
> {
|
|
379
|
+
const tree = this.trees[treeId] as StandardIndexedTree;
|
|
380
|
+
if (!('batchInsert' in tree)) {
|
|
381
|
+
throw new Error('Tree does not support `batchInsert` method');
|
|
382
|
+
}
|
|
383
|
+
return await this.synchronise(() => tree.batchInsert(leaves, treeHeight, subtreeHeight));
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Waits for all jobs to finish before executing the given function.
|
|
388
|
+
* @param fn - The function to execute.
|
|
389
|
+
* @returns Promise containing the result of the function.
|
|
390
|
+
*/
|
|
391
|
+
private async synchronise<T>(fn: () => Promise<T>): Promise<T> {
|
|
392
|
+
return await this.jobQueue.put(fn);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Returns the tree info for the specified tree id.
|
|
397
|
+
* @param treeId - Id of the tree to get information from.
|
|
398
|
+
* @param includeUncommitted - Indicates whether to include uncommitted data.
|
|
399
|
+
* @returns The tree info for the specified tree.
|
|
400
|
+
*/
|
|
401
|
+
private _getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise<TreeInfo> {
|
|
402
|
+
const treeInfo = {
|
|
403
|
+
treeId,
|
|
404
|
+
root: this.trees[treeId].getRoot(includeUncommitted),
|
|
405
|
+
size: this.trees[treeId].getNumLeaves(includeUncommitted),
|
|
406
|
+
depth: this.trees[treeId].getDepth(),
|
|
407
|
+
} as TreeInfo;
|
|
408
|
+
return Promise.resolve(treeInfo);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Returns an instance of an indexed tree.
|
|
413
|
+
* @param treeId - Id of the tree to get an instance of.
|
|
414
|
+
* @returns The indexed tree for the specified tree id.
|
|
415
|
+
*/
|
|
416
|
+
private _getIndexedTree(treeId: IndexedTreeId): IndexedTree {
|
|
417
|
+
return this.trees[treeId] as IndexedTree;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Returns the sibling path for a leaf in a tree.
|
|
422
|
+
* @param treeId - Id of the tree to get the sibling path from.
|
|
423
|
+
* @param index - Index of the leaf to get the sibling path for.
|
|
424
|
+
* @param includeUncommitted - Indicates whether to include uncommitted updates in the sibling path.
|
|
425
|
+
* @returns Promise containing the sibling path for the leaf.
|
|
426
|
+
*/
|
|
427
|
+
private _getSiblingPath<N extends number>(
|
|
428
|
+
treeId: MerkleTreeId,
|
|
429
|
+
index: bigint,
|
|
430
|
+
includeUncommitted: boolean,
|
|
431
|
+
): Promise<SiblingPath<N>> {
|
|
432
|
+
return Promise.resolve(this.trees[treeId].getSiblingPath<N>(index, includeUncommitted));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Appends leaves to a tree.
|
|
437
|
+
* @param treeId - Id of the tree to append leaves to.
|
|
438
|
+
* @param leaves - Leaves to append.
|
|
439
|
+
* @returns Empty promise.
|
|
440
|
+
*/
|
|
441
|
+
private async _appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise<void> {
|
|
442
|
+
const tree = this.trees[treeId];
|
|
443
|
+
if (!('appendLeaves' in tree)) {
|
|
444
|
+
throw new Error('Tree does not support `appendLeaves` method');
|
|
445
|
+
}
|
|
446
|
+
return await tree.appendLeaves(leaves);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
private async _updateLeaf(
|
|
450
|
+
treeId: IndexedTreeId | PublicTreeId,
|
|
451
|
+
leaf: LeafData | Buffer,
|
|
452
|
+
index: bigint,
|
|
453
|
+
): Promise<void> {
|
|
454
|
+
const tree = this.trees[treeId];
|
|
455
|
+
if (!('updateLeaf' in tree)) {
|
|
456
|
+
throw new Error('Tree does not support `updateLeaf` method');
|
|
457
|
+
}
|
|
458
|
+
return await tree.updateLeaf(leaf, index);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Commits all pending updates.
|
|
463
|
+
* @returns Empty promise.
|
|
464
|
+
*/
|
|
465
|
+
private async _commit(): Promise<void> {
|
|
466
|
+
for (const tree of this.trees) {
|
|
467
|
+
await tree.commit();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Rolls back all pending updates.
|
|
473
|
+
* @returns Empty promise.
|
|
474
|
+
*/
|
|
475
|
+
private async _rollback(): Promise<void> {
|
|
476
|
+
for (const tree of this.trees) {
|
|
477
|
+
await tree.rollback();
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
|
|
483
|
+
* @param l2Block - The L2 block to handle.
|
|
484
|
+
*/
|
|
485
|
+
private async _handleL2Block(l2Block: L2Block) {
|
|
486
|
+
const compareRoot = (root: Fr, treeId: MerkleTreeId) => {
|
|
487
|
+
const treeRoot = this.trees[treeId].getRoot(true);
|
|
488
|
+
return treeRoot.equals(root.toBuffer());
|
|
489
|
+
};
|
|
490
|
+
const rootChecks = [
|
|
491
|
+
compareRoot(l2Block.endContractTreeSnapshot.root, MerkleTreeId.CONTRACT_TREE),
|
|
492
|
+
compareRoot(l2Block.endNullifierTreeSnapshot.root, MerkleTreeId.NULLIFIER_TREE),
|
|
493
|
+
compareRoot(l2Block.endPrivateDataTreeSnapshot.root, MerkleTreeId.PRIVATE_DATA_TREE),
|
|
494
|
+
compareRoot(l2Block.endPublicDataTreeRoot, MerkleTreeId.PUBLIC_DATA_TREE),
|
|
495
|
+
compareRoot(l2Block.endTreeOfHistoricContractTreeRootsSnapshot.root, MerkleTreeId.CONTRACT_TREE_ROOTS_TREE),
|
|
496
|
+
compareRoot(
|
|
497
|
+
l2Block.endTreeOfHistoricPrivateDataTreeRootsSnapshot.root,
|
|
498
|
+
MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE,
|
|
499
|
+
),
|
|
500
|
+
compareRoot(l2Block.endL1ToL2MessageTreeSnapshot.root, MerkleTreeId.L1_TO_L2_MESSAGES_TREE),
|
|
501
|
+
compareRoot(
|
|
502
|
+
l2Block.endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot.root,
|
|
503
|
+
MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE,
|
|
504
|
+
),
|
|
505
|
+
];
|
|
506
|
+
const ourBlock = rootChecks.every(x => x);
|
|
507
|
+
if (ourBlock) {
|
|
508
|
+
this.log(`Block ${l2Block.number} is ours, committing world state..`);
|
|
509
|
+
await this._commit();
|
|
510
|
+
} else {
|
|
511
|
+
this.log(`Block ${l2Block.number} is not ours, rolling back world state and committing state from chain..`);
|
|
512
|
+
await this._rollback();
|
|
513
|
+
|
|
514
|
+
for (const [tree, leaves] of [
|
|
515
|
+
[MerkleTreeId.CONTRACT_TREE, l2Block.newContracts],
|
|
516
|
+
[MerkleTreeId.NULLIFIER_TREE, l2Block.newNullifiers],
|
|
517
|
+
[MerkleTreeId.PRIVATE_DATA_TREE, l2Block.newCommitments],
|
|
518
|
+
[MerkleTreeId.L1_TO_L2_MESSAGES_TREE, l2Block.newL1ToL2Messages],
|
|
519
|
+
] as const) {
|
|
520
|
+
await this._appendLeaves(
|
|
521
|
+
tree,
|
|
522
|
+
leaves.map(fr => fr.toBuffer()),
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
for (const dataWrite of l2Block.newPublicDataWrites) {
|
|
527
|
+
if (dataWrite.isEmpty()) continue;
|
|
528
|
+
const { newValue, leafIndex } = dataWrite;
|
|
529
|
+
await this._updateLeaf(MerkleTreeId.PUBLIC_DATA_TREE, newValue.toBuffer(), leafIndex.value);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
for (const [newTree, rootTree] of [
|
|
533
|
+
[MerkleTreeId.PRIVATE_DATA_TREE, MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE],
|
|
534
|
+
[MerkleTreeId.CONTRACT_TREE, MerkleTreeId.CONTRACT_TREE_ROOTS_TREE],
|
|
535
|
+
[MerkleTreeId.L1_TO_L2_MESSAGES_TREE, MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE],
|
|
536
|
+
] as const) {
|
|
537
|
+
const newTreeRoot = this.trees[newTree].getRoot(true);
|
|
538
|
+
await this._appendLeaves(rootTree, [newTreeRoot]);
|
|
539
|
+
}
|
|
540
|
+
await this._commit();
|
|
541
|
+
}
|
|
542
|
+
for (const treeId of merkleTreeIds()) {
|
|
543
|
+
const info = await this._getTreeInfo(treeId, false);
|
|
544
|
+
this.log(`Tree ${MerkleTreeId[treeId]} synched with size ${info.size} root ${info.root.toString('hex')}`);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "..",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dest",
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"tsBuildInfoFile": ".tsbuildinfo"
|
|
7
|
+
},
|
|
8
|
+
"references": [
|
|
9
|
+
{
|
|
10
|
+
"path": "../circuits.js"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"path": "../foundation"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"path": "../merkle-tree"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"path": "../types"
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
"include": ["src"]
|
|
23
|
+
}
|