@aztec/archiver 0.0.1-commit.c2595eba → 0.0.1-commit.c2eed6949

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 (100) hide show
  1. package/dest/archiver.d.ts +7 -5
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +73 -111
  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 +4 -5
  11. package/dest/factory.d.ts.map +1 -1
  12. package/dest/factory.js +31 -26
  13. package/dest/index.d.ts +2 -1
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +1 -0
  16. package/dest/l1/bin/retrieve-calldata.js +36 -33
  17. package/dest/l1/calldata_retriever.d.ts +73 -50
  18. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  19. package/dest/l1/calldata_retriever.js +190 -259
  20. package/dest/l1/data_retrieval.d.ts +9 -9
  21. package/dest/l1/data_retrieval.d.ts.map +1 -1
  22. package/dest/l1/data_retrieval.js +22 -20
  23. package/dest/l1/spire_proposer.d.ts +5 -5
  24. package/dest/l1/spire_proposer.d.ts.map +1 -1
  25. package/dest/l1/spire_proposer.js +9 -17
  26. package/dest/modules/data_source_base.d.ts +12 -7
  27. package/dest/modules/data_source_base.d.ts.map +1 -1
  28. package/dest/modules/data_source_base.js +33 -77
  29. package/dest/modules/data_store_updater.d.ts +24 -12
  30. package/dest/modules/data_store_updater.d.ts.map +1 -1
  31. package/dest/modules/data_store_updater.js +116 -94
  32. package/dest/modules/instrumentation.d.ts +15 -2
  33. package/dest/modules/instrumentation.d.ts.map +1 -1
  34. package/dest/modules/instrumentation.js +19 -2
  35. package/dest/modules/l1_synchronizer.d.ts +5 -8
  36. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  37. package/dest/modules/l1_synchronizer.js +50 -15
  38. package/dest/store/block_store.d.ts +29 -26
  39. package/dest/store/block_store.d.ts.map +1 -1
  40. package/dest/store/block_store.js +130 -78
  41. package/dest/store/contract_class_store.d.ts +2 -3
  42. package/dest/store/contract_class_store.d.ts.map +1 -1
  43. package/dest/store/contract_class_store.js +16 -72
  44. package/dest/store/contract_instance_store.d.ts +1 -1
  45. package/dest/store/contract_instance_store.d.ts.map +1 -1
  46. package/dest/store/contract_instance_store.js +6 -2
  47. package/dest/store/kv_archiver_store.d.ts +45 -21
  48. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  49. package/dest/store/kv_archiver_store.js +52 -21
  50. package/dest/store/l2_tips_cache.d.ts +19 -0
  51. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  52. package/dest/store/l2_tips_cache.js +89 -0
  53. package/dest/store/log_store.d.ts +6 -3
  54. package/dest/store/log_store.d.ts.map +1 -1
  55. package/dest/store/log_store.js +148 -51
  56. package/dest/store/message_store.d.ts +5 -1
  57. package/dest/store/message_store.d.ts.map +1 -1
  58. package/dest/store/message_store.js +14 -1
  59. package/dest/test/fake_l1_state.d.ts +13 -1
  60. package/dest/test/fake_l1_state.d.ts.map +1 -1
  61. package/dest/test/fake_l1_state.js +95 -23
  62. package/dest/test/mock_archiver.d.ts +1 -1
  63. package/dest/test/mock_archiver.d.ts.map +1 -1
  64. package/dest/test/mock_archiver.js +3 -2
  65. package/dest/test/mock_l2_block_source.d.ts +21 -5
  66. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  67. package/dest/test/mock_l2_block_source.js +132 -86
  68. package/dest/test/mock_structs.d.ts +4 -1
  69. package/dest/test/mock_structs.d.ts.map +1 -1
  70. package/dest/test/mock_structs.js +13 -1
  71. package/dest/test/noop_l1_archiver.d.ts +4 -1
  72. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  73. package/dest/test/noop_l1_archiver.js +5 -1
  74. package/package.json +13 -13
  75. package/src/archiver.ts +88 -131
  76. package/src/config.ts +8 -1
  77. package/src/errors.ts +40 -24
  78. package/src/factory.ts +46 -24
  79. package/src/index.ts +1 -0
  80. package/src/l1/README.md +25 -68
  81. package/src/l1/bin/retrieve-calldata.ts +46 -39
  82. package/src/l1/calldata_retriever.ts +249 -379
  83. package/src/l1/data_retrieval.ts +24 -26
  84. package/src/l1/spire_proposer.ts +7 -15
  85. package/src/modules/data_source_base.ts +64 -98
  86. package/src/modules/data_store_updater.ts +125 -124
  87. package/src/modules/instrumentation.ts +29 -2
  88. package/src/modules/l1_synchronizer.ts +61 -24
  89. package/src/store/block_store.ts +157 -105
  90. package/src/store/contract_class_store.ts +16 -110
  91. package/src/store/contract_instance_store.ts +8 -5
  92. package/src/store/kv_archiver_store.ts +78 -35
  93. package/src/store/l2_tips_cache.ts +89 -0
  94. package/src/store/log_store.ts +219 -58
  95. package/src/store/message_store.ts +20 -1
  96. package/src/test/fake_l1_state.ts +125 -26
  97. package/src/test/mock_archiver.ts +3 -2
  98. package/src/test/mock_l2_block_source.ts +173 -81
  99. package/src/test/mock_structs.ts +20 -6
  100. package/src/test/noop_l1_archiver.ts +7 -1
@@ -1,30 +1,23 @@
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,
12
8
  } from '@aztec/protocol-contracts/instance-registry';
13
9
  import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
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';
20
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
28
21
 
29
22
  /** Operation type for contract data updates. */
30
23
  enum Operation {
@@ -44,60 +37,68 @@ type ReconcileCheckpointsResult = {
44
37
  export class ArchiverDataStoreUpdater {
45
38
  private readonly log = createLogger('archiver:store_updater');
46
39
 
47
- constructor(private store: KVArchiverDataStore) {}
40
+ constructor(
41
+ private store: KVArchiverDataStore,
42
+ private l2TipsCache?: L2TipsCache,
43
+ private opts: { rollupManaLimit?: number } = {},
44
+ ) {}
48
45
 
49
46
  /**
50
- * Adds proposed blocks to the store with contract class/instance extraction from logs.
51
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
52
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
53
- * 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.
54
50
  *
55
- * @param blocks - The proposed L2 blocks to add.
51
+ * @param block - The proposed L2 block to add.
56
52
  * @param pendingChainValidationStatus - Optional validation status to set.
57
53
  * @returns True if the operation is successful.
58
54
  */
59
- public addProposedBlocks(
60
- blocks: L2Block[],
55
+ public async addProposedBlock(
56
+ block: L2Block,
61
57
  pendingChainValidationStatus?: ValidateCheckpointResult,
62
58
  ): Promise<boolean> {
63
- return this.store.transactionAsync(async () => {
64
- await this.store.addProposedBlocks(blocks);
59
+ const result = await this.store.transactionAsync(async () => {
60
+ await this.store.addProposedBlock(block);
65
61
 
66
62
  const opResults = await Promise.all([
67
63
  // Update the pending chain validation status if provided
68
64
  pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
69
- // Add any logs emitted during the retrieved blocks
70
- this.store.addLogs(blocks),
71
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
72
- ...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),
73
69
  ]);
74
70
 
71
+ await this.l2TipsCache?.refresh();
75
72
  return opResults.every(Boolean);
76
73
  });
74
+ return result;
77
75
  }
78
76
 
79
77
  /**
80
78
  * Reconciles local blocks with incoming checkpoints from L1.
81
79
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
82
80
  * Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
83
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
84
- * and individually broadcasted functions from the checkpoint block logs.
81
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
85
82
  *
86
83
  * @param checkpoints - The published checkpoints to add.
87
84
  * @param pendingChainValidationStatus - Optional validation status to set.
88
85
  * @returns Result with information about any pruned blocks.
89
86
  */
90
- public addCheckpoints(
87
+ public async addCheckpoints(
91
88
  checkpoints: PublishedCheckpoint[],
92
89
  pendingChainValidationStatus?: ValidateCheckpointResult,
93
90
  ): Promise<ReconcileCheckpointsResult> {
94
- return this.store.transactionAsync(async () => {
91
+ for (const checkpoint of checkpoints) {
92
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
93
+ }
94
+
95
+ const result = await this.store.transactionAsync(async () => {
95
96
  // Before adding checkpoints, check for conflicts with local blocks if any
96
97
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
97
98
 
98
99
  await this.store.addCheckpoints(checkpoints);
99
100
 
100
- // 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
101
102
  const newBlocks = checkpoints
102
103
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
103
104
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
@@ -111,8 +112,10 @@ export class ArchiverDataStoreUpdater {
111
112
  ...newBlocks.map(block => this.addContractDataToDb(block)),
112
113
  ]);
113
114
 
115
+ await this.l2TipsCache?.refresh();
114
116
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
115
117
  });
118
+ return result;
116
119
  }
117
120
 
118
121
  /**
@@ -165,7 +168,7 @@ export class ArchiverDataStoreUpdater {
165
168
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
166
169
  lastAlreadyInsertedBlockNumber = blockNumber;
167
170
  } else {
168
- 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);
169
172
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
170
173
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
171
174
  }
@@ -197,8 +200,8 @@ export class ArchiverDataStoreUpdater {
197
200
  * @returns The removed blocks.
198
201
  * @throws Error if any block to be removed is checkpointed.
199
202
  */
200
- public removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
201
- return this.store.transactionAsync(async () => {
203
+ public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
204
+ const result = await this.store.transactionAsync(async () => {
202
205
  // Verify we're only removing uncheckpointed blocks
203
206
  const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
204
207
  if (blockNumber < lastCheckpointedBlockNumber) {
@@ -207,8 +210,11 @@ export class ArchiverDataStoreUpdater {
207
210
  );
208
211
  }
209
212
 
210
- return await this.removeBlocksAfter(blockNumber);
213
+ const result = await this.removeBlocksAfter(blockNumber);
214
+ await this.l2TipsCache?.refresh();
215
+ return result;
211
216
  });
217
+ return result;
212
218
  }
213
219
 
214
220
  /**
@@ -238,17 +244,42 @@ export class ArchiverDataStoreUpdater {
238
244
  * @returns True if the operation is successful.
239
245
  */
240
246
  public async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
241
- const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
242
-
243
- const opResults = await Promise.all([
244
- // Prune rolls back to the last proven block, which is by definition valid
245
- this.store.setPendingChainValidationStatus({ valid: true }),
246
- // Remove contract data for all blocks being removed
247
- ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
248
- this.store.deleteLogs(blocksRemoved),
249
- ]);
247
+ return await this.store.transactionAsync(async () => {
248
+ const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
249
+
250
+ const opResults = await Promise.all([
251
+ // Prune rolls back to the last proven block, which is by definition valid
252
+ this.store.setPendingChainValidationStatus({ valid: true }),
253
+ // Remove contract data for all blocks being removed
254
+ ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
255
+ this.store.deleteLogs(blocksRemoved),
256
+ ]);
257
+
258
+ await this.l2TipsCache?.refresh();
259
+ return opResults.every(Boolean);
260
+ });
261
+ }
262
+
263
+ /**
264
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
265
+ * @param checkpointNumber - The checkpoint number to set as proven.
266
+ */
267
+ public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
268
+ await this.store.transactionAsync(async () => {
269
+ await this.store.setProvenCheckpointNumber(checkpointNumber);
270
+ await this.l2TipsCache?.refresh();
271
+ });
272
+ }
250
273
 
251
- return opResults.every(Boolean);
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
+ });
252
283
  }
253
284
 
254
285
  /** Extracts and stores contract data from a single block. */
@@ -272,9 +303,6 @@ export class ArchiverDataStoreUpdater {
272
303
  this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
273
304
  this.updateDeployedContractInstances(privateLogs, block.number, operation),
274
305
  this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
275
- operation === Operation.Store
276
- ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
277
- : Promise.resolve(true),
278
306
  ])
279
307
  ).every(Boolean);
280
308
  }
@@ -291,18 +319,37 @@ export class ArchiverDataStoreUpdater {
291
319
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
292
320
  .map(log => ContractClassPublishedEvent.fromLog(log));
293
321
 
294
- const contractClasses = await Promise.all(contractClassPublishedEvents.map(e => e.toContractClassPublic()));
295
- if (contractClasses.length > 0) {
296
- contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
297
- if (operation == Operation.Store) {
298
- // TODO: Will probably want to create some worker threads to compute these bytecode commitments as they are expensive
299
- const commitments = await Promise.all(
300
- contractClasses.map(c => computePublicBytecodeCommitment(c.packedBytecode)),
301
- );
302
- return await this.store.addContractClasses(contractClasses, commitments, blockNum);
303
- } 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()}`));
304
326
  return await this.store.deleteContractClasses(contractClasses, blockNum);
305
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);
306
353
  }
307
354
  return true;
308
355
  }
@@ -315,10 +362,27 @@ export class ArchiverDataStoreUpdater {
315
362
  blockNum: BlockNumber,
316
363
  operation: Operation,
317
364
  ): Promise<boolean> {
318
- const contractInstances = allLogs
365
+ const allInstances = allLogs
319
366
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
320
367
  .map(log => ContractInstancePublishedEvent.fromLog(log))
321
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
+
322
386
  if (contractInstances.length > 0) {
323
387
  contractInstances.forEach(c =>
324
388
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -357,67 +421,4 @@ export class ArchiverDataStoreUpdater {
357
421
  }
358
422
  return true;
359
423
  }
360
-
361
- /**
362
- * Stores the functions that were broadcasted individually.
363
- *
364
- * @dev Beware that there is not a delete variant of this, since they are added to contract classes
365
- * and will be deleted as part of the class if needed.
366
- */
367
- private async storeBroadcastedIndividualFunctions(
368
- allLogs: ContractClassLog[],
369
- _blockNum: BlockNumber,
370
- ): Promise<boolean> {
371
- // Filter out private and utility function broadcast events
372
- const privateFnEvents = allLogs
373
- .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
374
- .map(log => PrivateFunctionBroadcastedEvent.fromLog(log));
375
- const utilityFnEvents = allLogs
376
- .filter(log => UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log))
377
- .map(log => UtilityFunctionBroadcastedEvent.fromLog(log));
378
-
379
- // Group all events by contract class id
380
- for (const [classIdString, classEvents] of Object.entries(
381
- groupBy([...privateFnEvents, ...utilityFnEvents], e => e.contractClassId.toString()),
382
- )) {
383
- const contractClassId = Fr.fromHexString(classIdString);
384
- const contractClass = await this.store.getContractClass(contractClassId);
385
- if (!contractClass) {
386
- this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
387
- continue;
388
- }
389
-
390
- // Split private and utility functions, and filter out invalid ones
391
- const allFns = classEvents.map(e => e.toFunctionWithMembershipProof());
392
- const privateFns = allFns.filter(
393
- (fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'utilityFunctionsTreeRoot' in fn,
394
- );
395
- const utilityFns = allFns.filter(
396
- (fn): fn is UtilityFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
397
- );
398
-
399
- const privateFunctionsWithValidity = await Promise.all(
400
- privateFns.map(async fn => ({ fn, valid: await isValidPrivateFunctionMembershipProof(fn, contractClass) })),
401
- );
402
- const validPrivateFns = privateFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
403
- const utilityFunctionsWithValidity = await Promise.all(
404
- utilityFns.map(async fn => ({
405
- fn,
406
- valid: await isValidUtilityFunctionMembershipProof(fn, contractClass),
407
- })),
408
- );
409
- const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
410
- const validFnCount = validPrivateFns.length + validUtilityFns.length;
411
- if (validFnCount !== allFns.length) {
412
- this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
413
- }
414
-
415
- // Store the functions in the contract class in a single operation
416
- if (validFnCount > 0) {
417
- this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
418
- }
419
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
420
- }
421
- return true;
422
- }
423
424
  }
@@ -1,5 +1,9 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
1
2
  import { createLogger } from '@aztec/foundation/log';
2
3
  import type { L2Block } from '@aztec/stdlib/block';
4
+ import type { CheckpointData } from '@aztec/stdlib/checkpoint';
5
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
6
+ import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
3
7
  import {
4
8
  Attributes,
5
9
  type Gauge,
@@ -17,6 +21,7 @@ export class ArchiverInstrumentation {
17
21
  public readonly tracer: Tracer;
18
22
 
19
23
  private blockHeight: Gauge;
24
+ private checkpointHeight: Gauge;
20
25
  private txCount: UpDownCounter;
21
26
  private l1BlockHeight: Gauge;
22
27
  private proofsSubmittedDelay: Histogram;
@@ -36,6 +41,8 @@ export class ArchiverInstrumentation {
36
41
 
37
42
  private blockProposalTxTargetCount: UpDownCounter;
38
43
 
44
+ private checkpointL1InclusionDelay: Histogram;
45
+
39
46
  private log = createLogger('archiver:instrumentation');
40
47
 
41
48
  private constructor(
@@ -47,6 +54,8 @@ export class ArchiverInstrumentation {
47
54
 
48
55
  this.blockHeight = meter.createGauge(Metrics.ARCHIVER_BLOCK_HEIGHT);
49
56
 
57
+ this.checkpointHeight = meter.createGauge(Metrics.ARCHIVER_CHECKPOINT_HEIGHT);
58
+
50
59
  this.l1BlockHeight = meter.createGauge(Metrics.ARCHIVER_L1_BLOCK_HEIGHT);
51
60
 
52
61
  this.txCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_TOTAL_TXS);
@@ -81,6 +90,8 @@ export class ArchiverInstrumentation {
81
90
  },
82
91
  );
83
92
 
93
+ this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
94
+
84
95
  this.dbMetrics = new LmdbMetrics(
85
96
  meter,
86
97
  {
@@ -105,6 +116,7 @@ export class ArchiverInstrumentation {
105
116
  public processNewBlocks(syncTimePerBlock: number, blocks: L2Block[]) {
106
117
  this.syncDurationPerBlock.record(Math.ceil(syncTimePerBlock));
107
118
  this.blockHeight.record(Math.max(...blocks.map(b => b.number)));
119
+ this.checkpointHeight.record(Math.max(...blocks.map(b => b.checkpointNumber)));
108
120
  this.syncBlockCount.add(blocks.length);
109
121
 
110
122
  for (const block of blocks) {
@@ -127,8 +139,10 @@ export class ArchiverInstrumentation {
127
139
  this.pruneDuration.record(Math.ceil(duration));
128
140
  }
129
141
 
130
- public updateLastProvenBlock(blockNumber: number) {
131
- this.blockHeight.record(blockNumber, { [Attributes.STATUS]: 'proven' });
142
+ public updateLastProvenCheckpoint(checkpoint: CheckpointData) {
143
+ const lastBlockNumberInCheckpoint = checkpoint.startBlock + checkpoint.blockCount - 1;
144
+ this.blockHeight.record(lastBlockNumberInCheckpoint, { [Attributes.STATUS]: 'proven' });
145
+ this.checkpointHeight.record(checkpoint.checkpointNumber, { [Attributes.STATUS]: 'proven' });
132
146
  }
133
147
 
134
148
  public processProofsVerified(logs: { proverId: string; l2BlockNumber: bigint; delay: bigint }[]) {
@@ -154,4 +168,17 @@ export class ArchiverInstrumentation {
154
168
  [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: usedTrace,
155
169
  });
156
170
  }
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
+ }
157
184
  }
@@ -1,15 +1,14 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
2
  import { EpochCache } from '@aztec/epoch-cache';
3
3
  import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
4
- import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
5
4
  import type { L1BlockId } from '@aztec/ethereum/l1-types';
6
5
  import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
6
+ import { asyncPool } from '@aztec/foundation/async-pool';
7
7
  import { maxBigint } from '@aztec/foundation/bigint';
8
8
  import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
9
9
  import { Buffer32 } from '@aztec/foundation/buffer';
10
10
  import { pick } from '@aztec/foundation/collection';
11
11
  import { Fr } from '@aztec/foundation/curves/bn254';
12
- import { EthAddress } from '@aztec/foundation/eth-address';
13
12
  import { type Logger, createLogger } from '@aztec/foundation/log';
14
13
  import { count } from '@aztec/foundation/string';
15
14
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
@@ -28,6 +27,7 @@ import {
28
27
  retrievedToPublishedCheckpoint,
29
28
  } from '../l1/data_retrieval.js';
30
29
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
30
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
31
31
  import type { InboxMessage } from '../structs/inbox_message.js';
32
32
  import { ArchiverDataStoreUpdater } from './data_store_updater.js';
33
33
  import type { ArchiverInstrumentation } from './instrumentation.js';
@@ -60,10 +60,6 @@ export class ArchiverL1Synchronizer implements Traceable {
60
60
  private readonly debugClient: ViemPublicDebugClient,
61
61
  private readonly rollup: RollupContract,
62
62
  private readonly inbox: InboxContract,
63
- private readonly l1Addresses: Pick<
64
- L1ContractAddresses,
65
- 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
66
- > & { slashingProposerAddress: EthAddress },
67
63
  private readonly store: KVArchiverDataStore,
68
64
  private config: {
69
65
  batchSize: number;
@@ -74,12 +70,18 @@ export class ArchiverL1Synchronizer implements Traceable {
74
70
  private readonly epochCache: EpochCache,
75
71
  private readonly dateProvider: DateProvider,
76
72
  private readonly instrumentation: ArchiverInstrumentation,
77
- private readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
73
+ private readonly l1Constants: L1RollupConstants & {
74
+ l1StartBlockHash: Buffer32;
75
+ genesisArchiveRoot: Fr;
76
+ },
78
77
  private readonly events: ArchiverEmitter,
79
78
  tracer: Tracer,
79
+ l2TipsCache?: L2TipsCache,
80
80
  private readonly log: Logger = createLogger('archiver:l1-sync'),
81
81
  ) {
82
- this.updater = new ArchiverDataStoreUpdater(this.store);
82
+ this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
83
+ rollupManaLimit: l1Constants.rollupManaLimit,
84
+ });
83
85
  this.tracer = tracer;
84
86
  }
85
87
 
@@ -215,6 +217,9 @@ export class ArchiverL1Synchronizer implements Traceable {
215
217
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
216
218
  }
217
219
 
220
+ // Update the finalized L2 checkpoint based on L1 finality.
221
+ await this.updateFinalizedCheckpoint();
222
+
218
223
  // After syncing has completed, update the current l1 block number and timestamp,
219
224
  // otherwise we risk announcing to the world that we've synced to a given point,
220
225
  // but the corresponding blocks have not been processed (see #12631).
@@ -230,6 +235,27 @@ export class ArchiverL1Synchronizer implements Traceable {
230
235
  });
231
236
  }
232
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
+
233
259
  /** Prune all proposed local blocks that should have been checkpointed by now. */
234
260
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
235
261
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
@@ -308,17 +334,20 @@ export class ArchiverL1Synchronizer implements Traceable {
308
334
 
309
335
  const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
310
336
 
311
- const checkpointPromises = Array.from({ length: checkpointsToUnwind })
312
- .fill(0)
313
- .map((_, i) => this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
314
- const checkpoints = await Promise.all(checkpointPromises);
315
-
316
- const blockPromises = await Promise.all(
317
- checkpoints
318
- .filter(isDefined)
319
- .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,
320
343
  );
321
- 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();
322
351
 
323
352
  // Emit an event for listening services to react to the chain prune
324
353
  this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
@@ -366,6 +395,7 @@ export class ArchiverL1Synchronizer implements Traceable {
366
395
  const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
367
396
  const localLastMessage = await this.store.getLastL1ToL2Message();
368
397
  const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
398
+ await this.store.setInboxTreeInProgress(remoteMessagesState.treeInProgress);
369
399
 
370
400
  this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
371
401
  localMessagesInserted,
@@ -550,7 +580,7 @@ export class ArchiverL1Synchronizer implements Traceable {
550
580
  if (provenCheckpointNumber === 0) {
551
581
  const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
552
582
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
553
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
583
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
554
584
  this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
555
585
  }
556
586
  }
@@ -582,13 +612,13 @@ export class ArchiverL1Synchronizer implements Traceable {
582
612
  ) {
583
613
  const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
584
614
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
585
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
615
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
586
616
  this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
587
617
  const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
588
618
  const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1Constants);
589
619
  const lastBlockNumberInCheckpoint =
590
620
  localCheckpointForDestinationProvenCheckpointNumber.startBlock +
591
- localCheckpointForDestinationProvenCheckpointNumber.numBlocks -
621
+ localCheckpointForDestinationProvenCheckpointNumber.blockCount -
592
622
  1;
593
623
 
594
624
  this.events.emit(L2BlockSourceEvents.L2BlockProven, {
@@ -597,7 +627,7 @@ export class ArchiverL1Synchronizer implements Traceable {
597
627
  slotNumber: provenSlotNumber,
598
628
  epochNumber: provenEpochNumber,
599
629
  });
600
- this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
630
+ this.instrumentation.updateLastProvenCheckpoint(localCheckpointForDestinationProvenCheckpointNumber);
601
631
  } else {
602
632
  this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
603
633
  }
@@ -706,7 +736,6 @@ export class ArchiverL1Synchronizer implements Traceable {
706
736
  this.blobClient,
707
737
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
708
738
  searchEndBlock,
709
- this.l1Addresses,
710
739
  this.instrumentation,
711
740
  this.log,
712
741
  !initialSyncComplete, // isHistoricalSync
@@ -801,6 +830,14 @@ export class ArchiverL1Synchronizer implements Traceable {
801
830
  );
802
831
  }
803
832
 
833
+ for (const published of validCheckpoints) {
834
+ this.instrumentation.processCheckpointL1Timing({
835
+ slotNumber: published.checkpoint.header.slotNumber,
836
+ l1Timestamp: published.l1.timestamp,
837
+ l1Constants: this.l1Constants,
838
+ });
839
+ }
840
+
804
841
  try {
805
842
  const updatedValidationResult =
806
843
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
@@ -819,7 +856,7 @@ export class ArchiverL1Synchronizer implements Traceable {
819
856
  const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
820
857
  const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
821
858
 
822
- this.log.warn(
859
+ this.log.info(
823
860
  `Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
824
861
  { prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
825
862
  );