@aztec/world-state 3.0.0-devnet.2 → 3.0.0-devnet.2-patch.1

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 (49) hide show
  1. package/dest/index.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts +1 -1
  3. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  4. package/dest/native/bench_metrics.d.ts +1 -1
  5. package/dest/native/bench_metrics.d.ts.map +1 -1
  6. package/dest/native/fork_checkpoint.d.ts +1 -1
  7. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  8. package/dest/native/index.d.ts +1 -1
  9. package/dest/native/merkle_trees_facade.d.ts +6 -5
  10. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  11. package/dest/native/merkle_trees_facade.js +5 -3
  12. package/dest/native/message.d.ts +12 -11
  13. package/dest/native/message.d.ts.map +1 -1
  14. package/dest/native/message.js +14 -13
  15. package/dest/native/native_world_state.d.ts +10 -9
  16. package/dest/native/native_world_state.d.ts.map +1 -1
  17. package/dest/native/native_world_state.js +13 -9
  18. package/dest/native/native_world_state_instance.d.ts +10 -1
  19. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  20. package/dest/native/native_world_state_instance.js +21 -0
  21. package/dest/native/world_state_ops_queue.d.ts +1 -1
  22. package/dest/native/world_state_ops_queue.d.ts.map +1 -1
  23. package/dest/synchronizer/config.d.ts +1 -1
  24. package/dest/synchronizer/errors.d.ts +1 -1
  25. package/dest/synchronizer/errors.d.ts.map +1 -1
  26. package/dest/synchronizer/factory.d.ts +1 -1
  27. package/dest/synchronizer/index.d.ts +1 -1
  28. package/dest/synchronizer/server_world_state_synchronizer.d.ts +7 -25
  29. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  30. package/dest/synchronizer/server_world_state_synchronizer.js +32 -48
  31. package/dest/test/index.d.ts +1 -1
  32. package/dest/test/utils.d.ts +16 -9
  33. package/dest/test/utils.d.ts.map +1 -1
  34. package/dest/test/utils.js +52 -52
  35. package/dest/testing.d.ts +2 -2
  36. package/dest/testing.d.ts.map +1 -1
  37. package/dest/testing.js +1 -1
  38. package/dest/world-state-db/index.d.ts +1 -1
  39. package/dest/world-state-db/merkle_tree_db.d.ts +8 -9
  40. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  41. package/package.json +13 -12
  42. package/src/native/merkle_trees_facade.ts +7 -6
  43. package/src/native/message.ts +23 -22
  44. package/src/native/native_world_state.ts +29 -21
  45. package/src/native/native_world_state_instance.ts +28 -0
  46. package/src/synchronizer/server_world_state_synchronizer.ts +59 -73
  47. package/src/test/utils.ts +88 -94
  48. package/src/testing.ts +1 -1
  49. package/src/world-state-db/merkle_tree_db.ts +7 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/world-state",
3
- "version": "3.0.0-devnet.2",
3
+ "version": "3.0.0-devnet.2-patch.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
@@ -18,8 +18,8 @@
18
18
  "tsconfig": "./tsconfig.json"
19
19
  },
20
20
  "scripts": {
21
- "build": "yarn clean && tsc -b",
22
- "build:dev": "tsc -b --watch",
21
+ "build": "yarn clean && ../scripts/tsc.sh",
22
+ "build:dev": "../scripts/tsc.sh --watch",
23
23
  "clean": "rm -rf ./dest .tsbuildinfo",
24
24
  "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
25
25
  },
@@ -64,22 +64,23 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "@aztec/constants": "3.0.0-devnet.2",
68
- "@aztec/foundation": "3.0.0-devnet.2",
69
- "@aztec/kv-store": "3.0.0-devnet.2",
70
- "@aztec/merkle-tree": "3.0.0-devnet.2",
71
- "@aztec/native": "3.0.0-devnet.2",
72
- "@aztec/protocol-contracts": "3.0.0-devnet.2",
73
- "@aztec/stdlib": "3.0.0-devnet.2",
74
- "@aztec/telemetry-client": "3.0.0-devnet.2",
67
+ "@aztec/constants": "3.0.0-devnet.2-patch.1",
68
+ "@aztec/foundation": "3.0.0-devnet.2-patch.1",
69
+ "@aztec/kv-store": "3.0.0-devnet.2-patch.1",
70
+ "@aztec/merkle-tree": "3.0.0-devnet.2-patch.1",
71
+ "@aztec/native": "3.0.0-devnet.2-patch.1",
72
+ "@aztec/protocol-contracts": "3.0.0-devnet.2-patch.1",
73
+ "@aztec/stdlib": "3.0.0-devnet.2-patch.1",
74
+ "@aztec/telemetry-client": "3.0.0-devnet.2-patch.1",
75
75
  "tslib": "^2.4.0",
76
76
  "zod": "^3.23.8"
77
77
  },
78
78
  "devDependencies": {
79
- "@aztec/archiver": "3.0.0-devnet.2",
79
+ "@aztec/archiver": "3.0.0-devnet.2-patch.1",
80
80
  "@jest/globals": "^30.0.0",
81
81
  "@types/jest": "^30.0.0",
82
82
  "@types/node": "^22.15.17",
83
+ "@typescript/native-preview": "7.0.0-dev.20251126.1",
83
84
  "jest": "^30.0.0",
84
85
  "jest-mock-extended": "^4.0.0",
85
86
  "ts-node": "^10.9.1",
@@ -1,4 +1,5 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import { serializeToBuffer } from '@aztec/foundation/serialize';
3
4
  import { type IndexedTreeLeafPreimage, SiblingPath } from '@aztec/foundation/trees';
4
5
  import type {
@@ -18,7 +19,7 @@ import {
18
19
  PublicDataTreeLeafPreimage,
19
20
  } from '@aztec/stdlib/trees';
20
21
  import { type BlockHeader, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
21
- import type { WorldStateRevision } from '@aztec/stdlib/world-state';
22
+ import { type WorldStateRevision, WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
22
23
 
23
24
  import assert from 'assert';
24
25
 
@@ -42,8 +43,8 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
42
43
  return this.initialHeader;
43
44
  }
44
45
 
45
- getRevision(): WorldStateRevision {
46
- return this.revision;
46
+ getRevision(): WorldStateRevisionWithHandle {
47
+ return WorldStateRevisionWithHandle.fromWorldStateRevision(this.revision, this.instance.getHandle());
47
48
  }
48
49
 
49
50
  findLeafIndices(treeId: MerkleTreeId, values: MerkleTreeLeafType<MerkleTreeId>[]): Promise<(bigint | undefined)[]> {
@@ -191,14 +192,14 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
191
192
  async getBlockNumbersForLeafIndices<ID extends MerkleTreeId>(
192
193
  treeId: ID,
193
194
  leafIndices: bigint[],
194
- ): Promise<(bigint | undefined)[]> {
195
+ ): Promise<(BlockNumber | undefined)[]> {
195
196
  const response = await this.instance.call(WorldStateMessageType.GET_BLOCK_NUMBERS_FOR_LEAF_INDICES, {
196
197
  treeId,
197
198
  revision: this.revision,
198
199
  leafIndices,
199
200
  });
200
201
 
201
- return response.blockNumbers.map(x => (x === undefined || x === null ? undefined : BigInt(x)));
202
+ return response.blockNumbers.map(x => (x === undefined || x === null ? undefined : BlockNumber(Number(x))));
202
203
  }
203
204
  }
204
205
 
@@ -1,4 +1,5 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import type { Tuple } from '@aztec/foundation/serialize';
3
4
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
4
5
  import type { StateReference } from '@aztec/stdlib/tx';
@@ -56,11 +57,11 @@ interface WithTreeId {
56
57
 
57
58
  export interface WorldStateStatusSummary {
58
59
  /** Last block number that can still be unwound. */
59
- unfinalizedBlockNumber: bigint;
60
+ unfinalizedBlockNumber: BlockNumber;
60
61
  /** Last block number that is finalized and cannot be unwound. */
61
- finalizedBlockNumber: bigint;
62
+ finalizedBlockNumber: BlockNumber;
62
63
  /** Oldest block still available for historical queries and forks. */
63
- oldestHistoricalBlock: bigint;
64
+ oldestHistoricalBlock: BlockNumber;
64
65
  /** Whether the trees are in sync with each other */
65
66
  treesAreSynched: boolean;
66
67
  }
@@ -81,11 +82,11 @@ export interface TreeMeta {
81
82
  /** The tree's initial root value */
82
83
  initialRoot: Fr;
83
84
  /** The current oldest historical block number of the tree */
84
- oldestHistoricBlock: bigint;
85
+ oldestHistoricBlock: BlockNumber;
85
86
  /** The current unfinalized block number of the tree */
86
- unfinalizedBlockHeight: bigint;
87
+ unfinalizedBlockHeight: BlockNumber;
87
88
  /** The current finalized block number of the tree */
88
- finalizedBlockHeight: bigint;
89
+ finalizedBlockHeight: BlockNumber;
89
90
  }
90
91
 
91
92
  export interface DBStats {
@@ -173,9 +174,9 @@ export function buildEmptyTreeMeta() {
173
174
  depth: 0,
174
175
  size: 0n,
175
176
  committedSize: 0n,
176
- unfinalizedBlockHeight: 0n,
177
- finalizedBlockHeight: 0n,
178
- oldestHistoricBlock: 0n,
177
+ unfinalizedBlockHeight: BlockNumber.ZERO,
178
+ finalizedBlockHeight: BlockNumber.ZERO,
179
+ oldestHistoricBlock: BlockNumber.ZERO,
179
180
  root: Fr.ZERO,
180
181
  initialRoot: Fr.ZERO,
181
182
  initialSize: 0n,
@@ -204,9 +205,9 @@ export function buildEmptyWorldStateDBStats() {
204
205
 
205
206
  export function buildEmptyWorldStateSummary() {
206
207
  return {
207
- unfinalizedBlockNumber: 0n,
208
- finalizedBlockNumber: 0n,
209
- oldestHistoricalBlock: 0n,
208
+ unfinalizedBlockNumber: BlockNumber.ZERO,
209
+ finalizedBlockNumber: BlockNumber.ZERO,
210
+ oldestHistoricalBlock: BlockNumber.ZERO,
210
211
  treesAreSynched: true,
211
212
  } as WorldStateStatusSummary;
212
213
  }
@@ -220,9 +221,9 @@ export function buildEmptyWorldStateStatusFull() {
220
221
  }
221
222
 
222
223
  export function sanitizeSummary(summary: WorldStateStatusSummary) {
223
- summary.finalizedBlockNumber = BigInt(summary.finalizedBlockNumber);
224
- summary.unfinalizedBlockNumber = BigInt(summary.unfinalizedBlockNumber);
225
- summary.oldestHistoricalBlock = BigInt(summary.oldestHistoricalBlock);
224
+ summary.finalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.finalizedBlockNumber));
225
+ summary.unfinalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.unfinalizedBlockNumber));
226
+ summary.oldestHistoricalBlock = BlockNumber.fromBigInt(BigInt(summary.oldestHistoricalBlock));
226
227
  return summary;
227
228
  }
228
229
 
@@ -234,11 +235,11 @@ export function sanitizeDBStats(stats: DBStats) {
234
235
 
235
236
  export function sanitizeMeta(meta: TreeMeta) {
236
237
  meta.committedSize = BigInt(meta.committedSize);
237
- meta.finalizedBlockHeight = BigInt(meta.finalizedBlockHeight);
238
+ meta.finalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.finalizedBlockHeight));
238
239
  meta.initialSize = BigInt(meta.initialSize);
239
- meta.oldestHistoricBlock = BigInt(meta.oldestHistoricBlock);
240
+ meta.oldestHistoricBlock = BlockNumber.fromBigInt(BigInt(meta.oldestHistoricBlock));
240
241
  meta.size = BigInt(meta.size);
241
- meta.unfinalizedBlockHeight = BigInt(meta.unfinalizedBlockHeight);
242
+ meta.unfinalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.unfinalizedBlockHeight));
242
243
  return meta;
243
244
  }
244
245
 
@@ -310,7 +311,7 @@ interface WithLeafValues {
310
311
  }
311
312
 
312
313
  interface BlockShiftRequest extends WithCanonicalForkId {
313
- toBlockNumber: bigint;
314
+ toBlockNumber: BlockNumber;
314
315
  }
315
316
 
316
317
  interface WithLeaves {
@@ -409,7 +410,7 @@ interface UpdateArchiveRequest extends WithForkId {
409
410
  }
410
411
 
411
412
  interface SyncBlockRequest extends WithCanonicalForkId {
412
- blockNumber: number;
413
+ blockNumber: BlockNumber;
413
414
  blockStateRef: BlockStateReference;
414
415
  blockHeaderHash: Fr;
415
416
  paddedNoteHashes: readonly SerializedLeafValue[];
@@ -420,7 +421,7 @@ interface SyncBlockRequest extends WithCanonicalForkId {
420
421
 
421
422
  interface CreateForkRequest extends WithCanonicalForkId {
422
423
  latest: boolean;
423
- blockNumber: number;
424
+ blockNumber: BlockNumber;
424
425
  }
425
426
 
426
427
  interface CreateForkResponse {
@@ -1,10 +1,11 @@
1
1
  import { MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
3
  import { fromEntries, padArrayEnd } from '@aztec/foundation/collection';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
5
  import { EthAddress } from '@aztec/foundation/eth-address';
4
- import { Fr } from '@aztec/foundation/fields';
5
6
  import { tryRmDir } from '@aztec/foundation/fs';
6
7
  import { type Logger, createLogger } from '@aztec/foundation/log';
7
- import type { L2Block } from '@aztec/stdlib/block';
8
+ import type { L2BlockNew } from '@aztec/stdlib/block';
8
9
  import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
9
10
  import type {
10
11
  IndexedTreeId,
@@ -150,7 +151,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
150
151
  return new MerkleTreesFacade(this.instance, this.initialHeader!, WorldStateRevision.empty());
151
152
  }
152
153
 
153
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
154
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
154
155
  return new MerkleTreesFacade(
155
156
  this.instance,
156
157
  this.initialHeader!,
@@ -158,16 +159,20 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
158
159
  );
159
160
  }
160
161
 
161
- public async fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
162
+ public async fork(blockNumber?: BlockNumber): Promise<MerkleTreeWriteOperations> {
162
163
  const resp = await this.instance.call(WorldStateMessageType.CREATE_FORK, {
163
164
  latest: blockNumber === undefined,
164
- blockNumber: blockNumber ?? 0,
165
+ blockNumber: blockNumber ?? BlockNumber.ZERO,
165
166
  canonical: true,
166
167
  });
167
168
  return new MerkleTreesForkFacade(
168
169
  this.instance,
169
170
  this.initialHeader!,
170
- new WorldStateRevision(/*forkId=*/ resp.forkId, /* blockNumber=*/ 0, /* includeUncommitted=*/ true),
171
+ new WorldStateRevision(
172
+ /*forkId=*/ resp.forkId,
173
+ /* blockNumber=*/ BlockNumber.ZERO,
174
+ /* includeUncommitted=*/ true,
175
+ ),
171
176
  );
172
177
  }
173
178
 
@@ -175,21 +180,24 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
175
180
  return this.initialHeader!;
176
181
  }
177
182
 
178
- public async handleL2BlockAndMessages(
179
- l2Block: L2Block,
180
- l1ToL2Messages: Fr[],
181
- // TODO(#17027)
182
- // Temporary hack to only insert l1 to l2 messages for the first block in a checkpoint.
183
- isFirstBlock = true,
184
- ): Promise<WorldStateStatusFull> {
185
- // We have to pad both the values within tx effects because that's how the trees are built by circuits.
186
- const paddedNoteHashes = l2Block.body.txEffects.flatMap(txEffect =>
187
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
188
- );
183
+ public async handleL2BlockAndMessages(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
184
+ const isFirstBlock = l2Block.indexWithinCheckpoint === 0;
185
+ if (!isFirstBlock && l1ToL2Messages.length > 0) {
186
+ throw new Error(
187
+ `L1 to L2 messages must be empty for non-first blocks, but got ${l1ToL2Messages.length} messages for block ${l2Block.number}.`,
188
+ );
189
+ }
190
+
191
+ // We have to pad the given l1 to l2 messages, and the note hashes and nullifiers within tx effects, because that's
192
+ // how the trees are built by circuits.
189
193
  const paddedL1ToL2Messages = isFirstBlock
190
194
  ? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
191
195
  : [];
192
196
 
197
+ const paddedNoteHashes = l2Block.body.txEffects.flatMap(txEffect =>
198
+ padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
199
+ );
200
+
193
201
  const paddedNullifiers = l2Block.body.txEffects
194
202
  .flatMap(txEffect => padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX))
195
203
  .map(nullifier => new NullifierLeaf(nullifier));
@@ -208,7 +216,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
208
216
  WorldStateMessageType.SYNC_BLOCK,
209
217
  {
210
218
  blockNumber: l2Block.number,
211
- blockHeaderHash: await l2Block.getBlockHeader().hash(),
219
+ blockHeaderHash: await l2Block.hash(),
212
220
  paddedL1ToL2Messages: paddedL1ToL2Messages.map(serializeLeaf),
213
221
  paddedNoteHashes: paddedNoteHashes.map(serializeLeaf),
214
222
  paddedNullifiers: paddedNullifiers.map(serializeLeaf),
@@ -256,7 +264,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
256
264
  * @param toBlockNumber The block number that is now the tip of the finalized chain
257
265
  * @returns The new WorldStateStatus
258
266
  */
259
- public async setFinalized(toBlockNumber: bigint) {
267
+ public async setFinalized(toBlockNumber: BlockNumber) {
260
268
  try {
261
269
  await this.instance.call(
262
270
  WorldStateMessageType.FINALIZE_BLOCKS,
@@ -279,7 +287,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
279
287
  * @param toBlockNumber The block number of the new oldest historical block
280
288
  * @returns The new WorldStateStatus
281
289
  */
282
- public async removeHistoricalBlocks(toBlockNumber: bigint) {
290
+ public async removeHistoricalBlocks(toBlockNumber: BlockNumber) {
283
291
  try {
284
292
  return await this.instance.call(
285
293
  WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
@@ -301,7 +309,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
301
309
  * @param toBlockNumber The block number of the new tip of the pending chain,
302
310
  * @returns The new WorldStateStatus
303
311
  */
304
- public async unwindBlocks(toBlockNumber: bigint) {
312
+ public async unwindBlocks(toBlockNumber: BlockNumber) {
305
313
  try {
306
314
  return await this.instance.call(
307
315
  WorldStateMessageType.UNWIND_BLOCKS,
@@ -36,6 +36,8 @@ export interface NativeWorldStateInstance {
36
36
  messageType: T,
37
37
  body: WorldStateRequest[T] & WorldStateRequestCategories,
38
38
  ): Promise<WorldStateResponse[T]>;
39
+ // TODO(dbanks12): this returns any type, but we should strongly type it
40
+ getHandle(): any;
39
41
  }
40
42
 
41
43
  /**
@@ -107,6 +109,32 @@ export class NativeWorldState implements NativeWorldStateInstance {
107
109
  );
108
110
  }
109
111
 
112
+ /**
113
+ * Gets the native WorldState handle from the underlying native instance.
114
+ * We call the getHandle() method on the native WorldState to get a NAPI External
115
+ * that wraps the underlying C++ WorldState pointer.
116
+ * @returns The NAPI External handle to the native WorldState instance,since
117
+ * the NAPI external type is opaque, we return any (we could also use an opaque symbol type)
118
+ */
119
+ public getHandle(): any {
120
+ const worldStateWrapper = (this.instance as any).dest;
121
+
122
+ if (!worldStateWrapper) {
123
+ throw new Error('No WorldStateWrapper found');
124
+ }
125
+
126
+ if (typeof worldStateWrapper.getHandle !== 'function') {
127
+ throw new Error('WorldStateWrapper does not have getHandle method');
128
+ }
129
+
130
+ // Call getHandle() to get the NAPI External
131
+ try {
132
+ return worldStateWrapper.getHandle();
133
+ } catch (error) {
134
+ this.log.error('Failed to get native WorldState handle', error);
135
+ }
136
+ }
137
+
110
138
  /**
111
139
  * Sends a message to the native instance and returns the response.
112
140
  * @param messageType - The type of message to send
@@ -1,13 +1,11 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import { SHA256Trunc } from '@aztec/foundation/crypto';
3
- import type { Fr } from '@aztec/foundation/fields';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import type { Fr } from '@aztec/foundation/curves/bn254';
4
3
  import { type Logger, createLogger } from '@aztec/foundation/log';
5
4
  import { promiseWithResolvers } from '@aztec/foundation/promise';
6
5
  import { elapsed } from '@aztec/foundation/timer';
7
- import { MerkleTreeCalculator } from '@aztec/foundation/trees';
8
6
  import type {
9
- L2Block,
10
7
  L2BlockId,
8
+ L2BlockNew,
11
9
  L2BlockSource,
12
10
  L2BlockStream,
13
11
  L2BlockStreamEvent,
@@ -45,17 +43,17 @@ export class ServerWorldStateSynchronizer
45
43
  {
46
44
  private readonly merkleTreeCommitted: MerkleTreeReadOperations;
47
45
 
48
- private latestBlockNumberAtStart = 0;
46
+ private latestBlockNumberAtStart = BlockNumber.ZERO;
49
47
  private historyToKeep: number | undefined;
50
48
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
51
- private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
49
+ private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
52
50
 
53
51
  private syncPromise = promiseWithResolvers<void>();
54
52
  protected blockStream: L2BlockStream | undefined;
55
53
 
56
54
  // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
57
55
  // store the proven block number here, in the synchronizer, so that we don't end up spamming the logs with 'chain-proved' events
58
- private provenBlockNumber: bigint | undefined;
56
+ private provenBlockNumber: BlockNumber | undefined;
59
57
 
60
58
  constructor(
61
59
  private readonly merkleTreeDb: MerkleTreeAdminDatabase,
@@ -77,11 +75,11 @@ export class ServerWorldStateSynchronizer
77
75
  return this.merkleTreeDb.getCommitted();
78
76
  }
79
77
 
80
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
78
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
81
79
  return this.merkleTreeDb.getSnapshot(blockNumber);
82
80
  }
83
81
 
84
- public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
82
+ public fork(blockNumber?: BlockNumber): Promise<MerkleTreeWriteOperations> {
85
83
  return this.merkleTreeDb.fork(blockNumber);
86
84
  }
87
85
 
@@ -102,9 +100,11 @@ export class ServerWorldStateSynchronizer
102
100
  }
103
101
 
104
102
  // Get the current latest block number
105
- this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly
106
- ? this.l2BlockSource.getProvenBlockNumber()
107
- : this.l2BlockSource.getBlockNumber());
103
+ this.latestBlockNumberAtStart = BlockNumber(
104
+ await (this.config.worldStateProvenBlocksOnly
105
+ ? this.l2BlockSource.getProvenBlockNumber()
106
+ : this.l2BlockSource.getBlockNumber()),
107
+ );
108
108
 
109
109
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
110
110
 
@@ -147,10 +147,10 @@ export class ServerWorldStateSynchronizer
147
147
  public async status(): Promise<WorldStateSynchronizerStatus> {
148
148
  const summary = await this.merkleTreeDb.getStatusSummary();
149
149
  const status: WorldStateSyncStatus = {
150
- latestBlockNumber: Number(summary.unfinalizedBlockNumber),
151
- latestBlockHash: (await this.getL2BlockHash(Number(summary.unfinalizedBlockNumber))) ?? '',
152
- finalizedBlockNumber: Number(summary.finalizedBlockNumber),
153
- oldestHistoricBlockNumber: Number(summary.oldestHistoricalBlock),
150
+ latestBlockNumber: summary.unfinalizedBlockNumber,
151
+ latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
152
+ finalizedBlockNumber: summary.finalizedBlockNumber,
153
+ oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
154
154
  treesAreSynched: summary.treesAreSynched,
155
155
  };
156
156
  return {
@@ -184,7 +184,10 @@ export class ServerWorldStateSynchronizer
184
184
  * @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
185
185
  * @returns A promise that resolves with the block number the world state was synced to
186
186
  */
187
- public async syncImmediate(targetBlockNumber?: number, skipThrowIfTargetNotReached?: boolean): Promise<number> {
187
+ public async syncImmediate(
188
+ targetBlockNumber?: BlockNumber,
189
+ skipThrowIfTargetNotReached?: boolean,
190
+ ): Promise<BlockNumber> {
188
191
  if (this.currentState !== WorldStateRunningState.RUNNING) {
189
192
  throw new Error(`World State is not running. Unable to perform sync.`);
190
193
  }
@@ -202,7 +205,7 @@ export class ServerWorldStateSynchronizer
202
205
 
203
206
  // If the archiver is behind the target block, force an archiver sync
204
207
  if (targetBlockNumber) {
205
- const archiverLatestBlock = await this.l2BlockSource.getBlockNumber();
208
+ const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
206
209
  if (archiverLatestBlock < targetBlockNumber) {
207
210
  this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
208
211
  await this.l2BlockSource.syncImmediate();
@@ -232,8 +235,8 @@ export class ServerWorldStateSynchronizer
232
235
  }
233
236
 
234
237
  /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
235
- public async getL2BlockHash(number: number): Promise<string | undefined> {
236
- if (number === 0) {
238
+ public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
239
+ if (number === BlockNumber.ZERO) {
237
240
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
238
241
  }
239
242
  if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
@@ -250,13 +253,13 @@ export class ServerWorldStateSynchronizer
250
253
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
251
254
  public async getL2Tips(): Promise<L2Tips> {
252
255
  const status = await this.merkleTreeDb.getStatusSummary();
253
- const unfinalizedBlockHash = await this.getL2BlockHash(Number(status.unfinalizedBlockNumber));
254
- const latestBlockId: L2BlockId = { number: Number(status.unfinalizedBlockNumber), hash: unfinalizedBlockHash! };
256
+ const unfinalizedBlockHash = await this.getL2BlockHash(status.unfinalizedBlockNumber);
257
+ const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
255
258
 
256
259
  return {
257
260
  latest: latestBlockId,
258
- finalized: { number: Number(status.finalizedBlockNumber), hash: '' },
259
- proven: { number: Number(this.provenBlockNumber ?? status.finalizedBlockNumber), hash: '' }, // TODO(palla/reorg): Using finalized as proven for now
261
+ finalized: { number: status.finalizedBlockNumber, hash: '' },
262
+ proven: { number: this.provenBlockNumber ?? status.finalizedBlockNumber, hash: '' }, // TODO(palla/reorg): Using finalized as proven for now
260
263
  };
261
264
  }
262
265
 
@@ -264,7 +267,7 @@ export class ServerWorldStateSynchronizer
264
267
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
265
268
  switch (event.type) {
266
269
  case 'blocks-added':
267
- await this.handleL2Blocks(event.blocks.map(b => b.block));
270
+ await this.handleL2Blocks(event.blocks.map(b => b.block.toL2Block()));
268
271
  break;
269
272
  case 'chain-pruned':
270
273
  await this.handleChainPruned(event.block.number);
@@ -283,22 +286,32 @@ export class ServerWorldStateSynchronizer
283
286
  * @param l2Blocks - The L2 blocks to handle.
284
287
  * @returns Whether the block handled was produced by this same node.
285
288
  */
286
- private async handleL2Blocks(l2Blocks: L2Block[]) {
289
+ private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
287
290
  this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
288
291
 
289
- const messagePromises = l2Blocks.map(block => this.l2BlockSource.getL1ToL2Messages(block.number));
290
- const l1ToL2Messages: Fr[][] = await Promise.all(messagePromises);
291
- let updateStatus: WorldStateStatusFull | undefined = undefined;
292
+ // Fetch the L1->L2 messages for the first block in a checkpoint.
293
+ const messagesForBlocks = new Map<BlockNumber, Fr[]>();
294
+ await Promise.all(
295
+ l2Blocks
296
+ .filter(b => b.indexWithinCheckpoint === 0)
297
+ .map(async block => {
298
+ const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
299
+ messagesForBlocks.set(block.number, l1ToL2Messages);
300
+ }),
301
+ );
292
302
 
293
- for (let i = 0; i < l2Blocks.length; i++) {
294
- const [duration, result] = await elapsed(() => this.handleL2Block(l2Blocks[i], l1ToL2Messages[i]));
295
- this.log.info(`World state updated with L2 block ${l2Blocks[i].number}`, {
303
+ let updateStatus: WorldStateStatusFull | undefined = undefined;
304
+ for (const block of l2Blocks) {
305
+ const [duration, result] = await elapsed(() =>
306
+ this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
307
+ );
308
+ this.log.info(`World state updated with L2 block ${block.number}`, {
296
309
  eventName: 'l2-block-handled',
297
310
  duration,
298
- unfinalizedBlockNumber: result.summary.unfinalizedBlockNumber,
299
- finalizedBlockNumber: result.summary.finalizedBlockNumber,
300
- oldestHistoricBlock: result.summary.oldestHistoricalBlock,
301
- ...l2Blocks[i].getStats(),
311
+ unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
312
+ finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
313
+ oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
314
+ ...block.getStats(),
302
315
  } satisfies L2BlockHandledStats);
303
316
  updateStatus = result;
304
317
  }
@@ -314,14 +327,7 @@ export class ServerWorldStateSynchronizer
314
327
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
315
328
  * @returns Whether the block handled was produced by this same node.
316
329
  */
317
- private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
318
- // First we check that the L1 to L2 messages hash to the block inHash.
319
- // Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
320
- // to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
321
- // tree uses pedersen and we don't have access to the converted root.
322
- await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
323
-
324
- // If the above check succeeds, we can proceed to handle the block.
330
+ private async handleL2Block(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
325
331
  this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
326
332
  blockNumber: l2Block.number,
327
333
  blockHash: await l2Block.hash().then(h => h.toString()),
@@ -337,30 +343,30 @@ export class ServerWorldStateSynchronizer
337
343
  return result;
338
344
  }
339
345
 
340
- private async handleChainFinalized(blockNumber: number) {
346
+ private async handleChainFinalized(blockNumber: BlockNumber) {
341
347
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
342
- const summary = await this.merkleTreeDb.setFinalized(BigInt(blockNumber));
348
+ const summary = await this.merkleTreeDb.setFinalized(blockNumber);
343
349
  if (this.historyToKeep === undefined) {
344
350
  return;
345
351
  }
346
- const newHistoricBlock = summary.finalizedBlockNumber - BigInt(this.historyToKeep) + 1n;
352
+ const newHistoricBlock = summary.finalizedBlockNumber - this.historyToKeep + 1;
347
353
  if (newHistoricBlock <= 1) {
348
354
  return;
349
355
  }
350
356
  this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
351
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
357
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock));
352
358
  this.log.debug(`World state summary `, status.summary);
353
359
  }
354
360
 
355
- private handleChainProven(blockNumber: number) {
356
- this.provenBlockNumber = BigInt(blockNumber);
361
+ private handleChainProven(blockNumber: BlockNumber) {
362
+ this.provenBlockNumber = blockNumber;
357
363
  this.log.debug(`Proven chain is now at block ${blockNumber}`);
358
364
  return Promise.resolve();
359
365
  }
360
366
 
361
- private async handleChainPruned(blockNumber: number) {
367
+ private async handleChainPruned(blockNumber: BlockNumber) {
362
368
  this.log.warn(`Chain pruned to block ${blockNumber}`);
363
- const status = await this.merkleTreeDb.unwindBlocks(BigInt(blockNumber));
369
+ const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
364
370
  this.latestBlockHashQuery = undefined;
365
371
  this.provenBlockNumber = undefined;
366
372
  this.instrumentation.updateWorldStateMetrics(status);
@@ -374,24 +380,4 @@ export class ServerWorldStateSynchronizer
374
380
  this.currentState = newState;
375
381
  this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
376
382
  }
377
-
378
- /**
379
- * Verifies that the L1 to L2 messages hash to the block inHash.
380
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
381
- * @param inHash - The inHash of the block.
382
- * @throws If the L1 to L2 messages do not hash to the block inHash.
383
- */
384
- protected async verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Fr) {
385
- const treeCalculator = await MerkleTreeCalculator.create(
386
- L1_TO_L2_MSG_SUBTREE_HEIGHT,
387
- Buffer.alloc(32),
388
- (lhs, rhs) => Promise.resolve(new SHA256Trunc().hash(lhs, rhs)),
389
- );
390
-
391
- const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
392
-
393
- if (!root.equals(inHash.toBuffer())) {
394
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
395
- }
396
- }
397
383
  }