@aztec/archiver 4.0.0-devnet.2-patch.4 → 4.0.0-devnet.3-patch.1

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 (78) hide show
  1. package/dest/archiver.d.ts +3 -3
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +67 -22
  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 +25 -23
  10. package/dest/l1/bin/retrieve-calldata.js +32 -28
  11. package/dest/l1/calldata_retriever.d.ts +70 -53
  12. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  13. package/dest/l1/calldata_retriever.js +178 -260
  14. package/dest/l1/data_retrieval.d.ts +2 -6
  15. package/dest/l1/data_retrieval.d.ts.map +1 -1
  16. package/dest/l1/data_retrieval.js +6 -11
  17. package/dest/l1/spire_proposer.d.ts +5 -5
  18. package/dest/l1/spire_proposer.d.ts.map +1 -1
  19. package/dest/l1/spire_proposer.js +9 -17
  20. package/dest/modules/data_source_base.d.ts +3 -3
  21. package/dest/modules/data_source_base.d.ts.map +1 -1
  22. package/dest/modules/data_source_base.js +1 -1
  23. package/dest/modules/data_store_updater.d.ts +15 -7
  24. package/dest/modules/data_store_updater.d.ts.map +1 -1
  25. package/dest/modules/data_store_updater.js +75 -23
  26. package/dest/modules/l1_synchronizer.d.ts +3 -7
  27. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  28. package/dest/modules/l1_synchronizer.js +36 -6
  29. package/dest/store/block_store.d.ts +13 -13
  30. package/dest/store/block_store.d.ts.map +1 -1
  31. package/dest/store/block_store.js +111 -66
  32. package/dest/store/contract_class_store.d.ts +1 -1
  33. package/dest/store/contract_class_store.d.ts.map +1 -1
  34. package/dest/store/contract_class_store.js +6 -2
  35. package/dest/store/contract_instance_store.d.ts +1 -1
  36. package/dest/store/contract_instance_store.d.ts.map +1 -1
  37. package/dest/store/contract_instance_store.js +6 -2
  38. package/dest/store/kv_archiver_store.d.ts +20 -12
  39. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  40. package/dest/store/kv_archiver_store.js +24 -13
  41. package/dest/store/log_store.d.ts +1 -1
  42. package/dest/store/log_store.d.ts.map +1 -1
  43. package/dest/store/log_store.js +48 -10
  44. package/dest/store/message_store.js +1 -1
  45. package/dest/test/fake_l1_state.d.ts +17 -1
  46. package/dest/test/fake_l1_state.d.ts.map +1 -1
  47. package/dest/test/fake_l1_state.js +83 -12
  48. package/dest/test/mock_l2_block_source.d.ts +4 -3
  49. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  50. package/dest/test/mock_l2_block_source.js +7 -4
  51. package/dest/test/mock_structs.d.ts +4 -1
  52. package/dest/test/mock_structs.d.ts.map +1 -1
  53. package/dest/test/mock_structs.js +13 -1
  54. package/dest/test/noop_l1_archiver.d.ts +4 -1
  55. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  56. package/dest/test/noop_l1_archiver.js +5 -1
  57. package/package.json +13 -13
  58. package/src/archiver.ts +80 -22
  59. package/src/errors.ts +10 -24
  60. package/src/factory.ts +22 -13
  61. package/src/l1/README.md +25 -68
  62. package/src/l1/bin/retrieve-calldata.ts +40 -27
  63. package/src/l1/calldata_retriever.ts +231 -383
  64. package/src/l1/data_retrieval.ts +3 -16
  65. package/src/l1/spire_proposer.ts +7 -15
  66. package/src/modules/data_source_base.ts +3 -3
  67. package/src/modules/data_store_updater.ts +81 -26
  68. package/src/modules/l1_synchronizer.ts +41 -10
  69. package/src/store/block_store.ts +134 -74
  70. package/src/store/contract_class_store.ts +7 -3
  71. package/src/store/contract_instance_store.ts +8 -5
  72. package/src/store/kv_archiver_store.ts +31 -17
  73. package/src/store/log_store.ts +66 -12
  74. package/src/store/message_store.ts +1 -1
  75. package/src/test/fake_l1_state.ts +110 -14
  76. package/src/test/mock_l2_block_source.ts +15 -3
  77. package/src/test/mock_structs.ts +20 -6
  78. package/src/test/noop_l1_archiver.ts +7 -1
@@ -20,7 +20,7 @@ import {
20
20
  serializeValidateCheckpointResult,
21
21
  } from '@aztec/stdlib/block';
22
22
  import { type CheckpointData, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
23
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
23
+ import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
24
24
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
25
25
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
26
26
  import {
@@ -35,15 +35,14 @@ import {
35
35
  } from '@aztec/stdlib/tx';
36
36
 
37
37
  import {
38
+ BlockAlreadyCheckpointedError,
38
39
  BlockArchiveNotConsistentError,
39
40
  BlockIndexNotSequentialError,
40
41
  BlockNotFoundError,
41
42
  BlockNumberNotSequentialError,
42
43
  CannotOverwriteCheckpointedBlockError,
43
44
  CheckpointNotFoundError,
44
- CheckpointNumberNotConsistentError,
45
45
  CheckpointNumberNotSequentialError,
46
- InitialBlockNumberNotSequentialError,
47
46
  InitialCheckpointNumberNotSequentialError,
48
47
  } from '../errors.js';
49
48
 
@@ -97,6 +96,9 @@ export class BlockStore {
97
96
  /** Stores last proven checkpoint */
98
97
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
99
98
 
99
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
100
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
101
+
100
102
  /** Stores the pending chain validation status */
101
103
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
102
104
 
@@ -111,10 +113,7 @@ export class BlockStore {
111
113
 
112
114
  #log = createLogger('archiver:block_store');
113
115
 
114
- constructor(
115
- private db: AztecAsyncKVStore,
116
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
117
- ) {
116
+ constructor(private db: AztecAsyncKVStore) {
118
117
  this.#blocks = db.openMap('archiver_blocks');
119
118
  this.#blockTxs = db.openMap('archiver_block_txs');
120
119
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -123,41 +122,42 @@ export class BlockStore {
123
122
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
124
123
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
125
124
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
125
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
126
126
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
127
127
  this.#checkpoints = db.openMap('archiver_checkpoints');
128
128
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
129
129
  }
130
130
 
131
131
  /**
132
- * Computes the finalized block number based on the proven block number.
133
- * A block is considered finalized when it's 2 epochs behind the proven block.
134
- * TODO(#13569): Compute proper finalized block number based on L1 finalized block.
135
- * TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
132
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
133
+ * in an L1 block that has itself been finalized on Ethereum.
136
134
  * @returns The finalized block number.
137
135
  */
138
136
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
139
- const provenBlockNumber = await this.getProvenBlockNumber();
140
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
137
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
138
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
139
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
140
+ }
141
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
142
+ if (!checkpointStorage) {
143
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
144
+ }
145
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
141
146
  }
142
147
 
143
148
  /**
144
- * Append new proposed blocks to the store's list. All blocks must be for the 'current' checkpoint.
145
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
149
+ * Append a new proposed block to the store.
150
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
146
151
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
147
- * @param blocks - The proposed L2 blocks to be added to the store.
152
+ * @param block - The proposed L2 block to be added to the store.
148
153
  * @returns True if the operation is successful.
149
154
  */
150
- async addProposedBlocks(blocks: L2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
151
- if (blocks.length === 0) {
152
- return true;
153
- }
154
-
155
+ async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
155
156
  return await this.db.transactionAsync(async () => {
156
- // Check that the block immediately before the first block to be added is present in the store.
157
- const firstBlockNumber = blocks[0].number;
158
- const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
159
- const firstBlockIndex = blocks[0].indexWithinCheckpoint;
160
- const firstBlockLastArchive = blocks[0].header.lastArchive.root;
157
+ const blockNumber = block.number;
158
+ const blockCheckpointNumber = block.checkpointNumber;
159
+ const blockIndex = block.indexWithinCheckpoint;
160
+ const blockLastArchive = block.header.lastArchive.root;
161
161
 
162
162
  // Extract the latest block and checkpoint numbers
163
163
  const previousBlockNumber = await this.getLatestBlockNumber();
@@ -165,71 +165,52 @@ export class BlockStore {
165
165
 
166
166
  // Verify we're not overwriting checkpointed blocks
167
167
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
168
- if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
169
- throw new CannotOverwriteCheckpointedBlockError(firstBlockNumber, lastCheckpointedBlockNumber);
168
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
169
+ // Check if the proposed block matches the already-checkpointed one
170
+ const existingBlock = await this.getBlock(BlockNumber(blockNumber));
171
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
172
+ throw new BlockAlreadyCheckpointedError(blockNumber);
173
+ }
174
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
170
175
  }
171
176
 
172
- // Check that the first block number is the expected one
173
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
174
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
177
+ // Check that the block number is the expected one
178
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
179
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
175
180
  }
176
181
 
177
182
  // The same check as above but for checkpoints
178
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
179
- throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
183
+ if (!opts.force && previousCheckpointNumber !== blockCheckpointNumber - 1) {
184
+ throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, previousCheckpointNumber);
180
185
  }
181
186
 
182
187
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
183
188
  const previousBlockResult = await this.getBlock(previousBlockNumber);
184
189
 
185
- let expectedFirstblockIndex = 0;
190
+ let expectedBlockIndex = 0;
186
191
  let previousBlockIndex: number | undefined = undefined;
187
192
  if (previousBlockResult !== undefined) {
188
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
193
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
189
194
  // The previous block is for the same checkpoint, therefore our index should follow it
190
195
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
191
- expectedFirstblockIndex = previousBlockIndex + 1;
196
+ expectedBlockIndex = previousBlockIndex + 1;
192
197
  }
193
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
198
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
194
199
  throw new BlockArchiveNotConsistentError(
195
- firstBlockNumber,
200
+ blockNumber,
196
201
  previousBlockResult.number,
197
- firstBlockLastArchive,
202
+ blockLastArchive,
198
203
  previousBlockResult.archive.root,
199
204
  );
200
205
  }
201
206
  }
202
207
 
203
- // Now check that the first block has the expected index value
204
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
205
- throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
208
+ // Now check that the block has the expected index value
209
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
210
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
206
211
  }
207
212
 
208
- // Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
209
- let previousBlock: L2Block | undefined = undefined;
210
- for (const block of blocks) {
211
- if (!opts.force && previousBlock) {
212
- if (previousBlock.number + 1 !== block.number) {
213
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
214
- }
215
- if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
216
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
217
- }
218
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
219
- throw new BlockArchiveNotConsistentError(
220
- block.number,
221
- previousBlock.number,
222
- block.header.lastArchive.root,
223
- previousBlock.archive.root,
224
- );
225
- }
226
- }
227
- if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
228
- throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
229
- }
230
- previousBlock = block;
231
- await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
232
- }
213
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
233
214
 
234
215
  return true;
235
216
  });
@@ -246,21 +227,34 @@ export class BlockStore {
246
227
  }
247
228
 
248
229
  return await this.db.transactionAsync(async () => {
249
- // Check that the checkpoint immediately before the first block to be added is present in the store.
250
230
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
251
231
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
252
232
 
253
- if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
233
+ // Handle already-stored checkpoints at the start of the batch.
234
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
235
+ // We accept them if archives match (same content) and update their L1 metadata.
236
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
237
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
238
+ if (checkpoints.length === 0) {
239
+ return true;
240
+ }
241
+ // Re-check sequentiality after skipping
242
+ const newFirstNumber = checkpoints[0].checkpoint.number;
243
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
244
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
245
+ }
246
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
254
247
  throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
255
248
  }
256
249
 
257
250
  // Extract the previous checkpoint if there is one
251
+ const currentFirstCheckpointNumber = checkpoints[0].checkpoint.number;
258
252
  let previousCheckpointData: CheckpointData | undefined = undefined;
259
- if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
253
+ if (currentFirstCheckpointNumber - 1 !== INITIAL_CHECKPOINT_NUMBER - 1) {
260
254
  // There should be a previous checkpoint
261
- previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
255
+ previousCheckpointData = await this.getCheckpointData(CheckpointNumber(currentFirstCheckpointNumber - 1));
262
256
  if (previousCheckpointData === undefined) {
263
- throw new CheckpointNotFoundError(previousCheckpointNumber);
257
+ throw new CheckpointNotFoundError(CheckpointNumber(currentFirstCheckpointNumber - 1));
264
258
  }
265
259
  }
266
260
 
@@ -350,6 +344,50 @@ export class BlockStore {
350
344
  });
351
345
  }
352
346
 
347
+ /**
348
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
349
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
350
+ */
351
+ private async skipOrUpdateAlreadyStoredCheckpoints(
352
+ checkpoints: PublishedCheckpoint[],
353
+ latestStored: CheckpointNumber,
354
+ ): Promise<PublishedCheckpoint[]> {
355
+ let i = 0;
356
+ for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
357
+ const incoming = checkpoints[i];
358
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
359
+ if (!stored) {
360
+ // Should not happen if latestStored is correct, but be safe
361
+ break;
362
+ }
363
+ // Verify the checkpoint content matches (archive root)
364
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
365
+ throw new Error(
366
+ `Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
367
+ `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
368
+ );
369
+ }
370
+ // Update L1 metadata and attestations for the already-stored checkpoint
371
+ this.#log.warn(
372
+ `Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
373
+ `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
374
+ );
375
+ await this.#checkpoints.set(incoming.checkpoint.number, {
376
+ header: incoming.checkpoint.header.toBuffer(),
377
+ archive: incoming.checkpoint.archive.toBuffer(),
378
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
379
+ l1: incoming.l1.toBuffer(),
380
+ attestations: incoming.attestations.map(a => a.toBuffer()),
381
+ checkpointNumber: incoming.checkpoint.number,
382
+ startBlock: incoming.checkpoint.blocks[0].number,
383
+ blockCount: incoming.checkpoint.blocks.length,
384
+ });
385
+ // Update the sync point to reflect the new L1 block
386
+ await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
387
+ }
388
+ return checkpoints.slice(i);
389
+ }
390
+
353
391
  private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
354
392
  const blockHash = await block.hash();
355
393
 
@@ -871,7 +909,10 @@ export class BlockStore {
871
909
  * @param txHash - The hash of a tx we try to get the receipt for.
872
910
  * @returns The requested tx receipt (or undefined if not found).
873
911
  */
874
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
912
+ async getSettledTxReceipt(
913
+ txHash: TxHash,
914
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
915
+ ): Promise<TxReceipt | undefined> {
875
916
  const txEffect = await this.getTxEffect(txHash);
876
917
  if (!txEffect) {
877
918
  return undefined;
@@ -880,10 +921,11 @@ export class BlockStore {
880
921
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
881
922
 
882
923
  // Use existing archiver methods to determine finalization level
883
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
924
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
884
925
  this.getProvenBlockNumber(),
885
926
  this.getCheckpointedL2BlockNumber(),
886
927
  this.getFinalizedL2BlockNumber(),
928
+ this.getBlockData(blockNumber),
887
929
  ]);
888
930
 
889
931
  let status: TxStatus;
@@ -897,6 +939,9 @@ export class BlockStore {
897
939
  status = TxStatus.PROPOSED;
898
940
  }
899
941
 
942
+ const epochNumber =
943
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
944
+
900
945
  return new TxReceipt(
901
946
  txHash,
902
947
  status,
@@ -905,6 +950,7 @@ export class BlockStore {
905
950
  txEffect.data.transactionFee.toBigInt(),
906
951
  txEffect.l2BlockHash,
907
952
  blockNumber,
953
+ epochNumber,
908
954
  );
909
955
  }
910
956
 
@@ -976,6 +1022,20 @@ export class BlockStore {
976
1022
  return result;
977
1023
  }
978
1024
 
1025
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
1026
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1027
+ this.getLatestCheckpointNumber(),
1028
+ this.#lastFinalizedCheckpoint.getAsync(),
1029
+ ]);
1030
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
1031
+ ? latestCheckpointNumber
1032
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1033
+ }
1034
+
1035
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1036
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1037
+ }
1038
+
979
1039
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
980
1040
  if (limit < 1) {
981
1041
  throw new Error(`Invalid limit: ${limit}`);
@@ -29,11 +29,15 @@ export class ContractClassStore {
29
29
  blockNumber: number,
30
30
  ): Promise<void> {
31
31
  await this.db.transactionAsync(async () => {
32
- await this.#contractClasses.setIfNotExists(
33
- contractClass.id.toString(),
32
+ const key = contractClass.id.toString();
33
+ if (await this.#contractClasses.hasAsync(key)) {
34
+ throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
35
+ }
36
+ await this.#contractClasses.set(
37
+ key,
34
38
  serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
35
39
  );
36
- await this.#bytecodeCommitments.setIfNotExists(contractClass.id.toString(), bytecodeCommitment.toBuffer());
40
+ await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
37
41
  });
38
42
  }
39
43
 
@@ -27,11 +27,14 @@ export class ContractInstanceStore {
27
27
 
28
28
  addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
29
29
  return this.db.transactionAsync(async () => {
30
- await this.#contractInstances.set(
31
- contractInstance.address.toString(),
32
- new SerializableContractInstance(contractInstance).toBuffer(),
33
- );
34
- await this.#contractInstancePublishedAt.set(contractInstance.address.toString(), blockNumber);
30
+ const key = contractInstance.address.toString();
31
+ if (await this.#contractInstances.hasAsync(key)) {
32
+ throw new Error(
33
+ `Contract instance at ${key} already exists (deployed at block ${await this.#contractInstancePublishedAt.getAsync(key)}), cannot add again at block ${blockNumber}`,
34
+ );
35
+ }
36
+ await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
37
+ await this.#contractInstancePublishedAt.set(key, blockNumber);
35
38
  });
36
39
  }
37
40
 
@@ -16,6 +16,7 @@ import {
16
16
  import type { CheckpointData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
17
17
  import type {
18
18
  ContractClassPublic,
19
+ ContractClassPublicWithCommitment,
19
20
  ContractDataSource,
20
21
  ContractInstanceUpdateWithAddress,
21
22
  ContractInstanceWithAddress,
@@ -71,9 +72,8 @@ export class KVArchiverDataStore implements ContractDataSource {
71
72
  constructor(
72
73
  private db: AztecAsyncKVStore,
73
74
  logsMaxPageSize: number = 1000,
74
- l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
75
75
  ) {
76
- this.#blockStore = new BlockStore(db, l1Constants);
76
+ this.#blockStore = new BlockStore(db);
77
77
  this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
78
78
  this.#messageStore = new MessageStore(db);
79
79
  this.#contractClassStore = new ContractClassStore(db);
@@ -168,19 +168,14 @@ export class KVArchiverDataStore implements ContractDataSource {
168
168
 
169
169
  /**
170
170
  * Add new contract classes from an L2 block to the store's list.
171
- * @param data - List of contract classes to be added.
172
- * @param bytecodeCommitments - Bytecode commitments for the contract classes.
171
+ * @param data - List of contract classes (with bytecode commitments) to be added.
173
172
  * @param blockNumber - Number of the L2 block the contracts were registered in.
174
173
  * @returns True if the operation is successful.
175
174
  */
176
- async addContractClasses(
177
- data: ContractClassPublic[],
178
- bytecodeCommitments: Fr[],
179
- blockNumber: BlockNumber,
180
- ): Promise<boolean> {
175
+ async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: BlockNumber): Promise<boolean> {
181
176
  return (
182
177
  await Promise.all(
183
- data.map((c, i) => this.#contractClassStore.addContractClass(c, bytecodeCommitments[i], blockNumber)),
178
+ data.map(c => this.#contractClassStore.addContractClass(c, c.publicBytecodeCommitment, blockNumber)),
184
179
  )
185
180
  ).every(Boolean);
186
181
  }
@@ -246,14 +241,14 @@ export class KVArchiverDataStore implements ContractDataSource {
246
241
  }
247
242
 
248
243
  /**
249
- * Append new proposed blocks to the store's list.
250
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
244
+ * Append a new proposed block to the store.
245
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
251
246
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
252
- * @param blocks - The proposed L2 blocks to be added to the store.
247
+ * @param block - The proposed L2 block to be added to the store.
253
248
  * @returns True if the operation is successful.
254
249
  */
255
- addProposedBlocks(blocks: L2Block[], opts: { force?: boolean; checkpointNumber?: number } = {}): Promise<boolean> {
256
- return this.#blockStore.addProposedBlocks(blocks, opts);
250
+ addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
251
+ return this.#blockStore.addProposedBlock(block, opts);
257
252
  }
258
253
 
259
254
  /**
@@ -410,8 +405,11 @@ export class KVArchiverDataStore implements ContractDataSource {
410
405
  * @param txHash - The hash of a tx we try to get the receipt for.
411
406
  * @returns The requested tx receipt (or undefined if not found).
412
407
  */
413
- getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
414
- return this.#blockStore.getSettledTxReceipt(txHash);
408
+ getSettledTxReceipt(
409
+ txHash: TxHash,
410
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
411
+ ): Promise<TxReceipt | undefined> {
412
+ return this.#blockStore.getSettledTxReceipt(txHash, l1Constants);
415
413
  }
416
414
 
417
415
  /**
@@ -542,6 +540,22 @@ export class KVArchiverDataStore implements ContractDataSource {
542
540
  await this.#blockStore.setProvenCheckpointNumber(checkpointNumber);
543
541
  }
544
542
 
543
+ /**
544
+ * Gets the number of the latest finalized checkpoint processed.
545
+ * @returns The number of the latest finalized checkpoint processed.
546
+ */
547
+ getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
548
+ return this.#blockStore.getFinalizedCheckpointNumber();
549
+ }
550
+
551
+ /**
552
+ * Stores the number of the latest finalized checkpoint processed.
553
+ * @param checkpointNumber - The number of the latest finalized checkpoint processed.
554
+ */
555
+ async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
556
+ await this.#blockStore.setFinalizedCheckpointNumber(checkpointNumber);
557
+ }
558
+
545
559
  async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
546
560
  await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
547
561
  }
@@ -1,6 +1,6 @@
1
1
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
2
  import { BlockNumber } from '@aztec/foundation/branded-types';
3
- import { filterAsync } from '@aztec/foundation/collection';
3
+ import { compactArray, filterAsync } from '@aztec/foundation/collection';
4
4
  import { Fr } from '@aztec/foundation/curves/bn254';
5
5
  import { createLogger } from '@aztec/foundation/log';
6
6
  import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
@@ -290,18 +290,49 @@ export class LogStore {
290
290
 
291
291
  deleteLogs(blocks: L2Block[]): Promise<boolean> {
292
292
  return this.db.transactionAsync(async () => {
293
- await Promise.all(
294
- blocks.map(async block => {
295
- // Delete private logs
296
- const privateKeys = (await this.#privateLogKeysByBlock.getAsync(block.number)) ?? [];
297
- await Promise.all(privateKeys.map(tag => this.#privateLogsByTag.delete(tag)));
298
-
299
- // Delete public logs
300
- const publicKeys = (await this.#publicLogKeysByBlock.getAsync(block.number)) ?? [];
301
- await Promise.all(publicKeys.map(key => this.#publicLogsByContractAndTag.delete(key)));
302
- }),
293
+ const blockNumbers = new Set(blocks.map(block => block.number));
294
+ const firstBlockToDelete = Math.min(...blockNumbers);
295
+
296
+ // Collect all unique private tags across all blocks being deleted
297
+ const allPrivateTags = new Set(
298
+ compactArray(await Promise.all(blocks.map(block => this.#privateLogKeysByBlock.getAsync(block.number)))).flat(),
303
299
  );
304
300
 
301
+ // Trim private logs: for each tag, delete all instances including and after the first block being deleted.
302
+ // This hinges on the invariant that logs for a given tag are always inserted in order of block number, which is enforced in #addPrivateLogs.
303
+ for (const tag of allPrivateTags) {
304
+ const existing = await this.#privateLogsByTag.getAsync(tag);
305
+ if (existing === undefined || existing.length === 0) {
306
+ continue;
307
+ }
308
+ const lastIndexToKeep = existing.findLastIndex(
309
+ buf => TxScopedL2Log.getBlockNumberFromBuffer(buf) < firstBlockToDelete,
310
+ );
311
+ const remaining = existing.slice(0, lastIndexToKeep + 1);
312
+ await (remaining.length > 0 ? this.#privateLogsByTag.set(tag, remaining) : this.#privateLogsByTag.delete(tag));
313
+ }
314
+
315
+ // Collect all unique public keys across all blocks being deleted
316
+ const allPublicKeys = new Set(
317
+ compactArray(await Promise.all(blocks.map(block => this.#publicLogKeysByBlock.getAsync(block.number)))).flat(),
318
+ );
319
+
320
+ // And do the same as we did with private logs
321
+ for (const key of allPublicKeys) {
322
+ const existing = await this.#publicLogsByContractAndTag.getAsync(key);
323
+ if (existing === undefined || existing.length === 0) {
324
+ continue;
325
+ }
326
+ const lastIndexToKeep = existing.findLastIndex(
327
+ buf => TxScopedL2Log.getBlockNumberFromBuffer(buf) < firstBlockToDelete,
328
+ );
329
+ const remaining = existing.slice(0, lastIndexToKeep + 1);
330
+ await (remaining.length > 0
331
+ ? this.#publicLogsByContractAndTag.set(key, remaining)
332
+ : this.#publicLogsByContractAndTag.delete(key));
333
+ }
334
+
335
+ // After trimming the tagged logs, we can delete the block-level keys that track which tags are in which blocks.
305
336
  await Promise.all(
306
337
  blocks.map(block =>
307
338
  Promise.all([
@@ -588,11 +619,24 @@ export class LogStore {
588
619
  txLogs: PublicLog[],
589
620
  filter: LogFilter = {},
590
621
  ): boolean {
622
+ if (filter.fromBlock && blockNumber < filter.fromBlock) {
623
+ return false;
624
+ }
625
+ if (filter.toBlock && blockNumber >= filter.toBlock) {
626
+ return false;
627
+ }
628
+ if (filter.txHash && !txHash.equals(filter.txHash)) {
629
+ return false;
630
+ }
631
+
591
632
  let maxLogsHit = false;
592
633
  let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
593
634
  for (; logIndex < txLogs.length; logIndex++) {
594
635
  const log = txLogs[logIndex];
595
- if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
636
+ if (
637
+ (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) &&
638
+ (!filter.tag || log.fields[0]?.equals(filter.tag))
639
+ ) {
596
640
  results.push(
597
641
  new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
598
642
  );
@@ -616,6 +660,16 @@ export class LogStore {
616
660
  txLogs: ContractClassLog[],
617
661
  filter: LogFilter = {},
618
662
  ): boolean {
663
+ if (filter.fromBlock && blockNumber < filter.fromBlock) {
664
+ return false;
665
+ }
666
+ if (filter.toBlock && blockNumber >= filter.toBlock) {
667
+ return false;
668
+ }
669
+ if (filter.txHash && !txHash.equals(filter.txHash)) {
670
+ return false;
671
+ }
672
+
619
673
  let maxLogsHit = false;
620
674
  let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
621
675
  for (; logIndex < txLogs.length; logIndex++) {
@@ -137,7 +137,7 @@ export class MessageStore {
137
137
  );
138
138
  }
139
139
 
140
- // Check the first message in a block has the correct index.
140
+ // Check the first message in a checkpoint has the correct index.
141
141
  if (
142
142
  (!lastMessage || message.checkpointNumber > lastMessage.checkpointNumber) &&
143
143
  message.index !== expectedStart