@aztec/world-state 0.72.1 → 0.74.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.
Files changed (47) hide show
  1. package/dest/native/merkle_trees_facade.js +2 -2
  2. package/dest/native/message.d.ts +0 -30
  3. package/dest/native/message.d.ts.map +1 -1
  4. package/dest/native/message.js +1 -23
  5. package/dest/native/native_world_state.js +3 -3
  6. package/dest/native/native_world_state_instance.d.ts +1 -13
  7. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  8. package/dest/native/native_world_state_instance.js +27 -94
  9. package/dest/native/world_state_version.js +2 -2
  10. package/dest/synchronizer/server_world_state_synchronizer.d.ts +1 -1
  11. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  12. package/dest/synchronizer/server_world_state_synchronizer.js +6 -6
  13. package/dest/test/index.d.ts +2 -0
  14. package/dest/test/index.d.ts.map +1 -0
  15. package/dest/test/index.js +2 -0
  16. package/dest/world-state-db/index.d.ts +0 -3
  17. package/dest/world-state-db/index.d.ts.map +1 -1
  18. package/dest/world-state-db/index.js +1 -4
  19. package/package.json +13 -17
  20. package/src/native/merkle_trees_facade.ts +1 -1
  21. package/src/native/message.ts +0 -44
  22. package/src/native/native_world_state.ts +2 -2
  23. package/src/native/native_world_state_instance.ts +28 -124
  24. package/src/native/world_state_version.ts +1 -1
  25. package/src/synchronizer/server_world_state_synchronizer.ts +6 -6
  26. package/src/test/index.ts +1 -0
  27. package/src/world-state-db/index.ts +0 -3
  28. package/dest/world-state-db/merkle_tree_map.d.ts +0 -11
  29. package/dest/world-state-db/merkle_tree_map.d.ts.map +0 -1
  30. package/dest/world-state-db/merkle_tree_map.js +0 -2
  31. package/dest/world-state-db/merkle_tree_operations_facade.d.ts +0 -116
  32. package/dest/world-state-db/merkle_tree_operations_facade.d.ts.map +0 -1
  33. package/dest/world-state-db/merkle_tree_operations_facade.js +0 -133
  34. package/dest/world-state-db/merkle_tree_snapshot_operations_facade.d.ts +0 -32
  35. package/dest/world-state-db/merkle_tree_snapshot_operations_facade.d.ts.map +0 -1
  36. package/dest/world-state-db/merkle_tree_snapshot_operations_facade.js +0 -76
  37. package/dest/world-state-db/merkle_trees.d.ts +0 -174
  38. package/dest/world-state-db/merkle_trees.d.ts.map +0 -1
  39. package/dest/world-state-db/merkle_trees.js +0 -461
  40. package/dest/world-state-db/metrics.d.ts +0 -14
  41. package/dest/world-state-db/metrics.d.ts.map +0 -1
  42. package/dest/world-state-db/metrics.js +0 -44
  43. package/src/world-state-db/merkle_tree_map.ts +0 -11
  44. package/src/world-state-db/merkle_tree_operations_facade.ts +0 -199
  45. package/src/world-state-db/merkle_tree_snapshot_operations_facade.ts +0 -151
  46. package/src/world-state-db/merkle_trees.ts +0 -727
  47. package/src/world-state-db/metrics.ts +0 -64
@@ -1,727 +0,0 @@
1
- import { type L2Block, MerkleTreeId, type SiblingPath } from '@aztec/circuit-types';
2
- import {
3
- type BatchInsertionResult,
4
- type IndexedTreeId,
5
- type MerkleTreeLeafType,
6
- type MerkleTreeReadOperations,
7
- type MerkleTreeWriteOperations,
8
- type TreeInfo,
9
- } from '@aztec/circuit-types/interfaces';
10
- import {
11
- ARCHIVE_HEIGHT,
12
- AppendOnlyTreeSnapshot,
13
- BlockHeader,
14
- Fr,
15
- L1_TO_L2_MSG_TREE_HEIGHT,
16
- MAX_NOTE_HASHES_PER_TX,
17
- MAX_NULLIFIERS_PER_TX,
18
- MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
19
- NOTE_HASH_TREE_HEIGHT,
20
- NULLIFIER_SUBTREE_HEIGHT,
21
- NULLIFIER_TREE_HEIGHT,
22
- NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
23
- NullifierLeaf,
24
- NullifierLeafPreimage,
25
- PUBLIC_DATA_SUBTREE_HEIGHT,
26
- PUBLIC_DATA_TREE_HEIGHT,
27
- PartialStateReference,
28
- PublicDataTreeLeaf,
29
- PublicDataTreeLeafPreimage,
30
- PublicDataWrite,
31
- StateReference,
32
- } from '@aztec/circuits.js';
33
- import { padArrayEnd } from '@aztec/foundation/collection';
34
- import { type Logger, createLogger } from '@aztec/foundation/log';
35
- import { SerialQueue } from '@aztec/foundation/queue';
36
- import { Timer, elapsed } from '@aztec/foundation/timer';
37
- import { type IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
38
- import { type AztecKVStore, type AztecSingleton } from '@aztec/kv-store';
39
- import { openTmpStore } from '@aztec/kv-store/lmdb';
40
- import {
41
- type AppendOnlyTree,
42
- type IndexedTree,
43
- Poseidon,
44
- StandardIndexedTree,
45
- StandardTree,
46
- getTreeMeta,
47
- loadTree,
48
- newTree,
49
- } from '@aztec/merkle-tree';
50
- import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
51
- import { type Hasher } from '@aztec/types/interfaces';
52
-
53
- import {
54
- type WorldStateStatusFull,
55
- type WorldStateStatusSummary,
56
- buildEmptyWorldStateStatusFull,
57
- } from '../native/message.js';
58
- import {
59
- INITIAL_NULLIFIER_TREE_SIZE,
60
- INITIAL_PUBLIC_DATA_TREE_SIZE,
61
- type MerkleTreeAdminDatabase,
62
- type TreeSnapshots,
63
- } from './merkle_tree_db.js';
64
- import { type MerkleTreeMap } from './merkle_tree_map.js';
65
- import { MerkleTreeReadOperationsFacade } from './merkle_tree_operations_facade.js';
66
- import { MerkleTreeSnapshotOperationsFacade } from './merkle_tree_snapshot_operations_facade.js';
67
- import { WorldStateMetrics } from './metrics.js';
68
-
69
- /**
70
- * The nullifier tree is an indexed tree.
71
- */
72
- class NullifierTree extends StandardIndexedTree {
73
- constructor(
74
- store: AztecKVStore,
75
- hasher: Hasher,
76
- name: string,
77
- depth: number,
78
- size: bigint = 0n,
79
- _noop: any,
80
- root?: Buffer,
81
- ) {
82
- super(store, hasher, name, depth, size, NullifierLeafPreimage, NullifierLeaf, root);
83
- }
84
- }
85
-
86
- /**
87
- * The public data tree is an indexed tree.
88
- */
89
- class PublicDataTree extends StandardIndexedTree {
90
- constructor(
91
- store: AztecKVStore,
92
- hasher: Hasher,
93
- name: string,
94
- depth: number,
95
- size: bigint = 0n,
96
- _noop: any,
97
- root?: Buffer,
98
- ) {
99
- super(store, hasher, name, depth, size, PublicDataTreeLeafPreimage, PublicDataTreeLeaf, root);
100
- }
101
- }
102
-
103
- /**
104
- * A convenience class for managing multiple merkle trees.
105
- */
106
- export class MerkleTrees implements MerkleTreeAdminDatabase {
107
- // gets initialized in #init
108
- private trees: MerkleTreeMap = null as any;
109
- private jobQueue = new SerialQueue();
110
- private initialStateReference: AztecSingleton<Buffer>;
111
- private metrics: WorldStateMetrics;
112
-
113
- private constructor(private store: AztecKVStore, private telemetryClient: TelemetryClient, private log: Logger) {
114
- this.initialStateReference = store.openSingleton('merkle_trees_initial_state_reference');
115
- this.metrics = new WorldStateMetrics(telemetryClient);
116
- }
117
-
118
- /**
119
- * Method to asynchronously create and initialize a MerkleTrees instance.
120
- * @param store - The db instance to use for data persistance.
121
- * @returns - A fully initialized MerkleTrees instance.
122
- */
123
- public static async new(
124
- store: AztecKVStore,
125
- client: TelemetryClient = getTelemetryClient(),
126
- log = createLogger('world-state:merkle_trees'),
127
- ) {
128
- const merkleTrees = new MerkleTrees(store, client, log);
129
- await merkleTrees.#init();
130
- return merkleTrees;
131
- }
132
-
133
- /**
134
- * Creates a temporary store. Useful for testing.
135
- */
136
- public static tmp() {
137
- const store = openTmpStore();
138
- return MerkleTrees.new(store, getTelemetryClient());
139
- }
140
-
141
- /**
142
- * Initializes the collection of Merkle Trees.
143
- */
144
- async #init(loadFromDb?: boolean) {
145
- const fromDb = loadFromDb === undefined ? this.#isDbPopulated() : loadFromDb;
146
- const initializeTree = fromDb ? loadTree : newTree;
147
-
148
- const hasher = new Poseidon();
149
-
150
- const nullifierTree = await initializeTree(
151
- NullifierTree,
152
- this.store,
153
- hasher,
154
- `${MerkleTreeId[MerkleTreeId.NULLIFIER_TREE]}`,
155
- {},
156
- NULLIFIER_TREE_HEIGHT,
157
- INITIAL_NULLIFIER_TREE_SIZE,
158
- );
159
- const noteHashTree: AppendOnlyTree<Fr> = await initializeTree(
160
- StandardTree,
161
- this.store,
162
- hasher,
163
- `${MerkleTreeId[MerkleTreeId.NOTE_HASH_TREE]}`,
164
- Fr,
165
- NOTE_HASH_TREE_HEIGHT,
166
- );
167
- const publicDataTree = await initializeTree(
168
- PublicDataTree,
169
- this.store,
170
- hasher,
171
- `${MerkleTreeId[MerkleTreeId.PUBLIC_DATA_TREE]}`,
172
- {},
173
- PUBLIC_DATA_TREE_HEIGHT,
174
- INITIAL_PUBLIC_DATA_TREE_SIZE,
175
- );
176
- const l1Tol2MessageTree: AppendOnlyTree<Fr> = await initializeTree(
177
- StandardTree,
178
- this.store,
179
- hasher,
180
- `${MerkleTreeId[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]}`,
181
- Fr,
182
- L1_TO_L2_MSG_TREE_HEIGHT,
183
- );
184
- const archive: AppendOnlyTree<Fr> = await initializeTree(
185
- StandardTree,
186
- this.store,
187
- hasher,
188
- `${MerkleTreeId[MerkleTreeId.ARCHIVE]}`,
189
- Fr,
190
- ARCHIVE_HEIGHT,
191
- );
192
- this.trees = [nullifierTree, noteHashTree, publicDataTree, l1Tol2MessageTree, archive];
193
-
194
- this.jobQueue.start();
195
-
196
- if (!fromDb) {
197
- // We are not initializing from db so we need to populate the first leaf of the archive tree which is a hash of the initial header,
198
- // and persist the initial header state reference so we can later load it when requested.
199
- const initialState = await this.getStateReference(true);
200
- await this.#saveInitialStateReference(initialState);
201
- await this.#updateArchive(this.getInitialHeader());
202
-
203
- // And commit anything we did to initialize this set of trees
204
- await this.#commit();
205
- }
206
- }
207
-
208
- public removeHistoricalBlocks(_toBlockNumber: bigint): Promise<WorldStateStatusFull> {
209
- throw new Error('Method not implemented.');
210
- }
211
-
212
- public unwindBlocks(_toBlockNumber: bigint): Promise<WorldStateStatusFull> {
213
- throw new Error('Method not implemented.');
214
- }
215
-
216
- public setFinalised(_toBlockNumber: bigint): Promise<WorldStateStatusSummary> {
217
- throw new Error('Method not implemented.');
218
- }
219
-
220
- public getStatusSummary(): Promise<WorldStateStatusSummary> {
221
- throw new Error('Method not implemented.');
222
- }
223
-
224
- public async fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
225
- if (blockNumber) {
226
- throw new Error('Block number forking is not supported in js world state');
227
- }
228
- const [ms, db] = await elapsed(async () => {
229
- const forked = await this.store.fork();
230
- return MerkleTrees.new(forked, this.telemetryClient, this.log);
231
- });
232
-
233
- this.metrics.recordForkDuration(ms);
234
- return new MerkleTreeReadOperationsFacade(db, true);
235
- }
236
-
237
- // REFACTOR: We're hiding the `commit` operations in the tree behind a type check only, but
238
- // we should make sure it's not accidentally called elsewhere by splitting this class into one
239
- // that can work on a read-only store and one that actually writes to the store. This implies
240
- // having read-only versions of the kv-stores, all kv-containers, and all trees.
241
- public async ephemeralFork(): Promise<MerkleTreeWriteOperations> {
242
- const forked = new MerkleTrees(
243
- this.store,
244
- this.telemetryClient,
245
- createLogger('world-state:merkle_trees:ephemeral_fork'),
246
- );
247
- await forked.#init(true);
248
- return new MerkleTreeReadOperationsFacade(forked, true);
249
- }
250
-
251
- public async delete() {
252
- await this.store.delete();
253
- }
254
-
255
- public getInitialHeader(): BlockHeader {
256
- return BlockHeader.empty({ state: this.#loadInitialStateReference() });
257
- }
258
-
259
- /**
260
- * Stops the job queue (waits for all jobs to finish).
261
- */
262
- public async close() {
263
- await this.jobQueue.end();
264
- }
265
-
266
- /**
267
- * Gets a view of this db that returns uncommitted data.
268
- * @returns - A facade for this instance.
269
- */
270
- public getLatest(): Promise<MerkleTreeWriteOperations> {
271
- return Promise.resolve(new MerkleTreeReadOperationsFacade(this, true));
272
- }
273
-
274
- /**
275
- * Gets a view of this db that returns committed data only.
276
- * @returns - A facade for this instance.
277
- */
278
- public getCommitted(): MerkleTreeReadOperations {
279
- return new MerkleTreeReadOperationsFacade(this, false);
280
- }
281
-
282
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
283
- return new MerkleTreeSnapshotOperationsFacade(this, blockNumber);
284
- }
285
-
286
- /**
287
- * Updates the archive with the new block/header hash.
288
- * @param header - The header whose hash to insert into the archive.
289
- * @param includeUncommitted - Indicates whether to include uncommitted data.
290
- */
291
- public async updateArchive(header: BlockHeader) {
292
- await this.synchronize(() => this.#updateArchive(header));
293
- }
294
-
295
- /**
296
- * Gets the tree info for the specified tree.
297
- * @param treeId - Id of the tree to get information from.
298
- * @param includeUncommitted - Indicates whether to include uncommitted data.
299
- * @returns The tree info for the specified tree.
300
- */
301
- public async getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise<TreeInfo> {
302
- return await this.synchronize(() => this.#getTreeInfo(treeId, includeUncommitted));
303
- }
304
-
305
- /**
306
- * Get the current state reference
307
- * @param includeUncommitted - Indicates whether to include uncommitted data.
308
- * @returns The current state reference
309
- */
310
- public getStateReference(includeUncommitted: boolean): Promise<StateReference> {
311
- const getAppendOnlyTreeSnapshot = (treeId: MerkleTreeId) => {
312
- const tree = this.trees[treeId] as AppendOnlyTree;
313
- return new AppendOnlyTreeSnapshot(
314
- Fr.fromBuffer(tree.getRoot(includeUncommitted)),
315
- Number(tree.getNumLeaves(includeUncommitted)),
316
- );
317
- };
318
-
319
- const state = new StateReference(
320
- getAppendOnlyTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE),
321
- new PartialStateReference(
322
- getAppendOnlyTreeSnapshot(MerkleTreeId.NOTE_HASH_TREE),
323
- getAppendOnlyTreeSnapshot(MerkleTreeId.NULLIFIER_TREE),
324
- getAppendOnlyTreeSnapshot(MerkleTreeId.PUBLIC_DATA_TREE),
325
- ),
326
- );
327
- return Promise.resolve(state);
328
- }
329
-
330
- /**
331
- * Gets the value at the given index.
332
- * @param treeId - The ID of the tree to get the leaf value from.
333
- * @param index - The index of the leaf.
334
- * @param includeUncommitted - Indicates whether to include uncommitted changes.
335
- * @returns Leaf value at the given index (undefined if not found).
336
- */
337
- public async getLeafValue(
338
- treeId: MerkleTreeId,
339
- index: bigint,
340
- includeUncommitted: boolean,
341
- ): Promise<MerkleTreeLeafType<typeof treeId> | undefined> {
342
- return await this.synchronize(() => Promise.resolve(this.trees[treeId].getLeafValue(index, includeUncommitted)));
343
- }
344
-
345
- /**
346
- * Gets the sibling path for a leaf in a tree.
347
- * @param treeId - The ID of the tree.
348
- * @param index - The index of the leaf.
349
- * @param includeUncommitted - Indicates whether the sibling path should include uncommitted data.
350
- * @returns The sibling path for the leaf.
351
- */
352
- public async getSiblingPath<N extends number>(
353
- treeId: MerkleTreeId,
354
- index: bigint,
355
- includeUncommitted: boolean,
356
- ): Promise<SiblingPath<N>> {
357
- return await this.synchronize(() => this.trees[treeId].getSiblingPath<N>(index, includeUncommitted));
358
- }
359
-
360
- /**
361
- * Appends leaves to a tree.
362
- * @param treeId - The ID of the tree.
363
- * @param leaves - The leaves to append.
364
- * @returns Empty promise.
365
- */
366
- public async appendLeaves<ID extends MerkleTreeId>(treeId: ID, leaves: MerkleTreeLeafType<ID>[]): Promise<void> {
367
- return await this.synchronize(() => this.#appendLeaves(treeId, leaves));
368
- }
369
-
370
- /**
371
- * Commits all pending updates.
372
- * @returns Empty promise.
373
- */
374
- public async commit(): Promise<void> {
375
- return await this.synchronize(() => this.#commit());
376
- }
377
-
378
- /**
379
- * Rolls back all pending updates.
380
- * @returns Empty promise.
381
- */
382
- public async rollback(): Promise<void> {
383
- return await this.synchronize(() => this.#rollback());
384
- }
385
-
386
- /**
387
- * Finds the index of the largest leaf whose value is less than or equal to the provided value.
388
- * @param treeId - The ID of the tree to search.
389
- * @param value - The value to be inserted into the tree.
390
- * @param includeUncommitted - If true, the uncommitted changes are included in the search.
391
- * @returns The found leaf index and a flag indicating if the corresponding leaf's value is equal to `newValue`.
392
- */
393
- public async getPreviousValueIndex(
394
- treeId: IndexedTreeId,
395
- value: bigint,
396
- includeUncommitted: boolean,
397
- ): Promise<
398
- | {
399
- /**
400
- * The index of the found leaf.
401
- */
402
- index: bigint;
403
- /**
404
- * A flag indicating if the corresponding leaf's value is equal to `newValue`.
405
- */
406
- alreadyPresent: boolean;
407
- }
408
- | undefined
409
- > {
410
- return await this.synchronize(() =>
411
- Promise.resolve(this.#getIndexedTree(treeId).findIndexOfPreviousKey(value, includeUncommitted)),
412
- );
413
- }
414
-
415
- /**
416
- * Gets the leaf data at a given index and tree.
417
- * @param treeId - The ID of the tree get the leaf from.
418
- * @param index - The index of the leaf to get.
419
- * @param includeUncommitted - Indicates whether to include uncommitted data.
420
- * @returns Leaf preimage.
421
- */
422
- public async getLeafPreimage(
423
- treeId: IndexedTreeId,
424
- index: bigint,
425
- includeUncommitted: boolean,
426
- ): Promise<IndexedTreeLeafPreimage | undefined> {
427
- return await this.synchronize(() =>
428
- Promise.resolve(this.#getIndexedTree(treeId).getLatestLeafPreimageCopy(index, includeUncommitted)),
429
- );
430
- }
431
-
432
- /**
433
- * Returns the index of a leaf given its value, or undefined if no leaf with that value is found.
434
- * @param treeId - The ID of the tree.
435
- * @param value - The leaf value to look for.
436
- * @param includeUncommitted - Indicates whether to include uncommitted data.
437
- * @returns The index of the first leaf found with a given value (undefined if not found).
438
- */
439
- public async findLeafIndex<ID extends MerkleTreeId>(
440
- treeId: ID,
441
- value: MerkleTreeLeafType<ID>,
442
- includeUncommitted: boolean,
443
- ): Promise<bigint | undefined> {
444
- return await this.synchronize(() => {
445
- const tree = this.trees[treeId];
446
- // TODO #5448 fix "as any"
447
- return Promise.resolve(tree.findLeafIndex(value as any, includeUncommitted));
448
- });
449
- }
450
-
451
- /**
452
- * Returns the first index containing a leaf value after `startIndex`.
453
- * @param treeId - The tree for which the index should be returned.
454
- * @param value - The value to search for in the tree.
455
- * @param startIndex - The index to start searching from (used when skipping nullified messages)
456
- * @param includeUncommitted - Indicates whether to include uncommitted data.
457
- */
458
- public async findLeafIndexAfter<ID extends MerkleTreeId>(
459
- treeId: ID,
460
- value: MerkleTreeLeafType<ID>,
461
- startIndex: bigint,
462
- includeUncommitted: boolean,
463
- ): Promise<bigint | undefined> {
464
- return await this.synchronize(() => {
465
- const tree = this.trees[treeId];
466
- // TODO #5448 fix "as any"
467
- return Promise.resolve(tree.findLeafIndexAfter(value as any, startIndex, includeUncommitted));
468
- });
469
- }
470
-
471
- /**
472
- * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
473
- * @param block - The L2 block to handle.
474
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
475
- * @returns Whether the block handled was produced by this same node.
476
- */
477
- public async handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
478
- return await this.synchronize(() => this.#handleL2BlockAndMessages(block, l1ToL2Messages));
479
- }
480
-
481
- /**
482
- * Batch insert multiple leaves into the tree.
483
- * @param treeId - The ID of the tree.
484
- * @param leaves - Leaves to insert into the tree.
485
- * @param subtreeHeight - Height of the subtree.
486
- * @returns The data for the leaves to be updated when inserting the new ones.
487
- */
488
- public async batchInsert<
489
- TreeHeight extends number,
490
- SubtreeHeight extends number,
491
- SubtreeSiblingPathHeight extends number,
492
- >(
493
- treeId: IndexedTreeId,
494
- leaves: Buffer[],
495
- subtreeHeight: SubtreeHeight,
496
- ): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>> {
497
- const tree = this.trees[treeId] as StandardIndexedTree;
498
- if (!('batchInsert' in tree)) {
499
- throw new Error('Tree does not support `batchInsert` method');
500
- }
501
- return await this.synchronize(() => tree.batchInsert(leaves, subtreeHeight));
502
- }
503
-
504
- /**
505
- * Waits for all jobs to finish before executing the given function.
506
- * @param fn - The function to execute.
507
- * @returns Promise containing the result of the function.
508
- */
509
- private async synchronize<T>(fn: () => Promise<T>): Promise<T> {
510
- return await this.jobQueue.put(fn);
511
- }
512
-
513
- #saveInitialStateReference(state: StateReference) {
514
- return this.initialStateReference.set(state.toBuffer());
515
- }
516
-
517
- #loadInitialStateReference(): StateReference {
518
- const serialized = this.initialStateReference.get();
519
- if (!serialized) {
520
- throw new Error('Initial state reference not found');
521
- }
522
- return StateReference.fromBuffer(serialized);
523
- }
524
-
525
- async #updateArchive(header: BlockHeader) {
526
- const state = await this.getStateReference(true);
527
-
528
- // This method should be called only when the block builder already updated the state so we sanity check that it's
529
- // the case here.
530
- if (!state.toBuffer().equals(header.state.toBuffer())) {
531
- throw new Error('State in header does not match current state');
532
- }
533
-
534
- const blockHash = header.hash();
535
- await this.#appendLeaves(MerkleTreeId.ARCHIVE, [blockHash]);
536
- }
537
-
538
- /**
539
- * Returns the tree info for the specified tree id.
540
- * @param treeId - Id of the tree to get information from.
541
- * @param includeUncommitted - Indicates whether to include uncommitted data.
542
- * @returns The tree info for the specified tree.
543
- */
544
- #getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise<TreeInfo> {
545
- const treeInfo = {
546
- treeId,
547
- root: this.trees[treeId].getRoot(includeUncommitted),
548
- size: this.trees[treeId].getNumLeaves(includeUncommitted),
549
- depth: this.trees[treeId].getDepth(),
550
- } as TreeInfo;
551
- return Promise.resolve(treeInfo);
552
- }
553
-
554
- /**
555
- * Returns an instance of an indexed tree.
556
- * @param treeId - Id of the tree to get an instance of.
557
- * @returns The indexed tree for the specified tree id.
558
- */
559
- #getIndexedTree(treeId: IndexedTreeId): IndexedTree {
560
- return this.trees[treeId] as IndexedTree;
561
- }
562
-
563
- /**
564
- * Appends leaves to a tree.
565
- * @param treeId - Id of the tree to append leaves to.
566
- * @param leaves - Leaves to append.
567
- * @returns Empty promise.
568
- */
569
- async #appendLeaves<ID extends MerkleTreeId>(treeId: ID, leaves: MerkleTreeLeafType<typeof treeId>[]): Promise<void> {
570
- const tree = this.trees[treeId];
571
- if (!('appendLeaves' in tree)) {
572
- throw new Error('Tree does not support `appendLeaves` method');
573
- }
574
- // TODO #5448 fix "as any"
575
- return await tree.appendLeaves(leaves as any[]);
576
- }
577
-
578
- /**
579
- * Commits all pending updates.
580
- * @returns Empty promise.
581
- */
582
- async #commit(): Promise<void> {
583
- for (const tree of Object.values(this.trees)) {
584
- await tree.commit();
585
- }
586
- }
587
-
588
- /**
589
- * Rolls back all pending updates.
590
- * @returns Empty promise.
591
- */
592
- async #rollback(): Promise<void> {
593
- for (const tree of Object.values(this.trees)) {
594
- await tree.rollback();
595
- }
596
- }
597
-
598
- public async getTreeSnapshots(blockNumber: number): Promise<TreeSnapshots> {
599
- const snapshots = await Promise.all([
600
- this.trees[MerkleTreeId.NULLIFIER_TREE].getSnapshot(blockNumber),
601
- this.trees[MerkleTreeId.NOTE_HASH_TREE].getSnapshot(blockNumber),
602
- this.trees[MerkleTreeId.PUBLIC_DATA_TREE].getSnapshot(blockNumber),
603
- this.trees[MerkleTreeId.L1_TO_L2_MESSAGE_TREE].getSnapshot(blockNumber),
604
- this.trees[MerkleTreeId.ARCHIVE].getSnapshot(blockNumber),
605
- ]);
606
-
607
- return {
608
- [MerkleTreeId.NULLIFIER_TREE]: snapshots[0],
609
- [MerkleTreeId.NOTE_HASH_TREE]: snapshots[1],
610
- [MerkleTreeId.PUBLIC_DATA_TREE]: snapshots[2],
611
- [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: snapshots[3],
612
- [MerkleTreeId.ARCHIVE]: snapshots[4],
613
- };
614
- }
615
-
616
- async #snapshot(blockNumber: number): Promise<void> {
617
- for (const tree of Object.values(this.trees)) {
618
- await tree.snapshot(blockNumber);
619
- }
620
- }
621
-
622
- /**
623
- * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
624
- * @param l2Block - The L2 block to handle.
625
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
626
- */
627
- async #handleL2BlockAndMessages(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
628
- const timer = new Timer();
629
-
630
- const treeRootWithIdPairs = [
631
- [l2Block.header.state.partial.nullifierTree.root, MerkleTreeId.NULLIFIER_TREE],
632
- [l2Block.header.state.partial.noteHashTree.root, MerkleTreeId.NOTE_HASH_TREE],
633
- [l2Block.header.state.partial.publicDataTree.root, MerkleTreeId.PUBLIC_DATA_TREE],
634
- [l2Block.header.state.l1ToL2MessageTree.root, MerkleTreeId.L1_TO_L2_MESSAGE_TREE],
635
- [l2Block.archive.root, MerkleTreeId.ARCHIVE],
636
- ] as const;
637
- const compareRoot = (root: Fr, treeId: MerkleTreeId) => {
638
- const treeRoot = this.trees[treeId].getRoot(true);
639
- return treeRoot.equals(root.toBuffer());
640
- };
641
- const ourBlock = treeRootWithIdPairs.every(([root, id]) => compareRoot(root, id));
642
- if (ourBlock) {
643
- this.log.verbose(`Block ${l2Block.number} is ours, committing world state`);
644
- await this.#commit();
645
- } else {
646
- this.log.verbose(`Block ${l2Block.number} is not ours, rolling back world state and committing state from chain`);
647
- await this.#rollback();
648
-
649
- // We have to pad the values within tx effects because that's how the trees are built by circuits.
650
-
651
- // Sync the append only trees
652
- {
653
- const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
654
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
655
- );
656
- await this.#appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
657
-
658
- const l1ToL2MessagesPadded = padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
659
- await this.#appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
660
- }
661
-
662
- // Sync the indexed trees
663
- {
664
- const nullifiersPadded = l2Block.body.txEffects.flatMap(txEffect =>
665
- padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX),
666
- );
667
- await (this.trees[MerkleTreeId.NULLIFIER_TREE] as StandardIndexedTree).batchInsert(
668
- nullifiersPadded.map(nullifier => nullifier.toBuffer()),
669
- NULLIFIER_SUBTREE_HEIGHT,
670
- );
671
-
672
- const publicDataTree = this.trees[MerkleTreeId.PUBLIC_DATA_TREE] as StandardIndexedTree;
673
-
674
- // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
675
- for (const txEffect of l2Block.body.txEffects) {
676
- const publicDataWrites = padArrayEnd(
677
- txEffect.publicDataWrites,
678
- PublicDataWrite.empty(),
679
- MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
680
- );
681
-
682
- await publicDataTree.batchInsert(
683
- publicDataWrites.map(write => write.toBuffer()),
684
- PUBLIC_DATA_SUBTREE_HEIGHT,
685
- );
686
- }
687
- }
688
-
689
- // The last thing remaining is to update the archive
690
- await this.#updateArchive(l2Block.header);
691
-
692
- await this.#commit();
693
- }
694
-
695
- for (const [root, treeId] of treeRootWithIdPairs) {
696
- const treeName = MerkleTreeId[treeId];
697
- const info = await this.#getTreeInfo(treeId, false);
698
- const syncedStr = '0x' + info.root.toString('hex');
699
- const rootStr = root.toString();
700
- // Sanity check that the rebuilt trees match the roots published by the L2 block
701
- if (!info.root.equals(root.toBuffer())) {
702
- throw new Error(
703
- `Synced tree root ${treeName} does not match published L2 block root: ${syncedStr} != ${rootStr}`,
704
- );
705
- } else {
706
- this.log.debug(`Tree ${treeName} synched with size ${info.size} root ${rootStr}`);
707
- this.metrics.recordTreeSize(treeName, info.size);
708
- }
709
- }
710
- await this.#snapshot(l2Block.number);
711
-
712
- this.metrics.recordDbSize(this.store.estimateSize().actualSize);
713
- this.metrics.recordSyncDuration('commit', timer);
714
- return buildEmptyWorldStateStatusFull();
715
- }
716
-
717
- #isDbPopulated(): boolean {
718
- try {
719
- getTreeMeta(this.store, MerkleTreeId[MerkleTreeId.NULLIFIER_TREE]);
720
- // Tree meta was found --> db is populated
721
- return true;
722
- } catch (e) {
723
- // Tree meta was not found --> db is not populated
724
- return false;
725
- }
726
- }
727
- }