@aztec/world-state 0.16.0 → 0.16.2

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 (28) hide show
  1. package/README.md +2 -2
  2. package/dest/merkle-tree/merkle_tree_operations_facade.d.ts +11 -9
  3. package/dest/merkle-tree/merkle_tree_operations_facade.d.ts.map +1 -1
  4. package/dest/merkle-tree/merkle_tree_operations_facade.js +7 -6
  5. package/dest/merkle-tree/merkle_tree_snapshot_operations_facade.d.ts +40 -0
  6. package/dest/merkle-tree/merkle_tree_snapshot_operations_facade.d.ts.map +1 -0
  7. package/dest/merkle-tree/merkle_tree_snapshot_operations_facade.js +100 -0
  8. package/dest/synchronizer/server_world_state_synchronizer.d.ts +3 -2
  9. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  10. package/dest/synchronizer/server_world_state_synchronizer.js +9 -5
  11. package/dest/synchronizer/world_state_synchronizer.d.ts +8 -2
  12. package/dest/synchronizer/world_state_synchronizer.d.ts.map +1 -1
  13. package/dest/world-state-db/index.d.ts +0 -1
  14. package/dest/world-state-db/index.d.ts.map +1 -1
  15. package/dest/world-state-db/merkle_tree_db.d.ts +16 -8
  16. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  17. package/dest/world-state-db/merkle_tree_db.js +1 -1
  18. package/dest/world-state-db/merkle_trees.d.ts +15 -12
  19. package/dest/world-state-db/merkle_trees.d.ts.map +1 -1
  20. package/dest/world-state-db/merkle_trees.js +40 -24
  21. package/package.json +5 -5
  22. package/src/merkle-tree/merkle_tree_operations_facade.ts +29 -27
  23. package/src/merkle-tree/merkle_tree_snapshot_operations_facade.ts +143 -0
  24. package/src/synchronizer/server_world_state_synchronizer.ts +10 -5
  25. package/src/synchronizer/world_state_synchronizer.ts +9 -2
  26. package/src/world-state-db/index.ts +0 -1
  27. package/src/world-state-db/merkle_tree_db.ts +28 -18
  28. package/src/world-state-db/merkle_trees.ts +72 -53
@@ -8,6 +8,7 @@ import { LevelUp } from 'levelup';
8
8
 
9
9
  import { HandleL2BlockResult, MerkleTreeOperations, MerkleTrees } from '../index.js';
10
10
  import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js';
11
+ import { MerkleTreeSnapshotOperationsFacade } from '../merkle-tree/merkle_tree_snapshot_operations_facade.js';
11
12
  import { WorldStateConfig } from './config.js';
12
13
  import { WorldStateRunningState, WorldStateStatus, WorldStateSynchronizer } from './world_state_synchronizer.js';
13
14
 
@@ -52,6 +53,10 @@ export class ServerWorldStateSynchronizer implements WorldStateSynchronizer {
52
53
  return new MerkleTreeOperationsFacade(this.merkleTreeDb, false);
53
54
  }
54
55
 
56
+ public getSnapshot(blockNumber: number): MerkleTreeOperations {
57
+ return new MerkleTreeSnapshotOperationsFacade(this.merkleTreeDb, blockNumber);
58
+ }
59
+
55
60
  public static async new(
56
61
  db: LevelUp,
57
62
  merkleTreeDb: MerkleTrees,
@@ -130,16 +135,16 @@ export class ServerWorldStateSynchronizer implements WorldStateSynchronizer {
130
135
  /**
131
136
  * Forces an immediate sync
132
137
  * @param minBlockNumber - The minimum block number that we must sync to
133
- * @returns A promise that resolves once the sync has completed.
138
+ * @returns A promise that resolves with the block number the world state was synced to
134
139
  */
135
- public async syncImmediate(minBlockNumber?: number): Promise<void> {
140
+ public async syncImmediate(minBlockNumber?: number): Promise<number> {
136
141
  if (this.currentState !== WorldStateRunningState.RUNNING) {
137
142
  throw new Error(`World State is not running, unable to perform sync`);
138
143
  }
139
144
  // If we have been given a block number to sync to and we have reached that number
140
145
  // then return.
141
146
  if (minBlockNumber !== undefined && minBlockNumber <= this.currentL2BlockNum) {
142
- return;
147
+ return this.currentL2BlockNum;
143
148
  }
144
149
  const blockToSyncTo = minBlockNumber === undefined ? 'latest' : `${minBlockNumber}`;
145
150
  this.log(`World State at block ${this.currentL2BlockNum}, told to sync to block ${blockToSyncTo}...`);
@@ -148,7 +153,7 @@ export class ServerWorldStateSynchronizer implements WorldStateSynchronizer {
148
153
  while (true) {
149
154
  // Check the block number again
150
155
  if (minBlockNumber !== undefined && minBlockNumber <= this.currentL2BlockNum) {
151
- return;
156
+ return this.currentL2BlockNum;
152
157
  }
153
158
  // Poll for more blocks
154
159
  const numBlocks = await this.l2BlockDownloader.pollImmediate();
@@ -164,7 +169,7 @@ export class ServerWorldStateSynchronizer implements WorldStateSynchronizer {
164
169
  `Unable to sync to block number ${minBlockNumber}, currently synced to block ${this.currentL2BlockNum}`,
165
170
  );
166
171
  }
167
- return;
172
+ return this.currentL2BlockNum;
168
173
  }
169
174
  }
170
175
 
@@ -48,9 +48,9 @@ export interface WorldStateSynchronizer {
48
48
  /**
49
49
  * Forces an immediate sync to an optionally provided minimum block number
50
50
  * @param minBlockNumber - The minimum block number that we must sync to
51
- * @returns A promise that resolves once the sync has completed.
51
+ * @returns A promise that resolves with the block number the world state was synced to
52
52
  */
53
- syncImmediate(minBlockNumber?: number): Promise<void>;
53
+ syncImmediate(minBlockNumber?: number): Promise<number>;
54
54
 
55
55
  /**
56
56
  * Returns an instance of MerkleTreeOperations that will include uncommitted data.
@@ -63,4 +63,11 @@ export interface WorldStateSynchronizer {
63
63
  * @returns An instance of MerkleTreeOperations that will not include uncommitted data.
64
64
  */
65
65
  getCommitted(): MerkleTreeOperations;
66
+
67
+ /**
68
+ * Returns a readonly instance of MerkleTreeOperations where the state is as it was at the given block number
69
+ * @param block - The block number to look at
70
+ * @returns An instance of MerkleTreeOperations
71
+ */
72
+ getSnapshot(block: number): MerkleTreeOperations;
66
73
  }
@@ -1,3 +1,2 @@
1
1
  export * from './merkle_trees.js';
2
2
  export * from './merkle_tree_db.js';
3
- export { LeafData } from '@aztec/merkle-tree';
@@ -1,7 +1,8 @@
1
- import { MAX_NEW_NULLIFIERS_PER_TX } from '@aztec/circuits.js';
1
+ import { MAX_NEW_NULLIFIERS_PER_TX, NullifierLeafPreimage } from '@aztec/circuits.js';
2
2
  import { Fr } from '@aztec/foundation/fields';
3
3
  import { createDebugLogger } from '@aztec/foundation/log';
4
- import { LeafData, LowLeafWitnessData } from '@aztec/merkle-tree';
4
+ import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
5
+ import { BatchInsertionResult, IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
5
6
  import { L2Block, MerkleTreeId, SiblingPath } from '@aztec/types';
6
7
 
7
8
  /**
@@ -91,7 +92,13 @@ export type MerkleTreeDb = {
91
92
  [Property in keyof MerkleTreeOperations as Exclude<Property, MerkleTreeSetters>]: WithIncludeUncommitted<
92
93
  MerkleTreeOperations[Property]
93
94
  >;
94
- } & Pick<MerkleTreeOperations, MerkleTreeSetters>;
95
+ } & Pick<MerkleTreeOperations, MerkleTreeSetters> & {
96
+ /**
97
+ * Returns a snapshot of the current state of the trees.
98
+ * @param block - The block number to take the snapshot at.
99
+ */
100
+ getSnapshot(block: number): Promise<ReadonlyArray<TreeSnapshot | IndexedTreeSnapshot>>;
101
+ };
95
102
 
96
103
  /**
97
104
  * Defines the interface for operations on a set of Merkle Trees.
@@ -130,23 +137,26 @@ export interface MerkleTreeOperations {
130
137
  getPreviousValueIndex(
131
138
  treeId: IndexedTreeId,
132
139
  value: bigint,
133
- ): Promise<{
134
- /**
135
- * The index of the found leaf.
136
- */
137
- index: number;
138
- /**
139
- * A flag indicating if the corresponding leaf's value is equal to `newValue`.
140
- */
141
- alreadyPresent: boolean;
142
- }>;
140
+ ): Promise<
141
+ | {
142
+ /**
143
+ * The index of the found leaf.
144
+ */
145
+ index: bigint;
146
+ /**
147
+ * A flag indicating if the corresponding leaf's value is equal to `newValue`.
148
+ */
149
+ alreadyPresent: boolean;
150
+ }
151
+ | undefined
152
+ >;
143
153
 
144
154
  /**
145
155
  * Returns the data at a specific leaf.
146
156
  * @param treeId - The tree for which leaf data should be returned.
147
157
  * @param index - The index of the leaf required.
148
158
  */
149
- getLeafData(treeId: IndexedTreeId, index: number): Promise<LeafData | undefined>;
159
+ getLeafPreimage(treeId: IndexedTreeId, index: bigint): Promise<IndexedTreeLeafPreimage | undefined>;
150
160
 
151
161
  /**
152
162
  * Update the leaf data at the given index.
@@ -154,7 +164,7 @@ export interface MerkleTreeOperations {
154
164
  * @param leaf - The updated leaf value.
155
165
  * @param index - The index of the leaf to be updated.
156
166
  */
157
- updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: LeafData | Buffer, index: bigint): Promise<void>;
167
+ updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: NullifierLeafPreimage | Buffer, index: bigint): Promise<void>;
158
168
 
159
169
  /**
160
170
  * Returns the index containing a leaf value.
@@ -175,7 +185,7 @@ export interface MerkleTreeOperations {
175
185
  * This includes all of the current roots of all of the data trees and the current blocks global vars.
176
186
  * @param globalVariablesHash - The global variables hash to insert into the block hash.
177
187
  */
178
- updateHistoricBlocksTree(globalVariablesHash: Fr): Promise<void>;
188
+ updateBlocksTree(globalVariablesHash: Fr): Promise<void>;
179
189
 
180
190
  /**
181
191
  * Updates the latest global variables hash
@@ -195,11 +205,11 @@ export interface MerkleTreeOperations {
195
205
  * @param subtreeHeight - Height of the subtree.
196
206
  * @returns The witness data for the leaves to be updated when inserting the new ones.
197
207
  */
198
- batchInsert(
208
+ batchInsert<TreeHeight extends number, SubtreeSiblingPathHeight extends number>(
199
209
  treeId: MerkleTreeId,
200
210
  leaves: Buffer[],
201
211
  subtreeHeight: number,
202
- ): Promise<[LowLeafWitnessData<number>[], SiblingPath<number>] | [undefined, SiblingPath<number>]>;
212
+ ): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>>;
203
213
 
204
214
  /**
205
215
  * Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
@@ -1,23 +1,25 @@
1
1
  import {
2
+ BLOCKS_TREE_HEIGHT,
2
3
  CONTRACT_TREE_HEIGHT,
3
4
  Fr,
4
5
  GlobalVariables,
5
- HISTORIC_BLOCKS_TREE_HEIGHT,
6
6
  L1_TO_L2_MSG_TREE_HEIGHT,
7
7
  NOTE_HASH_TREE_HEIGHT,
8
8
  NULLIFIER_SUBTREE_HEIGHT,
9
9
  NULLIFIER_TREE_HEIGHT,
10
+ NullifierLeaf,
11
+ NullifierLeafPreimage,
10
12
  PUBLIC_DATA_TREE_HEIGHT,
11
13
  } from '@aztec/circuits.js';
12
14
  import { computeBlockHash, computeGlobalsHash } from '@aztec/circuits.js/abis';
13
15
  import { Committable } from '@aztec/foundation/committable';
14
16
  import { SerialQueue } from '@aztec/foundation/fifo';
15
17
  import { createDebugLogger } from '@aztec/foundation/log';
18
+ import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
16
19
  import {
17
20
  AppendOnlyTree,
21
+ BatchInsertionResult,
18
22
  IndexedTree,
19
- LeafData,
20
- LowLeafWitnessData,
21
23
  Pedersen,
22
24
  SparseTree,
23
25
  StandardIndexedTree,
@@ -26,7 +28,7 @@ import {
26
28
  loadTree,
27
29
  newTree,
28
30
  } from '@aztec/merkle-tree';
29
- import { L2Block, MerkleTreeId, SiblingPath } from '@aztec/types';
31
+ import { Hasher, L2Block, MerkleTreeId, SiblingPath } from '@aztec/types';
30
32
 
31
33
  import { default as levelup } from 'levelup';
32
34
 
@@ -47,9 +49,20 @@ import {
47
49
  */
48
50
  interface FromDbOptions {
49
51
  /**
50
- * The global variables from the last block.
52
+ * The global variables hash from the last block.
51
53
  */
52
- globalVariables: GlobalVariables;
54
+ globalVariablesHash: Fr;
55
+ }
56
+
57
+ const LAST_GLOBAL_VARS_HASH = 'lastGlobalVarsHash';
58
+
59
+ /**
60
+ * The nullifier tree is an indexed tree.
61
+ */
62
+ class NullifierTree extends StandardIndexedTree {
63
+ constructor(db: levelup.LevelUp, hasher: Hasher, name: string, depth: number, size: bigint = 0n, root?: Buffer) {
64
+ super(db, hasher, name, depth, size, NullifierLeafPreimage, NullifierLeaf, root);
65
+ }
53
66
  }
54
67
 
55
68
  /**
@@ -81,7 +94,7 @@ export class MerkleTrees implements MerkleTreeDb {
81
94
  CONTRACT_TREE_HEIGHT,
82
95
  );
83
96
  const nullifierTree = await initializeTree(
84
- StandardIndexedTree,
97
+ NullifierTree,
85
98
  this.db,
86
99
  hasher,
87
100
  `${MerkleTreeId[MerkleTreeId.NULLIFIER_TREE]}`,
@@ -109,14 +122,14 @@ export class MerkleTrees implements MerkleTreeDb {
109
122
  `${MerkleTreeId[MerkleTreeId.L1_TO_L2_MESSAGES_TREE]}`,
110
123
  L1_TO_L2_MSG_TREE_HEIGHT,
111
124
  );
112
- const historicBlocksTree: AppendOnlyTree = await initializeTree(
125
+ const blocksTree: AppendOnlyTree = await initializeTree(
113
126
  StandardTree,
114
127
  this.db,
115
128
  hasher,
116
129
  `${MerkleTreeId[MerkleTreeId.BLOCKS_TREE]}`,
117
- HISTORIC_BLOCKS_TREE_HEIGHT,
130
+ BLOCKS_TREE_HEIGHT,
118
131
  );
119
- this.trees = [contractTree, nullifierTree, noteHashTree, publicDataTree, l1Tol2MessagesTree, historicBlocksTree];
132
+ this.trees = [contractTree, nullifierTree, noteHashTree, publicDataTree, l1Tol2MessagesTree, blocksTree];
120
133
 
121
134
  this.jobQueue.start();
122
135
 
@@ -124,10 +137,12 @@ export class MerkleTrees implements MerkleTreeDb {
124
137
  if (!fromDb) {
125
138
  const initialGlobalVariablesHash = computeGlobalsHash(GlobalVariables.empty());
126
139
  await this._updateLatestGlobalVariablesHash(initialGlobalVariablesHash);
127
- await this._updateHistoricBlocksTree(initialGlobalVariablesHash, true);
140
+ await this._updateBlocksTree(initialGlobalVariablesHash, true);
128
141
  await this._commit();
129
142
  } else {
130
- await this._updateLatestGlobalVariablesHash(computeGlobalsHash(fromDbOptions.globalVariables));
143
+ await this._updateLatestGlobalVariablesHash(fromDbOptions.globalVariablesHash);
144
+ // make the restored global variables hash and tree roots current
145
+ await this._commit();
131
146
  }
132
147
  }
133
148
 
@@ -138,7 +153,10 @@ export class MerkleTrees implements MerkleTreeDb {
138
153
  */
139
154
  public static async new(db: levelup.LevelUp) {
140
155
  const merkleTrees = new MerkleTrees(db);
141
- await merkleTrees.init();
156
+ const globalVariablesHash: Buffer | undefined = await db.get(LAST_GLOBAL_VARS_HASH).catch(() => undefined);
157
+ await merkleTrees.init(
158
+ globalVariablesHash ? { globalVariablesHash: Fr.fromBuffer(globalVariablesHash) } : undefined,
159
+ );
142
160
  return merkleTrees;
143
161
  }
144
162
 
@@ -171,8 +189,8 @@ export class MerkleTrees implements MerkleTreeDb {
171
189
  * @param globalsHash - The current global variables hash.
172
190
  * @param includeUncommitted - Indicates whether to include uncommitted data.
173
191
  */
174
- public async updateHistoricBlocksTree(globalsHash: Fr, includeUncommitted: boolean) {
175
- await this.synchronize(() => this._updateHistoricBlocksTree(globalsHash, includeUncommitted));
192
+ public async updateBlocksTree(globalsHash: Fr, includeUncommitted: boolean) {
193
+ await this.synchronize(() => this._updateBlocksTree(globalsHash, includeUncommitted));
176
194
  }
177
195
 
178
196
  /**
@@ -304,19 +322,20 @@ export class MerkleTrees implements MerkleTreeDb {
304
322
  treeId: IndexedTreeId,
305
323
  value: bigint,
306
324
  includeUncommitted: boolean,
307
- ): Promise<{
308
- /**
309
- * The index of the found leaf.
310
- */
311
- index: number;
312
- /**
313
- * A flag indicating if the corresponding leaf's value is equal to `newValue`.
314
- */
315
- alreadyPresent: boolean;
316
- }> {
317
- return await this.synchronize(() =>
318
- Promise.resolve(this._getIndexedTree(treeId).findIndexOfPreviousValue(value, includeUncommitted)),
319
- );
325
+ ): Promise<
326
+ | {
327
+ /**
328
+ * The index of the found leaf.
329
+ */
330
+ index: bigint;
331
+ /**
332
+ * A flag indicating if the corresponding leaf's value is equal to `newValue`.
333
+ */
334
+ alreadyPresent: boolean;
335
+ }
336
+ | undefined
337
+ > {
338
+ return await this.synchronize(() => this._getIndexedTree(treeId).findIndexOfPreviousKey(value, includeUncommitted));
320
339
  }
321
340
 
322
341
  /**
@@ -324,15 +343,15 @@ export class MerkleTrees implements MerkleTreeDb {
324
343
  * @param treeId - The ID of the tree get the leaf from.
325
344
  * @param index - The index of the leaf to get.
326
345
  * @param includeUncommitted - Indicates whether to include uncommitted data.
327
- * @returns Leaf data.
346
+ * @returns Leaf preimage.
328
347
  */
329
- public async getLeafData(
348
+ public async getLeafPreimage(
330
349
  treeId: IndexedTreeId,
331
- index: number,
350
+ index: bigint,
332
351
  includeUncommitted: boolean,
333
- ): Promise<LeafData | undefined> {
352
+ ): Promise<IndexedTreeLeafPreimage | undefined> {
334
353
  return await this.synchronize(() =>
335
- Promise.resolve(this._getIndexedTree(treeId).getLatestLeafDataCopy(index, includeUncommitted)),
354
+ this._getIndexedTree(treeId).getLatestLeafPreimageCopy(index, includeUncommitted),
336
355
  );
337
356
  }
338
357
 
@@ -350,13 +369,7 @@ export class MerkleTrees implements MerkleTreeDb {
350
369
  ): Promise<bigint | undefined> {
351
370
  return await this.synchronize(async () => {
352
371
  const tree = this.trees[treeId];
353
- for (let i = 0n; i < tree.getNumLeaves(includeUncommitted); i++) {
354
- const currentValue = await tree.getLeafValue(i, includeUncommitted);
355
- if (currentValue && currentValue.equals(value)) {
356
- return i;
357
- }
358
- }
359
- return undefined;
372
+ return await tree.findLeafIndex(value, includeUncommitted);
360
373
  });
361
374
  }
362
375
 
@@ -367,7 +380,7 @@ export class MerkleTrees implements MerkleTreeDb {
367
380
  * @param index - The index to insert into.
368
381
  * @returns Empty promise.
369
382
  */
370
- public async updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: LeafData | Buffer, index: bigint): Promise<void> {
383
+ public async updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: Buffer, index: bigint): Promise<void> {
371
384
  return await this.synchronize(() => this._updateLeaf(treeId, leaf, index));
372
385
  }
373
386
 
@@ -395,10 +408,7 @@ export class MerkleTrees implements MerkleTreeDb {
395
408
  treeId: MerkleTreeId,
396
409
  leaves: Buffer[],
397
410
  subtreeHeight: SubtreeHeight,
398
- ): Promise<
399
- | [LowLeafWitnessData<TreeHeight>[], SiblingPath<SubtreeSiblingPathHeight>]
400
- | [undefined, SiblingPath<SubtreeSiblingPathHeight>]
401
- > {
411
+ ): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>> {
402
412
  const tree = this.trees[treeId] as StandardIndexedTree;
403
413
  if (!('batchInsert' in tree)) {
404
414
  throw new Error('Tree does not support `batchInsert` method');
@@ -424,7 +434,7 @@ export class MerkleTrees implements MerkleTreeDb {
424
434
  return Promise.resolve(this.latestGlobalVariablesHash.get(includeUncommitted));
425
435
  }
426
436
 
427
- private async _updateHistoricBlocksTree(globalsHash: Fr, includeUncommitted: boolean) {
437
+ private async _updateBlocksTree(globalsHash: Fr, includeUncommitted: boolean) {
428
438
  const blockHash = await this._getCurrentBlockHash(globalsHash, includeUncommitted);
429
439
  await this._appendLeaves(MerkleTreeId.BLOCKS_TREE, [blockHash.toBuffer()]);
430
440
  }
@@ -483,11 +493,7 @@ export class MerkleTrees implements MerkleTreeDb {
483
493
  return await tree.appendLeaves(leaves);
484
494
  }
485
495
 
486
- private async _updateLeaf(
487
- treeId: IndexedTreeId | PublicTreeId,
488
- leaf: LeafData | Buffer,
489
- index: bigint,
490
- ): Promise<void> {
496
+ private async _updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: Buffer, index: bigint): Promise<void> {
491
497
  const tree = this.trees[treeId];
492
498
  if (!('updateLeaf' in tree)) {
493
499
  throw new Error('Tree does not support `updateLeaf` method');
@@ -504,6 +510,7 @@ export class MerkleTrees implements MerkleTreeDb {
504
510
  await tree.commit();
505
511
  }
506
512
  this.latestGlobalVariablesHash.commit();
513
+ await this.db.put(LAST_GLOBAL_VARS_HASH, this.latestGlobalVariablesHash.get().toBuffer());
507
514
  }
508
515
 
509
516
  /**
@@ -517,6 +524,16 @@ export class MerkleTrees implements MerkleTreeDb {
517
524
  this.latestGlobalVariablesHash.rollback();
518
525
  }
519
526
 
527
+ public getSnapshot(blockNumber: number) {
528
+ return Promise.all(this.trees.map(tree => tree.getSnapshot(blockNumber)));
529
+ }
530
+
531
+ private async _snapshot(blockNumber: number): Promise<void> {
532
+ for (const tree of this.trees) {
533
+ await tree.snapshot(blockNumber);
534
+ }
535
+ }
536
+
520
537
  /**
521
538
  * Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
522
539
  * @param l2Block - The L2 block to handle.
@@ -528,7 +545,7 @@ export class MerkleTrees implements MerkleTreeDb {
528
545
  [l2Block.endNoteHashTreeSnapshot.root, MerkleTreeId.NOTE_HASH_TREE],
529
546
  [l2Block.endPublicDataTreeRoot, MerkleTreeId.PUBLIC_DATA_TREE],
530
547
  [l2Block.endL1ToL2MessagesTreeSnapshot.root, MerkleTreeId.L1_TO_L2_MESSAGES_TREE],
531
- [l2Block.endHistoricBlocksTreeSnapshot.root, MerkleTreeId.BLOCKS_TREE],
548
+ [l2Block.endBlocksTreeSnapshot.root, MerkleTreeId.BLOCKS_TREE],
532
549
  ] as const;
533
550
  const compareRoot = (root: Fr, treeId: MerkleTreeId) => {
534
551
  const treeRoot = this.trees[treeId].getRoot(true);
@@ -569,7 +586,7 @@ export class MerkleTrees implements MerkleTreeDb {
569
586
  await this._updateLeaf(MerkleTreeId.PUBLIC_DATA_TREE, newValue.toBuffer(), leafIndex.value);
570
587
  }
571
588
 
572
- // Sync and add the block to the historic blocks tree
589
+ // Sync and add the block to the blocks tree
573
590
  const globalVariablesHash = computeGlobalsHash(l2Block.globalVariables);
574
591
  await this._updateLatestGlobalVariablesHash(globalVariablesHash);
575
592
  this.log(`Synced global variables with hash ${globalVariablesHash}`);
@@ -595,6 +612,8 @@ export class MerkleTrees implements MerkleTreeDb {
595
612
  }
596
613
  }
597
614
 
615
+ await this._snapshot(l2Block.number);
616
+
598
617
  return { isBlockOurs: ourBlock };
599
618
  }
600
619
  }