@aztec/archiver 5.0.0-private.20260319 → 5.0.0-rc.2

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 (121) hide show
  1. package/README.md +19 -13
  2. package/dest/archiver.d.ts +71 -15
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +268 -74
  5. package/dest/config.d.ts +7 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +39 -16
  8. package/dest/errors.d.ts +44 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +66 -12
  11. package/dest/factory.d.ts +12 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +40 -25
  14. package/dest/index.d.ts +11 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +10 -2
  17. package/dest/l1/calldata_retriever.d.ts +2 -1
  18. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  19. package/dest/l1/calldata_retriever.js +15 -5
  20. package/dest/l1/data_retrieval.d.ts +19 -10
  21. package/dest/l1/data_retrieval.d.ts.map +1 -1
  22. package/dest/l1/data_retrieval.js +25 -32
  23. package/dest/l1/trace_tx.d.ts +12 -66
  24. package/dest/l1/trace_tx.d.ts.map +1 -1
  25. package/dest/l1/validate_historical_logs.d.ts +23 -0
  26. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  27. package/dest/l1/validate_historical_logs.js +108 -0
  28. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  29. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  30. package/dest/modules/contract_data_source_adapter.js +32 -0
  31. package/dest/modules/data_source_base.d.ts +72 -48
  32. package/dest/modules/data_source_base.d.ts.map +1 -1
  33. package/dest/modules/data_source_base.js +266 -143
  34. package/dest/modules/data_store_updater.d.ts +30 -13
  35. package/dest/modules/data_store_updater.d.ts.map +1 -1
  36. package/dest/modules/data_store_updater.js +172 -117
  37. package/dest/modules/instrumentation.d.ts +7 -2
  38. package/dest/modules/instrumentation.d.ts.map +1 -1
  39. package/dest/modules/instrumentation.js +25 -7
  40. package/dest/modules/l1_synchronizer.d.ts +11 -6
  41. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  42. package/dest/modules/l1_synchronizer.js +437 -217
  43. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  44. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  45. package/dest/modules/outbox_trees_resolver.js +162 -0
  46. package/dest/modules/validation.d.ts +24 -5
  47. package/dest/modules/validation.d.ts.map +1 -1
  48. package/dest/modules/validation.js +40 -15
  49. package/dest/store/block_store.d.ts +164 -66
  50. package/dest/store/block_store.d.ts.map +1 -1
  51. package/dest/store/block_store.js +638 -233
  52. package/dest/store/contract_class_store.d.ts +17 -4
  53. package/dest/store/contract_class_store.d.ts.map +1 -1
  54. package/dest/store/contract_class_store.js +36 -68
  55. package/dest/store/contract_instance_store.d.ts +28 -1
  56. package/dest/store/contract_instance_store.d.ts.map +1 -1
  57. package/dest/store/contract_instance_store.js +48 -2
  58. package/dest/store/data_stores.d.ts +68 -0
  59. package/dest/store/data_stores.d.ts.map +1 -0
  60. package/dest/store/data_stores.js +54 -0
  61. package/dest/store/function_names_cache.d.ts +17 -0
  62. package/dest/store/function_names_cache.d.ts.map +1 -0
  63. package/dest/store/function_names_cache.js +30 -0
  64. package/dest/store/l2_tips_cache.d.ts +13 -7
  65. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  66. package/dest/store/l2_tips_cache.js +13 -76
  67. package/dest/store/log_store.d.ts +42 -40
  68. package/dest/store/log_store.d.ts.map +1 -1
  69. package/dest/store/log_store.js +257 -480
  70. package/dest/store/log_store_codec.d.ts +78 -0
  71. package/dest/store/log_store_codec.d.ts.map +1 -0
  72. package/dest/store/log_store_codec.js +110 -0
  73. package/dest/store/message_store.d.ts +9 -3
  74. package/dest/store/message_store.d.ts.map +1 -1
  75. package/dest/store/message_store.js +39 -10
  76. package/dest/test/fake_l1_state.d.ts +15 -3
  77. package/dest/test/fake_l1_state.d.ts.map +1 -1
  78. package/dest/test/fake_l1_state.js +80 -18
  79. package/dest/test/mock_l2_block_source.d.ts +48 -50
  80. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  81. package/dest/test/mock_l2_block_source.js +223 -179
  82. package/dest/test/mock_structs.d.ts +1 -1
  83. package/dest/test/mock_structs.d.ts.map +1 -1
  84. package/dest/test/mock_structs.js +2 -2
  85. package/dest/test/noop_l1_archiver.d.ts +14 -6
  86. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  87. package/dest/test/noop_l1_archiver.js +25 -9
  88. package/package.json +14 -14
  89. package/src/archiver.ts +353 -80
  90. package/src/config.ts +43 -20
  91. package/src/errors.ts +103 -18
  92. package/src/factory.ts +56 -24
  93. package/src/index.ts +18 -2
  94. package/src/l1/calldata_retriever.ts +16 -5
  95. package/src/l1/data_retrieval.ts +38 -46
  96. package/src/l1/validate_historical_logs.ts +140 -0
  97. package/src/modules/contract_data_source_adapter.ts +46 -0
  98. package/src/modules/data_source_base.ts +340 -189
  99. package/src/modules/data_store_updater.ts +211 -148
  100. package/src/modules/instrumentation.ts +28 -8
  101. package/src/modules/l1_synchronizer.ts +583 -255
  102. package/src/modules/outbox_trees_resolver.ts +199 -0
  103. package/src/modules/validation.ts +81 -21
  104. package/src/store/block_store.ts +792 -285
  105. package/src/store/contract_class_store.ts +43 -103
  106. package/src/store/contract_instance_store.ts +62 -5
  107. package/src/store/data_stores.ts +104 -0
  108. package/src/store/function_names_cache.ts +37 -0
  109. package/src/store/l2_tips_cache.ts +16 -70
  110. package/src/store/log_store.ts +296 -652
  111. package/src/store/log_store_codec.ts +143 -0
  112. package/src/store/message_store.ts +43 -12
  113. package/src/structs/inbox_message.ts +1 -1
  114. package/src/test/fake_l1_state.ts +99 -27
  115. package/src/test/mock_l2_block_source.ts +279 -233
  116. package/src/test/mock_structs.ts +5 -2
  117. package/src/test/noop_l1_archiver.ts +52 -9
  118. package/dest/store/kv_archiver_store.d.ts +0 -367
  119. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  120. package/dest/store/kv_archiver_store.js +0 -481
  121. package/src/store/kv_archiver_store.ts +0 -697
@@ -1,37 +1,51 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
2
  import { EpochCache } from '@aztec/epoch-cache';
3
- import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
3
+ import { InboxContract, type InboxContractState, RollupContract } from '@aztec/ethereum/contracts';
4
4
  import type { L1BlockId } from '@aztec/ethereum/l1-types';
5
+ import { getFinalizedL1Block } from '@aztec/ethereum/queries';
5
6
  import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
6
7
  import { asyncPool } from '@aztec/foundation/async-pool';
7
8
  import { maxBigint } from '@aztec/foundation/bigint';
8
9
  import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
9
- import { Buffer32 } from '@aztec/foundation/buffer';
10
- import { pick } from '@aztec/foundation/collection';
10
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
11
+ import { compactArray, partition, pick } from '@aztec/foundation/collection';
11
12
  import { Fr } from '@aztec/foundation/curves/bn254';
13
+ import { EthAddress } from '@aztec/foundation/eth-address';
12
14
  import { type Logger, createLogger } from '@aztec/foundation/log';
15
+ import { retryTimes } from '@aztec/foundation/retry';
13
16
  import { count } from '@aztec/foundation/string';
14
17
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
15
- import { isDefined } from '@aztec/foundation/types';
18
+ import { isDefined, isErrorClass } from '@aztec/foundation/types';
16
19
  import { type ArchiverEmitter, L2BlockSourceEvents, type ValidateCheckpointResult } from '@aztec/stdlib/block';
17
- import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
20
+ import {
21
+ Checkpoint,
22
+ type CheckpointData,
23
+ type CheckpointInfo,
24
+ type L1PublishedData,
25
+ PublishedCheckpoint,
26
+ } from '@aztec/stdlib/checkpoint';
18
27
  import { type L1RollupConstants, getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
19
28
  import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
29
+ import type { CoordinationSignatureContext } from '@aztec/stdlib/p2p';
20
30
  import { type Traceable, type Tracer, execInSpan, trackSpan } from '@aztec/telemetry-client';
21
31
 
22
32
  import { InitialCheckpointNumberNotSequentialError } from '../errors.js';
23
33
  import {
24
- retrieveCheckpointsFromRollup,
34
+ type RetrievedCheckpointFromCalldata,
35
+ getCheckpointBlobDataFromBlobs,
36
+ retrieveCheckpointCalldataFromRollup,
25
37
  retrieveL1ToL2Message,
26
38
  retrieveL1ToL2Messages,
27
39
  retrievedToPublishedCheckpoint,
28
40
  } from '../l1/data_retrieval.js';
29
- import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
41
+ import type { RejectedCheckpoint } from '../store/block_store.js';
42
+ import { type ArchiverDataStores, getArchiverSynchPoint } from '../store/data_stores.js';
30
43
  import type { L2TipsCache } from '../store/l2_tips_cache.js';
44
+ import { MessageStoreError } from '../store/message_store.js';
31
45
  import type { InboxMessage } from '../structs/inbox_message.js';
32
46
  import { ArchiverDataStoreUpdater } from './data_store_updater.js';
33
47
  import type { ArchiverInstrumentation } from './instrumentation.js';
34
- import { validateCheckpointAttestations } from './validation.js';
48
+ import { validateCheckpointAttestationsFromCalldata } from './validation.js';
35
49
 
36
50
  type RollupStatus = {
37
51
  provenCheckpointNumber: CheckpointNumber;
@@ -39,13 +53,15 @@ type RollupStatus = {
39
53
  pendingCheckpointNumber: CheckpointNumber;
40
54
  pendingArchive: string;
41
55
  validationResult: ValidateCheckpointResult | undefined;
56
+ /** Last valid checkpoint observed on L1 and synced on this iteration */
42
57
  lastRetrievedCheckpoint?: PublishedCheckpoint;
43
- lastL1BlockWithCheckpoint?: bigint;
58
+ /** Last checkpoint observed on L1 across both valid and rejected entries on this iteration */
59
+ lastSeenCheckpoint?: { checkpointNumber: CheckpointNumber; l1: L1PublishedData };
44
60
  };
45
61
 
46
62
  /**
47
63
  * Handles L1 synchronization for the archiver.
48
- * Responsible for fetching checkpoints, L1L2 messages, and handling L1 reorgs.
64
+ * Responsible for fetching checkpoints, L1 to L2 messages, and handling L1 reorgs.
49
65
  */
50
66
  export class ArchiverL1Synchronizer implements Traceable {
51
67
  private l1BlockNumber: bigint | undefined;
@@ -60,10 +76,11 @@ export class ArchiverL1Synchronizer implements Traceable {
60
76
  private readonly debugClient: ViemPublicDebugClient,
61
77
  private readonly rollup: RollupContract,
62
78
  private readonly inbox: InboxContract,
63
- private readonly store: KVArchiverDataStore,
79
+ private readonly stores: ArchiverDataStores,
64
80
  private config: {
65
81
  batchSize: number;
66
82
  skipValidateCheckpointAttestations?: boolean;
83
+ skipPromoteProposedCheckpointDuringL1Sync?: boolean;
67
84
  maxAllowedEthClientDriftSeconds: number;
68
85
  },
69
86
  private readonly blobClient: BlobClientInterface,
@@ -79,7 +96,7 @@ export class ArchiverL1Synchronizer implements Traceable {
79
96
  l2TipsCache?: L2TipsCache,
80
97
  private readonly log: Logger = createLogger('archiver:l1-sync'),
81
98
  ) {
82
- this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
99
+ this.updater = new ArchiverDataStoreUpdater(this.stores, l2TipsCache, {
83
100
  rollupManaLimit: l1Constants.rollupManaLimit,
84
101
  });
85
102
  this.tracer = tracer;
@@ -89,6 +106,7 @@ export class ArchiverL1Synchronizer implements Traceable {
89
106
  public setConfig(newConfig: {
90
107
  batchSize: number;
91
108
  skipValidateCheckpointAttestations?: boolean;
109
+ skipPromoteProposedCheckpointDuringL1Sync?: boolean;
92
110
  maxAllowedEthClientDriftSeconds: number;
93
111
  }) {
94
112
  this.config = newConfig;
@@ -104,6 +122,13 @@ export class ArchiverL1Synchronizer implements Traceable {
104
122
  return this.l1Timestamp;
105
123
  }
106
124
 
125
+ private getSignatureContext(): CoordinationSignatureContext {
126
+ return {
127
+ chainId: this.publicClient.chain.id,
128
+ rollupAddress: EthAddress.fromString(this.rollup.address),
129
+ };
130
+ }
131
+
107
132
  /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */
108
133
  public async testEthereumNodeSynced(): Promise<void> {
109
134
  const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
@@ -121,65 +146,52 @@ export class ArchiverL1Synchronizer implements Traceable {
121
146
 
122
147
  @trackSpan('Archiver.syncFromL1')
123
148
  public async syncFromL1(initialSyncComplete: boolean): Promise<void> {
149
+ // In between the various calls to L1, the block number can move meaning some of the following
150
+ // calls will return data for blocks that were not present during earlier calls. To combat this
151
+ // we ensure that all data retrieval methods only retrieve data up to the currentBlockNumber
152
+ // captured at the top of this function.
124
153
  const currentL1Block = await this.publicClient.getBlock({ includeTransactions: false });
125
154
  const currentL1BlockNumber = currentL1Block.number;
126
155
  const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
127
156
  const currentL1Timestamp = currentL1Block.timestamp;
157
+ const currentL1BlockData = { l1BlockNumber: currentL1BlockNumber, l1BlockHash: currentL1BlockHash };
128
158
 
129
159
  if (this.l1BlockHash && currentL1BlockHash.equals(this.l1BlockHash)) {
130
160
  this.log.trace(`No new L1 blocks since last sync at L1 block ${this.l1BlockNumber}`);
131
161
  return;
132
162
  }
133
163
 
134
- // Warn if the latest L1 block timestamp is too old
164
+ // Log at error if the latest L1 block timestamp is too old
135
165
  const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
136
166
  const now = this.dateProvider.nowInSeconds();
137
167
  if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
138
- this.log.warn(
168
+ this.log.error(
139
169
  `Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`,
140
170
  { currentL1BlockNumber, currentL1Timestamp, now, maxAllowedDelay },
141
171
  );
142
172
  }
143
173
 
144
- // Load sync point for blocks and messages defaulting to start block
145
- const {
146
- blocksSynchedTo = this.l1Constants.l1StartBlock,
147
- messagesSynchedTo = {
148
- l1BlockNumber: this.l1Constants.l1StartBlock,
149
- l1BlockHash: this.l1Constants.l1StartBlockHash,
150
- },
151
- } = await this.store.getSynchPoint();
174
+ // Query finalized block on L1
175
+ const rawFinalizedL1Block = await getFinalizedL1Block(this.publicClient);
176
+ const finalizedL1Block: L1BlockId | undefined = rawFinalizedL1Block && {
177
+ l1BlockNumber: rawFinalizedL1Block.number,
178
+ l1BlockHash: Buffer32.fromString(rawFinalizedL1Block.hash),
179
+ };
152
180
 
153
- this.log.debug(`Starting new archiver sync iteration`, {
154
- blocksSynchedTo,
155
- messagesSynchedTo,
156
- currentL1BlockNumber,
157
- currentL1BlockHash,
158
- });
181
+ // Load sync point for blocks defaulting to start block
182
+ const { blocksSynchedTo = this.l1Constants.l1StartBlock } = await getArchiverSynchPoint(this.stores);
183
+ this.log.debug(`Starting new archiver sync iteration`, { blocksSynchedTo, currentL1BlockData, finalizedL1Block });
184
+
185
+ // Sync L1 to L2 messages. We retry this a few times since there are error conditions that reset the sync point, requiring a new iteration.
186
+ // Note that we cannot just wait for the l1 synchronizer to loop again, since the synchronizer would report as synced up to the current L1
187
+ // block, when that wouldn't be the case, since L1 to L2 messages would need another iteration.
188
+ await retryTimes(
189
+ () => this.handleL1ToL2Messages(currentL1BlockData, finalizedL1Block),
190
+ 'Handling L1 to L2 messages',
191
+ 3,
192
+ 0.1,
193
+ );
159
194
 
160
- // ********** Ensuring Consistency of data pulled from L1 **********
161
-
162
- /**
163
- * There are a number of calls in this sync operation to L1 for retrieving
164
- * events and transaction data. There are a couple of things we need to bear in mind
165
- * to ensure that data is read exactly once.
166
- *
167
- * The first is the problem of eventually consistent ETH service providers like Infura.
168
- * Each L1 read operation will query data from the last L1 block that it saw emit its kind of data.
169
- * (so pending L1 to L2 messages will read from the last L1 block that emitted a message and so on)
170
- * This will mean the archiver will lag behind L1 and will only advance when there's L2-relevant activity on the chain.
171
- *
172
- * The second is that in between the various calls to L1, the block number can move meaning some
173
- * of the following calls will return data for blocks that were not present during earlier calls.
174
- * To combat this for the time being we simply ensure that all data retrieval methods only retrieve
175
- * data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
176
- * in future but for the time being it should give us the guarantees that we need
177
- */
178
-
179
- // ********** Events that are processed per L1 block **********
180
- await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
181
-
182
- // ********** Events that are processed per checkpoint **********
183
195
  if (currentL1BlockNumber > blocksSynchedTo) {
184
196
  // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
185
197
  // pending chain validation status, proven checkpoint number, and synched L1 block number.
@@ -199,18 +211,10 @@ export class ArchiverL1Synchronizer implements Traceable {
199
211
  currentL1Timestamp,
200
212
  );
201
213
 
202
- // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
203
- // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
204
- // we get a valid checkpoint to advance the syncpoint.
205
- if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
206
- await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
207
- }
208
-
209
214
  // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
210
215
  // We only do this if rollup cant prune on the next submission. Otherwise we will end up
211
- // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
212
- // since the archiver will rightfully refuse to sync up to it.
213
- if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
216
+ // re-syncing the checkpoints we have just unwound above.
217
+ if (!rollupCanPrune) {
214
218
  await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
215
219
  }
216
220
 
@@ -218,7 +222,7 @@ export class ArchiverL1Synchronizer implements Traceable {
218
222
  }
219
223
 
220
224
  // Update the finalized L2 checkpoint based on L1 finality.
221
- await this.updateFinalizedCheckpoint();
225
+ await this.updateFinalizedCheckpoint(finalizedL1Block);
222
226
 
223
227
  // After syncing has completed, update the current l1 block number and timestamp,
224
228
  // otherwise we risk announcing to the world that we've synced to a given point,
@@ -235,15 +239,18 @@ export class ArchiverL1Synchronizer implements Traceable {
235
239
  });
236
240
  }
237
241
 
238
- /** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
239
- private async updateFinalizedCheckpoint(): Promise<void> {
242
+ /** Updates the finalized checkpoint using the pre-fetched finalized L1 block from the current sync iteration. */
243
+ private async updateFinalizedCheckpoint(finalizedL1Block: L1BlockId | undefined): Promise<void> {
240
244
  try {
241
- const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
242
- const finalizedL1BlockNumber = finalizedL1Block.number;
245
+ if (!finalizedL1Block) {
246
+ this.log.trace(`Skipping finalized checkpoint update: L1 has no finalized block yet.`);
247
+ return;
248
+ }
249
+ const finalizedL1BlockNumber = finalizedL1Block.l1BlockNumber;
243
250
  const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
244
251
  blockNumber: finalizedL1BlockNumber,
245
252
  });
246
- const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
253
+ const localFinalizedCheckpointNumber = await this.stores.blocks.getFinalizedCheckpointNumber();
247
254
  if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
248
255
  await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
249
256
  this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
@@ -251,16 +258,19 @@ export class ArchiverL1Synchronizer implements Traceable {
251
258
  finalizedL1BlockNumber,
252
259
  });
253
260
  }
254
- } catch (err) {
255
- this.log.warn(`Failed to update finalized checkpoint: ${err}`);
261
+ } catch (err: any) {
262
+ // The rollup contract may not exist at the finalized L1 block right after deployment.
263
+ if (!err?.message?.includes('returned no data')) {
264
+ this.log.warn(`Failed to update finalized checkpoint: ${err}`);
265
+ }
256
266
  }
257
267
  }
258
268
 
259
269
  /** Prune all proposed local blocks that should have been checkpointed by now. */
260
270
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
261
271
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
262
- this.store.getCheckpointedL2BlockNumber(),
263
- this.store.getLatestBlockNumber(),
272
+ this.stores.blocks.getCheckpointedL2BlockNumber(),
273
+ this.stores.blocks.getLatestL2BlockNumber(),
264
274
  ]);
265
275
 
266
276
  // If there are no uncheckpointed blocks, we got nothing to do
@@ -269,29 +279,34 @@ export class ArchiverL1Synchronizer implements Traceable {
269
279
  return;
270
280
  }
271
281
 
272
- // What's the slot of the first uncheckpointed block?
273
- const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
274
- const [firstUncheckpointedBlockHeader] = await this.store.getBlockHeaders(firstUncheckpointedBlockNumber, 1);
275
- const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
276
-
277
282
  // What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
278
283
  const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
284
+ const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
279
285
 
280
- // Prune provisional blocks from slots that have ended without being checkpointed
281
- if (firstUncheckpointedBlockSlot !== undefined && firstUncheckpointedBlockSlot < slotAtNextL1Block) {
282
- this.log.warn(
283
- `Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`,
284
- { firstUncheckpointedBlockHeader: firstUncheckpointedBlockHeader.toInspect(), slotAtNextL1Block },
285
- );
286
- const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
286
+ // What's the slot of the first uncheckpointed block?
287
+ const firstUncheckpointedBlockData = await this.stores.blocks.getBlockData({
288
+ number: firstUncheckpointedBlockNumber,
289
+ });
290
+ const firstUncheckpointedBlockSlot = firstUncheckpointedBlockData?.header.getSlot();
287
291
 
288
- if (prunedBlocks.length > 0) {
289
- this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
290
- type: L2BlockSourceEvents.L2PruneUncheckpointed,
291
- slotNumber: firstUncheckpointedBlockSlot,
292
- blocks: prunedBlocks,
293
- });
294
- }
292
+ if (firstUncheckpointedBlockSlot === undefined || firstUncheckpointedBlockSlot >= slotAtNextL1Block) {
293
+ return;
294
+ }
295
+
296
+ // Prune provisional blocks from slots that have ended without being checkpointed.
297
+ // This also clears any proposed checkpoint whose blocks are being pruned.
298
+ this.log.warn(
299
+ `Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`,
300
+ { firstUncheckpointedBlockHeader: firstUncheckpointedBlockData?.header.toInspect(), slotAtNextL1Block },
301
+ );
302
+
303
+ const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
304
+ if (prunedBlocks.length > 0) {
305
+ this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
306
+ type: L2BlockSourceEvents.L2PruneUncheckpointed,
307
+ slotNumber: firstUncheckpointedBlockSlot,
308
+ blocks: prunedBlocks,
309
+ });
295
310
  }
296
311
  }
297
312
 
@@ -317,7 +332,7 @@ export class ArchiverL1Synchronizer implements Traceable {
317
332
  currentL1Timestamp: bigint,
318
333
  ): Promise<{ rollupCanPrune: boolean }> {
319
334
  const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
320
- const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
335
+ const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
321
336
  const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
322
337
 
323
338
  if (canPrune) {
@@ -338,12 +353,12 @@ export class ArchiverL1Synchronizer implements Traceable {
338
353
  // promises when the gap between local pending and proven checkpoint numbers is large.
339
354
  const BATCH_SIZE = 10;
340
355
  const indices = Array.from({ length: checkpointsToUnwind }, (_, i) => CheckpointNumber(i + pruneFrom));
341
- const checkpoints = (await asyncPool(BATCH_SIZE, indices, idx => this.store.getCheckpointData(idx))).filter(
342
- isDefined,
343
- );
356
+ const checkpoints = (
357
+ await asyncPool(BATCH_SIZE, indices, idx => this.stores.blocks.getCheckpointData(idx))
358
+ ).filter(isDefined);
344
359
  const newBlocks = (
345
360
  await asyncPool(BATCH_SIZE, checkpoints, cp =>
346
- this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)),
361
+ this.stores.blocks.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)),
347
362
  )
348
363
  )
349
364
  .filter(isDefined)
@@ -363,12 +378,12 @@ export class ArchiverL1Synchronizer implements Traceable {
363
378
  this.log.warn(
364
379
  `Removed ${count(checkpointsToUnwind, 'checkpoint')} after checkpoint ${provenCheckpointNumber} ` +
365
380
  `due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
366
- `Updated latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`,
381
+ `Updated latest checkpoint is ${await this.stores.blocks.getLatestCheckpointNumber()}.`,
367
382
  );
368
383
  this.instrumentation.processPrune(timer.ms());
369
384
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
370
385
  // Seems like the next iteration should handle this.
371
- // await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
386
+ // await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
372
387
  }
373
388
 
374
389
  return { rollupCanPrune };
@@ -385,68 +400,102 @@ export class ArchiverL1Synchronizer implements Traceable {
385
400
  }
386
401
 
387
402
  @trackSpan('Archiver.handleL1ToL2Messages')
388
- private async handleL1ToL2Messages(messagesSyncPoint: L1BlockId, currentL1BlockNumber: bigint): Promise<void> {
389
- this.log.trace(`Handling L1 to L2 messages from ${messagesSyncPoint.l1BlockNumber} to ${currentL1BlockNumber}.`);
390
- if (currentL1BlockNumber <= messagesSyncPoint.l1BlockNumber) {
391
- return;
403
+ private async handleL1ToL2Messages(
404
+ currentL1Block: L1BlockId,
405
+ finalizedL1Block: L1BlockId | undefined,
406
+ ): Promise<boolean> {
407
+ // Load the syncpoint, which may have been updated in a previous iteration
408
+ const {
409
+ messagesSynchedTo = {
410
+ l1BlockNumber: this.l1Constants.l1StartBlock,
411
+ l1BlockHash: this.l1Constants.l1StartBlockHash,
412
+ },
413
+ } = await getArchiverSynchPoint(this.stores);
414
+
415
+ // Nothing to do if L1 block number has not moved forward
416
+ const currentL1BlockNumber = currentL1Block.l1BlockNumber;
417
+ if (currentL1BlockNumber <= messagesSynchedTo.l1BlockNumber) {
418
+ return true;
392
419
  }
393
420
 
394
- // Load remote and local inbox states.
395
- const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
396
- const localLastMessage = await this.store.getLastL1ToL2Message();
421
+ // Compare local message store state with the remote. If they match, we just advance the match pointer.
397
422
  const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
398
- await this.store.setInboxTreeInProgress(remoteMessagesState.treeInProgress);
423
+ const localLastMessage = await this.stores.messages.getLastMessage();
424
+ if (await this.localStateMatches(localLastMessage, remoteMessagesState)) {
425
+ this.log.trace(`Local L1 to L2 messages are already in sync with remote at L1 block ${currentL1BlockNumber}`);
426
+ await this.stores.messages.setMessageSyncState(
427
+ currentL1Block,
428
+ remoteMessagesState.treeInProgress,
429
+ finalizedL1Block,
430
+ );
431
+ return true;
432
+ }
399
433
 
400
- this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
401
- localMessagesInserted,
402
- localLastMessage,
403
- remoteMessagesState,
404
- });
434
+ // If not, then we are out of sync. Most likely there are new messages on the inbox, so we try retrieving them.
435
+ // However, it could also be the case that there was an L1 reorg and our syncpoint is no longer valid.
436
+ // If that's the case, we'd get an exception out of the message store since the rolling hash of the first message
437
+ // we try to insert would not match the one in the db, in which case we rollback to the last common message with L1.
438
+ try {
439
+ await this.retrieveAndStoreMessages(messagesSynchedTo.l1BlockNumber, currentL1BlockNumber);
440
+ } catch (error) {
441
+ if (isErrorClass(error, MessageStoreError)) {
442
+ this.log.warn(
443
+ `Failed to store L1 to L2 messages retrieved from L1: ${error.message}. Rolling back syncpoint to retry.`,
444
+ { inboxMessage: error.inboxMessage },
445
+ );
446
+ await this.rollbackL1ToL2Messages(remoteMessagesState);
447
+ return false;
448
+ }
449
+ throw error;
450
+ }
405
451
 
406
- // Compare message count and rolling hash. If they match, no need to retrieve anything.
407
- if (
408
- remoteMessagesState.totalMessagesInserted === localMessagesInserted &&
409
- remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer32.ZERO)
410
- ) {
411
- this.log.trace(
412
- `No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`,
452
+ // Note that, if there are no new messages to insert, but there was an L1 reorg that pruned out last messages,
453
+ // we'd notice by comparing our local state with the remote one again, and seeing they don't match even after
454
+ // our sync attempt. In this case, we also rollback our syncpoint, and trigger a retry.
455
+ const localLastMessageAfterSync = await this.stores.messages.getLastMessage();
456
+ if (!(await this.localStateMatches(localLastMessageAfterSync, remoteMessagesState))) {
457
+ this.log.warn(
458
+ `Local L1 to L2 messages state does not match remote after sync attempt. Rolling back syncpoint to retry.`,
459
+ { localLastMessageAfterSync, remoteMessagesState },
413
460
  );
414
- return;
461
+ await this.rollbackL1ToL2Messages(remoteMessagesState);
462
+ return false;
415
463
  }
416
464
 
417
- // Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
418
- // Note that we need to fetch it from logs and not from inbox state at the syncpoint l1 block number, since it
419
- // could be older than 128 blocks and non-archive nodes cannot resolve it.
420
- if (localLastMessage) {
421
- const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
422
- this.log.trace(`Retrieved remote message for local last`, { remoteLastMessage, localLastMessage });
423
- if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
424
- this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
425
- remoteLastMessage,
426
- messagesSyncPoint,
427
- localLastMessage,
428
- });
465
+ // Advance the syncpoint after a successful sync
466
+ await this.stores.messages.setMessageSyncState(
467
+ currentL1Block,
468
+ remoteMessagesState.treeInProgress,
469
+ finalizedL1Block,
470
+ );
471
+ return true;
472
+ }
429
473
 
430
- messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
431
- this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
432
- messagesSyncPoint,
433
- });
434
- }
435
- }
474
+ /** Checks if the local rolling hash and message count matches the remote state */
475
+ private async localStateMatches(localLastMessage: InboxMessage | undefined, remoteState: InboxContractState) {
476
+ const localMessageCount = await this.stores.messages.getTotalL1ToL2MessageCount();
477
+ this.log.trace(`Comparing local and remote inbox state`, { localMessageCount, localLastMessage, remoteState });
478
+
479
+ return (
480
+ remoteState.totalMessagesInserted === localMessageCount &&
481
+ remoteState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
482
+ );
483
+ }
436
484
 
437
- // Retrieve and save messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
485
+ /** Retrieves L1 to L2 messages from L1 in batches and stores them. */
486
+ private async retrieveAndStoreMessages(fromL1Block: bigint, toL1Block: bigint): Promise<void> {
438
487
  let searchStartBlock: bigint = 0n;
439
- let searchEndBlock: bigint = messagesSyncPoint.l1BlockNumber;
488
+ let searchEndBlock: bigint = fromL1Block;
440
489
 
441
490
  let lastMessage: InboxMessage | undefined;
442
491
  let messageCount = 0;
443
492
 
444
493
  do {
445
- [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
494
+ [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, toL1Block);
446
495
  this.log.trace(`Retrieving L1 to L2 messages in L1 blocks ${searchStartBlock}-${searchEndBlock}`);
447
496
  const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
448
497
  const timer = new Timer();
449
- await this.store.addL1ToL2Messages(messages);
498
+ await this.stores.messages.addL1ToL2Messages(messages);
450
499
  const perMsg = timer.ms() / messages.length;
451
500
  this.instrumentation.processNewMessages(messages.length, perMsg);
452
501
  for (const msg of messages) {
@@ -454,81 +503,108 @@ export class ArchiverL1Synchronizer implements Traceable {
454
503
  lastMessage = msg;
455
504
  messageCount++;
456
505
  }
457
- } while (searchEndBlock < currentL1BlockNumber);
506
+ } while (searchEndBlock < toL1Block);
458
507
 
459
- // Log stats for messages retrieved (if any).
460
508
  if (messageCount > 0) {
461
509
  this.log.info(
462
510
  `Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`,
463
511
  { lastMessage, messageCount },
464
512
  );
465
513
  }
466
-
467
- // Warn if the resulting rolling hash does not match the remote state we had retrieved.
468
- if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
469
- this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
470
- lastMessage,
471
- remoteMessagesState,
472
- });
473
- }
474
514
  }
475
515
 
476
- private async retrieveL1ToL2Message(leaf: Fr): Promise<InboxMessage | undefined> {
477
- const currentL1BlockNumber = await this.publicClient.getBlockNumber();
478
- let searchStartBlock: bigint = 0n;
479
- let searchEndBlock: bigint = this.l1Constants.l1StartBlock - 1n;
516
+ /**
517
+ * Rolls back local L1 to L2 messages to the last common message with L1, and updates the syncpoint to the L1 block of that message.
518
+ * If no common message is found, rolls back all messages and sets the syncpoint to the start block.
519
+ */
520
+ private async rollbackL1ToL2Messages(remoteMessagesState: InboxContractState): Promise<L1BlockId> {
521
+ const { treeInProgress: remoteTreeInProgress, messagesRollingHash: remoteRollingHash } = remoteMessagesState;
480
522
 
481
- do {
482
- [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
523
+ const messagesFinalizedL1Block = await this.stores.messages.getMessagesFinalizedL1Block();
524
+ const finalizedL1BlockNumber = messagesFinalizedL1Block?.l1BlockNumber;
483
525
 
484
- const message = await retrieveL1ToL2Message(this.inbox, leaf, searchStartBlock, searchEndBlock);
485
-
486
- if (message) {
487
- return message;
526
+ // Slowly go back through our messages until we find the last common message. We could query the logs in
527
+ // batch as an optimization, but the depth of the reorg should not be deep, and this is a very rare case,
528
+ // so it's fine to query one log at a time.
529
+ let commonMsg: undefined | InboxMessage;
530
+ let messagesToDelete = 0;
531
+ this.log.verbose(`Searching most recent common L1 to L2 message`);
532
+ for await (const localMsg of this.stores.messages.iterateL1ToL2Messages({ reverse: true })) {
533
+ const logCtx = { remoteMsg: undefined as InboxMessage | undefined, localMsg, remoteMessagesState };
534
+
535
+ // First check if the local message rolling hash matches the current rolling hash of the inbox contract,
536
+ // which means we just need to rollback some local messages and we should be back in sync. This means there
537
+ // was an L1 reorg that removed some of the messages we had, but no new messages were added compared.
538
+ if (localMsg.rollingHash.equals(remoteRollingHash)) {
539
+ this.log.info(
540
+ `Found common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber} matching current remote state`,
541
+ logCtx,
542
+ );
543
+ commonMsg = localMsg;
544
+ break;
488
545
  }
489
- } while (searchEndBlock < currentL1BlockNumber);
490
546
 
491
- return undefined;
492
- }
547
+ // Messages at or below the finalized L1 block cannot have been reorged — accept as common without querying L1.
548
+ if (finalizedL1BlockNumber !== undefined && localMsg.l1BlockNumber <= finalizedL1BlockNumber) {
549
+ this.log.info(`Found common L1 to L2 message at finalized L1 block ${localMsg.l1BlockNumber}`, logCtx);
550
+ commonMsg = localMsg;
551
+ break;
552
+ }
493
553
 
494
- private async rollbackL1ToL2Messages(
495
- localLastMessage: InboxMessage,
496
- messagesSyncPoint: L1BlockId,
497
- ): Promise<L1BlockId> {
498
- // Slowly go back through our messages until we find the last common message.
499
- // We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
500
- // is a very rare case, so it's fine to query one log at a time.
501
- let commonMsg: undefined | InboxMessage;
502
- this.log.verbose(`Searching most recent common L1 to L2 message at or before index ${localLastMessage.index}`);
503
- for await (const msg of this.store.iterateL1ToL2Messages({ reverse: true, end: localLastMessage.index })) {
504
- const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
505
- const logCtx = { remoteMsg, localMsg: msg };
506
- if (remoteMsg && remoteMsg.rollingHash.equals(msg.rollingHash)) {
507
- this.log.verbose(
508
- `Found most recent common L1 to L2 message at index ${msg.index} on L1 block ${msg.l1BlockNumber}`,
554
+ // If there's no match with the current remote state, check if the message exists on the inbox contract at all
555
+ // by looking at the inbox events. If the L1 reorg *added* new messages in addition to deleting existing ones,
556
+ // then the current remote state's rolling hash will not match anything we have locally, so we need to check existence
557
+ // of individual messages via logs. Note we use logs and not historical queries so we don't have to depend on
558
+ // an archival rpc node, since the message could be from a long time ago if we're catching up with syncing.
559
+ const remoteMsg = await retrieveL1ToL2Message(this.inbox, localMsg);
560
+ logCtx.remoteMsg = remoteMsg;
561
+ if (remoteMsg && remoteMsg.rollingHash.equals(localMsg.rollingHash)) {
562
+ this.log.info(
563
+ `Found most recent common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber}`,
509
564
  logCtx,
510
565
  );
511
566
  commonMsg = remoteMsg;
512
567
  break;
513
568
  } else if (remoteMsg) {
514
- this.log.debug(`Local L1 to L2 message with index ${msg.index} has different rolling hash`, logCtx);
569
+ this.log.debug(`Local L1 to L2 message with index ${localMsg.index} has different rolling hash`, logCtx);
570
+ messagesToDelete++;
515
571
  } else {
516
- this.log.debug(`Local L1 to L2 message with index ${msg.index} not found on L1`, logCtx);
572
+ this.log.debug(`Local L1 to L2 message with index ${localMsg.index} not found on L1`, logCtx);
573
+ messagesToDelete++;
517
574
  }
518
575
  }
519
576
 
520
- // Delete everything after the common message we found.
521
- const lastGoodIndex = commonMsg?.index;
522
- this.log.warn(`Deleting all local L1 to L2 messages after index ${lastGoodIndex ?? 'undefined'}`);
523
- await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
577
+ // Delete everything after the common message we found, if anything needs to be deleted.
578
+ // Do not exit early if there are no messages to delete, we still want to update the syncpoint.
579
+ if (messagesToDelete > 0) {
580
+ const lastGoodIndex = commonMsg?.index;
581
+ this.log.warn(`Rolling back all local L1 to L2 messages after index ${lastGoodIndex ?? 'initial'}`);
582
+ await this.stores.messages.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
583
+ }
524
584
 
525
585
  // Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
526
586
  // the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
527
- // after the last common message.
528
- const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1Constants.l1StartBlock;
529
- const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
530
- messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
531
- await this.store.setMessageSynchedL1Block(messagesSyncPoint);
587
+ // after the last common message. Cap at the finalized L1 block: messages at or below finalized cannot
588
+ // have been reorged, so there is no need to walk back any further than that.
589
+ const syncPointL1BlockNumber = maxBigint(
590
+ ...compactArray([
591
+ commonMsg ? commonMsg.l1BlockNumber - 1n : undefined,
592
+ finalizedL1BlockNumber,
593
+ this.l1Constants.l1StartBlock,
594
+ ]),
595
+ );
596
+
597
+ const syncPointL1BlockHash =
598
+ syncPointL1BlockNumber === finalizedL1BlockNumber
599
+ ? messagesFinalizedL1Block!.l1BlockHash
600
+ : await this.getL1BlockHash(syncPointL1BlockNumber);
601
+
602
+ const messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
603
+ await this.stores.messages.setMessageSyncState(messagesSyncPoint, remoteTreeInProgress);
604
+ this.log.verbose(`Updated messages syncpoint to L1 block ${messagesSyncPoint.l1BlockNumber}`, {
605
+ ...messagesSyncPoint,
606
+ remoteTreeInProgress,
607
+ });
532
608
  return messagesSyncPoint;
533
609
  }
534
610
 
@@ -546,9 +622,9 @@ export class ArchiverL1Synchronizer implements Traceable {
546
622
  currentL1BlockNumber: bigint,
547
623
  initialSyncComplete: boolean,
548
624
  ): Promise<RollupStatus> {
549
- const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
625
+ const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
550
626
  const initialValidationResult: ValidateCheckpointResult | undefined =
551
- await this.store.getPendingChainValidationStatus();
627
+ await this.stores.blocks.getPendingChainValidationStatus();
552
628
  const {
553
629
  provenCheckpointNumber,
554
630
  provenArchive,
@@ -578,7 +654,7 @@ export class ArchiverL1Synchronizer implements Traceable {
578
654
  // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
579
655
  // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
580
656
  if (provenCheckpointNumber === 0) {
581
- const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
657
+ const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
582
658
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
583
659
  await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
584
660
  this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
@@ -586,11 +662,11 @@ export class ArchiverL1Synchronizer implements Traceable {
586
662
  }
587
663
 
588
664
  const localCheckpointForDestinationProvenCheckpointNumber =
589
- await this.store.getCheckpointData(provenCheckpointNumber);
665
+ await this.stores.blocks.getCheckpointData(provenCheckpointNumber);
590
666
 
591
667
  // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
592
668
  // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
593
- const synched = await this.store.getSynchedCheckpointNumber();
669
+ const synched = await this.stores.blocks.getLatestCheckpointNumber();
594
670
  if (
595
671
  localCheckpointForDestinationProvenCheckpointNumber &&
596
672
  synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber
@@ -610,7 +686,7 @@ export class ArchiverL1Synchronizer implements Traceable {
610
686
  localCheckpointForDestinationProvenCheckpointNumber &&
611
687
  provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)
612
688
  ) {
613
- const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
689
+ const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
614
690
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
615
691
  await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
616
692
  this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
@@ -638,7 +714,7 @@ export class ArchiverL1Synchronizer implements Traceable {
638
714
  // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
639
715
  const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
640
716
  if (noCheckpoints) {
641
- await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
717
+ await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
642
718
  this.log.debug(
643
719
  `No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`,
644
720
  );
@@ -650,7 +726,7 @@ export class ArchiverL1Synchronizer implements Traceable {
650
726
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
651
727
  // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
652
728
  if (localPendingCheckpointNumber > 0) {
653
- const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
729
+ const localPendingCheckpoint = await this.stores.blocks.getCheckpointData(localPendingCheckpointNumber);
654
730
  if (localPendingCheckpoint === undefined) {
655
731
  throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
656
732
  }
@@ -665,7 +741,7 @@ export class ArchiverL1Synchronizer implements Traceable {
665
741
  // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
666
742
  // We must only set this block number based on actually retrieved logs.
667
743
  // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
668
- // await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
744
+ // await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
669
745
  this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
670
746
  return rollupStatus;
671
747
  }
@@ -685,7 +761,7 @@ export class ArchiverL1Synchronizer implements Traceable {
685
761
 
686
762
  let tipAfterUnwind = localPendingCheckpointNumber;
687
763
  while (true) {
688
- const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
764
+ const candidateCheckpoint = await this.stores.blocks.getCheckpointData(tipAfterUnwind);
689
765
  if (candidateCheckpoint === undefined) {
690
766
  break;
691
767
  }
@@ -710,7 +786,7 @@ export class ArchiverL1Synchronizer implements Traceable {
710
786
  this.log.warn(
711
787
  `Removed ${count(checkpointsToRemove, 'checkpoint')} after checkpoint ${tipAfterUnwind} ` +
712
788
  `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` +
713
- `Updated L2 latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`,
789
+ `Updated L2 latest checkpoint is ${await this.stores.blocks.getLatestCheckpointNumber()}.`,
714
790
  );
715
791
  }
716
792
  }
@@ -720,29 +796,27 @@ export class ArchiverL1Synchronizer implements Traceable {
720
796
  let searchStartBlock: bigint = blocksSynchedTo;
721
797
  let searchEndBlock: bigint = blocksSynchedTo;
722
798
  let lastRetrievedCheckpoint: PublishedCheckpoint | undefined;
723
- let lastL1BlockWithCheckpoint: bigint | undefined = undefined;
799
+ let lastSeenCheckpoint: { checkpointNumber: CheckpointNumber; l1: L1PublishedData } | undefined;
724
800
 
725
801
  do {
726
802
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
727
803
 
728
804
  this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
729
805
 
730
- // TODO(md): Retrieve from blob client then from consensus client, then from peers
731
- const retrievedCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointsFromRollup', () =>
732
- retrieveCheckpointsFromRollup(
806
+ // First fetch calldata only, no blobs yet, since we may be able to just get that data out of the proposed chain
807
+ const calldataCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointCalldataFromRollup', () =>
808
+ retrieveCheckpointCalldataFromRollup(
733
809
  this.rollup,
734
810
  this.publicClient,
735
811
  this.debugClient,
736
- this.blobClient,
737
812
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
738
813
  searchEndBlock,
739
814
  this.instrumentation,
740
815
  this.log,
741
- !initialSyncComplete, // isHistoricalSync
742
816
  ),
743
817
  );
744
818
 
745
- if (retrievedCheckpoints.length === 0) {
819
+ if (calldataCheckpoints.length === 0) {
746
820
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
747
821
  // See further details in earlier comments.
748
822
  this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
@@ -750,40 +824,74 @@ export class ArchiverL1Synchronizer implements Traceable {
750
824
  }
751
825
 
752
826
  this.log.debug(
753
- `Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
827
+ `Retrieved ${calldataCheckpoints.length} new checkpoint calldata between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
754
828
  {
755
- lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
829
+ lastProcessedCheckpoint: calldataCheckpoints[calldataCheckpoints.length - 1].l1,
756
830
  searchStartBlock,
757
831
  searchEndBlock,
758
832
  },
759
833
  );
760
834
 
761
- const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map(b => retrievedToPublishedCheckpoint(b)));
762
- const validCheckpoints: PublishedCheckpoint[] = [];
763
-
764
- for (const published of publishedCheckpoints) {
835
+ // Check if the last checkpoint matches a local pending entry (so we can skip blob fetch).
836
+ // We only check the last one; if it matches, the blob fetch is skipped for that entry.
837
+ // TODO(palla/pipelining): We may have more than a single checkpoint to promote
838
+ const lastCalldataCheckpoint = calldataCheckpoints[calldataCheckpoints.length - 1];
839
+ const promoteResult = await this.tryBuildPublishedCheckpointFromProposed(lastCalldataCheckpoint);
840
+ const checkpointToPromote = promoteResult && !('diverged' in promoteResult) ? promoteResult : undefined;
841
+ const evictProposedFrom =
842
+ promoteResult && 'diverged' in promoteResult ? promoteResult.fromCheckpointNumber : undefined;
843
+
844
+ // Validate attestations from CALLDATA before fetching any blobs. A checkpoint with invalid
845
+ // attestations (or one descending from a rejected ancestor) is rejected here without fetching its
846
+ // blobs, so a malformed blob does not throw during decode before the rejection path runs and
847
+ // stall sync. The signed consensus payload (header, archive root, fee asset price
848
+ // modifier) is fully available from calldata.
849
+ const checkpointsToIngest: RetrievedCheckpointFromCalldata[] = [];
850
+
851
+ for (const calldataCheckpoint of calldataCheckpoints) {
852
+ // Check the attestations uploaded by the publisher to L1 are correct.
853
+ // Rollup contract does not validate attestations to save on gas, so this
854
+ // falls on the nodes to verify offchain and skip those checkpoints.
765
855
  const validationResult = this.config.skipValidateCheckpointAttestations
766
856
  ? { valid: true as const }
767
- : await validateCheckpointAttestations(published, this.epochCache, this.l1Constants, this.log);
857
+ : await validateCheckpointAttestationsFromCalldata(
858
+ calldataCheckpoint,
859
+ this.epochCache,
860
+ this.l1Constants,
861
+ this.getSignatureContext(),
862
+ this.log,
863
+ );
864
+
865
+ // Also skip the checkpoint if it builds on a previously-rejected ancestor. Without
866
+ // this, addCheckpoints would throw InitialCheckpointNumberNotSequentialError when the
867
+ // ancestor was skipped earlier (e.g. due to invalid attestations), the catch handler
868
+ // would roll back the L1 sync point, and the next iteration would re-fetch and re-throw.
869
+ const rejectedAncestor = await this.stores.blocks.getRejectedCheckpointByArchiveRoot(
870
+ calldataCheckpoint.header.lastArchiveRoot,
871
+ );
768
872
 
769
- // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
873
+ // Update the validation result if it has changed, so we can keep track of the first invalid checkpoint
770
874
  // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
771
875
  // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
772
876
  // we need to update the validation result, since we need to be able to invalidate the new one.
773
877
  // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
774
- if (
775
- rollupStatus.validationResult?.valid !== validationResult.valid ||
776
- (!rollupStatus.validationResult.valid &&
777
- !validationResult.valid &&
778
- rollupStatus.validationResult.checkpoint.checkpointNumber === validationResult.checkpoint.checkpointNumber)
779
- ) {
878
+ // Do not update the validation result if there is a rejected ancestor, since in that case we want to keep the
879
+ // original invalidation, as the new checkpoint is extending from a previous invalid one.
880
+ const validStatusChanged = rollupStatus.validationResult?.valid !== validationResult.valid;
881
+ const invalidStatusWithSameCheckpointNumber =
882
+ !validationResult.valid &&
883
+ rollupStatus.validationResult &&
884
+ !rollupStatus.validationResult.valid &&
885
+ rollupStatus.validationResult.checkpoint.checkpointNumber === validationResult.checkpoint.checkpointNumber;
886
+
887
+ if (!rejectedAncestor && (validStatusChanged || invalidStatusWithSameCheckpointNumber)) {
780
888
  rollupStatus.validationResult = validationResult;
781
889
  }
782
890
 
783
891
  if (!validationResult.valid) {
784
- this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
785
- checkpointHash: published.checkpoint.hash(),
786
- l1BlockNumber: published.l1.blockNumber,
892
+ this.log.warn(`Skipping checkpoint ${calldataCheckpoint.checkpointNumber} due to invalid attestations`, {
893
+ checkpointNumber: calldataCheckpoint.checkpointNumber,
894
+ l1BlockNumber: calldataCheckpoint.l1.blockNumber,
787
895
  ...pick(validationResult, 'reason'),
788
896
  });
789
897
 
@@ -793,16 +901,104 @@ export class ArchiverL1Synchronizer implements Traceable {
793
901
  validationResult,
794
902
  });
795
903
 
796
- // We keep consuming checkpoints if we find an invalid one, since we do not listen for CheckpointInvalidated events
797
- // We just pretend the invalid ones are not there and keep consuming the next checkpoints
798
- // Note that this breaks if the committee ever attests to a descendant of an invalid checkpoint
904
+ // Persist a rejected-ancestor entry so any later checkpoint that builds on this one
905
+ // is detected and skipped (rather than tripping the addCheckpoints consecutive-number
906
+ // check and causing the sync point to roll back in a loop).
907
+ await this.stores.blocks.addRejectedCheckpoint({
908
+ checkpointNumber: calldataCheckpoint.checkpointNumber,
909
+ archiveRoot: calldataCheckpoint.archiveRoot,
910
+ parentArchiveRoot: calldataCheckpoint.header.lastArchiveRoot,
911
+ slotNumber: calldataCheckpoint.header.slotNumber,
912
+ l1: calldataCheckpoint.l1,
913
+ reason: 'invalid-attestations' as const,
914
+ });
915
+
916
+ continue;
917
+ }
918
+
919
+ if (rejectedAncestor) {
920
+ const descendantInfo: CheckpointInfo = {
921
+ archive: calldataCheckpoint.archiveRoot,
922
+ lastArchive: calldataCheckpoint.header.lastArchiveRoot,
923
+ slotNumber: calldataCheckpoint.header.slotNumber,
924
+ checkpointNumber: calldataCheckpoint.checkpointNumber,
925
+ timestamp: calldataCheckpoint.header.timestamp,
926
+ };
927
+ this.log.warn(
928
+ `Skipping checkpoint ${calldataCheckpoint.checkpointNumber} as it is a descendant of ` +
929
+ `rejected checkpoint ${rejectedAncestor.checkpointNumber} (${rejectedAncestor.reason})`,
930
+ {
931
+ checkpointNumber: calldataCheckpoint.checkpointNumber,
932
+ l1BlockNumber: calldataCheckpoint.l1.blockNumber,
933
+ l1BlockHash: calldataCheckpoint.l1.blockHash,
934
+ ancestorCheckpointNumber: rejectedAncestor.checkpointNumber,
935
+ ancestorArchiveRoot: rejectedAncestor.archiveRoot.toString(),
936
+ ancestorReason: rejectedAncestor.reason,
937
+ },
938
+ );
939
+
940
+ this.events.emit(L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected, {
941
+ type: L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
942
+ checkpoint: descendantInfo,
943
+ ancestorArchiveRoot: rejectedAncestor.archiveRoot,
944
+ ancestorCheckpointNumber: rejectedAncestor.checkpointNumber,
945
+ });
946
+
947
+ // Persist this chainpoint as rejected as well, so we can construct a chain of
948
+ // skipped checkpoints starting from the first one with invalid attestations.
949
+ await this.stores.blocks.addRejectedCheckpoint({
950
+ checkpointNumber: calldataCheckpoint.checkpointNumber,
951
+ archiveRoot: calldataCheckpoint.archiveRoot,
952
+ parentArchiveRoot: calldataCheckpoint.header.lastArchiveRoot,
953
+ slotNumber: calldataCheckpoint.header.slotNumber,
954
+ l1: calldataCheckpoint.l1,
955
+ reason: 'descends-from-invalid-attestations' as const,
956
+ });
957
+
799
958
  continue;
800
959
  }
801
960
 
961
+ checkpointsToIngest.push(calldataCheckpoint);
962
+ }
963
+
964
+ // Fetch blobs in parallel only for the surviving (attestation-valid, non-descendant) checkpoints,
965
+ // then build the full published checkpoints. The last calldata checkpoint may be promotable from a
966
+ // local proposed block (checkpointToPromote), in which case it carries no blob to fetch. A missing or
967
+ // undecodable blob throws and propagates, rolling back the L1 sync point so the fetch is retried.
968
+ const toFetchBlobs = checkpointToPromote
969
+ ? checkpointsToIngest.filter(c => c.checkpointNumber !== checkpointToPromote.checkpoint.number)
970
+ : checkpointsToIngest;
971
+ const blobFetched = await asyncPool(10, toFetchBlobs, async checkpoint =>
972
+ retrievedToPublishedCheckpoint({
973
+ ...checkpoint,
974
+ checkpointBlobData: await getCheckpointBlobDataFromBlobs(
975
+ this.blobClient,
976
+ checkpoint.l1.blockHash,
977
+ checkpoint.blobHashes,
978
+ checkpoint.checkpointNumber,
979
+ this.log,
980
+ !initialSyncComplete,
981
+ checkpoint.parentBeaconBlockRoot,
982
+ checkpoint.l1.timestamp,
983
+ ),
984
+ }),
985
+ );
986
+
987
+ // Index the built checkpoints by number so we can ingest them in calldata order, slotting in the
988
+ // promoted checkpoint (built from a local proposed block rather than blobs).
989
+ const publishedByNumber = new Map(blobFetched.map(published => [published.checkpoint.number, published]));
990
+ if (checkpointToPromote) {
991
+ publishedByNumber.set(checkpointToPromote.checkpoint.number, checkpointToPromote);
992
+ }
993
+
994
+ const validCheckpoints: PublishedCheckpoint[] = [];
995
+ for (const calldataCheckpoint of checkpointsToIngest) {
996
+ const published = publishedByNumber.get(calldataCheckpoint.checkpointNumber)!;
997
+
802
998
  // Check the inHash of the checkpoint against the l1->l2 messages.
803
999
  // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
804
1000
  // checkpoints we just retrieved.
805
- const l1ToL2Messages = await this.store.getL1ToL2Messages(published.checkpoint.number);
1001
+ const l1ToL2Messages = await this.stores.messages.getL1ToL2Messages(published.checkpoint.number);
806
1002
  const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
807
1003
  const publishedInHash = published.checkpoint.header.inHash;
808
1004
  if (!computedInHash.equals(publishedInHash)) {
@@ -841,15 +1037,35 @@ export class ArchiverL1Synchronizer implements Traceable {
841
1037
  try {
842
1038
  const updatedValidationResult =
843
1039
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
1040
+
1041
+ // Split valid checkpoints: the promoted one (if any) is persisted via the proposed-promotion path,
1042
+ // the rest via addCheckpoints. Both paths run within the same store transaction for atomicity.
1043
+ const [[maybeValidCheckpointToPromote], checkpointsToAdd] = partition(
1044
+ validCheckpoints,
1045
+ c => c.checkpoint.number === checkpointToPromote?.checkpoint.number,
1046
+ );
1047
+
844
1048
  const [processDuration, result] = await elapsed(() =>
845
1049
  execInSpan(this.tracer, 'Archiver.addCheckpoints', () =>
846
- this.updater.addCheckpoints(validCheckpoints, updatedValidationResult),
1050
+ this.updater.addCheckpoints(
1051
+ checkpointsToAdd,
1052
+ updatedValidationResult,
1053
+ maybeValidCheckpointToPromote && {
1054
+ l1: lastCalldataCheckpoint.l1,
1055
+ attestations: lastCalldataCheckpoint.attestations,
1056
+ checkpoint: maybeValidCheckpointToPromote,
1057
+ },
1058
+ evictProposedFrom,
1059
+ ),
847
1060
  ),
848
1061
  );
849
- this.instrumentation.processNewBlocks(
850
- processDuration / validCheckpoints.length,
851
- validCheckpoints.flatMap(c => c.checkpoint.blocks),
852
- );
1062
+
1063
+ if (validCheckpoints.length > 0) {
1064
+ this.instrumentation.processNewCheckpointedBlocks(
1065
+ processDuration / validCheckpoints.length,
1066
+ validCheckpoints.flatMap(c => c.checkpoint.blocks),
1067
+ );
1068
+ }
853
1069
 
854
1070
  // If blocks were pruned due to conflict with L1 checkpoints, emit event
855
1071
  if (result.prunedBlocks && result.prunedBlocks.length > 0) {
@@ -874,14 +1090,23 @@ export class ArchiverL1Synchronizer implements Traceable {
874
1090
  if (err instanceof InitialCheckpointNumberNotSequentialError) {
875
1091
  const { previousCheckpointNumber, newCheckpointNumber } = err;
876
1092
  const previousCheckpoint = previousCheckpointNumber
877
- ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber))
1093
+ ? await this.stores.blocks.getCheckpointData(CheckpointNumber(previousCheckpointNumber))
878
1094
  : undefined;
879
- const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1Constants.l1StartBlock;
880
- await this.store.setCheckpointSynchedL1BlockNumber(updatedL1SyncPoint);
1095
+ const lastFinalizedCheckpoint = await this.stores.blocks.getCheckpointData(
1096
+ await this.stores.blocks.getFinalizedCheckpointNumber(),
1097
+ );
1098
+ const updatedL1SyncPoint =
1099
+ previousCheckpoint?.l1.blockNumber ??
1100
+ lastFinalizedCheckpoint?.l1.blockNumber ??
1101
+ this.l1Constants.l1StartBlock;
1102
+ await this.stores.blocks.setSynchedL1BlockNumber(updatedL1SyncPoint);
881
1103
  this.log.warn(
882
1104
  `Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`,
883
1105
  {
884
1106
  previousCheckpointNumber,
1107
+ previousCheckpoint: previousCheckpoint?.header.toInspect(),
1108
+ lastFinalizedCheckpoint: lastFinalizedCheckpoint?.header.toInspect(),
1109
+ l1StartBlock: this.l1Constants.l1StartBlock,
885
1110
  newCheckpointNumber,
886
1111
  updatedL1SyncPoint,
887
1112
  },
@@ -902,13 +1127,107 @@ export class ArchiverL1Synchronizer implements Traceable {
902
1127
  });
903
1128
  }
904
1129
  lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
905
- lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
1130
+ // The last checkpoint seen on L1 this batch (valid or rejected), tracked from calldata since
1131
+ // rejected checkpoints are no longer built into PublishedCheckpoints.
1132
+ lastSeenCheckpoint = lastCalldataCheckpoint;
906
1133
  } while (searchEndBlock < currentL1BlockNumber);
907
1134
 
908
1135
  // Important that we update AFTER inserting the blocks.
909
1136
  await updateProvenCheckpoint();
910
1137
 
911
- return { ...rollupStatus, lastRetrievedCheckpoint, lastL1BlockWithCheckpoint };
1138
+ return { ...rollupStatus, lastRetrievedCheckpoint, lastSeenCheckpoint };
1139
+ }
1140
+
1141
+ /**
1142
+ * Checks if a specific checkpoint matches a local pending entry, and if so, loads local data to build
1143
+ * a synthetic published checkpoint (skipping blob fetch).
1144
+ *
1145
+ * Returns { diverged: true, fromCheckpointNumber } when the L1 checkpoint does NOT match local pending
1146
+ * data for that number, so the caller can evict the entire pending suffix >= fromCheckpointNumber
1147
+ * (those entries chain off the now-invalid local state) within the same addCheckpoints transaction.
1148
+ */
1149
+ private async tryBuildPublishedCheckpointFromProposed(
1150
+ calldataCheckpoint: RetrievedCheckpointFromCalldata | undefined,
1151
+ ): Promise<PublishedCheckpoint | { diverged: true; fromCheckpointNumber: CheckpointNumber } | undefined> {
1152
+ if (this.config.skipPromoteProposedCheckpointDuringL1Sync || !calldataCheckpoint) {
1153
+ return undefined;
1154
+ }
1155
+
1156
+ // Look up the specific pending entry for the checkpoint being mined, not just the tip
1157
+ const proposed = await this.stores.blocks.getProposedCheckpointByNumber(calldataCheckpoint.checkpointNumber);
1158
+ if (!proposed) {
1159
+ return undefined;
1160
+ }
1161
+
1162
+ if (
1163
+ !proposed.header.equals(calldataCheckpoint.header) ||
1164
+ !proposed.archive.root.equals(calldataCheckpoint.archiveRoot)
1165
+ ) {
1166
+ this.log.warn(
1167
+ `Local proposed checkpoint ${proposed.checkpointNumber} does not match checkpoint retrieved from L1, overriding with L1 data`,
1168
+ {
1169
+ proposedCheckpointNumber: proposed.checkpointNumber,
1170
+ proposedHeader: proposed.header.toInspect(),
1171
+ proposedArchiveRoot: proposed.archive.root.toString(),
1172
+ calldataCheckpointNumber: calldataCheckpoint.checkpointNumber,
1173
+ calldataHeader: calldataCheckpoint.header.toInspect(),
1174
+ calldataArchiveRoot: calldataCheckpoint.archiveRoot.toString(),
1175
+ },
1176
+ );
1177
+ // Both the locally-proposed checkpoint and the L1-confirmed one are signed by the
1178
+ // slot proposer; emit a divergence event so the slasher can attribute equivocation.
1179
+ // Only emit when the slots match — uncheckpointed entries are pruned above so this
1180
+ // should always hold, but guard defensively to avoid mis-attributing a slash.
1181
+ if (proposed.header.slotNumber === calldataCheckpoint.header.slotNumber) {
1182
+ this.events.emit(L2BlockSourceEvents.CheckpointEquivocationDetected, {
1183
+ type: L2BlockSourceEvents.CheckpointEquivocationDetected,
1184
+ slotNumber: calldataCheckpoint.header.slotNumber,
1185
+ checkpointNumber: calldataCheckpoint.checkpointNumber,
1186
+ l1ArchiveRoot: calldataCheckpoint.archiveRoot,
1187
+ proposedArchiveRoot: proposed.archive.root,
1188
+ });
1189
+ }
1190
+ // Return a divergence signal so the caller can evict pending >= this number
1191
+ return { diverged: true, fromCheckpointNumber: proposed.checkpointNumber };
1192
+ }
1193
+
1194
+ this.log.debug(
1195
+ `Building published checkpoint from proposed ${calldataCheckpoint.checkpointNumber} (skipping blob fetch)`,
1196
+ { proposedHeader: proposed.header.toInspect(), proposedArchiveRoot: proposed.archive.root.toString() },
1197
+ );
1198
+
1199
+ const blocks = await this.stores.blocks.getBlocks({
1200
+ from: BlockNumber(proposed.startBlock),
1201
+ limit: proposed.blockCount,
1202
+ });
1203
+ if (blocks.length !== proposed.blockCount) {
1204
+ this.log.warn(
1205
+ `Local proposed checkpoint ${proposed.checkpointNumber} has wrong block count (expected ${proposed.blockCount} blocks starting at ${proposed.startBlock} but got ${blocks.length})`,
1206
+ {
1207
+ proposedCheckpointNumber: proposed.checkpointNumber,
1208
+ proposedStartBlock: proposed.startBlock,
1209
+ proposedBlockCount: proposed.blockCount,
1210
+ retrievedBlocks: blocks.map(b => b.number),
1211
+ },
1212
+ );
1213
+ return undefined;
1214
+ }
1215
+
1216
+ const checkpoint = Checkpoint.from({
1217
+ archive: proposed.archive,
1218
+ header: proposed.header,
1219
+ blocks,
1220
+ number: proposed.checkpointNumber,
1221
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier,
1222
+ });
1223
+ const promotedCheckpoint = PublishedCheckpoint.from({
1224
+ checkpoint,
1225
+ l1: calldataCheckpoint.l1,
1226
+ attestations: calldataCheckpoint.attestations,
1227
+ });
1228
+ this.instrumentation.processCheckpointPromoted();
1229
+
1230
+ return promotedCheckpoint;
912
1231
  }
913
1232
 
914
1233
  private async checkForNewCheckpointsBeforeL1SyncPoint(
@@ -916,39 +1235,48 @@ export class ArchiverL1Synchronizer implements Traceable {
916
1235
  blocksSynchedTo: bigint,
917
1236
  currentL1BlockNumber: bigint,
918
1237
  ): Promise<void> {
919
- const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
920
- // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
921
- // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
1238
+ const { lastSeenCheckpoint, pendingCheckpointNumber } = status;
1239
+ // Compare the last checkpoint (valid or not) we have (either retrieved in this round or loaded from store)
1240
+ // with what the rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
922
1241
  const latestLocalCheckpointNumber =
923
- lastRetrievedCheckpoint?.checkpoint.number ?? (await this.store.getSynchedCheckpointNumber());
1242
+ lastSeenCheckpoint?.checkpointNumber ??
1243
+ CheckpointNumber.max(
1244
+ await this.stores.blocks.getLatestCheckpointNumber(),
1245
+ await this.stores.blocks.getLatestRejectedCheckpointNumber(),
1246
+ ) ??
1247
+ CheckpointNumber.ZERO;
1248
+
924
1249
  if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
925
1250
  // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
926
1251
  // but still haven't reached the pending checkpoint according to the call to the rollup contract.
927
1252
  // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
928
1253
  // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
929
1254
  // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
930
- let latestLocalCheckpointArchive: string | undefined = undefined;
931
- let targetL1BlockNumber = maxBigint(currentL1BlockNumber - 64n, 0n);
932
- if (lastRetrievedCheckpoint) {
933
- latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
934
- targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
935
- } else if (latestLocalCheckpointNumber > 0) {
936
- const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c]) => c);
937
- latestLocalCheckpointArchive = checkpoint.archive.root.toString();
938
- targetL1BlockNumber = checkpoint.l1.blockNumber;
939
- }
1255
+ const latestLocalCheckpoint:
1256
+ | { checkpointNumber: CheckpointNumber; l1: L1PublishedData }
1257
+ | CheckpointData
1258
+ | RejectedCheckpoint
1259
+ | undefined =
1260
+ lastSeenCheckpoint ??
1261
+ (await this.stores.blocks.getCheckpointData(latestLocalCheckpointNumber)) ??
1262
+ (await this.stores.blocks.getRejectedCheckpointByNumber(latestLocalCheckpointNumber));
1263
+
1264
+ const targetL1BlockNumber =
1265
+ latestLocalCheckpoint?.l1.blockNumber ??
1266
+ maxBigint(currentL1BlockNumber - 64n, this.l1Constants.l1StartBlock, 0n);
1267
+
940
1268
  this.log.warn(
941
1269
  `Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` +
942
1270
  `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`,
943
1271
  {
944
1272
  latestLocalCheckpointNumber,
945
- latestLocalCheckpointArchive,
1273
+ latestLocalCheckpointL1: latestLocalCheckpoint?.l1,
946
1274
  blocksSynchedTo,
947
1275
  currentL1BlockNumber,
948
1276
  ...status,
949
1277
  },
950
1278
  );
951
- await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
1279
+ await this.stores.blocks.setSynchedL1BlockNumber(targetL1BlockNumber);
952
1280
  } else {
953
1281
  this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
954
1282
  latestLocalCheckpointNumber,
@@ -958,7 +1286,7 @@ export class ArchiverL1Synchronizer implements Traceable {
958
1286
  }
959
1287
 
960
1288
  private async getCheckpointHeader(number: CheckpointNumber) {
961
- const checkpoint = await this.store.getCheckpointData(number);
1289
+ const checkpoint = await this.stores.blocks.getCheckpointData(number);
962
1290
  if (!checkpoint) {
963
1291
  return undefined;
964
1292
  }