@aztec/archiver 4.0.0-nightly.20250907 → 4.0.0-nightly.20260108

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