@aztec/archiver 0.0.1-commit.cb6bed7c2 → 0.0.1-commit.cbf2c2d5d

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 (65) hide show
  1. package/dest/archiver.d.ts +3 -6
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +51 -18
  4. package/dest/config.d.ts +3 -3
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +2 -1
  7. package/dest/errors.d.ts +21 -9
  8. package/dest/errors.d.ts.map +1 -1
  9. package/dest/errors.js +27 -14
  10. package/dest/factory.d.ts +3 -4
  11. package/dest/factory.d.ts.map +1 -1
  12. package/dest/factory.js +19 -18
  13. package/dest/modules/data_source_base.d.ts +5 -5
  14. package/dest/modules/data_source_base.d.ts.map +1 -1
  15. package/dest/modules/data_source_base.js +5 -5
  16. package/dest/modules/data_store_updater.d.ts +12 -10
  17. package/dest/modules/data_store_updater.d.ts.map +1 -1
  18. package/dest/modules/data_store_updater.js +69 -76
  19. package/dest/modules/l1_synchronizer.d.ts +2 -2
  20. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  21. package/dest/modules/l1_synchronizer.js +34 -6
  22. package/dest/store/block_store.d.ts +12 -13
  23. package/dest/store/block_store.d.ts.map +1 -1
  24. package/dest/store/block_store.js +61 -61
  25. package/dest/store/contract_class_store.d.ts +2 -3
  26. package/dest/store/contract_class_store.d.ts.map +1 -1
  27. package/dest/store/contract_class_store.js +7 -67
  28. package/dest/store/contract_instance_store.d.ts +1 -1
  29. package/dest/store/contract_instance_store.d.ts.map +1 -1
  30. package/dest/store/contract_instance_store.js +6 -2
  31. package/dest/store/kv_archiver_store.d.ts +28 -18
  32. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  33. package/dest/store/kv_archiver_store.js +34 -21
  34. package/dest/store/log_store.d.ts +6 -3
  35. package/dest/store/log_store.d.ts.map +1 -1
  36. package/dest/store/log_store.js +93 -16
  37. package/dest/store/message_store.d.ts +5 -1
  38. package/dest/store/message_store.d.ts.map +1 -1
  39. package/dest/store/message_store.js +13 -0
  40. package/dest/test/fake_l1_state.d.ts +8 -1
  41. package/dest/test/fake_l1_state.d.ts.map +1 -1
  42. package/dest/test/fake_l1_state.js +39 -5
  43. package/dest/test/mock_l2_block_source.d.ts +3 -3
  44. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  45. package/dest/test/mock_l2_block_source.js +4 -4
  46. package/dest/test/noop_l1_archiver.d.ts +4 -1
  47. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  48. package/dest/test/noop_l1_archiver.js +5 -1
  49. package/package.json +13 -13
  50. package/src/archiver.ts +53 -19
  51. package/src/config.ts +8 -1
  52. package/src/errors.ts +40 -24
  53. package/src/factory.ts +19 -14
  54. package/src/modules/data_source_base.ts +11 -6
  55. package/src/modules/data_store_updater.ts +77 -106
  56. package/src/modules/l1_synchronizer.ts +40 -12
  57. package/src/store/block_store.ts +72 -69
  58. package/src/store/contract_class_store.ts +8 -106
  59. package/src/store/contract_instance_store.ts +8 -5
  60. package/src/store/kv_archiver_store.ts +43 -32
  61. package/src/store/log_store.ts +126 -27
  62. package/src/store/message_store.ts +19 -0
  63. package/src/test/fake_l1_state.ts +50 -9
  64. package/src/test/mock_l2_block_source.ts +9 -3
  65. package/src/test/noop_l1_archiver.ts +7 -1
@@ -1,11 +1,7 @@
1
1
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
- import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { filterAsync } from '@aztec/foundation/collection';
3
3
  import { createLogger } from '@aztec/foundation/log';
4
- import {
5
- ContractClassPublishedEvent,
6
- PrivateFunctionBroadcastedEvent,
7
- UtilityFunctionBroadcastedEvent,
8
- } from '@aztec/protocol-contracts/class-registry';
4
+ import { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
9
5
  import {
10
6
  ContractInstancePublishedEvent,
11
7
  ContractInstanceUpdatedEvent,
@@ -13,17 +9,13 @@ import {
13
9
  import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
10
  import { type PublishedCheckpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
15
11
  import {
16
- type ExecutablePrivateFunctionWithMembershipProof,
17
- type UtilityFunctionWithMembershipProof,
18
- computePublicBytecodeCommitment,
19
- isValidPrivateFunctionMembershipProof,
20
- isValidUtilityFunctionMembershipProof,
12
+ type ContractClassPublicWithCommitment,
13
+ computeContractAddressFromInstance,
14
+ computeContractClassId,
21
15
  } from '@aztec/stdlib/contract';
22
16
  import type { ContractClassLog, PrivateLog, PublicLog } from '@aztec/stdlib/logs';
23
17
  import type { UInt64 } from '@aztec/stdlib/types';
24
18
 
25
- import groupBy from 'lodash.groupby';
26
-
27
19
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
28
20
  import type { L2TipsCache } from '../store/l2_tips_cache.js';
29
21
 
@@ -52,29 +44,28 @@ export class ArchiverDataStoreUpdater {
52
44
  ) {}
53
45
 
54
46
  /**
55
- * Adds proposed blocks to the store with contract class/instance extraction from logs.
56
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
57
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
58
- * and individually broadcasted functions from the block logs.
47
+ * Adds a proposed block to the store with contract class/instance extraction from logs.
48
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
49
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
59
50
  *
60
- * @param blocks - The proposed L2 blocks to add.
51
+ * @param block - The proposed L2 block to add.
61
52
  * @param pendingChainValidationStatus - Optional validation status to set.
62
53
  * @returns True if the operation is successful.
63
54
  */
64
- public async addProposedBlocks(
65
- blocks: L2Block[],
55
+ public async addProposedBlock(
56
+ block: L2Block,
66
57
  pendingChainValidationStatus?: ValidateCheckpointResult,
67
58
  ): Promise<boolean> {
68
59
  const result = await this.store.transactionAsync(async () => {
69
- await this.store.addProposedBlocks(blocks);
60
+ await this.store.addProposedBlock(block);
70
61
 
71
62
  const opResults = await Promise.all([
72
63
  // Update the pending chain validation status if provided
73
64
  pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
74
- // Add any logs emitted during the retrieved blocks
75
- this.store.addLogs(blocks),
76
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
77
- ...blocks.map(block => this.addContractDataToDb(block)),
65
+ // Add any logs emitted during the retrieved block
66
+ this.store.addLogs([block]),
67
+ // Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
68
+ this.addContractDataToDb(block),
78
69
  ]);
79
70
 
80
71
  await this.l2TipsCache?.refresh();
@@ -87,8 +78,7 @@ export class ArchiverDataStoreUpdater {
87
78
  * Reconciles local blocks with incoming checkpoints from L1.
88
79
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
89
80
  * Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
90
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
91
- * and individually broadcasted functions from the checkpoint block logs.
81
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
92
82
  *
93
83
  * @param checkpoints - The published checkpoints to add.
94
84
  * @param pendingChainValidationStatus - Optional validation status to set.
@@ -108,7 +98,7 @@ export class ArchiverDataStoreUpdater {
108
98
 
109
99
  await this.store.addCheckpoints(checkpoints);
110
100
 
111
- // Filter out blocks that were already inserted via addProposedBlocks() to avoid duplicating logs/contract data
101
+ // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
112
102
  const newBlocks = checkpoints
113
103
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
114
104
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
@@ -178,7 +168,7 @@ export class ArchiverDataStoreUpdater {
178
168
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
179
169
  lastAlreadyInsertedBlockNumber = blockNumber;
180
170
  } else {
181
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
171
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
182
172
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
183
173
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
184
174
  }
@@ -281,6 +271,17 @@ export class ArchiverDataStoreUpdater {
281
271
  });
282
272
  }
283
273
 
274
+ /**
275
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
276
+ * @param checkpointNumber - The checkpoint number to set as finalized.
277
+ */
278
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
279
+ await this.store.transactionAsync(async () => {
280
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
281
+ await this.l2TipsCache?.refresh();
282
+ });
283
+ }
284
+
284
285
  /** Extracts and stores contract data from a single block. */
285
286
  private addContractDataToDb(block: L2Block): Promise<boolean> {
286
287
  return this.updateContractDataOnDb(block, Operation.Store);
@@ -302,9 +303,6 @@ export class ArchiverDataStoreUpdater {
302
303
  this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
303
304
  this.updateDeployedContractInstances(privateLogs, block.number, operation),
304
305
  this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
305
- operation === Operation.Store
306
- ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
307
- : Promise.resolve(true),
308
306
  ])
309
307
  ).every(Boolean);
310
308
  }
@@ -321,18 +319,37 @@ export class ArchiverDataStoreUpdater {
321
319
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
322
320
  .map(log => ContractClassPublishedEvent.fromLog(log));
323
321
 
324
- const contractClasses = await Promise.all(contractClassPublishedEvents.map(e => e.toContractClassPublic()));
325
- if (contractClasses.length > 0) {
326
- contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
327
- if (operation == Operation.Store) {
328
- // TODO: Will probably want to create some worker threads to compute these bytecode commitments as they are expensive
329
- const commitments = await Promise.all(
330
- contractClasses.map(c => computePublicBytecodeCommitment(c.packedBytecode)),
331
- );
332
- return await this.store.addContractClasses(contractClasses, commitments, blockNum);
333
- } else if (operation == Operation.Delete) {
322
+ if (operation == Operation.Delete) {
323
+ const contractClasses = contractClassPublishedEvents.map(e => e.toContractClassPublic());
324
+ if (contractClasses.length > 0) {
325
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
334
326
  return await this.store.deleteContractClasses(contractClasses, blockNum);
335
327
  }
328
+ return true;
329
+ }
330
+
331
+ // Compute bytecode commitments and validate class IDs in a single pass.
332
+ const contractClasses: ContractClassPublicWithCommitment[] = [];
333
+ for (const event of contractClassPublishedEvents) {
334
+ const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
335
+ const computedClassId = await computeContractClassId({
336
+ artifactHash: contractClass.artifactHash,
337
+ privateFunctionsRoot: contractClass.privateFunctionsRoot,
338
+ publicBytecodeCommitment: contractClass.publicBytecodeCommitment,
339
+ });
340
+ if (!computedClassId.equals(contractClass.id)) {
341
+ this.log.warn(
342
+ `Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`,
343
+ { blockNum, contractClassId: event.contractClassId.toString() },
344
+ );
345
+ continue;
346
+ }
347
+ contractClasses.push(contractClass);
348
+ }
349
+
350
+ if (contractClasses.length > 0) {
351
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
352
+ return await this.store.addContractClasses(contractClasses, blockNum);
336
353
  }
337
354
  return true;
338
355
  }
@@ -345,10 +362,27 @@ export class ArchiverDataStoreUpdater {
345
362
  blockNum: BlockNumber,
346
363
  operation: Operation,
347
364
  ): Promise<boolean> {
348
- const contractInstances = allLogs
365
+ const allInstances = allLogs
349
366
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
350
367
  .map(log => ContractInstancePublishedEvent.fromLog(log))
351
368
  .map(e => e.toContractInstance());
369
+
370
+ // Verify that each instance's address matches the one derived from its fields if we're adding
371
+ const contractInstances =
372
+ operation === Operation.Delete
373
+ ? allInstances
374
+ : await filterAsync(allInstances, async instance => {
375
+ const computedAddress = await computeContractAddressFromInstance(instance);
376
+ if (!computedAddress.equals(instance.address)) {
377
+ this.log.warn(
378
+ `Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
379
+ { instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
380
+ );
381
+ return false;
382
+ }
383
+ return true;
384
+ });
385
+
352
386
  if (contractInstances.length > 0) {
353
387
  contractInstances.forEach(c =>
354
388
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -387,67 +421,4 @@ export class ArchiverDataStoreUpdater {
387
421
  }
388
422
  return true;
389
423
  }
390
-
391
- /**
392
- * Stores the functions that were broadcasted individually.
393
- *
394
- * @dev Beware that there is not a delete variant of this, since they are added to contract classes
395
- * and will be deleted as part of the class if needed.
396
- */
397
- private async storeBroadcastedIndividualFunctions(
398
- allLogs: ContractClassLog[],
399
- _blockNum: BlockNumber,
400
- ): Promise<boolean> {
401
- // Filter out private and utility function broadcast events
402
- const privateFnEvents = allLogs
403
- .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
404
- .map(log => PrivateFunctionBroadcastedEvent.fromLog(log));
405
- const utilityFnEvents = allLogs
406
- .filter(log => UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log))
407
- .map(log => UtilityFunctionBroadcastedEvent.fromLog(log));
408
-
409
- // Group all events by contract class id
410
- for (const [classIdString, classEvents] of Object.entries(
411
- groupBy([...privateFnEvents, ...utilityFnEvents], e => e.contractClassId.toString()),
412
- )) {
413
- const contractClassId = Fr.fromHexString(classIdString);
414
- const contractClass = await this.store.getContractClass(contractClassId);
415
- if (!contractClass) {
416
- this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
417
- continue;
418
- }
419
-
420
- // Split private and utility functions, and filter out invalid ones
421
- const allFns = classEvents.map(e => e.toFunctionWithMembershipProof());
422
- const privateFns = allFns.filter(
423
- (fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'utilityFunctionsTreeRoot' in fn,
424
- );
425
- const utilityFns = allFns.filter(
426
- (fn): fn is UtilityFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
427
- );
428
-
429
- const privateFunctionsWithValidity = await Promise.all(
430
- privateFns.map(async fn => ({ fn, valid: await isValidPrivateFunctionMembershipProof(fn, contractClass) })),
431
- );
432
- const validPrivateFns = privateFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
433
- const utilityFunctionsWithValidity = await Promise.all(
434
- utilityFns.map(async fn => ({
435
- fn,
436
- valid: await isValidUtilityFunctionMembershipProof(fn, contractClass),
437
- })),
438
- );
439
- const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
440
- const validFnCount = validPrivateFns.length + validUtilityFns.length;
441
- if (validFnCount !== allFns.length) {
442
- this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
443
- }
444
-
445
- // Store the functions in the contract class in a single operation
446
- if (validFnCount > 0) {
447
- this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
448
- }
449
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
450
- }
451
- return true;
452
- }
453
424
  }
@@ -3,6 +3,7 @@ import { EpochCache } from '@aztec/epoch-cache';
3
3
  import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
4
4
  import type { L1BlockId } from '@aztec/ethereum/l1-types';
5
5
  import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
6
+ import { asyncPool } from '@aztec/foundation/async-pool';
6
7
  import { maxBigint } from '@aztec/foundation/bigint';
7
8
  import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
8
9
  import { Buffer32 } from '@aztec/foundation/buffer';
@@ -72,7 +73,6 @@ export class ArchiverL1Synchronizer implements Traceable {
72
73
  private readonly l1Constants: L1RollupConstants & {
73
74
  l1StartBlockHash: Buffer32;
74
75
  genesisArchiveRoot: Fr;
75
- rollupManaLimit?: number;
76
76
  },
77
77
  private readonly events: ArchiverEmitter,
78
78
  tracer: Tracer,
@@ -217,6 +217,9 @@ export class ArchiverL1Synchronizer implements Traceable {
217
217
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
218
218
  }
219
219
 
220
+ // Update the finalized L2 checkpoint based on L1 finality.
221
+ await this.updateFinalizedCheckpoint();
222
+
220
223
  // After syncing has completed, update the current l1 block number and timestamp,
221
224
  // otherwise we risk announcing to the world that we've synced to a given point,
222
225
  // but the corresponding blocks have not been processed (see #12631).
@@ -232,6 +235,27 @@ export class ArchiverL1Synchronizer implements Traceable {
232
235
  });
233
236
  }
234
237
 
238
+ /** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
239
+ private async updateFinalizedCheckpoint(): Promise<void> {
240
+ try {
241
+ const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
242
+ const finalizedL1BlockNumber = finalizedL1Block.number;
243
+ const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
244
+ blockNumber: finalizedL1BlockNumber,
245
+ });
246
+ const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
247
+ if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
248
+ await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
249
+ this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
250
+ finalizedCheckpointNumber,
251
+ finalizedL1BlockNumber,
252
+ });
253
+ }
254
+ } catch (err) {
255
+ this.log.warn(`Failed to update finalized checkpoint: ${err}`);
256
+ }
257
+ }
258
+
235
259
  /** Prune all proposed local blocks that should have been checkpointed by now. */
236
260
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
237
261
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
@@ -310,17 +334,20 @@ export class ArchiverL1Synchronizer implements Traceable {
310
334
 
311
335
  const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
312
336
 
313
- const checkpointPromises = Array.from({ length: checkpointsToUnwind })
314
- .fill(0)
315
- .map((_, i) => this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
316
- const checkpoints = await Promise.all(checkpointPromises);
317
-
318
- const blockPromises = await Promise.all(
319
- checkpoints
320
- .filter(isDefined)
321
- .map(cp => this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))),
337
+ // Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
338
+ // promises when the gap between local pending and proven checkpoint numbers is large.
339
+ const BATCH_SIZE = 10;
340
+ const indices = Array.from({ length: checkpointsToUnwind }, (_, i) => CheckpointNumber(i + pruneFrom));
341
+ const checkpoints = (await asyncPool(BATCH_SIZE, indices, idx => this.store.getCheckpointData(idx))).filter(
342
+ isDefined,
322
343
  );
323
- const newBlocks = blockPromises.filter(isDefined).flat();
344
+ const newBlocks = (
345
+ await asyncPool(BATCH_SIZE, checkpoints, cp =>
346
+ this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)),
347
+ )
348
+ )
349
+ .filter(isDefined)
350
+ .flat();
324
351
 
325
352
  // Emit an event for listening services to react to the chain prune
326
353
  this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
@@ -368,6 +395,7 @@ export class ArchiverL1Synchronizer implements Traceable {
368
395
  const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
369
396
  const localLastMessage = await this.store.getLastL1ToL2Message();
370
397
  const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
398
+ await this.store.setInboxTreeInProgress(remoteMessagesState.treeInProgress);
371
399
 
372
400
  this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
373
401
  localMessagesInserted,
@@ -828,7 +856,7 @@ export class ArchiverL1Synchronizer implements Traceable {
828
856
  const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
829
857
  const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
830
858
 
831
- this.log.warn(
859
+ this.log.info(
832
860
  `Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
833
861
  { prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
834
862
  );
@@ -20,7 +20,7 @@ import {
20
20
  serializeValidateCheckpointResult,
21
21
  } from '@aztec/stdlib/block';
22
22
  import { type CheckpointData, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
23
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
23
+ import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
24
24
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
25
25
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
26
26
  import {
@@ -35,15 +35,14 @@ import {
35
35
  } from '@aztec/stdlib/tx';
36
36
 
37
37
  import {
38
+ BlockAlreadyCheckpointedError,
38
39
  BlockArchiveNotConsistentError,
39
40
  BlockIndexNotSequentialError,
40
41
  BlockNotFoundError,
41
42
  BlockNumberNotSequentialError,
42
43
  CannotOverwriteCheckpointedBlockError,
43
44
  CheckpointNotFoundError,
44
- CheckpointNumberNotConsistentError,
45
45
  CheckpointNumberNotSequentialError,
46
- InitialBlockNumberNotSequentialError,
47
46
  InitialCheckpointNumberNotSequentialError,
48
47
  } from '../errors.js';
49
48
 
@@ -97,6 +96,9 @@ export class BlockStore {
97
96
  /** Stores last proven checkpoint */
98
97
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
99
98
 
99
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
100
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
101
+
100
102
  /** Stores the pending chain validation status */
101
103
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
102
104
 
@@ -111,10 +113,7 @@ export class BlockStore {
111
113
 
112
114
  #log = createLogger('archiver:block_store');
113
115
 
114
- constructor(
115
- private db: AztecAsyncKVStore,
116
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
117
- ) {
116
+ constructor(private db: AztecAsyncKVStore) {
118
117
  this.#blocks = db.openMap('archiver_blocks');
119
118
  this.#blockTxs = db.openMap('archiver_block_txs');
120
119
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -123,41 +122,42 @@ export class BlockStore {
123
122
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
124
123
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
125
124
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
125
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
126
126
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
127
127
  this.#checkpoints = db.openMap('archiver_checkpoints');
128
128
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
129
129
  }
130
130
 
131
131
  /**
132
- * Computes the finalized block number based on the proven block number.
133
- * A block is considered finalized when it's 2 epochs behind the proven block.
134
- * TODO(#13569): Compute proper finalized block number based on L1 finalized block.
135
- * TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
132
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
133
+ * in an L1 block that has itself been finalized on Ethereum.
136
134
  * @returns The finalized block number.
137
135
  */
138
136
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
139
- const provenBlockNumber = await this.getProvenBlockNumber();
140
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
137
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
138
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
139
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
140
+ }
141
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
142
+ if (!checkpointStorage) {
143
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
144
+ }
145
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
141
146
  }
142
147
 
143
148
  /**
144
- * Append new proposed blocks to the store's list. All blocks must be for the 'current' checkpoint.
145
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
149
+ * Append a new proposed block to the store.
150
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
146
151
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
147
- * @param blocks - The proposed L2 blocks to be added to the store.
152
+ * @param block - The proposed L2 block to be added to the store.
148
153
  * @returns True if the operation is successful.
149
154
  */
150
- async addProposedBlocks(blocks: L2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
151
- if (blocks.length === 0) {
152
- return true;
153
- }
154
-
155
+ async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
155
156
  return await this.db.transactionAsync(async () => {
156
- // Check that the block immediately before the first block to be added is present in the store.
157
- const firstBlockNumber = blocks[0].number;
158
- const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
159
- const firstBlockIndex = blocks[0].indexWithinCheckpoint;
160
- const firstBlockLastArchive = blocks[0].header.lastArchive.root;
157
+ const blockNumber = block.number;
158
+ const blockCheckpointNumber = block.checkpointNumber;
159
+ const blockIndex = block.indexWithinCheckpoint;
160
+ const blockLastArchive = block.header.lastArchive.root;
161
161
 
162
162
  // Extract the latest block and checkpoint numbers
163
163
  const previousBlockNumber = await this.getLatestBlockNumber();
@@ -165,71 +165,52 @@ export class BlockStore {
165
165
 
166
166
  // Verify we're not overwriting checkpointed blocks
167
167
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
168
- if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
169
- throw new CannotOverwriteCheckpointedBlockError(firstBlockNumber, lastCheckpointedBlockNumber);
168
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
169
+ // Check if the proposed block matches the already-checkpointed one
170
+ const existingBlock = await this.getBlock(BlockNumber(blockNumber));
171
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
172
+ throw new BlockAlreadyCheckpointedError(blockNumber);
173
+ }
174
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
170
175
  }
171
176
 
172
- // Check that the first block number is the expected one
173
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
174
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
177
+ // Check that the block number is the expected one
178
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
179
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
175
180
  }
176
181
 
177
182
  // The same check as above but for checkpoints
178
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
179
- throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
183
+ if (!opts.force && previousCheckpointNumber !== blockCheckpointNumber - 1) {
184
+ throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, previousCheckpointNumber);
180
185
  }
181
186
 
182
187
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
183
188
  const previousBlockResult = await this.getBlock(previousBlockNumber);
184
189
 
185
- let expectedFirstblockIndex = 0;
190
+ let expectedBlockIndex = 0;
186
191
  let previousBlockIndex: number | undefined = undefined;
187
192
  if (previousBlockResult !== undefined) {
188
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
193
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
189
194
  // The previous block is for the same checkpoint, therefore our index should follow it
190
195
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
191
- expectedFirstblockIndex = previousBlockIndex + 1;
196
+ expectedBlockIndex = previousBlockIndex + 1;
192
197
  }
193
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
198
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
194
199
  throw new BlockArchiveNotConsistentError(
195
- firstBlockNumber,
200
+ blockNumber,
196
201
  previousBlockResult.number,
197
- firstBlockLastArchive,
202
+ blockLastArchive,
198
203
  previousBlockResult.archive.root,
199
204
  );
200
205
  }
201
206
  }
202
207
 
203
- // Now check that the first block has the expected index value
204
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
205
- throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
208
+ // Now check that the block has the expected index value
209
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
210
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
206
211
  }
207
212
 
208
- // Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
209
- let previousBlock: L2Block | undefined = undefined;
210
- for (const block of blocks) {
211
- if (!opts.force && previousBlock) {
212
- if (previousBlock.number + 1 !== block.number) {
213
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
214
- }
215
- if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
216
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
217
- }
218
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
219
- throw new BlockArchiveNotConsistentError(
220
- block.number,
221
- previousBlock.number,
222
- block.header.lastArchive.root,
223
- previousBlock.archive.root,
224
- );
225
- }
226
- }
227
- if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
228
- throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
229
- }
230
- previousBlock = block;
231
- await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
232
- }
213
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
233
214
 
234
215
  return true;
235
216
  });
@@ -871,7 +852,10 @@ export class BlockStore {
871
852
  * @param txHash - The hash of a tx we try to get the receipt for.
872
853
  * @returns The requested tx receipt (or undefined if not found).
873
854
  */
874
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
855
+ async getSettledTxReceipt(
856
+ txHash: TxHash,
857
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
858
+ ): Promise<TxReceipt | undefined> {
875
859
  const txEffect = await this.getTxEffect(txHash);
876
860
  if (!txEffect) {
877
861
  return undefined;
@@ -880,10 +864,11 @@ export class BlockStore {
880
864
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
881
865
 
882
866
  // Use existing archiver methods to determine finalization level
883
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
867
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
884
868
  this.getProvenBlockNumber(),
885
869
  this.getCheckpointedL2BlockNumber(),
886
870
  this.getFinalizedL2BlockNumber(),
871
+ this.getBlockData(blockNumber),
887
872
  ]);
888
873
 
889
874
  let status: TxStatus;
@@ -897,6 +882,9 @@ export class BlockStore {
897
882
  status = TxStatus.PROPOSED;
898
883
  }
899
884
 
885
+ const epochNumber =
886
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
887
+
900
888
  return new TxReceipt(
901
889
  txHash,
902
890
  status,
@@ -905,6 +893,7 @@ export class BlockStore {
905
893
  txEffect.data.transactionFee.toBigInt(),
906
894
  txEffect.l2BlockHash,
907
895
  blockNumber,
896
+ epochNumber,
908
897
  );
909
898
  }
910
899
 
@@ -976,6 +965,20 @@ export class BlockStore {
976
965
  return result;
977
966
  }
978
967
 
968
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
969
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
970
+ this.getLatestCheckpointNumber(),
971
+ this.#lastFinalizedCheckpoint.getAsync(),
972
+ ]);
973
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
974
+ ? latestCheckpointNumber
975
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
976
+ }
977
+
978
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
979
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
980
+ }
981
+
979
982
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
980
983
  if (limit < 1) {
981
984
  throw new Error(`Invalid limit: ${limit}`);