@aztec/archiver 0.0.1-commit.2ed92850 → 0.0.1-commit.2f68f620

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 (142) hide show
  1. package/README.md +19 -11
  2. package/dest/archiver.d.ts +60 -19
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +292 -155
  5. package/dest/config.d.ts +4 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +21 -14
  8. package/dest/errors.d.ts +55 -9
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +81 -14
  11. package/dest/factory.d.ts +14 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +56 -41
  14. package/dest/index.d.ts +12 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +11 -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 +201 -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/trace_tx.d.ts +12 -66
  28. package/dest/l1/trace_tx.d.ts.map +1 -1
  29. package/dest/l1/validate_historical_logs.d.ts +23 -0
  30. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  31. package/dest/l1/validate_historical_logs.js +108 -0
  32. package/dest/l1/validate_trace.d.ts +6 -3
  33. package/dest/l1/validate_trace.d.ts.map +1 -1
  34. package/dest/l1/validate_trace.js +13 -9
  35. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  36. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  37. package/dest/modules/contract_data_source_adapter.js +40 -0
  38. package/dest/modules/data_source_base.d.ts +74 -45
  39. package/dest/modules/data_source_base.d.ts.map +1 -1
  40. package/dest/modules/data_source_base.js +269 -181
  41. package/dest/modules/data_store_updater.d.ts +49 -17
  42. package/dest/modules/data_store_updater.d.ts.map +1 -1
  43. package/dest/modules/data_store_updater.js +211 -121
  44. package/dest/modules/instrumentation.d.ts +21 -3
  45. package/dest/modules/instrumentation.d.ts.map +1 -1
  46. package/dest/modules/instrumentation.js +61 -19
  47. package/dest/modules/l1_synchronizer.d.ts +14 -12
  48. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  49. package/dest/modules/l1_synchronizer.js +445 -214
  50. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  51. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  52. package/dest/modules/outbox_trees_resolver.js +162 -0
  53. package/dest/modules/validation.d.ts +4 -3
  54. package/dest/modules/validation.d.ts.map +1 -1
  55. package/dest/modules/validation.js +6 -6
  56. package/dest/store/block_store.d.ts +174 -72
  57. package/dest/store/block_store.d.ts.map +1 -1
  58. package/dest/store/block_store.js +740 -273
  59. package/dest/store/contract_class_store.d.ts +17 -4
  60. package/dest/store/contract_class_store.d.ts.map +1 -1
  61. package/dest/store/contract_class_store.js +33 -73
  62. package/dest/store/contract_instance_store.d.ts +28 -1
  63. package/dest/store/contract_instance_store.d.ts.map +1 -1
  64. package/dest/store/contract_instance_store.js +37 -2
  65. package/dest/store/data_stores.d.ts +68 -0
  66. package/dest/store/data_stores.d.ts.map +1 -0
  67. package/dest/store/data_stores.js +54 -0
  68. package/dest/store/function_names_cache.d.ts +17 -0
  69. package/dest/store/function_names_cache.d.ts.map +1 -0
  70. package/dest/store/function_names_cache.js +30 -0
  71. package/dest/store/l2_tips_cache.d.ts +25 -0
  72. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  73. package/dest/store/l2_tips_cache.js +26 -0
  74. package/dest/store/log_store.d.ts +42 -37
  75. package/dest/store/log_store.d.ts.map +1 -1
  76. package/dest/store/log_store.js +262 -388
  77. package/dest/store/log_store_codec.d.ts +70 -0
  78. package/dest/store/log_store_codec.d.ts.map +1 -0
  79. package/dest/store/log_store_codec.js +101 -0
  80. package/dest/store/message_store.d.ts +11 -1
  81. package/dest/store/message_store.d.ts.map +1 -1
  82. package/dest/store/message_store.js +51 -9
  83. package/dest/test/fake_l1_state.d.ts +25 -1
  84. package/dest/test/fake_l1_state.d.ts.map +1 -1
  85. package/dest/test/fake_l1_state.js +166 -32
  86. package/dest/test/index.js +3 -1
  87. package/dest/test/mock_archiver.d.ts +1 -1
  88. package/dest/test/mock_archiver.d.ts.map +1 -1
  89. package/dest/test/mock_archiver.js +3 -2
  90. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  91. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  92. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  93. package/dest/test/mock_l2_block_source.d.ts +65 -48
  94. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  95. package/dest/test/mock_l2_block_source.js +324 -215
  96. package/dest/test/mock_structs.d.ts +6 -2
  97. package/dest/test/mock_structs.d.ts.map +1 -1
  98. package/dest/test/mock_structs.js +20 -6
  99. package/dest/test/noop_l1_archiver.d.ts +30 -0
  100. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  101. package/dest/test/noop_l1_archiver.js +85 -0
  102. package/package.json +15 -14
  103. package/src/archiver.ts +376 -180
  104. package/src/config.ts +24 -11
  105. package/src/errors.ts +122 -21
  106. package/src/factory.ts +80 -39
  107. package/src/index.ts +19 -2
  108. package/src/l1/README.md +25 -68
  109. package/src/l1/bin/retrieve-calldata.ts +46 -39
  110. package/src/l1/calldata_retriever.ts +260 -379
  111. package/src/l1/data_retrieval.ts +59 -70
  112. package/src/l1/spire_proposer.ts +7 -15
  113. package/src/l1/validate_historical_logs.ts +140 -0
  114. package/src/l1/validate_trace.ts +24 -6
  115. package/src/modules/contract_data_source_adapter.ts +55 -0
  116. package/src/modules/data_source_base.ts +354 -226
  117. package/src/modules/data_store_updater.ts +248 -153
  118. package/src/modules/instrumentation.ts +71 -19
  119. package/src/modules/l1_synchronizer.ts +586 -260
  120. package/src/modules/outbox_trees_resolver.ts +199 -0
  121. package/src/modules/validation.ts +10 -9
  122. package/src/store/block_store.ts +919 -345
  123. package/src/store/contract_class_store.ts +39 -107
  124. package/src/store/contract_instance_store.ts +51 -5
  125. package/src/store/data_stores.ts +104 -0
  126. package/src/store/function_names_cache.ts +37 -0
  127. package/src/store/l2_tips_cache.ts +35 -0
  128. package/src/store/log_store.ts +303 -499
  129. package/src/store/log_store_codec.ts +132 -0
  130. package/src/store/message_store.ts +60 -10
  131. package/src/structs/inbox_message.ts +1 -1
  132. package/src/test/fake_l1_state.ts +213 -42
  133. package/src/test/index.ts +3 -0
  134. package/src/test/mock_archiver.ts +3 -2
  135. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  136. package/src/test/mock_l2_block_source.ts +392 -233
  137. package/src/test/mock_structs.ts +42 -12
  138. package/src/test/noop_l1_archiver.ts +148 -0
  139. package/dest/store/kv_archiver_store.d.ts +0 -340
  140. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  141. package/dest/store/kv_archiver_store.js +0 -447
  142. package/src/store/kv_archiver_store.ts +0 -639
package/src/archiver.ts CHANGED
@@ -1,56 +1,70 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
- import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
3
- import { EpochCache } from '@aztec/epoch-cache';
4
- import { BlockTagTooOldError, RollupContract } from '@aztec/ethereum/contracts';
2
+ import { EpochCache, PROPOSER_PIPELINING_SLOT_OFFSET } from '@aztec/epoch-cache';
3
+ import { BlockTagTooOldError, OutboxContract, RollupContract } from '@aztec/ethereum/contracts';
5
4
  import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
6
5
  import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
7
6
  import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
8
7
  import { Buffer32 } from '@aztec/foundation/buffer';
9
- import { merge } from '@aztec/foundation/collection';
8
+ import { merge, pick } from '@aztec/foundation/collection';
10
9
  import { Fr } from '@aztec/foundation/curves/bn254';
11
10
  import { EthAddress } from '@aztec/foundation/eth-address';
12
11
  import { type Logger, createLogger } from '@aztec/foundation/log';
13
12
  import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
14
13
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
15
- import { DateProvider } from '@aztec/foundation/timer';
14
+ import { DateProvider, elapsed } from '@aztec/foundation/timer';
16
15
  import {
17
16
  type ArchiverEmitter,
18
- type CheckpointId,
19
- GENESIS_CHECKPOINT_HEADER_HASH,
17
+ type BlockHash,
20
18
  L2Block,
21
19
  type L2BlockSink,
20
+ L2BlockSourceEvents,
22
21
  type L2Tips,
23
22
  type ValidateCheckpointResult,
24
23
  } from '@aztec/stdlib/block';
25
- import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
24
+ import { type ProposedCheckpointInput, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
26
25
  import {
27
26
  type L1RollupConstants,
28
- getEpochNumberAtTimestamp,
29
- getSlotAtTimestamp,
27
+ getEpochAtSlot,
28
+ getSlotAtNextL1Block,
30
29
  getSlotRangeForEpoch,
30
+ getTimestampForSlot,
31
31
  getTimestampRangeForEpoch,
32
32
  } from '@aztec/stdlib/epoch-helpers';
33
+ import type { L2ToL1MembershipWitness } from '@aztec/stdlib/messaging';
34
+ import type { BlockHeader, TxHash } from '@aztec/stdlib/tx';
33
35
  import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
34
36
 
35
37
  import { type ArchiverConfig, mapArchiverConfig } from './config.js';
36
- import { NoBlobBodiesFoundError } from './errors.js';
38
+ import { BlockAlreadyCheckpointedError, BlockOrCheckpointSlotExpiredError, NoBlobBodiesFoundError } from './errors.js';
39
+ import { validateAndLogHistoricalLogsAvailability } from './l1/validate_historical_logs.js';
37
40
  import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
38
41
  import { ArchiverDataSourceBase } from './modules/data_source_base.js';
39
42
  import { ArchiverDataStoreUpdater } from './modules/data_store_updater.js';
40
43
  import type { ArchiverInstrumentation } from './modules/instrumentation.js';
41
44
  import type { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
42
- import type { KVArchiverDataStore } from './store/kv_archiver_store.js';
45
+ import { OutboxTreesResolver } from './modules/outbox_trees_resolver.js';
46
+ import { type ArchiverDataStores, backupArchiverDataStores, getArchiverSynchPoint } from './store/data_stores.js';
47
+ import { L2TipsCache } from './store/l2_tips_cache.js';
43
48
 
44
49
  /** Export ArchiverEmitter for use in factory and tests. */
45
50
  export type { ArchiverEmitter };
46
51
 
47
52
  /** Request to add a block to the archiver, queued for processing by the sync loop. */
48
53
  type AddBlockRequest = {
54
+ type: 'block';
49
55
  block: L2Block;
50
56
  resolve: () => void;
51
57
  reject: (err: Error) => void;
52
58
  };
53
59
 
60
+ /** Request to add a proposed checkpoint to the archiver, queued for processing by the sync loop. */
61
+ type AddProposedCheckpointRequest = {
62
+ type: 'checkpoint';
63
+ checkpoint: ProposedCheckpointInput;
64
+ resolve: () => void;
65
+ reject: (err: Error) => void;
66
+ };
67
+
54
68
  export type ArchiverDeps = {
55
69
  telemetry?: TelemetryClient;
56
70
  blobClient: BlobClientInterface;
@@ -68,68 +82,107 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
68
82
  public readonly events: ArchiverEmitter;
69
83
 
70
84
  /** A loop in which we will be continually fetching new checkpoints. */
71
- private runningPromise: RunningPromise;
85
+ protected runningPromise: RunningPromise;
72
86
 
73
87
  /** L1 synchronizer that handles fetching checkpoints and messages from L1. */
74
88
  private readonly synchronizer: ArchiverL1Synchronizer;
75
89
 
90
+ /** Resolver for L2-to-L1 message membership witnesses, built over this archiver's read view. */
91
+ private readonly outboxTreesResolver: OutboxTreesResolver;
92
+
76
93
  private initialSyncComplete: boolean = false;
77
94
  private initialSyncPromise: PromiseWithResolvers<void>;
78
95
 
79
- /** Queue of blocks to be added to the store, processed by the sync loop. */
80
- private blockQueue: AddBlockRequest[] = [];
96
+ /** Queue of blocks and checkpoints to be added to the store, processed by the sync loop. */
97
+ private inboundQueue: (AddBlockRequest | AddProposedCheckpointRequest)[] = [];
81
98
 
82
99
  /** Helper to handle updates to the store */
83
100
  private readonly updater: ArchiverDataStoreUpdater;
84
101
 
102
+ /** In-memory cache for L2 chain tips. */
103
+ private readonly l2TipsCache: L2TipsCache;
104
+
85
105
  public readonly tracer: Tracer;
86
106
 
107
+ private readonly instrumentation: ArchiverInstrumentation;
108
+
87
109
  /**
88
110
  * Creates a new instance of the Archiver.
89
111
  * @param publicClient - A client for interacting with the Ethereum node.
90
112
  * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
91
113
  * @param rollup - Rollup contract instance.
114
+ * @param outbox - Outbox contract instance, used to read per-epoch L2-to-L1 message roots.
92
115
  * @param inbox - Inbox contract instance.
93
- * @param l1Addresses - L1 contract addresses (registry, governance proposer, slash factory, slashing proposer).
94
- * @param dataStore - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
116
+ * @param l1Addresses - L1 contract addresses (registry, governance proposer, slashing proposer).
117
+ * @param dataStores - Archiver substores for storage & retrieval of blocks, encrypted logs & contract data.
95
118
  * @param config - Archiver configuration options.
96
119
  * @param blobClient - Client for retrieving blob data.
97
- * @param epochCache - Cache for epoch-related data.
98
- * @param dateProvider - Provider for current date/time.
99
120
  * @param instrumentation - Instrumentation for metrics and tracing.
100
121
  * @param l1Constants - L1 rollup constants.
122
+ * @param synchronizer - L1 synchronizer that handles fetching checkpoints and messages from L1.
123
+ * @param events - Event emitter shared with the synchronizer.
124
+ * @param initialHeader - Genesis block header.
125
+ * @param initialBlockHash - Precomputed hash of the genesis block header.
126
+ * @param l2TipsCache - In-memory cache for L2 chain tips.
127
+ * @param dateProvider - Provider for current date/time, used for wall-clock orphan-block pruning.
101
128
  * @param log - A logger.
102
129
  */
103
130
  constructor(
104
131
  private readonly publicClient: ViemPublicClient,
105
132
  private readonly debugClient: ViemPublicDebugClient,
106
133
  private readonly rollup: RollupContract,
134
+ private readonly outbox: OutboxContract,
107
135
  private readonly l1Addresses: Pick<
108
136
  L1ContractAddresses,
109
- 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
110
- > & { slashingProposerAddress: EthAddress },
111
- readonly dataStore: KVArchiverDataStore,
137
+ 'rollupAddress' | 'registryAddress' | 'inboxAddress' | 'governanceProposerAddress'
138
+ > & {
139
+ slashingProposerAddress: EthAddress;
140
+ },
141
+ readonly dataStores: ArchiverDataStores,
112
142
  private config: {
113
143
  pollingIntervalMs: number;
114
144
  batchSize: number;
115
145
  skipValidateCheckpointAttestations?: boolean;
116
146
  maxAllowedEthClientDriftSeconds: number;
117
147
  ethereumAllowNoDebugHosts?: boolean;
148
+ skipHistoricalLogsCheck?: boolean;
149
+ orphanProposedBlockPruneGraceSeconds: number;
150
+ enableOrphanProposedBlockPruning: boolean;
118
151
  },
119
152
  private readonly blobClient: BlobClientInterface,
120
153
  instrumentation: ArchiverInstrumentation,
121
- protected override readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
154
+ protected override readonly l1Constants: L1RollupConstants & {
155
+ l1StartBlockHash: Buffer32;
156
+ genesisArchiveRoot: Fr;
157
+ },
122
158
  synchronizer: ArchiverL1Synchronizer,
123
159
  events: ArchiverEmitter,
160
+ initialHeader: BlockHeader,
161
+ initialBlockHash: BlockHash,
162
+ l2TipsCache: L2TipsCache,
163
+ private readonly dateProvider: DateProvider,
124
164
  private readonly log: Logger = createLogger('archiver'),
125
165
  ) {
126
- super(dataStore, l1Constants);
166
+ super(dataStores, l1Constants, initialHeader, initialBlockHash, l1Constants.genesisArchiveRoot);
127
167
 
128
168
  this.tracer = instrumentation.tracer;
169
+ this.instrumentation = instrumentation;
129
170
  this.initialSyncPromise = promiseWithResolvers();
130
171
  this.synchronizer = synchronizer;
131
172
  this.events = events;
132
- this.updater = new ArchiverDataStoreUpdater(this.dataStore);
173
+ this.l2TipsCache = l2TipsCache;
174
+ this.updater = new ArchiverDataStoreUpdater(this.dataStores, this.l2TipsCache, {
175
+ rollupManaLimit: l1Constants.rollupManaLimit,
176
+ });
177
+
178
+ // The resolver reads its witness data from this archiver and pins its lazy Outbox-root fetches
179
+ // to the archiver's synced L1 block. No method on `this` is invoked during construction.
180
+ this.outboxTreesResolver = new OutboxTreesResolver(
181
+ this.outbox,
182
+ this,
183
+ () => Promise.resolve(this.getL1BlockNumber()),
184
+ l1Constants.epochDuration,
185
+ );
133
186
 
134
187
  // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
135
188
  // are done as fast as possible. This then gets updated once the initial sync completes.
@@ -141,6 +194,27 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
141
194
  );
142
195
  }
143
196
 
197
+ /**
198
+ * Returns the L2-to-L1 membership witness for `message` emitted by tx `txHash`. The node selects
199
+ * the smallest partial-proof root on the Outbox that covers the tx's checkpoint and builds the
200
+ * witness against it.
201
+ *
202
+ * The node reads the Outbox roots lazily, pinned to its synced L1 block, so the witness reflects
203
+ * the node's synced view. Returns `undefined` if the tx isn't yet in a block/epoch or no covering
204
+ * root has landed on L1 as of the synced block.
205
+ *
206
+ * Caveat: roots are cached only for the current synced L1 block and re-fetched once the node
207
+ * advances, so a reorg is picked up on the next synced-block advance rather than re-validated per
208
+ * request.
209
+ */
210
+ public getL2ToL1MembershipWitness(
211
+ txHash: TxHash,
212
+ message: Fr,
213
+ messageIndexInTx?: number,
214
+ ): Promise<L2ToL1MembershipWitness | undefined> {
215
+ return this.outboxTreesResolver.getL2ToL1MembershipWitness(txHash, message, messageIndexInTx);
216
+ }
217
+
144
218
  /** Updates archiver config */
145
219
  public updateConfig(newConfig: Partial<ArchiverConfig>) {
146
220
  this.config = merge(this.config, mapArchiverConfig(newConfig));
@@ -158,12 +232,29 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
158
232
 
159
233
  await this.blobClient.testSources();
160
234
  await this.synchronizer.testEthereumNodeSynced();
161
- await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
235
+ await validateAndLogTraceAvailability(
236
+ this.debugClient,
237
+ this.config.ethereumAllowNoDebugHosts ?? false,
238
+ this.log.getBindings(),
239
+ );
240
+ await validateAndLogHistoricalLogsAvailability(
241
+ this.publicClient,
242
+ {
243
+ rollupAddress: this.l1Addresses.rollupAddress,
244
+ inboxAddress: this.l1Addresses.inboxAddress,
245
+ registryAddress: this.l1Addresses.registryAddress,
246
+ governanceProposerAddress: this.l1Addresses.governanceProposerAddress,
247
+ },
248
+ this.config.skipHistoricalLogsCheck ?? false,
249
+ this.log.getBindings(),
250
+ );
162
251
 
163
252
  // Log initial state for the archiver
164
253
  const { l1StartBlock } = this.l1Constants;
165
- const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
166
- const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
254
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await getArchiverSynchPoint(
255
+ this.stores,
256
+ );
257
+ const currentL2Checkpoint = await this.getCheckpointNumber();
167
258
  this.log.info(
168
259
  `Starting archiver sync to rollup contract ${this.rollup.address} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`,
169
260
  { blocksSynchedTo, messagesSynchedTo, currentL2Checkpoint },
@@ -180,6 +271,14 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
180
271
  return this.runningPromise.trigger();
181
272
  }
182
273
 
274
+ public trySyncImmediate() {
275
+ try {
276
+ return this.syncImmediate();
277
+ } catch (err) {
278
+ this.log.error(`Failed to trigger immediate archiver sync: ${err}`, err);
279
+ }
280
+ }
281
+
183
282
  /**
184
283
  * Queues a block to be added to the archiver store and triggers processing.
185
284
  * The block will be processed by the sync loop.
@@ -188,38 +287,85 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
188
287
  * @returns A promise that resolves when the block has been added to the store, or rejects on error.
189
288
  */
190
289
  public addBlock(block: L2Block): Promise<void> {
191
- return new Promise<void>((resolve, reject) => {
192
- this.blockQueue.push({ block, resolve, reject });
193
- this.log.debug(`Queued block ${block.number} for processing`);
194
- // Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
195
- this.syncImmediate().catch(err => {
196
- this.log.error(`Sync immediate call failed: ${err}`);
197
- });
198
- });
290
+ const promise = promiseWithResolvers<void>();
291
+ this.inboundQueue.push({ block, ...promise, type: 'block' });
292
+ this.log.debug(`Queued block ${block.number} for processing`);
293
+ void this.trySyncImmediate();
294
+ return promise.promise;
295
+ }
296
+
297
+ /**
298
+ * Queues a new proposed checkpoint into the archiver store.
299
+ * Checks that the checkpoint is not for an L2 slot already synced from L1.
300
+ * Resolves once the checkpoint has been processed.
301
+ */
302
+ public addProposedCheckpoint(pending: ProposedCheckpointInput): Promise<void> {
303
+ const promise = promiseWithResolvers<void>();
304
+ this.inboundQueue.push({ checkpoint: pending, ...promise, type: 'checkpoint' });
305
+ this.log.debug(`Queued checkpoint ${pending.checkpointNumber} for processing`);
306
+ void this.trySyncImmediate();
307
+ return promise.promise;
199
308
  }
200
309
 
201
310
  /**
202
- * Processes all queued blocks, adding them to the store.
311
+ * Processes all queued blocks and checkpoints, adding them to the store.
203
312
  * Called at the beginning of each sync iteration.
204
- * Blocks are processed in the order they were queued.
313
+ * Items are processed in the order they were queued.
205
314
  */
206
- private async processQueuedBlocks(): Promise<void> {
207
- if (this.blockQueue.length === 0) {
315
+ private async processInboundQueue(): Promise<void> {
316
+ if (this.inboundQueue.length === 0) {
208
317
  return;
209
318
  }
210
319
 
211
- // Take all blocks from the queue
212
- const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
213
- this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
320
+ // Take all items from the queue
321
+ const queuedItems = this.inboundQueue.splice(0, this.inboundQueue.length);
322
+ this.log.debug(`Processing ${queuedItems.length} queued inbound items`);
323
+
324
+ // Calculate slot threshold for validation
325
+ const l1Timestamp = this.synchronizer.getL1Timestamp();
326
+ const slotAtNextL1Block =
327
+ l1Timestamp === undefined ? undefined : getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
328
+
329
+ // Helpers for manipulating blocks and checkpoints in the queue
330
+ const getSlot: (item: AddBlockRequest | AddProposedCheckpointRequest) => SlotNumber = item =>
331
+ item.type === 'block' ? item.block.header.globalVariables.slotNumber : item.checkpoint.header.slotNumber;
332
+ const getNumber: (item: AddBlockRequest | AddProposedCheckpointRequest) => number = item =>
333
+ item.type === 'block' ? item.block.number : item.checkpoint.checkpointNumber;
334
+
335
+ // Process each item individually to properly resolve/reject each promise
336
+ for (const item of queuedItems) {
337
+ const { resolve, reject, type } = item;
338
+ const itemSlot = getSlot(item);
339
+ const itemNumber = getNumber(item);
340
+ if (slotAtNextL1Block !== undefined && itemSlot < slotAtNextL1Block) {
341
+ const nextSlotTimestamp = getTimestampForSlot(slotAtNextL1Block, this.l1Constants);
342
+ this.log.warn(
343
+ `Rejecting proposed ${type} ${itemNumber} for past slot ${itemSlot} (current ${slotAtNextL1Block})`,
344
+ { number: itemNumber, type, l1Timestamp, slotAtNextL1Block, nextSlotTimestamp },
345
+ );
346
+ reject(new BlockOrCheckpointSlotExpiredError(itemSlot, nextSlotTimestamp, l1Timestamp));
347
+ continue;
348
+ }
214
349
 
215
- // Process each block individually to properly resolve/reject each promise
216
- for (const { block, resolve, reject } of queuedItems) {
217
350
  try {
218
- await this.updater.addProposedBlocks([block]);
219
- this.log.debug(`Added block ${block.number} to store`);
351
+ if (type === 'block') {
352
+ const [durationMs] = await elapsed(() => this.updater.addProposedBlock(item.block));
353
+ this.instrumentation.processNewProposedBlock(durationMs, item.block);
354
+ } else {
355
+ await this.updater.addProposedCheckpoint(item.checkpoint);
356
+ }
357
+ this.log.debug(`Added ${type} ${itemNumber} to store`);
220
358
  resolve();
221
359
  } catch (err: any) {
222
- this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
360
+ if (err instanceof BlockAlreadyCheckpointedError) {
361
+ this.log.debug(`Proposed block ${itemNumber} matches already checkpointed block, ignoring late proposal`);
362
+ resolve();
363
+ continue;
364
+ }
365
+ this.log.error(`Failed to add ${type} ${itemNumber} to store: ${err.message}`, err, {
366
+ number: itemNumber,
367
+ type,
368
+ });
223
369
  reject(err);
224
370
  }
225
371
  }
@@ -235,9 +381,106 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
235
381
  @trackSpan('Archiver.sync')
236
382
  private async sync() {
237
383
  // Process any queued blocks first, before doing L1 sync
238
- await this.processQueuedBlocks();
384
+ await this.processInboundQueue();
239
385
  // Now perform L1 sync
240
386
  await this.syncFromL1();
387
+ // Prune proposed blocks with no corresponding proposed checkpoint by the end of their build slot.
388
+ await this.pruneOrphanProposedBlocks();
389
+ }
390
+
391
+ /**
392
+ * Prunes a block-only local tip that was built atop a checkpoint that was never itself proposed.
393
+ *
394
+ * Under pipelining, a proposer publishes the blocks for a checkpoint (block-only proposals) before
395
+ * assembling and publishing the enclosing proposed checkpoint at the end of the build slot. A node
396
+ * that received those blocks but never the proposed checkpoint is left with an orphan tip it must
397
+ * not build on. We prune it once enough wall-clock time has elapsed that the proposed checkpoint
398
+ * should have arrived. This runs on wall-clock time (not L1 block advancement) so it fires during
399
+ * quiet L1 periods, and is the liveness counterpart to the sequencer's checkSync guard.
400
+ *
401
+ * The uncheckpointed suffix is scanned in order. Blocks covered by proposed checkpoints are left in
402
+ * place; the first block not covered by a proposed checkpoint starts the orphan suffix to prune.
403
+ */
404
+ private async pruneOrphanProposedBlocks(): Promise<void> {
405
+ // Orphan pruning is a pipelining-only liveness mechanism: it cleans up block-only proposals
406
+ // whose enclosing checkpoint never arrived over gossip. The test-only automine sequencer is
407
+ // non-pipelined — it publishes each checkpoint synchronously in the slot it builds — so it
408
+ // never produces orphan proposed blocks. Running the prune there would race its two-step local
409
+ // push (addBlock then addProposedCheckpoint) and delete the block before its checkpoint attaches.
410
+ if (!this.config.enableOrphanProposedBlockPruning) {
411
+ return;
412
+ }
413
+ const tips = await this.getL2Tips();
414
+ const now = BigInt(this.dateProvider.nowInSeconds());
415
+ const pipeliningOffset = PROPOSER_PIPELINING_SLOT_OFFSET;
416
+
417
+ // The proposed tip is a proposed-checkpointed block, so there are no orphan proposed blocks to prune
418
+ if (tips.proposedCheckpoint.block.number === tips.proposed.number) {
419
+ this.log.trace(
420
+ `No orphan proposed blocks to prune: proposed tip ${tips.proposed.number} is checkpointed`,
421
+ pick(tips, 'proposed', 'proposedCheckpoint'),
422
+ );
423
+ return;
424
+ }
425
+
426
+ // Load the blocks that are candidates for pruning (ie blocks without a proposed checkpoint covering them)
427
+ const blocksWithoutProposedCheckpoint = await this.stores.blocks.getBlocksData({
428
+ from: BlockNumber(tips.proposedCheckpoint.block.number + 1),
429
+ limit: tips.proposed.number - tips.proposedCheckpoint.block.number,
430
+ });
431
+
432
+ // Iterate through them in order, the first one with a slot that should have received a proposed checkpoint
433
+ // is the first orphan block, and all blocks after it are also orphaned and should be pruned.
434
+ let lastSlotChecked = undefined;
435
+ for (const blockData of blocksWithoutProposedCheckpoint) {
436
+ // No need to recheck if this block had the same slot as the previous one.
437
+ const blockSlot = blockData.header.getSlot();
438
+ const blockNumber = blockData.header.getBlockNumber();
439
+ if (lastSlotChecked !== undefined && blockSlot === lastSlotChecked) {
440
+ continue;
441
+ }
442
+ lastSlotChecked = blockSlot;
443
+
444
+ // The proposed checkpoint should have landed by the start of the slot after the block's build slot
445
+ // (build slot = blockSlot - pipeliningOffset). Wait a grace period beyond that to tolerate propagation.
446
+ const expectedCheckpointedBySlot = SlotNumber(Number(blockSlot) - pipeliningOffset + 1);
447
+ const expectedCheckpointedByTime =
448
+ getTimestampForSlot(expectedCheckpointedBySlot, this.l1Constants) +
449
+ BigInt(this.config.orphanProposedBlockPruneGraceSeconds);
450
+
451
+ // If it's not checkpointed by the expected time, prune it along with all blocks after it.
452
+ if (now >= expectedCheckpointedByTime) {
453
+ const pruneAfterBlockNumber = BlockNumber(blockNumber - 1);
454
+ this.log.warn(
455
+ `Pruning orphan blocks after block ${pruneAfterBlockNumber}: block at slot ${blockSlot} belongs to ` +
456
+ `checkpoint ${blockData.checkpointNumber} which has no matching proposed checkpoint`,
457
+ {
458
+ firstUncheckpointedBlockHeader: blockData.header.toInspect(),
459
+ blockCheckpointNumber: blockData.checkpointNumber,
460
+ blockNumber,
461
+ blockSlot,
462
+ pipeliningOffset,
463
+ expectedCheckpointedBySlot,
464
+ expectedCheckpointedByTime,
465
+ now,
466
+ },
467
+ );
468
+
469
+ const prunedBlocks = await this.updater.removeBlocksWithoutProposedCheckpointAfter(pruneAfterBlockNumber);
470
+ if (prunedBlocks.length > 0) {
471
+ this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
472
+ type: L2BlockSourceEvents.L2PruneUncheckpointed,
473
+ slotNumber: blockSlot,
474
+ blocks: prunedBlocks,
475
+ });
476
+ }
477
+ return;
478
+ }
479
+ }
480
+
481
+ this.log.trace('No orphan proposed blocks to prune: all uncheckpointed blocks are still within the grace period', {
482
+ blocksWithoutProposedCheckpoint: blocksWithoutProposedCheckpoint.map(b => b.header.toInspect()),
483
+ });
241
484
  }
242
485
 
243
486
  private async syncFromL1() {
@@ -251,7 +494,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
251
494
  if (currentL1BlockNumber + 1n >= l1BlockNumberAtEnd) {
252
495
  this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
253
496
  l1BlockNumber: currentL1BlockNumber,
254
- syncPoint: await this.store.getSynchPoint(),
497
+ syncPoint: await getArchiverSynchPoint(this.stores),
255
498
  ...(await this.getL2Tips()),
256
499
  });
257
500
  this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
@@ -283,7 +526,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
283
526
  }
284
527
 
285
528
  public backupTo(destPath: string): Promise<string> {
286
- return this.dataStore.backupTo(destPath);
529
+ return backupArchiverDataStores(this.dataStores, destPath);
287
530
  }
288
531
 
289
532
  public getL1Constants(): Promise<L1RollupConstants> {
@@ -310,24 +553,56 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
310
553
  return Promise.resolve(this.synchronizer.getL1Timestamp());
311
554
  }
312
555
 
313
- public getL2SlotNumber(): Promise<SlotNumber | undefined> {
556
+ public async getSyncedL2SlotNumber(): Promise<SlotNumber | undefined> {
557
+ // The synced L2 slot is the latest slot for which we have all L1 data,
558
+ // either because we have seen all L1 blocks for that slot, or because
559
+ // we have seen the corresponding checkpoint.
560
+
561
+ let slotFromL1Sync: SlotNumber | undefined;
314
562
  const l1Timestamp = this.synchronizer.getL1Timestamp();
315
- return Promise.resolve(l1Timestamp === undefined ? undefined : getSlotAtTimestamp(l1Timestamp, this.l1Constants));
563
+ if (l1Timestamp !== undefined) {
564
+ const nextL1BlockSlot = getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
565
+ if (Number(nextL1BlockSlot) > 0) {
566
+ slotFromL1Sync = SlotNumber.add(nextL1BlockSlot, -1);
567
+ }
568
+ }
569
+
570
+ let slotFromCheckpoint: SlotNumber | undefined;
571
+ const latestCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
572
+ if (latestCheckpointNumber > 0) {
573
+ const checkpointData = await this.stores.blocks.getCheckpointData(latestCheckpointNumber);
574
+ if (checkpointData) {
575
+ slotFromCheckpoint = checkpointData.header.slotNumber;
576
+ }
577
+ }
578
+
579
+ if (slotFromL1Sync === undefined && slotFromCheckpoint === undefined) {
580
+ return undefined;
581
+ }
582
+ return SlotNumber(Math.max(slotFromL1Sync ?? 0, slotFromCheckpoint ?? 0));
316
583
  }
317
584
 
318
- public getL2EpochNumber(): Promise<EpochNumber | undefined> {
319
- const l1Timestamp = this.synchronizer.getL1Timestamp();
320
- return Promise.resolve(
321
- l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(l1Timestamp, this.l1Constants),
322
- );
585
+ public async getSyncedL2EpochNumber(): Promise<EpochNumber | undefined> {
586
+ const syncedSlot = await this.getSyncedL2SlotNumber();
587
+ if (syncedSlot === undefined) {
588
+ return undefined;
589
+ }
590
+ // An epoch is fully synced when all its slots are synced.
591
+ // We check if syncedSlot is the last slot of its epoch; if so, that epoch is fully synced.
592
+ // Otherwise, only the previous epoch is fully synced.
593
+ const epoch = getEpochAtSlot(syncedSlot, this.l1Constants);
594
+ const [, endSlot] = getSlotRangeForEpoch(epoch, this.l1Constants);
595
+ if (syncedSlot >= endSlot) {
596
+ return epoch;
597
+ }
598
+ return Number(epoch) > 0 ? EpochNumber(Number(epoch) - 1) : undefined;
323
599
  }
324
600
 
325
601
  public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
326
602
  // The epoch is complete if the current checkpointed L2 block is the last one in the epoch (or later).
327
603
  // We use the checkpointed block number (synced from L1) instead of 'latest' to avoid returning true
328
604
  // prematurely when proposed blocks have been pushed to the archiver but not yet checkpointed on L1.
329
- const checkpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
330
- const header = checkpointedBlockNumber > 0 ? await this.getBlockHeader(checkpointedBlockNumber) : undefined;
605
+ const header = (await this.getBlockData({ tag: 'checkpointed' }))?.header;
331
606
  const slot = header ? header.globalVariables.slotNumber : undefined;
332
607
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
333
608
  if (slot && slot >= endSlot) {
@@ -371,115 +646,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
371
646
  return true;
372
647
  }
373
648
 
374
- public async getL2Tips(): Promise<L2Tips> {
375
- const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
376
- this.getBlockNumber(),
377
- this.getProvenBlockNumber(),
378
- this.getCheckpointedL2BlockNumber(),
379
- this.getFinalizedL2BlockNumber(),
380
- ] as const);
381
-
382
- const beforeInitialblockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
383
-
384
- // Get the latest block header and checkpointed blocks for proven, finalised and checkpointed blocks
385
- const [latestBlockHeader, provenCheckpointedBlock, finalizedCheckpointedBlock, checkpointedBlock] =
386
- await Promise.all([
387
- latestBlockNumber > beforeInitialblockNumber ? this.getBlockHeader(latestBlockNumber) : undefined,
388
- provenBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(provenBlockNumber) : undefined,
389
- finalizedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(finalizedBlockNumber) : undefined,
390
- checkpointedBlockNumber > beforeInitialblockNumber
391
- ? this.getCheckpointedBlock(checkpointedBlockNumber)
392
- : undefined,
393
- ] as const);
394
-
395
- if (latestBlockNumber > beforeInitialblockNumber && !latestBlockHeader) {
396
- throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
397
- }
398
-
399
- // Checkpointed blocks must exist for proven, finalized and checkpointed tips if they are beyond the initial block number.
400
- if (checkpointedBlockNumber > beforeInitialblockNumber && !checkpointedBlock?.block.header) {
401
- throw new Error(
402
- `Failed to retrieve checkpointed block header for block ${checkpointedBlockNumber} (latest block is ${latestBlockNumber})`,
403
- );
404
- }
405
-
406
- if (provenBlockNumber > beforeInitialblockNumber && !provenCheckpointedBlock?.block.header) {
407
- throw new Error(
408
- `Failed to retrieve proven checkpointed for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`,
409
- );
410
- }
411
-
412
- if (finalizedBlockNumber > beforeInitialblockNumber && !finalizedCheckpointedBlock?.block.header) {
413
- throw new Error(
414
- `Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`,
415
- );
416
- }
417
-
418
- const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
419
- const provenBlockHeaderHash = (await provenCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
420
- const finalizedBlockHeaderHash =
421
- (await finalizedCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
422
- const checkpointedBlockHeaderHash = (await checkpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
423
-
424
- // Now attempt to retrieve checkpoints for proven, finalised and checkpointed blocks
425
- const [[provenBlockCheckpoint], [finalizedBlockCheckpoint], [checkpointedBlockCheckpoint]] = await Promise.all([
426
- provenCheckpointedBlock !== undefined
427
- ? await this.getCheckpoints(provenCheckpointedBlock?.checkpointNumber, 1)
428
- : [undefined],
429
- finalizedCheckpointedBlock !== undefined
430
- ? await this.getCheckpoints(finalizedCheckpointedBlock?.checkpointNumber, 1)
431
- : [undefined],
432
- checkpointedBlock !== undefined ? await this.getCheckpoints(checkpointedBlock?.checkpointNumber, 1) : [undefined],
433
- ]);
434
-
435
- const initialcheckpointId: CheckpointId = {
436
- number: CheckpointNumber.ZERO,
437
- hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
438
- };
439
-
440
- const makeCheckpointId = (checkpoint: PublishedCheckpoint | undefined) => {
441
- if (checkpoint === undefined) {
442
- return initialcheckpointId;
443
- }
444
- return {
445
- number: checkpoint.checkpoint.number,
446
- hash: checkpoint.checkpoint.hash().toString(),
447
- };
448
- };
449
-
450
- const l2Tips: L2Tips = {
451
- proposed: {
452
- number: latestBlockNumber,
453
- hash: latestBlockHeaderHash.toString(),
454
- },
455
- proven: {
456
- block: {
457
- number: provenBlockNumber,
458
- hash: provenBlockHeaderHash.toString(),
459
- },
460
- checkpoint: makeCheckpointId(provenBlockCheckpoint),
461
- },
462
- finalized: {
463
- block: {
464
- number: finalizedBlockNumber,
465
- hash: finalizedBlockHeaderHash.toString(),
466
- },
467
- checkpoint: makeCheckpointId(finalizedBlockCheckpoint),
468
- },
469
- checkpointed: {
470
- block: {
471
- number: checkpointedBlockNumber,
472
- hash: checkpointedBlockHeaderHash.toString(),
473
- },
474
- checkpoint: makeCheckpointId(checkpointedBlockCheckpoint),
475
- },
476
- };
477
-
478
- return l2Tips;
649
+ public getL2Tips(): Promise<L2Tips> {
650
+ return this.l2TipsCache.getL2Tips();
479
651
  }
480
652
 
481
653
  public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
482
- // TODO(pw/mbps): This still assumes 1 block per checkpoint
483
654
  const currentBlocks = await this.getL2Tips();
484
655
  const currentL2Block = currentBlocks.proposed.number;
485
656
  const currentProvenBlock = currentBlocks.proven.block.number;
@@ -487,12 +658,34 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
487
658
  if (targetL2BlockNumber >= currentL2Block) {
488
659
  throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
489
660
  }
490
- const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
491
- if (!targetL2Block) {
661
+ const checkpointedTip = await this.stores.blocks.getCheckpointedL2BlockNumber();
662
+ if (targetL2BlockNumber > checkpointedTip) {
663
+ throw new Error(`Target L2 block ${targetL2BlockNumber} is not checkpointed yet`);
664
+ }
665
+ const targetBlockData = await this.stores.blocks.getBlockData({ number: targetL2BlockNumber });
666
+ if (!targetBlockData) {
492
667
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
493
668
  }
494
- const targetL1BlockNumber = targetL2Block.l1.blockNumber;
495
- const targetCheckpointNumber = targetL2Block.checkpointNumber;
669
+ const targetCheckpointNumber = targetBlockData.checkpointNumber;
670
+
671
+ // Rollback operates at checkpoint granularity: the target block must be the last block of its checkpoint.
672
+ const checkpointData = await this.stores.blocks.getCheckpointData(targetCheckpointNumber);
673
+ if (!checkpointData) {
674
+ throw new Error(`Checkpoint ${targetCheckpointNumber} not found for block ${targetL2BlockNumber}`);
675
+ }
676
+ const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
677
+ if (targetL2BlockNumber !== lastBlockInCheckpoint) {
678
+ const previousCheckpointBoundary =
679
+ checkpointData.startBlock > 1 ? BlockNumber(checkpointData.startBlock - 1) : BlockNumber(0);
680
+ throw new Error(
681
+ `Target L2 block ${targetL2BlockNumber} is not at a checkpoint boundary. ` +
682
+ `Checkpoint ${targetCheckpointNumber} spans blocks ${checkpointData.startBlock} to ${lastBlockInCheckpoint}. ` +
683
+ `Use block ${lastBlockInCheckpoint} to roll back to this checkpoint, ` +
684
+ `or block ${previousCheckpointBoundary} to roll back to the previous one.`,
685
+ );
686
+ }
687
+
688
+ const targetL1BlockNumber = checkpointData.l1.blockNumber;
496
689
  const targetL1Block = await this.publicClient.getBlock({
497
690
  blockNumber: targetL1BlockNumber,
498
691
  includeTransactions: false,
@@ -506,18 +699,21 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
506
699
  );
507
700
  await this.updater.removeCheckpointsAfter(targetCheckpointNumber);
508
701
  this.log.info(`Rolling back L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
509
- await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
702
+ await this.stores.messages.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
510
703
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
511
- await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
512
- await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
704
+ await this.stores.blocks.setSynchedL1BlockNumber(targetL1BlockNumber);
705
+ await this.stores.messages.setMessageSyncState(
706
+ { l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash },
707
+ undefined,
708
+ );
513
709
  if (targetL2BlockNumber < currentProvenBlock) {
514
- this.log.info(`Clearing proven L2 block number`);
515
- await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
710
+ this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
711
+ await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
712
+ }
713
+ const currentFinalizedBlock = currentBlocks.finalized.block.number;
714
+ if (targetL2BlockNumber < currentFinalizedBlock) {
715
+ this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
716
+ await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
516
717
  }
517
- // TODO(palla/reorg): Set the finalized block when we add support for it.
518
- // if (targetL2BlockNumber < currentFinalizedBlock) {
519
- // this.log.info(`Clearing finalized L2 block number`);
520
- // await this.store.setFinalizedL2BlockNumber(0);
521
- // }
522
718
  }
523
719
  }