@aztec/archiver 0.0.1-commit.a072138 → 0.0.1-commit.a89ec08

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 (94) hide show
  1. package/dest/archiver.d.ts +7 -4
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +73 -110
  4. package/dest/errors.d.ts +7 -9
  5. package/dest/errors.d.ts.map +1 -1
  6. package/dest/errors.js +9 -14
  7. package/dest/factory.d.ts +3 -4
  8. package/dest/factory.d.ts.map +1 -1
  9. package/dest/factory.js +31 -24
  10. package/dest/index.d.ts +2 -1
  11. package/dest/index.d.ts.map +1 -1
  12. package/dest/index.js +1 -0
  13. package/dest/l1/bin/retrieve-calldata.js +36 -33
  14. package/dest/l1/calldata_retriever.d.ts +73 -50
  15. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  16. package/dest/l1/calldata_retriever.js +190 -259
  17. package/dest/l1/data_retrieval.d.ts +4 -7
  18. package/dest/l1/data_retrieval.d.ts.map +1 -1
  19. package/dest/l1/data_retrieval.js +9 -13
  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 +10 -5
  24. package/dest/modules/data_source_base.d.ts.map +1 -1
  25. package/dest/modules/data_source_base.js +29 -73
  26. package/dest/modules/data_store_updater.d.ts +22 -7
  27. package/dest/modules/data_store_updater.d.ts.map +1 -1
  28. package/dest/modules/data_store_updater.js +113 -40
  29. package/dest/modules/instrumentation.d.ts +4 -2
  30. package/dest/modules/instrumentation.d.ts.map +1 -1
  31. package/dest/modules/instrumentation.js +9 -2
  32. package/dest/modules/l1_synchronizer.d.ts +5 -8
  33. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  34. package/dest/modules/l1_synchronizer.js +40 -10
  35. package/dest/store/block_store.d.ts +30 -26
  36. package/dest/store/block_store.d.ts.map +1 -1
  37. package/dest/store/block_store.js +180 -83
  38. package/dest/store/contract_class_store.d.ts +1 -1
  39. package/dest/store/contract_class_store.d.ts.map +1 -1
  40. package/dest/store/contract_class_store.js +6 -2
  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 +37 -15
  45. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  46. package/dest/store/kv_archiver_store.js +42 -13
  47. package/dest/store/l2_tips_cache.d.ts +19 -0
  48. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  49. package/dest/store/l2_tips_cache.js +89 -0
  50. package/dest/store/log_store.d.ts +1 -1
  51. package/dest/store/log_store.d.ts.map +1 -1
  52. package/dest/store/log_store.js +103 -45
  53. package/dest/store/message_store.js +1 -1
  54. package/dest/test/fake_l1_state.d.ts +20 -1
  55. package/dest/test/fake_l1_state.d.ts.map +1 -1
  56. package/dest/test/fake_l1_state.js +97 -20
  57. package/dest/test/mock_archiver.d.ts +1 -1
  58. package/dest/test/mock_archiver.d.ts.map +1 -1
  59. package/dest/test/mock_archiver.js +3 -2
  60. package/dest/test/mock_l2_block_source.d.ts +21 -5
  61. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  62. package/dest/test/mock_l2_block_source.js +132 -86
  63. package/dest/test/mock_structs.d.ts +4 -1
  64. package/dest/test/mock_structs.d.ts.map +1 -1
  65. package/dest/test/mock_structs.js +13 -1
  66. package/dest/test/noop_l1_archiver.d.ts +4 -1
  67. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  68. package/dest/test/noop_l1_archiver.js +5 -1
  69. package/package.json +13 -13
  70. package/src/archiver.ts +88 -130
  71. package/src/errors.ts +10 -24
  72. package/src/factory.ts +45 -21
  73. package/src/index.ts +1 -0
  74. package/src/l1/README.md +25 -68
  75. package/src/l1/bin/retrieve-calldata.ts +46 -39
  76. package/src/l1/calldata_retriever.ts +249 -379
  77. package/src/l1/data_retrieval.ts +6 -16
  78. package/src/l1/spire_proposer.ts +7 -15
  79. package/src/modules/data_source_base.ts +56 -95
  80. package/src/modules/data_store_updater.ts +123 -43
  81. package/src/modules/instrumentation.ts +9 -2
  82. package/src/modules/l1_synchronizer.ts +47 -14
  83. package/src/store/block_store.ts +219 -110
  84. package/src/store/contract_class_store.ts +7 -3
  85. package/src/store/contract_instance_store.ts +8 -5
  86. package/src/store/kv_archiver_store.ts +66 -20
  87. package/src/store/l2_tips_cache.ts +89 -0
  88. package/src/store/log_store.ts +159 -43
  89. package/src/store/message_store.ts +1 -1
  90. package/src/test/fake_l1_state.ts +125 -21
  91. package/src/test/mock_archiver.ts +3 -2
  92. package/src/test/mock_l2_block_source.ts +173 -81
  93. package/src/test/mock_structs.ts +20 -6
  94. package/src/test/noop_l1_archiver.ts +7 -1
@@ -38,6 +38,7 @@ import { CalldataRetriever } from './calldata_retriever.js';
38
38
  export type RetrievedCheckpoint = {
39
39
  checkpointNumber: CheckpointNumber;
40
40
  archiveRoot: Fr;
41
+ feeAssetPriceModifier: bigint;
41
42
  header: CheckpointHeader;
42
43
  checkpointBlobData: CheckpointBlobData;
43
44
  l1: L1PublishedData;
@@ -49,6 +50,7 @@ export type RetrievedCheckpoint = {
49
50
  export async function retrievedToPublishedCheckpoint({
50
51
  checkpointNumber,
51
52
  archiveRoot,
53
+ feeAssetPriceModifier,
52
54
  header: checkpointHeader,
53
55
  checkpointBlobData,
54
56
  l1,
@@ -128,6 +130,7 @@ export async function retrievedToPublishedCheckpoint({
128
130
  header: checkpointHeader,
129
131
  blocks: l2Blocks,
130
132
  number: checkpointNumber,
133
+ feeAssetPriceModifier: feeAssetPriceModifier,
131
134
  });
132
135
 
133
136
  return PublishedCheckpoint.from({ checkpoint, l1, attestations });
@@ -154,11 +157,6 @@ export async function retrieveCheckpointsFromRollup(
154
157
  blobClient: BlobClientInterface,
155
158
  searchStartBlock: bigint,
156
159
  searchEndBlock: bigint,
157
- contractAddresses: {
158
- governanceProposerAddress: EthAddress;
159
- slashFactoryAddress?: EthAddress;
160
- slashingProposerAddress: EthAddress;
161
- },
162
160
  instrumentation: ArchiverInstrumentation,
163
161
  logger: Logger = createLogger('archiver'),
164
162
  isHistoricalSync: boolean = false,
@@ -202,7 +200,6 @@ export async function retrieveCheckpointsFromRollup(
202
200
  blobClient,
203
201
  checkpointProposedLogs,
204
202
  rollupConstants,
205
- contractAddresses,
206
203
  instrumentation,
207
204
  logger,
208
205
  isHistoricalSync,
@@ -223,7 +220,6 @@ export async function retrieveCheckpointsFromRollup(
223
220
  * @param blobClient - The blob client client for fetching blob data.
224
221
  * @param logs - CheckpointProposed logs.
225
222
  * @param rollupConstants - The rollup constants (chainId, version, targetCommitteeSize).
226
- * @param contractAddresses - The contract addresses (governanceProposerAddress, slashFactoryAddress, slashingProposerAddress).
227
223
  * @param instrumentation - The archiver instrumentation instance.
228
224
  * @param logger - The logger instance.
229
225
  * @param isHistoricalSync - Whether this is a historical sync.
@@ -236,11 +232,6 @@ async function processCheckpointProposedLogs(
236
232
  blobClient: BlobClientInterface,
237
233
  logs: CheckpointProposedLog[],
238
234
  { chainId, version, targetCommitteeSize }: { chainId: Fr; version: Fr; targetCommitteeSize: number },
239
- contractAddresses: {
240
- governanceProposerAddress: EthAddress;
241
- slashFactoryAddress?: EthAddress;
242
- slashingProposerAddress: EthAddress;
243
- },
244
235
  instrumentation: ArchiverInstrumentation,
245
236
  logger: Logger,
246
237
  isHistoricalSync: boolean,
@@ -252,7 +243,7 @@ async function processCheckpointProposedLogs(
252
243
  targetCommitteeSize,
253
244
  instrumentation,
254
245
  logger,
255
- { ...contractAddresses, rollupAddress: EthAddress.fromString(rollup.address) },
246
+ EthAddress.fromString(rollup.address),
256
247
  );
257
248
 
258
249
  await asyncPool(10, logs, async log => {
@@ -263,10 +254,9 @@ async function processCheckpointProposedLogs(
263
254
 
264
255
  // The value from the event and contract will match only if the checkpoint is in the chain.
265
256
  if (archive.equals(archiveFromChain)) {
266
- // Build expected hashes object (fields may be undefined for backwards compatibility with older events)
267
257
  const expectedHashes = {
268
- attestationsHash: log.args.attestationsHash?.toString(),
269
- payloadDigest: log.args.payloadDigest?.toString(),
258
+ attestationsHash: log.args.attestationsHash.toString() as Hex,
259
+ payloadDigest: log.args.payloadDigest.toString() as Hex,
270
260
  };
271
261
 
272
262
  const checkpoint = await calldataRetriever.getCheckpointFromRollupTx(
@@ -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 });
@@ -1,11 +1,12 @@
1
+ import { range } from '@aztec/foundation/array';
1
2
  import { BlockNumber, CheckpointNumber, type EpochNumber, type SlotNumber } from '@aztec/foundation/branded-types';
2
3
  import type { Fr } from '@aztec/foundation/curves/bn254';
3
4
  import type { EthAddress } from '@aztec/foundation/eth-address';
4
5
  import { isDefined } from '@aztec/foundation/types';
5
6
  import type { FunctionSelector } from '@aztec/stdlib/abi';
6
7
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
7
- import { type BlockHash, CheckpointedL2Block, CommitteeAttestation, L2Block, type L2Tips } from '@aztec/stdlib/block';
8
- import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
8
+ import { type BlockData, type BlockHash, CheckpointedL2Block, L2Block, type L2Tips } from '@aztec/stdlib/block';
9
+ import { Checkpoint, type CheckpointData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
9
10
  import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
10
11
  import { type L1RollupConstants, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
11
12
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
@@ -17,7 +18,6 @@ import type { BlockHeader, IndexedTxEffect, TxHash, TxReceipt } from '@aztec/std
17
18
  import type { UInt64 } from '@aztec/stdlib/types';
18
19
 
19
20
  import type { ArchiverDataSource } from '../interfaces.js';
20
- import type { CheckpointData } from '../store/block_store.js';
21
21
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
22
22
  import type { ValidateCheckpointResult } from './validation.js';
23
23
 
@@ -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
 
@@ -114,7 +114,7 @@ export abstract class ArchiverDataSourceBase
114
114
  if (!checkpointData) {
115
115
  return undefined;
116
116
  }
117
- return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
117
+ return BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
118
118
  }
119
119
 
120
120
  public getCheckpointedBlocks(from: BlockNumber, limit: number): Promise<CheckpointedL2Block[]> {
@@ -129,6 +129,14 @@ export abstract class ArchiverDataSourceBase
129
129
  return this.store.getBlockHeaderByArchive(archive);
130
130
  }
131
131
 
132
+ public getBlockData(number: BlockNumber): Promise<BlockData | undefined> {
133
+ return this.store.getBlockData(number);
134
+ }
135
+
136
+ public getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
137
+ return this.store.getBlockDataByArchive(archive);
138
+ }
139
+
132
140
  public async getL2Block(number: BlockNumber): Promise<L2Block | undefined> {
133
141
  // If the number provided is -ve, then return the latest block.
134
142
  if (number < 0) {
@@ -146,7 +154,7 @@ export abstract class ArchiverDataSourceBase
146
154
  }
147
155
 
148
156
  public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
149
- return this.store.getSettledTxReceipt(txHash);
157
+ return this.store.getSettledTxReceipt(txHash, this.l1Constants);
150
158
  }
151
159
 
152
160
  public isPendingChainInvalid(): Promise<boolean> {
@@ -223,28 +231,21 @@ export abstract class ArchiverDataSourceBase
223
231
 
224
232
  public async getCheckpoints(checkpointNumber: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
225
233
  const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
226
- const blocks = (
227
- await Promise.all(checkpoints.map(ch => this.store.getBlocksForCheckpoint(ch.checkpointNumber)))
228
- ).filter(isDefined);
229
-
230
- const fullCheckpoints: PublishedCheckpoint[] = [];
231
- for (let i = 0; i < checkpoints.length; i++) {
232
- const blocksForCheckpoint = blocks[i];
233
- const checkpoint = checkpoints[i];
234
- const fullCheckpoint = new Checkpoint(
235
- checkpoint.archive,
236
- checkpoint.header,
237
- blocksForCheckpoint,
238
- checkpoint.checkpointNumber,
239
- );
240
- const publishedCheckpoint = new PublishedCheckpoint(
241
- fullCheckpoint,
242
- checkpoint.l1,
243
- checkpoint.attestations.map(x => CommitteeAttestation.fromBuffer(x)),
244
- );
245
- fullCheckpoints.push(publishedCheckpoint);
234
+ return Promise.all(checkpoints.map(ch => this.getPublishedCheckpointFromCheckpointData(ch)));
235
+ }
236
+
237
+ private async getPublishedCheckpointFromCheckpointData(checkpoint: CheckpointData): Promise<PublishedCheckpoint> {
238
+ const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpoint.checkpointNumber);
239
+ if (!blocksForCheckpoint) {
240
+ throw new Error(`Blocks for checkpoint ${checkpoint.checkpointNumber} not found`);
246
241
  }
247
- return fullCheckpoints;
242
+ const fullCheckpoint = new Checkpoint(
243
+ checkpoint.archive,
244
+ checkpoint.header,
245
+ blocksForCheckpoint,
246
+ checkpoint.checkpointNumber,
247
+ );
248
+ return new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations);
248
249
  }
249
250
 
250
251
  public getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
@@ -252,84 +253,44 @@ export abstract class ArchiverDataSourceBase
252
253
  }
253
254
 
254
255
  public async getCheckpointedBlocksForEpoch(epochNumber: EpochNumber): Promise<CheckpointedL2Block[]> {
255
- if (!this.l1Constants) {
256
- throw new Error('L1 constants not set');
257
- }
258
-
259
- const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
260
- const blocks: CheckpointedL2Block[] = [];
261
-
262
- // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
263
- // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
264
- let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
265
- const slot = (b: CheckpointData) => b.header.slotNumber;
266
- while (checkpoint && slot(checkpoint) >= start) {
267
- if (slot(checkpoint) <= end) {
268
- // push the blocks on backwards
269
- const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
270
- for (let i = endBlock; i >= checkpoint.startBlock; i--) {
271
- const checkpointedBlock = await this.getCheckpointedBlock(BlockNumber(i));
272
- if (checkpointedBlock) {
273
- blocks.push(checkpointedBlock);
274
- }
275
- }
276
- }
277
- checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
278
- }
279
-
280
- return blocks.reverse();
256
+ const checkpointsData = await this.getCheckpointsDataForEpoch(epochNumber);
257
+ const blocks = await Promise.all(
258
+ checkpointsData.flatMap(checkpoint =>
259
+ range(checkpoint.blockCount, checkpoint.startBlock).map(blockNumber =>
260
+ this.getCheckpointedBlock(BlockNumber(blockNumber)),
261
+ ),
262
+ ),
263
+ );
264
+ return blocks.filter(isDefined);
281
265
  }
282
266
 
283
267
  public async getCheckpointedBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
284
- if (!this.l1Constants) {
285
- throw new Error('L1 constants not set');
286
- }
287
-
288
- const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
289
- const blocks: BlockHeader[] = [];
290
-
291
- // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
292
- // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
293
- let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
294
- const slot = (b: CheckpointData) => b.header.slotNumber;
295
- while (checkpoint && slot(checkpoint) >= start) {
296
- if (slot(checkpoint) <= end) {
297
- // push the blocks on backwards
298
- const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
299
- for (let i = endBlock; i >= checkpoint.startBlock; i--) {
300
- const block = await this.getBlockHeader(BlockNumber(i));
301
- if (block) {
302
- blocks.push(block);
303
- }
304
- }
305
- }
306
- checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
307
- }
308
- return blocks.reverse();
268
+ const checkpointsData = await this.getCheckpointsDataForEpoch(epochNumber);
269
+ const blocks = await Promise.all(
270
+ checkpointsData.flatMap(checkpoint =>
271
+ range(checkpoint.blockCount, checkpoint.startBlock).map(blockNumber =>
272
+ this.getBlockHeader(BlockNumber(blockNumber)),
273
+ ),
274
+ ),
275
+ );
276
+ return blocks.filter(isDefined);
309
277
  }
310
278
 
311
279
  public async getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
280
+ const checkpointsData = await this.getCheckpointsDataForEpoch(epochNumber);
281
+ return Promise.all(
282
+ checkpointsData.map(data => this.getPublishedCheckpointFromCheckpointData(data).then(p => p.checkpoint)),
283
+ );
284
+ }
285
+
286
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given epoch. */
287
+ public getCheckpointsDataForEpoch(epochNumber: EpochNumber): Promise<CheckpointData[]> {
312
288
  if (!this.l1Constants) {
313
289
  throw new Error('L1 constants not set');
314
290
  }
315
291
 
316
292
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
317
- const checkpoints: Checkpoint[] = [];
318
-
319
- // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
320
- // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
321
- let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
322
- const slot = (b: CheckpointData) => b.header.slotNumber;
323
- while (checkpointData && slot(checkpointData) >= start) {
324
- if (slot(checkpointData) <= end) {
325
- // push the checkpoints on backwards
326
- const [checkpoint] = await this.getCheckpoints(checkpointData.checkpointNumber, 1);
327
- checkpoints.push(checkpoint.checkpoint);
328
- }
329
- checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
330
- }
331
-
332
- return checkpoints.reverse();
293
+ return this.store.getCheckpointDataForSlotRange(start, end);
333
294
  }
334
295
 
335
296
  public async getBlock(number: BlockNumber): Promise<L2Block | undefined> {
@@ -1,4 +1,5 @@
1
1
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { filterAsync } from '@aztec/foundation/collection';
2
3
  import { Fr } from '@aztec/foundation/curves/bn254';
3
4
  import { createLogger } from '@aztec/foundation/log';
4
5
  import {
@@ -11,11 +12,13 @@ import {
11
12
  ContractInstanceUpdatedEvent,
12
13
  } from '@aztec/protocol-contracts/instance-registry';
13
14
  import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
15
+ import { type PublishedCheckpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
15
16
  import {
17
+ type ContractClassPublicWithCommitment,
16
18
  type ExecutablePrivateFunctionWithMembershipProof,
17
19
  type UtilityFunctionWithMembershipProof,
18
- computePublicBytecodeCommitment,
20
+ computeContractAddressFromInstance,
21
+ computeContractClassId,
19
22
  isValidPrivateFunctionMembershipProof,
20
23
  isValidUtilityFunctionMembershipProof,
21
24
  } from '@aztec/stdlib/contract';
@@ -25,6 +28,7 @@ import type { UInt64 } from '@aztec/stdlib/types';
25
28
  import groupBy from 'lodash.groupby';
26
29
 
27
30
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
31
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
28
32
 
29
33
  /** Operation type for contract data updates. */
30
34
  enum Operation {
@@ -44,36 +48,42 @@ type ReconcileCheckpointsResult = {
44
48
  export class ArchiverDataStoreUpdater {
45
49
  private readonly log = createLogger('archiver:store_updater');
46
50
 
47
- constructor(private store: KVArchiverDataStore) {}
51
+ constructor(
52
+ private store: KVArchiverDataStore,
53
+ private l2TipsCache?: L2TipsCache,
54
+ private opts: { rollupManaLimit?: number } = {},
55
+ ) {}
48
56
 
49
57
  /**
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.
58
+ * Adds a proposed block to the store with contract class/instance extraction from logs.
59
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
52
60
  * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
53
61
  * and individually broadcasted functions from the block logs.
54
62
  *
55
- * @param blocks - The proposed L2 blocks to add.
63
+ * @param block - The proposed L2 block to add.
56
64
  * @param pendingChainValidationStatus - Optional validation status to set.
57
65
  * @returns True if the operation is successful.
58
66
  */
59
- public addProposedBlocks(
60
- blocks: L2Block[],
67
+ public async addProposedBlock(
68
+ block: L2Block,
61
69
  pendingChainValidationStatus?: ValidateCheckpointResult,
62
70
  ): Promise<boolean> {
63
- return this.store.transactionAsync(async () => {
64
- await this.store.addProposedBlocks(blocks);
71
+ const result = await this.store.transactionAsync(async () => {
72
+ await this.store.addProposedBlock(block);
65
73
 
66
74
  const opResults = await Promise.all([
67
75
  // Update the pending chain validation status if provided
68
76
  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)),
77
+ // Add any logs emitted during the retrieved block
78
+ this.store.addLogs([block]),
79
+ // Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
80
+ this.addContractDataToDb(block),
73
81
  ]);
74
82
 
83
+ await this.l2TipsCache?.refresh();
75
84
  return opResults.every(Boolean);
76
85
  });
86
+ return result;
77
87
  }
78
88
 
79
89
  /**
@@ -87,17 +97,21 @@ export class ArchiverDataStoreUpdater {
87
97
  * @param pendingChainValidationStatus - Optional validation status to set.
88
98
  * @returns Result with information about any pruned blocks.
89
99
  */
90
- public addCheckpoints(
100
+ public async addCheckpoints(
91
101
  checkpoints: PublishedCheckpoint[],
92
102
  pendingChainValidationStatus?: ValidateCheckpointResult,
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
+
108
+ const result = await this.store.transactionAsync(async () => {
95
109
  // Before adding checkpoints, check for conflicts with local blocks if any
96
110
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
97
111
 
98
112
  await this.store.addCheckpoints(checkpoints);
99
113
 
100
- // Filter out blocks that were already inserted via addProposedBlocks() to avoid duplicating logs/contract data
114
+ // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
101
115
  const newBlocks = checkpoints
102
116
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
103
117
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
@@ -111,8 +125,10 @@ export class ArchiverDataStoreUpdater {
111
125
  ...newBlocks.map(block => this.addContractDataToDb(block)),
112
126
  ]);
113
127
 
128
+ await this.l2TipsCache?.refresh();
114
129
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
115
130
  });
131
+ return result;
116
132
  }
117
133
 
118
134
  /**
@@ -165,7 +181,7 @@ export class ArchiverDataStoreUpdater {
165
181
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
166
182
  lastAlreadyInsertedBlockNumber = blockNumber;
167
183
  } else {
168
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
184
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
169
185
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
170
186
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
171
187
  }
@@ -197,8 +213,8 @@ export class ArchiverDataStoreUpdater {
197
213
  * @returns The removed blocks.
198
214
  * @throws Error if any block to be removed is checkpointed.
199
215
  */
200
- public removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
201
- return this.store.transactionAsync(async () => {
216
+ public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
217
+ const result = await this.store.transactionAsync(async () => {
202
218
  // Verify we're only removing uncheckpointed blocks
203
219
  const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
204
220
  if (blockNumber < lastCheckpointedBlockNumber) {
@@ -207,8 +223,11 @@ export class ArchiverDataStoreUpdater {
207
223
  );
208
224
  }
209
225
 
210
- return await this.removeBlocksAfter(blockNumber);
226
+ const result = await this.removeBlocksAfter(blockNumber);
227
+ await this.l2TipsCache?.refresh();
228
+ return result;
211
229
  });
230
+ return result;
212
231
  }
213
232
 
214
233
  /**
@@ -238,17 +257,42 @@ export class ArchiverDataStoreUpdater {
238
257
  * @returns True if the operation is successful.
239
258
  */
240
259
  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
- ]);
260
+ return await this.store.transactionAsync(async () => {
261
+ const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
250
262
 
251
- return opResults.every(Boolean);
263
+ const opResults = await Promise.all([
264
+ // Prune rolls back to the last proven block, which is by definition valid
265
+ this.store.setPendingChainValidationStatus({ valid: true }),
266
+ // Remove contract data for all blocks being removed
267
+ ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
268
+ this.store.deleteLogs(blocksRemoved),
269
+ ]);
270
+
271
+ await this.l2TipsCache?.refresh();
272
+ return opResults.every(Boolean);
273
+ });
274
+ }
275
+
276
+ /**
277
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
278
+ * @param checkpointNumber - The checkpoint number to set as proven.
279
+ */
280
+ public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
281
+ await this.store.transactionAsync(async () => {
282
+ await this.store.setProvenCheckpointNumber(checkpointNumber);
283
+ await this.l2TipsCache?.refresh();
284
+ });
285
+ }
286
+
287
+ /**
288
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
289
+ * @param checkpointNumber - The checkpoint number to set as finalized.
290
+ */
291
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
292
+ await this.store.transactionAsync(async () => {
293
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
294
+ await this.l2TipsCache?.refresh();
295
+ });
252
296
  }
253
297
 
254
298
  /** Extracts and stores contract data from a single block. */
@@ -291,18 +335,37 @@ export class ArchiverDataStoreUpdater {
291
335
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
292
336
  .map(log => ContractClassPublishedEvent.fromLog(log));
293
337
 
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) {
338
+ if (operation == Operation.Delete) {
339
+ const contractClasses = contractClassPublishedEvents.map(e => e.toContractClassPublic());
340
+ if (contractClasses.length > 0) {
341
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
304
342
  return await this.store.deleteContractClasses(contractClasses, blockNum);
305
343
  }
344
+ return true;
345
+ }
346
+
347
+ // Compute bytecode commitments and validate class IDs in a single pass.
348
+ const contractClasses: ContractClassPublicWithCommitment[] = [];
349
+ for (const event of contractClassPublishedEvents) {
350
+ const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
351
+ const computedClassId = await computeContractClassId({
352
+ artifactHash: contractClass.artifactHash,
353
+ privateFunctionsRoot: contractClass.privateFunctionsRoot,
354
+ publicBytecodeCommitment: contractClass.publicBytecodeCommitment,
355
+ });
356
+ if (!computedClassId.equals(contractClass.id)) {
357
+ this.log.warn(
358
+ `Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`,
359
+ { blockNum, contractClassId: event.contractClassId.toString() },
360
+ );
361
+ continue;
362
+ }
363
+ contractClasses.push(contractClass);
364
+ }
365
+
366
+ if (contractClasses.length > 0) {
367
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
368
+ return await this.store.addContractClasses(contractClasses, blockNum);
306
369
  }
307
370
  return true;
308
371
  }
@@ -315,10 +378,27 @@ export class ArchiverDataStoreUpdater {
315
378
  blockNum: BlockNumber,
316
379
  operation: Operation,
317
380
  ): Promise<boolean> {
318
- const contractInstances = allLogs
381
+ const allInstances = allLogs
319
382
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
320
383
  .map(log => ContractInstancePublishedEvent.fromLog(log))
321
384
  .map(e => e.toContractInstance());
385
+
386
+ // Verify that each instance's address matches the one derived from its fields if we're adding
387
+ const contractInstances =
388
+ operation === Operation.Delete
389
+ ? allInstances
390
+ : await filterAsync(allInstances, async instance => {
391
+ const computedAddress = await computeContractAddressFromInstance(instance);
392
+ if (!computedAddress.equals(instance.address)) {
393
+ this.log.warn(
394
+ `Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
395
+ { instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
396
+ );
397
+ return false;
398
+ }
399
+ return true;
400
+ });
401
+
322
402
  if (contractInstances.length > 0) {
323
403
  contractInstances.forEach(c =>
324
404
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -416,7 +496,7 @@ export class ArchiverDataStoreUpdater {
416
496
  if (validFnCount > 0) {
417
497
  this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
418
498
  }
419
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
499
+ await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
420
500
  }
421
501
  return true;
422
502
  }