@aztec/archiver 0.0.1-commit.f504929 → 0.0.1-commit.f81dbcf

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 (59) hide show
  1. package/dest/archiver.d.ts +3 -3
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +36 -14
  4. package/dest/factory.d.ts +2 -3
  5. package/dest/factory.d.ts.map +1 -1
  6. package/dest/factory.js +8 -8
  7. package/dest/l1/data_retrieval.d.ts +3 -6
  8. package/dest/l1/data_retrieval.d.ts.map +1 -1
  9. package/dest/l1/data_retrieval.js +6 -12
  10. package/dest/modules/data_source_base.d.ts +3 -3
  11. package/dest/modules/data_source_base.d.ts.map +1 -1
  12. package/dest/modules/data_source_base.js +1 -1
  13. package/dest/modules/data_store_updater.d.ts +11 -3
  14. package/dest/modules/data_store_updater.d.ts.map +1 -1
  15. package/dest/modules/data_store_updater.js +36 -5
  16. package/dest/modules/instrumentation.d.ts +1 -12
  17. package/dest/modules/instrumentation.d.ts.map +1 -1
  18. package/dest/modules/instrumentation.js +0 -10
  19. package/dest/modules/l1_synchronizer.d.ts +2 -1
  20. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  21. package/dest/modules/l1_synchronizer.js +31 -9
  22. package/dest/store/block_store.d.ts +8 -9
  23. package/dest/store/block_store.d.ts.map +1 -1
  24. package/dest/store/block_store.js +31 -13
  25. package/dest/store/kv_archiver_store.d.ts +13 -3
  26. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  27. package/dest/store/kv_archiver_store.js +16 -4
  28. package/dest/store/log_store.d.ts +1 -1
  29. package/dest/store/log_store.d.ts.map +1 -1
  30. package/dest/store/log_store.js +48 -10
  31. package/dest/store/message_store.js +1 -1
  32. package/dest/test/fake_l1_state.d.ts +8 -1
  33. package/dest/test/fake_l1_state.d.ts.map +1 -1
  34. package/dest/test/fake_l1_state.js +28 -2
  35. package/dest/test/mock_l2_block_source.d.ts +4 -3
  36. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  37. package/dest/test/mock_l2_block_source.js +7 -4
  38. package/dest/test/mock_structs.d.ts +4 -1
  39. package/dest/test/mock_structs.d.ts.map +1 -1
  40. package/dest/test/mock_structs.js +13 -1
  41. package/dest/test/noop_l1_archiver.d.ts +4 -1
  42. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  43. package/dest/test/noop_l1_archiver.js +5 -1
  44. package/package.json +13 -13
  45. package/src/archiver.ts +39 -17
  46. package/src/factory.ts +5 -4
  47. package/src/l1/data_retrieval.ts +9 -17
  48. package/src/modules/data_source_base.ts +3 -3
  49. package/src/modules/data_store_updater.ts +39 -4
  50. package/src/modules/instrumentation.ts +0 -20
  51. package/src/modules/l1_synchronizer.ts +38 -11
  52. package/src/store/block_store.ts +41 -13
  53. package/src/store/kv_archiver_store.ts +22 -4
  54. package/src/store/log_store.ts +66 -12
  55. package/src/store/message_store.ts +1 -1
  56. package/src/test/fake_l1_state.ts +35 -4
  57. package/src/test/mock_l2_block_source.ts +15 -3
  58. package/src/test/mock_structs.ts +20 -6
  59. package/src/test/noop_l1_archiver.ts +7 -1
package/src/archiver.ts CHANGED
@@ -22,9 +22,8 @@ import {
22
22
  import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
23
23
  import {
24
24
  type L1RollupConstants,
25
- getEpochNumberAtTimestamp,
25
+ getEpochAtSlot,
26
26
  getSlotAtNextL1Block,
27
- getSlotAtTimestamp,
28
27
  getSlotRangeForEpoch,
29
28
  getTimestampRangeForEpoch,
30
29
  } from '@aztec/stdlib/epoch-helpers';
@@ -120,7 +119,10 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
120
119
  },
121
120
  private readonly blobClient: BlobClientInterface,
122
121
  instrumentation: ArchiverInstrumentation,
123
- protected override readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
122
+ protected override readonly l1Constants: L1RollupConstants & {
123
+ l1StartBlockHash: Buffer32;
124
+ genesisArchiveRoot: Fr;
125
+ },
124
126
  synchronizer: ArchiverL1Synchronizer,
125
127
  events: ArchiverEmitter,
126
128
  l2TipsCache?: L2TipsCache,
@@ -133,7 +135,9 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
133
135
  this.synchronizer = synchronizer;
134
136
  this.events = events;
135
137
  this.l2TipsCache = l2TipsCache ?? new L2TipsCache(this.dataStore.blockStore);
136
- this.updater = new ArchiverDataStoreUpdater(this.dataStore, this.l2TipsCache);
138
+ this.updater = new ArchiverDataStoreUpdater(this.dataStore, this.l2TipsCache, {
139
+ rollupManaLimit: l1Constants.rollupManaLimit,
140
+ });
137
141
 
138
142
  // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
139
143
  // are done as fast as possible. This then gets updated once the initial sync completes.
@@ -333,16 +337,35 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
333
337
  return Promise.resolve(this.synchronizer.getL1Timestamp());
334
338
  }
335
339
 
336
- public getL2SlotNumber(): Promise<SlotNumber | undefined> {
340
+ public getSyncedL2SlotNumber(): Promise<SlotNumber | undefined> {
337
341
  const l1Timestamp = this.synchronizer.getL1Timestamp();
338
- return Promise.resolve(l1Timestamp === undefined ? undefined : getSlotAtTimestamp(l1Timestamp, this.l1Constants));
342
+ if (l1Timestamp === undefined) {
343
+ return Promise.resolve(undefined);
344
+ }
345
+ // The synced slot is the last L2 slot whose all L1 blocks have been processed.
346
+ // If the next L1 block (at l1Timestamp + ethereumSlotDuration) falls in slot N,
347
+ // then we've fully synced slot N-1.
348
+ const nextL1BlockSlot = getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
349
+ if (Number(nextL1BlockSlot) === 0) {
350
+ return Promise.resolve(undefined);
351
+ }
352
+ return Promise.resolve(SlotNumber(nextL1BlockSlot - 1));
339
353
  }
340
354
 
341
- public getL2EpochNumber(): Promise<EpochNumber | undefined> {
342
- const l1Timestamp = this.synchronizer.getL1Timestamp();
343
- return Promise.resolve(
344
- l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(l1Timestamp, this.l1Constants),
345
- );
355
+ public async getSyncedL2EpochNumber(): Promise<EpochNumber | undefined> {
356
+ const syncedSlot = await this.getSyncedL2SlotNumber();
357
+ if (syncedSlot === undefined) {
358
+ return undefined;
359
+ }
360
+ // An epoch is fully synced when all its slots are synced.
361
+ // We check if syncedSlot is the last slot of its epoch; if so, that epoch is fully synced.
362
+ // Otherwise, only the previous epoch is fully synced.
363
+ const epoch = getEpochAtSlot(syncedSlot, this.l1Constants);
364
+ const [, endSlot] = getSlotRangeForEpoch(epoch, this.l1Constants);
365
+ if (syncedSlot >= endSlot) {
366
+ return epoch;
367
+ }
368
+ return Number(epoch) > 0 ? EpochNumber(Number(epoch) - 1) : undefined;
346
369
  }
347
370
 
348
371
  public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
@@ -450,11 +473,10 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
450
473
  this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
451
474
  await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
452
475
  }
453
- // TODO(palla/reorg): Set the finalized block when we add support for it.
454
- // const currentFinalizedBlock = currentBlocks.finalized.block.number;
455
- // if (targetL2BlockNumber < currentFinalizedBlock) {
456
- // this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
457
- // await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
458
- // }
476
+ const currentFinalizedBlock = currentBlocks.finalized.block.number;
477
+ if (targetL2BlockNumber < currentFinalizedBlock) {
478
+ this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
479
+ await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
480
+ }
459
481
  }
460
482
  }
package/src/factory.ts CHANGED
@@ -14,7 +14,6 @@ import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/prov
14
14
  import { FunctionType, decodeFunctionSignature } from '@aztec/stdlib/abi';
15
15
  import type { ArchiverEmitter } from '@aztec/stdlib/block';
16
16
  import { type ContractClassPublic, computePublicBytecodeCommitment } from '@aztec/stdlib/contract';
17
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
18
17
  import { getTelemetryClient } from '@aztec/telemetry-client';
19
18
 
20
19
  import { EventEmitter } from 'events';
@@ -32,14 +31,13 @@ export const ARCHIVER_STORE_NAME = 'archiver';
32
31
  /** Creates an archiver store. */
33
32
  export async function createArchiverStore(
34
33
  userConfig: Pick<ArchiverConfig, 'archiverStoreMapSizeKb' | 'maxLogs'> & DataStoreConfig,
35
- l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
36
34
  ) {
37
35
  const config = {
38
36
  ...userConfig,
39
37
  dataStoreMapSizeKb: userConfig.archiverStoreMapSizeKb ?? userConfig.dataStoreMapSizeKb,
40
38
  };
41
39
  const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config);
42
- return new KVArchiverDataStore(store, config.maxLogs, l1Constants);
40
+ return new KVArchiverDataStore(store, config.maxLogs);
43
41
  }
44
42
 
45
43
  /**
@@ -54,7 +52,7 @@ export async function createArchiver(
54
52
  deps: ArchiverDeps,
55
53
  opts: { blockUntilSync: boolean } = { blockUntilSync: true },
56
54
  ): Promise<Archiver> {
57
- const archiverStore = await createArchiverStore(config, { epochDuration: config.aztecEpochDuration });
55
+ const archiverStore = await createArchiverStore(config);
58
56
  await registerProtocolContracts(archiverStore);
59
57
 
60
58
  // Create Ethereum clients
@@ -85,6 +83,7 @@ export async function createArchiver(
85
83
  genesisArchiveRoot,
86
84
  slashingProposerAddress,
87
85
  targetCommitteeSize,
86
+ rollupManaLimit,
88
87
  ] = await Promise.all([
89
88
  rollup.getL1StartBlock(),
90
89
  rollup.getL1GenesisTime(),
@@ -92,6 +91,7 @@ export async function createArchiver(
92
91
  rollup.getGenesisArchiveTreeRoot(),
93
92
  rollup.getSlashingProposerAddress(),
94
93
  rollup.getTargetCommitteeSize(),
94
+ rollup.getManaLimit(),
95
95
  ] as const);
96
96
 
97
97
  const l1StartBlockHash = await publicClient
@@ -110,6 +110,7 @@ export async function createArchiver(
110
110
  proofSubmissionEpochs: Number(proofSubmissionEpochs),
111
111
  targetCommitteeSize,
112
112
  genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString()),
113
+ rollupManaLimit: Number(rollupManaLimit),
113
114
  };
114
115
 
115
116
  const archiverConfig = merge(
@@ -265,9 +265,6 @@ async function processCheckpointProposedLogs(
265
265
  checkpointNumber,
266
266
  expectedHashes,
267
267
  );
268
- const { timestamp, parentBeaconBlockRoot } = await getL1Block(publicClient, log.l1BlockNumber);
269
- const l1 = new L1PublishedData(log.l1BlockNumber, timestamp, log.l1BlockHash.toString());
270
-
271
268
  const checkpointBlobData = await getCheckpointBlobDataFromBlobs(
272
269
  blobClient,
273
270
  checkpoint.blockHash,
@@ -275,8 +272,12 @@ async function processCheckpointProposedLogs(
275
272
  checkpointNumber,
276
273
  logger,
277
274
  isHistoricalSync,
278
- parentBeaconBlockRoot,
279
- timestamp,
275
+ );
276
+
277
+ const l1 = new L1PublishedData(
278
+ log.l1BlockNumber,
279
+ await getL1BlockTime(publicClient, log.l1BlockNumber),
280
+ log.l1BlockHash.toString(),
280
281
  );
281
282
 
282
283
  retrievedCheckpoints.push({ ...checkpoint, checkpointBlobData, l1, chainId, version });
@@ -297,12 +298,9 @@ async function processCheckpointProposedLogs(
297
298
  return retrievedCheckpoints;
298
299
  }
299
300
 
300
- export async function getL1Block(
301
- publicClient: ViemPublicClient,
302
- blockNumber: bigint,
303
- ): Promise<{ timestamp: bigint; parentBeaconBlockRoot: string | undefined }> {
301
+ export async function getL1BlockTime(publicClient: ViemPublicClient, blockNumber: bigint): Promise<bigint> {
304
302
  const block = await publicClient.getBlock({ blockNumber, includeTransactions: false });
305
- return { timestamp: block.timestamp, parentBeaconBlockRoot: block.parentBeaconBlockRoot };
303
+ return block.timestamp;
306
304
  }
307
305
 
308
306
  export async function getCheckpointBlobDataFromBlobs(
@@ -312,14 +310,8 @@ export async function getCheckpointBlobDataFromBlobs(
312
310
  checkpointNumber: CheckpointNumber,
313
311
  logger: Logger,
314
312
  isHistoricalSync: boolean,
315
- parentBeaconBlockRoot?: string,
316
- l1BlockTimestamp?: bigint,
317
313
  ): Promise<CheckpointBlobData> {
318
- const blobBodies = await blobClient.getBlobSidecar(blockHash, blobHashes, {
319
- isHistoricalSync,
320
- parentBeaconBlockRoot,
321
- l1BlockTimestamp,
322
- });
314
+ const blobBodies = await blobClient.getBlobSidecar(blockHash, blobHashes, { isHistoricalSync });
323
315
  if (blobBodies.length === 0) {
324
316
  throw new NoBlobBodiesFoundError(checkpointNumber);
325
317
  }
@@ -46,9 +46,9 @@ export abstract class ArchiverDataSourceBase
46
46
 
47
47
  abstract getL2Tips(): Promise<L2Tips>;
48
48
 
49
- abstract getL2SlotNumber(): Promise<SlotNumber | undefined>;
49
+ abstract getSyncedL2SlotNumber(): Promise<SlotNumber | undefined>;
50
50
 
51
- abstract getL2EpochNumber(): Promise<EpochNumber | undefined>;
51
+ abstract getSyncedL2EpochNumber(): Promise<EpochNumber | undefined>;
52
52
 
53
53
  abstract isEpochComplete(epochNumber: EpochNumber): Promise<boolean>;
54
54
 
@@ -154,7 +154,7 @@ export abstract class ArchiverDataSourceBase
154
154
  }
155
155
 
156
156
  public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
157
- return this.store.getSettledTxReceipt(txHash);
157
+ return this.store.getSettledTxReceipt(txHash, this.l1Constants);
158
158
  }
159
159
 
160
160
  public isPendingChainInvalid(): Promise<boolean> {
@@ -1,4 +1,5 @@
1
1
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { filterAsync } from '@aztec/foundation/collection';
2
3
  import { Fr } from '@aztec/foundation/curves/bn254';
3
4
  import { createLogger } from '@aztec/foundation/log';
4
5
  import {
@@ -11,10 +12,11 @@ import {
11
12
  ContractInstanceUpdatedEvent,
12
13
  } from '@aztec/protocol-contracts/instance-registry';
13
14
  import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
15
+ import { type PublishedCheckpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
15
16
  import {
16
17
  type ExecutablePrivateFunctionWithMembershipProof,
17
18
  type UtilityFunctionWithMembershipProof,
19
+ computeContractAddressFromInstance,
18
20
  computePublicBytecodeCommitment,
19
21
  isValidPrivateFunctionMembershipProof,
20
22
  isValidUtilityFunctionMembershipProof,
@@ -48,6 +50,7 @@ export class ArchiverDataStoreUpdater {
48
50
  constructor(
49
51
  private store: KVArchiverDataStore,
50
52
  private l2TipsCache?: L2TipsCache,
53
+ private opts: { rollupManaLimit?: number } = {},
51
54
  ) {}
52
55
 
53
56
  /**
@@ -97,6 +100,10 @@ export class ArchiverDataStoreUpdater {
97
100
  checkpoints: PublishedCheckpoint[],
98
101
  pendingChainValidationStatus?: ValidateCheckpointResult,
99
102
  ): Promise<ReconcileCheckpointsResult> {
103
+ for (const checkpoint of checkpoints) {
104
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
105
+ }
106
+
100
107
  const result = await this.store.transactionAsync(async () => {
101
108
  // Before adding checkpoints, check for conflicts with local blocks if any
102
109
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
@@ -173,7 +180,7 @@ export class ArchiverDataStoreUpdater {
173
180
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
174
181
  lastAlreadyInsertedBlockNumber = blockNumber;
175
182
  } else {
176
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
183
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
177
184
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
178
185
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
179
186
  }
@@ -276,6 +283,17 @@ export class ArchiverDataStoreUpdater {
276
283
  });
277
284
  }
278
285
 
286
+ /**
287
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
288
+ * @param checkpointNumber - The checkpoint number to set as finalized.
289
+ */
290
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
291
+ await this.store.transactionAsync(async () => {
292
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
293
+ await this.l2TipsCache?.refresh();
294
+ });
295
+ }
296
+
279
297
  /** Extracts and stores contract data from a single block. */
280
298
  private addContractDataToDb(block: L2Block): Promise<boolean> {
281
299
  return this.updateContractDataOnDb(block, Operation.Store);
@@ -340,10 +358,27 @@ export class ArchiverDataStoreUpdater {
340
358
  blockNum: BlockNumber,
341
359
  operation: Operation,
342
360
  ): Promise<boolean> {
343
- const contractInstances = allLogs
361
+ const allInstances = allLogs
344
362
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
345
363
  .map(log => ContractInstancePublishedEvent.fromLog(log))
346
364
  .map(e => e.toContractInstance());
365
+
366
+ // Verify that each instance's address matches the one derived from its fields if we're adding
367
+ const contractInstances =
368
+ operation === Operation.Delete
369
+ ? allInstances
370
+ : await filterAsync(allInstances, async instance => {
371
+ const computedAddress = await computeContractAddressFromInstance(instance);
372
+ if (!computedAddress.equals(instance.address)) {
373
+ this.log.warn(
374
+ `Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
375
+ { instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
376
+ );
377
+ return false;
378
+ }
379
+ return true;
380
+ });
381
+
347
382
  if (contractInstances.length > 0) {
348
383
  contractInstances.forEach(c =>
349
384
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -441,7 +476,7 @@ export class ArchiverDataStoreUpdater {
441
476
  if (validFnCount > 0) {
442
477
  this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
443
478
  }
444
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
479
+ await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
445
480
  }
446
481
  return true;
447
482
  }
@@ -1,9 +1,6 @@
1
- import type { SlotNumber } from '@aztec/foundation/branded-types';
2
1
  import { createLogger } from '@aztec/foundation/log';
3
2
  import type { L2Block } from '@aztec/stdlib/block';
4
3
  import type { CheckpointData } from '@aztec/stdlib/checkpoint';
5
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
6
- import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
7
4
  import {
8
5
  Attributes,
9
6
  type Gauge,
@@ -41,8 +38,6 @@ export class ArchiverInstrumentation {
41
38
 
42
39
  private blockProposalTxTargetCount: UpDownCounter;
43
40
 
44
- private checkpointL1InclusionDelay: Histogram;
45
-
46
41
  private log = createLogger('archiver:instrumentation');
47
42
 
48
43
  private constructor(
@@ -90,8 +85,6 @@ export class ArchiverInstrumentation {
90
85
  },
91
86
  );
92
87
 
93
- this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
94
-
95
88
  this.dbMetrics = new LmdbMetrics(
96
89
  meter,
97
90
  {
@@ -168,17 +161,4 @@ export class ArchiverInstrumentation {
168
161
  [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: usedTrace,
169
162
  });
170
163
  }
171
-
172
- /**
173
- * Records L1 inclusion timing for a checkpoint observed on L1 (seconds into the L2 slot).
174
- */
175
- public processCheckpointL1Timing(data: {
176
- slotNumber: SlotNumber;
177
- l1Timestamp: bigint;
178
- l1Constants: Pick<L1RollupConstants, 'l1GenesisTime' | 'slotDuration'>;
179
- }): void {
180
- const slotStartTs = getTimestampForSlot(data.slotNumber, data.l1Constants);
181
- const inclusionDelaySeconds = Number(data.l1Timestamp - slotStartTs);
182
- this.checkpointL1InclusionDelay.record(inclusionDelaySeconds);
183
- }
184
164
  }
@@ -69,13 +69,18 @@ export class ArchiverL1Synchronizer implements Traceable {
69
69
  private readonly epochCache: EpochCache,
70
70
  private readonly dateProvider: DateProvider,
71
71
  private readonly instrumentation: ArchiverInstrumentation,
72
- private readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
72
+ private readonly l1Constants: L1RollupConstants & {
73
+ l1StartBlockHash: Buffer32;
74
+ genesisArchiveRoot: Fr;
75
+ },
73
76
  private readonly events: ArchiverEmitter,
74
77
  tracer: Tracer,
75
78
  l2TipsCache?: L2TipsCache,
76
79
  private readonly log: Logger = createLogger('archiver:l1-sync'),
77
80
  ) {
78
- this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache);
81
+ this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
82
+ rollupManaLimit: l1Constants.rollupManaLimit,
83
+ });
79
84
  this.tracer = tracer;
80
85
  }
81
86
 
@@ -211,6 +216,9 @@ export class ArchiverL1Synchronizer implements Traceable {
211
216
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
212
217
  }
213
218
 
219
+ // Update the finalized L2 checkpoint based on L1 finality.
220
+ await this.updateFinalizedCheckpoint();
221
+
214
222
  // After syncing has completed, update the current l1 block number and timestamp,
215
223
  // otherwise we risk announcing to the world that we've synced to a given point,
216
224
  // but the corresponding blocks have not been processed (see #12631).
@@ -226,6 +234,33 @@ export class ArchiverL1Synchronizer implements Traceable {
226
234
  });
227
235
  }
228
236
 
237
+ /** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
238
+ private async updateFinalizedCheckpoint(): Promise<void> {
239
+ try {
240
+ const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
241
+ const finalizedL1BlockNumber = finalizedL1Block.number;
242
+ const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
243
+ blockNumber: finalizedL1BlockNumber,
244
+ });
245
+ const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
246
+ if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
247
+ await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
248
+ const finalizedL2BlockNumber = await this.store.getFinalizedL2BlockNumber();
249
+ this.log.info(
250
+ `Updated finalized chain to checkpoint ${finalizedCheckpointNumber} (L2 block ${finalizedL2BlockNumber})`,
251
+ {
252
+ finalizedCheckpointNumber,
253
+ previousFinalizedCheckpointNumber: localFinalizedCheckpointNumber,
254
+ finalizedL2BlockNumber,
255
+ finalizedL1BlockNumber,
256
+ },
257
+ );
258
+ }
259
+ } catch (err) {
260
+ this.log.warn(`Failed to update finalized checkpoint: ${err}`);
261
+ }
262
+ }
263
+
229
264
  /** Prune all proposed local blocks that should have been checkpointed by now. */
230
265
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
231
266
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
@@ -796,14 +831,6 @@ export class ArchiverL1Synchronizer implements Traceable {
796
831
  );
797
832
  }
798
833
 
799
- for (const published of validCheckpoints) {
800
- this.instrumentation.processCheckpointL1Timing({
801
- slotNumber: published.checkpoint.header.slotNumber,
802
- l1Timestamp: published.l1.timestamp,
803
- l1Constants: this.l1Constants,
804
- });
805
- }
806
-
807
834
  try {
808
835
  const updatedValidationResult =
809
836
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
@@ -822,7 +849,7 @@ export class ArchiverL1Synchronizer implements Traceable {
822
849
  const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
823
850
  const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
824
851
 
825
- this.log.warn(
852
+ this.log.info(
826
853
  `Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
827
854
  { prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
828
855
  );
@@ -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 {
@@ -97,6 +97,9 @@ export class BlockStore {
97
97
  /** Stores last proven checkpoint */
98
98
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
99
99
 
100
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
101
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
102
+
100
103
  /** Stores the pending chain validation status */
101
104
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
102
105
 
@@ -111,10 +114,7 @@ export class BlockStore {
111
114
 
112
115
  #log = createLogger('archiver:block_store');
113
116
 
114
- constructor(
115
- private db: AztecAsyncKVStore,
116
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
117
- ) {
117
+ constructor(private db: AztecAsyncKVStore) {
118
118
  this.#blocks = db.openMap('archiver_blocks');
119
119
  this.#blockTxs = db.openMap('archiver_block_txs');
120
120
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -123,21 +123,27 @@ export class BlockStore {
123
123
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
124
124
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
125
125
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
126
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
126
127
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
127
128
  this.#checkpoints = db.openMap('archiver_checkpoints');
128
129
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
129
130
  }
130
131
 
131
132
  /**
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
133
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
134
+ * in an L1 block that has itself been finalized on Ethereum.
136
135
  * @returns The finalized block number.
137
136
  */
138
137
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
139
- const provenBlockNumber = await this.getProvenBlockNumber();
140
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
138
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
139
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
140
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
141
+ }
142
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
143
+ if (!checkpointStorage) {
144
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
145
+ }
146
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
141
147
  }
142
148
 
143
149
  /**
@@ -871,7 +877,10 @@ export class BlockStore {
871
877
  * @param txHash - The hash of a tx we try to get the receipt for.
872
878
  * @returns The requested tx receipt (or undefined if not found).
873
879
  */
874
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
880
+ async getSettledTxReceipt(
881
+ txHash: TxHash,
882
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
883
+ ): Promise<TxReceipt | undefined> {
875
884
  const txEffect = await this.getTxEffect(txHash);
876
885
  if (!txEffect) {
877
886
  return undefined;
@@ -880,10 +889,11 @@ export class BlockStore {
880
889
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
881
890
 
882
891
  // Use existing archiver methods to determine finalization level
883
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
892
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
884
893
  this.getProvenBlockNumber(),
885
894
  this.getCheckpointedL2BlockNumber(),
886
895
  this.getFinalizedL2BlockNumber(),
896
+ this.getBlockData(blockNumber),
887
897
  ]);
888
898
 
889
899
  let status: TxStatus;
@@ -897,6 +907,9 @@ export class BlockStore {
897
907
  status = TxStatus.PROPOSED;
898
908
  }
899
909
 
910
+ const epochNumber =
911
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
912
+
900
913
  return new TxReceipt(
901
914
  txHash,
902
915
  status,
@@ -905,6 +918,7 @@ export class BlockStore {
905
918
  txEffect.data.transactionFee.toBigInt(),
906
919
  txEffect.l2BlockHash,
907
920
  blockNumber,
921
+ epochNumber,
908
922
  );
909
923
  }
910
924
 
@@ -976,6 +990,20 @@ export class BlockStore {
976
990
  return result;
977
991
  }
978
992
 
993
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
994
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
995
+ this.getLatestCheckpointNumber(),
996
+ this.#lastFinalizedCheckpoint.getAsync(),
997
+ ]);
998
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
999
+ ? latestCheckpointNumber
1000
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1001
+ }
1002
+
1003
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1004
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1005
+ }
1006
+
979
1007
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
980
1008
  if (limit < 1) {
981
1009
  throw new Error(`Invalid limit: ${limit}`);
@@ -71,9 +71,8 @@ export class KVArchiverDataStore implements ContractDataSource {
71
71
  constructor(
72
72
  private db: AztecAsyncKVStore,
73
73
  logsMaxPageSize: number = 1000,
74
- l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
75
74
  ) {
76
- this.#blockStore = new BlockStore(db, l1Constants);
75
+ this.#blockStore = new BlockStore(db);
77
76
  this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
78
77
  this.#messageStore = new MessageStore(db);
79
78
  this.#contractClassStore = new ContractClassStore(db);
@@ -410,8 +409,11 @@ export class KVArchiverDataStore implements ContractDataSource {
410
409
  * @param txHash - The hash of a tx we try to get the receipt for.
411
410
  * @returns The requested tx receipt (or undefined if not found).
412
411
  */
413
- getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
414
- return this.#blockStore.getSettledTxReceipt(txHash);
412
+ getSettledTxReceipt(
413
+ txHash: TxHash,
414
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
415
+ ): Promise<TxReceipt | undefined> {
416
+ return this.#blockStore.getSettledTxReceipt(txHash, l1Constants);
415
417
  }
416
418
 
417
419
  /**
@@ -542,6 +544,22 @@ export class KVArchiverDataStore implements ContractDataSource {
542
544
  await this.#blockStore.setProvenCheckpointNumber(checkpointNumber);
543
545
  }
544
546
 
547
+ /**
548
+ * Gets the number of the latest finalized checkpoint processed.
549
+ * @returns The number of the latest finalized checkpoint processed.
550
+ */
551
+ getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
552
+ return this.#blockStore.getFinalizedCheckpointNumber();
553
+ }
554
+
555
+ /**
556
+ * Stores the number of the latest finalized checkpoint processed.
557
+ * @param checkpointNumber - The number of the latest finalized checkpoint processed.
558
+ */
559
+ async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
560
+ await this.#blockStore.setFinalizedCheckpointNumber(checkpointNumber);
561
+ }
562
+
545
563
  async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
546
564
  await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
547
565
  }