@aztec/archiver 0.0.1-commit.3469e52 → 0.0.1-commit.3895657bc

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 (97) hide show
  1. package/README.md +9 -0
  2. package/dest/archiver.d.ts +12 -6
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +52 -111
  5. package/dest/errors.d.ts +6 -1
  6. package/dest/errors.d.ts.map +1 -1
  7. package/dest/errors.js +8 -0
  8. package/dest/factory.d.ts +6 -3
  9. package/dest/factory.d.ts.map +1 -1
  10. package/dest/factory.js +19 -14
  11. package/dest/index.d.ts +2 -1
  12. package/dest/index.d.ts.map +1 -1
  13. package/dest/index.js +1 -0
  14. package/dest/l1/bin/retrieve-calldata.js +35 -32
  15. package/dest/l1/calldata_retriever.d.ts +73 -50
  16. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  17. package/dest/l1/calldata_retriever.js +190 -259
  18. package/dest/l1/data_retrieval.d.ts +9 -9
  19. package/dest/l1/data_retrieval.d.ts.map +1 -1
  20. package/dest/l1/data_retrieval.js +24 -22
  21. package/dest/l1/spire_proposer.d.ts +5 -5
  22. package/dest/l1/spire_proposer.d.ts.map +1 -1
  23. package/dest/l1/spire_proposer.js +9 -17
  24. package/dest/l1/validate_trace.d.ts +6 -3
  25. package/dest/l1/validate_trace.d.ts.map +1 -1
  26. package/dest/l1/validate_trace.js +13 -9
  27. package/dest/modules/data_source_base.d.ts +23 -19
  28. package/dest/modules/data_source_base.d.ts.map +1 -1
  29. package/dest/modules/data_source_base.js +44 -119
  30. package/dest/modules/data_store_updater.d.ts +35 -21
  31. package/dest/modules/data_store_updater.d.ts.map +1 -1
  32. package/dest/modules/data_store_updater.js +87 -60
  33. package/dest/modules/instrumentation.d.ts +17 -4
  34. package/dest/modules/instrumentation.d.ts.map +1 -1
  35. package/dest/modules/instrumentation.js +36 -12
  36. package/dest/modules/l1_synchronizer.d.ts +5 -8
  37. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  38. package/dest/modules/l1_synchronizer.js +25 -19
  39. package/dest/store/block_store.d.ts +50 -32
  40. package/dest/store/block_store.d.ts.map +1 -1
  41. package/dest/store/block_store.js +147 -54
  42. package/dest/store/contract_class_store.d.ts +1 -1
  43. package/dest/store/contract_class_store.d.ts.map +1 -1
  44. package/dest/store/contract_class_store.js +11 -7
  45. package/dest/store/kv_archiver_store.d.ts +43 -25
  46. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  47. package/dest/store/kv_archiver_store.js +38 -17
  48. package/dest/store/l2_tips_cache.d.ts +19 -0
  49. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  50. package/dest/store/l2_tips_cache.js +89 -0
  51. package/dest/store/log_store.d.ts +4 -4
  52. package/dest/store/log_store.d.ts.map +1 -1
  53. package/dest/store/log_store.js +57 -37
  54. package/dest/store/message_store.js +1 -1
  55. package/dest/test/fake_l1_state.d.ts +9 -4
  56. package/dest/test/fake_l1_state.d.ts.map +1 -1
  57. package/dest/test/fake_l1_state.js +56 -18
  58. package/dest/test/index.js +3 -1
  59. package/dest/test/mock_archiver.d.ts +1 -1
  60. package/dest/test/mock_archiver.d.ts.map +1 -1
  61. package/dest/test/mock_archiver.js +3 -2
  62. package/dest/test/mock_l2_block_source.d.ts +37 -21
  63. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  64. package/dest/test/mock_l2_block_source.js +154 -109
  65. package/dest/test/mock_structs.d.ts +6 -2
  66. package/dest/test/mock_structs.d.ts.map +1 -1
  67. package/dest/test/mock_structs.js +24 -10
  68. package/dest/test/noop_l1_archiver.d.ts +23 -0
  69. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  70. package/dest/test/noop_l1_archiver.js +68 -0
  71. package/package.json +14 -13
  72. package/src/archiver.ts +78 -137
  73. package/src/errors.ts +12 -0
  74. package/src/factory.ts +34 -15
  75. package/src/index.ts +1 -0
  76. package/src/l1/README.md +25 -68
  77. package/src/l1/bin/retrieve-calldata.ts +45 -33
  78. package/src/l1/calldata_retriever.ts +249 -379
  79. package/src/l1/data_retrieval.ts +27 -29
  80. package/src/l1/spire_proposer.ts +7 -15
  81. package/src/l1/validate_trace.ts +24 -6
  82. package/src/modules/data_source_base.ts +73 -163
  83. package/src/modules/data_store_updater.ts +98 -64
  84. package/src/modules/instrumentation.ts +46 -14
  85. package/src/modules/l1_synchronizer.ts +33 -25
  86. package/src/store/block_store.ts +188 -92
  87. package/src/store/contract_class_store.ts +11 -7
  88. package/src/store/kv_archiver_store.ts +69 -29
  89. package/src/store/l2_tips_cache.ts +89 -0
  90. package/src/store/log_store.ts +105 -43
  91. package/src/store/message_store.ts +1 -1
  92. package/src/test/fake_l1_state.ts +77 -19
  93. package/src/test/index.ts +3 -0
  94. package/src/test/mock_archiver.ts +3 -2
  95. package/src/test/mock_l2_block_source.ts +202 -126
  96. package/src/test/mock_structs.ts +45 -15
  97. package/src/test/noop_l1_archiver.ts +109 -0
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@aztec/archiver",
3
- "version": "0.0.1-commit.3469e52",
3
+ "version": "0.0.1-commit.3895657bc",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
7
7
  "./test": "./dest/test/index.js",
8
+ "./test/noop-l1": "./dest/test/noop_l1_archiver.js",
8
9
  "./config": "./dest/config.js"
9
10
  },
10
11
  "typedocOptions": {
@@ -64,18 +65,18 @@
64
65
  ]
65
66
  },
66
67
  "dependencies": {
67
- "@aztec/blob-client": "0.0.1-commit.3469e52",
68
- "@aztec/blob-lib": "0.0.1-commit.3469e52",
69
- "@aztec/constants": "0.0.1-commit.3469e52",
70
- "@aztec/epoch-cache": "0.0.1-commit.3469e52",
71
- "@aztec/ethereum": "0.0.1-commit.3469e52",
72
- "@aztec/foundation": "0.0.1-commit.3469e52",
73
- "@aztec/kv-store": "0.0.1-commit.3469e52",
74
- "@aztec/l1-artifacts": "0.0.1-commit.3469e52",
75
- "@aztec/noir-protocol-circuits-types": "0.0.1-commit.3469e52",
76
- "@aztec/protocol-contracts": "0.0.1-commit.3469e52",
77
- "@aztec/stdlib": "0.0.1-commit.3469e52",
78
- "@aztec/telemetry-client": "0.0.1-commit.3469e52",
68
+ "@aztec/blob-client": "0.0.1-commit.3895657bc",
69
+ "@aztec/blob-lib": "0.0.1-commit.3895657bc",
70
+ "@aztec/constants": "0.0.1-commit.3895657bc",
71
+ "@aztec/epoch-cache": "0.0.1-commit.3895657bc",
72
+ "@aztec/ethereum": "0.0.1-commit.3895657bc",
73
+ "@aztec/foundation": "0.0.1-commit.3895657bc",
74
+ "@aztec/kv-store": "0.0.1-commit.3895657bc",
75
+ "@aztec/l1-artifacts": "0.0.1-commit.3895657bc",
76
+ "@aztec/noir-protocol-circuits-types": "0.0.1-commit.3895657bc",
77
+ "@aztec/protocol-contracts": "0.0.1-commit.3895657bc",
78
+ "@aztec/stdlib": "0.0.1-commit.3895657bc",
79
+ "@aztec/telemetry-client": "0.0.1-commit.3895657bc",
79
80
  "lodash.groupby": "^4.6.0",
80
81
  "lodash.omit": "^4.5.0",
81
82
  "tslib": "^2.5.0",
package/src/archiver.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
- import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
3
2
  import { EpochCache } from '@aztec/epoch-cache';
4
3
  import { BlockTagTooOldError, RollupContract } from '@aztec/ethereum/contracts';
5
4
  import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
@@ -15,9 +14,7 @@ import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/runni
15
14
  import { DateProvider } from '@aztec/foundation/timer';
16
15
  import {
17
16
  type ArchiverEmitter,
18
- type CheckpointId,
19
- GENESIS_CHECKPOINT_HEADER_HASH,
20
- L2BlockNew,
17
+ L2Block,
21
18
  type L2BlockSink,
22
19
  type L2Tips,
23
20
  type ValidateCheckpointResult,
@@ -26,6 +23,7 @@ import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
26
23
  import {
27
24
  type L1RollupConstants,
28
25
  getEpochNumberAtTimestamp,
26
+ getSlotAtNextL1Block,
29
27
  getSlotAtTimestamp,
30
28
  getSlotRangeForEpoch,
31
29
  getTimestampRangeForEpoch,
@@ -40,13 +38,14 @@ import { ArchiverDataStoreUpdater } from './modules/data_store_updater.js';
40
38
  import type { ArchiverInstrumentation } from './modules/instrumentation.js';
41
39
  import type { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
42
40
  import type { KVArchiverDataStore } from './store/kv_archiver_store.js';
41
+ import { L2TipsCache } from './store/l2_tips_cache.js';
43
42
 
44
43
  /** Export ArchiverEmitter for use in factory and tests. */
45
44
  export type { ArchiverEmitter };
46
45
 
47
46
  /** Request to add a block to the archiver, queued for processing by the sync loop. */
48
47
  type AddBlockRequest = {
49
- block: L2BlockNew;
48
+ block: L2Block;
50
49
  resolve: () => void;
51
50
  reject: (err: Error) => void;
52
51
  };
@@ -68,7 +67,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
68
67
  public readonly events: ArchiverEmitter;
69
68
 
70
69
  /** A loop in which we will be continually fetching new checkpoints. */
71
- private runningPromise: RunningPromise;
70
+ protected runningPromise: RunningPromise;
72
71
 
73
72
  /** L1 synchronizer that handles fetching checkpoints and messages from L1. */
74
73
  private readonly synchronizer: ArchiverL1Synchronizer;
@@ -82,6 +81,9 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
82
81
  /** Helper to handle updates to the store */
83
82
  private readonly updater: ArchiverDataStoreUpdater;
84
83
 
84
+ /** In-memory cache for L2 chain tips. */
85
+ private readonly l2TipsCache: L2TipsCache;
86
+
85
87
  public readonly tracer: Tracer;
86
88
 
87
89
  /**
@@ -118,9 +120,14 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
118
120
  },
119
121
  private readonly blobClient: BlobClientInterface,
120
122
  instrumentation: ArchiverInstrumentation,
121
- protected override readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
123
+ protected override readonly l1Constants: L1RollupConstants & {
124
+ l1StartBlockHash: Buffer32;
125
+ genesisArchiveRoot: Fr;
126
+ rollupManaLimit?: number;
127
+ },
122
128
  synchronizer: ArchiverL1Synchronizer,
123
129
  events: ArchiverEmitter,
130
+ l2TipsCache?: L2TipsCache,
124
131
  private readonly log: Logger = createLogger('archiver'),
125
132
  ) {
126
133
  super(dataStore, l1Constants);
@@ -129,7 +136,10 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
129
136
  this.initialSyncPromise = promiseWithResolvers();
130
137
  this.synchronizer = synchronizer;
131
138
  this.events = events;
132
- this.updater = new ArchiverDataStoreUpdater(this.dataStore);
139
+ this.l2TipsCache = l2TipsCache ?? new L2TipsCache(this.dataStore.blockStore);
140
+ this.updater = new ArchiverDataStoreUpdater(this.dataStore, this.l2TipsCache, {
141
+ rollupManaLimit: l1Constants.rollupManaLimit,
142
+ });
133
143
 
134
144
  // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
135
145
  // are done as fast as possible. This then gets updated once the initial sync completes.
@@ -158,7 +168,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
158
168
 
159
169
  await this.blobClient.testSources();
160
170
  await this.synchronizer.testEthereumNodeSynced();
161
- await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
171
+ await validateAndLogTraceAvailability(
172
+ this.debugClient,
173
+ this.config.ethereumAllowNoDebugHosts ?? false,
174
+ this.log.getBindings(),
175
+ );
162
176
 
163
177
  // Log initial state for the archiver
164
178
  const { l1StartBlock } = this.l1Constants;
@@ -187,7 +201,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
187
201
  * @param block - The L2 block to add.
188
202
  * @returns A promise that resolves when the block has been added to the store, or rejects on error.
189
203
  */
190
- public addBlock(block: L2BlockNew): Promise<void> {
204
+ public addBlock(block: L2Block): Promise<void> {
191
205
  return new Promise<void>((resolve, reject) => {
192
206
  this.blockQueue.push({ block, resolve, reject });
193
207
  this.log.debug(`Queued block ${block.number} for processing`);
@@ -212,10 +226,25 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
212
226
  const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
213
227
  this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
214
228
 
229
+ // Calculate slot threshold for validation
230
+ const l1Timestamp = this.synchronizer.getL1Timestamp();
231
+ const slotAtNextL1Block =
232
+ l1Timestamp === undefined ? undefined : getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
233
+
215
234
  // Process each block individually to properly resolve/reject each promise
216
235
  for (const { block, resolve, reject } of queuedItems) {
236
+ const blockSlot = block.header.globalVariables.slotNumber;
237
+ if (slotAtNextL1Block !== undefined && blockSlot < slotAtNextL1Block) {
238
+ this.log.warn(
239
+ `Rejecting proposed block ${block.number} for past slot ${blockSlot} (current is ${slotAtNextL1Block})`,
240
+ { block: block.toBlockInfo(), l1Timestamp, slotAtNextL1Block },
241
+ );
242
+ reject(new Error(`Block ${block.number} is for past slot ${blockSlot} (current is ${slotAtNextL1Block})`));
243
+ continue;
244
+ }
245
+
217
246
  try {
218
- await this.updater.addBlocks([block]);
247
+ await this.updater.addProposedBlocks([block]);
219
248
  this.log.debug(`Added block ${block.number} to store`);
220
249
  resolve();
221
250
  } catch (err: any) {
@@ -323,8 +352,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
323
352
  }
324
353
 
325
354
  public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
326
- // The epoch is complete if the current L2 block is the last one in the epoch (or later)
327
- const header = await this.getBlockHeader('latest');
355
+ // The epoch is complete if the current checkpointed L2 block is the last one in the epoch (or later).
356
+ // We use the checkpointed block number (synced from L1) instead of 'latest' to avoid returning true
357
+ // prematurely when proposed blocks have been pushed to the archiver but not yet checkpointed on L1.
358
+ const checkpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
359
+ const header = checkpointedBlockNumber > 0 ? await this.getBlockHeader(checkpointedBlockNumber) : undefined;
328
360
  const slot = header ? header.globalVariables.slotNumber : undefined;
329
361
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
330
362
  if (slot && slot >= endSlot) {
@@ -355,8 +387,8 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
355
387
  return this.initialSyncComplete;
356
388
  }
357
389
 
358
- public unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number): Promise<boolean> {
359
- return this.updater.unwindCheckpoints(from, checkpointsToUnwind);
390
+ public removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
391
+ return this.updater.removeCheckpointsAfter(checkpointNumber);
360
392
  }
361
393
 
362
394
  /** Used by TXE to add checkpoints directly without syncing from L1. */
@@ -364,125 +396,15 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
364
396
  checkpoints: PublishedCheckpoint[],
365
397
  pendingChainValidationStatus?: ValidateCheckpointResult,
366
398
  ): Promise<boolean> {
367
- await this.updater.setNewCheckpointData(checkpoints, pendingChainValidationStatus);
399
+ await this.updater.addCheckpoints(checkpoints, pendingChainValidationStatus);
368
400
  return true;
369
401
  }
370
402
 
371
- public async getL2Tips(): Promise<L2Tips> {
372
- const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber] = await Promise.all([
373
- this.getBlockNumber(),
374
- this.getProvenBlockNumber(),
375
- this.getCheckpointedBlockNumber(),
376
- ] as const);
377
-
378
- // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
379
- // We just force it 2 epochs worth of proven data for now.
380
- // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
381
- const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
382
-
383
- const beforeInitialblockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
384
-
385
- // Get the latest block header and checkpointed blocks for proven, finalised and checkpointed blocks
386
- const [latestBlockHeader, provenCheckpointedBlock, finalizedCheckpointedBlock, checkpointedBlock] =
387
- await Promise.all([
388
- latestBlockNumber > beforeInitialblockNumber ? this.getBlockHeader(latestBlockNumber) : undefined,
389
- provenBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(provenBlockNumber) : undefined,
390
- finalizedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(finalizedBlockNumber) : undefined,
391
- checkpointedBlockNumber > beforeInitialblockNumber
392
- ? this.getCheckpointedBlock(checkpointedBlockNumber)
393
- : undefined,
394
- ] as const);
395
-
396
- if (latestBlockNumber > beforeInitialblockNumber && !latestBlockHeader) {
397
- throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
398
- }
399
-
400
- // Checkpointed blocks must exist for proven, finalized and checkpointed tips if they are beyond the initial block number.
401
- if (checkpointedBlockNumber > beforeInitialblockNumber && !checkpointedBlock?.block.header) {
402
- throw new Error(
403
- `Failed to retrieve checkpointed block header for block ${checkpointedBlockNumber} (latest block is ${latestBlockNumber})`,
404
- );
405
- }
406
-
407
- if (provenBlockNumber > beforeInitialblockNumber && !provenCheckpointedBlock?.block.header) {
408
- throw new Error(
409
- `Failed to retrieve proven checkpointed for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`,
410
- );
411
- }
412
-
413
- if (finalizedBlockNumber > beforeInitialblockNumber && !finalizedCheckpointedBlock?.block.header) {
414
- throw new Error(
415
- `Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`,
416
- );
417
- }
418
-
419
- const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
420
- const provenBlockHeaderHash = (await provenCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
421
- const finalizedBlockHeaderHash =
422
- (await finalizedCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
423
- const checkpointedBlockHeaderHash = (await checkpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
424
-
425
- // Now attempt to retrieve checkpoints for proven, finalised and checkpointed blocks
426
- const [[provenBlockCheckpoint], [finalizedBlockCheckpoint], [checkpointedBlockCheckpoint]] = await Promise.all([
427
- provenCheckpointedBlock !== undefined
428
- ? await this.getPublishedCheckpoints(provenCheckpointedBlock?.checkpointNumber, 1)
429
- : [undefined],
430
- finalizedCheckpointedBlock !== undefined
431
- ? await this.getPublishedCheckpoints(finalizedCheckpointedBlock?.checkpointNumber, 1)
432
- : [undefined],
433
- checkpointedBlock !== undefined
434
- ? await this.getPublishedCheckpoints(checkpointedBlock?.checkpointNumber, 1)
435
- : [undefined],
436
- ]);
437
-
438
- const initialcheckpointId: CheckpointId = {
439
- number: CheckpointNumber.ZERO,
440
- hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
441
- };
442
-
443
- const makeCheckpointId = (checkpoint: PublishedCheckpoint | undefined) => {
444
- if (checkpoint === undefined) {
445
- return initialcheckpointId;
446
- }
447
- return {
448
- number: checkpoint.checkpoint.number,
449
- hash: checkpoint.checkpoint.hash().toString(),
450
- };
451
- };
452
-
453
- const l2Tips: L2Tips = {
454
- proposed: {
455
- number: latestBlockNumber,
456
- hash: latestBlockHeaderHash.toString(),
457
- },
458
- proven: {
459
- block: {
460
- number: provenBlockNumber,
461
- hash: provenBlockHeaderHash.toString(),
462
- },
463
- checkpoint: makeCheckpointId(provenBlockCheckpoint),
464
- },
465
- finalized: {
466
- block: {
467
- number: finalizedBlockNumber,
468
- hash: finalizedBlockHeaderHash.toString(),
469
- },
470
- checkpoint: makeCheckpointId(finalizedBlockCheckpoint),
471
- },
472
- checkpointed: {
473
- block: {
474
- number: checkpointedBlockNumber,
475
- hash: checkpointedBlockHeaderHash.toString(),
476
- },
477
- checkpoint: makeCheckpointId(checkpointedBlockCheckpoint),
478
- },
479
- };
480
-
481
- return l2Tips;
403
+ public getL2Tips(): Promise<L2Tips> {
404
+ return this.l2TipsCache.getL2Tips();
482
405
  }
483
406
 
484
407
  public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
485
- // TODO(pw/mbps): This still assumes 1 block per checkpoint
486
408
  const currentBlocks = await this.getL2Tips();
487
409
  const currentL2Block = currentBlocks.proposed.number;
488
410
  const currentProvenBlock = currentBlocks.proven.block.number;
@@ -490,13 +412,29 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
490
412
  if (targetL2BlockNumber >= currentL2Block) {
491
413
  throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
492
414
  }
493
- const blocksToUnwind = currentL2Block - targetL2BlockNumber;
494
415
  const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
495
416
  if (!targetL2Block) {
496
417
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
497
418
  }
419
+ const targetCheckpointNumber = targetL2Block.checkpointNumber;
420
+
421
+ // Rollback operates at checkpoint granularity: the target block must be the last block of its checkpoint.
422
+ const checkpointData = await this.store.getCheckpointData(targetCheckpointNumber);
423
+ if (checkpointData) {
424
+ const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
425
+ if (targetL2BlockNumber !== lastBlockInCheckpoint) {
426
+ const previousCheckpointBoundary =
427
+ checkpointData.startBlock > 1 ? BlockNumber(checkpointData.startBlock - 1) : BlockNumber(0);
428
+ throw new Error(
429
+ `Target L2 block ${targetL2BlockNumber} is not at a checkpoint boundary. ` +
430
+ `Checkpoint ${targetCheckpointNumber} spans blocks ${checkpointData.startBlock} to ${lastBlockInCheckpoint}. ` +
431
+ `Use block ${lastBlockInCheckpoint} to roll back to this checkpoint, ` +
432
+ `or block ${previousCheckpointBoundary} to roll back to the previous one.`,
433
+ );
434
+ }
435
+ }
436
+
498
437
  const targetL1BlockNumber = targetL2Block.l1.blockNumber;
499
- const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
500
438
  const targetL1Block = await this.publicClient.getBlock({
501
439
  blockNumber: targetL1BlockNumber,
502
440
  includeTransactions: false,
@@ -505,21 +443,24 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
505
443
  throw new Error(`Missing L1 block ${targetL1BlockNumber}`);
506
444
  }
507
445
  const targetL1BlockHash = Buffer32.fromString(targetL1Block.hash);
508
- this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
509
- await this.updater.unwindCheckpoints(CheckpointNumber(currentL2Block), blocksToUnwind);
510
- this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
446
+ this.log.info(
447
+ `Removing checkpoints after checkpoint ${targetCheckpointNumber} (target block ${targetL2BlockNumber})`,
448
+ );
449
+ await this.updater.removeCheckpointsAfter(targetCheckpointNumber);
450
+ this.log.info(`Rolling back L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
511
451
  await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
512
452
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
513
453
  await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
514
454
  await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
515
455
  if (targetL2BlockNumber < currentProvenBlock) {
516
- this.log.info(`Clearing proven L2 block number`);
517
- await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
456
+ this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
457
+ await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
518
458
  }
519
459
  // TODO(palla/reorg): Set the finalized block when we add support for it.
460
+ // const currentFinalizedBlock = currentBlocks.finalized.block.number;
520
461
  // if (targetL2BlockNumber < currentFinalizedBlock) {
521
- // this.log.info(`Clearing finalized L2 block number`);
522
- // await this.store.setFinalizedL2BlockNumber(0);
462
+ // this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
463
+ // await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
523
464
  // }
524
465
  }
525
466
  }
package/src/errors.ts CHANGED
@@ -88,3 +88,15 @@ export class BlockNotFoundError extends Error {
88
88
  super(`Failed to find expected block number ${blockNumber}`);
89
89
  }
90
90
  }
91
+
92
+ export class CannotOverwriteCheckpointedBlockError extends Error {
93
+ constructor(
94
+ public readonly blockNumber: number,
95
+ public readonly lastCheckpointedBlock: number,
96
+ ) {
97
+ super(
98
+ `Cannot add block ${blockNumber}: would overwrite checkpointed data (checkpointed up to block ${lastCheckpointedBlock})`,
99
+ );
100
+ this.name = 'CannotOverwriteCheckpointedBlockError';
101
+ }
102
+ }
package/src/factory.ts CHANGED
@@ -6,15 +6,15 @@ import { BlockNumber } from '@aztec/foundation/branded-types';
6
6
  import { Buffer32 } from '@aztec/foundation/buffer';
7
7
  import { merge } from '@aztec/foundation/collection';
8
8
  import { Fr } from '@aztec/foundation/curves/bn254';
9
- import { createLogger } from '@aztec/foundation/log';
10
9
  import { DateProvider } from '@aztec/foundation/timer';
11
- import type { DataStoreConfig } from '@aztec/kv-store/config';
12
10
  import { createStore } from '@aztec/kv-store/lmdb-v2';
13
11
  import { protocolContractNames } from '@aztec/protocol-contracts';
14
12
  import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/providers/bundle';
15
13
  import { FunctionType, decodeFunctionSignature } from '@aztec/stdlib/abi';
16
14
  import type { ArchiverEmitter } from '@aztec/stdlib/block';
17
15
  import { type ContractClassPublic, computePublicBytecodeCommitment } from '@aztec/stdlib/contract';
16
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
17
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
18
18
  import { getTelemetryClient } from '@aztec/telemetry-client';
19
19
 
20
20
  import { EventEmitter } from 'events';
@@ -25,19 +25,21 @@ import { type ArchiverConfig, mapArchiverConfig } from './config.js';
25
25
  import { ArchiverInstrumentation } from './modules/instrumentation.js';
26
26
  import { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
27
27
  import { ARCHIVER_DB_VERSION, KVArchiverDataStore } from './store/kv_archiver_store.js';
28
+ import { L2TipsCache } from './store/l2_tips_cache.js';
28
29
 
29
30
  export const ARCHIVER_STORE_NAME = 'archiver';
30
31
 
31
32
  /** Creates an archiver store. */
32
33
  export async function createArchiverStore(
33
34
  userConfig: Pick<ArchiverConfig, 'archiverStoreMapSizeKb' | 'maxLogs'> & DataStoreConfig,
35
+ l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
34
36
  ) {
35
37
  const config = {
36
38
  ...userConfig,
37
39
  dataStoreMapSizeKb: userConfig.archiverStoreMapSizeKb ?? userConfig.dataStoreMapSizeKb,
38
40
  };
39
- const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config, createLogger('archiver:lmdb'));
40
- return new KVArchiverDataStore(store, config.maxLogs);
41
+ const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config);
42
+ return new KVArchiverDataStore(store, config.maxLogs, l1Constants);
41
43
  }
42
44
 
43
45
  /**
@@ -52,7 +54,7 @@ export async function createArchiver(
52
54
  deps: ArchiverDeps,
53
55
  opts: { blockUntilSync: boolean } = { blockUntilSync: true },
54
56
  ): Promise<Archiver> {
55
- const archiverStore = await createArchiverStore(config);
57
+ const archiverStore = await createArchiverStore(config, { epochDuration: config.aztecEpochDuration });
56
58
  await registerProtocolContracts(archiverStore);
57
59
 
58
60
  // Create Ethereum clients
@@ -76,14 +78,23 @@ export async function createArchiver(
76
78
  const inbox = new InboxContract(publicClient, config.l1Contracts.inboxAddress);
77
79
 
78
80
  // Fetch L1 constants from rollup contract
79
- const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] =
80
- await Promise.all([
81
- rollup.getL1StartBlock(),
82
- rollup.getL1GenesisTime(),
83
- rollup.getProofSubmissionEpochs(),
84
- rollup.getGenesisArchiveTreeRoot(),
85
- rollup.getSlashingProposerAddress(),
86
- ] as const);
81
+ const [
82
+ l1StartBlock,
83
+ l1GenesisTime,
84
+ proofSubmissionEpochs,
85
+ genesisArchiveRoot,
86
+ slashingProposerAddress,
87
+ targetCommitteeSize,
88
+ rollupManaLimit,
89
+ ] = await Promise.all([
90
+ rollup.getL1StartBlock(),
91
+ rollup.getL1GenesisTime(),
92
+ rollup.getProofSubmissionEpochs(),
93
+ rollup.getGenesisArchiveTreeRoot(),
94
+ rollup.getSlashingProposerAddress(),
95
+ rollup.getTargetCommitteeSize(),
96
+ rollup.getManaLimit(),
97
+ ] as const);
87
98
 
88
99
  const l1StartBlockHash = await publicClient
89
100
  .getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
@@ -99,7 +110,9 @@ export async function createArchiver(
99
110
  slotDuration,
100
111
  ethereumSlotDuration,
101
112
  proofSubmissionEpochs: Number(proofSubmissionEpochs),
113
+ targetCommitteeSize,
102
114
  genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString()),
115
+ rollupManaLimit: Number(rollupManaLimit),
103
116
  };
104
117
 
105
118
  const archiverConfig = merge(
@@ -119,13 +132,15 @@ export async function createArchiver(
119
132
  // Create the event emitter that will be shared by archiver and synchronizer
120
133
  const events = new EventEmitter() as ArchiverEmitter;
121
134
 
135
+ // Create L2 tips cache shared by archiver and synchronizer
136
+ const l2TipsCache = new L2TipsCache(archiverStore.blockStore);
137
+
122
138
  // Create the L1 synchronizer
123
139
  const synchronizer = new ArchiverL1Synchronizer(
124
140
  publicClient,
125
141
  debugClient,
126
142
  rollup,
127
143
  inbox,
128
- { ...config.l1Contracts, slashingProposerAddress },
129
144
  archiverStore,
130
145
  archiverConfig,
131
146
  deps.blobClient,
@@ -135,6 +150,8 @@ export async function createArchiver(
135
150
  l1Constants,
136
151
  events,
137
152
  instrumentation.tracer,
153
+ l2TipsCache,
154
+ undefined, // log (use default)
138
155
  );
139
156
 
140
157
  const archiver = new Archiver(
@@ -149,13 +166,15 @@ export async function createArchiver(
149
166
  l1Constants,
150
167
  synchronizer,
151
168
  events,
169
+ l2TipsCache,
152
170
  );
153
171
 
154
172
  await archiver.start(opts.blockUntilSync);
155
173
  return archiver;
156
174
  }
157
175
 
158
- async function registerProtocolContracts(store: KVArchiverDataStore) {
176
+ /** Registers protocol contracts in the archiver store. */
177
+ export async function registerProtocolContracts(store: KVArchiverDataStore) {
159
178
  const blockNumber = 0;
160
179
  for (const name of protocolContractNames) {
161
180
  const provider = new BundledProtocolContractsProvider();
package/src/index.ts CHANGED
@@ -8,5 +8,6 @@ export * from './config.js';
8
8
  export { type L1PublishedData } from './structs/published.js';
9
9
  export { KVArchiverDataStore, ARCHIVER_DB_VERSION } from './store/kv_archiver_store.js';
10
10
  export { ContractInstanceStore } from './store/contract_instance_store.js';
11
+ export { L2TipsCache } from './store/l2_tips_cache.js';
11
12
 
12
13
  export { retrieveCheckpointsFromRollup, retrieveL2ProofVerifiedEvents } from './l1/data_retrieval.js';