@aztec/archiver 0.0.1-commit.29c6b1a3 → 0.0.1-commit.2c0ee1788

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 (134) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +26 -15
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +169 -148
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +16 -4
  8. package/dest/errors.d.ts +61 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +88 -14
  11. package/dest/factory.d.ts +6 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +41 -34
  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/bin/retrieve-calldata.js +36 -33
  18. package/dest/l1/calldata_retriever.d.ts +74 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +197 -260
  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 +43 -48
  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/l1/validate_trace.d.ts +6 -3
  31. package/dest/l1/validate_trace.d.ts.map +1 -1
  32. package/dest/l1/validate_trace.js +13 -9
  33. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  34. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  35. package/dest/modules/contract_data_source_adapter.js +42 -0
  36. package/dest/modules/data_source_base.d.ts +30 -17
  37. package/dest/modules/data_source_base.d.ts.map +1 -1
  38. package/dest/modules/data_source_base.js +98 -125
  39. package/dest/modules/data_store_updater.d.ts +37 -17
  40. package/dest/modules/data_store_updater.d.ts.map +1 -1
  41. package/dest/modules/data_store_updater.js +155 -112
  42. package/dest/modules/instrumentation.d.ts +21 -3
  43. package/dest/modules/instrumentation.d.ts.map +1 -1
  44. package/dest/modules/instrumentation.js +58 -18
  45. package/dest/modules/l1_synchronizer.d.ts +13 -11
  46. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  47. package/dest/modules/l1_synchronizer.js +334 -181
  48. package/dest/modules/validation.d.ts +4 -3
  49. package/dest/modules/validation.d.ts.map +1 -1
  50. package/dest/modules/validation.js +6 -6
  51. package/dest/store/block_store.d.ts +108 -32
  52. package/dest/store/block_store.d.ts.map +1 -1
  53. package/dest/store/block_store.js +477 -141
  54. package/dest/store/contract_class_store.d.ts +17 -4
  55. package/dest/store/contract_class_store.d.ts.map +1 -1
  56. package/dest/store/contract_class_store.js +33 -73
  57. package/dest/store/contract_instance_store.d.ts +28 -1
  58. package/dest/store/contract_instance_store.d.ts.map +1 -1
  59. package/dest/store/contract_instance_store.js +37 -2
  60. package/dest/store/data_stores.d.ts +68 -0
  61. package/dest/store/data_stores.d.ts.map +1 -0
  62. package/dest/store/data_stores.js +50 -0
  63. package/dest/store/function_names_cache.d.ts +17 -0
  64. package/dest/store/function_names_cache.d.ts.map +1 -0
  65. package/dest/store/function_names_cache.js +30 -0
  66. package/dest/store/l2_tips_cache.d.ts +20 -0
  67. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  68. package/dest/store/l2_tips_cache.js +109 -0
  69. package/dest/store/log_store.d.ts +6 -3
  70. package/dest/store/log_store.d.ts.map +1 -1
  71. package/dest/store/log_store.js +150 -55
  72. package/dest/store/message_store.d.ts +5 -1
  73. package/dest/store/message_store.d.ts.map +1 -1
  74. package/dest/store/message_store.js +21 -9
  75. package/dest/test/fake_l1_state.d.ts +25 -1
  76. package/dest/test/fake_l1_state.d.ts.map +1 -1
  77. package/dest/test/fake_l1_state.js +166 -32
  78. package/dest/test/index.d.ts +1 -2
  79. package/dest/test/index.d.ts.map +1 -1
  80. package/dest/test/index.js +3 -2
  81. package/dest/test/mock_archiver.d.ts +1 -1
  82. package/dest/test/mock_archiver.d.ts.map +1 -1
  83. package/dest/test/mock_archiver.js +3 -2
  84. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  85. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  86. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  87. package/dest/test/mock_l2_block_source.d.ts +38 -8
  88. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  89. package/dest/test/mock_l2_block_source.js +183 -90
  90. package/dest/test/mock_structs.d.ts +4 -1
  91. package/dest/test/mock_structs.d.ts.map +1 -1
  92. package/dest/test/mock_structs.js +13 -1
  93. package/dest/test/noop_l1_archiver.d.ts +7 -4
  94. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  95. package/dest/test/noop_l1_archiver.js +14 -8
  96. package/package.json +14 -13
  97. package/src/archiver.ts +215 -170
  98. package/src/config.ts +23 -2
  99. package/src/errors.ts +133 -22
  100. package/src/factory.ts +54 -32
  101. package/src/index.ts +18 -2
  102. package/src/l1/README.md +25 -68
  103. package/src/l1/bin/retrieve-calldata.ts +46 -39
  104. package/src/l1/calldata_retriever.ts +261 -379
  105. package/src/l1/data_retrieval.ts +59 -70
  106. package/src/l1/spire_proposer.ts +7 -15
  107. package/src/l1/validate_historical_logs.ts +140 -0
  108. package/src/l1/validate_trace.ts +24 -6
  109. package/src/modules/contract_data_source_adapter.ts +59 -0
  110. package/src/modules/data_source_base.ts +145 -147
  111. package/src/modules/data_store_updater.ts +187 -141
  112. package/src/modules/instrumentation.ts +71 -19
  113. package/src/modules/l1_synchronizer.ts +419 -219
  114. package/src/modules/validation.ts +10 -9
  115. package/src/store/block_store.ts +589 -179
  116. package/src/store/contract_class_store.ts +39 -107
  117. package/src/store/contract_instance_store.ts +51 -5
  118. package/src/store/data_stores.ts +108 -0
  119. package/src/store/function_names_cache.ts +37 -0
  120. package/src/store/l2_tips_cache.ts +134 -0
  121. package/src/store/log_store.ts +222 -64
  122. package/src/store/message_store.ts +27 -10
  123. package/src/structs/inbox_message.ts +1 -1
  124. package/src/test/fake_l1_state.ts +213 -42
  125. package/src/test/index.ts +3 -1
  126. package/src/test/mock_archiver.ts +3 -2
  127. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  128. package/src/test/mock_l2_block_source.ts +235 -87
  129. package/src/test/mock_structs.ts +20 -6
  130. package/src/test/noop_l1_archiver.ts +16 -8
  131. package/dest/store/kv_archiver_store.d.ts +0 -340
  132. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  133. package/dest/store/kv_archiver_store.js +0 -447
  134. package/src/store/kv_archiver_store.ts +0 -639
@@ -1,33 +1,40 @@
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';
4
- import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
3
+ import { InboxContract, type InboxContractState, RollupContract } from '@aztec/ethereum/contracts';
5
4
  import type { L1BlockId } from '@aztec/ethereum/l1-types';
5
+ import { getFinalizedL1Block } from '@aztec/ethereum/queries';
6
6
  import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
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 { partition, pick } from '@aztec/foundation/collection';
11
12
  import { Fr } from '@aztec/foundation/curves/bn254';
12
13
  import { EthAddress } from '@aztec/foundation/eth-address';
13
14
  import { type Logger, createLogger } from '@aztec/foundation/log';
15
+ import { retryTimes } from '@aztec/foundation/retry';
14
16
  import { count } from '@aztec/foundation/string';
15
17
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
16
- import { isDefined } from '@aztec/foundation/types';
18
+ import { isDefined, isErrorClass } from '@aztec/foundation/types';
17
19
  import { type ArchiverEmitter, L2BlockSourceEvents, type ValidateCheckpointResult } from '@aztec/stdlib/block';
18
- import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
19
- import { type L1RollupConstants, getEpochAtSlot, getSlotAtTimestamp } from '@aztec/stdlib/epoch-helpers';
20
+ import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
21
+ import { type L1RollupConstants, getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
20
22
  import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
23
+ import type { CoordinationSignatureContext } from '@aztec/stdlib/p2p';
21
24
  import { type Traceable, type Tracer, execInSpan, trackSpan } from '@aztec/telemetry-client';
22
25
 
23
26
  import { InitialCheckpointNumberNotSequentialError } from '../errors.js';
24
27
  import {
25
- retrieveCheckpointsFromRollup,
28
+ type RetrievedCheckpointFromCalldata,
29
+ getCheckpointBlobDataFromBlobs,
30
+ retrieveCheckpointCalldataFromRollup,
26
31
  retrieveL1ToL2Message,
27
32
  retrieveL1ToL2Messages,
28
33
  retrievedToPublishedCheckpoint,
29
34
  } from '../l1/data_retrieval.js';
30
- import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
35
+ import { type ArchiverDataStores, getArchiverSynchPoint } from '../store/data_stores.js';
36
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
37
+ import { MessageStoreError } from '../store/message_store.js';
31
38
  import type { InboxMessage } from '../structs/inbox_message.js';
32
39
  import { ArchiverDataStoreUpdater } from './data_store_updater.js';
33
40
  import type { ArchiverInstrumentation } from './instrumentation.js';
@@ -60,26 +67,29 @@ export class ArchiverL1Synchronizer implements Traceable {
60
67
  private readonly debugClient: ViemPublicDebugClient,
61
68
  private readonly rollup: RollupContract,
62
69
  private readonly inbox: InboxContract,
63
- private readonly l1Addresses: Pick<
64
- L1ContractAddresses,
65
- 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
66
- > & { slashingProposerAddress: EthAddress },
67
- private readonly store: KVArchiverDataStore,
70
+ private readonly stores: ArchiverDataStores,
68
71
  private config: {
69
72
  batchSize: number;
70
73
  skipValidateCheckpointAttestations?: boolean;
74
+ skipPromoteProposedCheckpointDuringL1Sync?: boolean;
71
75
  maxAllowedEthClientDriftSeconds: number;
72
76
  },
73
77
  private readonly blobClient: BlobClientInterface,
74
78
  private readonly epochCache: EpochCache,
75
79
  private readonly dateProvider: DateProvider,
76
80
  private readonly instrumentation: ArchiverInstrumentation,
77
- private readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
81
+ private readonly l1Constants: L1RollupConstants & {
82
+ l1StartBlockHash: Buffer32;
83
+ genesisArchiveRoot: Fr;
84
+ },
78
85
  private readonly events: ArchiverEmitter,
79
86
  tracer: Tracer,
87
+ l2TipsCache?: L2TipsCache,
80
88
  private readonly log: Logger = createLogger('archiver:l1-sync'),
81
89
  ) {
82
- this.updater = new ArchiverDataStoreUpdater(this.store);
90
+ this.updater = new ArchiverDataStoreUpdater(this.stores, l2TipsCache, {
91
+ rollupManaLimit: l1Constants.rollupManaLimit,
92
+ });
83
93
  this.tracer = tracer;
84
94
  }
85
95
 
@@ -87,6 +97,7 @@ export class ArchiverL1Synchronizer implements Traceable {
87
97
  public setConfig(newConfig: {
88
98
  batchSize: number;
89
99
  skipValidateCheckpointAttestations?: boolean;
100
+ skipPromoteProposedCheckpointDuringL1Sync?: boolean;
90
101
  maxAllowedEthClientDriftSeconds: number;
91
102
  }) {
92
103
  this.config = newConfig;
@@ -102,6 +113,13 @@ export class ArchiverL1Synchronizer implements Traceable {
102
113
  return this.l1Timestamp;
103
114
  }
104
115
 
116
+ private getSignatureContext(): CoordinationSignatureContext {
117
+ return {
118
+ chainId: this.publicClient.chain.id,
119
+ rollupAddress: EthAddress.fromString(this.rollup.address),
120
+ };
121
+ }
122
+
105
123
  /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */
106
124
  public async testEthereumNodeSynced(): Promise<void> {
107
125
  const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
@@ -119,10 +137,15 @@ export class ArchiverL1Synchronizer implements Traceable {
119
137
 
120
138
  @trackSpan('Archiver.syncFromL1')
121
139
  public async syncFromL1(initialSyncComplete: boolean): Promise<void> {
140
+ // In between the various calls to L1, the block number can move meaning some of the following
141
+ // calls will return data for blocks that were not present during earlier calls. To combat this
142
+ // we ensure that all data retrieval methods only retrieve data up to the currentBlockNumber
143
+ // captured at the top of this function.
122
144
  const currentL1Block = await this.publicClient.getBlock({ includeTransactions: false });
123
145
  const currentL1BlockNumber = currentL1Block.number;
124
146
  const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
125
147
  const currentL1Timestamp = currentL1Block.timestamp;
148
+ const currentL1BlockData = { l1BlockNumber: currentL1BlockNumber, l1BlockHash: currentL1BlockHash };
126
149
 
127
150
  if (this.l1BlockHash && currentL1BlockHash.equals(this.l1BlockHash)) {
128
151
  this.log.trace(`No new L1 blocks since last sync at L1 block ${this.l1BlockNumber}`);
@@ -139,45 +162,15 @@ export class ArchiverL1Synchronizer implements Traceable {
139
162
  );
140
163
  }
141
164
 
142
- // Load sync point for blocks and messages defaulting to start block
143
- const {
144
- blocksSynchedTo = this.l1Constants.l1StartBlock,
145
- messagesSynchedTo = {
146
- l1BlockNumber: this.l1Constants.l1StartBlock,
147
- l1BlockHash: this.l1Constants.l1StartBlockHash,
148
- },
149
- } = await this.store.getSynchPoint();
165
+ // Load sync point for blocks defaulting to start block
166
+ const { blocksSynchedTo = this.l1Constants.l1StartBlock } = await getArchiverSynchPoint(this.stores);
167
+ this.log.debug(`Starting new archiver sync iteration`, { blocksSynchedTo, currentL1BlockData });
150
168
 
151
- this.log.debug(`Starting new archiver sync iteration`, {
152
- blocksSynchedTo,
153
- messagesSynchedTo,
154
- currentL1BlockNumber,
155
- currentL1BlockHash,
156
- });
169
+ // 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.
170
+ // 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
171
+ // block, when that wouldn't be the case, since L1 to L2 messages would need another iteration.
172
+ await retryTimes(() => this.handleL1ToL2Messages(currentL1BlockData), 'Handling L1 to L2 messages', 3, 0.1);
157
173
 
158
- // ********** Ensuring Consistency of data pulled from L1 **********
159
-
160
- /**
161
- * There are a number of calls in this sync operation to L1 for retrieving
162
- * events and transaction data. There are a couple of things we need to bear in mind
163
- * to ensure that data is read exactly once.
164
- *
165
- * The first is the problem of eventually consistent ETH service providers like Infura.
166
- * Each L1 read operation will query data from the last L1 block that it saw emit its kind of data.
167
- * (so pending L1 to L2 messages will read from the last L1 block that emitted a message and so on)
168
- * This will mean the archiver will lag behind L1 and will only advance when there's L2-relevant activity on the chain.
169
- *
170
- * The second is that in between the various calls to L1, the block number can move meaning some
171
- * of the following calls will return data for blocks that were not present during earlier calls.
172
- * To combat this for the time being we simply ensure that all data retrieval methods only retrieve
173
- * data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
174
- * in future but for the time being it should give us the guarantees that we need
175
- */
176
-
177
- // ********** Events that are processed per L1 block **********
178
- await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
179
-
180
- // ********** Events that are processed per checkpoint **********
181
174
  if (currentL1BlockNumber > blocksSynchedTo) {
182
175
  // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
183
176
  // pending chain validation status, proven checkpoint number, and synched L1 block number.
@@ -201,7 +194,7 @@ export class ArchiverL1Synchronizer implements Traceable {
201
194
  // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
202
195
  // we get a valid checkpoint to advance the syncpoint.
203
196
  if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
204
- await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
197
+ await this.stores.blocks.setSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
205
198
  }
206
199
 
207
200
  // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
@@ -215,6 +208,9 @@ export class ArchiverL1Synchronizer implements Traceable {
215
208
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
216
209
  }
217
210
 
211
+ // Update the finalized L2 checkpoint based on L1 finality.
212
+ await this.updateFinalizedCheckpoint();
213
+
218
214
  // After syncing has completed, update the current l1 block number and timestamp,
219
215
  // otherwise we risk announcing to the world that we've synced to a given point,
220
216
  // but the corresponding blocks have not been processed (see #12631).
@@ -230,11 +226,39 @@ export class ArchiverL1Synchronizer implements Traceable {
230
226
  });
231
227
  }
232
228
 
229
+ /** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
230
+ private async updateFinalizedCheckpoint(): Promise<void> {
231
+ try {
232
+ const finalizedL1Block = await getFinalizedL1Block(this.publicClient);
233
+ if (!finalizedL1Block) {
234
+ this.log.trace(`Skipping finalized checkpoint update: L1 has no finalized block yet.`);
235
+ return;
236
+ }
237
+ const finalizedL1BlockNumber = finalizedL1Block.number;
238
+ const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
239
+ blockNumber: finalizedL1BlockNumber,
240
+ });
241
+ const localFinalizedCheckpointNumber = await this.stores.blocks.getFinalizedCheckpointNumber();
242
+ if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
243
+ await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
244
+ this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
245
+ finalizedCheckpointNumber,
246
+ finalizedL1BlockNumber,
247
+ });
248
+ }
249
+ } catch (err: any) {
250
+ // The rollup contract may not exist at the finalized L1 block right after deployment.
251
+ if (!err?.message?.includes('returned no data')) {
252
+ this.log.warn(`Failed to update finalized checkpoint: ${err}`);
253
+ }
254
+ }
255
+ }
256
+
233
257
  /** Prune all proposed local blocks that should have been checkpointed by now. */
234
258
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
235
259
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
236
- this.store.getCheckpointedL2BlockNumber(),
237
- this.store.getLatestBlockNumber(),
260
+ this.stores.blocks.getCheckpointedL2BlockNumber(),
261
+ this.stores.blocks.getLatestL2BlockNumber(),
238
262
  ]);
239
263
 
240
264
  // If there are no uncheckpointed blocks, we got nothing to do
@@ -243,30 +267,36 @@ export class ArchiverL1Synchronizer implements Traceable {
243
267
  return;
244
268
  }
245
269
 
246
- // What's the slot of the first uncheckpointed block?
270
+ // What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
271
+ const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
247
272
  const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
248
- const [firstUncheckpointedBlockHeader] = await this.store.getBlockHeaders(firstUncheckpointedBlockNumber, 1);
273
+
274
+ // What's the slot of the first uncheckpointed block?
275
+ const [firstUncheckpointedBlockHeader] = await this.stores.blocks.getBlockHeaders(
276
+ firstUncheckpointedBlockNumber,
277
+ 1,
278
+ );
249
279
  const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
250
280
 
251
- // What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
252
- const nextL1BlockTimestamp = currentL1Timestamp + BigInt(this.l1Constants.ethereumSlotDuration);
253
- const slotAtNextL1Block = getSlotAtTimestamp(nextL1BlockTimestamp, this.l1Constants);
281
+ if (firstUncheckpointedBlockSlot === undefined || firstUncheckpointedBlockSlot >= slotAtNextL1Block) {
282
+ return;
283
+ }
254
284
 
255
- // Prune provisional blocks from slots that have ended without being checkpointed
256
- if (firstUncheckpointedBlockSlot !== undefined && firstUncheckpointedBlockSlot < slotAtNextL1Block) {
257
- this.log.warn(
258
- `Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`,
259
- { firstUncheckpointedBlockHeader: firstUncheckpointedBlockHeader.toInspect(), slotAtNextL1Block },
260
- );
261
- const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
285
+ // Prune provisional blocks from slots that have ended without being checkpointed.
286
+ // This also clears any proposed checkpoint whose blocks are being pruned.
287
+ this.log.warn(
288
+ `Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`,
289
+ { firstUncheckpointedBlockHeader: firstUncheckpointedBlockHeader.toInspect(), slotAtNextL1Block },
290
+ );
262
291
 
263
- if (prunedBlocks.length > 0) {
264
- this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
265
- type: L2BlockSourceEvents.L2PruneUncheckpointed,
266
- slotNumber: firstUncheckpointedBlockSlot,
267
- blocks: prunedBlocks,
268
- });
269
- }
292
+ const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
293
+
294
+ if (prunedBlocks.length > 0) {
295
+ this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
296
+ type: L2BlockSourceEvents.L2PruneUncheckpointed,
297
+ slotNumber: firstUncheckpointedBlockSlot,
298
+ blocks: prunedBlocks,
299
+ });
270
300
  }
271
301
  }
272
302
 
@@ -292,7 +322,7 @@ export class ArchiverL1Synchronizer implements Traceable {
292
322
  currentL1Timestamp: bigint,
293
323
  ): Promise<{ rollupCanPrune: boolean }> {
294
324
  const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
295
- const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
325
+ const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
296
326
  const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
297
327
 
298
328
  if (canPrune) {
@@ -309,17 +339,20 @@ export class ArchiverL1Synchronizer implements Traceable {
309
339
 
310
340
  const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
311
341
 
312
- const checkpointPromises = Array.from({ length: checkpointsToUnwind })
313
- .fill(0)
314
- .map((_, i) => this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
315
- const checkpoints = await Promise.all(checkpointPromises);
316
-
317
- const blockPromises = await Promise.all(
318
- checkpoints
319
- .filter(isDefined)
320
- .map(cp => this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))),
321
- );
322
- const newBlocks = blockPromises.filter(isDefined).flat();
342
+ // Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
343
+ // promises when the gap between local pending and proven checkpoint numbers is large.
344
+ const BATCH_SIZE = 10;
345
+ const indices = Array.from({ length: checkpointsToUnwind }, (_, i) => CheckpointNumber(i + pruneFrom));
346
+ const checkpoints = (
347
+ await asyncPool(BATCH_SIZE, indices, idx => this.stores.blocks.getCheckpointData(idx))
348
+ ).filter(isDefined);
349
+ const newBlocks = (
350
+ await asyncPool(BATCH_SIZE, checkpoints, cp =>
351
+ this.stores.blocks.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)),
352
+ )
353
+ )
354
+ .filter(isDefined)
355
+ .flat();
323
356
 
324
357
  // Emit an event for listening services to react to the chain prune
325
358
  this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
@@ -335,12 +368,12 @@ export class ArchiverL1Synchronizer implements Traceable {
335
368
  this.log.warn(
336
369
  `Removed ${count(checkpointsToUnwind, 'checkpoint')} after checkpoint ${provenCheckpointNumber} ` +
337
370
  `due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
338
- `Updated latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`,
371
+ `Updated latest checkpoint is ${await this.stores.blocks.getLatestCheckpointNumber()}.`,
339
372
  );
340
373
  this.instrumentation.processPrune(timer.ms());
341
374
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
342
375
  // Seems like the next iteration should handle this.
343
- // await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
376
+ // await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
344
377
  }
345
378
 
346
379
  return { rollupCanPrune };
@@ -357,67 +390,91 @@ export class ArchiverL1Synchronizer implements Traceable {
357
390
  }
358
391
 
359
392
  @trackSpan('Archiver.handleL1ToL2Messages')
360
- private async handleL1ToL2Messages(messagesSyncPoint: L1BlockId, currentL1BlockNumber: bigint): Promise<void> {
361
- this.log.trace(`Handling L1 to L2 messages from ${messagesSyncPoint.l1BlockNumber} to ${currentL1BlockNumber}.`);
362
- if (currentL1BlockNumber <= messagesSyncPoint.l1BlockNumber) {
363
- return;
393
+ private async handleL1ToL2Messages(currentL1Block: L1BlockId): Promise<boolean> {
394
+ // Load the syncpoint, which may have been updated in a previous iteration
395
+ const {
396
+ messagesSynchedTo = {
397
+ l1BlockNumber: this.l1Constants.l1StartBlock,
398
+ l1BlockHash: this.l1Constants.l1StartBlockHash,
399
+ },
400
+ } = await getArchiverSynchPoint(this.stores);
401
+
402
+ // Nothing to do if L1 block number has not moved forward
403
+ const currentL1BlockNumber = currentL1Block.l1BlockNumber;
404
+ if (currentL1BlockNumber <= messagesSynchedTo.l1BlockNumber) {
405
+ return true;
364
406
  }
365
407
 
366
- // Load remote and local inbox states.
367
- const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
368
- const localLastMessage = await this.store.getLastL1ToL2Message();
408
+ // Compare local message store state with the remote. If they match, we just advance the match pointer.
369
409
  const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
410
+ const localLastMessage = await this.stores.messages.getLastMessage();
411
+ if (await this.localStateMatches(localLastMessage, remoteMessagesState)) {
412
+ this.log.trace(`Local L1 to L2 messages are already in sync with remote at L1 block ${currentL1BlockNumber}`);
413
+ await this.stores.messages.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress);
414
+ return true;
415
+ }
370
416
 
371
- this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
372
- localMessagesInserted,
373
- localLastMessage,
374
- remoteMessagesState,
375
- });
417
+ // If not, then we are out of sync. Most likely there are new messages on the inbox, so we try retrieving them.
418
+ // However, it could also be the case that there was an L1 reorg and our syncpoint is no longer valid.
419
+ // If that's the case, we'd get an exception out of the message store since the rolling hash of the first message
420
+ // we try to insert would not match the one in the db, in which case we rollback to the last common message with L1.
421
+ try {
422
+ await this.retrieveAndStoreMessages(messagesSynchedTo.l1BlockNumber, currentL1BlockNumber);
423
+ } catch (error) {
424
+ if (isErrorClass(error, MessageStoreError)) {
425
+ this.log.warn(
426
+ `Failed to store L1 to L2 messages retrieved from L1: ${error.message}. Rolling back syncpoint to retry.`,
427
+ { inboxMessage: error.inboxMessage },
428
+ );
429
+ await this.rollbackL1ToL2Messages(remoteMessagesState);
430
+ return false;
431
+ }
432
+ throw error;
433
+ }
376
434
 
377
- // Compare message count and rolling hash. If they match, no need to retrieve anything.
378
- if (
379
- remoteMessagesState.totalMessagesInserted === localMessagesInserted &&
380
- remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer32.ZERO)
381
- ) {
382
- this.log.trace(
383
- `No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`,
435
+ // Note that, if there are no new messages to insert, but there was an L1 reorg that pruned out last messages,
436
+ // we'd notice by comparing our local state with the remote one again, and seeing they don't match even after
437
+ // our sync attempt. In this case, we also rollback our syncpoint, and trigger a retry.
438
+ const localLastMessageAfterSync = await this.stores.messages.getLastMessage();
439
+ if (!(await this.localStateMatches(localLastMessageAfterSync, remoteMessagesState))) {
440
+ this.log.warn(
441
+ `Local L1 to L2 messages state does not match remote after sync attempt. Rolling back syncpoint to retry.`,
442
+ { localLastMessageAfterSync, remoteMessagesState },
384
443
  );
385
- return;
444
+ await this.rollbackL1ToL2Messages(remoteMessagesState);
445
+ return false;
386
446
  }
387
447
 
388
- // Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
389
- // Note that we need to fetch it from logs and not from inbox state at the syncpoint l1 block number, since it
390
- // could be older than 128 blocks and non-archive nodes cannot resolve it.
391
- if (localLastMessage) {
392
- const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
393
- this.log.trace(`Retrieved remote message for local last`, { remoteLastMessage, localLastMessage });
394
- if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
395
- this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
396
- remoteLastMessage,
397
- messagesSyncPoint,
398
- localLastMessage,
399
- });
448
+ // Advance the syncpoint after a successful sync
449
+ await this.stores.messages.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress);
450
+ return true;
451
+ }
400
452
 
401
- messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
402
- this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
403
- messagesSyncPoint,
404
- });
405
- }
406
- }
453
+ /** Checks if the local rolling hash and message count matches the remote state */
454
+ private async localStateMatches(localLastMessage: InboxMessage | undefined, remoteState: InboxContractState) {
455
+ const localMessageCount = await this.stores.messages.getTotalL1ToL2MessageCount();
456
+ this.log.trace(`Comparing local and remote inbox state`, { localMessageCount, localLastMessage, remoteState });
407
457
 
408
- // Retrieve and save messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
458
+ return (
459
+ remoteState.totalMessagesInserted === localMessageCount &&
460
+ remoteState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
461
+ );
462
+ }
463
+
464
+ /** Retrieves L1 to L2 messages from L1 in batches and stores them. */
465
+ private async retrieveAndStoreMessages(fromL1Block: bigint, toL1Block: bigint): Promise<void> {
409
466
  let searchStartBlock: bigint = 0n;
410
- let searchEndBlock: bigint = messagesSyncPoint.l1BlockNumber;
467
+ let searchEndBlock: bigint = fromL1Block;
411
468
 
412
469
  let lastMessage: InboxMessage | undefined;
413
470
  let messageCount = 0;
414
471
 
415
472
  do {
416
- [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
473
+ [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, toL1Block);
417
474
  this.log.trace(`Retrieving L1 to L2 messages in L1 blocks ${searchStartBlock}-${searchEndBlock}`);
418
475
  const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
419
476
  const timer = new Timer();
420
- await this.store.addL1ToL2Messages(messages);
477
+ await this.stores.messages.addL1ToL2Messages(messages);
421
478
  const perMsg = timer.ms() / messages.length;
422
479
  this.instrumentation.processNewMessages(messages.length, perMsg);
423
480
  for (const msg of messages) {
@@ -425,81 +482,86 @@ export class ArchiverL1Synchronizer implements Traceable {
425
482
  lastMessage = msg;
426
483
  messageCount++;
427
484
  }
428
- } while (searchEndBlock < currentL1BlockNumber);
485
+ } while (searchEndBlock < toL1Block);
429
486
 
430
- // Log stats for messages retrieved (if any).
431
487
  if (messageCount > 0) {
432
488
  this.log.info(
433
489
  `Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`,
434
490
  { lastMessage, messageCount },
435
491
  );
436
492
  }
437
-
438
- // Warn if the resulting rolling hash does not match the remote state we had retrieved.
439
- if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
440
- this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
441
- lastMessage,
442
- remoteMessagesState,
443
- });
444
- }
445
493
  }
446
494
 
447
- private async retrieveL1ToL2Message(leaf: Fr): Promise<InboxMessage | undefined> {
448
- const currentL1BlockNumber = await this.publicClient.getBlockNumber();
449
- let searchStartBlock: bigint = 0n;
450
- let searchEndBlock: bigint = this.l1Constants.l1StartBlock - 1n;
451
-
452
- do {
453
- [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
495
+ /**
496
+ * 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.
497
+ * If no common message is found, rolls back all messages and sets the syncpoint to the start block.
498
+ */
499
+ private async rollbackL1ToL2Messages(remoteMessagesState: InboxContractState): Promise<L1BlockId> {
500
+ const { treeInProgress: remoteTreeInProgress, messagesRollingHash: remoteRollingHash } = remoteMessagesState;
454
501
 
455
- const message = await retrieveL1ToL2Message(this.inbox, leaf, searchStartBlock, searchEndBlock);
456
-
457
- if (message) {
458
- return message;
502
+ // Slowly go back through our messages until we find the last common message. We could query the logs in
503
+ // batch as an optimization, but the depth of the reorg should not be deep, and this is a very rare case,
504
+ // so it's fine to query one log at a time.
505
+ let commonMsg: undefined | InboxMessage;
506
+ let messagesToDelete = 0;
507
+ this.log.verbose(`Searching most recent common L1 to L2 message`);
508
+ for await (const localMsg of this.stores.messages.iterateL1ToL2Messages({ reverse: true })) {
509
+ const logCtx = { remoteMsg: undefined as InboxMessage | undefined, localMsg, remoteMessagesState };
510
+
511
+ // First check if the local message rolling hash matches the current rolling hash of the inbox contract,
512
+ // which means we just need to rollback some local messages and we should be back in sync. This means there
513
+ // was an L1 reorg that removed some of the messages we had, but no new messages were added compared.
514
+ if (localMsg.rollingHash.equals(remoteRollingHash)) {
515
+ this.log.info(
516
+ `Found common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber} matching current remote state`,
517
+ logCtx,
518
+ );
519
+ commonMsg = localMsg;
520
+ break;
459
521
  }
460
- } while (searchEndBlock < currentL1BlockNumber);
461
522
 
462
- return undefined;
463
- }
464
-
465
- private async rollbackL1ToL2Messages(
466
- localLastMessage: InboxMessage,
467
- messagesSyncPoint: L1BlockId,
468
- ): Promise<L1BlockId> {
469
- // Slowly go back through our messages until we find the last common message.
470
- // We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
471
- // is a very rare case, so it's fine to query one log at a time.
472
- let commonMsg: undefined | InboxMessage;
473
- this.log.verbose(`Searching most recent common L1 to L2 message at or before index ${localLastMessage.index}`);
474
- for await (const msg of this.store.iterateL1ToL2Messages({ reverse: true, end: localLastMessage.index })) {
475
- const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
476
- const logCtx = { remoteMsg, localMsg: msg };
477
- if (remoteMsg && remoteMsg.rollingHash.equals(msg.rollingHash)) {
478
- this.log.verbose(
479
- `Found most recent common L1 to L2 message at index ${msg.index} on L1 block ${msg.l1BlockNumber}`,
523
+ // If there's no match with the current remote state, check if the message exists on the inbox contract at all
524
+ // by looking at the inbox events. If the L1 reorg *added* new messages in addition to deleting existing ones,
525
+ // then the current remote state's rolling hash will not match anything we have locally, so we need to check existence
526
+ // of individual messages via logs. Note we use logs and not historical queries so we don't have to depend on
527
+ // an archival rpc node, since the message could be from a long time ago if we're catching up with syncing.
528
+ const remoteMsg = await retrieveL1ToL2Message(this.inbox, localMsg);
529
+ logCtx.remoteMsg = remoteMsg;
530
+ if (remoteMsg && remoteMsg.rollingHash.equals(localMsg.rollingHash)) {
531
+ this.log.info(
532
+ `Found most recent common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber}`,
480
533
  logCtx,
481
534
  );
482
535
  commonMsg = remoteMsg;
483
536
  break;
484
537
  } else if (remoteMsg) {
485
- this.log.debug(`Local L1 to L2 message with index ${msg.index} has different rolling hash`, logCtx);
538
+ this.log.debug(`Local L1 to L2 message with index ${localMsg.index} has different rolling hash`, logCtx);
539
+ messagesToDelete++;
486
540
  } else {
487
- this.log.debug(`Local L1 to L2 message with index ${msg.index} not found on L1`, logCtx);
541
+ this.log.debug(`Local L1 to L2 message with index ${localMsg.index} not found on L1`, logCtx);
542
+ messagesToDelete++;
488
543
  }
489
544
  }
490
545
 
491
- // Delete everything after the common message we found.
492
- const lastGoodIndex = commonMsg?.index;
493
- this.log.warn(`Deleting all local L1 to L2 messages after index ${lastGoodIndex ?? 'undefined'}`);
494
- await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
546
+ // Delete everything after the common message we found, if anything needs to be deleted.
547
+ // Do not exit early if there are no messages to delete, we still want to update the syncpoint.
548
+ if (messagesToDelete > 0) {
549
+ const lastGoodIndex = commonMsg?.index;
550
+ this.log.warn(`Rolling back all local L1 to L2 messages after index ${lastGoodIndex ?? 'initial'}`);
551
+ await this.stores.messages.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
552
+ }
495
553
 
496
554
  // Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
497
555
  // the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
498
556
  // after the last common message.
499
557
  const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1Constants.l1StartBlock;
500
558
  const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
501
- messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
502
- await this.store.setMessageSynchedL1Block(messagesSyncPoint);
559
+ const messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
560
+ await this.stores.messages.setMessageSyncState(messagesSyncPoint, remoteTreeInProgress);
561
+ this.log.verbose(`Updated messages syncpoint to L1 block ${syncPointL1BlockNumber}`, {
562
+ ...messagesSyncPoint,
563
+ remoteTreeInProgress,
564
+ });
503
565
  return messagesSyncPoint;
504
566
  }
505
567
 
@@ -517,9 +579,9 @@ export class ArchiverL1Synchronizer implements Traceable {
517
579
  currentL1BlockNumber: bigint,
518
580
  initialSyncComplete: boolean,
519
581
  ): Promise<RollupStatus> {
520
- const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
582
+ const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
521
583
  const initialValidationResult: ValidateCheckpointResult | undefined =
522
- await this.store.getPendingChainValidationStatus();
584
+ await this.stores.blocks.getPendingChainValidationStatus();
523
585
  const {
524
586
  provenCheckpointNumber,
525
587
  provenArchive,
@@ -549,19 +611,19 @@ export class ArchiverL1Synchronizer implements Traceable {
549
611
  // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
550
612
  // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
551
613
  if (provenCheckpointNumber === 0) {
552
- const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
614
+ const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
553
615
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
554
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
616
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
555
617
  this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
556
618
  }
557
619
  }
558
620
 
559
621
  const localCheckpointForDestinationProvenCheckpointNumber =
560
- await this.store.getCheckpointData(provenCheckpointNumber);
622
+ await this.stores.blocks.getCheckpointData(provenCheckpointNumber);
561
623
 
562
624
  // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
563
625
  // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
564
- const synched = await this.store.getSynchedCheckpointNumber();
626
+ const synched = await this.stores.blocks.getLatestCheckpointNumber();
565
627
  if (
566
628
  localCheckpointForDestinationProvenCheckpointNumber &&
567
629
  synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber
@@ -581,15 +643,15 @@ export class ArchiverL1Synchronizer implements Traceable {
581
643
  localCheckpointForDestinationProvenCheckpointNumber &&
582
644
  provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)
583
645
  ) {
584
- const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
646
+ const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
585
647
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
586
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
648
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
587
649
  this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
588
650
  const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
589
651
  const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1Constants);
590
652
  const lastBlockNumberInCheckpoint =
591
653
  localCheckpointForDestinationProvenCheckpointNumber.startBlock +
592
- localCheckpointForDestinationProvenCheckpointNumber.numBlocks -
654
+ localCheckpointForDestinationProvenCheckpointNumber.blockCount -
593
655
  1;
594
656
 
595
657
  this.events.emit(L2BlockSourceEvents.L2BlockProven, {
@@ -598,7 +660,7 @@ export class ArchiverL1Synchronizer implements Traceable {
598
660
  slotNumber: provenSlotNumber,
599
661
  epochNumber: provenEpochNumber,
600
662
  });
601
- this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
663
+ this.instrumentation.updateLastProvenCheckpoint(localCheckpointForDestinationProvenCheckpointNumber);
602
664
  } else {
603
665
  this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
604
666
  }
@@ -609,7 +671,7 @@ export class ArchiverL1Synchronizer implements Traceable {
609
671
  // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
610
672
  const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
611
673
  if (noCheckpoints) {
612
- await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
674
+ await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
613
675
  this.log.debug(
614
676
  `No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`,
615
677
  );
@@ -621,7 +683,7 @@ export class ArchiverL1Synchronizer implements Traceable {
621
683
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
622
684
  // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
623
685
  if (localPendingCheckpointNumber > 0) {
624
- const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
686
+ const localPendingCheckpoint = await this.stores.blocks.getCheckpointData(localPendingCheckpointNumber);
625
687
  if (localPendingCheckpoint === undefined) {
626
688
  throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
627
689
  }
@@ -636,7 +698,7 @@ export class ArchiverL1Synchronizer implements Traceable {
636
698
  // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
637
699
  // We must only set this block number based on actually retrieved logs.
638
700
  // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
639
- // await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
701
+ // await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
640
702
  this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
641
703
  return rollupStatus;
642
704
  }
@@ -656,7 +718,7 @@ export class ArchiverL1Synchronizer implements Traceable {
656
718
 
657
719
  let tipAfterUnwind = localPendingCheckpointNumber;
658
720
  while (true) {
659
- const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
721
+ const candidateCheckpoint = await this.stores.blocks.getCheckpointData(tipAfterUnwind);
660
722
  if (candidateCheckpoint === undefined) {
661
723
  break;
662
724
  }
@@ -681,7 +743,7 @@ export class ArchiverL1Synchronizer implements Traceable {
681
743
  this.log.warn(
682
744
  `Removed ${count(checkpointsToRemove, 'checkpoint')} after checkpoint ${tipAfterUnwind} ` +
683
745
  `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` +
684
- `Updated L2 latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`,
746
+ `Updated L2 latest checkpoint is ${await this.stores.blocks.getLatestCheckpointNumber()}.`,
685
747
  );
686
748
  }
687
749
  }
@@ -698,23 +760,20 @@ export class ArchiverL1Synchronizer implements Traceable {
698
760
 
699
761
  this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
700
762
 
701
- // TODO(md): Retrieve from blob client then from consensus client, then from peers
702
- const retrievedCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointsFromRollup', () =>
703
- retrieveCheckpointsFromRollup(
763
+ // First fetch calldata only, no blobs yet, since we may be able to just get that data out of the proposed chain
764
+ const calldataCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointCalldataFromRollup', () =>
765
+ retrieveCheckpointCalldataFromRollup(
704
766
  this.rollup,
705
767
  this.publicClient,
706
768
  this.debugClient,
707
- this.blobClient,
708
769
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
709
770
  searchEndBlock,
710
- this.l1Addresses,
711
771
  this.instrumentation,
712
772
  this.log,
713
- !initialSyncComplete, // isHistoricalSync
714
773
  ),
715
774
  );
716
775
 
717
- if (retrievedCheckpoints.length === 0) {
776
+ if (calldataCheckpoints.length === 0) {
718
777
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
719
778
  // See further details in earlier comments.
720
779
  this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
@@ -722,21 +781,56 @@ export class ArchiverL1Synchronizer implements Traceable {
722
781
  }
723
782
 
724
783
  this.log.debug(
725
- `Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
784
+ `Retrieved ${calldataCheckpoints.length} new checkpoint calldata between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
726
785
  {
727
- lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
786
+ lastProcessedCheckpoint: calldataCheckpoints[calldataCheckpoints.length - 1].l1,
728
787
  searchStartBlock,
729
788
  searchEndBlock,
730
789
  },
731
790
  );
732
791
 
733
- const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map(b => retrievedToPublishedCheckpoint(b)));
792
+ // Check if the last checkpoint matches a local pending entry (so we can skip blob fetch).
793
+ // We only check the last one; if it matches, the blob fetch is skipped for that entry.
794
+ // TODO(palla/pipelining): We may have more than a single checkpoint to promote
795
+ const lastCalldataCheckpoint = calldataCheckpoints[calldataCheckpoints.length - 1];
796
+ const promoteResult = await this.tryBuildPublishedCheckpointFromProposed(lastCalldataCheckpoint);
797
+ const checkpointToPromote = promoteResult && !('diverged' in promoteResult) ? promoteResult : undefined;
798
+ const evictProposedFrom =
799
+ promoteResult && 'diverged' in promoteResult ? promoteResult.fromCheckpointNumber : undefined;
800
+
801
+ // Then fetch blobs in parallel and build the full published checkpoints
802
+ const toFetchBlobs = checkpointToPromote ? calldataCheckpoints.slice(0, -1) : calldataCheckpoints;
803
+ const blobFetched = await asyncPool(10, toFetchBlobs, async checkpoint =>
804
+ retrievedToPublishedCheckpoint({
805
+ ...checkpoint,
806
+ checkpointBlobData: await getCheckpointBlobDataFromBlobs(
807
+ this.blobClient,
808
+ checkpoint.l1.blockHash,
809
+ checkpoint.blobHashes,
810
+ checkpoint.checkpointNumber,
811
+ this.log,
812
+ !initialSyncComplete,
813
+ checkpoint.parentBeaconBlockRoot,
814
+ checkpoint.l1.timestamp,
815
+ ),
816
+ }),
817
+ );
818
+
819
+ // And add the promoted checkpoint to the list of all checkpoints
820
+ const publishedCheckpoints = checkpointToPromote ? [...blobFetched, checkpointToPromote] : blobFetched;
734
821
  const validCheckpoints: PublishedCheckpoint[] = [];
735
822
 
823
+ // Now loop through all checkpoints and validate their attestations
736
824
  for (const published of publishedCheckpoints) {
737
825
  const validationResult = this.config.skipValidateCheckpointAttestations
738
826
  ? { valid: true as const }
739
- : await validateCheckpointAttestations(published, this.epochCache, this.l1Constants, this.log);
827
+ : await validateCheckpointAttestations(
828
+ published,
829
+ this.epochCache,
830
+ this.l1Constants,
831
+ this.getSignatureContext(),
832
+ this.log,
833
+ );
740
834
 
741
835
  // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
742
836
  // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
@@ -774,7 +868,7 @@ export class ArchiverL1Synchronizer implements Traceable {
774
868
  // Check the inHash of the checkpoint against the l1->l2 messages.
775
869
  // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
776
870
  // checkpoints we just retrieved.
777
- const l1ToL2Messages = await this.store.getL1ToL2Messages(published.checkpoint.number);
871
+ const l1ToL2Messages = await this.stores.messages.getL1ToL2Messages(published.checkpoint.number);
778
872
  const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
779
873
  const publishedInHash = published.checkpoint.header.inHash;
780
874
  if (!computedInHash.equals(publishedInHash)) {
@@ -802,25 +896,53 @@ export class ArchiverL1Synchronizer implements Traceable {
802
896
  );
803
897
  }
804
898
 
899
+ for (const published of validCheckpoints) {
900
+ this.instrumentation.processCheckpointL1Timing({
901
+ slotNumber: published.checkpoint.header.slotNumber,
902
+ l1Timestamp: published.l1.timestamp,
903
+ l1Constants: this.l1Constants,
904
+ });
905
+ }
906
+
805
907
  try {
806
908
  const updatedValidationResult =
807
909
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
910
+
911
+ // Split valid checkpoints: the promoted one (if any) is persisted via the proposed-promotion path,
912
+ // the rest via addCheckpoints. Both paths run within the same store transaction for atomicity.
913
+ const [[maybeValidCheckpointToPromote], checkpointsToAdd] = partition(
914
+ validCheckpoints,
915
+ c => c.checkpoint.number === checkpointToPromote?.checkpoint.number,
916
+ );
917
+
808
918
  const [processDuration, result] = await elapsed(() =>
809
919
  execInSpan(this.tracer, 'Archiver.addCheckpoints', () =>
810
- this.updater.addCheckpoints(validCheckpoints, updatedValidationResult),
920
+ this.updater.addCheckpoints(
921
+ checkpointsToAdd,
922
+ updatedValidationResult,
923
+ maybeValidCheckpointToPromote && {
924
+ l1: lastCalldataCheckpoint.l1,
925
+ attestations: lastCalldataCheckpoint.attestations,
926
+ checkpoint: maybeValidCheckpointToPromote,
927
+ },
928
+ evictProposedFrom,
929
+ ),
811
930
  ),
812
931
  );
813
- this.instrumentation.processNewBlocks(
814
- processDuration / validCheckpoints.length,
815
- validCheckpoints.flatMap(c => c.checkpoint.blocks),
816
- );
932
+
933
+ if (checkpointsToAdd.length > 0) {
934
+ this.instrumentation.processNewCheckpointedBlocks(
935
+ processDuration / checkpointsToAdd.length,
936
+ checkpointsToAdd.flatMap(c => c.checkpoint.blocks),
937
+ );
938
+ }
817
939
 
818
940
  // If blocks were pruned due to conflict with L1 checkpoints, emit event
819
941
  if (result.prunedBlocks && result.prunedBlocks.length > 0) {
820
942
  const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
821
943
  const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
822
944
 
823
- this.log.warn(
945
+ this.log.info(
824
946
  `Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
825
947
  { prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
826
948
  );
@@ -838,10 +960,10 @@ export class ArchiverL1Synchronizer implements Traceable {
838
960
  if (err instanceof InitialCheckpointNumberNotSequentialError) {
839
961
  const { previousCheckpointNumber, newCheckpointNumber } = err;
840
962
  const previousCheckpoint = previousCheckpointNumber
841
- ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber))
963
+ ? await this.stores.blocks.getCheckpointData(CheckpointNumber(previousCheckpointNumber))
842
964
  : undefined;
843
965
  const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1Constants.l1StartBlock;
844
- await this.store.setCheckpointSynchedL1BlockNumber(updatedL1SyncPoint);
966
+ await this.stores.blocks.setSynchedL1BlockNumber(updatedL1SyncPoint);
845
967
  this.log.warn(
846
968
  `Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`,
847
969
  {
@@ -866,7 +988,7 @@ export class ArchiverL1Synchronizer implements Traceable {
866
988
  });
867
989
  }
868
990
  lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
869
- lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
991
+ lastL1BlockWithCheckpoint = calldataCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
870
992
  } while (searchEndBlock < currentL1BlockNumber);
871
993
 
872
994
  // Important that we update AFTER inserting the blocks.
@@ -875,6 +997,82 @@ export class ArchiverL1Synchronizer implements Traceable {
875
997
  return { ...rollupStatus, lastRetrievedCheckpoint, lastL1BlockWithCheckpoint };
876
998
  }
877
999
 
1000
+ /**
1001
+ * Checks if a specific checkpoint matches a local pending entry, and if so, loads local data to build
1002
+ * a synthetic published checkpoint (skipping blob fetch).
1003
+ *
1004
+ * Returns { diverged: true, fromCheckpointNumber } when the L1 checkpoint does NOT match local pending
1005
+ * data for that number, so the caller can evict the entire pending suffix >= fromCheckpointNumber
1006
+ * (those entries chain off the now-invalid local state) within the same addCheckpoints transaction.
1007
+ */
1008
+ private async tryBuildPublishedCheckpointFromProposed(
1009
+ calldataCheckpoint: RetrievedCheckpointFromCalldata | undefined,
1010
+ ): Promise<PublishedCheckpoint | { diverged: true; fromCheckpointNumber: CheckpointNumber } | undefined> {
1011
+ if (this.config.skipPromoteProposedCheckpointDuringL1Sync || !calldataCheckpoint) {
1012
+ return undefined;
1013
+ }
1014
+
1015
+ // Look up the specific pending entry for the checkpoint being mined, not just the tip
1016
+ const proposed = await this.stores.blocks.getProposedCheckpointByNumber(calldataCheckpoint.checkpointNumber);
1017
+ if (!proposed) {
1018
+ return undefined;
1019
+ }
1020
+
1021
+ if (
1022
+ !proposed.header.equals(calldataCheckpoint.header) ||
1023
+ !proposed.archive.root.equals(calldataCheckpoint.archiveRoot)
1024
+ ) {
1025
+ this.log.warn(
1026
+ `Local proposed checkpoint ${proposed.checkpointNumber} does not match checkpoint retrieved from L1, overriding with L1 data`,
1027
+ {
1028
+ proposedCheckpointNumber: proposed.checkpointNumber,
1029
+ proposedHeader: proposed.header.toInspect(),
1030
+ proposedArchiveRoot: proposed.archive.root.toString(),
1031
+ calldataCheckpointNumber: calldataCheckpoint.checkpointNumber,
1032
+ calldataHeader: calldataCheckpoint.header.toInspect(),
1033
+ calldataArchiveRoot: calldataCheckpoint.archiveRoot.toString(),
1034
+ },
1035
+ );
1036
+ // Return a divergence signal so the caller can evict pending >= this number
1037
+ return { diverged: true, fromCheckpointNumber: proposed.checkpointNumber };
1038
+ }
1039
+
1040
+ this.log.debug(
1041
+ `Building published checkpoint from proposed ${calldataCheckpoint.checkpointNumber} (skipping blob fetch)`,
1042
+ { proposedHeader: proposed.header.toInspect(), proposedArchiveRoot: proposed.archive.root.toString() },
1043
+ );
1044
+
1045
+ const blocks = await this.stores.blocks.getBlocks(BlockNumber(proposed.startBlock), proposed.blockCount);
1046
+ if (blocks.length !== proposed.blockCount) {
1047
+ this.log.warn(
1048
+ `Local proposed checkpoint ${proposed.checkpointNumber} has wrong block count (expected ${proposed.blockCount} blocks starting at ${proposed.startBlock} but got ${blocks.length})`,
1049
+ {
1050
+ proposedCheckpointNumber: proposed.checkpointNumber,
1051
+ proposedStartBlock: proposed.startBlock,
1052
+ proposedBlockCount: proposed.blockCount,
1053
+ retrievedBlocks: blocks.map(b => b.number),
1054
+ },
1055
+ );
1056
+ return undefined;
1057
+ }
1058
+
1059
+ const checkpoint = Checkpoint.from({
1060
+ archive: proposed.archive,
1061
+ header: proposed.header,
1062
+ blocks,
1063
+ number: proposed.checkpointNumber,
1064
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier,
1065
+ });
1066
+ const promotedCheckpoint = PublishedCheckpoint.from({
1067
+ checkpoint,
1068
+ l1: calldataCheckpoint.l1,
1069
+ attestations: calldataCheckpoint.attestations,
1070
+ });
1071
+ this.instrumentation.processCheckpointPromoted();
1072
+
1073
+ return promotedCheckpoint;
1074
+ }
1075
+
878
1076
  private async checkForNewCheckpointsBeforeL1SyncPoint(
879
1077
  status: RollupStatus,
880
1078
  blocksSynchedTo: bigint,
@@ -884,7 +1082,7 @@ export class ArchiverL1Synchronizer implements Traceable {
884
1082
  // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
885
1083
  // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
886
1084
  const latestLocalCheckpointNumber =
887
- lastRetrievedCheckpoint?.checkpoint.number ?? (await this.store.getSynchedCheckpointNumber());
1085
+ lastRetrievedCheckpoint?.checkpoint.number ?? (await this.stores.blocks.getLatestCheckpointNumber());
888
1086
  if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
889
1087
  // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
890
1088
  // but still haven't reached the pending checkpoint according to the call to the rollup contract.
@@ -897,7 +1095,9 @@ export class ArchiverL1Synchronizer implements Traceable {
897
1095
  latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
898
1096
  targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
899
1097
  } else if (latestLocalCheckpointNumber > 0) {
900
- const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c]) => c);
1098
+ const checkpoint = await this.stores.blocks
1099
+ .getRangeOfCheckpoints(latestLocalCheckpointNumber, 1)
1100
+ .then(([c]) => c);
901
1101
  latestLocalCheckpointArchive = checkpoint.archive.root.toString();
902
1102
  targetL1BlockNumber = checkpoint.l1.blockNumber;
903
1103
  }
@@ -912,7 +1112,7 @@ export class ArchiverL1Synchronizer implements Traceable {
912
1112
  ...status,
913
1113
  },
914
1114
  );
915
- await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
1115
+ await this.stores.blocks.setSynchedL1BlockNumber(targetL1BlockNumber);
916
1116
  } else {
917
1117
  this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
918
1118
  latestLocalCheckpointNumber,
@@ -922,7 +1122,7 @@ export class ArchiverL1Synchronizer implements Traceable {
922
1122
  }
923
1123
 
924
1124
  private async getCheckpointHeader(number: CheckpointNumber) {
925
- const checkpoint = await this.store.getCheckpointData(number);
1125
+ const checkpoint = await this.stores.blocks.getCheckpointData(number);
926
1126
  if (!checkpoint) {
927
1127
  return undefined;
928
1128
  }