@aztec/world-state 0.0.0-test.1 → 0.0.1-commit.017a351

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 (69) hide show
  1. package/dest/index.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts +6 -4
  3. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  4. package/dest/instrumentation/instrumentation.js +25 -41
  5. package/dest/native/bench_metrics.d.ts +23 -0
  6. package/dest/native/bench_metrics.d.ts.map +1 -0
  7. package/dest/native/bench_metrics.js +81 -0
  8. package/dest/native/fork_checkpoint.d.ts +7 -1
  9. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  10. package/dest/native/fork_checkpoint.js +15 -3
  11. package/dest/native/index.d.ts +1 -1
  12. package/dest/native/ipc_world_state_instance.d.ts +45 -0
  13. package/dest/native/ipc_world_state_instance.d.ts.map +1 -0
  14. package/dest/native/ipc_world_state_instance.js +750 -0
  15. package/dest/native/merkle_trees_facade.d.ts +25 -9
  16. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  17. package/dest/native/merkle_trees_facade.js +109 -25
  18. package/dest/native/message.d.ts +87 -58
  19. package/dest/native/message.d.ts.map +1 -1
  20. package/dest/native/message.js +61 -61
  21. package/dest/native/native_world_state.d.ts +41 -21
  22. package/dest/native/native_world_state.d.ts.map +1 -1
  23. package/dest/native/native_world_state.js +177 -50
  24. package/dest/native/native_world_state_instance.d.ts +23 -25
  25. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  26. package/dest/native/native_world_state_instance.js +8 -163
  27. package/dest/native/world_state_ops_queue.d.ts +1 -1
  28. package/dest/native/world_state_ops_queue.d.ts.map +1 -1
  29. package/dest/native/world_state_ops_queue.js +6 -6
  30. package/dest/synchronizer/config.d.ts +14 -6
  31. package/dest/synchronizer/config.d.ts.map +1 -1
  32. package/dest/synchronizer/config.js +36 -14
  33. package/dest/synchronizer/errors.d.ts +4 -0
  34. package/dest/synchronizer/errors.d.ts.map +1 -0
  35. package/dest/synchronizer/errors.js +5 -0
  36. package/dest/synchronizer/factory.d.ts +13 -5
  37. package/dest/synchronizer/factory.d.ts.map +1 -1
  38. package/dest/synchronizer/factory.js +15 -9
  39. package/dest/synchronizer/index.d.ts +1 -1
  40. package/dest/synchronizer/server_world_state_synchronizer.d.ts +21 -31
  41. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  42. package/dest/synchronizer/server_world_state_synchronizer.js +220 -96
  43. package/dest/test/index.d.ts +1 -1
  44. package/dest/test/utils.d.ts +12 -5
  45. package/dest/test/utils.d.ts.map +1 -1
  46. package/dest/test/utils.js +54 -47
  47. package/dest/testing.d.ts +6 -5
  48. package/dest/testing.d.ts.map +1 -1
  49. package/dest/testing.js +15 -15
  50. package/dest/world-state-db/index.d.ts +1 -1
  51. package/dest/world-state-db/merkle_tree_db.d.ts +12 -18
  52. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  53. package/package.json +27 -25
  54. package/src/instrumentation/instrumentation.ts +31 -43
  55. package/src/native/bench_metrics.ts +91 -0
  56. package/src/native/fork_checkpoint.ts +19 -3
  57. package/src/native/ipc_world_state_instance.ts +863 -0
  58. package/src/native/merkle_trees_facade.ts +127 -33
  59. package/src/native/message.ts +109 -80
  60. package/src/native/native_world_state.ts +207 -64
  61. package/src/native/native_world_state_instance.ts +28 -223
  62. package/src/native/world_state_ops_queue.ts +6 -6
  63. package/src/synchronizer/config.ts +58 -20
  64. package/src/synchronizer/errors.ts +5 -0
  65. package/src/synchronizer/factory.ts +48 -18
  66. package/src/synchronizer/server_world_state_synchronizer.ts +249 -121
  67. package/src/test/utils.ts +92 -82
  68. package/src/testing.ts +11 -16
  69. package/src/world-state-db/merkle_tree_db.ts +16 -18
@@ -1,19 +1,20 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/fields';
3
- import { createLogger } from '@aztec/foundation/log';
1
+ import { INITIAL_CHECKPOINT_NUMBER } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import type { Fr } from '@aztec/foundation/curves/bn254';
4
+ import { type Logger, createLogger } from '@aztec/foundation/log';
4
5
  import { promiseWithResolvers } from '@aztec/foundation/promise';
5
6
  import { elapsed } from '@aztec/foundation/timer';
6
- import { MerkleTreeCalculator } from '@aztec/foundation/trees';
7
- import { SHA256Trunc } from '@aztec/merkle-tree';
8
- import type {
9
- L2Block,
10
- L2BlockId,
11
- L2BlockSource,
7
+ import {
8
+ type BlockHash,
9
+ GENESIS_CHECKPOINT_HEADER_HASH,
10
+ type L2Block,
11
+ type L2BlockId,
12
+ type L2BlockSource,
12
13
  L2BlockStream,
13
- L2BlockStreamEvent,
14
- L2BlockStreamEventHandler,
15
- L2BlockStreamLocalDataProvider,
16
- L2Tips,
14
+ type L2BlockStreamEvent,
15
+ type L2BlockStreamEventHandler,
16
+ type L2BlockStreamLocalDataProvider,
17
+ type L2Tips,
17
18
  } from '@aztec/stdlib/block';
18
19
  import {
19
20
  WorldStateRunningState,
@@ -22,14 +23,18 @@ import {
22
23
  type WorldStateSynchronizerStatus,
23
24
  } from '@aztec/stdlib/interfaces/server';
24
25
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
26
+ import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
25
27
  import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
26
28
  import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
27
- import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
29
+ import { getTelemetryClient } from '@aztec/telemetry-client';
28
30
 
29
31
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
30
32
  import type { WorldStateStatusFull } from '../native/message.js';
31
33
  import type { MerkleTreeAdminDatabase } from '../world-state-db/merkle_tree_db.js';
32
34
  import type { WorldStateConfig } from './config.js';
35
+ import { WorldStateSynchronizerError } from './errors.js';
36
+
37
+ export type { SnapshotDataKeys };
33
38
 
34
39
  /**
35
40
  * Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
@@ -41,23 +46,26 @@ export class ServerWorldStateSynchronizer
41
46
  {
42
47
  private readonly merkleTreeCommitted: MerkleTreeReadOperations;
43
48
 
44
- private latestBlockNumberAtStart = 0;
49
+ private latestBlockNumberAtStart = BlockNumber.ZERO;
45
50
  private historyToKeep: number | undefined;
46
51
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
47
- private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
48
52
 
49
53
  private syncPromise = promiseWithResolvers<void>();
50
54
  protected blockStream: L2BlockStream | undefined;
51
55
 
56
+ // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
57
+ // 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: BlockNumber | undefined;
59
+
52
60
  constructor(
53
61
  private readonly merkleTreeDb: MerkleTreeAdminDatabase,
54
62
  private readonly l2BlockSource: L2BlockSource & L1ToL2MessageSource,
55
63
  private readonly config: WorldStateConfig,
56
64
  private instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
57
- private readonly log = createLogger('world_state'),
65
+ private readonly log: Logger = createLogger('world_state'),
58
66
  ) {
59
67
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
60
- this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
68
+ this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
61
69
  this.log.info(
62
70
  `Created world state synchroniser with block history of ${
63
71
  this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
@@ -69,12 +77,20 @@ export class ServerWorldStateSynchronizer
69
77
  return this.merkleTreeDb.getCommitted();
70
78
  }
71
79
 
72
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
80
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
73
81
  return this.merkleTreeDb.getSnapshot(blockNumber);
74
82
  }
75
83
 
76
- public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
77
- return this.merkleTreeDb.fork(blockNumber);
84
+ public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
85
+ return this.merkleTreeDb.fork(blockNumber, opts);
86
+ }
87
+
88
+ public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
89
+ return this.merkleTreeDb.backupTo(dstPath, compact);
90
+ }
91
+
92
+ public clear(): Promise<void> {
93
+ return this.merkleTreeDb.clear();
78
94
  }
79
95
 
80
96
  public async start() {
@@ -86,9 +102,7 @@ export class ServerWorldStateSynchronizer
86
102
  }
87
103
 
88
104
  // Get the current latest block number
89
- this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly
90
- ? this.l2BlockSource.getProvenBlockNumber()
91
- : this.l2BlockSource.getBlockNumber());
105
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
92
106
 
93
107
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
94
108
 
@@ -110,12 +124,11 @@ export class ServerWorldStateSynchronizer
110
124
  }
111
125
 
112
126
  protected createBlockStream(): L2BlockStream {
113
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
114
127
  const logger = createLogger('world-state:block_stream');
115
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
116
- proven: this.config.worldStateProvenBlocksOnly,
128
+ return new L2BlockStream(this.l2BlockSource, this, this, logger, {
117
129
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
118
130
  batchSize: this.config.worldStateBlockRequestBatchSize,
131
+ ignoreCheckpoints: true,
119
132
  });
120
133
  }
121
134
 
@@ -131,10 +144,10 @@ export class ServerWorldStateSynchronizer
131
144
  public async status(): Promise<WorldStateSynchronizerStatus> {
132
145
  const summary = await this.merkleTreeDb.getStatusSummary();
133
146
  const status: WorldStateSyncStatus = {
134
- latestBlockNumber: Number(summary.unfinalisedBlockNumber),
135
- latestBlockHash: (await this.getL2BlockHash(Number(summary.unfinalisedBlockNumber))) ?? '',
136
- finalisedBlockNumber: Number(summary.finalisedBlockNumber),
137
- oldestHistoricBlockNumber: Number(summary.oldestHistoricalBlock),
147
+ latestBlockNumber: summary.unfinalizedBlockNumber,
148
+ latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
149
+ finalizedBlockNumber: summary.finalizedBlockNumber,
150
+ oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
138
151
  treesAreSynched: summary.treesAreSynched,
139
152
  };
140
153
  return {
@@ -144,86 +157,173 @@ export class ServerWorldStateSynchronizer
144
157
  }
145
158
 
146
159
  public async getLatestBlockNumber() {
147
- return (await this.getL2Tips()).latest.number;
160
+ return (await this.getL2Tips()).proposed.number;
161
+ }
162
+
163
+ public async stopSync() {
164
+ this.log.debug('Stopping sync...');
165
+ await this.blockStream?.stop();
166
+ this.log.info('Stopped sync');
167
+ }
168
+
169
+ public resumeSync() {
170
+ if (!this.blockStream) {
171
+ throw new Error('Cannot resume sync as block stream is not initialized');
172
+ }
173
+ this.log.debug('Resuming sync...');
174
+ this.blockStream.start();
175
+ this.log.info('Resumed sync');
148
176
  }
149
177
 
150
178
  /**
151
179
  * Forces an immediate sync.
152
- * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it. Throws if cannot be reached.
180
+ * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
181
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
153
182
  * @returns A promise that resolves with the block number the world state was synced to
154
183
  */
155
- public async syncImmediate(targetBlockNumber?: number): Promise<number> {
156
- if (this.currentState !== WorldStateRunningState.RUNNING || this.blockStream === undefined) {
184
+ public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
185
+ if (this.currentState !== WorldStateRunningState.RUNNING) {
157
186
  throw new Error(`World State is not running. Unable to perform sync.`);
158
187
  }
159
188
 
189
+ if (this.blockStream === undefined) {
190
+ throw new Error('Block stream is not initialized. Unable to perform sync.');
191
+ }
192
+
160
193
  // If we have been given a block number to sync to and we have reached that number then return
161
194
  const currentBlockNumber = await this.getLatestBlockNumber();
162
195
  if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
163
- return currentBlockNumber;
196
+ if (blockHash === undefined) {
197
+ return currentBlockNumber;
198
+ }
199
+
200
+ // If a block hash was provided, verify we're on the expected fork
201
+ const currentHash = await this.getL2BlockHash(targetBlockNumber);
202
+ if (currentHash === blockHash.toString()) {
203
+ return currentBlockNumber;
204
+ }
205
+ // Hash mismatch: a reorg may have occurred, fall through to trigger sync
206
+ this.log.debug(
207
+ `World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`,
208
+ );
164
209
  }
165
210
  this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
166
211
 
212
+ // If the archiver is behind the target block, force an archiver sync
213
+ if (targetBlockNumber) {
214
+ const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
215
+ if (archiverLatestBlock < targetBlockNumber) {
216
+ this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
217
+ await this.l2BlockSource.syncImmediate();
218
+ }
219
+ }
220
+
167
221
  // Force the block stream to sync against the archiver now
168
222
  await this.blockStream.sync();
169
223
 
170
224
  // If we have been given a block number to sync to and we have not reached that number then fail
171
225
  const updatedBlockNumber = await this.getLatestBlockNumber();
172
226
  if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
173
- throw new Error(`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`);
227
+ throw new WorldStateSynchronizerError(
228
+ `Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
229
+ {
230
+ cause: {
231
+ reason: 'block_not_available',
232
+ previousBlockNumber: currentBlockNumber,
233
+ updatedBlockNumber,
234
+ targetBlockNumber,
235
+ },
236
+ },
237
+ );
238
+ }
239
+
240
+ // If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
241
+ if (blockHash !== undefined && targetBlockNumber !== undefined) {
242
+ const updatedHash = await this.getL2BlockHash(targetBlockNumber);
243
+ if (updatedHash !== blockHash.toString()) {
244
+ throw new WorldStateSynchronizerError(
245
+ `Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`,
246
+ {
247
+ cause: {
248
+ reason: 'block_hash_mismatch',
249
+ targetBlockNumber,
250
+ expectedHash: blockHash.toString(),
251
+ actualHash: updatedHash,
252
+ },
253
+ },
254
+ );
255
+ }
174
256
  }
175
257
 
176
258
  return updatedBlockNumber;
177
259
  }
178
260
 
179
261
  /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
180
- public async getL2BlockHash(number: number): Promise<string | undefined> {
181
- if (number === 0) {
262
+ public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
263
+ if (number === BlockNumber.ZERO) {
182
264
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
183
265
  }
184
- if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
185
- this.latestBlockHashQuery = {
186
- hash: await this.merkleTreeCommitted
187
- .getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
188
- .then(leaf => leaf?.toString()),
189
- blockNumber: number,
190
- };
191
- }
192
- return this.latestBlockHashQuery.hash;
266
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
193
267
  }
194
268
 
195
269
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
196
270
  public async getL2Tips(): Promise<L2Tips> {
197
271
  const status = await this.merkleTreeDb.getStatusSummary();
198
- const unfinalisedBlockHash = await this.getL2BlockHash(Number(status.unfinalisedBlockNumber));
199
- const latestBlockId: L2BlockId = { number: Number(status.unfinalisedBlockNumber), hash: unfinalisedBlockHash! };
200
-
272
+ const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
273
+ const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
274
+
275
+ const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
276
+ const provenBlockHashPromise =
277
+ this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
278
+
279
+ const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
280
+ unfinalizedBlockHashPromise,
281
+ finalizedBlockHashPromise,
282
+ provenBlockHashPromise,
283
+ ]);
284
+ const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
285
+
286
+ // World state doesn't track checkpointed blocks or checkpoints themselves.
287
+ // but we use a block stream so we need to provide 'local' L2Tips.
288
+ // We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
289
+ const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
290
+ const initialBlockHash = (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
201
291
  return {
202
- latest: latestBlockId,
203
- finalized: { number: Number(status.finalisedBlockNumber), hash: '' },
204
- proven: { number: Number(status.finalisedBlockNumber), hash: '' }, // TODO(palla/reorg): Using finalised as proven for now
292
+ proposed: latestBlockId,
293
+ checkpointed: {
294
+ block: { number: BlockNumber.ZERO, hash: initialBlockHash },
295
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
296
+ },
297
+ proposedCheckpoint: {
298
+ block: { number: BlockNumber.ZERO, hash: initialBlockHash },
299
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
300
+ },
301
+ finalized: {
302
+ block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
303
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
304
+ },
305
+ proven: {
306
+ block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
307
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
308
+ },
205
309
  };
206
310
  }
207
311
 
208
312
  /** Handles an event emitted by the block stream. */
209
313
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
210
- try {
211
- switch (event.type) {
212
- case 'blocks-added':
213
- await this.handleL2Blocks(event.blocks);
214
- break;
215
- case 'chain-pruned':
216
- await this.handleChainPruned(event.blockNumber);
217
- break;
218
- case 'chain-proven':
219
- await this.handleChainProven(event.blockNumber);
220
- break;
221
- case 'chain-finalized':
222
- await this.handleChainFinalized(event.blockNumber);
223
- break;
224
- }
225
- } catch (err) {
226
- this.log.error('Error processing block stream', err);
314
+ switch (event.type) {
315
+ case 'blocks-added':
316
+ await this.handleL2Blocks(event.blocks);
317
+ break;
318
+ case 'chain-pruned':
319
+ await this.handleChainPruned(event.block.number);
320
+ break;
321
+ case 'chain-proven':
322
+ await this.handleChainProven(event.block.number);
323
+ break;
324
+ case 'chain-finalized':
325
+ await this.handleChainFinalized(event.block.number);
326
+ break;
227
327
  }
228
328
  }
229
329
 
@@ -233,21 +333,31 @@ export class ServerWorldStateSynchronizer
233
333
  * @returns Whether the block handled was produced by this same node.
234
334
  */
235
335
  private async handleL2Blocks(l2Blocks: L2Block[]) {
236
- this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
336
+ this.log.debug(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
337
+
338
+ // Fetch the L1->L2 messages for the first block in a checkpoint.
339
+ const messagesForBlocks = new Map<BlockNumber, Fr[]>();
340
+ await Promise.all(
341
+ l2Blocks
342
+ .filter(b => b.indexWithinCheckpoint === 0)
343
+ .map(async block => {
344
+ const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
345
+ messagesForBlocks.set(block.number, l1ToL2Messages);
346
+ }),
347
+ );
237
348
 
238
- const messagePromises = l2Blocks.map(block => this.l2BlockSource.getL1ToL2Messages(BigInt(block.number)));
239
- const l1ToL2Messages: Fr[][] = await Promise.all(messagePromises);
240
349
  let updateStatus: WorldStateStatusFull | undefined = undefined;
241
-
242
- for (let i = 0; i < l2Blocks.length; i++) {
243
- const [duration, result] = await elapsed(() => this.handleL2Block(l2Blocks[i], l1ToL2Messages[i]));
244
- this.log.verbose(`World state updated with L2 block ${l2Blocks[i].number}`, {
350
+ for (const block of l2Blocks) {
351
+ const [duration, result] = await elapsed(() =>
352
+ this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
353
+ );
354
+ this.log.info(`World state updated with L2 block ${block.number}`, {
245
355
  eventName: 'l2-block-handled',
246
356
  duration,
247
- unfinalisedBlockNumber: result.summary.unfinalisedBlockNumber,
248
- finalisedBlockNumber: result.summary.finalisedBlockNumber,
249
- oldestHistoricBlock: result.summary.oldestHistoricalBlock,
250
- ...l2Blocks[i].getStats(),
357
+ unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
358
+ finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
359
+ oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
360
+ ...block.getStats(),
251
361
  } satisfies L2BlockHandledStats);
252
362
  updateStatus = result;
253
363
  }
@@ -264,17 +374,12 @@ export class ServerWorldStateSynchronizer
264
374
  * @returns Whether the block handled was produced by this same node.
265
375
  */
266
376
  private async handleL2Block(l2Block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
267
- // First we check that the L1 to L2 messages hash to the block inHash.
268
- // Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
269
- // to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
270
- // tree uses pedersen and we don't have access to the converted root.
271
- await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
272
-
273
- // If the above check succeeds, we can proceed to handle the block.
274
- this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
377
+ this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
275
378
  blockNumber: l2Block.number,
276
379
  blockHash: await l2Block.hash().then(h => h.toString()),
277
380
  l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
381
+ blockHeader: l2Block.header.toInspect(),
382
+ blockStats: l2Block.getStats(),
278
383
  });
279
384
  const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
280
385
 
@@ -286,30 +391,73 @@ export class ServerWorldStateSynchronizer
286
391
  return result;
287
392
  }
288
393
 
289
- private async handleChainFinalized(blockNumber: number) {
394
+ private async handleChainFinalized(blockNumber: BlockNumber) {
290
395
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
291
- const summary = await this.merkleTreeDb.setFinalised(BigInt(blockNumber));
396
+ // If the finalized block number is older than the oldest available block in world state,
397
+ // skip entirely. The finalized block number can jump backwards (e.g. when the finalization
398
+ // heuristic changes) and try to read block data that has already been pruned. When this
399
+ // happens, there is nothing useful to do — the native world state is already finalized
400
+ // past this point and pruning has already happened.
401
+ const currentSummary = await this.merkleTreeDb.getStatusSummary();
402
+ if (blockNumber < currentSummary.oldestHistoricalBlock || blockNumber < 1) {
403
+ this.log.trace(
404
+ `Finalized block ${blockNumber} is older than the oldest available block ${currentSummary.oldestHistoricalBlock}. Skipping.`,
405
+ );
406
+ return;
407
+ }
408
+ const summary = await this.merkleTreeDb.setFinalized(blockNumber);
292
409
  if (this.historyToKeep === undefined) {
293
410
  return;
294
411
  }
295
- const newHistoricBlock = summary.finalisedBlockNumber - BigInt(this.historyToKeep) + 1n;
296
- if (newHistoricBlock <= 1) {
412
+ const finalisedBlockData = await this.l2BlockSource.getBlockData({ number: summary.finalizedBlockNumber });
413
+ if (finalisedBlockData === undefined) {
414
+ this.log.warn(
415
+ `Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`,
416
+ );
417
+ return;
418
+ }
419
+ // Compute the required historic checkpoint number
420
+ const newHistoricCheckpointNumber = finalisedBlockData.checkpointNumber - this.historyToKeep + 1;
421
+ if (newHistoricCheckpointNumber <= 1) {
422
+ return;
423
+ }
424
+ // Retrieve the historic checkpoint
425
+ const historicCheckpoint = await this.l2BlockSource.getCheckpoint({
426
+ number: CheckpointNumber(newHistoricCheckpointNumber),
427
+ });
428
+ if (!historicCheckpoint) {
429
+ this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
430
+ return;
431
+ }
432
+ if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
433
+ this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
297
434
  return;
298
435
  }
299
- this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
300
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
436
+ // Find the block at the start of the checkpoint and remove blocks up to this one
437
+ const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
438
+ if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
439
+ this.log.debug(
440
+ `Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`,
441
+ );
442
+ return;
443
+ }
444
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
445
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
301
446
  this.log.debug(`World state summary `, status.summary);
302
447
  }
303
448
 
304
- private handleChainProven(blockNumber: number) {
449
+ private handleChainProven(blockNumber: BlockNumber) {
450
+ this.provenBlockNumber = blockNumber;
305
451
  this.log.debug(`Proven chain is now at block ${blockNumber}`);
306
452
  return Promise.resolve();
307
453
  }
308
454
 
309
- private async handleChainPruned(blockNumber: number) {
310
- this.log.warn(`Chain pruned to block ${blockNumber}`);
311
- const status = await this.merkleTreeDb.unwindBlocks(BigInt(blockNumber));
312
- this.latestBlockHashQuery = undefined;
455
+ private async handleChainPruned(blockNumber: BlockNumber) {
456
+ this.log.info(`Chain pruned to block ${blockNumber}`);
457
+ const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
458
+ if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
459
+ this.provenBlockNumber = undefined;
460
+ }
313
461
  this.instrumentation.updateWorldStateMetrics(status);
314
462
  }
315
463
 
@@ -321,24 +469,4 @@ export class ServerWorldStateSynchronizer
321
469
  this.currentState = newState;
322
470
  this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
323
471
  }
324
-
325
- /**
326
- * Verifies that the L1 to L2 messages hash to the block inHash.
327
- * @param l1ToL2Messages - The L1 to L2 messages for the block.
328
- * @param inHash - The inHash of the block.
329
- * @throws If the L1 to L2 messages do not hash to the block inHash.
330
- */
331
- protected async verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Buffer) {
332
- const treeCalculator = await MerkleTreeCalculator.create(
333
- L1_TO_L2_MSG_SUBTREE_HEIGHT,
334
- Buffer.alloc(32),
335
- (lhs, rhs) => Promise.resolve(new SHA256Trunc().hash(lhs, rhs)),
336
- );
337
-
338
- const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
339
-
340
- if (!root.equals(inHash)) {
341
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
342
- }
343
- }
344
472
  }