@aztec/archiver 0.0.1-commit.b655e406 → 0.0.1-commit.c7c42ec

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 (132) hide show
  1. package/dest/archiver/archiver.d.ts +110 -83
  2. package/dest/archiver/archiver.d.ts.map +1 -1
  3. package/dest/archiver/archiver.js +672 -349
  4. package/dest/archiver/archiver_store.d.ts +100 -47
  5. package/dest/archiver/archiver_store.d.ts.map +1 -1
  6. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  8. package/dest/archiver/archiver_store_test_suite.js +1871 -389
  9. package/dest/archiver/config.d.ts +5 -4
  10. package/dest/archiver/config.d.ts.map +1 -1
  11. package/dest/archiver/config.js +15 -3
  12. package/dest/archiver/errors.d.ts +25 -1
  13. package/dest/archiver/errors.d.ts.map +1 -1
  14. package/dest/archiver/errors.js +37 -0
  15. package/dest/archiver/index.d.ts +2 -2
  16. package/dest/archiver/index.d.ts.map +1 -1
  17. package/dest/archiver/instrumentation.d.ts +5 -3
  18. package/dest/archiver/instrumentation.d.ts.map +1 -1
  19. package/dest/archiver/instrumentation.js +11 -0
  20. package/dest/archiver/kv_archiver_store/block_store.d.ts +51 -18
  21. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  22. package/dest/archiver/kv_archiver_store/block_store.js +324 -87
  23. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +2 -2
  24. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  25. package/dest/archiver/kv_archiver_store/contract_class_store.js +1 -1
  26. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +2 -2
  27. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  28. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +47 -57
  29. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  30. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +65 -48
  31. package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
  32. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  33. package/dest/archiver/kv_archiver_store/log_store.js +149 -84
  34. package/dest/archiver/kv_archiver_store/message_store.d.ts +6 -5
  35. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  36. package/dest/archiver/kv_archiver_store/message_store.js +15 -14
  37. package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
  38. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
  39. package/dest/archiver/l1/bin/retrieve-calldata.js +149 -0
  40. package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
  41. package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
  42. package/dest/archiver/l1/calldata_retriever.js +471 -0
  43. package/dest/archiver/l1/data_retrieval.d.ts +90 -0
  44. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
  45. package/dest/archiver/l1/data_retrieval.js +331 -0
  46. package/dest/archiver/l1/debug_tx.d.ts +19 -0
  47. package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
  48. package/dest/archiver/l1/debug_tx.js +73 -0
  49. package/dest/archiver/l1/spire_proposer.d.ts +70 -0
  50. package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
  51. package/dest/archiver/l1/spire_proposer.js +157 -0
  52. package/dest/archiver/l1/trace_tx.d.ts +97 -0
  53. package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
  54. package/dest/archiver/l1/trace_tx.js +91 -0
  55. package/dest/archiver/l1/types.d.ts +12 -0
  56. package/dest/archiver/l1/types.d.ts.map +1 -0
  57. package/dest/archiver/l1/types.js +3 -0
  58. package/dest/archiver/l1/validate_trace.d.ts +29 -0
  59. package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
  60. package/dest/archiver/l1/validate_trace.js +150 -0
  61. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  62. package/dest/archiver/structs/inbox_message.d.ts +4 -4
  63. package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
  64. package/dest/archiver/structs/inbox_message.js +6 -5
  65. package/dest/archiver/structs/published.d.ts +2 -2
  66. package/dest/archiver/structs/published.d.ts.map +1 -1
  67. package/dest/archiver/validation.d.ts +10 -4
  68. package/dest/archiver/validation.d.ts.map +1 -1
  69. package/dest/archiver/validation.js +29 -21
  70. package/dest/factory.d.ts +2 -2
  71. package/dest/factory.d.ts.map +1 -1
  72. package/dest/factory.js +4 -3
  73. package/dest/index.d.ts +2 -2
  74. package/dest/index.d.ts.map +1 -1
  75. package/dest/index.js +1 -1
  76. package/dest/rpc/index.d.ts +2 -2
  77. package/dest/test/index.d.ts +1 -1
  78. package/dest/test/mock_archiver.d.ts +16 -8
  79. package/dest/test/mock_archiver.d.ts.map +1 -1
  80. package/dest/test/mock_archiver.js +19 -14
  81. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  82. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  83. package/dest/test/mock_l1_to_l2_message_source.js +10 -9
  84. package/dest/test/mock_l2_block_source.d.ts +23 -11
  85. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  86. package/dest/test/mock_l2_block_source.js +38 -24
  87. package/dest/test/mock_structs.d.ts +3 -2
  88. package/dest/test/mock_structs.d.ts.map +1 -1
  89. package/dest/test/mock_structs.js +9 -8
  90. package/package.json +18 -17
  91. package/src/archiver/archiver.ts +884 -449
  92. package/src/archiver/archiver_store.ts +113 -46
  93. package/src/archiver/archiver_store_test_suite.ts +1936 -356
  94. package/src/archiver/config.ts +20 -10
  95. package/src/archiver/errors.ts +64 -0
  96. package/src/archiver/index.ts +1 -1
  97. package/src/archiver/instrumentation.ts +16 -2
  98. package/src/archiver/kv_archiver_store/block_store.ts +442 -101
  99. package/src/archiver/kv_archiver_store/contract_class_store.ts +1 -1
  100. package/src/archiver/kv_archiver_store/contract_instance_store.ts +1 -1
  101. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +87 -71
  102. package/src/archiver/kv_archiver_store/log_store.ts +209 -99
  103. package/src/archiver/kv_archiver_store/message_store.ts +21 -18
  104. package/src/archiver/l1/README.md +98 -0
  105. package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
  106. package/src/archiver/l1/calldata_retriever.ts +641 -0
  107. package/src/archiver/l1/data_retrieval.ts +512 -0
  108. package/src/archiver/l1/debug_tx.ts +99 -0
  109. package/src/archiver/l1/spire_proposer.ts +160 -0
  110. package/src/archiver/l1/trace_tx.ts +128 -0
  111. package/src/archiver/l1/types.ts +13 -0
  112. package/src/archiver/l1/validate_trace.ts +211 -0
  113. package/src/archiver/structs/inbox_message.ts +8 -8
  114. package/src/archiver/structs/published.ts +1 -1
  115. package/src/archiver/validation.ts +52 -27
  116. package/src/factory.ts +4 -3
  117. package/src/index.ts +1 -1
  118. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  119. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  120. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  121. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  122. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  123. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  124. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  125. package/src/test/mock_archiver.ts +22 -16
  126. package/src/test/mock_l1_to_l2_message_source.ts +10 -9
  127. package/src/test/mock_l2_block_source.ts +51 -30
  128. package/src/test/mock_structs.ts +10 -9
  129. package/dest/archiver/data_retrieval.d.ts +0 -79
  130. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  131. package/dest/archiver/data_retrieval.js +0 -362
  132. package/src/archiver/data_retrieval.ts +0 -545
@@ -1,24 +1,23 @@
1
- import type { BlobSinkClientInterface } from '@aztec/blob-sink/client';
1
+ import type { BlobClientInterface } from '@aztec/blob-client/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
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';
17
16
  import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
18
17
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
19
- import { sleep } from '@aztec/foundation/sleep';
20
18
  import { count } from '@aztec/foundation/string';
21
19
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
20
+ import { isDefined } from '@aztec/foundation/types';
22
21
  import type { CustomRange } from '@aztec/kv-store';
23
22
  import { RollupAbi } from '@aztec/l1-artifacts';
24
23
  import {
@@ -34,12 +33,17 @@ import type { FunctionSelector } from '@aztec/stdlib/abi';
34
33
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
35
34
  import {
36
35
  type ArchiverEmitter,
37
- type L2Block,
38
- type L2BlockId,
36
+ CheckpointedL2Block,
37
+ CommitteeAttestation,
38
+ L2Block,
39
+ L2BlockNew,
40
+ type L2BlockSink,
39
41
  type L2BlockSource,
40
42
  L2BlockSourceEvents,
41
43
  type L2Tips,
44
+ PublishedL2Block,
42
45
  } from '@aztec/stdlib/block';
46
+ import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
43
47
  import {
44
48
  type ContractClassPublic,
45
49
  type ContractDataSource,
@@ -60,12 +64,20 @@ import {
60
64
  } from '@aztec/stdlib/epoch-helpers';
61
65
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
62
66
  import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
63
- import { ContractClassLog, type LogFilter, type PrivateLog, type PublicLog, TxScopedL2Log } from '@aztec/stdlib/logs';
64
- import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
67
+ import {
68
+ ContractClassLog,
69
+ type LogFilter,
70
+ type PrivateLog,
71
+ type PublicLog,
72
+ type SiloedTag,
73
+ Tag,
74
+ TxScopedL2Log,
75
+ } from '@aztec/stdlib/logs';
76
+ import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
77
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
65
78
  import { type BlockHeader, type IndexedTxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
66
79
  import type { UInt64 } from '@aztec/stdlib/types';
67
80
  import {
68
- Attributes,
69
81
  type TelemetryClient,
70
82
  type Traceable,
71
83
  type Tracer,
@@ -75,30 +87,38 @@ import {
75
87
 
76
88
  import { EventEmitter } from 'events';
77
89
  import groupBy from 'lodash.groupby';
78
- import { type GetContractReturnType, createPublicClient, fallback, http } from 'viem';
90
+ import { type GetContractReturnType, type Hex, createPublicClient, fallback, http } from 'viem';
79
91
 
80
92
  import type { ArchiverDataStore, ArchiverL1SynchPoint } from './archiver_store.js';
81
93
  import type { ArchiverConfig } from './config.js';
94
+ import { InitialCheckpointNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
95
+ import { ArchiverInstrumentation } from './instrumentation.js';
96
+ import type { CheckpointData } from './kv_archiver_store/block_store.js';
82
97
  import {
83
- retrieveBlocksFromRollup,
98
+ retrieveCheckpointsFromRollup,
84
99
  retrieveL1ToL2Message,
85
100
  retrieveL1ToL2Messages,
86
- retrievedBlockToPublishedL2Block,
87
- } from './data_retrieval.js';
88
- import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
89
- import { ArchiverInstrumentation } from './instrumentation.js';
101
+ retrievedToPublishedCheckpoint,
102
+ } from './l1/data_retrieval.js';
103
+ import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
90
104
  import type { InboxMessage } from './structs/inbox_message.js';
91
- import type { PublishedL2Block } from './structs/published.js';
92
- import { type ValidateBlockResult, validateBlockAttestations } from './validation.js';
105
+ import { type ValidateBlockResult, validateCheckpointAttestations } from './validation.js';
93
106
 
94
107
  /**
95
108
  * Helper interface to combine all sources this archiver implementation provides.
96
109
  */
97
110
  export type ArchiveSource = L2BlockSource & L2LogsSource & ContractDataSource & L1ToL2MessageSource;
98
111
 
112
+ /** Request to add a block to the archiver, queued for processing by the sync loop. */
113
+ type AddBlockRequest = {
114
+ block: L2BlockNew;
115
+ resolve: () => void;
116
+ reject: (err: Error) => void;
117
+ };
118
+
99
119
  export type ArchiverDeps = {
100
120
  telemetry?: TelemetryClient;
101
- blobSinkClient: BlobSinkClientInterface;
121
+ blobClient: BlobClientInterface;
102
122
  epochCache?: EpochCache;
103
123
  dateProvider?: DateProvider;
104
124
  };
@@ -108,17 +128,32 @@ function mapArchiverConfig(config: Partial<ArchiverConfig>) {
108
128
  pollingIntervalMs: config.archiverPollingIntervalMS,
109
129
  batchSize: config.archiverBatchSize,
110
130
  skipValidateBlockAttestations: config.skipValidateBlockAttestations,
131
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
132
+ ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts,
111
133
  };
112
134
  }
113
135
 
136
+ type RollupStatus = {
137
+ provenCheckpointNumber: CheckpointNumber;
138
+ provenArchive: Hex;
139
+ pendingCheckpointNumber: CheckpointNumber;
140
+ pendingArchive: Hex;
141
+ validationResult: ValidateBlockResult | undefined;
142
+ lastRetrievedCheckpoint?: PublishedCheckpoint;
143
+ lastL1BlockWithCheckpoint?: bigint;
144
+ };
145
+
114
146
  /**
115
- * Pulls L2 blocks in a non-blocking manner and provides interface for their retrieval.
147
+ * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
116
148
  * Responsible for handling robust L1 polling so that other components do not need to
117
149
  * concern themselves with it.
118
150
  */
119
- export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implements ArchiveSource, Traceable {
120
- /** A loop in which we will be continually fetching new L2 blocks. */
121
- private runningPromise?: RunningPromise;
151
+ export class Archiver
152
+ extends (EventEmitter as new () => ArchiverEmitter)
153
+ implements ArchiveSource, L2BlockSink, Traceable
154
+ {
155
+ /** A loop in which we will be continually fetching new checkpoints. */
156
+ private runningPromise: RunningPromise;
122
157
 
123
158
  private rollup: RollupContract;
124
159
  private inbox: InboxContract;
@@ -130,11 +165,15 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
130
165
  private initialSyncComplete: boolean = false;
131
166
  private initialSyncPromise: PromiseWithResolvers<void>;
132
167
 
168
+ /** Queue of blocks to be added to the store, processed by the sync loop. */
169
+ private blockQueue: AddBlockRequest[] = [];
170
+
133
171
  public readonly tracer: Tracer;
134
172
 
135
173
  /**
136
174
  * Creates a new instance of the Archiver.
137
175
  * @param publicClient - A client for interacting with the Ethereum node.
176
+ * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
138
177
  * @param rollupAddress - Ethereum address of the rollup contract.
139
178
  * @param inboxAddress - Ethereum address of the inbox contract.
140
179
  * @param registryAddress - Ethereum address of the registry contract.
@@ -144,11 +183,22 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
144
183
  */
145
184
  constructor(
146
185
  private readonly publicClient: ViemPublicClient,
147
- private readonly l1Addresses: { rollupAddress: EthAddress; inboxAddress: EthAddress; registryAddress: EthAddress },
186
+ private readonly debugClient: ViemPublicDebugClient,
187
+ private readonly l1Addresses: Pick<
188
+ L1ContractAddresses,
189
+ 'rollupAddress' | 'inboxAddress' | 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
190
+ > & { slashingProposerAddress: EthAddress },
148
191
  readonly dataStore: ArchiverDataStore,
149
- private config: { pollingIntervalMs: number; batchSize: number; skipValidateBlockAttestations?: boolean },
150
- private readonly blobSinkClient: BlobSinkClientInterface,
192
+ private config: {
193
+ pollingIntervalMs: number;
194
+ batchSize: number;
195
+ skipValidateBlockAttestations?: boolean;
196
+ maxAllowedEthClientDriftSeconds: number;
197
+ ethereumAllowNoDebugHosts?: boolean;
198
+ },
199
+ private readonly blobClient: BlobClientInterface,
151
200
  private readonly epochCache: EpochCache,
201
+ private readonly dateProvider: DateProvider,
152
202
  private readonly instrumentation: ArchiverInstrumentation,
153
203
  private readonly l1constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
154
204
  private readonly log: Logger = createLogger('archiver'),
@@ -161,6 +211,15 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
161
211
  this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
162
212
  this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
163
213
  this.initialSyncPromise = promiseWithResolvers();
214
+
215
+ // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
216
+ // are done as fast as possible. This then gets updated once the initial sync completes.
217
+ this.runningPromise = new RunningPromise(
218
+ () => this.sync(),
219
+ this.log,
220
+ this.config.pollingIntervalMs / 10,
221
+ makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError),
222
+ );
164
223
  }
165
224
 
166
225
  /**
@@ -179,18 +238,28 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
179
238
  const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
180
239
  const publicClient = createPublicClient({
181
240
  chain: chain.chainInfo,
182
- transport: fallback(config.l1RpcUrls.map(url => http(url))),
241
+ transport: fallback(config.l1RpcUrls.map(url => http(url, { batch: false }))),
183
242
  pollingInterval: config.viemPollingIntervalMS,
184
243
  });
185
244
 
245
+ // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
246
+ const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
247
+ const debugClient = createPublicClient({
248
+ chain: chain.chainInfo,
249
+ transport: fallback(debugRpcUrls.map(url => http(url, { batch: false }))),
250
+ pollingInterval: config.viemPollingIntervalMS,
251
+ }) as ViemPublicDebugClient;
252
+
186
253
  const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
187
254
 
188
- const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
189
- rollup.getL1StartBlock(),
190
- rollup.getL1GenesisTime(),
191
- rollup.getProofSubmissionEpochs(),
192
- rollup.getGenesisArchiveTreeRoot(),
193
- ] as const);
255
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] =
256
+ await Promise.all([
257
+ rollup.getL1StartBlock(),
258
+ rollup.getL1GenesisTime(),
259
+ rollup.getProofSubmissionEpochs(),
260
+ rollup.getGenesisArchiveTreeRoot(),
261
+ rollup.getSlashingProposerAddress(),
262
+ ] as const);
194
263
 
195
264
  const l1StartBlockHash = await publicClient
196
265
  .getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
@@ -209,18 +278,28 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
209
278
  genesisArchiveRoot: Fr.fromHexString(genesisArchiveRoot),
210
279
  };
211
280
 
212
- const opts = merge({ pollingIntervalMs: 10_000, batchSize: 100 }, mapArchiverConfig(config));
281
+ const opts = merge(
282
+ {
283
+ pollingIntervalMs: 10_000,
284
+ batchSize: 100,
285
+ maxAllowedEthClientDriftSeconds: 300,
286
+ ethereumAllowNoDebugHosts: false,
287
+ },
288
+ mapArchiverConfig(config),
289
+ );
213
290
 
214
291
  const epochCache = deps.epochCache ?? (await EpochCache.create(config.l1Contracts.rollupAddress, config, deps));
215
292
  const telemetry = deps.telemetry ?? getTelemetryClient();
216
293
 
217
294
  const archiver = new Archiver(
218
295
  publicClient,
219
- config.l1Contracts,
296
+ debugClient,
297
+ { ...config.l1Contracts, slashingProposerAddress },
220
298
  archiverStore,
221
299
  opts,
222
- deps.blobSinkClient,
300
+ deps.blobClient,
223
301
  epochCache,
302
+ deps.dateProvider ?? new DateProvider(),
224
303
  await ArchiverInstrumentation.new(telemetry, () => archiverStore.estimateSize()),
225
304
  l1Constants,
226
305
  );
@@ -238,66 +317,99 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
238
317
  * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
239
318
  */
240
319
  public async start(blockUntilSynced: boolean): Promise<void> {
241
- if (this.runningPromise) {
320
+ if (this.runningPromise.isRunning()) {
242
321
  throw new Error('Archiver is already running');
243
322
  }
244
323
 
245
- await this.blobSinkClient.testSources();
324
+ await this.blobClient.testSources();
325
+ await this.testEthereumNodeSynced();
326
+ await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
327
+
328
+ // Log initial state for the archiver
329
+ const { l1StartBlock } = this.l1constants;
330
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
331
+ const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
332
+ this.log.info(
333
+ `Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`,
334
+ { blocksSynchedTo, messagesSynchedTo, currentL2Checkpoint },
335
+ );
246
336
 
337
+ // Start sync loop, and return the wait for initial sync if we are asked to block until synced
338
+ this.runningPromise.start();
247
339
  if (blockUntilSynced) {
248
- while (!(await this.syncSafe(true))) {
249
- this.log.info(`Retrying initial archiver sync in ${this.config.pollingIntervalMs}ms`);
250
- await sleep(this.config.pollingIntervalMs);
251
- }
340
+ return this.waitForInitialSync();
252
341
  }
342
+ }
253
343
 
254
- this.runningPromise = new RunningPromise(
255
- () => this.sync(false),
256
- this.log,
257
- this.config.pollingIntervalMs,
258
- makeLoggingErrorHandler(
259
- this.log,
260
- // Ignored errors will not log to the console
261
- // We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
262
- NoBlobBodiesFoundError,
263
- ),
264
- );
344
+ public syncImmediate() {
345
+ return this.runningPromise.trigger();
346
+ }
265
347
 
266
- this.runningPromise.start();
348
+ /**
349
+ * Queues a block to be added to the archiver store and triggers processing.
350
+ * The block will be processed by the sync loop.
351
+ * Implements the L2BlockSink interface.
352
+ * @param block - The L2 block to add.
353
+ * @returns A promise that resolves when the block has been added to the store, or rejects on error.
354
+ */
355
+ public addBlock(block: L2BlockNew): Promise<void> {
356
+ return new Promise<void>((resolve, reject) => {
357
+ this.blockQueue.push({ block, resolve, reject });
358
+ this.log.debug(`Queued block ${block.number} for processing`);
359
+ // Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
360
+ this.syncImmediate().catch(err => {
361
+ this.log.error(`Sync immediate call failed: ${err}`);
362
+ });
363
+ });
267
364
  }
268
365
 
269
- public syncImmediate() {
270
- if (!this.runningPromise) {
271
- throw new Error('Archiver is not running');
366
+ /**
367
+ * Processes all queued blocks, adding them to the store.
368
+ * Called at the beginning of each sync iteration.
369
+ * Blocks are processed in the order they were queued.
370
+ */
371
+ private async processQueuedBlocks(): Promise<void> {
372
+ if (this.blockQueue.length === 0) {
373
+ return;
374
+ }
375
+
376
+ // Take all blocks from the queue
377
+ const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
378
+ this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
379
+
380
+ // Process each block individually to properly resolve/reject each promise
381
+ for (const { block, resolve, reject } of queuedItems) {
382
+ try {
383
+ await this.store.addBlocks([block]);
384
+ this.log.debug(`Added block ${block.number} to store`);
385
+ resolve();
386
+ } catch (err: any) {
387
+ this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
388
+ reject(err);
389
+ }
272
390
  }
273
- return this.runningPromise.trigger();
274
391
  }
275
392
 
276
393
  public waitForInitialSync() {
277
394
  return this.initialSyncPromise.promise;
278
395
  }
279
396
 
280
- private async syncSafe(initialRun: boolean) {
281
- try {
282
- await this.sync(initialRun);
283
- return true;
284
- } catch (error) {
285
- if (error instanceof NoBlobBodiesFoundError) {
286
- this.log.error(`Error syncing archiver: ${error.message}`);
287
- } else if (error instanceof BlockTagTooOldError) {
288
- this.log.warn(`Re-running archiver sync: ${error.message}`);
289
- } else {
290
- this.log.error('Error during archiver sync', error);
291
- }
292
- return false;
397
+ /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */
398
+ private async testEthereumNodeSynced() {
399
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
400
+ if (maxAllowedDelay === 0) {
401
+ return;
402
+ }
403
+ const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({ includeTransactions: false });
404
+ const currentTime = BigInt(this.dateProvider.nowInSeconds());
405
+ if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
406
+ throw new Error(
407
+ `Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`,
408
+ );
293
409
  }
294
410
  }
295
411
 
296
- /**
297
- * Fetches logs from L1 contracts and processes them.
298
- */
299
- @trackSpan('Archiver.sync', initialRun => ({ [Attributes.INITIAL_SYNC]: initialRun }))
300
- private async sync(initialRun: boolean) {
412
+ private async syncFromL1() {
301
413
  /**
302
414
  * We keep track of three "pointers" to L1 blocks:
303
415
  * 1. the last L1 block that published an L2 block
@@ -307,8 +419,6 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
307
419
  * We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
308
420
  * We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
309
421
  * The archiver will stay back, until there's data on L1 that will move the pointers forward.
310
- *
311
- * This code does not handle reorgs.
312
422
  */
313
423
  const { l1StartBlock, l1StartBlockHash } = this.l1constants;
314
424
  const {
@@ -320,13 +430,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
320
430
  const currentL1BlockNumber = currentL1Block.number;
321
431
  const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
322
432
 
323
- if (initialRun) {
324
- this.log.info(
325
- `Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo}` +
326
- ` to current L1 block ${currentL1BlockNumber} with hash ${currentL1BlockHash.toString()}`,
327
- { blocksSynchedTo, messagesSynchedTo },
328
- );
329
- }
433
+ this.log.trace(`Starting new archiver sync iteration`, {
434
+ blocksSynchedTo,
435
+ messagesSynchedTo,
436
+ currentL1BlockNumber,
437
+ currentL1BlockHash,
438
+ });
330
439
 
331
440
  // ********** Ensuring Consistency of data pulled from L1 **********
332
441
 
@@ -356,28 +465,45 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
356
465
  ? (await this.publicClient.getBlock({ blockNumber: currentL1BlockNumber })).timestamp
357
466
  : this.l1Timestamp;
358
467
 
359
- // ********** Events that are processed per L2 block **********
468
+ // Warn if the latest L1 block timestamp is too old
469
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
470
+ const now = this.dateProvider.nowInSeconds();
471
+ if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
472
+ this.log.warn(
473
+ `Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`,
474
+ { currentL1BlockNumber, currentL1Timestamp, now, maxAllowedDelay },
475
+ );
476
+ }
477
+
478
+ // ********** Events that are processed per checkpoint **********
360
479
  if (currentL1BlockNumber > blocksSynchedTo) {
361
- // First we retrieve new L2 blocks and store them in the DB. This will also update the
362
- // pending chain validation status, proven block number, and synched L1 block number.
363
- const rollupStatus = await this.handleL2blocks(blocksSynchedTo, currentL1BlockNumber);
480
+ // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
481
+ // pending chain validation status, proven checkpoint number, and synched L1 block number.
482
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
364
483
  // Then we prune the current epoch if it'd reorg on next submission.
365
- // Note that we don't do this before retrieving L2 blocks because we may need to retrieve
366
- // blocks from more than 2 epochs ago, so we want to make sure we have the latest view of
484
+ // Note that we don't do this before retrieving checkpoints because we may need to retrieve
485
+ // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
367
486
  // the chain locally before we start unwinding stuff. This can be optimized by figuring out
368
- // up to which point we're pruning, and then requesting L2 blocks up to that point only.
487
+ // up to which point we're pruning, and then requesting checkpoints up to that point only.
369
488
  const { rollupCanPrune } = await this.handleEpochPrune(
370
- rollupStatus.provenBlockNumber,
489
+ rollupStatus.provenCheckpointNumber,
371
490
  currentL1BlockNumber,
372
491
  currentL1Timestamp,
373
492
  );
374
493
 
375
- // And lastly we check if we are missing any L2 blocks behind us due to a possible L1 reorg.
494
+ // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
495
+ // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
496
+ // we get a valid checkpoint to advance the syncpoint.
497
+ if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
498
+ await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
499
+ }
500
+
501
+ // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
376
502
  // We only do this if rollup cant prune on the next submission. Otherwise we will end up
377
- // re-syncing the blocks we have just unwound above. We also dont do this if the last block is invalid,
503
+ // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
378
504
  // since the archiver will rightfully refuse to sync up to it.
379
505
  if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
380
- await this.checkForNewBlocksBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
506
+ await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
381
507
  }
382
508
 
383
509
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
@@ -388,18 +514,32 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
388
514
  // but the corresponding blocks have not been processed (see #12631).
389
515
  this.l1Timestamp = currentL1Timestamp;
390
516
  this.l1BlockNumber = currentL1BlockNumber;
391
- this.initialSyncComplete = true;
392
- this.initialSyncPromise.resolve();
393
517
 
394
- if (initialRun) {
395
- this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete.`, {
518
+ // We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
519
+ // so if the initial sync took too long, we still go for another iteration.
520
+ if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= (await this.publicClient.getBlockNumber())) {
521
+ this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
396
522
  l1BlockNumber: currentL1BlockNumber,
397
523
  syncPoint: await this.store.getSynchPoint(),
398
524
  ...(await this.getL2Tips()),
399
525
  });
526
+ this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
527
+ this.initialSyncComplete = true;
528
+ this.initialSyncPromise.resolve();
400
529
  }
401
530
  }
402
531
 
532
+ /**
533
+ * Fetches logs from L1 contracts and processes them.
534
+ */
535
+ @trackSpan('Archiver.sync')
536
+ private async sync() {
537
+ // Process any queued blocks first, before doing L1 sync
538
+ await this.processQueuedBlocks();
539
+ // Now perform L1 sync
540
+ await this.syncFromL1();
541
+ }
542
+
403
543
  /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */
404
544
  private async canPrune(currentL1BlockNumber: bigint, currentL1Timestamp: bigint) {
405
545
  const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
@@ -414,27 +554,44 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
414
554
  return result;
415
555
  }
416
556
 
417
- /** Checks if there'd be a reorg for the next block submission and start pruning now. */
418
- private async handleEpochPrune(provenBlockNumber: number, currentL1BlockNumber: bigint, currentL1Timestamp: bigint) {
557
+ /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */
558
+ private async handleEpochPrune(
559
+ provenCheckpointNumber: CheckpointNumber,
560
+ currentL1BlockNumber: bigint,
561
+ currentL1Timestamp: bigint,
562
+ ) {
419
563
  const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
420
- const localPendingBlockNumber = await this.getBlockNumber();
421
- const canPrune = localPendingBlockNumber > provenBlockNumber && rollupCanPrune;
564
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
565
+ const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
422
566
 
423
567
  if (canPrune) {
424
568
  const timer = new Timer();
425
- const pruneFrom = provenBlockNumber + 1;
569
+ const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
426
570
 
427
- const header = await this.getBlockHeader(Number(pruneFrom));
571
+ const header = await this.getCheckpointHeader(pruneFrom);
428
572
  if (header === undefined) {
429
- throw new Error(`Missing block header ${pruneFrom}`);
573
+ throw new Error(`Missing checkpoint header ${pruneFrom}`);
430
574
  }
431
575
 
432
- const pruneFromSlotNumber = header.globalVariables.slotNumber.toBigInt();
433
- const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
576
+ const pruneFromSlotNumber = header.slotNumber;
577
+ const pruneFromEpochNumber: EpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
578
+
579
+ const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
580
+
581
+ const checkpointPromises = Array.from({ length: checkpointsToUnwind })
582
+ .fill(0)
583
+ .map((_, i) => this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
584
+ const checkpoints = await Promise.all(checkpointPromises);
434
585
 
435
- const blocksToUnwind = localPendingBlockNumber - provenBlockNumber;
586
+ const blockPromises = await Promise.all(
587
+ checkpoints
588
+ .filter(isDefined)
589
+ .map(cp => this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))),
590
+ );
591
+ const newBlocks = blockPromises.filter(isDefined).flat();
436
592
 
437
- const blocks = await this.getBlocks(Number(provenBlockNumber) + 1, Number(blocksToUnwind));
593
+ // TODO(pw/mbps): Don't convert to legacy blocks here
594
+ const blocks: L2Block[] = (await Promise.all(newBlocks.map(x => this.getBlock(x.number)))).filter(isDefined);
438
595
 
439
596
  // Emit an event for listening services to react to the chain prune
440
597
  this.emit(L2BlockSourceEvents.L2PruneDetected, {
@@ -444,13 +601,13 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
444
601
  });
445
602
 
446
603
  this.log.debug(
447
- `L2 prune from ${provenBlockNumber + 1} to ${localPendingBlockNumber} will occur on next block submission.`,
604
+ `L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`,
448
605
  );
449
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
606
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
450
607
  this.log.warn(
451
- `Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` +
452
- `to ${provenBlockNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
453
- `Updated L2 latest block is ${await this.getBlockNumber()}.`,
608
+ `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
609
+ `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
610
+ `Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
454
611
  );
455
612
  this.instrumentation.processPrune(timer.ms());
456
613
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
@@ -497,7 +654,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
497
654
  remoteMessagesState.totalMessagesInserted === localMessagesInserted &&
498
655
  remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
499
656
  ) {
500
- this.log.debug(
657
+ this.log.trace(
501
658
  `No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`,
502
659
  );
503
660
  return;
@@ -551,7 +708,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
551
708
  // Log stats for messages retrieved (if any).
552
709
  if (messageCount > 0) {
553
710
  this.log.info(
554
- `Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for L2 block ${lastMessage?.l2BlockNumber}`,
711
+ `Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`,
555
712
  { lastMessage, messageCount },
556
713
  );
557
714
  }
@@ -629,200 +786,230 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
629
786
  return Buffer32.fromString(block.hash);
630
787
  }
631
788
 
632
- private async handleL2blocks(blocksSynchedTo: bigint, currentL1BlockNumber: bigint) {
633
- const localPendingBlockNumber = await this.getBlockNumber();
789
+ private async handleCheckpoints(blocksSynchedTo: bigint, currentL1BlockNumber: bigint): Promise<RollupStatus> {
790
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
634
791
  const initialValidationResult: ValidateBlockResult | undefined = await this.store.getPendingChainValidationStatus();
635
- const [provenBlockNumber, provenArchive, pendingBlockNumber, pendingArchive, archiveForLocalPendingBlockNumber] =
636
- await this.rollup.status(BigInt(localPendingBlockNumber), { blockNumber: currentL1BlockNumber });
792
+ const [
793
+ rollupProvenCheckpointNumber,
794
+ provenArchive,
795
+ rollupPendingCheckpointNumber,
796
+ pendingArchive,
797
+ archiveForLocalPendingCheckpointNumber,
798
+ ] = await this.rollup.status(localPendingCheckpointNumber, { blockNumber: currentL1BlockNumber });
799
+ const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
800
+ const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
637
801
  const rollupStatus = {
638
- provenBlockNumber: Number(provenBlockNumber),
802
+ provenCheckpointNumber,
639
803
  provenArchive,
640
- pendingBlockNumber: Number(pendingBlockNumber),
804
+ pendingCheckpointNumber,
641
805
  pendingArchive,
642
806
  validationResult: initialValidationResult,
643
807
  };
644
808
  this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
645
- localPendingBlockNumber,
809
+ localPendingCheckpointNumber,
646
810
  blocksSynchedTo,
647
811
  currentL1BlockNumber,
648
- archiveForLocalPendingBlockNumber,
812
+ archiveForLocalPendingCheckpointNumber,
649
813
  ...rollupStatus,
650
814
  });
651
815
 
652
- const updateProvenBlock = async () => {
653
- // Annoying edge case: if proven block is moved back to 0 due to a reorg at the beginning of the chain,
654
- // we need to set it to zero. This is an edge case because we dont have a block zero (initial block is one),
655
- // so localBlockForDestinationProvenBlockNumber would not be found below.
656
- if (provenBlockNumber === 0n) {
657
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
658
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
659
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
660
- this.log.info(`Rolled back proven chain to block ${provenBlockNumber}`, { provenBlockNumber });
816
+ const updateProvenCheckpoint = async () => {
817
+ // Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
818
+ // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
819
+ // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
820
+ if (provenCheckpointNumber === 0) {
821
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
822
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
823
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
824
+ this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
661
825
  }
662
826
  }
663
827
 
664
- const localBlockForDestinationProvenBlockNumber = await this.getBlock(Number(provenBlockNumber));
828
+ const localCheckpointForDestinationProvenCheckpointNumber =
829
+ await this.store.getCheckpointData(provenCheckpointNumber);
665
830
 
666
- // Sanity check. I've hit what seems to be a state where the proven block is set to a value greater than the latest
667
- // synched block when requesting L2Tips from the archiver. This is the only place where the proven block is set.
668
- const synched = await this.store.getSynchedL2BlockNumber();
669
- if (localBlockForDestinationProvenBlockNumber && synched < localBlockForDestinationProvenBlockNumber?.number) {
831
+ // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
832
+ // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
833
+ const synched = await this.getSynchedCheckpointNumber();
834
+ if (
835
+ localCheckpointForDestinationProvenCheckpointNumber &&
836
+ synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber
837
+ ) {
670
838
  this.log.error(
671
- `Hit local block greater than last synched block: ${localBlockForDestinationProvenBlockNumber.number} > ${synched}`,
839
+ `Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`,
672
840
  );
673
841
  }
674
842
 
675
843
  this.log.trace(
676
- `Local block for remote proven block ${provenBlockNumber} is ${
677
- localBlockForDestinationProvenBlockNumber?.archive.root.toString() ?? 'undefined'
844
+ `Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${
845
+ localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'
678
846
  }`,
679
847
  );
680
848
 
681
849
  if (
682
- localBlockForDestinationProvenBlockNumber &&
683
- provenArchive === localBlockForDestinationProvenBlockNumber.archive.root.toString()
850
+ localCheckpointForDestinationProvenCheckpointNumber &&
851
+ provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()
684
852
  ) {
685
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
686
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
687
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
688
- this.log.info(`Updated proven chain to block ${provenBlockNumber}`, {
689
- provenBlockNumber,
853
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
854
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
855
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
856
+ this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
857
+ provenCheckpointNumber,
690
858
  });
691
- const provenSlotNumber =
692
- localBlockForDestinationProvenBlockNumber.header.globalVariables.slotNumber.toBigInt();
693
- const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
859
+ const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
860
+ const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
861
+ const lastBlockNumberInCheckpoint =
862
+ localCheckpointForDestinationProvenCheckpointNumber.startBlock +
863
+ localCheckpointForDestinationProvenCheckpointNumber.numBlocks -
864
+ 1;
865
+
694
866
  this.emit(L2BlockSourceEvents.L2BlockProven, {
695
867
  type: L2BlockSourceEvents.L2BlockProven,
696
- blockNumber: provenBlockNumber,
868
+ blockNumber: BlockNumber(lastBlockNumberInCheckpoint),
697
869
  slotNumber: provenSlotNumber,
698
870
  epochNumber: provenEpochNumber,
699
871
  });
872
+ this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
700
873
  } else {
701
- this.log.trace(`Proven block ${provenBlockNumber} already stored.`);
874
+ this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
702
875
  }
703
876
  }
704
- this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber));
705
877
  };
706
878
 
707
- // This is an edge case that we only hit if there are no proposed blocks.
708
- // If we have 0 blocks locally and there are no blocks onchain there is nothing to do.
709
- const noBlocks = localPendingBlockNumber === 0 && pendingBlockNumber === 0n;
710
- if (noBlocks) {
711
- await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
879
+ // This is an edge case that we only hit if there are no proposed checkpoints.
880
+ // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
881
+ const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
882
+ if (noCheckpoints) {
883
+ await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
712
884
  this.log.debug(
713
- `No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no blocks on chain`,
885
+ `No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`,
714
886
  );
715
887
  return rollupStatus;
716
888
  }
717
889
 
718
- await updateProvenBlock();
890
+ await updateProvenCheckpoint();
719
891
 
720
892
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
721
- // are any state that could be impacted by it. If we have no blocks, there is no impact.
722
- if (localPendingBlockNumber > 0) {
723
- const localPendingBlock = await this.getBlock(localPendingBlockNumber);
724
- if (localPendingBlock === undefined) {
725
- throw new Error(`Missing block ${localPendingBlockNumber}`);
893
+ // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
894
+ if (localPendingCheckpointNumber > 0) {
895
+ const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
896
+ if (localPendingCheckpoint === undefined) {
897
+ throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
726
898
  }
727
899
 
728
- const localPendingArchiveRoot = localPendingBlock.archive.root.toString();
729
- const noBlockSinceLast = localPendingBlock && pendingArchive === localPendingArchiveRoot;
730
- if (noBlockSinceLast) {
900
+ const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
901
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
902
+ if (noCheckpointSinceLast) {
731
903
  // We believe the following line causes a problem when we encounter L1 re-orgs.
732
904
  // Basically, by setting the synched L1 block number here, we are saying that we have
733
- // processed all blocks up to the current L1 block number and we will not attempt to retrieve logs from
905
+ // processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
734
906
  // this block again (or any blocks before).
735
- // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing blocks
907
+ // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
736
908
  // We must only set this block number based on actually retrieved logs.
737
909
  // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
738
910
  // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
739
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
911
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
740
912
  return rollupStatus;
741
913
  }
742
914
 
743
- const localPendingBlockInChain = archiveForLocalPendingBlockNumber === localPendingArchiveRoot;
744
- if (!localPendingBlockInChain) {
745
- // If our local pending block tip is not in the chain on L1 a "prune" must have happened
915
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
916
+ if (!localPendingCheckpointInChain) {
917
+ // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
746
918
  // or the L1 have reorged.
747
919
  // In any case, we have to figure out how far into the past the action will take us.
748
- // For simplicity here, we will simply rewind until we end in a block that is also on the chain on L1.
920
+ // For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
749
921
  this.log.debug(
750
- `L2 prune has been detected due to local pending block ${localPendingBlockNumber} not in chain`,
751
- { localPendingBlockNumber, localPendingArchiveRoot, archiveForLocalPendingBlockNumber },
922
+ `L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`,
923
+ { localPendingCheckpointNumber, localPendingArchiveRoot, archiveForLocalPendingCheckpointNumber },
752
924
  );
753
925
 
754
- let tipAfterUnwind = localPendingBlockNumber;
926
+ let tipAfterUnwind = localPendingCheckpointNumber;
755
927
  while (true) {
756
- const candidateBlock = await this.getBlock(Number(tipAfterUnwind));
757
- if (candidateBlock === undefined) {
928
+ const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
929
+ if (candidateCheckpoint === undefined) {
758
930
  break;
759
931
  }
760
932
 
761
- const archiveAtContract = await this.rollup.archiveAt(BigInt(candidateBlock.number));
762
-
763
- if (archiveAtContract === candidateBlock.archive.root.toString()) {
933
+ const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.checkpointNumber);
934
+ this.log.trace(
935
+ `Checking local checkpoint ${candidateCheckpoint.checkpointNumber} with archive ${candidateCheckpoint.archive.root}`,
936
+ {
937
+ archiveAtContract,
938
+ archiveLocal: candidateCheckpoint.archive.root.toString(),
939
+ },
940
+ );
941
+ if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
764
942
  break;
765
943
  }
766
944
  tipAfterUnwind--;
767
945
  }
768
946
 
769
- const blocksToUnwind = localPendingBlockNumber - tipAfterUnwind;
770
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
947
+ const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
948
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
771
949
 
772
950
  this.log.warn(
773
- `Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` +
774
- `due to mismatched block hashes at L1 block ${currentL1BlockNumber}. ` +
775
- `Updated L2 latest block is ${await this.getBlockNumber()}.`,
951
+ `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
952
+ `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` +
953
+ `Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
776
954
  );
777
955
  }
778
956
  }
779
957
 
780
- // Retrieve L2 blocks in batches. Each batch is estimated to accommodate up to L2 'blockBatchSize' blocks,
958
+ // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
781
959
  // computed using the L2 block time vs the L1 block time.
782
960
  let searchStartBlock: bigint = blocksSynchedTo;
783
961
  let searchEndBlock: bigint = blocksSynchedTo;
784
- let lastRetrievedBlock: PublishedL2Block | undefined;
962
+ let lastRetrievedCheckpoint: PublishedCheckpoint | undefined;
963
+ let lastL1BlockWithCheckpoint: bigint | undefined = undefined;
785
964
 
786
965
  do {
787
966
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
788
967
 
789
- this.log.trace(`Retrieving L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
968
+ this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
790
969
 
791
- // TODO(md): Retrieve from blob sink then from consensus client, then from peers
792
- const retrievedBlocks = await retrieveBlocksFromRollup(
970
+ // TODO(md): Retrieve from blob client then from consensus client, then from peers
971
+ const retrievedCheckpoints = await retrieveCheckpointsFromRollup(
793
972
  this.rollup.getContract() as GetContractReturnType<typeof RollupAbi, ViemPublicClient>,
794
973
  this.publicClient,
795
- this.blobSinkClient,
974
+ this.debugClient,
975
+ this.blobClient,
796
976
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
797
977
  searchEndBlock,
978
+ this.l1Addresses,
979
+ this.instrumentation,
798
980
  this.log,
981
+ !this.initialSyncComplete, // isHistoricalSync
799
982
  );
800
983
 
801
- if (retrievedBlocks.length === 0) {
984
+ if (retrievedCheckpoints.length === 0) {
802
985
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
803
986
  // See further details in earlier comments.
804
- this.log.trace(`Retrieved no new L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
987
+ this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
805
988
  continue;
806
989
  }
807
990
 
808
- const lastProcessedL1BlockNumber = retrievedBlocks[retrievedBlocks.length - 1].l1.blockNumber;
809
991
  this.log.debug(
810
- `Retrieved ${retrievedBlocks.length} new L2 blocks between L1 blocks ${searchStartBlock} and ${searchEndBlock} with last processed L1 block ${lastProcessedL1BlockNumber}.`,
992
+ `Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
993
+ {
994
+ lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
995
+ searchStartBlock,
996
+ searchEndBlock,
997
+ },
811
998
  );
812
999
 
813
- const publishedBlocks = await Promise.all(retrievedBlocks.map(b => retrievedBlockToPublishedL2Block(b)));
814
- const validBlocks: PublishedL2Block[] = [];
1000
+ const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map(b => retrievedToPublishedCheckpoint(b)));
1001
+ const validCheckpoints: PublishedCheckpoint[] = [];
815
1002
 
816
- for (const block of publishedBlocks) {
1003
+ for (const published of publishedCheckpoints) {
817
1004
  const validationResult = this.config.skipValidateBlockAttestations
818
1005
  ? { valid: true as const }
819
- : await validateBlockAttestations(block, this.epochCache, this.l1constants, this.log);
1006
+ : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
820
1007
 
821
- // Only update the validation result if it has changed, so we can keep track of the first invalid block
822
- // in case there is a sequence of more than one invalid block, as we need to invalidate the first one.
823
- // There is an exception though: if an invalid block is invalidated and replaced with another invalid block,
1008
+ // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
1009
+ // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
1010
+ // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
824
1011
  // we need to update the validation result, since we need to be able to invalidate the new one.
825
- // See test 'chain progresses if an invalid block is invalidated with an invalid one' for more info.
1012
+ // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
826
1013
  if (
827
1014
  rollupStatus.validationResult?.valid !== validationResult.valid ||
828
1015
  (!rollupStatus.validationResult.valid &&
@@ -833,9 +1020,9 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
833
1020
  }
834
1021
 
835
1022
  if (!validationResult.valid) {
836
- this.log.warn(`Skipping block ${block.block.number} due to invalid attestations`, {
837
- blockHash: block.block.hash(),
838
- l1BlockNumber: block.l1.blockNumber,
1023
+ this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
1024
+ checkpointHash: published.checkpoint.hash(),
1025
+ l1BlockNumber: published.l1.blockNumber,
839
1026
  ...pick(validationResult, 'reason'),
840
1027
  });
841
1028
 
@@ -845,43 +1032,64 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
845
1032
  validationResult,
846
1033
  });
847
1034
 
848
- // We keep consuming blocks if we find an invalid one, since we do not listen for BlockInvalidated events
849
- // We just pretend the invalid ones are not there and keep consuming the next blocks
850
- // Note that this breaks if the committee ever attests to a descendant of an invalid block
1035
+ // We keep consuming checkpoints if we find an invalid one, since we do not listen for CheckpointInvalidated events
1036
+ // We just pretend the invalid ones are not there and keep consuming the next checkpoints
1037
+ // Note that this breaks if the committee ever attests to a descendant of an invalid checkpoint
851
1038
  continue;
852
1039
  }
853
1040
 
854
- validBlocks.push(block);
855
- this.log.debug(`Ingesting new L2 block ${block.block.number} with ${block.block.body.txEffects.length} txs`, {
856
- blockHash: block.block.hash(),
857
- l1BlockNumber: block.l1.blockNumber,
858
- ...block.block.header.globalVariables.toInspect(),
859
- ...block.block.getStats(),
860
- });
1041
+ // Check the inHash of the checkpoint against the l1->l2 messages.
1042
+ // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
1043
+ // checkpoints we just retrieved.
1044
+ const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
1045
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
1046
+ const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
1047
+ if (!computedInHash.equals(publishedInHash)) {
1048
+ this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
1049
+ checkpointHash: published.checkpoint.hash(),
1050
+ l1BlockNumber: published.l1.blockNumber,
1051
+ computedInHash,
1052
+ publishedInHash,
1053
+ });
1054
+ // Throwing an error since this is most likely caused by a bug.
1055
+ throw new Error(
1056
+ `Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`,
1057
+ );
1058
+ }
1059
+
1060
+ validCheckpoints.push(published);
1061
+ this.log.debug(
1062
+ `Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`,
1063
+ {
1064
+ checkpointHash: published.checkpoint.hash(),
1065
+ l1BlockNumber: published.l1.blockNumber,
1066
+ ...published.checkpoint.header.toInspect(),
1067
+ blocks: published.checkpoint.blocks.map(b => b.getStats()),
1068
+ },
1069
+ );
861
1070
  }
862
1071
 
863
1072
  try {
864
1073
  const updatedValidationResult =
865
1074
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
866
- const [processDuration] = await elapsed(() => this.store.addBlocks(validBlocks, updatedValidationResult));
1075
+ const [processDuration] = await elapsed(() => this.addCheckpoints(validCheckpoints, updatedValidationResult));
867
1076
  this.instrumentation.processNewBlocks(
868
- processDuration / validBlocks.length,
869
- validBlocks.map(b => b.block),
1077
+ processDuration / validCheckpoints.length,
1078
+ validCheckpoints.flatMap(c => c.checkpoint.blocks),
870
1079
  );
871
1080
  } catch (err) {
872
- if (err instanceof InitialBlockNumberNotSequentialError) {
873
- const { previousBlockNumber, newBlockNumber } = err;
874
- const previousBlock = previousBlockNumber
875
- ? await this.store.getPublishedBlock(previousBlockNumber)
1081
+ if (err instanceof InitialCheckpointNumberNotSequentialError) {
1082
+ const { previousCheckpointNumber, newCheckpointNumber } = err;
1083
+ const previousCheckpoint = previousCheckpointNumber
1084
+ ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber))
876
1085
  : undefined;
877
- const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
1086
+ const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1constants.l1StartBlock;
878
1087
  await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
879
1088
  this.log.warn(
880
- `Attempting to insert block ${newBlockNumber} with previous block ${previousBlockNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`,
1089
+ `Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`,
881
1090
  {
882
- previousBlockNumber,
883
- previousBlockHash: await previousBlock?.block.hash(),
884
- newBlockNumber,
1091
+ previousCheckpointNumber,
1092
+ newCheckpointNumber,
885
1093
  updatedL1SyncPoint,
886
1094
  },
887
1095
  );
@@ -889,56 +1097,59 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
889
1097
  throw err;
890
1098
  }
891
1099
 
892
- for (const block of validBlocks) {
893
- this.log.info(`Downloaded L2 block ${block.block.number}`, {
894
- blockHash: await block.block.hash(),
895
- blockNumber: block.block.number,
896
- txCount: block.block.body.txEffects.length,
897
- globalVariables: block.block.header.globalVariables.toInspect(),
898
- archiveRoot: block.block.archive.root.toString(),
899
- archiveNextLeafIndex: block.block.archive.nextAvailableLeafIndex,
1100
+ for (const checkpoint of validCheckpoints) {
1101
+ this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
1102
+ checkpointHash: checkpoint.checkpoint.hash(),
1103
+ checkpointNumber: checkpoint.checkpoint.number,
1104
+ blockCount: checkpoint.checkpoint.blocks.length,
1105
+ txCount: checkpoint.checkpoint.blocks.reduce((acc, b) => acc + b.body.txEffects.length, 0),
1106
+ header: checkpoint.checkpoint.header.toInspect(),
1107
+ archiveRoot: checkpoint.checkpoint.archive.root.toString(),
1108
+ archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex,
900
1109
  });
901
1110
  }
902
- lastRetrievedBlock = validBlocks.at(-1) ?? lastRetrievedBlock;
1111
+ lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
1112
+ lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
903
1113
  } while (searchEndBlock < currentL1BlockNumber);
904
1114
 
905
1115
  // Important that we update AFTER inserting the blocks.
906
- await updateProvenBlock();
1116
+ await updateProvenCheckpoint();
907
1117
 
908
- return { ...rollupStatus, lastRetrievedBlock };
1118
+ return { ...rollupStatus, lastRetrievedCheckpoint, lastL1BlockWithCheckpoint };
909
1119
  }
910
1120
 
911
- private async checkForNewBlocksBeforeL1SyncPoint(
912
- status: {
913
- lastRetrievedBlock?: PublishedL2Block;
914
- pendingBlockNumber: number;
915
- },
1121
+ private async checkForNewCheckpointsBeforeL1SyncPoint(
1122
+ status: RollupStatus,
916
1123
  blocksSynchedTo: bigint,
917
1124
  currentL1BlockNumber: bigint,
918
1125
  ) {
919
- const { lastRetrievedBlock, pendingBlockNumber } = status;
920
- // Compare the last L2 block we have (either retrieved in this round or loaded from store) with what the
1126
+ const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
1127
+ // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
921
1128
  // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
922
- const latestLocalL2BlockNumber = lastRetrievedBlock?.block.number ?? (await this.store.getSynchedL2BlockNumber());
923
- if (latestLocalL2BlockNumber < pendingBlockNumber) {
1129
+ const latestLocalCheckpointNumber =
1130
+ lastRetrievedCheckpoint?.checkpoint.number ?? (await this.getSynchedCheckpointNumber());
1131
+ if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
924
1132
  // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
925
- // but still havent reached the pending block according to the call to the rollup contract.
926
- // We suspect an L1 reorg that added blocks *behind* us. If that is the case, it must have happened between the
927
- // last L2 block we saw and the current one, so we reset the last synched L1 block number. In the edge case we
928
- // don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
929
- const latestLocalL2Block =
930
- lastRetrievedBlock ??
931
- (latestLocalL2BlockNumber > 0
932
- ? await this.store.getPublishedBlocks(latestLocalL2BlockNumber, 1).then(([b]) => b)
933
- : undefined);
934
- const targetL1BlockNumber = latestLocalL2Block?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
935
- const latestLocalL2BlockArchive = latestLocalL2Block?.block.archive.root.toString();
1133
+ // but still haven't reached the pending checkpoint according to the call to the rollup contract.
1134
+ // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
1135
+ // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
1136
+ // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
1137
+ let latestLocalCheckpointArchive: string | undefined = undefined;
1138
+ let targetL1BlockNumber = maxBigint(currentL1BlockNumber - 64n, 0n);
1139
+ if (lastRetrievedCheckpoint) {
1140
+ latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
1141
+ targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
1142
+ } else if (latestLocalCheckpointNumber > 0) {
1143
+ const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c]) => c);
1144
+ latestLocalCheckpointArchive = checkpoint.archive.root.toString();
1145
+ targetL1BlockNumber = checkpoint.l1.blockNumber;
1146
+ }
936
1147
  this.log.warn(
937
- `Failed to reach L2 block ${pendingBlockNumber} at ${currentL1BlockNumber} (latest is ${latestLocalL2BlockNumber}). ` +
1148
+ `Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` +
938
1149
  `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`,
939
1150
  {
940
- latestLocalL2BlockNumber,
941
- latestLocalL2BlockArchive,
1151
+ latestLocalCheckpointNumber,
1152
+ latestLocalCheckpointArchive,
942
1153
  blocksSynchedTo,
943
1154
  currentL1BlockNumber,
944
1155
  ...status,
@@ -946,18 +1157,15 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
946
1157
  );
947
1158
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
948
1159
  } else {
949
- this.log.trace(`No new blocks behind L1 sync point to retrieve.`, {
950
- latestLocalL2BlockNumber,
951
- pendingBlockNumber,
1160
+ this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
1161
+ latestLocalCheckpointNumber,
1162
+ pendingCheckpointNumber,
952
1163
  });
953
1164
  }
954
1165
  }
955
1166
 
956
1167
  /** Resumes the archiver after a stop. */
957
1168
  public resume() {
958
- if (!this.runningPromise) {
959
- throw new Error(`Archiver was never started`);
960
- }
961
1169
  if (this.runningPromise.isRunning()) {
962
1170
  this.log.warn(`Archiver already running`);
963
1171
  }
@@ -971,7 +1179,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
971
1179
  */
972
1180
  public async stop(): Promise<void> {
973
1181
  this.log.debug('Stopping...');
974
- await this.runningPromise?.stop();
1182
+ await this.runningPromise.stop();
975
1183
 
976
1184
  this.log.info('Stopped.');
977
1185
  return Promise.resolve();
@@ -1005,58 +1213,71 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1005
1213
  return Promise.resolve(this.l1Timestamp);
1006
1214
  }
1007
1215
 
1008
- public getL2SlotNumber(): Promise<bigint | undefined> {
1216
+ public getL2SlotNumber(): Promise<SlotNumber | undefined> {
1009
1217
  return Promise.resolve(
1010
1218
  this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants),
1011
1219
  );
1012
1220
  }
1013
1221
 
1014
- public getL2EpochNumber(): Promise<bigint | undefined> {
1222
+ public getL2EpochNumber(): Promise<EpochNumber | undefined> {
1015
1223
  return Promise.resolve(
1016
1224
  this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants),
1017
1225
  );
1018
1226
  }
1019
1227
 
1020
- public async getBlocksForEpoch(epochNumber: bigint): Promise<L2Block[]> {
1228
+ public async getBlocksForEpoch(epochNumber: EpochNumber): Promise<L2Block[]> {
1021
1229
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1022
1230
  const blocks: L2Block[] = [];
1023
1231
 
1024
- // Walk the list of blocks backwards and filter by slots matching the requested epoch.
1025
- // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
1026
- let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
1027
- const slot = (b: L2Block) => b.header.globalVariables.slotNumber.toBigInt();
1028
- while (block && slot(block) >= start) {
1029
- if (slot(block) <= end) {
1030
- blocks.push(block);
1232
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1233
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1234
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1235
+ const slot = (b: CheckpointData) => b.header.slotNumber;
1236
+ while (checkpoint && slot(checkpoint) >= start) {
1237
+ if (slot(checkpoint) <= end) {
1238
+ // push the blocks on backwards
1239
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
1240
+ for (let i = endBlock; i >= checkpoint.startBlock; i--) {
1241
+ const block = await this.getBlock(BlockNumber(i));
1242
+ if (block) {
1243
+ blocks.push(block);
1244
+ }
1245
+ }
1031
1246
  }
1032
- block = await this.getBlock(block.number - 1);
1247
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
1033
1248
  }
1034
1249
 
1035
1250
  return blocks.reverse();
1036
1251
  }
1037
1252
 
1038
- public async getBlockHeadersForEpoch(epochNumber: bigint): Promise<BlockHeader[]> {
1253
+ public async getBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
1039
1254
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1040
1255
  const blocks: BlockHeader[] = [];
1041
1256
 
1042
- // Walk the list of blocks backwards and filter by slots matching the requested epoch.
1043
- // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
1044
- let number = await this.store.getSynchedL2BlockNumber();
1045
- let header = await this.getBlockHeader(number);
1046
- const slot = (b: BlockHeader) => b.globalVariables.slotNumber.toBigInt();
1047
- while (header && slot(header) >= start) {
1048
- if (slot(header) <= end) {
1049
- blocks.push(header);
1257
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1258
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1259
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1260
+ const slot = (b: CheckpointData) => b.header.slotNumber;
1261
+ while (checkpoint && slot(checkpoint) >= start) {
1262
+ if (slot(checkpoint) <= end) {
1263
+ // push the blocks on backwards
1264
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
1265
+ for (let i = endBlock; i >= checkpoint.startBlock; i--) {
1266
+ const block = await this.getBlockHeader(BlockNumber(i));
1267
+ if (block) {
1268
+ blocks.push(block);
1269
+ }
1270
+ }
1050
1271
  }
1051
- header = await this.getBlockHeader(--number);
1272
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
1052
1273
  }
1053
1274
  return blocks.reverse();
1054
1275
  }
1055
1276
 
1056
- public async isEpochComplete(epochNumber: bigint): Promise<boolean> {
1277
+ public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
1057
1278
  // The epoch is complete if the current L2 block is the last one in the epoch (or later)
1058
1279
  const header = await this.getBlockHeader('latest');
1059
- const slot = header?.globalVariables.slotNumber.toBigInt();
1280
+ const slot = header ? header.globalVariables.slotNumber : undefined;
1060
1281
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1061
1282
  if (slot && slot >= endSlot) {
1062
1283
  return true;
@@ -1086,31 +1307,53 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1086
1307
  return this.initialSyncComplete;
1087
1308
  }
1088
1309
 
1089
- /**
1090
- * Gets up to `limit` amount of L2 blocks starting from `from`.
1091
- * @param from - Number of the first block to return (inclusive).
1092
- * @param limit - The number of blocks to return.
1093
- * @param proven - If true, only return blocks that have been proven.
1094
- * @returns The requested L2 blocks.
1095
- */
1096
- public getBlocks(from: number, limit: number, proven?: boolean): Promise<L2Block[]> {
1097
- return this.getPublishedBlocks(from, limit, proven).then(blocks => blocks.map(b => b.block));
1310
+ public async getCheckpointHeader(number: CheckpointNumber | 'latest'): Promise<CheckpointHeader | undefined> {
1311
+ if (number === 'latest') {
1312
+ number = await this.getSynchedCheckpointNumber();
1313
+ }
1314
+ if (number === 0) {
1315
+ return undefined;
1316
+ }
1317
+ const checkpoint = await this.store.getCheckpointData(number);
1318
+ if (!checkpoint) {
1319
+ return undefined;
1320
+ }
1321
+ return checkpoint.header;
1098
1322
  }
1099
1323
 
1100
- /** Equivalent to getBlocks but includes publish data. */
1101
- public async getPublishedBlocks(from: number, limit: number, proven?: boolean): Promise<PublishedL2Block[]> {
1102
- const limitWithProven = proven
1103
- ? Math.min(limit, Math.max((await this.store.getProvenL2BlockNumber()) - from + 1, 0))
1104
- : limit;
1105
- return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
1324
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
1325
+ return this.getSynchedCheckpointNumber();
1106
1326
  }
1107
1327
 
1108
- public getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
1109
- return this.store.getPublishedBlockByHash(blockHash);
1328
+ public getSynchedCheckpointNumber(): Promise<CheckpointNumber> {
1329
+ return this.store.getSynchedCheckpointNumber();
1110
1330
  }
1111
1331
 
1112
- public getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
1113
- return this.store.getPublishedBlockByArchive(archive);
1332
+ public getProvenCheckpointNumber(): Promise<CheckpointNumber> {
1333
+ return this.store.getProvenCheckpointNumber();
1334
+ }
1335
+
1336
+ public setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
1337
+ return this.store.setProvenCheckpointNumber(checkpointNumber);
1338
+ }
1339
+
1340
+ public unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number): Promise<boolean> {
1341
+ return this.store.unwindCheckpoints(from, checkpointsToUnwind);
1342
+ }
1343
+
1344
+ public async getLastBlockNumberInCheckpoint(checkpointNumber: CheckpointNumber): Promise<BlockNumber | undefined> {
1345
+ const checkpointData = await this.store.getCheckpointData(checkpointNumber);
1346
+ if (!checkpointData) {
1347
+ return undefined;
1348
+ }
1349
+ return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
1350
+ }
1351
+
1352
+ public addCheckpoints(
1353
+ checkpoints: PublishedCheckpoint[],
1354
+ pendingChainValidationStatus?: ValidateBlockResult,
1355
+ ): Promise<boolean> {
1356
+ return this.store.addCheckpoints(checkpoints, pendingChainValidationStatus);
1114
1357
  }
1115
1358
 
1116
1359
  public getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
@@ -1126,7 +1369,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1126
1369
  * @param number - The block number to return.
1127
1370
  * @returns The requested L2 block.
1128
1371
  */
1129
- public async getBlock(number: number): Promise<L2Block | undefined> {
1372
+ public async getL2BlockNew(number: BlockNumber): Promise<L2BlockNew | undefined> {
1130
1373
  // If the number provided is -ve, then return the latest block.
1131
1374
  if (number < 0) {
1132
1375
  number = await this.store.getSynchedL2BlockNumber();
@@ -1134,11 +1377,11 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1134
1377
  if (number === 0) {
1135
1378
  return undefined;
1136
1379
  }
1137
- const publishedBlock = await this.store.getPublishedBlock(number);
1138
- return publishedBlock?.block;
1380
+ const publishedBlock = await this.store.store.getBlock(number);
1381
+ return publishedBlock;
1139
1382
  }
1140
1383
 
1141
- public async getBlockHeader(number: number | 'latest'): Promise<BlockHeader | undefined> {
1384
+ public async getBlockHeader(number: BlockNumber | 'latest'): Promise<BlockHeader | undefined> {
1142
1385
  if (number === 'latest') {
1143
1386
  number = await this.store.getSynchedL2BlockNumber();
1144
1387
  }
@@ -1149,6 +1392,21 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1149
1392
  return headers.length === 0 ? undefined : headers[0];
1150
1393
  }
1151
1394
 
1395
+ getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
1396
+ return this.store.getCheckpointedBlock(number);
1397
+ }
1398
+
1399
+ getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
1400
+ return this.store.getCheckpointedBlockByHash(blockHash);
1401
+ }
1402
+
1403
+ getProvenBlockNumber(): Promise<BlockNumber> {
1404
+ return this.store.getProvenBlockNumber();
1405
+ }
1406
+ getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
1407
+ return this.store.getCheckpointedBlockByArchive(archive);
1408
+ }
1409
+
1152
1410
  public getTxEffect(txHash: TxHash) {
1153
1411
  return this.store.getTxEffect(txHash);
1154
1412
  }
@@ -1157,24 +1415,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1157
1415
  return this.store.getSettledTxReceipt(txHash);
1158
1416
  }
1159
1417
 
1160
- /**
1161
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
1162
- * @param from - The block number from which to begin retrieving logs.
1163
- * @param limit - The maximum number of blocks to retrieve logs from.
1164
- * @returns An array of private logs from the specified range of blocks.
1165
- */
1166
- public getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
1167
- return this.store.getPrivateLogs(from, limit);
1418
+ getPrivateLogsByTags(tags: SiloedTag[]): Promise<TxScopedL2Log[][]> {
1419
+ return this.store.getPrivateLogsByTags(tags);
1168
1420
  }
1169
1421
 
1170
- /**
1171
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
1172
- * @param tags - The tags to filter the logs by.
1173
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
1174
- * that tag.
1175
- */
1176
- getLogsByTags(tags: Fr[]): Promise<TxScopedL2Log[][]> {
1177
- return this.store.getLogsByTags(tags);
1422
+ getPublicLogsByTagsFromContract(contractAddress: AztecAddress, tags: Tag[]): Promise<TxScopedL2Log[][]> {
1423
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
1178
1424
  }
1179
1425
 
1180
1426
  /**
@@ -1197,19 +1443,11 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1197
1443
 
1198
1444
  /**
1199
1445
  * Gets the number of the latest L2 block processed by the block source implementation.
1446
+ * This includes both checkpointed and uncheckpointed blocks.
1200
1447
  * @returns The number of the latest L2 block processed by the block source implementation.
1201
1448
  */
1202
- public getBlockNumber(): Promise<number> {
1203
- return this.store.getSynchedL2BlockNumber();
1204
- }
1205
-
1206
- public getProvenBlockNumber(): Promise<number> {
1207
- return this.store.getProvenL2BlockNumber();
1208
- }
1209
-
1210
- /** Forcefully updates the last proven block number. Use for testing. */
1211
- public setProvenBlockNumber(blockNumber: number): Promise<void> {
1212
- return this.store.setProvenL2BlockNumber(blockNumber);
1449
+ public getBlockNumber(): Promise<BlockNumber> {
1450
+ return this.store.getLatestBlockNumber();
1213
1451
  }
1214
1452
 
1215
1453
  public getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
@@ -1237,12 +1475,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1237
1475
  }
1238
1476
 
1239
1477
  /**
1240
- * Gets L1 to L2 message (to be) included in a given block.
1241
- * @param blockNumber - L2 block number to get messages for.
1478
+ * Gets L1 to L2 message (to be) included in a given checkpoint.
1479
+ * @param checkpointNumber - Checkpoint number to get messages for.
1242
1480
  * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
1243
1481
  */
1244
- getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
1245
- return this.store.getL1ToL2Messages(blockNumber);
1482
+ getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
1483
+ return this.store.getL1ToL2Messages(checkpointNumber);
1246
1484
  }
1247
1485
 
1248
1486
  /**
@@ -1283,7 +1521,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1283
1521
  // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
1284
1522
  // We just force it 2 epochs worth of proven data for now.
1285
1523
  // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
1286
- const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
1524
+ const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
1287
1525
 
1288
1526
  const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
1289
1527
  latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
@@ -1307,52 +1545,44 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1307
1545
  );
1308
1546
  }
1309
1547
 
1310
- const latestBlockHeaderHash = await latestBlockHeader?.hash();
1311
- const provenBlockHeaderHash = await provenBlockHeader?.hash();
1312
- const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
1548
+ const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
1549
+ const provenBlockHeaderHash = (await provenBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
1550
+ const finalizedBlockHeaderHash = (await finalizedBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
1313
1551
 
1314
1552
  return {
1315
- latest: {
1316
- number: latestBlockNumber,
1317
- hash: latestBlockHeaderHash?.toString(),
1318
- } as L2BlockId,
1319
- proven: {
1320
- number: provenBlockNumber,
1321
- hash: provenBlockHeaderHash?.toString(),
1322
- } as L2BlockId,
1323
- finalized: {
1324
- number: finalizedBlockNumber,
1325
- hash: finalizedBlockHeaderHash?.toString(),
1326
- } as L2BlockId,
1553
+ latest: { number: latestBlockNumber, hash: latestBlockHeaderHash.toString() },
1554
+ proven: { number: provenBlockNumber, hash: provenBlockHeaderHash.toString() },
1555
+ finalized: { number: finalizedBlockNumber, hash: finalizedBlockHeaderHash.toString() },
1327
1556
  };
1328
1557
  }
1329
1558
 
1330
- public async rollbackTo(targetL2BlockNumber: number): Promise<void> {
1559
+ public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
1560
+ // TODO(pw/mbps): This still assumes 1 block per checkpoint
1331
1561
  const currentBlocks = await this.getL2Tips();
1332
1562
  const currentL2Block = currentBlocks.latest.number;
1333
1563
  const currentProvenBlock = currentBlocks.proven.number;
1334
- // const currentFinalizedBlock = currentBlocks.finalized.number;
1335
1564
 
1336
1565
  if (targetL2BlockNumber >= currentL2Block) {
1337
1566
  throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
1338
1567
  }
1339
1568
  const blocksToUnwind = currentL2Block - targetL2BlockNumber;
1340
- const targetL2Block = await this.store.getPublishedBlock(targetL2BlockNumber);
1569
+ const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
1341
1570
  if (!targetL2Block) {
1342
1571
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1343
1572
  }
1344
1573
  const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1574
+ const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
1345
1575
  const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1346
- this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
1347
- await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
1348
- this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
1349
- await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
1576
+ this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
1577
+ await this.store.unwindCheckpoints(CheckpointNumber(currentL2Block), blocksToUnwind);
1578
+ this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
1579
+ await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1350
1580
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1351
1581
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1352
1582
  await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
1353
1583
  if (targetL2BlockNumber < currentProvenBlock) {
1354
1584
  this.log.info(`Clearing proven L2 block number`);
1355
- await this.store.setProvenL2BlockNumber(0);
1585
+ await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
1356
1586
  }
1357
1587
  // TODO(palla/reorg): Set the finalized block when we add support for it.
1358
1588
  // if (targetL2BlockNumber < currentFinalizedBlock) {
@@ -1360,6 +1590,150 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
1360
1590
  // await this.store.setFinalizedL2BlockNumber(0);
1361
1591
  // }
1362
1592
  }
1593
+
1594
+ public async getPublishedCheckpoints(
1595
+ checkpointNumber: CheckpointNumber,
1596
+ limit: number,
1597
+ ): Promise<PublishedCheckpoint[]> {
1598
+ const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
1599
+ const blocks = (
1600
+ await Promise.all(checkpoints.map(ch => this.store.getBlocksForCheckpoint(ch.checkpointNumber)))
1601
+ ).filter(isDefined);
1602
+
1603
+ const fullCheckpoints: PublishedCheckpoint[] = [];
1604
+ for (let i = 0; i < checkpoints.length; i++) {
1605
+ const blocksForCheckpoint = blocks[i];
1606
+ const checkpoint = checkpoints[i];
1607
+ const fullCheckpoint = new Checkpoint(
1608
+ checkpoint.archive,
1609
+ checkpoint.header,
1610
+ blocksForCheckpoint,
1611
+ checkpoint.checkpointNumber,
1612
+ );
1613
+ const publishedCheckpoint = new PublishedCheckpoint(
1614
+ fullCheckpoint,
1615
+ checkpoint.l1,
1616
+ checkpoint.attestations.map(x => CommitteeAttestation.fromBuffer(x)),
1617
+ );
1618
+ fullCheckpoints.push(publishedCheckpoint);
1619
+ }
1620
+ return fullCheckpoints;
1621
+ }
1622
+
1623
+ public async getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
1624
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1625
+ const checkpoints: Checkpoint[] = [];
1626
+
1627
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1628
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1629
+ let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1630
+ const slot = (b: CheckpointData) => b.header.slotNumber;
1631
+ while (checkpointData && slot(checkpointData) >= start) {
1632
+ if (slot(checkpointData) <= end) {
1633
+ // push the checkpoints on backwards
1634
+ const [checkpoint] = await this.getPublishedCheckpoints(checkpointData.checkpointNumber, 1);
1635
+ checkpoints.push(checkpoint.checkpoint);
1636
+ }
1637
+ checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
1638
+ }
1639
+
1640
+ return checkpoints.reverse();
1641
+ }
1642
+
1643
+ /* Legacy APIs */
1644
+
1645
+ public async getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
1646
+ const checkpointedBlock = await this.store.getCheckpointedBlockByHash(blockHash);
1647
+ return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
1648
+ }
1649
+ public async getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
1650
+ const checkpointedBlock = await this.store.getCheckpointedBlockByArchive(archive);
1651
+ return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
1652
+ }
1653
+
1654
+ /**
1655
+ * Gets up to `limit` amount of L2 blocks starting from `from`.
1656
+ * @param from - Number of the first block to return (inclusive).
1657
+ * @param limit - The number of blocks to return.
1658
+ * @param proven - If true, only return blocks that have been proven.
1659
+ * @returns The requested L2 blocks.
1660
+ */
1661
+ public async getBlocks(from: BlockNumber, limit: number, proven?: boolean): Promise<L2Block[]> {
1662
+ const publishedBlocks = await this.getPublishedBlocks(from, limit, proven);
1663
+ return publishedBlocks.map(x => x.block);
1664
+ }
1665
+
1666
+ public async getPublishedBlocks(from: BlockNumber, limit: number, proven?: boolean): Promise<PublishedL2Block[]> {
1667
+ const checkpoints = await this.store.getRangeOfCheckpoints(CheckpointNumber(from), limit);
1668
+ const provenCheckpointNumber = await this.getProvenCheckpointNumber();
1669
+ const blocks = (
1670
+ await Promise.all(checkpoints.map(ch => this.store.getBlocksForCheckpoint(ch.checkpointNumber)))
1671
+ ).filter(isDefined);
1672
+
1673
+ const olbBlocks: PublishedL2Block[] = [];
1674
+ for (let i = 0; i < checkpoints.length; i++) {
1675
+ const blockForCheckpoint = blocks[i][0];
1676
+ const checkpoint = checkpoints[i];
1677
+ if (checkpoint.checkpointNumber > provenCheckpointNumber && proven === true) {
1678
+ // this checkpointisn't proven and we only want proven
1679
+ continue;
1680
+ }
1681
+ const oldCheckpoint = new Checkpoint(
1682
+ blockForCheckpoint.archive,
1683
+ checkpoint.header,
1684
+ [blockForCheckpoint],
1685
+ checkpoint.checkpointNumber,
1686
+ );
1687
+ const oldBlock = L2Block.fromCheckpoint(oldCheckpoint);
1688
+ const publishedBlock = new PublishedL2Block(
1689
+ oldBlock,
1690
+ checkpoint.l1,
1691
+ checkpoint.attestations.map(x => CommitteeAttestation.fromBuffer(x)),
1692
+ );
1693
+ olbBlocks.push(publishedBlock);
1694
+ }
1695
+ return olbBlocks;
1696
+ }
1697
+
1698
+ private async buildOldBlockFromCheckpointedBlock(
1699
+ checkpointedBlock: CheckpointedL2Block | undefined,
1700
+ ): Promise<PublishedL2Block | undefined> {
1701
+ if (!checkpointedBlock) {
1702
+ return undefined;
1703
+ }
1704
+ const checkpoint = await this.store.getCheckpointData(checkpointedBlock.checkpointNumber);
1705
+ if (!checkpoint) {
1706
+ return checkpoint;
1707
+ }
1708
+ const fullCheckpoint = new Checkpoint(
1709
+ checkpointedBlock?.block.archive,
1710
+ checkpoint?.header,
1711
+ [checkpointedBlock.block],
1712
+ checkpoint.checkpointNumber,
1713
+ );
1714
+ const oldBlock = L2Block.fromCheckpoint(fullCheckpoint);
1715
+ const published = new PublishedL2Block(
1716
+ oldBlock,
1717
+ checkpoint.l1,
1718
+ checkpoint.attestations.map(x => CommitteeAttestation.fromBuffer(x)),
1719
+ );
1720
+ return published;
1721
+ }
1722
+
1723
+ public async getBlock(number: BlockNumber): Promise<L2Block | undefined> {
1724
+ // If the number provided is -ve, then return the latest block.
1725
+ if (number < 0) {
1726
+ number = await this.store.getSynchedL2BlockNumber();
1727
+ }
1728
+ if (number === 0) {
1729
+ return undefined;
1730
+ }
1731
+ const publishedBlocks = await this.getPublishedBlocks(number, 1);
1732
+ if (publishedBlocks.length === 0) {
1733
+ return undefined;
1734
+ }
1735
+ return publishedBlocks[0].block;
1736
+ }
1363
1737
  }
1364
1738
 
1365
1739
  enum Operation {
@@ -1390,17 +1764,19 @@ export class ArchiverStoreHelper
1390
1764
  | 'close'
1391
1765
  | 'transactionAsync'
1392
1766
  | 'addBlocks'
1767
+ | 'getBlock'
1768
+ | 'getBlocks'
1393
1769
  >
1394
1770
  {
1395
1771
  #log = createLogger('archiver:block-helper');
1396
1772
 
1397
- constructor(protected readonly store: ArchiverDataStore) {}
1773
+ constructor(public readonly store: ArchiverDataStore) {}
1398
1774
 
1399
1775
  /**
1400
1776
  * Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
1401
1777
  * @param allLogs - All logs emitted in a bunch of blocks.
1402
1778
  */
1403
- async #updatePublishedContractClasses(allLogs: ContractClassLog[], blockNum: number, operation: Operation) {
1779
+ async #updatePublishedContractClasses(allLogs: ContractClassLog[], blockNum: BlockNumber, operation: Operation) {
1404
1780
  const contractClassPublishedEvents = allLogs
1405
1781
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
1406
1782
  .map(log => ContractClassPublishedEvent.fromLog(log));
@@ -1425,7 +1801,7 @@ export class ArchiverStoreHelper
1425
1801
  * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
1426
1802
  * @param allLogs - All logs emitted in a bunch of blocks.
1427
1803
  */
1428
- async #updateDeployedContractInstances(allLogs: PrivateLog[], blockNum: number, operation: Operation) {
1804
+ async #updateDeployedContractInstances(allLogs: PrivateLog[], blockNum: BlockNumber, operation: Operation) {
1429
1805
  const contractInstances = allLogs
1430
1806
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
1431
1807
  .map(log => ContractInstancePublishedEvent.fromLog(log))
@@ -1478,7 +1854,7 @@ export class ArchiverStoreHelper
1478
1854
  * @param _blockNum - The block number
1479
1855
  * @returns
1480
1856
  */
1481
- async #storeBroadcastedIndividualFunctions(allLogs: ContractClassLog[], _blockNum: number) {
1857
+ async #storeBroadcastedIndividualFunctions(allLogs: ContractClassLog[], _blockNum: BlockNumber) {
1482
1858
  // Filter out private and utility function broadcast events
1483
1859
  const privateFnEvents = allLogs
1484
1860
  .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
@@ -1532,7 +1908,23 @@ export class ArchiverStoreHelper
1532
1908
  return true;
1533
1909
  }
1534
1910
 
1535
- public addBlocks(blocks: PublishedL2Block[], pendingChainValidationStatus?: ValidateBlockResult): Promise<boolean> {
1911
+ private async addBlockDataToDB(block: L2BlockNew) {
1912
+ const contractClassLogs = block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1913
+ // ContractInstancePublished event logs are broadcast in privateLogs.
1914
+ const privateLogs = block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
1915
+ const publicLogs = block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
1916
+
1917
+ return (
1918
+ await Promise.all([
1919
+ this.#updatePublishedContractClasses(contractClassLogs, block.number, Operation.Store),
1920
+ this.#updateDeployedContractInstances(privateLogs, block.number, Operation.Store),
1921
+ this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, Operation.Store),
1922
+ this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.number),
1923
+ ])
1924
+ ).every(Boolean);
1925
+ }
1926
+
1927
+ public addBlocks(blocks: L2BlockNew[], pendingChainValidationStatus?: ValidateBlockResult): Promise<boolean> {
1536
1928
  // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1537
1929
  // or if the previous block is not in the store.
1538
1930
  return this.store.transactionAsync(async () => {
@@ -1542,25 +1934,10 @@ export class ArchiverStoreHelper
1542
1934
  // Update the pending chain validation status if provided
1543
1935
  pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1544
1936
  // Add any logs emitted during the retrieved blocks
1545
- this.store.addLogs(blocks.map(block => block.block)),
1937
+ this.store.addLogs(blocks),
1546
1938
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1547
- ...blocks.map(async block => {
1548
- const contractClassLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1549
- // ContractInstancePublished event logs are broadcast in privateLogs.
1550
- const privateLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
1551
- const publicLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
1552
- return (
1553
- await Promise.all([
1554
- this.#updatePublishedContractClasses(contractClassLogs, block.block.number, Operation.Store),
1555
- this.#updateDeployedContractInstances(privateLogs, block.block.number, Operation.Store),
1556
- this.#updateUpdatedContractInstances(
1557
- publicLogs,
1558
- block.block.header.globalVariables.timestamp,
1559
- Operation.Store,
1560
- ),
1561
- this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.block.number),
1562
- ])
1563
- ).every(Boolean);
1939
+ ...blocks.map(block => {
1940
+ return this.addBlockDataToDB(block);
1564
1941
  }),
1565
1942
  ]);
1566
1943
 
@@ -1568,61 +1945,104 @@ export class ArchiverStoreHelper
1568
1945
  });
1569
1946
  }
1570
1947
 
1571
- public async unwindBlocks(from: number, blocksToUnwind: number): Promise<boolean> {
1572
- const last = await this.getSynchedL2BlockNumber();
1573
- if (from != last) {
1574
- throw new Error(`Cannot unwind blocks from block ${from} when the last block is ${last}`);
1948
+ public addCheckpoints(
1949
+ checkpoints: PublishedCheckpoint[],
1950
+ pendingChainValidationStatus?: ValidateBlockResult,
1951
+ ): Promise<boolean> {
1952
+ // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1953
+ // or if the previous block is not in the store.
1954
+ return this.store.transactionAsync(async () => {
1955
+ await this.store.addCheckpoints(checkpoints);
1956
+ const allBlocks = checkpoints.flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks);
1957
+
1958
+ const opResults = await Promise.all([
1959
+ // Update the pending chain validation status if provided
1960
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1961
+ // Add any logs emitted during the retrieved blocks
1962
+ this.store.addLogs(allBlocks),
1963
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1964
+ ...allBlocks.map(block => {
1965
+ return this.addBlockDataToDB(block);
1966
+ }),
1967
+ ]);
1968
+
1969
+ return opResults.every(Boolean);
1970
+ });
1971
+ }
1972
+
1973
+ public async unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number): Promise<boolean> {
1974
+ if (checkpointsToUnwind <= 0) {
1975
+ throw new Error(`Cannot unwind ${checkpointsToUnwind} blocks`);
1575
1976
  }
1576
- if (blocksToUnwind <= 0) {
1577
- throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
1977
+
1978
+ const last = await this.getSynchedCheckpointNumber();
1979
+ if (from != last) {
1980
+ throw new Error(`Cannot unwind checkpoints from checkpoint ${from} when the last checkpoint is ${last}`);
1578
1981
  }
1579
1982
 
1580
- // from - blocksToUnwind = the new head, so + 1 for what we need to remove
1581
- const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1983
+ const blocks = [];
1984
+ const lastCheckpointNumber = from + checkpointsToUnwind - 1;
1985
+ for (let checkpointNumber = from; checkpointNumber <= lastCheckpointNumber; checkpointNumber++) {
1986
+ const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpointNumber);
1987
+ if (!blocksForCheckpoint) {
1988
+ continue;
1989
+ }
1990
+ blocks.push(...blocksForCheckpoint);
1991
+ }
1582
1992
 
1583
1993
  const opResults = await Promise.all([
1584
1994
  // Prune rolls back to the last proven block, which is by definition valid
1585
1995
  this.store.setPendingChainValidationStatus({ valid: true }),
1586
1996
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1587
1997
  ...blocks.map(async block => {
1588
- const contractClassLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1998
+ const contractClassLogs = block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1589
1999
  // ContractInstancePublished event logs are broadcast in privateLogs.
1590
- const privateLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
1591
- const publicLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
2000
+ const privateLogs = block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
2001
+ const publicLogs = block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
1592
2002
 
1593
2003
  return (
1594
2004
  await Promise.all([
1595
- this.#updatePublishedContractClasses(contractClassLogs, block.block.number, Operation.Delete),
1596
- this.#updateDeployedContractInstances(privateLogs, block.block.number, Operation.Delete),
1597
- this.#updateUpdatedContractInstances(
1598
- publicLogs,
1599
- block.block.header.globalVariables.timestamp,
1600
- Operation.Delete,
1601
- ),
2005
+ this.#updatePublishedContractClasses(contractClassLogs, block.number, Operation.Delete),
2006
+ this.#updateDeployedContractInstances(privateLogs, block.number, Operation.Delete),
2007
+ this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, Operation.Delete),
1602
2008
  ])
1603
2009
  ).every(Boolean);
1604
2010
  }),
1605
2011
 
1606
- this.store.deleteLogs(blocks.map(b => b.block)),
1607
- this.store.unwindBlocks(from, blocksToUnwind),
2012
+ this.store.deleteLogs(blocks),
2013
+ this.store.unwindCheckpoints(from, checkpointsToUnwind),
1608
2014
  ]);
1609
2015
 
1610
2016
  return opResults.every(Boolean);
1611
2017
  }
1612
2018
 
1613
- getPublishedBlocks(from: number, limit: number): Promise<PublishedL2Block[]> {
1614
- return this.store.getPublishedBlocks(from, limit);
2019
+ getCheckpointData(checkpointNumber: CheckpointNumber): Promise<CheckpointData | undefined> {
2020
+ return this.store.getCheckpointData(checkpointNumber);
2021
+ }
2022
+
2023
+ getRangeOfCheckpoints(from: CheckpointNumber, limit: number): Promise<CheckpointData[]> {
2024
+ return this.store.getRangeOfCheckpoints(from, limit);
2025
+ }
2026
+
2027
+ getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
2028
+ return this.store.getCheckpointedL2BlockNumber();
1615
2029
  }
1616
- getPublishedBlock(number: number): Promise<PublishedL2Block | undefined> {
1617
- return this.store.getPublishedBlock(number);
2030
+ getSynchedCheckpointNumber(): Promise<CheckpointNumber> {
2031
+ return this.store.getSynchedCheckpointNumber();
1618
2032
  }
1619
- getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
1620
- return this.store.getPublishedBlockByHash(blockHash);
2033
+ setCheckpointSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
2034
+ return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
1621
2035
  }
1622
- getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
1623
- return this.store.getPublishedBlockByArchive(archive);
2036
+ getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
2037
+ return this.store.getCheckpointedBlock(number);
1624
2038
  }
1625
- getBlockHeaders(from: number, limit: number): Promise<BlockHeader[]> {
2039
+ getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
2040
+ return this.store.getCheckpointedBlockByHash(blockHash);
2041
+ }
2042
+ getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
2043
+ return this.store.getCheckpointedBlockByArchive(archive);
2044
+ }
2045
+ getBlockHeaders(from: BlockNumber, limit: number): Promise<BlockHeader[]> {
1626
2046
  return this.store.getBlockHeaders(from, limit);
1627
2047
  }
1628
2048
  getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
@@ -1631,6 +2051,18 @@ export class ArchiverStoreHelper
1631
2051
  getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
1632
2052
  return this.store.getBlockHeaderByArchive(archive);
1633
2053
  }
2054
+ getBlockByHash(blockHash: Fr): Promise<L2BlockNew | undefined> {
2055
+ return this.store.getBlockByHash(blockHash);
2056
+ }
2057
+ getBlockByArchive(archive: Fr): Promise<L2BlockNew | undefined> {
2058
+ return this.store.getBlockByArchive(archive);
2059
+ }
2060
+ getLatestBlockNumber(): Promise<BlockNumber> {
2061
+ return this.store.getLatestBlockNumber();
2062
+ }
2063
+ getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2BlockNew[] | undefined> {
2064
+ return this.store.getBlocksForCheckpoint(checkpointNumber);
2065
+ }
1634
2066
  getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
1635
2067
  return this.store.getTxEffect(txHash);
1636
2068
  }
@@ -1640,17 +2072,17 @@ export class ArchiverStoreHelper
1640
2072
  addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
1641
2073
  return this.store.addL1ToL2Messages(messages);
1642
2074
  }
1643
- getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
1644
- return this.store.getL1ToL2Messages(blockNumber);
2075
+ getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
2076
+ return this.store.getL1ToL2Messages(checkpointNumber);
1645
2077
  }
1646
2078
  getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
1647
2079
  return this.store.getL1ToL2MessageIndex(l1ToL2Message);
1648
2080
  }
1649
- getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
1650
- return this.store.getPrivateLogs(from, limit);
2081
+ getPrivateLogsByTags(tags: SiloedTag[]): Promise<TxScopedL2Log[][]> {
2082
+ return this.store.getPrivateLogsByTags(tags);
1651
2083
  }
1652
- getLogsByTags(tags: Fr[], logsPerTag?: number): Promise<TxScopedL2Log[][]> {
1653
- return this.store.getLogsByTags(tags, logsPerTag);
2084
+ getPublicLogsByTagsFromContract(contractAddress: AztecAddress, tags: Tag[]): Promise<TxScopedL2Log[][]> {
2085
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
1654
2086
  }
1655
2087
  getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
1656
2088
  return this.store.getPublicLogs(filter);
@@ -1658,17 +2090,20 @@ export class ArchiverStoreHelper
1658
2090
  getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
1659
2091
  return this.store.getContractClassLogs(filter);
1660
2092
  }
1661
- getSynchedL2BlockNumber(): Promise<number> {
1662
- return this.store.getSynchedL2BlockNumber();
2093
+ getSynchedL2BlockNumber(): Promise<BlockNumber> {
2094
+ return this.store.getCheckpointedL2BlockNumber();
2095
+ }
2096
+ getProvenCheckpointNumber(): Promise<CheckpointNumber> {
2097
+ return this.store.getProvenCheckpointNumber();
1663
2098
  }
1664
- getProvenL2BlockNumber(): Promise<number> {
1665
- return this.store.getProvenL2BlockNumber();
2099
+ getProvenBlockNumber(): Promise<BlockNumber> {
2100
+ return this.store.getProvenBlockNumber();
1666
2101
  }
1667
- setProvenL2BlockNumber(l2BlockNumber: number): Promise<void> {
1668
- return this.store.setProvenL2BlockNumber(l2BlockNumber);
2102
+ setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
2103
+ return this.store.setProvenCheckpointNumber(checkpointNumber);
1669
2104
  }
1670
2105
  setBlockSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
1671
- return this.store.setBlockSynchedL1BlockNumber(l1BlockNumber);
2106
+ return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
1672
2107
  }
1673
2108
  setMessageSynchedL1Block(l1Block: L1BlockId): Promise<void> {
1674
2109
  return this.store.setMessageSynchedL1Block(l1Block);
@@ -1700,8 +2135,8 @@ export class ArchiverStoreHelper
1700
2135
  estimateSize(): Promise<{ mappingSize: number; physicalFileSize: number; actualSize: number; numItems: number }> {
1701
2136
  return this.store.estimateSize();
1702
2137
  }
1703
- rollbackL1ToL2MessagesToL2Block(targetBlockNumber: number): Promise<void> {
1704
- return this.store.rollbackL1ToL2MessagesToL2Block(targetBlockNumber);
2138
+ rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber: CheckpointNumber): Promise<void> {
2139
+ return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1705
2140
  }
1706
2141
  iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
1707
2142
  return this.store.iterateL1ToL2Messages(range);