@aztec/archiver 0.0.1-commit.aada20e3 → 0.0.1-commit.b2a5d0dd1

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 (114) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +13 -8
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +90 -114
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +15 -3
  8. package/dest/errors.d.ts +50 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +67 -16
  11. package/dest/factory.d.ts +4 -5
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +33 -27
  14. package/dest/index.d.ts +4 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +3 -1
  17. package/dest/l1/bin/retrieve-calldata.js +36 -33
  18. package/dest/l1/calldata_retriever.d.ts +73 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +191 -259
  21. package/dest/l1/data_retrieval.d.ts +26 -17
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +42 -47
  24. package/dest/l1/spire_proposer.d.ts +5 -5
  25. package/dest/l1/spire_proposer.d.ts.map +1 -1
  26. package/dest/l1/spire_proposer.js +9 -17
  27. package/dest/l1/validate_historical_logs.d.ts +23 -0
  28. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  29. package/dest/l1/validate_historical_logs.js +108 -0
  30. package/dest/modules/data_source_base.d.ts +14 -7
  31. package/dest/modules/data_source_base.d.ts.map +1 -1
  32. package/dest/modules/data_source_base.js +39 -77
  33. package/dest/modules/data_store_updater.d.ts +35 -15
  34. package/dest/modules/data_store_updater.d.ts.map +1 -1
  35. package/dest/modules/data_store_updater.js +137 -96
  36. package/dest/modules/instrumentation.d.ts +21 -3
  37. package/dest/modules/instrumentation.d.ts.map +1 -1
  38. package/dest/modules/instrumentation.js +41 -8
  39. package/dest/modules/l1_synchronizer.d.ts +10 -9
  40. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  41. package/dest/modules/l1_synchronizer.js +279 -150
  42. package/dest/modules/validation.d.ts +1 -1
  43. package/dest/modules/validation.d.ts.map +1 -1
  44. package/dest/modules/validation.js +2 -2
  45. package/dest/store/block_store.d.ts +73 -28
  46. package/dest/store/block_store.d.ts.map +1 -1
  47. package/dest/store/block_store.js +395 -136
  48. package/dest/store/contract_class_store.d.ts +2 -3
  49. package/dest/store/contract_class_store.d.ts.map +1 -1
  50. package/dest/store/contract_class_store.js +7 -67
  51. package/dest/store/contract_instance_store.d.ts +1 -1
  52. package/dest/store/contract_instance_store.d.ts.map +1 -1
  53. package/dest/store/contract_instance_store.js +6 -2
  54. package/dest/store/kv_archiver_store.d.ts +67 -24
  55. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  56. package/dest/store/kv_archiver_store.js +82 -27
  57. package/dest/store/l2_tips_cache.d.ts +20 -0
  58. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  59. package/dest/store/l2_tips_cache.js +109 -0
  60. package/dest/store/log_store.d.ts +6 -3
  61. package/dest/store/log_store.d.ts.map +1 -1
  62. package/dest/store/log_store.js +150 -55
  63. package/dest/store/message_store.d.ts +5 -1
  64. package/dest/store/message_store.d.ts.map +1 -1
  65. package/dest/store/message_store.js +21 -9
  66. package/dest/test/fake_l1_state.d.ts +24 -1
  67. package/dest/test/fake_l1_state.d.ts.map +1 -1
  68. package/dest/test/fake_l1_state.js +145 -28
  69. package/dest/test/mock_archiver.d.ts +1 -1
  70. package/dest/test/mock_archiver.d.ts.map +1 -1
  71. package/dest/test/mock_archiver.js +3 -2
  72. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  73. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  74. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  75. package/dest/test/mock_l2_block_source.d.ts +26 -5
  76. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  77. package/dest/test/mock_l2_block_source.js +160 -89
  78. package/dest/test/mock_structs.d.ts +4 -1
  79. package/dest/test/mock_structs.d.ts.map +1 -1
  80. package/dest/test/mock_structs.js +13 -1
  81. package/dest/test/noop_l1_archiver.d.ts +4 -1
  82. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  83. package/dest/test/noop_l1_archiver.js +9 -3
  84. package/package.json +13 -13
  85. package/src/archiver.ts +120 -137
  86. package/src/config.ts +22 -2
  87. package/src/errors.ts +104 -26
  88. package/src/factory.ts +47 -24
  89. package/src/index.ts +3 -1
  90. package/src/l1/README.md +25 -68
  91. package/src/l1/bin/retrieve-calldata.ts +46 -39
  92. package/src/l1/calldata_retriever.ts +250 -379
  93. package/src/l1/data_retrieval.ts +58 -69
  94. package/src/l1/spire_proposer.ts +7 -15
  95. package/src/l1/validate_historical_logs.ts +140 -0
  96. package/src/modules/data_source_base.ts +78 -98
  97. package/src/modules/data_store_updater.ts +164 -126
  98. package/src/modules/instrumentation.ts +56 -9
  99. package/src/modules/l1_synchronizer.ts +357 -188
  100. package/src/modules/validation.ts +2 -2
  101. package/src/store/block_store.ts +503 -172
  102. package/src/store/contract_class_store.ts +8 -106
  103. package/src/store/contract_instance_store.ts +8 -5
  104. package/src/store/kv_archiver_store.ts +130 -41
  105. package/src/store/l2_tips_cache.ts +134 -0
  106. package/src/store/log_store.ts +221 -63
  107. package/src/store/message_store.ts +27 -10
  108. package/src/structs/inbox_message.ts +1 -1
  109. package/src/test/fake_l1_state.ts +193 -32
  110. package/src/test/mock_archiver.ts +3 -2
  111. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  112. package/src/test/mock_l2_block_source.ts +209 -82
  113. package/src/test/mock_structs.ts +20 -6
  114. package/src/test/noop_l1_archiver.ts +10 -2
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';
@@ -12,35 +11,34 @@ import { EthAddress } from '@aztec/foundation/eth-address';
12
11
  import { type Logger, createLogger } from '@aztec/foundation/log';
13
12
  import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
14
13
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
15
- import { DateProvider } from '@aztec/foundation/timer';
14
+ import { DateProvider, elapsed } from '@aztec/foundation/timer';
16
15
  import {
17
16
  type ArchiverEmitter,
18
- type CheckpointId,
19
- GENESIS_CHECKPOINT_HEADER_HASH,
20
17
  L2Block,
21
18
  type L2BlockSink,
22
19
  type L2Tips,
23
20
  type ValidateCheckpointResult,
24
21
  } from '@aztec/stdlib/block';
25
- import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
22
+ import { type ProposedCheckpointInput, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
26
23
  import {
27
24
  type L1RollupConstants,
28
- getEpochNumberAtTimestamp,
25
+ getEpochAtSlot,
29
26
  getSlotAtNextL1Block,
30
- getSlotAtTimestamp,
31
27
  getSlotRangeForEpoch,
32
28
  getTimestampRangeForEpoch,
33
29
  } from '@aztec/stdlib/epoch-helpers';
34
30
  import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
35
31
 
36
32
  import { type ArchiverConfig, mapArchiverConfig } from './config.js';
37
- import { NoBlobBodiesFoundError } from './errors.js';
33
+ import { BlockAlreadyCheckpointedError, NoBlobBodiesFoundError } from './errors.js';
34
+ import { validateAndLogHistoricalLogsAvailability } from './l1/validate_historical_logs.js';
38
35
  import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
39
36
  import { ArchiverDataSourceBase } from './modules/data_source_base.js';
40
37
  import { ArchiverDataStoreUpdater } from './modules/data_store_updater.js';
41
38
  import type { ArchiverInstrumentation } from './modules/instrumentation.js';
42
39
  import type { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
43
40
  import type { KVArchiverDataStore } from './store/kv_archiver_store.js';
41
+ import { L2TipsCache } from './store/l2_tips_cache.js';
44
42
 
45
43
  /** Export ArchiverEmitter for use in factory and tests. */
46
44
  export type { ArchiverEmitter };
@@ -83,19 +81,23 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
83
81
  /** Helper to handle updates to the store */
84
82
  private readonly updater: ArchiverDataStoreUpdater;
85
83
 
84
+ /** In-memory cache for L2 chain tips. */
85
+ private readonly l2TipsCache: L2TipsCache;
86
+
86
87
  public readonly tracer: Tracer;
87
88
 
89
+ private readonly instrumentation: ArchiverInstrumentation;
90
+
88
91
  /**
89
92
  * Creates a new instance of the Archiver.
90
93
  * @param publicClient - A client for interacting with the Ethereum node.
91
94
  * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
92
95
  * @param rollup - Rollup contract instance.
93
96
  * @param inbox - Inbox contract instance.
94
- * @param l1Addresses - L1 contract addresses (registry, governance proposer, slash factory, slashing proposer).
97
+ * @param l1Addresses - L1 contract addresses (registry, governance proposer, slashing proposer).
95
98
  * @param dataStore - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
96
99
  * @param config - Archiver configuration options.
97
100
  * @param blobClient - Client for retrieving blob data.
98
- * @param epochCache - Cache for epoch-related data.
99
101
  * @param dateProvider - Provider for current date/time.
100
102
  * @param instrumentation - Instrumentation for metrics and tracing.
101
103
  * @param l1Constants - L1 rollup constants.
@@ -107,8 +109,10 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
107
109
  private readonly rollup: RollupContract,
108
110
  private readonly l1Addresses: Pick<
109
111
  L1ContractAddresses,
110
- 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
111
- > & { slashingProposerAddress: EthAddress },
112
+ 'rollupAddress' | 'registryAddress' | 'inboxAddress' | 'governanceProposerAddress'
113
+ > & {
114
+ slashingProposerAddress: EthAddress;
115
+ },
112
116
  readonly dataStore: KVArchiverDataStore,
113
117
  private config: {
114
118
  pollingIntervalMs: number;
@@ -116,21 +120,30 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
116
120
  skipValidateCheckpointAttestations?: boolean;
117
121
  maxAllowedEthClientDriftSeconds: number;
118
122
  ethereumAllowNoDebugHosts?: boolean;
123
+ skipHistoricalLogsCheck?: boolean;
119
124
  },
120
125
  private readonly blobClient: BlobClientInterface,
121
126
  instrumentation: ArchiverInstrumentation,
122
- protected override readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
127
+ protected override readonly l1Constants: L1RollupConstants & {
128
+ l1StartBlockHash: Buffer32;
129
+ genesisArchiveRoot: Fr;
130
+ },
123
131
  synchronizer: ArchiverL1Synchronizer,
124
132
  events: ArchiverEmitter,
133
+ l2TipsCache?: L2TipsCache,
125
134
  private readonly log: Logger = createLogger('archiver'),
126
135
  ) {
127
136
  super(dataStore, l1Constants);
128
137
 
129
138
  this.tracer = instrumentation.tracer;
139
+ this.instrumentation = instrumentation;
130
140
  this.initialSyncPromise = promiseWithResolvers();
131
141
  this.synchronizer = synchronizer;
132
142
  this.events = events;
133
- this.updater = new ArchiverDataStoreUpdater(this.dataStore);
143
+ this.l2TipsCache = l2TipsCache ?? new L2TipsCache(this.dataStore.blockStore);
144
+ this.updater = new ArchiverDataStoreUpdater(this.dataStore, this.l2TipsCache, {
145
+ rollupManaLimit: l1Constants.rollupManaLimit,
146
+ });
134
147
 
135
148
  // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
136
149
  // are done as fast as possible. This then gets updated once the initial sync completes.
@@ -164,6 +177,17 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
164
177
  this.config.ethereumAllowNoDebugHosts ?? false,
165
178
  this.log.getBindings(),
166
179
  );
180
+ await validateAndLogHistoricalLogsAvailability(
181
+ this.publicClient,
182
+ {
183
+ rollupAddress: this.l1Addresses.rollupAddress,
184
+ inboxAddress: this.l1Addresses.inboxAddress,
185
+ registryAddress: this.l1Addresses.registryAddress,
186
+ governanceProposerAddress: this.l1Addresses.governanceProposerAddress,
187
+ },
188
+ this.config.skipHistoricalLogsCheck ?? false,
189
+ this.log.getBindings(),
190
+ );
167
191
 
168
192
  // Log initial state for the archiver
169
193
  const { l1StartBlock } = this.l1Constants;
@@ -203,6 +227,10 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
203
227
  });
204
228
  }
205
229
 
230
+ public async setProposedCheckpoint(pending: ProposedCheckpointInput): Promise<void> {
231
+ await this.updater.setProposedCheckpoint(pending);
232
+ }
233
+
206
234
  /**
207
235
  * Processes all queued blocks, adding them to the store.
208
236
  * Called at the beginning of each sync iteration.
@@ -235,10 +263,16 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
235
263
  }
236
264
 
237
265
  try {
238
- await this.updater.addProposedBlocks([block]);
266
+ const [durationMs] = await elapsed(() => this.updater.addProposedBlock(block));
267
+ this.instrumentation.processNewProposedBlock(durationMs, block);
239
268
  this.log.debug(`Added block ${block.number} to store`);
240
269
  resolve();
241
270
  } catch (err: any) {
271
+ if (err instanceof BlockAlreadyCheckpointedError) {
272
+ this.log.debug(`Proposed block ${block.number} matches already checkpointed block, ignoring late proposal`);
273
+ resolve();
274
+ continue;
275
+ }
242
276
  this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
243
277
  reject(err);
244
278
  }
@@ -330,16 +364,49 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
330
364
  return Promise.resolve(this.synchronizer.getL1Timestamp());
331
365
  }
332
366
 
333
- public getL2SlotNumber(): Promise<SlotNumber | undefined> {
367
+ public async getSyncedL2SlotNumber(): Promise<SlotNumber | undefined> {
368
+ // The synced L2 slot is the latest slot for which we have all L1 data,
369
+ // either because we have seen all L1 blocks for that slot, or because
370
+ // we have seen the corresponding checkpoint.
371
+
372
+ let slotFromL1Sync: SlotNumber | undefined;
334
373
  const l1Timestamp = this.synchronizer.getL1Timestamp();
335
- return Promise.resolve(l1Timestamp === undefined ? undefined : getSlotAtTimestamp(l1Timestamp, this.l1Constants));
374
+ if (l1Timestamp !== undefined) {
375
+ const nextL1BlockSlot = getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
376
+ if (Number(nextL1BlockSlot) > 0) {
377
+ slotFromL1Sync = SlotNumber.add(nextL1BlockSlot, -1);
378
+ }
379
+ }
380
+
381
+ let slotFromCheckpoint: SlotNumber | undefined;
382
+ const latestCheckpointNumber = await this.store.getSynchedCheckpointNumber();
383
+ if (latestCheckpointNumber > 0) {
384
+ const checkpointData = await this.store.getCheckpointData(latestCheckpointNumber);
385
+ if (checkpointData) {
386
+ slotFromCheckpoint = checkpointData.header.slotNumber;
387
+ }
388
+ }
389
+
390
+ if (slotFromL1Sync === undefined && slotFromCheckpoint === undefined) {
391
+ return undefined;
392
+ }
393
+ return SlotNumber(Math.max(slotFromL1Sync ?? 0, slotFromCheckpoint ?? 0));
336
394
  }
337
395
 
338
- public getL2EpochNumber(): Promise<EpochNumber | undefined> {
339
- const l1Timestamp = this.synchronizer.getL1Timestamp();
340
- return Promise.resolve(
341
- l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(l1Timestamp, this.l1Constants),
342
- );
396
+ public async getSyncedL2EpochNumber(): Promise<EpochNumber | undefined> {
397
+ const syncedSlot = await this.getSyncedL2SlotNumber();
398
+ if (syncedSlot === undefined) {
399
+ return undefined;
400
+ }
401
+ // An epoch is fully synced when all its slots are synced.
402
+ // We check if syncedSlot is the last slot of its epoch; if so, that epoch is fully synced.
403
+ // Otherwise, only the previous epoch is fully synced.
404
+ const epoch = getEpochAtSlot(syncedSlot, this.l1Constants);
405
+ const [, endSlot] = getSlotRangeForEpoch(epoch, this.l1Constants);
406
+ if (syncedSlot >= endSlot) {
407
+ return epoch;
408
+ }
409
+ return Number(epoch) > 0 ? EpochNumber(Number(epoch) - 1) : undefined;
343
410
  }
344
411
 
345
412
  public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
@@ -391,115 +458,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
391
458
  return true;
392
459
  }
393
460
 
394
- public async getL2Tips(): Promise<L2Tips> {
395
- const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
396
- this.getBlockNumber(),
397
- this.getProvenBlockNumber(),
398
- this.getCheckpointedL2BlockNumber(),
399
- this.getFinalizedL2BlockNumber(),
400
- ] as const);
401
-
402
- const beforeInitialblockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
403
-
404
- // Get the latest block header and checkpointed blocks for proven, finalised and checkpointed blocks
405
- const [latestBlockHeader, provenCheckpointedBlock, finalizedCheckpointedBlock, checkpointedBlock] =
406
- await Promise.all([
407
- latestBlockNumber > beforeInitialblockNumber ? this.getBlockHeader(latestBlockNumber) : undefined,
408
- provenBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(provenBlockNumber) : undefined,
409
- finalizedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(finalizedBlockNumber) : undefined,
410
- checkpointedBlockNumber > beforeInitialblockNumber
411
- ? this.getCheckpointedBlock(checkpointedBlockNumber)
412
- : undefined,
413
- ] as const);
414
-
415
- if (latestBlockNumber > beforeInitialblockNumber && !latestBlockHeader) {
416
- throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
417
- }
418
-
419
- // Checkpointed blocks must exist for proven, finalized and checkpointed tips if they are beyond the initial block number.
420
- if (checkpointedBlockNumber > beforeInitialblockNumber && !checkpointedBlock?.block.header) {
421
- throw new Error(
422
- `Failed to retrieve checkpointed block header for block ${checkpointedBlockNumber} (latest block is ${latestBlockNumber})`,
423
- );
424
- }
425
-
426
- if (provenBlockNumber > beforeInitialblockNumber && !provenCheckpointedBlock?.block.header) {
427
- throw new Error(
428
- `Failed to retrieve proven checkpointed for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`,
429
- );
430
- }
431
-
432
- if (finalizedBlockNumber > beforeInitialblockNumber && !finalizedCheckpointedBlock?.block.header) {
433
- throw new Error(
434
- `Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`,
435
- );
436
- }
437
-
438
- const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
439
- const provenBlockHeaderHash = (await provenCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
440
- const finalizedBlockHeaderHash =
441
- (await finalizedCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
442
- const checkpointedBlockHeaderHash = (await checkpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
443
-
444
- // Now attempt to retrieve checkpoints for proven, finalised and checkpointed blocks
445
- const [[provenBlockCheckpoint], [finalizedBlockCheckpoint], [checkpointedBlockCheckpoint]] = await Promise.all([
446
- provenCheckpointedBlock !== undefined
447
- ? await this.getCheckpoints(provenCheckpointedBlock?.checkpointNumber, 1)
448
- : [undefined],
449
- finalizedCheckpointedBlock !== undefined
450
- ? await this.getCheckpoints(finalizedCheckpointedBlock?.checkpointNumber, 1)
451
- : [undefined],
452
- checkpointedBlock !== undefined ? await this.getCheckpoints(checkpointedBlock?.checkpointNumber, 1) : [undefined],
453
- ]);
454
-
455
- const initialcheckpointId: CheckpointId = {
456
- number: CheckpointNumber.ZERO,
457
- hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
458
- };
459
-
460
- const makeCheckpointId = (checkpoint: PublishedCheckpoint | undefined) => {
461
- if (checkpoint === undefined) {
462
- return initialcheckpointId;
463
- }
464
- return {
465
- number: checkpoint.checkpoint.number,
466
- hash: checkpoint.checkpoint.hash().toString(),
467
- };
468
- };
469
-
470
- const l2Tips: L2Tips = {
471
- proposed: {
472
- number: latestBlockNumber,
473
- hash: latestBlockHeaderHash.toString(),
474
- },
475
- proven: {
476
- block: {
477
- number: provenBlockNumber,
478
- hash: provenBlockHeaderHash.toString(),
479
- },
480
- checkpoint: makeCheckpointId(provenBlockCheckpoint),
481
- },
482
- finalized: {
483
- block: {
484
- number: finalizedBlockNumber,
485
- hash: finalizedBlockHeaderHash.toString(),
486
- },
487
- checkpoint: makeCheckpointId(finalizedBlockCheckpoint),
488
- },
489
- checkpointed: {
490
- block: {
491
- number: checkpointedBlockNumber,
492
- hash: checkpointedBlockHeaderHash.toString(),
493
- },
494
- checkpoint: makeCheckpointId(checkpointedBlockCheckpoint),
495
- },
496
- };
497
-
498
- return l2Tips;
461
+ public getL2Tips(): Promise<L2Tips> {
462
+ return this.l2TipsCache.getL2Tips();
499
463
  }
500
464
 
501
465
  public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
502
- // TODO(pw/mbps): This still assumes 1 block per checkpoint
503
466
  const currentBlocks = await this.getL2Tips();
504
467
  const currentL2Block = currentBlocks.proposed.number;
505
468
  const currentProvenBlock = currentBlocks.proven.block.number;
@@ -511,8 +474,25 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
511
474
  if (!targetL2Block) {
512
475
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
513
476
  }
514
- const targetL1BlockNumber = targetL2Block.l1.blockNumber;
515
477
  const targetCheckpointNumber = targetL2Block.checkpointNumber;
478
+
479
+ // Rollback operates at checkpoint granularity: the target block must be the last block of its checkpoint.
480
+ const checkpointData = await this.store.getCheckpointData(targetCheckpointNumber);
481
+ if (checkpointData) {
482
+ const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
483
+ if (targetL2BlockNumber !== lastBlockInCheckpoint) {
484
+ const previousCheckpointBoundary =
485
+ checkpointData.startBlock > 1 ? BlockNumber(checkpointData.startBlock - 1) : BlockNumber(0);
486
+ throw new Error(
487
+ `Target L2 block ${targetL2BlockNumber} is not at a checkpoint boundary. ` +
488
+ `Checkpoint ${targetCheckpointNumber} spans blocks ${checkpointData.startBlock} to ${lastBlockInCheckpoint}. ` +
489
+ `Use block ${lastBlockInCheckpoint} to roll back to this checkpoint, ` +
490
+ `or block ${previousCheckpointBoundary} to roll back to the previous one.`,
491
+ );
492
+ }
493
+ }
494
+
495
+ const targetL1BlockNumber = targetL2Block.l1.blockNumber;
516
496
  const targetL1Block = await this.publicClient.getBlock({
517
497
  blockNumber: targetL1BlockNumber,
518
498
  includeTransactions: false,
@@ -529,15 +509,18 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
529
509
  await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
530
510
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
531
511
  await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
532
- await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
512
+ await this.store.setMessageSyncState(
513
+ { l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash },
514
+ undefined,
515
+ );
533
516
  if (targetL2BlockNumber < currentProvenBlock) {
534
- this.log.info(`Clearing proven L2 block number`);
535
- await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
517
+ this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
518
+ await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
519
+ }
520
+ const currentFinalizedBlock = currentBlocks.finalized.block.number;
521
+ if (targetL2BlockNumber < currentFinalizedBlock) {
522
+ this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
523
+ await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
536
524
  }
537
- // TODO(palla/reorg): Set the finalized block when we add support for it.
538
- // if (targetL2BlockNumber < currentFinalizedBlock) {
539
- // this.log.info(`Clearing finalized L2 block number`);
540
- // await this.store.setFinalizedL2BlockNumber(0);
541
- // }
542
525
  }
543
526
  }
package/src/config.ts CHANGED
@@ -8,7 +8,12 @@ import {
8
8
  getConfigFromMappings,
9
9
  numberConfigHelper,
10
10
  } from '@aztec/foundation/config';
11
- import { type ChainConfig, chainConfigMappings } from '@aztec/stdlib/config';
11
+ import {
12
+ type ChainConfig,
13
+ type PipelineConfig,
14
+ chainConfigMappings,
15
+ pipelineConfigMappings,
16
+ } from '@aztec/stdlib/config';
12
17
  import type { ArchiverSpecificConfig } from '@aztec/stdlib/interfaces/server';
13
18
 
14
19
  /**
@@ -21,11 +26,13 @@ import type { ArchiverSpecificConfig } from '@aztec/stdlib/interfaces/server';
21
26
  export type ArchiverConfig = ArchiverSpecificConfig &
22
27
  L1ReaderConfig &
23
28
  L1ContractsConfig &
29
+ PipelineConfig & // required to pass through to epoch cache
24
30
  BlobClientConfig &
25
31
  ChainConfig;
26
32
 
27
33
  export const archiverConfigMappings: ConfigMappingsType<ArchiverConfig> = {
28
34
  ...blobClientConfigMapping,
35
+ ...pipelineConfigMappings,
29
36
  archiverPollingIntervalMS: {
30
37
  env: 'ARCHIVER_POLLING_INTERVAL_MS',
31
38
  description: 'The polling interval in ms for retrieving new L2 blocks and encrypted logs.',
@@ -43,13 +50,17 @@ export const archiverConfigMappings: ConfigMappingsType<ArchiverConfig> = {
43
50
  },
44
51
  archiverStoreMapSizeKb: {
45
52
  env: 'ARCHIVER_STORE_MAP_SIZE_KB',
46
- parseEnv: (val: string | undefined) => (val ? +val : undefined),
53
+ parseEnv: (val: string) => +val,
47
54
  description: 'The maximum possible size of the archiver DB in KB. Overwrites the general dataStoreMapSizeKb.',
48
55
  },
49
56
  skipValidateCheckpointAttestations: {
50
57
  description: 'Skip validating checkpoint attestations (for testing purposes only)',
51
58
  ...booleanConfigHelper(false),
52
59
  },
60
+ skipPromoteProposedCheckpointDuringL1Sync: {
61
+ description: 'Skip promoting proposed checkpoints during L1 sync (for testing purposes only)',
62
+ ...booleanConfigHelper(false),
63
+ },
53
64
  maxAllowedEthClientDriftSeconds: {
54
65
  env: 'MAX_ALLOWED_ETH_CLIENT_DRIFT_SECONDS',
55
66
  description: 'Maximum allowed drift in seconds between the Ethereum client and current time.',
@@ -60,6 +71,13 @@ export const archiverConfigMappings: ConfigMappingsType<ArchiverConfig> = {
60
71
  description: 'Whether to allow starting the archiver without debug/trace method support on Ethereum hosts',
61
72
  ...booleanConfigHelper(true),
62
73
  },
74
+ archiverSkipHistoricalLogsCheck: {
75
+ env: 'ARCHIVER_SKIP_HISTORICAL_LOGS_CHECK',
76
+ description:
77
+ 'Skip the startup check that probes the L1 RPC for historical Rollup contract logs. ' +
78
+ 'Set to true to bypass the check when the connected RPC node is known to prune old logs.',
79
+ ...booleanConfigHelper(false),
80
+ },
63
81
  ...chainConfigMappings,
64
82
  ...l1ReaderConfigMappings,
65
83
  viemPollingIntervalMS: {
@@ -89,7 +107,9 @@ export function mapArchiverConfig(config: Partial<ArchiverConfig>) {
89
107
  pollingIntervalMs: config.archiverPollingIntervalMS,
90
108
  batchSize: config.archiverBatchSize,
91
109
  skipValidateCheckpointAttestations: config.skipValidateCheckpointAttestations,
110
+ skipPromoteProposedCheckpointDuringL1Sync: config.skipPromoteProposedCheckpointDuringL1Sync,
92
111
  maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
93
112
  ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts,
113
+ skipHistoricalLogsCheck: config.archiverSkipHistoricalLogsCheck,
94
114
  };
95
115
  }
package/src/errors.ts CHANGED
@@ -6,24 +6,9 @@ export class NoBlobBodiesFoundError extends Error {
6
6
  }
7
7
  }
8
8
 
9
- export class InitialBlockNumberNotSequentialError extends Error {
10
- constructor(
11
- public readonly newBlockNumber: number,
12
- public readonly previousBlockNumber: number | undefined,
13
- ) {
14
- super(
15
- `Cannot insert new block ${newBlockNumber} given previous block number in store is ${
16
- previousBlockNumber ?? 'undefined'
17
- }`,
18
- );
19
- }
20
- }
21
-
22
9
  export class BlockNumberNotSequentialError extends Error {
23
10
  constructor(newBlockNumber: number, previous: number | undefined) {
24
- super(
25
- `Cannot insert new block ${newBlockNumber} given previous block number in batch is ${previous ?? 'undefined'}`,
26
- );
11
+ super(`Cannot insert new block ${newBlockNumber} given previous block number is ${previous ?? 'undefined'}`);
27
12
  }
28
13
  }
29
14
 
@@ -41,17 +26,13 @@ export class InitialCheckpointNumberNotSequentialError extends Error {
41
26
  }
42
27
 
43
28
  export class CheckpointNumberNotSequentialError extends Error {
44
- constructor(newCheckpointNumber: number, previous: number | undefined) {
45
- super(
46
- `Cannot insert new checkpoint ${newCheckpointNumber} given previous checkpoint number in batch is ${previous ?? 'undefined'}`,
47
- );
48
- }
49
- }
50
-
51
- export class CheckpointNumberNotConsistentError extends Error {
52
- constructor(newCheckpointNumber: number, previous: number | undefined) {
29
+ constructor(
30
+ newCheckpointNumber: number,
31
+ previous: number | undefined,
32
+ source: 'confirmed' | 'proposed' = 'confirmed',
33
+ ) {
53
34
  super(
54
- `Cannot insert block for new checkpoint ${newCheckpointNumber} given previous block was checkpoint ${previous ?? 'undefined'}`,
35
+ `Cannot insert new checkpoint ${newCheckpointNumber} given previous ${source} checkpoint number is ${previous ?? 'undefined'}`,
55
36
  );
56
37
  }
57
38
  }
@@ -89,6 +70,103 @@ export class BlockNotFoundError extends Error {
89
70
  }
90
71
  }
91
72
 
73
+ /** Thrown when a proposed block matches a block that was already checkpointed. This is expected for late proposals. */
74
+ export class BlockAlreadyCheckpointedError extends Error {
75
+ constructor(public readonly blockNumber: number) {
76
+ super(`Block ${blockNumber} has already been checkpointed with the same content`);
77
+ this.name = 'BlockAlreadyCheckpointedError';
78
+ }
79
+ }
80
+
81
+ /** Thrown when logs are added for a tag whose last stored log has a higher block number than the new log. */
82
+ export class OutOfOrderLogInsertionError extends Error {
83
+ constructor(
84
+ public readonly logType: 'private' | 'public',
85
+ public readonly tag: string,
86
+ public readonly lastBlockNumber: number,
87
+ public readonly newBlockNumber: number,
88
+ ) {
89
+ super(
90
+ `Out-of-order ${logType} log insertion for tag ${tag}: ` +
91
+ `last existing log is from block ${lastBlockNumber} but new log is from block ${newBlockNumber}`,
92
+ );
93
+ this.name = 'OutOfOrderLogInsertionError';
94
+ }
95
+ }
96
+
97
+ /** Thrown when L1 to L2 messages are requested for a checkpoint whose message tree hasn't been sealed yet. */
98
+ export class L1ToL2MessagesNotReadyError extends Error {
99
+ constructor(
100
+ public readonly checkpointNumber: number,
101
+ public readonly inboxTreeInProgress: bigint,
102
+ ) {
103
+ super(
104
+ `Cannot get L1 to L2 messages for checkpoint ${checkpointNumber}: ` +
105
+ `inbox tree in progress is ${inboxTreeInProgress}, messages not yet sealed`,
106
+ );
107
+ this.name = 'L1ToL2MessagesNotReadyError';
108
+ }
109
+ }
110
+
111
+ /** Thrown when a proposed checkpoint number is stale (already processed). */
112
+ export class ProposedCheckpointStaleError extends Error {
113
+ constructor(
114
+ public readonly proposedCheckpointNumber: number,
115
+ public readonly currentProposedNumber: number,
116
+ ) {
117
+ super(`Stale proposed checkpoint ${proposedCheckpointNumber}: current proposed is ${currentProposedNumber}`);
118
+ this.name = 'ProposedCheckpointStaleError';
119
+ }
120
+ }
121
+
122
+ /** Thrown when a proposed checkpoint number is not the expected confirmed + 1. */
123
+ export class ProposedCheckpointNotSequentialError extends Error {
124
+ constructor(
125
+ public readonly proposedCheckpointNumber: number,
126
+ public readonly confirmedCheckpointNumber: number,
127
+ ) {
128
+ super(
129
+ `Proposed checkpoint ${proposedCheckpointNumber} is not sequential: expected ${confirmedCheckpointNumber + 1} (confirmed + 1)`,
130
+ );
131
+ this.name = 'ProposedCheckpointNotSequentialError';
132
+ }
133
+ }
134
+
135
+ /** Thrown when attempting to promote a proposed checkpoint but no proposed checkpoint exists in the store. */
136
+ export class NoProposedCheckpointToPromoteError extends Error {
137
+ constructor() {
138
+ super('Cannot promote proposed checkpoint: no proposed checkpoint exists');
139
+ this.name = 'NoProposedCheckpointToPromoteError';
140
+ }
141
+ }
142
+
143
+ /** Thrown when the archive root of the proposed checkpoint does not match the expected one. */
144
+ export class ProposedCheckpointArchiveRootMismatchError extends Error {
145
+ constructor(
146
+ public readonly expectedArchiveRoot: Fr,
147
+ public readonly actualArchiveRoot: Fr,
148
+ ) {
149
+ super(
150
+ `Cannot promote proposed checkpoint: archive root mismatch (expected ${expectedArchiveRoot}, got ${actualArchiveRoot})`,
151
+ );
152
+ this.name = 'ProposedCheckpointArchiveRootMismatchError';
153
+ }
154
+ }
155
+
156
+ /** Thrown when the proposed checkpoint does not directly follow the latest confirmed checkpoint. */
157
+ export class ProposedCheckpointPromotionNotSequentialError extends Error {
158
+ constructor(
159
+ public readonly proposedCheckpointNumber: number,
160
+ public readonly latestCheckpointNumber: number,
161
+ ) {
162
+ super(
163
+ `Cannot promote proposed checkpoint: not sequential (latest ${latestCheckpointNumber}, proposed ${proposedCheckpointNumber})`,
164
+ );
165
+ this.name = 'ProposedCheckpointPromotionNotSequentialError';
166
+ }
167
+ }
168
+
169
+ /** Thrown when a proposed block conflicts with an already checkpointed block (different content). */
92
170
  export class CannotOverwriteCheckpointedBlockError extends Error {
93
171
  constructor(
94
172
  public readonly blockNumber: number,