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

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 (50) 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 +15 -8
  8. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  9. package/dest/native/merkle_trees_facade.js +53 -15
  10. package/dest/native/message.d.ts +24 -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 +19 -14
  14. package/dest/native/native_world_state.d.ts.map +1 -1
  15. package/dest/native/native_world_state.js +31 -21
  16. package/dest/native/native_world_state_instance.d.ts +5 -5
  17. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  18. package/dest/native/native_world_state_instance.js +8 -7
  19. package/dest/native/world_state_ops_queue.js +5 -5
  20. package/dest/synchronizer/config.d.ts +3 -5
  21. package/dest/synchronizer/config.d.ts.map +1 -1
  22. package/dest/synchronizer/config.js +14 -16
  23. package/dest/synchronizer/factory.d.ts +6 -5
  24. package/dest/synchronizer/factory.d.ts.map +1 -1
  25. package/dest/synchronizer/factory.js +6 -5
  26. package/dest/synchronizer/server_world_state_synchronizer.d.ts +11 -17
  27. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  28. package/dest/synchronizer/server_world_state_synchronizer.js +162 -81
  29. package/dest/test/utils.d.ts +12 -5
  30. package/dest/test/utils.d.ts.map +1 -1
  31. package/dest/test/utils.js +54 -50
  32. package/dest/testing.d.ts +5 -4
  33. package/dest/testing.d.ts.map +1 -1
  34. package/dest/testing.js +11 -7
  35. package/dest/world-state-db/merkle_tree_db.d.ts +10 -20
  36. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  37. package/package.json +12 -13
  38. package/src/instrumentation/instrumentation.ts +17 -41
  39. package/src/native/fork_checkpoint.ts +19 -3
  40. package/src/native/merkle_trees_facade.ts +62 -16
  41. package/src/native/message.ts +37 -26
  42. package/src/native/native_world_state.ts +52 -34
  43. package/src/native/native_world_state_instance.ts +14 -8
  44. package/src/native/world_state_ops_queue.ts +5 -5
  45. package/src/synchronizer/config.ts +15 -26
  46. package/src/synchronizer/factory.ts +13 -7
  47. package/src/synchronizer/server_world_state_synchronizer.ts +184 -106
  48. package/src/test/utils.ts +86 -91
  49. package/src/testing.ts +9 -10
  50. package/src/world-state-db/merkle_tree_db.ts +13 -24
@@ -1,19 +1,21 @@
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 { 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_BLOCK_HEADER_HASH,
10
+ GENESIS_CHECKPOINT_HEADER_HASH,
11
+ type L2Block,
12
+ type L2BlockId,
13
+ type L2BlockSource,
12
14
  L2BlockStream,
13
- L2BlockStreamEvent,
14
- L2BlockStreamEventHandler,
15
- L2BlockStreamLocalDataProvider,
16
- L2Tips,
15
+ type L2BlockStreamEvent,
16
+ type L2BlockStreamEventHandler,
17
+ type L2BlockStreamLocalDataProvider,
18
+ type L2Tips,
17
19
  } from '@aztec/stdlib/block';
18
20
  import {
19
21
  WorldStateRunningState,
@@ -25,7 +27,7 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
25
27
  import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
26
28
  import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
27
29
  import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
28
- import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
30
+ import { getTelemetryClient } from '@aztec/telemetry-client';
29
31
 
30
32
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
31
33
  import type { WorldStateStatusFull } from '../native/message.js';
@@ -45,17 +47,16 @@ export class ServerWorldStateSynchronizer
45
47
  {
46
48
  private readonly merkleTreeCommitted: MerkleTreeReadOperations;
47
49
 
48
- private latestBlockNumberAtStart = 0;
50
+ private latestBlockNumberAtStart = BlockNumber.ZERO;
49
51
  private historyToKeep: number | undefined;
50
52
  private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
51
- private latestBlockHashQuery: { blockNumber: number; hash: string | undefined } | undefined = undefined;
52
53
 
53
54
  private syncPromise = promiseWithResolvers<void>();
54
55
  protected blockStream: L2BlockStream | undefined;
55
56
 
56
57
  // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
57
58
  // 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;
59
+ private provenBlockNumber: BlockNumber | undefined;
59
60
 
60
61
  constructor(
61
62
  private readonly merkleTreeDb: MerkleTreeAdminDatabase,
@@ -65,7 +66,7 @@ export class ServerWorldStateSynchronizer
65
66
  private readonly log: Logger = createLogger('world_state'),
66
67
  ) {
67
68
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
68
- this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
69
+ this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
69
70
  this.log.info(
70
71
  `Created world state synchroniser with block history of ${
71
72
  this.historyToKeep === undefined ? 'infinity' : this.historyToKeep
@@ -77,12 +78,12 @@ export class ServerWorldStateSynchronizer
77
78
  return this.merkleTreeDb.getCommitted();
78
79
  }
79
80
 
80
- public getSnapshot(blockNumber: number): MerkleTreeReadOperations {
81
+ public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
81
82
  return this.merkleTreeDb.getSnapshot(blockNumber);
82
83
  }
83
84
 
84
- public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
85
- return this.merkleTreeDb.fork(blockNumber);
85
+ public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
86
+ return this.merkleTreeDb.fork(blockNumber, opts);
86
87
  }
87
88
 
88
89
  public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
@@ -102,9 +103,7 @@ export class ServerWorldStateSynchronizer
102
103
  }
103
104
 
104
105
  // Get the current latest block number
105
- this.latestBlockNumberAtStart = await (this.config.worldStateProvenBlocksOnly
106
- ? this.l2BlockSource.getProvenBlockNumber()
107
- : this.l2BlockSource.getBlockNumber());
106
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
108
107
 
109
108
  const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
110
109
 
@@ -126,12 +125,11 @@ export class ServerWorldStateSynchronizer
126
125
  }
127
126
 
128
127
  protected createBlockStream(): L2BlockStream {
129
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
130
128
  const logger = createLogger('world-state:block_stream');
131
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
132
- proven: this.config.worldStateProvenBlocksOnly,
129
+ return new L2BlockStream(this.l2BlockSource, this, this, logger, {
133
130
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
134
131
  batchSize: this.config.worldStateBlockRequestBatchSize,
132
+ ignoreCheckpoints: true,
135
133
  });
136
134
  }
137
135
 
@@ -147,10 +145,10 @@ export class ServerWorldStateSynchronizer
147
145
  public async status(): Promise<WorldStateSynchronizerStatus> {
148
146
  const summary = await this.merkleTreeDb.getStatusSummary();
149
147
  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),
148
+ latestBlockNumber: summary.unfinalizedBlockNumber,
149
+ latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
150
+ finalizedBlockNumber: summary.finalizedBlockNumber,
151
+ oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
154
152
  treesAreSynched: summary.treesAreSynched,
155
153
  };
156
154
  return {
@@ -160,7 +158,7 @@ export class ServerWorldStateSynchronizer
160
158
  }
161
159
 
162
160
  public async getLatestBlockNumber() {
163
- return (await this.getL2Tips()).latest.number;
161
+ return (await this.getL2Tips()).proposed.number;
164
162
  }
165
163
 
166
164
  public async stopSync() {
@@ -181,10 +179,10 @@ export class ServerWorldStateSynchronizer
181
179
  /**
182
180
  * Forces an immediate sync.
183
181
  * @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.
182
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
185
183
  * @returns A promise that resolves with the block number the world state was synced to
186
184
  */
187
- public async syncImmediate(targetBlockNumber?: number, skipThrowIfTargetNotReached?: boolean): Promise<number> {
185
+ public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
188
186
  if (this.currentState !== WorldStateRunningState.RUNNING) {
189
187
  throw new Error(`World State is not running. Unable to perform sync.`);
190
188
  }
@@ -196,13 +194,25 @@ export class ServerWorldStateSynchronizer
196
194
  // If we have been given a block number to sync to and we have reached that number then return
197
195
  const currentBlockNumber = await this.getLatestBlockNumber();
198
196
  if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
199
- return currentBlockNumber;
197
+ if (blockHash === undefined) {
198
+ return currentBlockNumber;
199
+ }
200
+
201
+ // If a block hash was provided, verify we're on the expected fork
202
+ const currentHash = await this.getL2BlockHash(targetBlockNumber);
203
+ if (currentHash === blockHash.toString()) {
204
+ return currentBlockNumber;
205
+ }
206
+ // Hash mismatch: a reorg may have occurred, fall through to trigger sync
207
+ this.log.debug(
208
+ `World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`,
209
+ );
200
210
  }
201
211
  this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
202
212
 
203
213
  // If the archiver is behind the target block, force an archiver sync
204
214
  if (targetBlockNumber) {
205
- const archiverLatestBlock = await this.l2BlockSource.getBlockNumber();
215
+ const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
206
216
  if (archiverLatestBlock < targetBlockNumber) {
207
217
  this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
208
218
  await this.l2BlockSource.syncImmediate();
@@ -214,7 +224,7 @@ export class ServerWorldStateSynchronizer
214
224
 
215
225
  // If we have been given a block number to sync to and we have not reached that number then fail
216
226
  const updatedBlockNumber = await this.getLatestBlockNumber();
217
- if (!skipThrowIfTargetNotReached && targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
227
+ if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
218
228
  throw new WorldStateSynchronizerError(
219
229
  `Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
220
230
  {
@@ -228,35 +238,74 @@ export class ServerWorldStateSynchronizer
228
238
  );
229
239
  }
230
240
 
241
+ // If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
242
+ if (blockHash !== undefined && targetBlockNumber !== undefined) {
243
+ const updatedHash = await this.getL2BlockHash(targetBlockNumber);
244
+ if (updatedHash !== blockHash.toString()) {
245
+ throw new WorldStateSynchronizerError(
246
+ `Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`,
247
+ {
248
+ cause: {
249
+ reason: 'block_hash_mismatch',
250
+ targetBlockNumber,
251
+ expectedHash: blockHash.toString(),
252
+ actualHash: updatedHash,
253
+ },
254
+ },
255
+ );
256
+ }
257
+ }
258
+
231
259
  return updatedBlockNumber;
232
260
  }
233
261
 
234
262
  /** 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) {
263
+ public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
264
+ if (number === BlockNumber.ZERO) {
237
265
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
238
266
  }
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;
267
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
248
268
  }
249
269
 
250
270
  /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
251
271
  public async getL2Tips(): Promise<L2Tips> {
252
272
  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
-
273
+ const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
274
+ const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
275
+
276
+ const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
277
+ const provenBlockHashPromise =
278
+ this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
279
+
280
+ const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
281
+ unfinalizedBlockHashPromise,
282
+ finalizedBlockHashPromise,
283
+ provenBlockHashPromise,
284
+ ]);
285
+ const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
286
+
287
+ // World state doesn't track checkpointed blocks or checkpoints themselves.
288
+ // but we use a block stream so we need to provide 'local' L2Tips.
289
+ // We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
290
+ const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
256
291
  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
292
+ proposed: latestBlockId,
293
+ checkpointed: {
294
+ block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
295
+ checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
296
+ },
297
+ proposedCheckpoint: {
298
+ block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
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
+ },
260
309
  };
261
310
  }
262
311
 
@@ -264,7 +313,7 @@ export class ServerWorldStateSynchronizer
264
313
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
265
314
  switch (event.type) {
266
315
  case 'blocks-added':
267
- await this.handleL2Blocks(event.blocks.map(b => b.block));
316
+ await this.handleL2Blocks(event.blocks);
268
317
  break;
269
318
  case 'chain-pruned':
270
319
  await this.handleChainPruned(event.block.number);
@@ -284,21 +333,31 @@ export class ServerWorldStateSynchronizer
284
333
  * @returns Whether the block handled was produced by this same node.
285
334
  */
286
335
  private async handleL2Blocks(l2Blocks: L2Block[]) {
287
- 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
+ );
288
348
 
289
- const messagePromises = l2Blocks.map(block => this.l2BlockSource.getL1ToL2Messages(block.number));
290
- const l1ToL2Messages: Fr[][] = await Promise.all(messagePromises);
291
349
  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}`, {
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}`, {
296
355
  eventName: 'l2-block-handled',
297
356
  duration,
298
- unfinalizedBlockNumber: result.summary.unfinalizedBlockNumber,
299
- finalizedBlockNumber: result.summary.finalizedBlockNumber,
300
- oldestHistoricBlock: result.summary.oldestHistoricalBlock,
301
- ...l2Blocks[i].getStats(),
357
+ unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
358
+ finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
359
+ oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
360
+ ...block.getStats(),
302
361
  } satisfies L2BlockHandledStats);
303
362
  updateStatus = result;
304
363
  }
@@ -315,17 +374,12 @@ export class ServerWorldStateSynchronizer
315
374
  * @returns Whether the block handled was produced by this same node.
316
375
  */
317
376
  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 `, {
377
+ this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
326
378
  blockNumber: l2Block.number,
327
379
  blockHash: await l2Block.hash().then(h => h.toString()),
328
380
  l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
381
+ blockHeader: l2Block.header.toInspect(),
382
+ blockStats: l2Block.getStats(),
329
383
  });
330
384
  const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
331
385
 
@@ -337,32 +391,76 @@ export class ServerWorldStateSynchronizer
337
391
  return result;
338
392
  }
339
393
 
340
- private async handleChainFinalized(blockNumber: number) {
394
+ private async handleChainFinalized(blockNumber: BlockNumber) {
341
395
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
342
- const summary = await this.merkleTreeDb.setFinalized(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);
343
409
  if (this.historyToKeep === undefined) {
344
410
  return;
345
411
  }
346
- const newHistoricBlock = summary.finalizedBlockNumber - BigInt(this.historyToKeep) + 1n;
347
- if (newHistoricBlock <= 1) {
412
+ // Get the checkpointed block for the finalized block number
413
+ const finalisedCheckpoint = await this.l2BlockSource.getCheckpointedBlock(summary.finalizedBlockNumber);
414
+ if (finalisedCheckpoint === undefined) {
415
+ this.log.warn(
416
+ `Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`,
417
+ );
418
+ return;
419
+ }
420
+ // Compute the required historic checkpoint number
421
+ const newHistoricCheckpointNumber = finalisedCheckpoint.checkpointNumber - this.historyToKeep + 1;
422
+ if (newHistoricCheckpointNumber <= 1) {
423
+ return;
424
+ }
425
+ // Retrieve the historic checkpoint
426
+ const historicCheckpoints = await this.l2BlockSource.getCheckpoints(
427
+ CheckpointNumber(newHistoricCheckpointNumber),
428
+ 1,
429
+ );
430
+ if (historicCheckpoints.length === 0 || historicCheckpoints[0] === undefined) {
431
+ this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
432
+ return;
433
+ }
434
+ const historicCheckpoint = historicCheckpoints[0];
435
+ if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
436
+ this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
348
437
  return;
349
438
  }
350
- this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
351
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
439
+ // Find the block at the start of the checkpoint and remove blocks up to this one
440
+ const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
441
+ if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
442
+ this.log.debug(
443
+ `Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`,
444
+ );
445
+ return;
446
+ }
447
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
448
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
352
449
  this.log.debug(`World state summary `, status.summary);
353
450
  }
354
451
 
355
- private handleChainProven(blockNumber: number) {
356
- this.provenBlockNumber = BigInt(blockNumber);
452
+ private handleChainProven(blockNumber: BlockNumber) {
453
+ this.provenBlockNumber = blockNumber;
357
454
  this.log.debug(`Proven chain is now at block ${blockNumber}`);
358
455
  return Promise.resolve();
359
456
  }
360
457
 
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;
365
- this.provenBlockNumber = undefined;
458
+ private async handleChainPruned(blockNumber: BlockNumber) {
459
+ this.log.info(`Chain pruned to block ${blockNumber}`);
460
+ const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
461
+ if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
462
+ this.provenBlockNumber = undefined;
463
+ }
366
464
  this.instrumentation.updateWorldStateMetrics(status);
367
465
  }
368
466
 
@@ -374,24 +472,4 @@ export class ServerWorldStateSynchronizer
374
472
  this.currentState = newState;
375
473
  this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
376
474
  }
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
475
  }