@aztec/world-state 0.0.1-commit.9b94fc1 → 0.0.1-commit.9ee6fcc6

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 (48) hide show
  1. package/dest/instrumentation/instrumentation.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  3. package/dest/instrumentation/instrumentation.js +17 -41
  4. package/dest/native/fork_checkpoint.d.ts +7 -1
  5. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  6. package/dest/native/fork_checkpoint.js +15 -3
  7. package/dest/native/merkle_trees_facade.d.ts +13 -7
  8. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  9. package/dest/native/merkle_trees_facade.js +45 -12
  10. package/dest/native/message.d.ts +25 -15
  11. package/dest/native/message.d.ts.map +1 -1
  12. package/dest/native/message.js +14 -13
  13. package/dest/native/native_world_state.d.ts +16 -13
  14. package/dest/native/native_world_state.d.ts.map +1 -1
  15. package/dest/native/native_world_state.js +22 -17
  16. package/dest/native/native_world_state_instance.d.ts +3 -3
  17. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  18. package/dest/native/native_world_state_instance.js +4 -4
  19. package/dest/synchronizer/config.d.ts +3 -5
  20. package/dest/synchronizer/config.d.ts.map +1 -1
  21. package/dest/synchronizer/config.js +7 -9
  22. package/dest/synchronizer/factory.d.ts +5 -4
  23. package/dest/synchronizer/factory.d.ts.map +1 -1
  24. package/dest/synchronizer/factory.js +5 -5
  25. package/dest/synchronizer/server_world_state_synchronizer.d.ts +11 -17
  26. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  27. package/dest/synchronizer/server_world_state_synchronizer.js +149 -80
  28. package/dest/test/utils.d.ts +12 -5
  29. package/dest/test/utils.d.ts.map +1 -1
  30. package/dest/test/utils.js +54 -50
  31. package/dest/testing.d.ts +2 -2
  32. package/dest/testing.d.ts.map +1 -1
  33. package/dest/testing.js +1 -1
  34. package/dest/world-state-db/merkle_tree_db.d.ts +10 -20
  35. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  36. package/package.json +12 -13
  37. package/src/instrumentation/instrumentation.ts +17 -41
  38. package/src/native/fork_checkpoint.ts +19 -3
  39. package/src/native/merkle_trees_facade.ts +52 -11
  40. package/src/native/message.ts +38 -26
  41. package/src/native/native_world_state.ts +52 -28
  42. package/src/native/native_world_state_instance.ts +6 -4
  43. package/src/synchronizer/config.ts +8 -19
  44. package/src/synchronizer/factory.ts +8 -2
  45. package/src/synchronizer/server_world_state_synchronizer.ts +176 -105
  46. package/src/test/utils.ts +86 -91
  47. package/src/testing.ts +1 -1
  48. package/src/world-state-db/merkle_tree_db.ts +13 -24
@@ -1,19 +1,20 @@
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 { GENESIS_BLOCK_HEADER_HASH, INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import type { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import { type Logger, createLogger } from '@aztec/foundation/log';
5
5
  import { promiseWithResolvers } from '@aztec/foundation/promise';
6
6
  import { elapsed } from '@aztec/foundation/timer';
7
- import { MerkleTreeCalculator } from '@aztec/foundation/trees';
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,
@@ -25,7 +26,7 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
25
26
  import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
26
27
  import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
27
28
  import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
28
- import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
29
+ import { getTelemetryClient } from '@aztec/telemetry-client';
29
30
 
30
31
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
31
32
  import type { WorldStateStatusFull } from '../native/message.js';
@@ -45,17 +46,16 @@ export class ServerWorldStateSynchronizer
45
46
  {
46
47
  private readonly merkleTreeCommitted: MerkleTreeReadOperations;
47
48
 
48
- private latestBlockNumberAtStart = 0;
49
+ private latestBlockNumberAtStart = BlockNumber.ZERO;
49
50
  private historyToKeep: number | undefined;
50
51
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
51
- private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
52
52
 
53
53
  private syncPromise = promiseWithResolvers<void>();
54
54
  protected blockStream: L2BlockStream | undefined;
55
55
 
56
56
  // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
57
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: bigint | undefined;
58
+ private provenBlockNumber: BlockNumber | undefined;
59
59
 
60
60
  constructor(
61
61
  private readonly merkleTreeDb: MerkleTreeAdminDatabase,
@@ -65,7 +65,7 @@ export class ServerWorldStateSynchronizer
65
65
  private readonly log: Logger = createLogger('world_state'),
66
66
  ) {
67
67
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
68
- this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
68
+ this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
69
69
  this.log.info(
70
70
  `Created world state synchroniser with block history of ${
71
71
  this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
@@ -77,12 +77,12 @@ export class ServerWorldStateSynchronizer
77
77
  return this.merkleTreeDb.getCommitted();
78
78
  }
79
79
 
80
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
80
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
81
81
  return this.merkleTreeDb.getSnapshot(blockNumber);
82
82
  }
83
83
 
84
- public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
85
- return this.merkleTreeDb.fork(blockNumber);
84
+ public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
85
+ return this.merkleTreeDb.fork(blockNumber, opts);
86
86
  }
87
87
 
88
88
  public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
@@ -102,9 +102,7 @@ export class ServerWorldStateSynchronizer
102
102
  }
103
103
 
104
104
  // Get the current latest block number
105
- this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly
106
- ? this.l2BlockSource.getProvenBlockNumber()
107
- : this.l2BlockSource.getBlockNumber());
105
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
108
106
 
109
107
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
110
108
 
@@ -126,12 +124,11 @@ export class ServerWorldStateSynchronizer
126
124
  }
127
125
 
128
126
  protected createBlockStream(): L2BlockStream {
129
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
130
127
  const logger = createLogger('world-state:block_stream');
131
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
132
- proven: this.config.worldStateProvenBlocksOnly,
128
+ return new L2BlockStream(this.l2BlockSource, this, this, logger, {
133
129
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
134
130
  batchSize: this.config.worldStateBlockRequestBatchSize,
131
+ ignoreCheckpoints: true,
135
132
  });
136
133
  }
137
134
 
@@ -147,10 +144,10 @@ export class ServerWorldStateSynchronizer
147
144
  public async status(): Promise<WorldStateSynchronizerStatus> {
148
145
  const summary = await this.merkleTreeDb.getStatusSummary();
149
146
  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),
147
+ latestBlockNumber: summary.unfinalizedBlockNumber,
148
+ latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
149
+ finalizedBlockNumber: summary.finalizedBlockNumber,
150
+ oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
154
151
  treesAreSynched: summary.treesAreSynched,
155
152
  };
156
153
  return {
@@ -160,7 +157,7 @@ export class ServerWorldStateSynchronizer
160
157
  }
161
158
 
162
159
  public async getLatestBlockNumber() {
163
- return (await this.getL2Tips()).latest.number;
160
+ return (await this.getL2Tips()).proposed.number;
164
161
  }
165
162
 
166
163
  public async stopSync() {
@@ -181,10 +178,10 @@ export class ServerWorldStateSynchronizer
181
178
  /**
182
179
  * Forces an immediate sync.
183
180
  * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
184
- * @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
181
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
185
182
  * @returns A promise that resolves with the block number the world state was synced to
186
183
  */
187
- public async syncImmediate(targetBlockNumber?: number, skipThrowIfTargetNotReached?: boolean): Promise<number> {
184
+ public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
188
185
  if (this.currentState !== WorldStateRunningState.RUNNING) {
189
186
  throw new Error(`World State is not running. Unable to perform sync.`);
190
187
  }
@@ -196,13 +193,25 @@ export class ServerWorldStateSynchronizer
196
193
  // If we have been given a block number to sync to and we have reached that number then return
197
194
  const currentBlockNumber = await this.getLatestBlockNumber();
198
195
  if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
199
- 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
+ );
200
209
  }
201
210
  this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
202
211
 
203
212
  // If the archiver is behind the target block, force an archiver sync
204
213
  if (targetBlockNumber) {
205
- const archiverLatestBlock = await this.l2BlockSource.getBlockNumber();
214
+ const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
206
215
  if (archiverLatestBlock < targetBlockNumber) {
207
216
  this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
208
217
  await this.l2BlockSource.syncImmediate();
@@ -214,7 +223,7 @@ export class ServerWorldStateSynchronizer
214
223
 
215
224
  // If we have been given a block number to sync to and we have not reached that number then fail
216
225
  const updatedBlockNumber = await this.getLatestBlockNumber();
217
- if (!skipThrowIfTargetNotReached && targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
226
+ if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
218
227
  throw new WorldStateSynchronizerError(
219
228
  `Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
220
229
  {
@@ -228,35 +237,70 @@ export class ServerWorldStateSynchronizer
228
237
  );
229
238
  }
230
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
+ }
256
+ }
257
+
231
258
  return updatedBlockNumber;
232
259
  }
233
260
 
234
261
  /** 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) {
262
+ public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
263
+ if (number === BlockNumber.ZERO) {
237
264
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
238
265
  }
239
- if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
240
- this.latestBlockHashQuery = {
241
- hash: await this.merkleTreeCommitted
242
- .getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
243
- .then(leaf => leaf?.toString()),
244
- blockNumber: number,
245
- };
246
- }
247
- return this.latestBlockHashQuery.hash;
266
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
248
267
  }
249
268
 
250
269
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
251
270
  public async getL2Tips(): Promise<L2Tips> {
252
271
  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! };
255
-
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();
256
290
  return {
257
- 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
291
+ proposed: latestBlockId,
292
+ checkpointed: {
293
+ block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
294
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
295
+ },
296
+ finalized: {
297
+ block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
298
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
299
+ },
300
+ proven: {
301
+ block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
302
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
303
+ },
260
304
  };
261
305
  }
262
306
 
@@ -264,7 +308,7 @@ export class ServerWorldStateSynchronizer
264
308
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
265
309
  switch (event.type) {
266
310
  case 'blocks-added':
267
- await this.handleL2Blocks(event.blocks.map(b => b.block));
311
+ await this.handleL2Blocks(event.blocks);
268
312
  break;
269
313
  case 'chain-pruned':
270
314
  await this.handleChainPruned(event.block.number);
@@ -284,21 +328,31 @@ export class ServerWorldStateSynchronizer
284
328
  * @returns Whether the block handled was produced by this same node.
285
329
  */
286
330
  private async handleL2Blocks(l2Blocks: L2Block[]) {
287
- this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
331
+ this.log.debug(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
332
+
333
+ // Fetch the L1->L2 messages for the first block in a checkpoint.
334
+ const messagesForBlocks = new Map<BlockNumber, Fr[]>();
335
+ await Promise.all(
336
+ l2Blocks
337
+ .filter(b => b.indexWithinCheckpoint === 0)
338
+ .map(async block => {
339
+ const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
340
+ messagesForBlocks.set(block.number, l1ToL2Messages);
341
+ }),
342
+ );
288
343
 
289
- const messagePromises = l2Blocks.map(block => this.l2BlockSource.getL1ToL2Messages(block.number));
290
- const l1ToL2Messages: Fr[][] = await Promise.all(messagePromises);
291
344
  let updateStatus: WorldStateStatusFull | undefined = undefined;
292
-
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}`, {
345
+ for (const block of l2Blocks) {
346
+ const [duration, result] = await elapsed(() =>
347
+ this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
348
+ );
349
+ this.log.info(`World state updated with L2 block ${block.number}`, {
296
350
  eventName: 'l2-block-handled',
297
351
  duration,
298
- unfinalizedBlockNumber: result.summary.unfinalizedBlockNumber,
299
- finalizedBlockNumber: result.summary.finalizedBlockNumber,
300
- oldestHistoricBlock: result.summary.oldestHistoricalBlock,
301
- ...l2Blocks[i].getStats(),
352
+ unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
353
+ finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
354
+ oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
355
+ ...block.getStats(),
302
356
  } satisfies L2BlockHandledStats);
303
357
  updateStatus = result;
304
358
  }
@@ -315,17 +369,12 @@ export class ServerWorldStateSynchronizer
315
369
  * @returns Whether the block handled was produced by this same node.
316
370
  */
317
371
  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.
325
- this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
372
+ this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
326
373
  blockNumber: l2Block.number,
327
374
  blockHash: await l2Block.hash().then(h => h.toString()),
328
375
  l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
376
+ blockHeader: l2Block.header.toInspect(),
377
+ blockStats: l2Block.getStats(),
329
378
  });
330
379
  const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
331
380
 
@@ -337,31 +386,73 @@ export class ServerWorldStateSynchronizer
337
386
  return result;
338
387
  }
339
388
 
340
- private async handleChainFinalized(blockNumber: number) {
389
+ private async handleChainFinalized(blockNumber: BlockNumber) {
341
390
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
342
- const summary = await this.merkleTreeDb.setFinalized(BigInt(blockNumber));
391
+ // If the finalized block number is older than the oldest available block in world state,
392
+ // skip entirely. The finalized block number can jump backwards (e.g. when the finalization
393
+ // heuristic changes) and try to read block data that has already been pruned. When this
394
+ // happens, there is nothing useful to do — the native world state is already finalized
395
+ // past this point and pruning has already happened.
396
+ const currentSummary = await this.merkleTreeDb.getStatusSummary();
397
+ if (blockNumber < currentSummary.oldestHistoricalBlock || blockNumber < 1) {
398
+ this.log.trace(
399
+ `Finalized block ${blockNumber} is older than the oldest available block ${currentSummary.oldestHistoricalBlock}. Skipping.`,
400
+ );
401
+ return;
402
+ }
403
+ const summary = await this.merkleTreeDb.setFinalized(blockNumber);
343
404
  if (this.historyToKeep === undefined) {
344
405
  return;
345
406
  }
346
- const newHistoricBlock = summary.finalizedBlockNumber - BigInt(this.historyToKeep) + 1n;
347
- if (newHistoricBlock <= 1) {
407
+ // Get the checkpointed block for the finalized block number
408
+ const finalisedCheckpoint = await this.l2BlockSource.getCheckpointedBlock(summary.finalizedBlockNumber);
409
+ if (finalisedCheckpoint === undefined) {
410
+ this.log.warn(
411
+ `Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`,
412
+ );
348
413
  return;
349
414
  }
350
- this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
351
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
415
+ // Compute the required historic checkpoint number
416
+ const newHistoricCheckpointNumber = finalisedCheckpoint.checkpointNumber - this.historyToKeep + 1;
417
+ if (newHistoricCheckpointNumber <= 1) {
418
+ return;
419
+ }
420
+ // Retrieve the historic checkpoint
421
+ const historicCheckpoints = await this.l2BlockSource.getCheckpoints(
422
+ CheckpointNumber(newHistoricCheckpointNumber),
423
+ 1,
424
+ );
425
+ if (historicCheckpoints.length === 0 || historicCheckpoints[0] === undefined) {
426
+ this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
427
+ return;
428
+ }
429
+ const historicCheckpoint = historicCheckpoints[0];
430
+ if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
431
+ this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
432
+ return;
433
+ }
434
+ // Find the block at the start of the checkpoint and remove blocks up to this one
435
+ const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
436
+ if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
437
+ this.log.debug(
438
+ `Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`,
439
+ );
440
+ return;
441
+ }
442
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
443
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
352
444
  this.log.debug(`World state summary `, status.summary);
353
445
  }
354
446
 
355
- private handleChainProven(blockNumber: number) {
356
- this.provenBlockNumber = BigInt(blockNumber);
447
+ private handleChainProven(blockNumber: BlockNumber) {
448
+ this.provenBlockNumber = blockNumber;
357
449
  this.log.debug(`Proven chain is now at block ${blockNumber}`);
358
450
  return Promise.resolve();
359
451
  }
360
452
 
361
- private async handleChainPruned(blockNumber: number) {
362
- this.log.warn(`Chain pruned to block ${blockNumber}`);
363
- const status = await this.merkleTreeDb.unwindBlocks(BigInt(blockNumber));
364
- this.latestBlockHashQuery = undefined;
453
+ private async handleChainPruned(blockNumber: BlockNumber) {
454
+ this.log.info(`Chain pruned to block ${blockNumber}`);
455
+ const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
365
456
  this.provenBlockNumber = undefined;
366
457
  this.instrumentation.updateWorldStateMetrics(status);
367
458
  }
@@ -374,24 +465,4 @@ export class ServerWorldStateSynchronizer
374
465
  this.currentState = newState;
375
466
  this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
376
467
  }
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
468
  }