@aztec/archiver 0.0.1-commit.9d2bcf6d → 0.0.1-commit.9d619b6c6

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 +13 -8
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +90 -114
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +15 -3
  8. package/dest/errors.d.ts +50 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +67 -16
  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 +73 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +191 -259
  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 +14 -7
  31. package/dest/modules/data_source_base.d.ts.map +1 -1
  32. package/dest/modules/data_source_base.js +39 -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 +137 -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 +10 -9
  40. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  41. package/dest/modules/l1_synchronizer.js +279 -150
  42. package/dest/modules/validation.d.ts +1 -1
  43. package/dest/modules/validation.d.ts.map +1 -1
  44. package/dest/modules/validation.js +2 -2
  45. package/dest/store/block_store.d.ts +73 -28
  46. package/dest/store/block_store.d.ts.map +1 -1
  47. package/dest/store/block_store.js +395 -136
  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 +67 -24
  55. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  56. package/dest/store/kv_archiver_store.js +82 -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 +24 -1
  67. package/dest/test/fake_l1_state.d.ts.map +1 -1
  68. package/dest/test/fake_l1_state.js +145 -28
  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 +26 -5
  76. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  77. package/dest/test/mock_l2_block_source.js +160 -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 +120 -137
  86. package/src/config.ts +22 -2
  87. package/src/errors.ts +104 -26
  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 +250 -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 +78 -98
  97. package/src/modules/data_store_updater.ts +164 -126
  98. package/src/modules/instrumentation.ts +56 -9
  99. package/src/modules/l1_synchronizer.ts +357 -188
  100. package/src/modules/validation.ts +2 -2
  101. package/src/store/block_store.ts +503 -172
  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 +130 -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 +193 -32
  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 +209 -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,79 @@ 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
+ },
93
103
  ): Promise<ReconcileCheckpointsResult> {
94
- return this.store.transactionAsync(async () => {
104
+ for (const checkpoint of checkpoints) {
105
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
106
+ }
107
+ if (promoteProposed) {
108
+ validateCheckpoint(promoteProposed.checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
109
+ }
110
+
111
+ const result = await this.store.transactionAsync(async () => {
95
112
  // Before adding checkpoints, check for conflicts with local blocks if any
96
113
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
97
114
 
98
115
  await this.store.addCheckpoints(checkpoints);
99
116
 
100
- // Filter out blocks that were already inserted via addProposedBlocks() to avoid duplicating logs/contract data
117
+ // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
101
118
  const newBlocks = checkpoints
102
119
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
103
120
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
@@ -109,10 +126,29 @@ export class ArchiverDataStoreUpdater {
109
126
  this.store.addLogs(newBlocks),
110
127
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
111
128
  ...newBlocks.map(block => this.addContractDataToDb(block)),
129
+ // Promote the proposed checkpoint if requested
130
+ promoteProposed
131
+ ? this.store.promoteProposedToCheckpointed(
132
+ promoteProposed.l1,
133
+ promoteProposed.attestations,
134
+ promoteProposed.checkpoint.checkpoint.archive.root,
135
+ )
136
+ : undefined,
112
137
  ]);
113
138
 
139
+ await this.l2TipsCache?.refresh();
114
140
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
115
141
  });
142
+ return result;
143
+ }
144
+
145
+ public async setProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput) {
146
+ const result = await this.store.transactionAsync(async () => {
147
+ await this.store.setProposedCheckpoint(proposedCheckpoint);
148
+ await this.l2TipsCache?.refresh();
149
+ });
150
+
151
+ return result;
116
152
  }
117
153
 
118
154
  /**
@@ -165,7 +201,7 @@ export class ArchiverDataStoreUpdater {
165
201
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
166
202
  lastAlreadyInsertedBlockNumber = blockNumber;
167
203
  } else {
168
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
204
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
169
205
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
170
206
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
171
207
  }
@@ -197,8 +233,8 @@ export class ArchiverDataStoreUpdater {
197
233
  * @returns The removed blocks.
198
234
  * @throws Error if any block to be removed is checkpointed.
199
235
  */
200
- public removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
201
- return this.store.transactionAsync(async () => {
236
+ public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
237
+ const result = await this.store.transactionAsync(async () => {
202
238
  // Verify we're only removing uncheckpointed blocks
203
239
  const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
204
240
  if (blockNumber < lastCheckpointedBlockNumber) {
@@ -207,8 +243,15 @@ export class ArchiverDataStoreUpdater {
207
243
  );
208
244
  }
209
245
 
210
- return await this.removeBlocksAfter(blockNumber);
246
+ const result = await this.removeBlocksAfter(blockNumber);
247
+
248
+ // Clear the proposed checkpoint if it exists, since its blocks have been pruned
249
+ await this.store.deleteProposedCheckpoint();
250
+
251
+ await this.l2TipsCache?.refresh();
252
+ return result;
211
253
  });
254
+ return result;
212
255
  }
213
256
 
214
257
  /**
@@ -238,17 +281,42 @@ export class ArchiverDataStoreUpdater {
238
281
  * @returns True if the operation is successful.
239
282
  */
240
283
  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
- ]);
284
+ return await this.store.transactionAsync(async () => {
285
+ const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
286
+
287
+ const opResults = await Promise.all([
288
+ // Prune rolls back to the last proven block, which is by definition valid
289
+ this.store.setPendingChainValidationStatus({ valid: true }),
290
+ // Remove contract data for all blocks being removed
291
+ ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
292
+ this.store.deleteLogs(blocksRemoved),
293
+ ]);
250
294
 
251
- return opResults.every(Boolean);
295
+ await this.l2TipsCache?.refresh();
296
+ return opResults.every(Boolean);
297
+ });
298
+ }
299
+
300
+ /**
301
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
302
+ * @param checkpointNumber - The checkpoint number to set as proven.
303
+ */
304
+ public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
305
+ await this.store.transactionAsync(async () => {
306
+ await this.store.setProvenCheckpointNumber(checkpointNumber);
307
+ await this.l2TipsCache?.refresh();
308
+ });
309
+ }
310
+
311
+ /**
312
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
313
+ * @param checkpointNumber - The checkpoint number to set as finalized.
314
+ */
315
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
316
+ await this.store.transactionAsync(async () => {
317
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
318
+ await this.l2TipsCache?.refresh();
319
+ });
252
320
  }
253
321
 
254
322
  /** Extracts and stores contract data from a single block. */
@@ -272,9 +340,6 @@ export class ArchiverDataStoreUpdater {
272
340
  this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
273
341
  this.updateDeployedContractInstances(privateLogs, block.number, operation),
274
342
  this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
275
- operation === Operation.Store
276
- ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
277
- : Promise.resolve(true),
278
343
  ])
279
344
  ).every(Boolean);
280
345
  }
@@ -291,18 +356,37 @@ export class ArchiverDataStoreUpdater {
291
356
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
292
357
  .map(log => ContractClassPublishedEvent.fromLog(log));
293
358
 
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) {
359
+ if (operation == Operation.Delete) {
360
+ const contractClasses = contractClassPublishedEvents.map(e => e.toContractClassPublic());
361
+ if (contractClasses.length > 0) {
362
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
304
363
  return await this.store.deleteContractClasses(contractClasses, blockNum);
305
364
  }
365
+ return true;
366
+ }
367
+
368
+ // Compute bytecode commitments and validate class IDs in a single pass.
369
+ const contractClasses: ContractClassPublicWithCommitment[] = [];
370
+ for (const event of contractClassPublishedEvents) {
371
+ const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
372
+ const computedClassId = await computeContractClassId({
373
+ artifactHash: contractClass.artifactHash,
374
+ privateFunctionsRoot: contractClass.privateFunctionsRoot,
375
+ publicBytecodeCommitment: contractClass.publicBytecodeCommitment,
376
+ });
377
+ if (!computedClassId.equals(contractClass.id)) {
378
+ this.log.warn(
379
+ `Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`,
380
+ { blockNum, contractClassId: event.contractClassId.toString() },
381
+ );
382
+ continue;
383
+ }
384
+ contractClasses.push(contractClass);
385
+ }
386
+
387
+ if (contractClasses.length > 0) {
388
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
389
+ return await this.store.addContractClasses(contractClasses, blockNum);
306
390
  }
307
391
  return true;
308
392
  }
@@ -315,10 +399,27 @@ export class ArchiverDataStoreUpdater {
315
399
  blockNum: BlockNumber,
316
400
  operation: Operation,
317
401
  ): Promise<boolean> {
318
- const contractInstances = allLogs
402
+ const allInstances = allLogs
319
403
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
320
404
  .map(log => ContractInstancePublishedEvent.fromLog(log))
321
405
  .map(e => e.toContractInstance());
406
+
407
+ // Verify that each instance's address matches the one derived from its fields if we're adding
408
+ const contractInstances =
409
+ operation === Operation.Delete
410
+ ? allInstances
411
+ : await filterAsync(allInstances, async instance => {
412
+ const computedAddress = await computeContractAddressFromInstance(instance);
413
+ if (!computedAddress.equals(instance.address)) {
414
+ this.log.warn(
415
+ `Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
416
+ { instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
417
+ );
418
+ return false;
419
+ }
420
+ return true;
421
+ });
422
+
322
423
  if (contractInstances.length > 0) {
323
424
  contractInstances.forEach(c =>
324
425
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -357,67 +458,4 @@ export class ArchiverDataStoreUpdater {
357
458
  }
358
459
  return true;
359
460
  }
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
461
  }
@@ -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
  }