@aztec/archiver 0.0.1-commit.358457c → 0.0.1-commit.381b1a9

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 (55) hide show
  1. package/dest/archiver.d.ts +3 -3
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +49 -18
  4. package/dest/factory.d.ts +2 -3
  5. package/dest/factory.d.ts.map +1 -1
  6. package/dest/factory.js +8 -8
  7. package/dest/modules/data_source_base.d.ts +3 -3
  8. package/dest/modules/data_source_base.d.ts.map +1 -1
  9. package/dest/modules/data_source_base.js +1 -1
  10. package/dest/modules/data_store_updater.d.ts +11 -3
  11. package/dest/modules/data_store_updater.d.ts.map +1 -1
  12. package/dest/modules/data_store_updater.js +36 -5
  13. package/dest/modules/instrumentation.d.ts +1 -12
  14. package/dest/modules/instrumentation.d.ts.map +1 -1
  15. package/dest/modules/instrumentation.js +0 -10
  16. package/dest/modules/l1_synchronizer.d.ts +2 -1
  17. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  18. package/dest/modules/l1_synchronizer.js +31 -9
  19. package/dest/store/block_store.d.ts +8 -9
  20. package/dest/store/block_store.d.ts.map +1 -1
  21. package/dest/store/block_store.js +31 -13
  22. package/dest/store/kv_archiver_store.d.ts +13 -3
  23. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  24. package/dest/store/kv_archiver_store.js +16 -4
  25. package/dest/store/log_store.d.ts +1 -1
  26. package/dest/store/log_store.d.ts.map +1 -1
  27. package/dest/store/log_store.js +48 -10
  28. package/dest/store/message_store.js +1 -1
  29. package/dest/test/fake_l1_state.d.ts +8 -1
  30. package/dest/test/fake_l1_state.d.ts.map +1 -1
  31. package/dest/test/fake_l1_state.js +28 -2
  32. package/dest/test/mock_l2_block_source.d.ts +4 -3
  33. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  34. package/dest/test/mock_l2_block_source.js +7 -4
  35. package/dest/test/mock_structs.d.ts +4 -1
  36. package/dest/test/mock_structs.d.ts.map +1 -1
  37. package/dest/test/mock_structs.js +13 -1
  38. package/dest/test/noop_l1_archiver.d.ts +4 -1
  39. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  40. package/dest/test/noop_l1_archiver.js +5 -1
  41. package/package.json +13 -13
  42. package/src/archiver.ts +59 -20
  43. package/src/factory.ts +5 -4
  44. package/src/modules/data_source_base.ts +3 -3
  45. package/src/modules/data_store_updater.ts +39 -4
  46. package/src/modules/instrumentation.ts +0 -20
  47. package/src/modules/l1_synchronizer.ts +38 -11
  48. package/src/store/block_store.ts +41 -13
  49. package/src/store/kv_archiver_store.ts +22 -4
  50. package/src/store/log_store.ts +66 -12
  51. package/src/store/message_store.ts +1 -1
  52. package/src/test/fake_l1_state.ts +35 -4
  53. package/src/test/mock_l2_block_source.ts +15 -3
  54. package/src/test/mock_structs.ts +20 -6
  55. package/src/test/noop_l1_archiver.ts +7 -1
package/src/factory.ts CHANGED
@@ -14,7 +14,6 @@ import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/prov
14
14
  import { FunctionType, decodeFunctionSignature } from '@aztec/stdlib/abi';
15
15
  import type { ArchiverEmitter } from '@aztec/stdlib/block';
16
16
  import { type ContractClassPublic, computePublicBytecodeCommitment } from '@aztec/stdlib/contract';
17
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
18
17
  import { getTelemetryClient } from '@aztec/telemetry-client';
19
18
 
20
19
  import { EventEmitter } from 'events';
@@ -32,14 +31,13 @@ export const ARCHIVER_STORE_NAME = 'archiver';
32
31
  /** Creates an archiver store. */
33
32
  export async function createArchiverStore(
34
33
  userConfig: Pick<ArchiverConfig, 'archiverStoreMapSizeKb' | 'maxLogs'> & DataStoreConfig,
35
- l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
36
34
  ) {
37
35
  const config = {
38
36
  ...userConfig,
39
37
  dataStoreMapSizeKb: userConfig.archiverStoreMapSizeKb ?? userConfig.dataStoreMapSizeKb,
40
38
  };
41
39
  const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config);
42
- return new KVArchiverDataStore(store, config.maxLogs, l1Constants);
40
+ return new KVArchiverDataStore(store, config.maxLogs);
43
41
  }
44
42
 
45
43
  /**
@@ -54,7 +52,7 @@ export async function createArchiver(
54
52
  deps: ArchiverDeps,
55
53
  opts: { blockUntilSync: boolean } = { blockUntilSync: true },
56
54
  ): Promise<Archiver> {
57
- const archiverStore = await createArchiverStore(config, { epochDuration: config.aztecEpochDuration });
55
+ const archiverStore = await createArchiverStore(config);
58
56
  await registerProtocolContracts(archiverStore);
59
57
 
60
58
  // Create Ethereum clients
@@ -85,6 +83,7 @@ export async function createArchiver(
85
83
  genesisArchiveRoot,
86
84
  slashingProposerAddress,
87
85
  targetCommitteeSize,
86
+ rollupManaLimit,
88
87
  ] = await Promise.all([
89
88
  rollup.getL1StartBlock(),
90
89
  rollup.getL1GenesisTime(),
@@ -92,6 +91,7 @@ export async function createArchiver(
92
91
  rollup.getGenesisArchiveTreeRoot(),
93
92
  rollup.getSlashingProposerAddress(),
94
93
  rollup.getTargetCommitteeSize(),
94
+ rollup.getManaLimit(),
95
95
  ] as const);
96
96
 
97
97
  const l1StartBlockHash = await publicClient
@@ -110,6 +110,7 @@ export async function createArchiver(
110
110
  proofSubmissionEpochs: Number(proofSubmissionEpochs),
111
111
  targetCommitteeSize,
112
112
  genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString()),
113
+ rollupManaLimit: Number(rollupManaLimit),
113
114
  };
114
115
 
115
116
  const archiverConfig = merge(
@@ -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> {
@@ -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,10 +12,11 @@ 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 {
16
17
  type ExecutablePrivateFunctionWithMembershipProof,
17
18
  type UtilityFunctionWithMembershipProof,
19
+ computeContractAddressFromInstance,
18
20
  computePublicBytecodeCommitment,
19
21
  isValidPrivateFunctionMembershipProof,
20
22
  isValidUtilityFunctionMembershipProof,
@@ -48,6 +50,7 @@ export class ArchiverDataStoreUpdater {
48
50
  constructor(
49
51
  private store: KVArchiverDataStore,
50
52
  private l2TipsCache?: L2TipsCache,
53
+ private opts: { rollupManaLimit?: number } = {},
51
54
  ) {}
52
55
 
53
56
  /**
@@ -97,6 +100,10 @@ export class ArchiverDataStoreUpdater {
97
100
  checkpoints: PublishedCheckpoint[],
98
101
  pendingChainValidationStatus?: ValidateCheckpointResult,
99
102
  ): Promise<ReconcileCheckpointsResult> {
103
+ for (const checkpoint of checkpoints) {
104
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
105
+ }
106
+
100
107
  const result = await this.store.transactionAsync(async () => {
101
108
  // Before adding checkpoints, check for conflicts with local blocks if any
102
109
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
@@ -173,7 +180,7 @@ export class ArchiverDataStoreUpdater {
173
180
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
174
181
  lastAlreadyInsertedBlockNumber = blockNumber;
175
182
  } else {
176
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
183
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
177
184
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
178
185
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
179
186
  }
@@ -276,6 +283,17 @@ export class ArchiverDataStoreUpdater {
276
283
  });
277
284
  }
278
285
 
286
+ /**
287
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
288
+ * @param checkpointNumber - The checkpoint number to set as finalized.
289
+ */
290
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
291
+ await this.store.transactionAsync(async () => {
292
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
293
+ await this.l2TipsCache?.refresh();
294
+ });
295
+ }
296
+
279
297
  /** Extracts and stores contract data from a single block. */
280
298
  private addContractDataToDb(block: L2Block): Promise<boolean> {
281
299
  return this.updateContractDataOnDb(block, Operation.Store);
@@ -340,10 +358,27 @@ export class ArchiverDataStoreUpdater {
340
358
  blockNum: BlockNumber,
341
359
  operation: Operation,
342
360
  ): Promise<boolean> {
343
- const contractInstances = allLogs
361
+ const allInstances = allLogs
344
362
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
345
363
  .map(log => ContractInstancePublishedEvent.fromLog(log))
346
364
  .map(e => e.toContractInstance());
365
+
366
+ // Verify that each instance's address matches the one derived from its fields if we're adding
367
+ const contractInstances =
368
+ operation === Operation.Delete
369
+ ? allInstances
370
+ : await filterAsync(allInstances, async instance => {
371
+ const computedAddress = await computeContractAddressFromInstance(instance);
372
+ if (!computedAddress.equals(instance.address)) {
373
+ this.log.warn(
374
+ `Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
375
+ { instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
376
+ );
377
+ return false;
378
+ }
379
+ return true;
380
+ });
381
+
347
382
  if (contractInstances.length > 0) {
348
383
  contractInstances.forEach(c =>
349
384
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
@@ -441,7 +476,7 @@ export class ArchiverDataStoreUpdater {
441
476
  if (validFnCount > 0) {
442
477
  this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
443
478
  }
444
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
479
+ await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
445
480
  }
446
481
  return true;
447
482
  }
@@ -1,9 +1,6 @@
1
- import type { SlotNumber } from '@aztec/foundation/branded-types';
2
1
  import { createLogger } from '@aztec/foundation/log';
3
2
  import type { L2Block } from '@aztec/stdlib/block';
4
3
  import type { CheckpointData } from '@aztec/stdlib/checkpoint';
5
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
6
- import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
7
4
  import {
8
5
  Attributes,
9
6
  type Gauge,
@@ -41,8 +38,6 @@ export class ArchiverInstrumentation {
41
38
 
42
39
  private blockProposalTxTargetCount: UpDownCounter;
43
40
 
44
- private checkpointL1InclusionDelay: Histogram;
45
-
46
41
  private log = createLogger('archiver:instrumentation');
47
42
 
48
43
  private constructor(
@@ -90,8 +85,6 @@ export class ArchiverInstrumentation {
90
85
  },
91
86
  );
92
87
 
93
- this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
94
-
95
88
  this.dbMetrics = new LmdbMetrics(
96
89
  meter,
97
90
  {
@@ -168,17 +161,4 @@ export class ArchiverInstrumentation {
168
161
  [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: usedTrace,
169
162
  });
170
163
  }
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
- }
184
164
  }
@@ -69,13 +69,18 @@ export class ArchiverL1Synchronizer implements Traceable {
69
69
  private readonly epochCache: EpochCache,
70
70
  private readonly dateProvider: DateProvider,
71
71
  private readonly instrumentation: ArchiverInstrumentation,
72
- private readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
72
+ private readonly l1Constants: L1RollupConstants & {
73
+ l1StartBlockHash: Buffer32;
74
+ genesisArchiveRoot: Fr;
75
+ },
73
76
  private readonly events: ArchiverEmitter,
74
77
  tracer: Tracer,
75
78
  l2TipsCache?: L2TipsCache,
76
79
  private readonly log: Logger = createLogger('archiver:l1-sync'),
77
80
  ) {
78
- this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache);
81
+ this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
82
+ rollupManaLimit: l1Constants.rollupManaLimit,
83
+ });
79
84
  this.tracer = tracer;
80
85
  }
81
86
 
@@ -211,6 +216,9 @@ export class ArchiverL1Synchronizer implements Traceable {
211
216
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
212
217
  }
213
218
 
219
+ // Update the finalized L2 checkpoint based on L1 finality.
220
+ await this.updateFinalizedCheckpoint();
221
+
214
222
  // After syncing has completed, update the current l1 block number and timestamp,
215
223
  // otherwise we risk announcing to the world that we've synced to a given point,
216
224
  // but the corresponding blocks have not been processed (see #12631).
@@ -226,6 +234,33 @@ export class ArchiverL1Synchronizer implements Traceable {
226
234
  });
227
235
  }
228
236
 
237
+ /** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
238
+ private async updateFinalizedCheckpoint(): Promise<void> {
239
+ try {
240
+ const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
241
+ const finalizedL1BlockNumber = finalizedL1Block.number;
242
+ const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
243
+ blockNumber: finalizedL1BlockNumber,
244
+ });
245
+ const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
246
+ if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
247
+ await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
248
+ const finalizedL2BlockNumber = await this.store.getFinalizedL2BlockNumber();
249
+ this.log.info(
250
+ `Updated finalized chain to checkpoint ${finalizedCheckpointNumber} (L2 block ${finalizedL2BlockNumber})`,
251
+ {
252
+ finalizedCheckpointNumber,
253
+ previousFinalizedCheckpointNumber: localFinalizedCheckpointNumber,
254
+ finalizedL2BlockNumber,
255
+ finalizedL1BlockNumber,
256
+ },
257
+ );
258
+ }
259
+ } catch (err) {
260
+ this.log.warn(`Failed to update finalized checkpoint: ${err}`);
261
+ }
262
+ }
263
+
229
264
  /** Prune all proposed local blocks that should have been checkpointed by now. */
230
265
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
231
266
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
@@ -796,14 +831,6 @@ export class ArchiverL1Synchronizer implements Traceable {
796
831
  );
797
832
  }
798
833
 
799
- for (const published of validCheckpoints) {
800
- this.instrumentation.processCheckpointL1Timing({
801
- slotNumber: published.checkpoint.header.slotNumber,
802
- l1Timestamp: published.l1.timestamp,
803
- l1Constants: this.l1Constants,
804
- });
805
- }
806
-
807
834
  try {
808
835
  const updatedValidationResult =
809
836
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
@@ -822,7 +849,7 @@ export class ArchiverL1Synchronizer implements Traceable {
822
849
  const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
823
850
  const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
824
851
 
825
- this.log.warn(
852
+ this.log.info(
826
853
  `Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
827
854
  { prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
828
855
  );
@@ -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 {
@@ -97,6 +97,9 @@ export class BlockStore {
97
97
  /** Stores last proven checkpoint */
98
98
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
99
99
 
100
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
101
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
102
+
100
103
  /** Stores the pending chain validation status */
101
104
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
102
105
 
@@ -111,10 +114,7 @@ export class BlockStore {
111
114
 
112
115
  #log = createLogger('archiver:block_store');
113
116
 
114
- constructor(
115
- private db: AztecAsyncKVStore,
116
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
117
- ) {
117
+ constructor(private db: AztecAsyncKVStore) {
118
118
  this.#blocks = db.openMap('archiver_blocks');
119
119
  this.#blockTxs = db.openMap('archiver_block_txs');
120
120
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -123,21 +123,27 @@ export class BlockStore {
123
123
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
124
124
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
125
125
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
126
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
126
127
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
127
128
  this.#checkpoints = db.openMap('archiver_checkpoints');
128
129
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
129
130
  }
130
131
 
131
132
  /**
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
133
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
134
+ * in an L1 block that has itself been finalized on Ethereum.
136
135
  * @returns The finalized block number.
137
136
  */
138
137
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
139
- const provenBlockNumber = await this.getProvenBlockNumber();
140
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
138
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
139
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
140
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
141
+ }
142
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
143
+ if (!checkpointStorage) {
144
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
145
+ }
146
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
141
147
  }
142
148
 
143
149
  /**
@@ -871,7 +877,10 @@ export class BlockStore {
871
877
  * @param txHash - The hash of a tx we try to get the receipt for.
872
878
  * @returns The requested tx receipt (or undefined if not found).
873
879
  */
874
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
880
+ async getSettledTxReceipt(
881
+ txHash: TxHash,
882
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
883
+ ): Promise<TxReceipt | undefined> {
875
884
  const txEffect = await this.getTxEffect(txHash);
876
885
  if (!txEffect) {
877
886
  return undefined;
@@ -880,10 +889,11 @@ export class BlockStore {
880
889
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
881
890
 
882
891
  // Use existing archiver methods to determine finalization level
883
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
892
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
884
893
  this.getProvenBlockNumber(),
885
894
  this.getCheckpointedL2BlockNumber(),
886
895
  this.getFinalizedL2BlockNumber(),
896
+ this.getBlockData(blockNumber),
887
897
  ]);
888
898
 
889
899
  let status: TxStatus;
@@ -897,6 +907,9 @@ export class BlockStore {
897
907
  status = TxStatus.PROPOSED;
898
908
  }
899
909
 
910
+ const epochNumber =
911
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
912
+
900
913
  return new TxReceipt(
901
914
  txHash,
902
915
  status,
@@ -905,6 +918,7 @@ export class BlockStore {
905
918
  txEffect.data.transactionFee.toBigInt(),
906
919
  txEffect.l2BlockHash,
907
920
  blockNumber,
921
+ epochNumber,
908
922
  );
909
923
  }
910
924
 
@@ -976,6 +990,20 @@ export class BlockStore {
976
990
  return result;
977
991
  }
978
992
 
993
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
994
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
995
+ this.getLatestCheckpointNumber(),
996
+ this.#lastFinalizedCheckpoint.getAsync(),
997
+ ]);
998
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
999
+ ? latestCheckpointNumber
1000
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1001
+ }
1002
+
1003
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1004
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1005
+ }
1006
+
979
1007
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
980
1008
  if (limit < 1) {
981
1009
  throw new Error(`Invalid limit: ${limit}`);
@@ -71,9 +71,8 @@ export class KVArchiverDataStore implements ContractDataSource {
71
71
  constructor(
72
72
  private db: AztecAsyncKVStore,
73
73
  logsMaxPageSize: number = 1000,
74
- l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
75
74
  ) {
76
- this.#blockStore = new BlockStore(db, l1Constants);
75
+ this.#blockStore = new BlockStore(db);
77
76
  this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
78
77
  this.#messageStore = new MessageStore(db);
79
78
  this.#contractClassStore = new ContractClassStore(db);
@@ -410,8 +409,11 @@ export class KVArchiverDataStore implements ContractDataSource {
410
409
  * @param txHash - The hash of a tx we try to get the receipt for.
411
410
  * @returns The requested tx receipt (or undefined if not found).
412
411
  */
413
- getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
414
- return this.#blockStore.getSettledTxReceipt(txHash);
412
+ getSettledTxReceipt(
413
+ txHash: TxHash,
414
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
415
+ ): Promise<TxReceipt | undefined> {
416
+ return this.#blockStore.getSettledTxReceipt(txHash, l1Constants);
415
417
  }
416
418
 
417
419
  /**
@@ -542,6 +544,22 @@ export class KVArchiverDataStore implements ContractDataSource {
542
544
  await this.#blockStore.setProvenCheckpointNumber(checkpointNumber);
543
545
  }
544
546
 
547
+ /**
548
+ * Gets the number of the latest finalized checkpoint processed.
549
+ * @returns The number of the latest finalized checkpoint processed.
550
+ */
551
+ getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
552
+ return this.#blockStore.getFinalizedCheckpointNumber();
553
+ }
554
+
555
+ /**
556
+ * Stores the number of the latest finalized checkpoint processed.
557
+ * @param checkpointNumber - The number of the latest finalized checkpoint processed.
558
+ */
559
+ async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
560
+ await this.#blockStore.setFinalizedCheckpointNumber(checkpointNumber);
561
+ }
562
+
545
563
  async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
546
564
  await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
547
565
  }
@@ -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