@aztec/archiver 0.0.1-commit.cd76b27 → 0.0.1-commit.ce4f8c4f2

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 (88) hide show
  1. package/dest/archiver.d.ts +3 -4
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +67 -23
  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 +25 -25
  13. package/dest/l1/bin/retrieve-calldata.js +32 -28
  14. package/dest/l1/calldata_retriever.d.ts +70 -53
  15. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  16. package/dest/l1/calldata_retriever.js +178 -260
  17. package/dest/l1/data_retrieval.d.ts +7 -8
  18. package/dest/l1/data_retrieval.d.ts.map +1 -1
  19. package/dest/l1/data_retrieval.js +18 -17
  20. package/dest/l1/spire_proposer.d.ts +5 -5
  21. package/dest/l1/spire_proposer.d.ts.map +1 -1
  22. package/dest/l1/spire_proposer.js +9 -17
  23. package/dest/modules/data_source_base.d.ts +5 -5
  24. package/dest/modules/data_source_base.d.ts.map +1 -1
  25. package/dest/modules/data_source_base.js +5 -5
  26. package/dest/modules/data_store_updater.d.ts +17 -12
  27. package/dest/modules/data_store_updater.d.ts.map +1 -1
  28. package/dest/modules/data_store_updater.js +78 -77
  29. package/dest/modules/instrumentation.d.ts +12 -1
  30. package/dest/modules/instrumentation.d.ts.map +1 -1
  31. package/dest/modules/instrumentation.js +10 -0
  32. package/dest/modules/l1_synchronizer.d.ts +3 -7
  33. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  34. package/dest/modules/l1_synchronizer.js +46 -11
  35. package/dest/store/block_store.d.ts +12 -13
  36. package/dest/store/block_store.d.ts.map +1 -1
  37. package/dest/store/block_store.js +61 -61
  38. package/dest/store/contract_class_store.d.ts +2 -3
  39. package/dest/store/contract_class_store.d.ts.map +1 -1
  40. package/dest/store/contract_class_store.js +7 -67
  41. package/dest/store/contract_instance_store.d.ts +1 -1
  42. package/dest/store/contract_instance_store.d.ts.map +1 -1
  43. package/dest/store/contract_instance_store.js +6 -2
  44. package/dest/store/kv_archiver_store.d.ts +28 -18
  45. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  46. package/dest/store/kv_archiver_store.js +34 -21
  47. package/dest/store/log_store.d.ts +6 -3
  48. package/dest/store/log_store.d.ts.map +1 -1
  49. package/dest/store/log_store.js +93 -16
  50. package/dest/store/message_store.d.ts +5 -1
  51. package/dest/store/message_store.d.ts.map +1 -1
  52. package/dest/store/message_store.js +14 -1
  53. package/dest/test/fake_l1_state.d.ts +10 -1
  54. package/dest/test/fake_l1_state.d.ts.map +1 -1
  55. package/dest/test/fake_l1_state.js +81 -15
  56. package/dest/test/mock_l2_block_source.d.ts +4 -3
  57. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  58. package/dest/test/mock_l2_block_source.js +7 -4
  59. package/dest/test/mock_structs.d.ts +4 -1
  60. package/dest/test/mock_structs.d.ts.map +1 -1
  61. package/dest/test/mock_structs.js +13 -1
  62. package/dest/test/noop_l1_archiver.d.ts +4 -1
  63. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  64. package/dest/test/noop_l1_archiver.js +5 -1
  65. package/package.json +13 -13
  66. package/src/archiver.ts +80 -23
  67. package/src/config.ts +8 -1
  68. package/src/errors.ts +40 -24
  69. package/src/factory.ts +23 -16
  70. package/src/l1/README.md +25 -68
  71. package/src/l1/bin/retrieve-calldata.ts +40 -27
  72. package/src/l1/calldata_retriever.ts +231 -383
  73. package/src/l1/data_retrieval.ts +20 -25
  74. package/src/l1/spire_proposer.ts +7 -15
  75. package/src/modules/data_source_base.ts +11 -6
  76. package/src/modules/data_store_updater.ts +83 -107
  77. package/src/modules/instrumentation.ts +20 -0
  78. package/src/modules/l1_synchronizer.ts +55 -20
  79. package/src/store/block_store.ts +72 -69
  80. package/src/store/contract_class_store.ts +8 -106
  81. package/src/store/contract_instance_store.ts +8 -5
  82. package/src/store/kv_archiver_store.ts +43 -32
  83. package/src/store/log_store.ts +126 -27
  84. package/src/store/message_store.ts +20 -1
  85. package/src/test/fake_l1_state.ts +110 -19
  86. package/src/test/mock_l2_block_source.ts +15 -3
  87. package/src/test/mock_structs.ts +20 -6
  88. package/src/test/noop_l1_archiver.ts +7 -1
@@ -157,11 +157,6 @@ export async function retrieveCheckpointsFromRollup(
157
157
  blobClient: BlobClientInterface,
158
158
  searchStartBlock: bigint,
159
159
  searchEndBlock: bigint,
160
- contractAddresses: {
161
- governanceProposerAddress: EthAddress;
162
- slashFactoryAddress?: EthAddress;
163
- slashingProposerAddress: EthAddress;
164
- },
165
160
  instrumentation: ArchiverInstrumentation,
166
161
  logger: Logger = createLogger('archiver'),
167
162
  isHistoricalSync: boolean = false,
@@ -205,7 +200,6 @@ export async function retrieveCheckpointsFromRollup(
205
200
  blobClient,
206
201
  checkpointProposedLogs,
207
202
  rollupConstants,
208
- contractAddresses,
209
203
  instrumentation,
210
204
  logger,
211
205
  isHistoricalSync,
@@ -226,7 +220,6 @@ export async function retrieveCheckpointsFromRollup(
226
220
  * @param blobClient - The blob client client for fetching blob data.
227
221
  * @param logs - CheckpointProposed logs.
228
222
  * @param rollupConstants - The rollup constants (chainId, version, targetCommitteeSize).
229
- * @param contractAddresses - The contract addresses (governanceProposerAddress, slashFactoryAddress, slashingProposerAddress).
230
223
  * @param instrumentation - The archiver instrumentation instance.
231
224
  * @param logger - The logger instance.
232
225
  * @param isHistoricalSync - Whether this is a historical sync.
@@ -239,11 +232,6 @@ async function processCheckpointProposedLogs(
239
232
  blobClient: BlobClientInterface,
240
233
  logs: CheckpointProposedLog[],
241
234
  { chainId, version, targetCommitteeSize }: { chainId: Fr; version: Fr; targetCommitteeSize: number },
242
- contractAddresses: {
243
- governanceProposerAddress: EthAddress;
244
- slashFactoryAddress?: EthAddress;
245
- slashingProposerAddress: EthAddress;
246
- },
247
235
  instrumentation: ArchiverInstrumentation,
248
236
  logger: Logger,
249
237
  isHistoricalSync: boolean,
@@ -255,7 +243,7 @@ async function processCheckpointProposedLogs(
255
243
  targetCommitteeSize,
256
244
  instrumentation,
257
245
  logger,
258
- { ...contractAddresses, rollupAddress: EthAddress.fromString(rollup.address) },
246
+ EthAddress.fromString(rollup.address),
259
247
  );
260
248
 
261
249
  await asyncPool(10, logs, async log => {
@@ -266,10 +254,9 @@ async function processCheckpointProposedLogs(
266
254
 
267
255
  // The value from the event and contract will match only if the checkpoint is in the chain.
268
256
  if (archive.equals(archiveFromChain)) {
269
- // Build expected hashes object (fields may be undefined for backwards compatibility with older events)
270
257
  const expectedHashes = {
271
- attestationsHash: log.args.attestationsHash?.toString(),
272
- payloadDigest: log.args.payloadDigest?.toString(),
258
+ attestationsHash: log.args.attestationsHash.toString() as Hex,
259
+ payloadDigest: log.args.payloadDigest.toString() as Hex,
273
260
  };
274
261
 
275
262
  const checkpoint = await calldataRetriever.getCheckpointFromRollupTx(
@@ -278,6 +265,9 @@ async function processCheckpointProposedLogs(
278
265
  checkpointNumber,
279
266
  expectedHashes,
280
267
  );
268
+ const { timestamp, parentBeaconBlockRoot } = await getL1Block(publicClient, log.l1BlockNumber);
269
+ const l1 = new L1PublishedData(log.l1BlockNumber, timestamp, log.l1BlockHash.toString());
270
+
281
271
  const checkpointBlobData = await getCheckpointBlobDataFromBlobs(
282
272
  blobClient,
283
273
  checkpoint.blockHash,
@@ -285,12 +275,8 @@ async function processCheckpointProposedLogs(
285
275
  checkpointNumber,
286
276
  logger,
287
277
  isHistoricalSync,
288
- );
289
-
290
- const l1 = new L1PublishedData(
291
- log.l1BlockNumber,
292
- await getL1BlockTime(publicClient, log.l1BlockNumber),
293
- log.l1BlockHash.toString(),
278
+ parentBeaconBlockRoot,
279
+ timestamp,
294
280
  );
295
281
 
296
282
  retrievedCheckpoints.push({ ...checkpoint, checkpointBlobData, l1, chainId, version });
@@ -311,9 +297,12 @@ async function processCheckpointProposedLogs(
311
297
  return retrievedCheckpoints;
312
298
  }
313
299
 
314
- export async function getL1BlockTime(publicClient: ViemPublicClient, blockNumber: bigint): Promise<bigint> {
300
+ export async function getL1Block(
301
+ publicClient: ViemPublicClient,
302
+ blockNumber: bigint,
303
+ ): Promise<{ timestamp: bigint; parentBeaconBlockRoot: string | undefined }> {
315
304
  const block = await publicClient.getBlock({ blockNumber, includeTransactions: false });
316
- return block.timestamp;
305
+ return { timestamp: block.timestamp, parentBeaconBlockRoot: block.parentBeaconBlockRoot };
317
306
  }
318
307
 
319
308
  export async function getCheckpointBlobDataFromBlobs(
@@ -323,8 +312,14 @@ export async function getCheckpointBlobDataFromBlobs(
323
312
  checkpointNumber: CheckpointNumber,
324
313
  logger: Logger,
325
314
  isHistoricalSync: boolean,
315
+ parentBeaconBlockRoot?: string,
316
+ l1BlockTimestamp?: bigint,
326
317
  ): Promise<CheckpointBlobData> {
327
- const blobBodies = await blobClient.getBlobSidecar(blockHash, blobHashes, { isHistoricalSync });
318
+ const blobBodies = await blobClient.getBlobSidecar(blockHash, blobHashes, {
319
+ isHistoricalSync,
320
+ parentBeaconBlockRoot,
321
+ l1BlockTimestamp,
322
+ });
328
323
  if (blobBodies.length === 0) {
329
324
  throw new NoBlobBodiesFoundError(checkpointNumber);
330
325
  }
@@ -87,17 +87,17 @@ export async function verifyProxyImplementation(
87
87
  /**
88
88
  * Attempts to decode transaction as a Spire Proposer Multicall.
89
89
  * Spire Proposer is a proxy contract that wraps multiple calls.
90
- * Returns the target address and calldata of the wrapped call if validation succeeds and there is a single call.
90
+ * Returns all wrapped calls if validation succeeds (caller handles hash matching to find the propose call).
91
91
  * @param tx - The transaction to decode
92
92
  * @param publicClient - The viem public client for proxy verification
93
93
  * @param logger - Logger instance
94
- * @returns Object with 'to' and 'data' of the wrapped call, or undefined if validation fails
94
+ * @returns Array of wrapped calls with 'to' and 'data', or undefined if not a valid Spire Proposer tx
95
95
  */
96
- export async function getCallFromSpireProposer(
96
+ export async function getCallsFromSpireProposer(
97
97
  tx: Transaction,
98
98
  publicClient: { getStorageAt: (params: { address: Hex; slot: Hex }) => Promise<Hex | undefined> },
99
99
  logger: Logger,
100
- ): Promise<{ to: Hex; data: Hex } | undefined> {
100
+ ): Promise<{ to: Hex; data: Hex }[] | undefined> {
101
101
  const txHash = tx.hash;
102
102
 
103
103
  try {
@@ -141,17 +141,9 @@ export async function getCallFromSpireProposer(
141
141
 
142
142
  const [calls] = spireArgs;
143
143
 
144
- // Validate exactly ONE call (see ./README.md for rationale)
145
- if (calls.length !== 1) {
146
- logger.warn(`Spire Proposer multicall must contain exactly one call (got ${calls.length})`, { txHash });
147
- return undefined;
148
- }
149
-
150
- const call = calls[0];
151
-
152
- // Successfully extracted the single wrapped call
153
- logger.trace(`Decoded Spire Proposer with single call to ${call.target}`, { txHash });
154
- return { to: call.target, data: call.data };
144
+ // Return all wrapped calls (hash matching in the caller determines which is the propose call)
145
+ logger.trace(`Decoded Spire Proposer with ${calls.length} call(s)`, { txHash });
146
+ return calls.map(call => ({ to: call.target, data: call.data }));
155
147
  } catch (err) {
156
148
  // Any decoding error triggers fallback to trace
157
149
  logger.warn(`Failed to decode Spire Proposer: ${err}`, { txHash });
@@ -46,9 +46,9 @@ export abstract class ArchiverDataSourceBase
46
46
 
47
47
  abstract getL2Tips(): Promise<L2Tips>;
48
48
 
49
- abstract getL2SlotNumber(): Promise<SlotNumber | undefined>;
49
+ abstract getSyncedL2SlotNumber(): Promise<SlotNumber | undefined>;
50
50
 
51
- abstract getL2EpochNumber(): Promise<EpochNumber | undefined>;
51
+ abstract getSyncedL2EpochNumber(): Promise<EpochNumber | undefined>;
52
52
 
53
53
  abstract isEpochComplete(epochNumber: EpochNumber): Promise<boolean>;
54
54
 
@@ -154,7 +154,7 @@ export abstract class ArchiverDataSourceBase
154
154
  }
155
155
 
156
156
  public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
157
- return this.store.getSettledTxReceipt(txHash);
157
+ return this.store.getSettledTxReceipt(txHash, this.l1Constants);
158
158
  }
159
159
 
160
160
  public isPendingChainInvalid(): Promise<boolean> {
@@ -165,16 +165,21 @@ export abstract class ArchiverDataSourceBase
165
165
  return (await this.store.getPendingChainValidationStatus()) ?? { valid: true };
166
166
  }
167
167
 
168
- public getPrivateLogsByTags(tags: SiloedTag[], page?: number): Promise<TxScopedL2Log[][]> {
169
- return this.store.getPrivateLogsByTags(tags, page);
168
+ public getPrivateLogsByTags(
169
+ tags: SiloedTag[],
170
+ page?: number,
171
+ upToBlockNumber?: BlockNumber,
172
+ ): Promise<TxScopedL2Log[][]> {
173
+ return this.store.getPrivateLogsByTags(tags, page, upToBlockNumber);
170
174
  }
171
175
 
172
176
  public getPublicLogsByTagsFromContract(
173
177
  contractAddress: AztecAddress,
174
178
  tags: Tag[],
175
179
  page?: number,
180
+ upToBlockNumber?: BlockNumber,
176
181
  ): Promise<TxScopedL2Log[][]> {
177
- return this.store.getPublicLogsByTagsFromContract(contractAddress, tags, page);
182
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
178
183
  }
179
184
 
180
185
  public getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
@@ -1,29 +1,21 @@
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';
28
20
  import type { L2TipsCache } from '../store/l2_tips_cache.js';
29
21
 
@@ -48,32 +40,32 @@ export class ArchiverDataStoreUpdater {
48
40
  constructor(
49
41
  private store: KVArchiverDataStore,
50
42
  private l2TipsCache?: L2TipsCache,
43
+ private opts: { rollupManaLimit?: number } = {},
51
44
  ) {}
52
45
 
53
46
  /**
54
- * Adds proposed blocks to the store with contract class/instance extraction from logs.
55
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
56
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
57
- * 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.
58
50
  *
59
- * @param blocks - The proposed L2 blocks to add.
51
+ * @param block - The proposed L2 block to add.
60
52
  * @param pendingChainValidationStatus - Optional validation status to set.
61
53
  * @returns True if the operation is successful.
62
54
  */
63
- public async addProposedBlocks(
64
- blocks: L2Block[],
55
+ public async addProposedBlock(
56
+ block: L2Block,
65
57
  pendingChainValidationStatus?: ValidateCheckpointResult,
66
58
  ): Promise<boolean> {
67
59
  const result = await this.store.transactionAsync(async () => {
68
- await this.store.addProposedBlocks(blocks);
60
+ await this.store.addProposedBlock(block);
69
61
 
70
62
  const opResults = await Promise.all([
71
63
  // Update the pending chain validation status if provided
72
64
  pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
73
- // Add any logs emitted during the retrieved blocks
74
- this.store.addLogs(blocks),
75
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
76
- ...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),
77
69
  ]);
78
70
 
79
71
  await this.l2TipsCache?.refresh();
@@ -86,8 +78,7 @@ export class ArchiverDataStoreUpdater {
86
78
  * Reconciles local blocks with incoming checkpoints from L1.
87
79
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
88
80
  * Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
89
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
90
- * and individually broadcasted functions from the checkpoint block logs.
81
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
91
82
  *
92
83
  * @param checkpoints - The published checkpoints to add.
93
84
  * @param pendingChainValidationStatus - Optional validation status to set.
@@ -97,13 +88,17 @@ export class ArchiverDataStoreUpdater {
97
88
  checkpoints: PublishedCheckpoint[],
98
89
  pendingChainValidationStatus?: ValidateCheckpointResult,
99
90
  ): Promise<ReconcileCheckpointsResult> {
91
+ for (const checkpoint of checkpoints) {
92
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
93
+ }
94
+
100
95
  const result = await this.store.transactionAsync(async () => {
101
96
  // Before adding checkpoints, check for conflicts with local blocks if any
102
97
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
103
98
 
104
99
  await this.store.addCheckpoints(checkpoints);
105
100
 
106
- // 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
107
102
  const newBlocks = checkpoints
108
103
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
109
104
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
@@ -173,7 +168,7 @@ export class ArchiverDataStoreUpdater {
173
168
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
174
169
  lastAlreadyInsertedBlockNumber = blockNumber;
175
170
  } else {
176
- 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);
177
172
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
178
173
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
179
174
  }
@@ -276,6 +271,17 @@ export class ArchiverDataStoreUpdater {
276
271
  });
277
272
  }
278
273
 
274
+ /**
275
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
276
+ * @param checkpointNumber - The checkpoint number to set as finalized.
277
+ */
278
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
279
+ await this.store.transactionAsync(async () => {
280
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
281
+ await this.l2TipsCache?.refresh();
282
+ });
283
+ }
284
+
279
285
  /** Extracts and stores contract data from a single block. */
280
286
  private addContractDataToDb(block: L2Block): Promise<boolean> {
281
287
  return this.updateContractDataOnDb(block, Operation.Store);
@@ -297,9 +303,6 @@ export class ArchiverDataStoreUpdater {
297
303
  this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
298
304
  this.updateDeployedContractInstances(privateLogs, block.number, operation),
299
305
  this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
300
- operation === Operation.Store
301
- ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
302
- : Promise.resolve(true),
303
306
  ])
304
307
  ).every(Boolean);
305
308
  }
@@ -316,18 +319,37 @@ export class ArchiverDataStoreUpdater {
316
319
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
317
320
  .map(log => ContractClassPublishedEvent.fromLog(log));
318
321
 
319
- const contractClasses = await Promise.all(contractClassPublishedEvents.map(e => e.toContractClassPublic()));
320
- if (contractClasses.length > 0) {
321
- contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
322
- if (operation == Operation.Store) {
323
- // TODO: Will probably want to create some worker threads to compute these bytecode commitments as they are expensive
324
- const commitments = await Promise.all(
325
- contractClasses.map(c => computePublicBytecodeCommitment(c.packedBytecode)),
326
- );
327
- return await this.store.addContractClasses(contractClasses, commitments, blockNum);
328
- } 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()}`));
329
326
  return await this.store.deleteContractClasses(contractClasses, blockNum);
330
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);
331
353
  }
332
354
  return true;
333
355
  }
@@ -340,10 +362,27 @@ export class ArchiverDataStoreUpdater {
340
362
  blockNum: BlockNumber,
341
363
  operation: Operation,
342
364
  ): Promise<boolean> {
343
- const contractInstances = allLogs
365
+ const allInstances = allLogs
344
366
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
345
367
  .map(log => ContractInstancePublishedEvent.fromLog(log))
346
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
+
347
386
  if (contractInstances.length > 0) {
348
387
  contractInstances.forEach(c =>
349
388
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -382,67 +421,4 @@ export class ArchiverDataStoreUpdater {
382
421
  }
383
422
  return true;
384
423
  }
385
-
386
- /**
387
- * Stores the functions that were broadcasted individually.
388
- *
389
- * @dev Beware that there is not a delete variant of this, since they are added to contract classes
390
- * and will be deleted as part of the class if needed.
391
- */
392
- private async storeBroadcastedIndividualFunctions(
393
- allLogs: ContractClassLog[],
394
- _blockNum: BlockNumber,
395
- ): Promise<boolean> {
396
- // Filter out private and utility function broadcast events
397
- const privateFnEvents = allLogs
398
- .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
399
- .map(log => PrivateFunctionBroadcastedEvent.fromLog(log));
400
- const utilityFnEvents = allLogs
401
- .filter(log => UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log))
402
- .map(log => UtilityFunctionBroadcastedEvent.fromLog(log));
403
-
404
- // Group all events by contract class id
405
- for (const [classIdString, classEvents] of Object.entries(
406
- groupBy([...privateFnEvents, ...utilityFnEvents], e => e.contractClassId.toString()),
407
- )) {
408
- const contractClassId = Fr.fromHexString(classIdString);
409
- const contractClass = await this.store.getContractClass(contractClassId);
410
- if (!contractClass) {
411
- this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
412
- continue;
413
- }
414
-
415
- // Split private and utility functions, and filter out invalid ones
416
- const allFns = classEvents.map(e => e.toFunctionWithMembershipProof());
417
- const privateFns = allFns.filter(
418
- (fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'utilityFunctionsTreeRoot' in fn,
419
- );
420
- const utilityFns = allFns.filter(
421
- (fn): fn is UtilityFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
422
- );
423
-
424
- const privateFunctionsWithValidity = await Promise.all(
425
- privateFns.map(async fn => ({ fn, valid: await isValidPrivateFunctionMembershipProof(fn, contractClass) })),
426
- );
427
- const validPrivateFns = privateFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
428
- const utilityFunctionsWithValidity = await Promise.all(
429
- utilityFns.map(async fn => ({
430
- fn,
431
- valid: await isValidUtilityFunctionMembershipProof(fn, contractClass),
432
- })),
433
- );
434
- const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
435
- const validFnCount = validPrivateFns.length + validUtilityFns.length;
436
- if (validFnCount !== allFns.length) {
437
- this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
438
- }
439
-
440
- // Store the functions in the contract class in a single operation
441
- if (validFnCount > 0) {
442
- this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
443
- }
444
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
445
- }
446
- return true;
447
- }
448
424
  }
@@ -1,6 +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';
3
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';
4
7
  import {
5
8
  Attributes,
6
9
  type Gauge,
@@ -38,6 +41,8 @@ export class ArchiverInstrumentation {
38
41
 
39
42
  private blockProposalTxTargetCount: UpDownCounter;
40
43
 
44
+ private checkpointL1InclusionDelay: Histogram;
45
+
41
46
  private log = createLogger('archiver:instrumentation');
42
47
 
43
48
  private constructor(
@@ -85,6 +90,8 @@ export class ArchiverInstrumentation {
85
90
  },
86
91
  );
87
92
 
93
+ this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
94
+
88
95
  this.dbMetrics = new LmdbMetrics(
89
96
  meter,
90
97
  {
@@ -161,4 +168,17 @@ export class ArchiverInstrumentation {
161
168
  [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: usedTrace,
162
169
  });
163
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
+ }
164
184
  }