@aztec/archiver 3.0.0-canary.a9708bd → 3.0.0-devnet.2-patch.1

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 (127) hide show
  1. package/README.md +27 -6
  2. package/dest/archiver/archiver.d.ts +87 -64
  3. package/dest/archiver/archiver.d.ts.map +1 -1
  4. package/dest/archiver/archiver.js +463 -278
  5. package/dest/archiver/archiver_store.d.ts +46 -28
  6. package/dest/archiver/archiver_store.d.ts.map +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  8. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  9. package/dest/archiver/archiver_store_test_suite.js +316 -143
  10. package/dest/archiver/config.d.ts +6 -23
  11. package/dest/archiver/config.d.ts.map +1 -1
  12. package/dest/archiver/config.js +19 -12
  13. package/dest/archiver/errors.d.ts +1 -1
  14. package/dest/archiver/errors.d.ts.map +1 -1
  15. package/dest/archiver/index.d.ts +1 -1
  16. package/dest/archiver/instrumentation.d.ts +5 -3
  17. package/dest/archiver/instrumentation.d.ts.map +1 -1
  18. package/dest/archiver/instrumentation.js +14 -0
  19. package/dest/archiver/kv_archiver_store/block_store.d.ts +45 -9
  20. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  21. package/dest/archiver/kv_archiver_store/block_store.js +99 -12
  22. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +2 -2
  23. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  24. package/dest/archiver/kv_archiver_store/contract_class_store.js +1 -1
  25. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +2 -2
  26. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  27. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +30 -30
  28. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  29. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +26 -15
  30. package/dest/archiver/kv_archiver_store/log_store.d.ts +3 -10
  31. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  32. package/dest/archiver/kv_archiver_store/log_store.js +4 -26
  33. package/dest/archiver/kv_archiver_store/message_store.d.ts +6 -5
  34. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  35. package/dest/archiver/kv_archiver_store/message_store.js +15 -14
  36. package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
  37. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
  38. package/dest/archiver/l1/bin/retrieve-calldata.js +147 -0
  39. package/dest/archiver/l1/calldata_retriever.d.ts +98 -0
  40. package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
  41. package/dest/archiver/l1/calldata_retriever.js +403 -0
  42. package/dest/archiver/l1/data_retrieval.d.ts +87 -0
  43. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
  44. package/dest/archiver/{data_retrieval.js → l1/data_retrieval.js} +118 -154
  45. package/dest/archiver/l1/debug_tx.d.ts +19 -0
  46. package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
  47. package/dest/archiver/l1/debug_tx.js +73 -0
  48. package/dest/archiver/l1/spire_proposer.d.ts +70 -0
  49. package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
  50. package/dest/archiver/l1/spire_proposer.js +157 -0
  51. package/dest/archiver/l1/trace_tx.d.ts +97 -0
  52. package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
  53. package/dest/archiver/l1/trace_tx.js +91 -0
  54. package/dest/archiver/l1/types.d.ts +12 -0
  55. package/dest/archiver/l1/types.d.ts.map +1 -0
  56. package/dest/archiver/l1/types.js +3 -0
  57. package/dest/archiver/l1/validate_trace.d.ts +29 -0
  58. package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
  59. package/dest/archiver/l1/validate_trace.js +150 -0
  60. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  61. package/dest/archiver/structs/inbox_message.d.ts +4 -4
  62. package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
  63. package/dest/archiver/structs/inbox_message.js +6 -5
  64. package/dest/archiver/structs/published.d.ts +3 -2
  65. package/dest/archiver/structs/published.d.ts.map +1 -1
  66. package/dest/archiver/validation.d.ts +10 -4
  67. package/dest/archiver/validation.d.ts.map +1 -1
  68. package/dest/archiver/validation.js +66 -44
  69. package/dest/factory.d.ts +3 -11
  70. package/dest/factory.d.ts.map +1 -1
  71. package/dest/factory.js +5 -17
  72. package/dest/index.d.ts +2 -2
  73. package/dest/index.d.ts.map +1 -1
  74. package/dest/index.js +1 -1
  75. package/dest/rpc/index.d.ts +2 -2
  76. package/dest/test/index.d.ts +1 -1
  77. package/dest/test/mock_archiver.d.ts +16 -8
  78. package/dest/test/mock_archiver.d.ts.map +1 -1
  79. package/dest/test/mock_archiver.js +19 -14
  80. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  81. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  82. package/dest/test/mock_l1_to_l2_message_source.js +10 -9
  83. package/dest/test/mock_l2_block_source.d.ts +24 -20
  84. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  85. package/dest/test/mock_l2_block_source.js +79 -13
  86. package/dest/test/mock_structs.d.ts +3 -2
  87. package/dest/test/mock_structs.d.ts.map +1 -1
  88. package/dest/test/mock_structs.js +9 -8
  89. package/package.json +18 -17
  90. package/src/archiver/archiver.ts +610 -363
  91. package/src/archiver/archiver_store.ts +55 -28
  92. package/src/archiver/archiver_store_test_suite.ts +369 -143
  93. package/src/archiver/config.ts +26 -51
  94. package/src/archiver/instrumentation.ts +19 -2
  95. package/src/archiver/kv_archiver_store/block_store.ts +139 -21
  96. package/src/archiver/kv_archiver_store/contract_class_store.ts +1 -1
  97. package/src/archiver/kv_archiver_store/contract_instance_store.ts +1 -1
  98. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +48 -33
  99. package/src/archiver/kv_archiver_store/log_store.ts +4 -30
  100. package/src/archiver/kv_archiver_store/message_store.ts +21 -18
  101. package/src/archiver/l1/README.md +98 -0
  102. package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
  103. package/src/archiver/l1/calldata_retriever.ts +531 -0
  104. package/src/archiver/{data_retrieval.ts → l1/data_retrieval.ts} +198 -242
  105. package/src/archiver/l1/debug_tx.ts +99 -0
  106. package/src/archiver/l1/spire_proposer.ts +160 -0
  107. package/src/archiver/l1/trace_tx.ts +128 -0
  108. package/src/archiver/l1/types.ts +13 -0
  109. package/src/archiver/l1/validate_trace.ts +211 -0
  110. package/src/archiver/structs/inbox_message.ts +8 -8
  111. package/src/archiver/structs/published.ts +2 -1
  112. package/src/archiver/validation.ts +86 -32
  113. package/src/factory.ts +6 -26
  114. package/src/index.ts +1 -1
  115. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  116. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  117. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  118. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  119. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  120. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  121. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  122. package/src/test/mock_archiver.ts +22 -16
  123. package/src/test/mock_l1_to_l2_message_source.ts +10 -9
  124. package/src/test/mock_l2_block_source.ts +110 -27
  125. package/src/test/mock_structs.ts +10 -9
  126. package/dest/archiver/data_retrieval.d.ts +0 -78
  127. package/dest/archiver/data_retrieval.d.ts.map +0 -1
@@ -1,21 +1,20 @@
1
1
  import type { BlobSinkClientInterface } from '@aztec/blob-sink/client';
2
+ import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
2
3
  import { EpochCache } from '@aztec/epoch-cache';
3
- import {
4
- BlockTagTooOldError,
5
- InboxContract,
6
- type L1BlockId,
7
- RollupContract,
8
- type ViemPublicClient,
9
- createEthereumChain,
10
- } from '@aztec/ethereum';
4
+ import { createEthereumChain } from '@aztec/ethereum/chain';
5
+ import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
6
+ import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
7
+ import type { L1BlockId } from '@aztec/ethereum/l1-types';
8
+ import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
11
9
  import { maxBigint } from '@aztec/foundation/bigint';
10
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
12
11
  import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
13
- import { pick } from '@aztec/foundation/collection';
12
+ import { merge, pick } from '@aztec/foundation/collection';
13
+ import { Fr } from '@aztec/foundation/curves/bn254';
14
14
  import type { EthAddress } from '@aztec/foundation/eth-address';
15
- import { Fr } from '@aztec/foundation/fields';
16
15
  import { type Logger, createLogger } from '@aztec/foundation/log';
16
+ import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
17
17
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
18
- import { sleep } from '@aztec/foundation/sleep';
19
18
  import { count } from '@aztec/foundation/string';
20
19
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
21
20
  import type { CustomRange } from '@aztec/kv-store';
@@ -33,12 +32,13 @@ import type { FunctionSelector } from '@aztec/stdlib/abi';
33
32
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
34
33
  import {
35
34
  type ArchiverEmitter,
36
- type L2Block,
37
- type L2BlockId,
35
+ L2Block,
38
36
  type L2BlockSource,
39
37
  L2BlockSourceEvents,
40
38
  type L2Tips,
39
+ PublishedL2Block,
41
40
  } from '@aztec/stdlib/block';
41
+ import type { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
42
42
  import {
43
43
  type ContractClassPublic,
44
44
  type ContractDataSource,
@@ -60,11 +60,11 @@ import {
60
60
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
61
61
  import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
62
62
  import { ContractClassLog, type LogFilter, type PrivateLog, type PublicLog, TxScopedL2Log } from '@aztec/stdlib/logs';
63
- import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
63
+ import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
64
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
64
65
  import { type BlockHeader, type IndexedTxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
65
66
  import type { UInt64 } from '@aztec/stdlib/types';
66
67
  import {
67
- Attributes,
68
68
  type TelemetryClient,
69
69
  type Traceable,
70
70
  type Tracer,
@@ -74,21 +74,21 @@ import {
74
74
 
75
75
  import { EventEmitter } from 'events';
76
76
  import groupBy from 'lodash.groupby';
77
- import { type GetContractReturnType, createPublicClient, fallback, http } from 'viem';
77
+ import { type GetContractReturnType, type Hex, createPublicClient, fallback, http } from 'viem';
78
78
 
79
79
  import type { ArchiverDataStore, ArchiverL1SynchPoint } from './archiver_store.js';
80
80
  import type { ArchiverConfig } from './config.js';
81
+ import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
82
+ import { ArchiverInstrumentation } from './instrumentation.js';
81
83
  import {
82
- retrieveBlocksFromRollup,
84
+ retrieveCheckpointsFromRollup,
83
85
  retrieveL1ToL2Message,
84
86
  retrieveL1ToL2Messages,
85
- retrievedBlockToPublishedL2Block,
86
- } from './data_retrieval.js';
87
- import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
88
- import { ArchiverInstrumentation } from './instrumentation.js';
87
+ retrievedToPublishedCheckpoint,
88
+ } from './l1/data_retrieval.js';
89
+ import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
89
90
  import type { InboxMessage } from './structs/inbox_message.js';
90
- import type { PublishedL2Block } from './structs/published.js';
91
- import { type ValidateBlockResult, validateBlockAttestations } from './validation.js';
91
+ import { type ValidateBlockResult, validateCheckpointAttestations } from './validation.js';
92
92
 
93
93
  /**
94
94
  * Helper interface to combine all sources this archiver implementation provides.
@@ -102,16 +102,34 @@ export type ArchiverDeps = {
102
102
  dateProvider?: DateProvider;
103
103
  };
104
104
 
105
+ function mapArchiverConfig(config: Partial<ArchiverConfig>) {
106
+ return {
107
+ pollingIntervalMs: config.archiverPollingIntervalMS,
108
+ batchSize: config.archiverBatchSize,
109
+ skipValidateBlockAttestations: config.skipValidateBlockAttestations,
110
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
111
+ ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts,
112
+ };
113
+ }
114
+
115
+ type RollupStatus = {
116
+ provenCheckpointNumber: CheckpointNumber;
117
+ provenArchive: Hex;
118
+ pendingCheckpointNumber: CheckpointNumber;
119
+ pendingArchive: Hex;
120
+ validationResult: ValidateBlockResult | undefined;
121
+ lastRetrievedCheckpoint?: PublishedCheckpoint;
122
+ lastL1BlockWithCheckpoint?: bigint;
123
+ };
124
+
105
125
  /**
106
- * Pulls L2 blocks in a non-blocking manner and provides interface for their retrieval.
126
+ * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
107
127
  * Responsible for handling robust L1 polling so that other components do not need to
108
128
  * concern themselves with it.
109
129
  */
110
130
  export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implements ArchiveSource, Traceable {
111
- /**
112
- * A promise in which we will be continually fetching new L2 blocks.
113
- */
114
- private runningPromise?: RunningPromise;
131
+ /** A loop in which we will be continually fetching new checkpoints. */
132
+ private runningPromise: RunningPromise;
115
133
 
116
134
  private rollup: RollupContract;
117
135
  private inbox: InboxContract;
@@ -120,14 +138,15 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
120
138
 
121
139
  private l1BlockNumber: bigint | undefined;
122
140
  private l1Timestamp: bigint | undefined;
123
- private pendingChainValidationStatus: ValidateBlockResult = { valid: true };
124
141
  private initialSyncComplete: boolean = false;
142
+ private initialSyncPromise: PromiseWithResolvers<void>;
125
143
 
126
144
  public readonly tracer: Tracer;
127
145
 
128
146
  /**
129
147
  * Creates a new instance of the Archiver.
130
148
  * @param publicClient - A client for interacting with the Ethereum node.
149
+ * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
131
150
  * @param rollupAddress - Ethereum address of the rollup contract.
132
151
  * @param inboxAddress - Ethereum address of the inbox contract.
133
152
  * @param registryAddress - Ethereum address of the registry contract.
@@ -137,13 +156,24 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
137
156
  */
138
157
  constructor(
139
158
  private readonly publicClient: ViemPublicClient,
140
- private readonly l1Addresses: { rollupAddress: EthAddress; inboxAddress: EthAddress; registryAddress: EthAddress },
159
+ private readonly debugClient: ViemPublicDebugClient,
160
+ private readonly l1Addresses: Pick<
161
+ L1ContractAddresses,
162
+ 'rollupAddress' | 'inboxAddress' | 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
163
+ > & { slashingProposerAddress: EthAddress },
141
164
  readonly dataStore: ArchiverDataStore,
142
- private readonly config: { pollingIntervalMs: number; batchSize: number },
165
+ private config: {
166
+ pollingIntervalMs: number;
167
+ batchSize: number;
168
+ skipValidateBlockAttestations?: boolean;
169
+ maxAllowedEthClientDriftSeconds: number;
170
+ ethereumAllowNoDebugHosts?: boolean;
171
+ },
143
172
  private readonly blobSinkClient: BlobSinkClientInterface,
144
173
  private readonly epochCache: EpochCache,
174
+ private readonly dateProvider: DateProvider,
145
175
  private readonly instrumentation: ArchiverInstrumentation,
146
- private readonly l1constants: L1RollupConstants & { l1StartBlockHash: Buffer32 },
176
+ private readonly l1constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
147
177
  private readonly log: Logger = createLogger('archiver'),
148
178
  ) {
149
179
  super();
@@ -153,6 +183,16 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
153
183
 
154
184
  this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
155
185
  this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
186
+ this.initialSyncPromise = promiseWithResolvers();
187
+
188
+ // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
189
+ // are done as fast as possible. This then gets updated once the initial sync completes.
190
+ this.runningPromise = new RunningPromise(
191
+ () => this.sync(),
192
+ this.log,
193
+ this.config.pollingIntervalMs / 10,
194
+ makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError),
195
+ );
156
196
  }
157
197
 
158
198
  /**
@@ -175,13 +215,24 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
175
215
  pollingInterval: config.viemPollingIntervalMS,
176
216
  });
177
217
 
218
+ // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
219
+ const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
220
+ const debugClient = createPublicClient({
221
+ chain: chain.chainInfo,
222
+ transport: fallback(debugRpcUrls.map(url => http(url))),
223
+ pollingInterval: config.viemPollingIntervalMS,
224
+ }) as ViemPublicDebugClient;
225
+
178
226
  const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
179
227
 
180
- const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs] = await Promise.all([
181
- rollup.getL1StartBlock(),
182
- rollup.getL1GenesisTime(),
183
- rollup.getProofSubmissionEpochs(),
184
- ] as const);
228
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] =
229
+ await Promise.all([
230
+ rollup.getL1StartBlock(),
231
+ rollup.getL1GenesisTime(),
232
+ rollup.getProofSubmissionEpochs(),
233
+ rollup.getGenesisArchiveTreeRoot(),
234
+ rollup.getSlashingProposerAddress(),
235
+ ] as const);
185
236
 
186
237
  const l1StartBlockHash = await publicClient
187
238
  .getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
@@ -197,23 +248,31 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
197
248
  slotDuration,
198
249
  ethereumSlotDuration,
199
250
  proofSubmissionEpochs: Number(proofSubmissionEpochs),
251
+ genesisArchiveRoot: Fr.fromHexString(genesisArchiveRoot),
200
252
  };
201
253
 
202
- const opts = {
203
- pollingIntervalMs: config.archiverPollingIntervalMS ?? 10_000,
204
- batchSize: config.archiverBatchSize ?? 100,
205
- };
254
+ const opts = merge(
255
+ {
256
+ pollingIntervalMs: 10_000,
257
+ batchSize: 100,
258
+ maxAllowedEthClientDriftSeconds: 300,
259
+ ethereumAllowNoDebugHosts: false,
260
+ },
261
+ mapArchiverConfig(config),
262
+ );
206
263
 
207
264
  const epochCache = deps.epochCache ?? (await EpochCache.create(config.l1Contracts.rollupAddress, config, deps));
208
265
  const telemetry = deps.telemetry ?? getTelemetryClient();
209
266
 
210
267
  const archiver = new Archiver(
211
268
  publicClient,
212
- config.l1Contracts,
269
+ debugClient,
270
+ { ...config.l1Contracts, slashingProposerAddress },
213
271
  archiverStore,
214
272
  opts,
215
273
  deps.blobSinkClient,
216
274
  epochCache,
275
+ deps.dateProvider ?? new DateProvider(),
217
276
  await ArchiverInstrumentation.new(telemetry, () => archiverStore.estimateSize()),
218
277
  l1Constants,
219
278
  );
@@ -221,67 +280,68 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
221
280
  return archiver;
222
281
  }
223
282
 
283
+ /** Updates archiver config */
284
+ public updateConfig(newConfig: Partial<ArchiverConfig>) {
285
+ this.config = merge(this.config, mapArchiverConfig(newConfig));
286
+ }
287
+
224
288
  /**
225
289
  * Starts sync process.
226
290
  * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
227
291
  */
228
292
  public async start(blockUntilSynced: boolean): Promise<void> {
229
- if (this.runningPromise) {
293
+ if (this.runningPromise.isRunning()) {
230
294
  throw new Error('Archiver is already running');
231
295
  }
232
296
 
233
297
  await this.blobSinkClient.testSources();
234
-
235
- if (blockUntilSynced) {
236
- while (!(await this.syncSafe(true))) {
237
- this.log.info(`Retrying initial archiver sync in ${this.config.pollingIntervalMs}ms`);
238
- await sleep(this.config.pollingIntervalMs);
239
- }
240
- }
241
-
242
- this.runningPromise = new RunningPromise(
243
- () => this.sync(false),
244
- this.log,
245
- this.config.pollingIntervalMs,
246
- makeLoggingErrorHandler(
247
- this.log,
248
- // Ignored errors will not log to the console
249
- // We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
250
- NoBlobBodiesFoundError,
251
- ),
298
+ await this.testEthereumNodeSynced();
299
+ await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
300
+
301
+ // Log initial state for the archiver
302
+ const { l1StartBlock } = this.l1constants;
303
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
304
+ const currentL2Block = await this.getBlockNumber();
305
+ this.log.info(
306
+ `Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`,
307
+ { blocksSynchedTo, messagesSynchedTo, currentL2Block },
252
308
  );
253
309
 
310
+ // Start sync loop, and return the wait for initial sync if we are asked to block until synced
254
311
  this.runningPromise.start();
312
+ if (blockUntilSynced) {
313
+ return this.waitForInitialSync();
314
+ }
255
315
  }
256
316
 
257
317
  public syncImmediate() {
258
- if (!this.runningPromise) {
259
- throw new Error('Archiver is not running');
260
- }
261
318
  return this.runningPromise.trigger();
262
319
  }
263
320
 
264
- private async syncSafe(initialRun: boolean) {
265
- try {
266
- await this.sync(initialRun);
267
- return true;
268
- } catch (error) {
269
- if (error instanceof NoBlobBodiesFoundError) {
270
- this.log.error(`Error syncing archiver: ${error.message}`);
271
- } else if (error instanceof BlockTagTooOldError) {
272
- this.log.warn(`Re-running archiver sync: ${error.message}`);
273
- } else {
274
- this.log.error('Error during archiver sync', error);
275
- }
276
- return false;
321
+ public waitForInitialSync() {
322
+ return this.initialSyncPromise.promise;
323
+ }
324
+
325
+ /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */
326
+ private async testEthereumNodeSynced() {
327
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
328
+ if (maxAllowedDelay === 0) {
329
+ return;
330
+ }
331
+ const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({ includeTransactions: false });
332
+ const currentTime = BigInt(this.dateProvider.nowInSeconds());
333
+ if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
334
+ throw new Error(
335
+ `Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`,
336
+ );
277
337
  }
278
338
  }
279
339
 
280
340
  /**
281
341
  * Fetches logs from L1 contracts and processes them.
282
342
  */
283
- @trackSpan('Archiver.sync', initialRun => ({ [Attributes.INITIAL_SYNC]: initialRun }))
284
- private async sync(initialRun: boolean) {
343
+ @trackSpan('Archiver.sync')
344
+ private async sync() {
285
345
  /**
286
346
  * We keep track of three "pointers" to L1 blocks:
287
347
  * 1. the last L1 block that published an L2 block
@@ -291,8 +351,6 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
291
351
  * We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
292
352
  * We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
293
353
  * The archiver will stay back, until there's data on L1 that will move the pointers forward.
294
- *
295
- * This code does not handle reorgs.
296
354
  */
297
355
  const { l1StartBlock, l1StartBlockHash } = this.l1constants;
298
356
  const {
@@ -304,13 +362,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
304
362
  const currentL1BlockNumber = currentL1Block.number;
305
363
  const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
306
364
 
307
- if (initialRun) {
308
- this.log.info(
309
- `Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo}` +
310
- ` to current L1 block ${currentL1BlockNumber} with hash ${currentL1BlockHash.toString()}`,
311
- { blocksSynchedTo, messagesSynchedTo },
312
- );
313
- }
365
+ this.log.trace(`Starting new archiver sync iteration`, {
366
+ blocksSynchedTo,
367
+ messagesSynchedTo,
368
+ currentL1BlockNumber,
369
+ currentL1BlockHash,
370
+ });
314
371
 
315
372
  // ********** Ensuring Consistency of data pulled from L1 **********
316
373
 
@@ -340,37 +397,45 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
340
397
  ? (await this.publicClient.getBlock({ blockNumber: currentL1BlockNumber })).timestamp
341
398
  : this.l1Timestamp;
342
399
 
343
- // ********** Events that are processed per L2 block **********
400
+ // Warn if the latest L1 block timestamp is too old
401
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
402
+ const now = this.dateProvider.nowInSeconds();
403
+ if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
404
+ this.log.warn(
405
+ `Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`,
406
+ { currentL1BlockNumber, currentL1Timestamp, now, maxAllowedDelay },
407
+ );
408
+ }
409
+
410
+ // ********** Events that are processed per checkpoint **********
344
411
  if (currentL1BlockNumber > blocksSynchedTo) {
345
- // First we retrieve new L2 blocks
346
- const rollupStatus = await this.handleL2blocks(blocksSynchedTo, currentL1BlockNumber);
412
+ // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
413
+ // pending chain validation status, proven checkpoint number, and synched L1 block number.
414
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
347
415
  // Then we prune the current epoch if it'd reorg on next submission.
348
- // Note that we don't do this before retrieving L2 blocks because we may need to retrieve
349
- // blocks from more than 2 epochs ago, so we want to make sure we have the latest view of
416
+ // Note that we don't do this before retrieving checkpoints because we may need to retrieve
417
+ // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
350
418
  // the chain locally before we start unwinding stuff. This can be optimized by figuring out
351
- // up to which point we're pruning, and then requesting L2 blocks up to that point only.
419
+ // up to which point we're pruning, and then requesting checkpoints up to that point only.
352
420
  const { rollupCanPrune } = await this.handleEpochPrune(
353
- rollupStatus.provenBlockNumber,
421
+ rollupStatus.provenCheckpointNumber,
354
422
  currentL1BlockNumber,
355
423
  currentL1Timestamp,
356
424
  );
357
425
 
358
- // Update the pending chain validation status with the last block validation result.
359
- // Again, we only update if validation status changed, so in a sequence of invalid blocks
360
- // we keep track of the first invalid block so we can invalidate that one if needed.
361
- if (
362
- rollupStatus.validationResult &&
363
- rollupStatus.validationResult?.valid !== this.pendingChainValidationStatus.valid
364
- ) {
365
- this.pendingChainValidationStatus = rollupStatus.validationResult;
426
+ // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
427
+ // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
428
+ // we get a valid checkpoint to advance the syncpoint.
429
+ if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
430
+ await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
366
431
  }
367
432
 
368
- // And lastly we check if we are missing any L2 blocks behind us due to a possible L1 reorg.
433
+ // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
369
434
  // We only do this if rollup cant prune on the next submission. Otherwise we will end up
370
- // re-syncing the blocks we have just unwound above. We also dont do this if the last block is invalid,
435
+ // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
371
436
  // since the archiver will rightfully refuse to sync up to it.
372
- if (!rollupCanPrune && this.pendingChainValidationStatus.valid) {
373
- await this.checkForNewBlocksBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
437
+ if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
438
+ await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
374
439
  }
375
440
 
376
441
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
@@ -381,14 +446,18 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
381
446
  // but the corresponding blocks have not been processed (see #12631).
382
447
  this.l1Timestamp = currentL1Timestamp;
383
448
  this.l1BlockNumber = currentL1BlockNumber;
384
- this.initialSyncComplete = true;
385
449
 
386
- if (initialRun) {
387
- this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete.`, {
450
+ // We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
451
+ // so if the initial sync took too long, we still go for another iteration.
452
+ if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= (await this.publicClient.getBlockNumber())) {
453
+ this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
388
454
  l1BlockNumber: currentL1BlockNumber,
389
455
  syncPoint: await this.store.getSynchPoint(),
390
456
  ...(await this.getL2Tips()),
391
457
  });
458
+ this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
459
+ this.initialSyncComplete = true;
460
+ this.initialSyncPromise.resolve();
392
461
  }
393
462
  }
394
463
 
@@ -406,43 +475,47 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
406
475
  return result;
407
476
  }
408
477
 
409
- /** Checks if there'd be a reorg for the next block submission and start pruning now. */
410
- private async handleEpochPrune(provenBlockNumber: number, currentL1BlockNumber: bigint, currentL1Timestamp: bigint) {
478
+ /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */
479
+ private async handleEpochPrune(
480
+ provenCheckpointNumber: CheckpointNumber,
481
+ currentL1BlockNumber: bigint,
482
+ currentL1Timestamp: bigint,
483
+ ) {
411
484
  const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
412
- const localPendingBlockNumber = await this.getBlockNumber();
413
- const canPrune = localPendingBlockNumber > provenBlockNumber && rollupCanPrune;
485
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
486
+ const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
414
487
 
415
488
  if (canPrune) {
416
489
  const timer = new Timer();
417
- const pruneFrom = provenBlockNumber + 1;
490
+ const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
418
491
 
419
- const header = await this.getBlockHeader(Number(pruneFrom));
492
+ const header = await this.getCheckpointHeader(pruneFrom);
420
493
  if (header === undefined) {
421
- throw new Error(`Missing block header ${pruneFrom}`);
494
+ throw new Error(`Missing checkpoint header ${pruneFrom}`);
422
495
  }
423
496
 
424
- const pruneFromSlotNumber = header.globalVariables.slotNumber.toBigInt();
425
- const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
497
+ const pruneFromSlotNumber = header.slotNumber;
498
+ const pruneFromEpochNumber: EpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
426
499
 
427
- const blocksToUnwind = localPendingBlockNumber - provenBlockNumber;
500
+ const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
428
501
 
429
- const blocks = await this.getBlocks(Number(provenBlockNumber) + 1, Number(blocksToUnwind));
502
+ const checkpoints = await this.getCheckpoints(pruneFrom, checkpointsToUnwind);
430
503
 
431
504
  // Emit an event for listening services to react to the chain prune
432
505
  this.emit(L2BlockSourceEvents.L2PruneDetected, {
433
506
  type: L2BlockSourceEvents.L2PruneDetected,
434
507
  epochNumber: pruneFromEpochNumber,
435
- blocks,
508
+ blocks: checkpoints.flatMap(c => L2Block.fromCheckpoint(c)),
436
509
  });
437
510
 
438
511
  this.log.debug(
439
- `L2 prune from ${provenBlockNumber + 1} to ${localPendingBlockNumber} will occur on next block submission.`,
512
+ `L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`,
440
513
  );
441
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
514
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
442
515
  this.log.warn(
443
- `Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` +
444
- `to ${provenBlockNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
445
- `Updated L2 latest block is ${await this.getBlockNumber()}.`,
516
+ `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
517
+ `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
518
+ `Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
446
519
  );
447
520
  this.instrumentation.processPrune(timer.ms());
448
521
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
@@ -489,7 +562,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
489
562
  remoteMessagesState.totalMessagesInserted === localMessagesInserted &&
490
563
  remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
491
564
  ) {
492
- this.log.debug(
565
+ this.log.trace(
493
566
  `No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`,
494
567
  );
495
568
  return;
@@ -543,7 +616,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
543
616
  // Log stats for messages retrieved (if any).
544
617
  if (messageCount > 0) {
545
618
  this.log.info(
546
- `Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for L2 block ${lastMessage?.l2BlockNumber}`,
619
+ `Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`,
547
620
  { lastMessage, messageCount },
548
621
  );
549
622
  }
@@ -621,202 +694,238 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
621
694
  return Buffer32.fromString(block.hash);
622
695
  }
623
696
 
624
- private async handleL2blocks(blocksSynchedTo: bigint, currentL1BlockNumber: bigint) {
625
- const localPendingBlockNumber = await this.getBlockNumber();
626
- const [provenBlockNumber, provenArchive, pendingBlockNumber, pendingArchive, archiveForLocalPendingBlockNumber] =
627
- await this.rollup.status(BigInt(localPendingBlockNumber), { blockNumber: currentL1BlockNumber });
697
+ private async handleCheckpoints(blocksSynchedTo: bigint, currentL1BlockNumber: bigint): Promise<RollupStatus> {
698
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
699
+ const initialValidationResult: ValidateBlockResult | undefined = await this.store.getPendingChainValidationStatus();
700
+ const [
701
+ rollupProvenCheckpointNumber,
702
+ provenArchive,
703
+ rollupPendingCheckpointNumber,
704
+ pendingArchive,
705
+ archiveForLocalPendingCheckpointNumber,
706
+ ] = await this.rollup.status(localPendingCheckpointNumber, { blockNumber: currentL1BlockNumber });
707
+ const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
708
+ const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
628
709
  const rollupStatus = {
629
- provenBlockNumber: Number(provenBlockNumber),
710
+ provenCheckpointNumber,
630
711
  provenArchive,
631
- pendingBlockNumber: Number(pendingBlockNumber),
712
+ pendingCheckpointNumber,
632
713
  pendingArchive,
633
- validationResult: undefined as ValidateBlockResult | undefined,
714
+ validationResult: initialValidationResult,
634
715
  };
635
716
  this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
636
- localPendingBlockNumber,
717
+ localPendingCheckpointNumber,
637
718
  blocksSynchedTo,
638
719
  currentL1BlockNumber,
639
- archiveForLocalPendingBlockNumber,
720
+ archiveForLocalPendingCheckpointNumber,
640
721
  ...rollupStatus,
641
722
  });
642
723
 
643
- const updateProvenBlock = async () => {
644
- // Annoying edge case: if proven block is moved back to 0 due to a reorg at the beginning of the chain,
645
- // we need to set it to zero. This is an edge case because we dont have a block zero (initial block is one),
646
- // so localBlockForDestinationProvenBlockNumber would not be found below.
647
- if (provenBlockNumber === 0n) {
648
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
649
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
650
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
651
- this.log.info(`Rolled back proven chain to block ${provenBlockNumber}`, { provenBlockNumber });
724
+ const updateProvenCheckpoint = async () => {
725
+ // Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
726
+ // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
727
+ // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
728
+ if (provenCheckpointNumber === 0) {
729
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
730
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
731
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
732
+ this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
652
733
  }
653
734
  }
654
735
 
655
- const localBlockForDestinationProvenBlockNumber = await this.getBlock(Number(provenBlockNumber));
736
+ const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
656
737
 
657
- // Sanity check. I've hit what seems to be a state where the proven block is set to a value greater than the latest
658
- // synched block when requesting L2Tips from the archiver. This is the only place where the proven block is set.
659
- const synched = await this.store.getSynchedL2BlockNumber();
660
- if (localBlockForDestinationProvenBlockNumber && synched < localBlockForDestinationProvenBlockNumber?.number) {
738
+ // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
739
+ // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
740
+ const synched = await this.getSynchedCheckpointNumber();
741
+ if (
742
+ localCheckpointForDestinationProvenCheckpointNumber &&
743
+ synched < localCheckpointForDestinationProvenCheckpointNumber.number
744
+ ) {
661
745
  this.log.error(
662
- `Hit local block greater than last synched block: ${localBlockForDestinationProvenBlockNumber.number} > ${synched}`,
746
+ `Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`,
663
747
  );
664
748
  }
665
749
 
666
750
  this.log.trace(
667
- `Local block for remote proven block ${provenBlockNumber} is ${
668
- localBlockForDestinationProvenBlockNumber?.archive.root.toString() ?? 'undefined'
751
+ `Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${
752
+ localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'
669
753
  }`,
670
754
  );
671
755
 
756
+ const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
672
757
  if (
673
- localBlockForDestinationProvenBlockNumber &&
674
- provenArchive === localBlockForDestinationProvenBlockNumber.archive.root.toString()
758
+ localCheckpointForDestinationProvenCheckpointNumber &&
759
+ provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()
675
760
  ) {
676
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
677
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
678
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
679
- this.log.info(`Updated proven chain to block ${provenBlockNumber}`, {
680
- provenBlockNumber,
761
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
762
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
763
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
764
+ this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
765
+ provenCheckpointNumber,
681
766
  });
682
- const provenSlotNumber =
683
- localBlockForDestinationProvenBlockNumber.header.globalVariables.slotNumber.toBigInt();
684
- const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
767
+ const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
768
+ const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
769
+
685
770
  this.emit(L2BlockSourceEvents.L2BlockProven, {
686
771
  type: L2BlockSourceEvents.L2BlockProven,
687
- blockNumber: provenBlockNumber,
772
+ blockNumber: lastProvenBlockNumber,
688
773
  slotNumber: provenSlotNumber,
689
774
  epochNumber: provenEpochNumber,
690
775
  });
691
776
  } else {
692
- this.log.trace(`Proven block ${provenBlockNumber} already stored.`);
777
+ this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
693
778
  }
694
779
  }
695
- this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber));
780
+ this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
696
781
  };
697
782
 
698
- // This is an edge case that we only hit if there are no proposed blocks.
699
- // If we have 0 blocks locally and there are no blocks onchain there is nothing to do.
700
- const noBlocks = localPendingBlockNumber === 0 && pendingBlockNumber === 0n;
701
- if (noBlocks) {
783
+ // This is an edge case that we only hit if there are no proposed checkpoints.
784
+ // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
785
+ const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
786
+ if (noCheckpoints) {
702
787
  await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
703
788
  this.log.debug(
704
- `No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no blocks on chain`,
789
+ `No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`,
705
790
  );
706
791
  return rollupStatus;
707
792
  }
708
793
 
709
- await updateProvenBlock();
794
+ await updateProvenCheckpoint();
710
795
 
711
796
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
712
- // are any state that could be impacted by it. If we have no blocks, there is no impact.
713
- if (localPendingBlockNumber > 0) {
714
- const localPendingBlock = await this.getBlock(localPendingBlockNumber);
715
- if (localPendingBlock === undefined) {
716
- throw new Error(`Missing block ${localPendingBlockNumber}`);
797
+ // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
798
+ if (localPendingCheckpointNumber > 0) {
799
+ const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
800
+ if (localPendingCheckpoint === undefined) {
801
+ throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
717
802
  }
718
803
 
719
- const localPendingArchiveRoot = localPendingBlock.archive.root.toString();
720
- const noBlockSinceLast = localPendingBlock && pendingArchive === localPendingArchiveRoot;
721
- if (noBlockSinceLast) {
804
+ const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
805
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
806
+ if (noCheckpointSinceLast) {
722
807
  // We believe the following line causes a problem when we encounter L1 re-orgs.
723
808
  // Basically, by setting the synched L1 block number here, we are saying that we have
724
- // processed all blocks up to the current L1 block number and we will not attempt to retrieve logs from
809
+ // processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
725
810
  // this block again (or any blocks before).
726
- // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing blocks
811
+ // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
727
812
  // We must only set this block number based on actually retrieved logs.
728
813
  // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
729
814
  // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
730
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
815
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
731
816
  return rollupStatus;
732
817
  }
733
818
 
734
- const localPendingBlockInChain = archiveForLocalPendingBlockNumber === localPendingArchiveRoot;
735
- if (!localPendingBlockInChain) {
736
- // If our local pending block tip is not in the chain on L1 a "prune" must have happened
819
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
820
+ if (!localPendingCheckpointInChain) {
821
+ // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
737
822
  // or the L1 have reorged.
738
823
  // In any case, we have to figure out how far into the past the action will take us.
739
- // For simplicity here, we will simply rewind until we end in a block that is also on the chain on L1.
824
+ // For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
740
825
  this.log.debug(
741
- `L2 prune has been detected due to local pending block ${localPendingBlockNumber} not in chain`,
742
- { localPendingBlockNumber, localPendingArchiveRoot, archiveForLocalPendingBlockNumber },
826
+ `L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`,
827
+ { localPendingCheckpointNumber, localPendingArchiveRoot, archiveForLocalPendingCheckpointNumber },
743
828
  );
744
829
 
745
- let tipAfterUnwind = localPendingBlockNumber;
830
+ let tipAfterUnwind = localPendingCheckpointNumber;
746
831
  while (true) {
747
- const candidateBlock = await this.getBlock(Number(tipAfterUnwind));
748
- if (candidateBlock === undefined) {
832
+ const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
833
+ if (candidateCheckpoint === undefined) {
749
834
  break;
750
835
  }
751
836
 
752
- const archiveAtContract = await this.rollup.archiveAt(BigInt(candidateBlock.number));
753
-
754
- if (archiveAtContract === candidateBlock.archive.root.toString()) {
837
+ const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.number);
838
+ this.log.trace(
839
+ `Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`,
840
+ {
841
+ archiveAtContract,
842
+ archiveLocal: candidateCheckpoint.archive.root.toString(),
843
+ },
844
+ );
845
+ if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
755
846
  break;
756
847
  }
757
848
  tipAfterUnwind--;
758
849
  }
759
850
 
760
- const blocksToUnwind = localPendingBlockNumber - tipAfterUnwind;
761
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
851
+ const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
852
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
762
853
 
763
854
  this.log.warn(
764
- `Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` +
765
- `due to mismatched block hashes at L1 block ${currentL1BlockNumber}. ` +
766
- `Updated L2 latest block is ${await this.getBlockNumber()}.`,
855
+ `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
856
+ `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` +
857
+ `Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
767
858
  );
768
859
  }
769
860
  }
770
861
 
771
- // Retrieve L2 blocks in batches. Each batch is estimated to accommodate up to L2 'blockBatchSize' blocks,
862
+ // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
772
863
  // computed using the L2 block time vs the L1 block time.
773
864
  let searchStartBlock: bigint = blocksSynchedTo;
774
865
  let searchEndBlock: bigint = blocksSynchedTo;
775
- let lastRetrievedBlock: PublishedL2Block | undefined;
866
+ let lastRetrievedCheckpoint: PublishedCheckpoint | undefined;
867
+ let lastL1BlockWithCheckpoint: bigint | undefined = undefined;
776
868
 
777
869
  do {
778
870
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
779
871
 
780
- this.log.trace(`Retrieving L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
872
+ this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
781
873
 
782
874
  // TODO(md): Retrieve from blob sink then from consensus client, then from peers
783
- const retrievedBlocks = await retrieveBlocksFromRollup(
875
+ const retrievedCheckpoints = await retrieveCheckpointsFromRollup(
784
876
  this.rollup.getContract() as GetContractReturnType<typeof RollupAbi, ViemPublicClient>,
785
877
  this.publicClient,
878
+ this.debugClient,
786
879
  this.blobSinkClient,
787
880
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
788
881
  searchEndBlock,
882
+ this.l1Addresses,
883
+ this.instrumentation,
789
884
  this.log,
790
885
  );
791
886
 
792
- if (retrievedBlocks.length === 0) {
887
+ if (retrievedCheckpoints.length === 0) {
793
888
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
794
889
  // See further details in earlier comments.
795
- this.log.trace(`Retrieved no new L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
890
+ this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
796
891
  continue;
797
892
  }
798
893
 
799
- const lastProcessedL1BlockNumber = retrievedBlocks[retrievedBlocks.length - 1].l1.blockNumber;
800
894
  this.log.debug(
801
- `Retrieved ${retrievedBlocks.length} new L2 blocks between L1 blocks ${searchStartBlock} and ${searchEndBlock} with last processed L1 block ${lastProcessedL1BlockNumber}.`,
895
+ `Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
896
+ {
897
+ lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
898
+ searchStartBlock,
899
+ searchEndBlock,
900
+ },
802
901
  );
803
902
 
804
- const publishedBlocks = retrievedBlocks.map(b => retrievedBlockToPublishedL2Block(b));
805
- const validBlocks: PublishedL2Block[] = [];
806
-
807
- for (const block of publishedBlocks) {
808
- const validationResult = await validateBlockAttestations(block, this.epochCache, this.l1constants, this.log);
809
-
810
- // Only update the validation result if it has changed, so we can keep track of the first invalid block
811
- // in case there is a sequence of more than one invalid block, as we need to invalidate the first one.
812
- if (rollupStatus.validationResult?.valid !== validationResult.valid) {
903
+ const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map(b => retrievedToPublishedCheckpoint(b)));
904
+ const validCheckpoints: PublishedCheckpoint[] = [];
905
+
906
+ for (const published of publishedCheckpoints) {
907
+ const validationResult = this.config.skipValidateBlockAttestations
908
+ ? { valid: true as const }
909
+ : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
910
+
911
+ // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
912
+ // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
913
+ // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
914
+ // we need to update the validation result, since we need to be able to invalidate the new one.
915
+ // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
916
+ if (
917
+ rollupStatus.validationResult?.valid !== validationResult.valid ||
918
+ (!rollupStatus.validationResult.valid &&
919
+ !validationResult.valid &&
920
+ rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber)
921
+ ) {
813
922
  rollupStatus.validationResult = validationResult;
814
923
  }
815
924
 
816
925
  if (!validationResult.valid) {
817
- this.log.warn(`Skipping block ${block.block.number} due to invalid attestations`, {
818
- blockHash: block.block.hash(),
819
- l1BlockNumber: block.l1.blockNumber,
926
+ this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
927
+ checkpointHash: published.checkpoint.hash(),
928
+ l1BlockNumber: published.l1.blockNumber,
820
929
  ...pick(validationResult, 'reason'),
821
930
  });
822
931
 
@@ -826,31 +935,56 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
826
935
  validationResult,
827
936
  });
828
937
 
829
- // We keep consuming blocks if we find an invalid one, since we do not listen for BlockInvalidated events
830
- // We just pretend the invalid ones are not there and keep consuming the next blocks
938
+ // We keep consuming checkpoints if we find an invalid one, since we do not listen for CheckpointInvalidated events
939
+ // We just pretend the invalid ones are not there and keep consuming the next checkpoints
940
+ // Note that this breaks if the committee ever attests to a descendant of an invalid checkpoint
831
941
  continue;
832
942
  }
833
943
 
834
- validBlocks.push(block);
835
- this.log.debug(`Ingesting new L2 block ${block.block.number} with ${block.block.body.txEffects.length} txs`, {
836
- blockHash: block.block.hash(),
837
- l1BlockNumber: block.l1.blockNumber,
838
- ...block.block.header.globalVariables.toInspect(),
839
- ...block.block.getStats(),
840
- });
944
+ // Check the inHash of the checkpoint against the l1->l2 messages.
945
+ // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
946
+ // checkpoints we just retrieved.
947
+ const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
948
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
949
+ const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
950
+ if (!computedInHash.equals(publishedInHash)) {
951
+ this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
952
+ checkpointHash: published.checkpoint.hash(),
953
+ l1BlockNumber: published.l1.blockNumber,
954
+ computedInHash,
955
+ publishedInHash,
956
+ });
957
+ // Throwing an error since this is most likely caused by a bug.
958
+ throw new Error(
959
+ `Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`,
960
+ );
961
+ }
962
+
963
+ validCheckpoints.push(published);
964
+ this.log.debug(
965
+ `Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`,
966
+ {
967
+ checkpointHash: published.checkpoint.hash(),
968
+ l1BlockNumber: published.l1.blockNumber,
969
+ ...published.checkpoint.header.toInspect(),
970
+ blocks: published.checkpoint.blocks.map(b => b.getStats()),
971
+ },
972
+ );
841
973
  }
842
974
 
843
975
  try {
844
- const [processDuration] = await elapsed(() => this.store.addBlocks(validBlocks));
976
+ const updatedValidationResult =
977
+ rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
978
+ const [processDuration] = await elapsed(() => this.addCheckpoints(validCheckpoints, updatedValidationResult));
845
979
  this.instrumentation.processNewBlocks(
846
- processDuration / validBlocks.length,
847
- validBlocks.map(b => b.block),
980
+ processDuration / validCheckpoints.length,
981
+ validCheckpoints.flatMap(c => c.checkpoint.blocks),
848
982
  );
849
983
  } catch (err) {
850
984
  if (err instanceof InitialBlockNumberNotSequentialError) {
851
985
  const { previousBlockNumber, newBlockNumber } = err;
852
986
  const previousBlock = previousBlockNumber
853
- ? await this.store.getPublishedBlock(previousBlockNumber)
987
+ ? await this.store.getPublishedBlock(BlockNumber(previousBlockNumber))
854
988
  : undefined;
855
989
  const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
856
990
  await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
@@ -867,56 +1001,56 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
867
1001
  throw err;
868
1002
  }
869
1003
 
870
- for (const block of validBlocks) {
871
- this.log.info(`Downloaded L2 block ${block.block.number}`, {
872
- blockHash: await block.block.hash(),
873
- blockNumber: block.block.number,
874
- txCount: block.block.body.txEffects.length,
875
- globalVariables: block.block.header.globalVariables.toInspect(),
876
- archiveRoot: block.block.archive.root.toString(),
877
- archiveNextLeafIndex: block.block.archive.nextAvailableLeafIndex,
1004
+ for (const checkpoint of validCheckpoints) {
1005
+ this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
1006
+ checkpointHash: checkpoint.checkpoint.hash(),
1007
+ checkpointNumber: checkpoint.checkpoint.number,
1008
+ blockCount: checkpoint.checkpoint.blocks.length,
1009
+ txCount: checkpoint.checkpoint.blocks.reduce((acc, b) => acc + b.body.txEffects.length, 0),
1010
+ header: checkpoint.checkpoint.header.toInspect(),
1011
+ archiveRoot: checkpoint.checkpoint.archive.root.toString(),
1012
+ archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex,
878
1013
  });
879
1014
  }
880
- lastRetrievedBlock = validBlocks.at(-1) ?? lastRetrievedBlock;
1015
+ lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
1016
+ lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
881
1017
  } while (searchEndBlock < currentL1BlockNumber);
882
1018
 
883
1019
  // Important that we update AFTER inserting the blocks.
884
- await updateProvenBlock();
1020
+ await updateProvenCheckpoint();
885
1021
 
886
- return { ...rollupStatus, lastRetrievedBlock };
1022
+ return { ...rollupStatus, lastRetrievedCheckpoint, lastL1BlockWithCheckpoint };
887
1023
  }
888
1024
 
889
- private async checkForNewBlocksBeforeL1SyncPoint(
890
- status: {
891
- lastRetrievedBlock?: PublishedL2Block;
892
- pendingBlockNumber: number;
893
- },
1025
+ private async checkForNewCheckpointsBeforeL1SyncPoint(
1026
+ status: RollupStatus,
894
1027
  blocksSynchedTo: bigint,
895
1028
  currentL1BlockNumber: bigint,
896
1029
  ) {
897
- const { lastRetrievedBlock, pendingBlockNumber } = status;
898
- // Compare the last L2 block we have (either retrieved in this round or loaded from store) with what the
1030
+ const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
1031
+ // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
899
1032
  // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
900
- const latestLocalL2BlockNumber = lastRetrievedBlock?.block.number ?? (await this.store.getSynchedL2BlockNumber());
901
- if (latestLocalL2BlockNumber < pendingBlockNumber) {
1033
+ const latestLocalCheckpointNumber =
1034
+ lastRetrievedCheckpoint?.checkpoint.number ?? (await this.getSynchedCheckpointNumber());
1035
+ if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
902
1036
  // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
903
- // but still havent reached the pending block according to the call to the rollup contract.
904
- // We suspect an L1 reorg that added blocks *behind* us. If that is the case, it must have happened between the
905
- // last L2 block we saw and the current one, so we reset the last synched L1 block number. In the edge case we
906
- // don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
907
- const latestLocalL2Block =
908
- lastRetrievedBlock ??
909
- (latestLocalL2BlockNumber > 0
910
- ? await this.store.getPublishedBlocks(latestLocalL2BlockNumber, 1).then(([b]) => b)
1037
+ // but still haven't reached the pending checkpoint according to the call to the rollup contract.
1038
+ // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
1039
+ // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
1040
+ // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
1041
+ const latestLocalCheckpoint =
1042
+ lastRetrievedCheckpoint ??
1043
+ (latestLocalCheckpointNumber > 0
1044
+ ? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c]) => c)
911
1045
  : undefined);
912
- const targetL1BlockNumber = latestLocalL2Block?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
913
- const latestLocalL2BlockArchive = latestLocalL2Block?.block.archive.root.toString();
1046
+ const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
1047
+ const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
914
1048
  this.log.warn(
915
- `Failed to reach L2 block ${pendingBlockNumber} at ${currentL1BlockNumber} (latest is ${latestLocalL2BlockNumber}). ` +
1049
+ `Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` +
916
1050
  `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`,
917
1051
  {
918
- latestLocalL2BlockNumber,
919
- latestLocalL2BlockArchive,
1052
+ latestLocalCheckpointNumber,
1053
+ latestLocalCheckpointArchive,
920
1054
  blocksSynchedTo,
921
1055
  currentL1BlockNumber,
922
1056
  ...status,
@@ -924,18 +1058,15 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
924
1058
  );
925
1059
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
926
1060
  } else {
927
- this.log.trace(`No new blocks behind L1 sync point to retrieve.`, {
928
- latestLocalL2BlockNumber,
929
- pendingBlockNumber,
1061
+ this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
1062
+ latestLocalCheckpointNumber,
1063
+ pendingCheckpointNumber,
930
1064
  });
931
1065
  }
932
1066
  }
933
1067
 
934
1068
  /** Resumes the archiver after a stop. */
935
1069
  public resume() {
936
- if (!this.runningPromise) {
937
- throw new Error(`Archiver was never started`);
938
- }
939
1070
  if (this.runningPromise.isRunning()) {
940
1071
  this.log.warn(`Archiver already running`);
941
1072
  }
@@ -949,7 +1080,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
949
1080
  */
950
1081
  public async stop(): Promise<void> {
951
1082
  this.log.debug('Stopping...');
952
- await this.runningPromise?.stop();
1083
+ await this.runningPromise.stop();
953
1084
 
954
1085
  this.log.info('Stopped.');
955
1086
  return Promise.resolve();
@@ -963,6 +1094,10 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
963
1094
  return Promise.resolve(this.l1constants);
964
1095
  }
965
1096
 
1097
+ public getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }> {
1098
+ return Promise.resolve({ genesisArchiveRoot: this.l1constants.genesisArchiveRoot });
1099
+ }
1100
+
966
1101
  public getRollupAddress(): Promise<EthAddress> {
967
1102
  return Promise.resolve(this.l1Addresses.rollupAddress);
968
1103
  }
@@ -971,49 +1106,45 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
971
1106
  return Promise.resolve(this.l1Addresses.registryAddress);
972
1107
  }
973
1108
 
974
- public getL1BlockNumber(): bigint {
975
- const l1BlockNumber = this.l1BlockNumber;
976
- if (!l1BlockNumber) {
977
- throw new Error('L1 block number not yet available. Complete an initial sync first.');
978
- }
979
- return l1BlockNumber;
1109
+ public getL1BlockNumber(): bigint | undefined {
1110
+ return this.l1BlockNumber;
980
1111
  }
981
1112
 
982
- public getL1Timestamp(): Promise<bigint> {
983
- const l1Timestamp = this.l1Timestamp;
984
- if (!l1Timestamp) {
985
- throw new Error('L1 timestamp not yet available. Complete an initial sync first.');
986
- }
987
- return Promise.resolve(l1Timestamp);
1113
+ public getL1Timestamp(): Promise<bigint | undefined> {
1114
+ return Promise.resolve(this.l1Timestamp);
988
1115
  }
989
1116
 
990
- public async getL2SlotNumber(): Promise<bigint> {
991
- return getSlotAtTimestamp(await this.getL1Timestamp(), this.l1constants);
1117
+ public getL2SlotNumber(): Promise<SlotNumber | undefined> {
1118
+ return Promise.resolve(
1119
+ this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants),
1120
+ );
992
1121
  }
993
1122
 
994
- public async getL2EpochNumber(): Promise<bigint> {
995
- return getEpochNumberAtTimestamp(await this.getL1Timestamp(), this.l1constants);
1123
+ public getL2EpochNumber(): Promise<EpochNumber | undefined> {
1124
+ return Promise.resolve(
1125
+ this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants),
1126
+ );
996
1127
  }
997
1128
 
998
- public async getBlocksForEpoch(epochNumber: bigint): Promise<L2Block[]> {
1129
+ public async getBlocksForEpoch(epochNumber: EpochNumber): Promise<L2Block[]> {
999
1130
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1000
1131
  const blocks: L2Block[] = [];
1001
1132
 
1002
1133
  // Walk the list of blocks backwards and filter by slots matching the requested epoch.
1003
1134
  // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
1004
1135
  let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
1005
- const slot = (b: L2Block) => b.header.globalVariables.slotNumber.toBigInt();
1136
+ const slot = (b: L2Block) => b.header.globalVariables.slotNumber;
1006
1137
  while (block && slot(block) >= start) {
1007
1138
  if (slot(block) <= end) {
1008
1139
  blocks.push(block);
1009
1140
  }
1010
- block = await this.getBlock(block.number - 1);
1141
+ block = await this.getBlock(BlockNumber(block.number - 1));
1011
1142
  }
1012
1143
 
1013
1144
  return blocks.reverse();
1014
1145
  }
1015
1146
 
1016
- public async getBlockHeadersForEpoch(epochNumber: bigint): Promise<BlockHeader[]> {
1147
+ public async getBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
1017
1148
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1018
1149
  const blocks: BlockHeader[] = [];
1019
1150
 
@@ -1021,20 +1152,21 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1021
1152
  // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
1022
1153
  let number = await this.store.getSynchedL2BlockNumber();
1023
1154
  let header = await this.getBlockHeader(number);
1024
- const slot = (b: BlockHeader) => b.globalVariables.slotNumber.toBigInt();
1155
+ const slot = (b: BlockHeader) => b.globalVariables.slotNumber;
1025
1156
  while (header && slot(header) >= start) {
1026
1157
  if (slot(header) <= end) {
1027
1158
  blocks.push(header);
1028
1159
  }
1029
- header = await this.getBlockHeader(--number);
1160
+ number = BlockNumber(number - 1);
1161
+ header = await this.getBlockHeader(number);
1030
1162
  }
1031
1163
  return blocks.reverse();
1032
1164
  }
1033
1165
 
1034
- public async isEpochComplete(epochNumber: bigint): Promise<boolean> {
1166
+ public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
1035
1167
  // The epoch is complete if the current L2 block is the last one in the epoch (or later)
1036
1168
  const header = await this.getBlockHeader('latest');
1037
- const slot = header?.globalVariables.slotNumber.toBigInt();
1169
+ const slot = header ? header.globalVariables.slotNumber : undefined;
1038
1170
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1039
1171
  if (slot && slot >= endSlot) {
1040
1172
  return true;
@@ -1064,6 +1196,101 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1064
1196
  return this.initialSyncComplete;
1065
1197
  }
1066
1198
 
1199
+ public async getPublishedCheckpoints(
1200
+ from: CheckpointNumber,
1201
+ limit: number,
1202
+ proven?: boolean,
1203
+ ): Promise<PublishedCheckpoint[]> {
1204
+ // TODO: Implement this properly. This only works when we have one block per checkpoint.
1205
+ const blocks = await this.getPublishedBlocks(BlockNumber(from), limit, proven);
1206
+ return blocks.map(b => b.toPublishedCheckpoint());
1207
+ }
1208
+
1209
+ public async getCheckpointByArchive(archive: Fr): Promise<Checkpoint | undefined> {
1210
+ // TODO: Implement this properly. This only works when we have one block per checkpoint.
1211
+ return (await this.getPublishedBlockByArchive(archive))?.block.toCheckpoint();
1212
+ }
1213
+
1214
+ public async getCheckpoints(from: CheckpointNumber, limit: number, proven?: boolean): Promise<Checkpoint[]> {
1215
+ const published = await this.getPublishedCheckpoints(from, limit, proven);
1216
+ return published.map(p => p.checkpoint);
1217
+ }
1218
+
1219
+ public async getCheckpoint(number: CheckpointNumber): Promise<Checkpoint | undefined> {
1220
+ if (number < 0) {
1221
+ number = await this.getSynchedCheckpointNumber();
1222
+ }
1223
+ if (number === 0) {
1224
+ return undefined;
1225
+ }
1226
+ const published = await this.getPublishedCheckpoints(number, 1);
1227
+ return published[0]?.checkpoint;
1228
+ }
1229
+
1230
+ public async getCheckpointHeader(number: CheckpointNumber | 'latest'): Promise<CheckpointHeader | undefined> {
1231
+ if (number === 'latest') {
1232
+ number = await this.getSynchedCheckpointNumber();
1233
+ }
1234
+ if (number === 0) {
1235
+ return undefined;
1236
+ }
1237
+ const checkpoint = await this.getCheckpoint(number);
1238
+ return checkpoint?.header;
1239
+ }
1240
+
1241
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
1242
+ return this.getSynchedCheckpointNumber();
1243
+ }
1244
+
1245
+ public async getSynchedCheckpointNumber(): Promise<CheckpointNumber> {
1246
+ // TODO: Create store and apis for checkpoints.
1247
+ // Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
1248
+ return CheckpointNumber(await this.store.getSynchedL2BlockNumber());
1249
+ }
1250
+
1251
+ public async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
1252
+ // TODO: Create store and apis for checkpoints.
1253
+ // Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
1254
+ return CheckpointNumber(await this.store.getProvenL2BlockNumber());
1255
+ }
1256
+
1257
+ public setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
1258
+ // TODO: Create store and apis for checkpoints.
1259
+ // Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
1260
+ return this.store.setProvenL2BlockNumber(BlockNumber.fromCheckpointNumber(checkpointNumber));
1261
+ }
1262
+
1263
+ public unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number): Promise<boolean> {
1264
+ // TODO: Create store and apis for checkpoints.
1265
+ // This only works when we have one block per checkpoint.
1266
+ return this.store.unwindBlocks(BlockNumber.fromCheckpointNumber(from), checkpointsToUnwind);
1267
+ }
1268
+
1269
+ public getLastBlockNumberInCheckpoint(checkpointNumber: CheckpointNumber): Promise<BlockNumber> {
1270
+ // TODO: Create store and apis for checkpoints.
1271
+ // Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
1272
+ return Promise.resolve(BlockNumber.fromCheckpointNumber(checkpointNumber));
1273
+ }
1274
+
1275
+ public addCheckpoints(
1276
+ checkpoints: PublishedCheckpoint[],
1277
+ pendingChainValidationStatus?: ValidateBlockResult,
1278
+ ): Promise<boolean> {
1279
+ // TODO: Create store and apis for checkpoints.
1280
+ // This only works when we have one block per checkpoint.
1281
+ return this.store.addBlocks(
1282
+ checkpoints.map(p => PublishedL2Block.fromPublishedCheckpoint(p)),
1283
+ pendingChainValidationStatus,
1284
+ );
1285
+ }
1286
+
1287
+ public async getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
1288
+ // TODO: Create store and apis for checkpoints.
1289
+ // This only works when we have one block per checkpoint.
1290
+ const blocks = await this.getBlocksForEpoch(epochNumber);
1291
+ return blocks.map(b => b.toCheckpoint());
1292
+ }
1293
+
1067
1294
  /**
1068
1295
  * Gets up to `limit` amount of L2 blocks starting from `from`.
1069
1296
  * @param from - Number of the first block to return (inclusive).
@@ -1071,24 +1298,40 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1071
1298
  * @param proven - If true, only return blocks that have been proven.
1072
1299
  * @returns The requested L2 blocks.
1073
1300
  */
1074
- public getBlocks(from: number, limit: number, proven?: boolean): Promise<L2Block[]> {
1301
+ public getBlocks(from: BlockNumber, limit: number, proven?: boolean): Promise<L2Block[]> {
1075
1302
  return this.getPublishedBlocks(from, limit, proven).then(blocks => blocks.map(b => b.block));
1076
1303
  }
1077
1304
 
1078
1305
  /** Equivalent to getBlocks but includes publish data. */
1079
- public async getPublishedBlocks(from: number, limit: number, proven?: boolean): Promise<PublishedL2Block[]> {
1306
+ public async getPublishedBlocks(from: BlockNumber, limit: number, proven?: boolean): Promise<PublishedL2Block[]> {
1080
1307
  const limitWithProven = proven
1081
1308
  ? Math.min(limit, Math.max((await this.store.getProvenL2BlockNumber()) - from + 1, 0))
1082
1309
  : limit;
1083
1310
  return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
1084
1311
  }
1085
1312
 
1313
+ public getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
1314
+ return this.store.getPublishedBlockByHash(blockHash);
1315
+ }
1316
+
1317
+ public getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
1318
+ return this.store.getPublishedBlockByArchive(archive);
1319
+ }
1320
+
1321
+ public getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
1322
+ return this.store.getBlockHeaderByHash(blockHash);
1323
+ }
1324
+
1325
+ public getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
1326
+ return this.store.getBlockHeaderByArchive(archive);
1327
+ }
1328
+
1086
1329
  /**
1087
1330
  * Gets an l2 block.
1088
1331
  * @param number - The block number to return.
1089
1332
  * @returns The requested L2 block.
1090
1333
  */
1091
- public async getBlock(number: number): Promise<L2Block | undefined> {
1334
+ public async getBlock(number: BlockNumber): Promise<L2Block | undefined> {
1092
1335
  // If the number provided is -ve, then return the latest block.
1093
1336
  if (number < 0) {
1094
1337
  number = await this.store.getSynchedL2BlockNumber();
@@ -1100,7 +1343,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1100
1343
  return publishedBlock?.block;
1101
1344
  }
1102
1345
 
1103
- public async getBlockHeader(number: number | 'latest'): Promise<BlockHeader | undefined> {
1346
+ public async getBlockHeader(number: BlockNumber | 'latest'): Promise<BlockHeader | undefined> {
1104
1347
  if (number === 'latest') {
1105
1348
  number = await this.store.getSynchedL2BlockNumber();
1106
1349
  }
@@ -1119,16 +1362,6 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1119
1362
  return this.store.getSettledTxReceipt(txHash);
1120
1363
  }
1121
1364
 
1122
- /**
1123
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
1124
- * @param from - The block number from which to begin retrieving logs.
1125
- * @param limit - The maximum number of blocks to retrieve logs from.
1126
- * @returns An array of private logs from the specified range of blocks.
1127
- */
1128
- public getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
1129
- return this.store.getPrivateLogs(from, limit);
1130
- }
1131
-
1132
1365
  /**
1133
1366
  * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
1134
1367
  * @param tags - The tags to filter the logs by.
@@ -1161,16 +1394,16 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1161
1394
  * Gets the number of the latest L2 block processed by the block source implementation.
1162
1395
  * @returns The number of the latest L2 block processed by the block source implementation.
1163
1396
  */
1164
- public getBlockNumber(): Promise<number> {
1397
+ public getBlockNumber(): Promise<BlockNumber> {
1165
1398
  return this.store.getSynchedL2BlockNumber();
1166
1399
  }
1167
1400
 
1168
- public getProvenBlockNumber(): Promise<number> {
1401
+ public getProvenBlockNumber(): Promise<BlockNumber> {
1169
1402
  return this.store.getProvenL2BlockNumber();
1170
1403
  }
1171
1404
 
1172
1405
  /** Forcefully updates the last proven block number. Use for testing. */
1173
- public setProvenBlockNumber(blockNumber: number): Promise<void> {
1406
+ public setProvenBlockNumber(blockNumber: BlockNumber): Promise<void> {
1174
1407
  return this.store.setProvenL2BlockNumber(blockNumber);
1175
1408
  }
1176
1409
 
@@ -1199,12 +1432,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1199
1432
  }
1200
1433
 
1201
1434
  /**
1202
- * Gets L1 to L2 message (to be) included in a given block.
1203
- * @param blockNumber - L2 block number to get messages for.
1435
+ * Gets L1 to L2 message (to be) included in a given checkpoint.
1436
+ * @param checkpointNumber - Checkpoint number to get messages for.
1204
1437
  * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
1205
1438
  */
1206
- getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
1207
- return this.store.getL1ToL2Messages(blockNumber);
1439
+ getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
1440
+ return this.store.getL1ToL2Messages(checkpointNumber);
1208
1441
  }
1209
1442
 
1210
1443
  /**
@@ -1228,12 +1461,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1228
1461
  return this.store.getDebugFunctionName(address, selector);
1229
1462
  }
1230
1463
 
1231
- getPendingChainValidationStatus(): Promise<ValidateBlockResult> {
1232
- return Promise.resolve(this.pendingChainValidationStatus);
1464
+ async getPendingChainValidationStatus(): Promise<ValidateBlockResult> {
1465
+ return (await this.store.getPendingChainValidationStatus()) ?? { valid: true };
1233
1466
  }
1234
1467
 
1235
1468
  isPendingChainInvalid(): Promise<boolean> {
1236
- return Promise.resolve(this.pendingChainValidationStatus.valid === false);
1469
+ return this.getPendingChainValidationStatus().then(status => !status.valid);
1237
1470
  }
1238
1471
 
1239
1472
  async getL2Tips(): Promise<L2Tips> {
@@ -1245,7 +1478,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1245
1478
  // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
1246
1479
  // We just force it 2 epochs worth of proven data for now.
1247
1480
  // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
1248
- const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
1481
+ const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
1249
1482
 
1250
1483
  const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
1251
1484
  latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
@@ -1269,27 +1502,18 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1269
1502
  );
1270
1503
  }
1271
1504
 
1272
- const latestBlockHeaderHash = await latestBlockHeader?.hash();
1273
- const provenBlockHeaderHash = await provenBlockHeader?.hash();
1274
- const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
1505
+ const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
1506
+ const provenBlockHeaderHash = (await provenBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
1507
+ const finalizedBlockHeaderHash = (await finalizedBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
1275
1508
 
1276
1509
  return {
1277
- latest: {
1278
- number: latestBlockNumber,
1279
- hash: latestBlockHeaderHash?.toString(),
1280
- } as L2BlockId,
1281
- proven: {
1282
- number: provenBlockNumber,
1283
- hash: provenBlockHeaderHash?.toString(),
1284
- } as L2BlockId,
1285
- finalized: {
1286
- number: finalizedBlockNumber,
1287
- hash: finalizedBlockHeaderHash?.toString(),
1288
- } as L2BlockId,
1510
+ latest: { number: latestBlockNumber, hash: latestBlockHeaderHash.toString() },
1511
+ proven: { number: provenBlockNumber, hash: provenBlockHeaderHash.toString() },
1512
+ finalized: { number: finalizedBlockNumber, hash: finalizedBlockHeaderHash.toString() },
1289
1513
  };
1290
1514
  }
1291
1515
 
1292
- public async rollbackTo(targetL2BlockNumber: number): Promise<void> {
1516
+ public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
1293
1517
  const currentBlocks = await this.getL2Tips();
1294
1518
  const currentL2Block = currentBlocks.latest.number;
1295
1519
  const currentProvenBlock = currentBlocks.proven.number;
@@ -1304,17 +1528,18 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1304
1528
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1305
1529
  }
1306
1530
  const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1531
+ const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
1307
1532
  const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1308
1533
  this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
1309
- await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
1310
- this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
1311
- await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
1534
+ await this.store.unwindBlocks(BlockNumber(currentL2Block), blocksToUnwind);
1535
+ this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
1536
+ await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1312
1537
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1313
1538
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1314
1539
  await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
1315
1540
  if (targetL2BlockNumber < currentProvenBlock) {
1316
1541
  this.log.info(`Clearing proven L2 block number`);
1317
- await this.store.setProvenL2BlockNumber(0);
1542
+ await this.store.setProvenL2BlockNumber(BlockNumber.ZERO);
1318
1543
  }
1319
1544
  // TODO(palla/reorg): Set the finalized block when we add support for it.
1320
1545
  // if (targetL2BlockNumber < currentFinalizedBlock) {
@@ -1351,6 +1576,7 @@ export class ArchiverStoreHelper
1351
1576
  | 'backupTo'
1352
1577
  | 'close'
1353
1578
  | 'transactionAsync'
1579
+ | 'addBlocks'
1354
1580
  >
1355
1581
  {
1356
1582
  #log = createLogger('archiver:block-helper');
@@ -1361,7 +1587,7 @@ export class ArchiverStoreHelper
1361
1587
  * Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
1362
1588
  * @param allLogs - All logs emitted in a bunch of blocks.
1363
1589
  */
1364
- async #updatePublishedContractClasses(allLogs: ContractClassLog[], blockNum: number, operation: Operation) {
1590
+ async #updatePublishedContractClasses(allLogs: ContractClassLog[], blockNum: BlockNumber, operation: Operation) {
1365
1591
  const contractClassPublishedEvents = allLogs
1366
1592
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
1367
1593
  .map(log => ContractClassPublishedEvent.fromLog(log));
@@ -1386,7 +1612,7 @@ export class ArchiverStoreHelper
1386
1612
  * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
1387
1613
  * @param allLogs - All logs emitted in a bunch of blocks.
1388
1614
  */
1389
- async #updateDeployedContractInstances(allLogs: PrivateLog[], blockNum: number, operation: Operation) {
1615
+ async #updateDeployedContractInstances(allLogs: PrivateLog[], blockNum: BlockNumber, operation: Operation) {
1390
1616
  const contractInstances = allLogs
1391
1617
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
1392
1618
  .map(log => ContractInstancePublishedEvent.fromLog(log))
@@ -1439,7 +1665,7 @@ export class ArchiverStoreHelper
1439
1665
  * @param _blockNum - The block number
1440
1666
  * @returns
1441
1667
  */
1442
- async #storeBroadcastedIndividualFunctions(allLogs: ContractClassLog[], _blockNum: number) {
1668
+ async #storeBroadcastedIndividualFunctions(allLogs: ContractClassLog[], _blockNum: BlockNumber) {
1443
1669
  // Filter out private and utility function broadcast events
1444
1670
  const privateFnEvents = allLogs
1445
1671
  .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
@@ -1493,13 +1719,16 @@ export class ArchiverStoreHelper
1493
1719
  return true;
1494
1720
  }
1495
1721
 
1496
- public addBlocks(blocks: PublishedL2Block[]): Promise<boolean> {
1722
+ public addBlocks(blocks: PublishedL2Block[], pendingChainValidationStatus?: ValidateBlockResult): Promise<boolean> {
1497
1723
  // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1498
1724
  // or if the previous block is not in the store.
1499
1725
  return this.store.transactionAsync(async () => {
1500
1726
  await this.store.addBlocks(blocks);
1501
1727
 
1502
1728
  const opResults = await Promise.all([
1729
+ // Update the pending chain validation status if provided
1730
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1731
+ // Add any logs emitted during the retrieved blocks
1503
1732
  this.store.addLogs(blocks.map(block => block.block)),
1504
1733
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1505
1734
  ...blocks.map(async block => {
@@ -1526,7 +1755,7 @@ export class ArchiverStoreHelper
1526
1755
  });
1527
1756
  }
1528
1757
 
1529
- public async unwindBlocks(from: number, blocksToUnwind: number): Promise<boolean> {
1758
+ public async unwindBlocks(from: BlockNumber, blocksToUnwind: number): Promise<boolean> {
1530
1759
  const last = await this.getSynchedL2BlockNumber();
1531
1760
  if (from != last) {
1532
1761
  throw new Error(`Cannot unwind blocks from block ${from} when the last block is ${last}`);
@@ -1536,9 +1765,11 @@ export class ArchiverStoreHelper
1536
1765
  }
1537
1766
 
1538
1767
  // from - blocksToUnwind = the new head, so + 1 for what we need to remove
1539
- const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1768
+ const blocks = await this.getPublishedBlocks(BlockNumber(from - blocksToUnwind + 1), blocksToUnwind);
1540
1769
 
1541
1770
  const opResults = await Promise.all([
1771
+ // Prune rolls back to the last proven block, which is by definition valid
1772
+ this.store.setPendingChainValidationStatus({ valid: true }),
1542
1773
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1543
1774
  ...blocks.map(async block => {
1544
1775
  const contractClassLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
@@ -1566,15 +1797,27 @@ export class ArchiverStoreHelper
1566
1797
  return opResults.every(Boolean);
1567
1798
  }
1568
1799
 
1569
- getPublishedBlocks(from: number, limit: number): Promise<PublishedL2Block[]> {
1800
+ getPublishedBlocks(from: BlockNumber, limit: number): Promise<PublishedL2Block[]> {
1570
1801
  return this.store.getPublishedBlocks(from, limit);
1571
1802
  }
1572
- getPublishedBlock(number: number): Promise<PublishedL2Block | undefined> {
1803
+ getPublishedBlock(number: BlockNumber): Promise<PublishedL2Block | undefined> {
1573
1804
  return this.store.getPublishedBlock(number);
1574
1805
  }
1575
- getBlockHeaders(from: number, limit: number): Promise<BlockHeader[]> {
1806
+ getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
1807
+ return this.store.getPublishedBlockByHash(blockHash);
1808
+ }
1809
+ getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
1810
+ return this.store.getPublishedBlockByArchive(archive);
1811
+ }
1812
+ getBlockHeaders(from: BlockNumber, limit: number): Promise<BlockHeader[]> {
1576
1813
  return this.store.getBlockHeaders(from, limit);
1577
1814
  }
1815
+ getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
1816
+ return this.store.getBlockHeaderByHash(blockHash);
1817
+ }
1818
+ getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
1819
+ return this.store.getBlockHeaderByArchive(archive);
1820
+ }
1578
1821
  getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
1579
1822
  return this.store.getTxEffect(txHash);
1580
1823
  }
@@ -1584,15 +1827,12 @@ export class ArchiverStoreHelper
1584
1827
  addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
1585
1828
  return this.store.addL1ToL2Messages(messages);
1586
1829
  }
1587
- getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
1588
- return this.store.getL1ToL2Messages(blockNumber);
1830
+ getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
1831
+ return this.store.getL1ToL2Messages(checkpointNumber);
1589
1832
  }
1590
1833
  getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
1591
1834
  return this.store.getL1ToL2MessageIndex(l1ToL2Message);
1592
1835
  }
1593
- getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
1594
- return this.store.getPrivateLogs(from, limit);
1595
- }
1596
1836
  getLogsByTags(tags: Fr[], logsPerTag?: number): Promise<TxScopedL2Log[][]> {
1597
1837
  return this.store.getLogsByTags(tags, logsPerTag);
1598
1838
  }
@@ -1602,13 +1842,13 @@ export class ArchiverStoreHelper
1602
1842
  getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
1603
1843
  return this.store.getContractClassLogs(filter);
1604
1844
  }
1605
- getSynchedL2BlockNumber(): Promise<number> {
1845
+ getSynchedL2BlockNumber(): Promise<BlockNumber> {
1606
1846
  return this.store.getSynchedL2BlockNumber();
1607
1847
  }
1608
- getProvenL2BlockNumber(): Promise<number> {
1848
+ getProvenL2BlockNumber(): Promise<BlockNumber> {
1609
1849
  return this.store.getProvenL2BlockNumber();
1610
1850
  }
1611
- setProvenL2BlockNumber(l2BlockNumber: number): Promise<void> {
1851
+ setProvenL2BlockNumber(l2BlockNumber: BlockNumber): Promise<void> {
1612
1852
  return this.store.setProvenL2BlockNumber(l2BlockNumber);
1613
1853
  }
1614
1854
  setBlockSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
@@ -1644,8 +1884,8 @@ export class ArchiverStoreHelper
1644
1884
  estimateSize(): Promise<{ mappingSize: number; physicalFileSize: number; actualSize: number; numItems: number }> {
1645
1885
  return this.store.estimateSize();
1646
1886
  }
1647
- rollbackL1ToL2MessagesToL2Block(targetBlockNumber: number): Promise<void> {
1648
- return this.store.rollbackL1ToL2MessagesToL2Block(targetBlockNumber);
1887
+ rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber: CheckpointNumber): Promise<void> {
1888
+ return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1649
1889
  }
1650
1890
  iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
1651
1891
  return this.store.iterateL1ToL2Messages(range);
@@ -1656,4 +1896,11 @@ export class ArchiverStoreHelper
1656
1896
  getLastL1ToL2Message(): Promise<InboxMessage | undefined> {
1657
1897
  return this.store.getLastL1ToL2Message();
1658
1898
  }
1899
+ getPendingChainValidationStatus(): Promise<ValidateBlockResult | undefined> {
1900
+ return this.store.getPendingChainValidationStatus();
1901
+ }
1902
+ setPendingChainValidationStatus(status: ValidateBlockResult | undefined): Promise<void> {
1903
+ this.#log.debug(`Setting pending chain validation status to valid ${status?.valid}`, status);
1904
+ return this.store.setPendingChainValidationStatus(status);
1905
+ }
1659
1906
  }