@aztec/archiver 0.0.1-commit.9372f48 → 0.0.1-commit.949a33fd8

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 (114) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +22 -11
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +152 -145
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +16 -4
  8. package/dest/errors.d.ts +61 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +88 -14
  11. package/dest/factory.d.ts +4 -5
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +33 -27
  14. package/dest/index.d.ts +4 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +3 -1
  17. package/dest/l1/bin/retrieve-calldata.js +36 -33
  18. package/dest/l1/calldata_retriever.d.ts +74 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +197 -260
  21. package/dest/l1/data_retrieval.d.ts +26 -17
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +42 -47
  24. package/dest/l1/spire_proposer.d.ts +5 -5
  25. package/dest/l1/spire_proposer.d.ts.map +1 -1
  26. package/dest/l1/spire_proposer.js +9 -17
  27. package/dest/l1/validate_historical_logs.d.ts +23 -0
  28. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  29. package/dest/l1/validate_historical_logs.js +108 -0
  30. package/dest/modules/data_source_base.d.ts +18 -7
  31. package/dest/modules/data_source_base.d.ts.map +1 -1
  32. package/dest/modules/data_source_base.js +51 -77
  33. package/dest/modules/data_store_updater.d.ts +35 -15
  34. package/dest/modules/data_store_updater.d.ts.map +1 -1
  35. package/dest/modules/data_store_updater.js +139 -96
  36. package/dest/modules/instrumentation.d.ts +21 -3
  37. package/dest/modules/instrumentation.d.ts.map +1 -1
  38. package/dest/modules/instrumentation.js +41 -8
  39. package/dest/modules/l1_synchronizer.d.ts +11 -9
  40. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  41. package/dest/modules/l1_synchronizer.js +304 -151
  42. package/dest/modules/validation.d.ts +4 -3
  43. package/dest/modules/validation.d.ts.map +1 -1
  44. package/dest/modules/validation.js +6 -6
  45. package/dest/store/block_store.d.ts +98 -28
  46. package/dest/store/block_store.d.ts.map +1 -1
  47. package/dest/store/block_store.js +465 -138
  48. package/dest/store/contract_class_store.d.ts +2 -3
  49. package/dest/store/contract_class_store.d.ts.map +1 -1
  50. package/dest/store/contract_class_store.js +7 -67
  51. package/dest/store/contract_instance_store.d.ts +1 -1
  52. package/dest/store/contract_instance_store.d.ts.map +1 -1
  53. package/dest/store/contract_instance_store.js +6 -2
  54. package/dest/store/kv_archiver_store.d.ts +85 -24
  55. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  56. package/dest/store/kv_archiver_store.js +106 -27
  57. package/dest/store/l2_tips_cache.d.ts +20 -0
  58. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  59. package/dest/store/l2_tips_cache.js +109 -0
  60. package/dest/store/log_store.d.ts +6 -3
  61. package/dest/store/log_store.d.ts.map +1 -1
  62. package/dest/store/log_store.js +150 -55
  63. package/dest/store/message_store.d.ts +5 -1
  64. package/dest/store/message_store.d.ts.map +1 -1
  65. package/dest/store/message_store.js +21 -9
  66. package/dest/test/fake_l1_state.d.ts +25 -1
  67. package/dest/test/fake_l1_state.d.ts.map +1 -1
  68. package/dest/test/fake_l1_state.js +166 -32
  69. package/dest/test/mock_archiver.d.ts +1 -1
  70. package/dest/test/mock_archiver.d.ts.map +1 -1
  71. package/dest/test/mock_archiver.js +3 -2
  72. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  73. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  74. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  75. package/dest/test/mock_l2_block_source.d.ts +35 -5
  76. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  77. package/dest/test/mock_l2_block_source.js +182 -89
  78. package/dest/test/mock_structs.d.ts +4 -1
  79. package/dest/test/mock_structs.d.ts.map +1 -1
  80. package/dest/test/mock_structs.js +13 -1
  81. package/dest/test/noop_l1_archiver.d.ts +4 -1
  82. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  83. package/dest/test/noop_l1_archiver.js +9 -3
  84. package/package.json +13 -13
  85. package/src/archiver.ts +187 -164
  86. package/src/config.ts +23 -2
  87. package/src/errors.ts +133 -22
  88. package/src/factory.ts +47 -24
  89. package/src/index.ts +3 -1
  90. package/src/l1/README.md +25 -68
  91. package/src/l1/bin/retrieve-calldata.ts +46 -39
  92. package/src/l1/calldata_retriever.ts +261 -379
  93. package/src/l1/data_retrieval.ts +58 -69
  94. package/src/l1/spire_proposer.ts +7 -15
  95. package/src/l1/validate_historical_logs.ts +140 -0
  96. package/src/modules/data_source_base.ts +95 -98
  97. package/src/modules/data_store_updater.ts +168 -126
  98. package/src/modules/instrumentation.ts +56 -9
  99. package/src/modules/l1_synchronizer.ts +384 -188
  100. package/src/modules/validation.ts +10 -9
  101. package/src/store/block_store.ts +569 -174
  102. package/src/store/contract_class_store.ts +8 -106
  103. package/src/store/contract_instance_store.ts +8 -5
  104. package/src/store/kv_archiver_store.ts +167 -41
  105. package/src/store/l2_tips_cache.ts +134 -0
  106. package/src/store/log_store.ts +221 -63
  107. package/src/store/message_store.ts +27 -10
  108. package/src/structs/inbox_message.ts +1 -1
  109. package/src/test/fake_l1_state.ts +213 -42
  110. package/src/test/mock_archiver.ts +3 -2
  111. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  112. package/src/test/mock_l2_block_source.ts +230 -82
  113. package/src/test/mock_structs.ts +20 -6
  114. package/src/test/noop_l1_archiver.ts +10 -2
@@ -1,30 +1,28 @@
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
- import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
9
+ import type { CommitteeAttestation, L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
10
+ import {
11
+ type L1PublishedData,
12
+ type ProposedCheckpointInput,
13
+ type PublishedCheckpoint,
14
+ validateCheckpoint,
15
+ } from '@aztec/stdlib/checkpoint';
15
16
  import {
16
- type ExecutablePrivateFunctionWithMembershipProof,
17
- type UtilityFunctionWithMembershipProof,
18
- computePublicBytecodeCommitment,
19
- isValidPrivateFunctionMembershipProof,
20
- isValidUtilityFunctionMembershipProof,
17
+ type ContractClassPublicWithCommitment,
18
+ computeContractAddressFromInstance,
19
+ computeContractClassId,
21
20
  } from '@aztec/stdlib/contract';
22
21
  import type { ContractClassLog, PrivateLog, PublicLog } from '@aztec/stdlib/logs';
23
22
  import type { UInt64 } from '@aztec/stdlib/types';
24
23
 
25
- import groupBy from 'lodash.groupby';
26
-
27
24
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
25
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
28
26
 
29
27
  /** Operation type for contract data updates. */
30
28
  enum Operation {
@@ -44,60 +42,80 @@ type ReconcileCheckpointsResult = {
44
42
  export class ArchiverDataStoreUpdater {
45
43
  private readonly log = createLogger('archiver:store_updater');
46
44
 
47
- constructor(private store: KVArchiverDataStore) {}
45
+ constructor(
46
+ private store: KVArchiverDataStore,
47
+ private l2TipsCache?: L2TipsCache,
48
+ private opts: { rollupManaLimit?: number } = {},
49
+ ) {}
48
50
 
49
51
  /**
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.
52
+ * Adds a proposed block to the store with contract class/instance extraction from logs.
53
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
54
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
54
55
  *
55
- * @param blocks - The proposed L2 blocks to add.
56
+ * @param block - The proposed L2 block to add.
56
57
  * @param pendingChainValidationStatus - Optional validation status to set.
57
58
  * @returns True if the operation is successful.
58
59
  */
59
- public addProposedBlocks(
60
- blocks: L2Block[],
60
+ public async addProposedBlock(
61
+ block: L2Block,
61
62
  pendingChainValidationStatus?: ValidateCheckpointResult,
62
63
  ): Promise<boolean> {
63
- return this.store.transactionAsync(async () => {
64
- await this.store.addProposedBlocks(blocks);
64
+ const result = await this.store.transactionAsync(async () => {
65
+ await this.store.addProposedBlock(block);
65
66
 
66
67
  const opResults = await Promise.all([
67
68
  // Update the pending chain validation status if provided
68
69
  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)),
70
+ // Add any logs emitted during the retrieved block
71
+ this.store.addLogs([block]),
72
+ // Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
73
+ this.addContractDataToDb(block),
73
74
  ]);
74
75
 
76
+ await this.l2TipsCache?.refresh();
75
77
  return opResults.every(Boolean);
76
78
  });
79
+ return result;
77
80
  }
78
81
 
79
82
  /**
80
83
  * Reconciles local blocks with incoming checkpoints from L1.
81
84
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
82
85
  * 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.
86
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
87
+ * If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
88
+ * as the added checkpoints so both updates are applied atomically.
85
89
  *
86
- * @param checkpoints - The published checkpoints to add.
90
+ * @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
87
91
  * @param pendingChainValidationStatus - Optional validation status to set.
92
+ * @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
88
93
  * @returns Result with information about any pruned blocks.
89
94
  */
90
- public addCheckpoints(
95
+ public async addCheckpoints(
91
96
  checkpoints: PublishedCheckpoint[],
92
97
  pendingChainValidationStatus?: ValidateCheckpointResult,
98
+ promoteProposed?: {
99
+ l1: L1PublishedData;
100
+ attestations: CommitteeAttestation[];
101
+ checkpoint: PublishedCheckpoint;
102
+ },
103
+ evictProposedFrom?: CheckpointNumber,
93
104
  ): Promise<ReconcileCheckpointsResult> {
94
- return this.store.transactionAsync(async () => {
105
+ for (const checkpoint of checkpoints) {
106
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
107
+ }
108
+ if (promoteProposed) {
109
+ validateCheckpoint(promoteProposed.checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
110
+ }
111
+
112
+ const result = await this.store.transactionAsync(async () => {
95
113
  // Before adding checkpoints, check for conflicts with local blocks if any
96
114
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
97
115
 
98
116
  await this.store.addCheckpoints(checkpoints);
99
117
 
100
- // Filter out blocks that were already inserted via addProposedBlocks() to avoid duplicating logs/contract data
118
+ // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
101
119
  const newBlocks = checkpoints
102
120
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
103
121
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
@@ -109,10 +127,32 @@ export class ArchiverDataStoreUpdater {
109
127
  this.store.addLogs(newBlocks),
110
128
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
111
129
  ...newBlocks.map(block => this.addContractDataToDb(block)),
130
+ // Promote the proposed checkpoint if requested (uses explicit checkpoint number)
131
+ promoteProposed
132
+ ? this.store.promoteProposedToCheckpointed(
133
+ promoteProposed.checkpoint.checkpoint.number,
134
+ promoteProposed.l1,
135
+ promoteProposed.attestations,
136
+ promoteProposed.checkpoint.checkpoint.archive.root,
137
+ )
138
+ : undefined,
139
+ // Evict pending checkpoints that diverged from what L1 mined
140
+ evictProposedFrom !== undefined ? this.store.evictProposedCheckpointsFrom(evictProposedFrom) : undefined,
112
141
  ]);
113
142
 
143
+ await this.l2TipsCache?.refresh();
114
144
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
115
145
  });
146
+ return result;
147
+ }
148
+
149
+ public async addProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput) {
150
+ const result = await this.store.transactionAsync(async () => {
151
+ await this.store.addProposedCheckpoint(proposedCheckpoint);
152
+ await this.l2TipsCache?.refresh();
153
+ });
154
+
155
+ return result;
116
156
  }
117
157
 
118
158
  /**
@@ -165,7 +205,7 @@ export class ArchiverDataStoreUpdater {
165
205
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
166
206
  lastAlreadyInsertedBlockNumber = blockNumber;
167
207
  } else {
168
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
208
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
169
209
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
170
210
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
171
211
  }
@@ -197,8 +237,8 @@ export class ArchiverDataStoreUpdater {
197
237
  * @returns The removed blocks.
198
238
  * @throws Error if any block to be removed is checkpointed.
199
239
  */
200
- public removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
201
- return this.store.transactionAsync(async () => {
240
+ public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
241
+ const result = await this.store.transactionAsync(async () => {
202
242
  // Verify we're only removing uncheckpointed blocks
203
243
  const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
204
244
  if (blockNumber < lastCheckpointedBlockNumber) {
@@ -207,8 +247,15 @@ export class ArchiverDataStoreUpdater {
207
247
  );
208
248
  }
209
249
 
210
- return await this.removeBlocksAfter(blockNumber);
250
+ const result = await this.removeBlocksAfter(blockNumber);
251
+
252
+ // Clear all pending proposed checkpoints since their blocks have been pruned
253
+ await this.store.deleteProposedCheckpoints();
254
+
255
+ await this.l2TipsCache?.refresh();
256
+ return result;
211
257
  });
258
+ return result;
212
259
  }
213
260
 
214
261
  /**
@@ -238,17 +285,42 @@ export class ArchiverDataStoreUpdater {
238
285
  * @returns True if the operation is successful.
239
286
  */
240
287
  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
- ]);
288
+ return await this.store.transactionAsync(async () => {
289
+ const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
290
+
291
+ const opResults = await Promise.all([
292
+ // Prune rolls back to the last proven block, which is by definition valid
293
+ this.store.setPendingChainValidationStatus({ valid: true }),
294
+ // Remove contract data for all blocks being removed
295
+ ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
296
+ this.store.deleteLogs(blocksRemoved),
297
+ ]);
250
298
 
251
- return opResults.every(Boolean);
299
+ await this.l2TipsCache?.refresh();
300
+ return opResults.every(Boolean);
301
+ });
302
+ }
303
+
304
+ /**
305
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
306
+ * @param checkpointNumber - The checkpoint number to set as proven.
307
+ */
308
+ public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
309
+ await this.store.transactionAsync(async () => {
310
+ await this.store.setProvenCheckpointNumber(checkpointNumber);
311
+ await this.l2TipsCache?.refresh();
312
+ });
313
+ }
314
+
315
+ /**
316
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
317
+ * @param checkpointNumber - The checkpoint number to set as finalized.
318
+ */
319
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
320
+ await this.store.transactionAsync(async () => {
321
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
322
+ await this.l2TipsCache?.refresh();
323
+ });
252
324
  }
253
325
 
254
326
  /** Extracts and stores contract data from a single block. */
@@ -272,9 +344,6 @@ export class ArchiverDataStoreUpdater {
272
344
  this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
273
345
  this.updateDeployedContractInstances(privateLogs, block.number, operation),
274
346
  this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
275
- operation === Operation.Store
276
- ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
277
- : Promise.resolve(true),
278
347
  ])
279
348
  ).every(Boolean);
280
349
  }
@@ -291,18 +360,37 @@ export class ArchiverDataStoreUpdater {
291
360
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
292
361
  .map(log => ContractClassPublishedEvent.fromLog(log));
293
362
 
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) {
363
+ if (operation == Operation.Delete) {
364
+ const contractClasses = contractClassPublishedEvents.map(e => e.toContractClassPublic());
365
+ if (contractClasses.length > 0) {
366
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
304
367
  return await this.store.deleteContractClasses(contractClasses, blockNum);
305
368
  }
369
+ return true;
370
+ }
371
+
372
+ // Compute bytecode commitments and validate class IDs in a single pass.
373
+ const contractClasses: ContractClassPublicWithCommitment[] = [];
374
+ for (const event of contractClassPublishedEvents) {
375
+ const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
376
+ const computedClassId = await computeContractClassId({
377
+ artifactHash: contractClass.artifactHash,
378
+ privateFunctionsRoot: contractClass.privateFunctionsRoot,
379
+ publicBytecodeCommitment: contractClass.publicBytecodeCommitment,
380
+ });
381
+ if (!computedClassId.equals(contractClass.id)) {
382
+ this.log.warn(
383
+ `Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`,
384
+ { blockNum, contractClassId: event.contractClassId.toString() },
385
+ );
386
+ continue;
387
+ }
388
+ contractClasses.push(contractClass);
389
+ }
390
+
391
+ if (contractClasses.length > 0) {
392
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
393
+ return await this.store.addContractClasses(contractClasses, blockNum);
306
394
  }
307
395
  return true;
308
396
  }
@@ -315,10 +403,27 @@ export class ArchiverDataStoreUpdater {
315
403
  blockNum: BlockNumber,
316
404
  operation: Operation,
317
405
  ): Promise<boolean> {
318
- const contractInstances = allLogs
406
+ const allInstances = allLogs
319
407
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
320
408
  .map(log => ContractInstancePublishedEvent.fromLog(log))
321
409
  .map(e => e.toContractInstance());
410
+
411
+ // Verify that each instance's address matches the one derived from its fields if we're adding
412
+ const contractInstances =
413
+ operation === Operation.Delete
414
+ ? allInstances
415
+ : await filterAsync(allInstances, async instance => {
416
+ const computedAddress = await computeContractAddressFromInstance(instance);
417
+ if (!computedAddress.equals(instance.address)) {
418
+ this.log.warn(
419
+ `Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
420
+ { instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
421
+ );
422
+ return false;
423
+ }
424
+ return true;
425
+ });
426
+
322
427
  if (contractInstances.length > 0) {
323
428
  contractInstances.forEach(c =>
324
429
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -357,67 +462,4 @@ export class ArchiverDataStoreUpdater {
357
462
  }
358
463
  return true;
359
464
  }
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
465
  }
@@ -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;
@@ -27,6 +32,7 @@ export class ArchiverInstrumentation {
27
32
  private pruneCount: UpDownCounter;
28
33
 
29
34
  private syncDurationPerBlock: Histogram;
35
+ private syncDurationPerCheckpoint: Histogram;
30
36
  private syncBlockCount: UpDownCounter;
31
37
  private manaPerBlock: Histogram;
32
38
  private txsPerBlock: Histogram;
@@ -36,6 +42,9 @@ export class ArchiverInstrumentation {
36
42
 
37
43
  private blockProposalTxTargetCount: UpDownCounter;
38
44
 
45
+ private checkpointL1InclusionDelay: Histogram;
46
+ private checkpointPromotedCount: UpDownCounter;
47
+
39
48
  private log = createLogger('archiver:instrumentation');
40
49
 
41
50
  private constructor(
@@ -47,6 +56,8 @@ export class ArchiverInstrumentation {
47
56
 
48
57
  this.blockHeight = meter.createGauge(Metrics.ARCHIVER_BLOCK_HEIGHT);
49
58
 
59
+ this.checkpointHeight = meter.createGauge(Metrics.ARCHIVER_CHECKPOINT_HEIGHT);
60
+
50
61
  this.l1BlockHeight = meter.createGauge(Metrics.ARCHIVER_L1_BLOCK_HEIGHT);
51
62
 
52
63
  this.txCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_TOTAL_TXS);
@@ -59,6 +70,8 @@ export class ArchiverInstrumentation {
59
70
 
60
71
  this.syncDurationPerBlock = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_BLOCK);
61
72
 
73
+ this.syncDurationPerCheckpoint = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_CHECKPOINT);
74
+
62
75
  this.syncBlockCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_SYNC_BLOCK_COUNT);
63
76
 
64
77
  this.manaPerBlock = meter.createHistogram(Metrics.ARCHIVER_MANA_PER_BLOCK);
@@ -81,6 +94,10 @@ export class ArchiverInstrumentation {
81
94
  },
82
95
  );
83
96
 
97
+ this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
98
+
99
+ this.checkpointPromotedCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_CHECKPOINT_PROMOTED_COUNT);
100
+
84
101
  this.dbMetrics = new LmdbMetrics(
85
102
  meter,
86
103
  {
@@ -102,16 +119,26 @@ export class ArchiverInstrumentation {
102
119
  return this.telemetry.isEnabled();
103
120
  }
104
121
 
105
- public processNewBlocks(syncTimePerBlock: number, blocks: L2Block[]) {
122
+ public processNewProposedBlock(syncTimePerBlock: number, block: L2Block) {
123
+ const attrs = { [Attributes.STATUS]: 'proposed' };
124
+ this.blockHeight.record(block.number, attrs);
106
125
  this.syncDurationPerBlock.record(Math.ceil(syncTimePerBlock));
107
- this.blockHeight.record(Math.max(...blocks.map(b => b.number)));
108
- this.syncBlockCount.add(blocks.length);
109
126
 
110
- for (const block of blocks) {
111
- this.txCount.add(block.body.txEffects.length);
112
- this.txsPerBlock.record(block.body.txEffects.length);
113
- this.manaPerBlock.record(block.header.totalManaUsed.toNumber() / 1e6);
127
+ // Per block metrics
128
+ this.txCount.add(block.body.txEffects.length);
129
+ this.txsPerBlock.record(block.body.txEffects.length);
130
+ this.manaPerBlock.record(block.header.totalManaUsed.toNumber() / 1e6);
131
+ }
132
+
133
+ public processNewCheckpointedBlocks(syncTimePerCheckpoint: number, blocks: L2Block[]) {
134
+ if (blocks.length === 0) {
135
+ return;
114
136
  }
137
+
138
+ this.syncDurationPerCheckpoint.record(Math.ceil(syncTimePerCheckpoint));
139
+ this.blockHeight.record(Math.max(...blocks.map(b => b.number)));
140
+ this.checkpointHeight.record(Math.max(...blocks.map(b => b.checkpointNumber)));
141
+ this.syncBlockCount.add(blocks.length);
115
142
  }
116
143
 
117
144
  public processNewMessages(count: number, syncPerMessageMs: number) {
@@ -127,8 +154,10 @@ export class ArchiverInstrumentation {
127
154
  this.pruneDuration.record(Math.ceil(duration));
128
155
  }
129
156
 
130
- public updateLastProvenBlock(blockNumber: number) {
131
- this.blockHeight.record(blockNumber, { [Attributes.STATUS]: 'proven' });
157
+ public updateLastProvenCheckpoint(checkpoint: CheckpointData) {
158
+ const lastBlockNumberInCheckpoint = checkpoint.startBlock + checkpoint.blockCount - 1;
159
+ this.blockHeight.record(lastBlockNumberInCheckpoint, { [Attributes.STATUS]: 'proven' });
160
+ this.checkpointHeight.record(checkpoint.checkpointNumber, { [Attributes.STATUS]: 'proven' });
132
161
  }
133
162
 
134
163
  public processProofsVerified(logs: { proverId: string; l2BlockNumber: bigint; delay: bigint }[]) {
@@ -154,4 +183,22 @@ export class ArchiverInstrumentation {
154
183
  [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: usedTrace,
155
184
  });
156
185
  }
186
+
187
+ /** Records a checkpoint promoted from proposed (blob fetch skipped). */
188
+ public processCheckpointPromoted() {
189
+ this.checkpointPromotedCount.add(1);
190
+ }
191
+
192
+ /**
193
+ * Records L1 inclusion timing for a checkpoint observed on L1 (seconds into the L2 slot).
194
+ */
195
+ public processCheckpointL1Timing(data: {
196
+ slotNumber: SlotNumber;
197
+ l1Timestamp: bigint;
198
+ l1Constants: Pick<L1RollupConstants, 'l1GenesisTime' | 'slotDuration'>;
199
+ }): void {
200
+ const slotStartTs = getTimestampForSlot(data.slotNumber, data.l1Constants);
201
+ const inclusionDelaySeconds = Number(data.l1Timestamp - slotStartTs);
202
+ this.checkpointL1InclusionDelay.record(inclusionDelaySeconds);
203
+ }
157
204
  }