@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.5476d83

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 (110) hide show
  1. package/README.md +27 -6
  2. package/dest/archiver/archiver.d.ts +147 -57
  3. package/dest/archiver/archiver.d.ts.map +1 -1
  4. package/dest/archiver/archiver.js +841 -333
  5. package/dest/archiver/archiver_store.d.ts +85 -50
  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 +708 -213
  10. package/dest/archiver/config.d.ts +5 -21
  11. package/dest/archiver/config.d.ts.map +1 -1
  12. package/dest/archiver/config.js +21 -12
  13. package/dest/archiver/data_retrieval.d.ts +32 -27
  14. package/dest/archiver/data_retrieval.d.ts.map +1 -1
  15. package/dest/archiver/data_retrieval.js +197 -94
  16. package/dest/archiver/errors.d.ts +9 -1
  17. package/dest/archiver/errors.d.ts.map +1 -1
  18. package/dest/archiver/errors.js +12 -0
  19. package/dest/archiver/index.d.ts +3 -4
  20. package/dest/archiver/index.d.ts.map +1 -1
  21. package/dest/archiver/index.js +1 -2
  22. package/dest/archiver/instrumentation.d.ts +12 -6
  23. package/dest/archiver/instrumentation.d.ts.map +1 -1
  24. package/dest/archiver/instrumentation.js +58 -17
  25. package/dest/archiver/kv_archiver_store/block_store.d.ts +48 -11
  26. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  27. package/dest/archiver/kv_archiver_store/block_store.js +216 -63
  28. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +3 -3
  29. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  30. package/dest/archiver/kv_archiver_store/contract_class_store.js +12 -18
  31. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +11 -8
  32. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  33. package/dest/archiver/kv_archiver_store/contract_instance_store.js +30 -16
  34. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +50 -35
  35. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  36. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +88 -46
  37. package/dest/archiver/kv_archiver_store/log_store.d.ts +2 -2
  38. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  39. package/dest/archiver/kv_archiver_store/log_store.js +18 -46
  40. package/dest/archiver/kv_archiver_store/message_store.d.ts +23 -17
  41. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  42. package/dest/archiver/kv_archiver_store/message_store.js +150 -48
  43. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  44. package/dest/archiver/structs/inbox_message.d.ts +15 -0
  45. package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
  46. package/dest/archiver/structs/inbox_message.js +38 -0
  47. package/dest/archiver/structs/published.d.ts +3 -11
  48. package/dest/archiver/structs/published.d.ts.map +1 -1
  49. package/dest/archiver/structs/published.js +1 -1
  50. package/dest/archiver/validation.d.ts +17 -0
  51. package/dest/archiver/validation.d.ts.map +1 -0
  52. package/dest/archiver/validation.js +98 -0
  53. package/dest/factory.d.ts +8 -13
  54. package/dest/factory.d.ts.map +1 -1
  55. package/dest/factory.js +18 -49
  56. package/dest/index.d.ts +2 -2
  57. package/dest/index.d.ts.map +1 -1
  58. package/dest/index.js +1 -1
  59. package/dest/rpc/index.d.ts +2 -3
  60. package/dest/rpc/index.d.ts.map +1 -1
  61. package/dest/rpc/index.js +1 -4
  62. package/dest/test/index.d.ts +1 -1
  63. package/dest/test/mock_archiver.d.ts +2 -2
  64. package/dest/test/mock_archiver.d.ts.map +1 -1
  65. package/dest/test/mock_l1_to_l2_message_source.d.ts +5 -3
  66. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  67. package/dest/test/mock_l1_to_l2_message_source.js +14 -1
  68. package/dest/test/mock_l2_block_source.d.ts +38 -10
  69. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  70. package/dest/test/mock_l2_block_source.js +119 -8
  71. package/dest/test/mock_structs.d.ts +9 -0
  72. package/dest/test/mock_structs.d.ts.map +1 -0
  73. package/dest/test/mock_structs.js +37 -0
  74. package/package.json +28 -30
  75. package/src/archiver/archiver.ts +1087 -410
  76. package/src/archiver/archiver_store.ts +97 -55
  77. package/src/archiver/archiver_store_test_suite.ts +664 -210
  78. package/src/archiver/config.ts +28 -41
  79. package/src/archiver/data_retrieval.ts +279 -125
  80. package/src/archiver/errors.ts +21 -0
  81. package/src/archiver/index.ts +2 -3
  82. package/src/archiver/instrumentation.ts +77 -22
  83. package/src/archiver/kv_archiver_store/block_store.ts +270 -72
  84. package/src/archiver/kv_archiver_store/contract_class_store.ts +13 -23
  85. package/src/archiver/kv_archiver_store/contract_instance_store.ts +35 -27
  86. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +127 -63
  87. package/src/archiver/kv_archiver_store/log_store.ts +24 -62
  88. package/src/archiver/kv_archiver_store/message_store.ts +209 -53
  89. package/src/archiver/structs/inbox_message.ts +41 -0
  90. package/src/archiver/structs/published.ts +2 -11
  91. package/src/archiver/validation.ts +124 -0
  92. package/src/factory.ts +24 -66
  93. package/src/index.ts +1 -1
  94. package/src/rpc/index.ts +1 -5
  95. package/src/test/mock_archiver.ts +1 -1
  96. package/src/test/mock_l1_to_l2_message_source.ts +14 -3
  97. package/src/test/mock_l2_block_source.ts +158 -13
  98. package/src/test/mock_structs.ts +49 -0
  99. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
  100. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
  101. package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
  102. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
  103. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
  104. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
  105. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
  106. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
  107. package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
  108. package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
  109. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
  110. package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
@@ -1,42 +1,56 @@
1
1
  import type { BlobSinkClientInterface } from '@aztec/blob-sink/client';
2
- import { type ViemPublicClient, createEthereumChain } from '@aztec/ethereum';
2
+ 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';
11
+ import { maxBigint } from '@aztec/foundation/bigint';
12
+ import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
13
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
14
+ import { merge, pick } from '@aztec/foundation/collection';
3
15
  import type { EthAddress } from '@aztec/foundation/eth-address';
4
16
  import { Fr } from '@aztec/foundation/fields';
5
17
  import { type Logger, createLogger } from '@aztec/foundation/log';
18
+ import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
6
19
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
7
20
  import { count } from '@aztec/foundation/string';
8
- import { elapsed } from '@aztec/foundation/timer';
9
- import { InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
21
+ import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
22
+ import type { CustomRange } from '@aztec/kv-store';
23
+ import { RollupAbi } from '@aztec/l1-artifacts';
10
24
  import {
11
- ContractClassRegisteredEvent,
25
+ ContractClassPublishedEvent,
12
26
  PrivateFunctionBroadcastedEvent,
13
- UnconstrainedFunctionBroadcastedEvent,
14
- } from '@aztec/protocol-contracts/class-registerer';
27
+ UtilityFunctionBroadcastedEvent,
28
+ } from '@aztec/protocol-contracts/class-registry';
15
29
  import {
16
- ContractInstanceDeployedEvent,
30
+ ContractInstancePublishedEvent,
17
31
  ContractInstanceUpdatedEvent,
18
- } from '@aztec/protocol-contracts/instance-deployer';
32
+ } from '@aztec/protocol-contracts/instance-registry';
19
33
  import type { FunctionSelector } from '@aztec/stdlib/abi';
20
34
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
21
35
  import {
22
- type InBlock,
23
- type L2Block,
36
+ type ArchiverEmitter,
37
+ L2Block,
24
38
  type L2BlockId,
25
39
  type L2BlockSource,
26
40
  L2BlockSourceEvents,
27
41
  type L2Tips,
28
- type NullifierWithBlockSource,
42
+ PublishedL2Block,
29
43
  } from '@aztec/stdlib/block';
44
+ import type { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
30
45
  import {
31
46
  type ContractClassPublic,
32
47
  type ContractDataSource,
33
48
  type ContractInstanceWithAddress,
34
49
  type ExecutablePrivateFunctionWithMembershipProof,
35
- type PublicFunction,
36
- type UnconstrainedFunctionWithMembershipProof,
50
+ type UtilityFunctionWithMembershipProof,
37
51
  computePublicBytecodeCommitment,
38
52
  isValidPrivateFunctionMembershipProof,
39
- isValidUnconstrainedFunctionMembershipProof,
53
+ isValidUtilityFunctionMembershipProof,
40
54
  } from '@aztec/stdlib/contract';
41
55
  import {
42
56
  type L1RollupConstants,
@@ -49,49 +63,84 @@ import {
49
63
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
50
64
  import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
51
65
  import { ContractClassLog, type LogFilter, type PrivateLog, type PublicLog, TxScopedL2Log } from '@aztec/stdlib/logs';
52
- import type { InboxLeaf, L1ToL2MessageSource } from '@aztec/stdlib/messaging';
53
- import { type BlockHeader, TxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
54
- import { Attributes, type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
66
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
67
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
68
+ import { type BlockHeader, type IndexedTxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
69
+ import type { UInt64 } from '@aztec/stdlib/types';
70
+ import {
71
+ type TelemetryClient,
72
+ type Traceable,
73
+ type Tracer,
74
+ getTelemetryClient,
75
+ trackSpan,
76
+ } from '@aztec/telemetry-client';
55
77
 
56
78
  import { EventEmitter } from 'events';
57
79
  import groupBy from 'lodash.groupby';
58
- import { type GetContractReturnType, createPublicClient, fallback, getContract, http } from 'viem';
80
+ import { type GetContractReturnType, type Hex, createPublicClient, fallback, http } from 'viem';
59
81
 
60
82
  import type { ArchiverDataStore, ArchiverL1SynchPoint } from './archiver_store.js';
61
83
  import type { ArchiverConfig } from './config.js';
62
- import { retrieveBlocksFromRollup, retrieveL1ToL2Messages } from './data_retrieval.js';
63
- import { NoBlobBodiesFoundError } from './errors.js';
84
+ import {
85
+ retrieveCheckpointsFromRollup,
86
+ retrieveL1ToL2Message,
87
+ retrieveL1ToL2Messages,
88
+ retrievedToPublishedCheckpoint,
89
+ } from './data_retrieval.js';
90
+ import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
64
91
  import { ArchiverInstrumentation } from './instrumentation.js';
65
- import type { DataRetrieval } from './structs/data_retrieval.js';
66
- import type { L1Published } from './structs/published.js';
92
+ import type { InboxMessage } from './structs/inbox_message.js';
93
+ import { type ValidateBlockResult, validateCheckpointAttestations } from './validation.js';
67
94
 
68
95
  /**
69
96
  * Helper interface to combine all sources this archiver implementation provides.
70
97
  */
71
- export type ArchiveSource = L2BlockSource &
72
- L2LogsSource &
73
- ContractDataSource &
74
- L1ToL2MessageSource &
75
- NullifierWithBlockSource;
98
+ export type ArchiveSource = L2BlockSource & L2LogsSource & ContractDataSource & L1ToL2MessageSource;
99
+
100
+ export type ArchiverDeps = {
101
+ telemetry?: TelemetryClient;
102
+ blobSinkClient: BlobSinkClientInterface;
103
+ epochCache?: EpochCache;
104
+ dateProvider?: DateProvider;
105
+ };
106
+
107
+ function mapArchiverConfig(config: Partial<ArchiverConfig>) {
108
+ return {
109
+ pollingIntervalMs: config.archiverPollingIntervalMS,
110
+ batchSize: config.archiverBatchSize,
111
+ skipValidateBlockAttestations: config.skipValidateBlockAttestations,
112
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
113
+ };
114
+ }
115
+
116
+ type RollupStatus = {
117
+ provenCheckpointNumber: number;
118
+ provenArchive: Hex;
119
+ pendingCheckpointNumber: number;
120
+ pendingArchive: Hex;
121
+ validationResult: ValidateBlockResult | undefined;
122
+ lastRetrievedCheckpoint?: PublishedCheckpoint;
123
+ lastL1BlockWithCheckpoint?: bigint;
124
+ };
76
125
 
77
126
  /**
78
- * Pulls L2 blocks in a non-blocking manner and provides interface for their retrieval.
127
+ * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
79
128
  * Responsible for handling robust L1 polling so that other components do not need to
80
129
  * concern themselves with it.
81
130
  */
82
- export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
83
- /**
84
- * A promise in which we will be continually fetching new L2 blocks.
85
- */
86
- private runningPromise?: RunningPromise;
131
+ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implements ArchiveSource, Traceable {
132
+ /** A loop in which we will be continually fetching new checkpoints. */
133
+ private runningPromise: RunningPromise;
87
134
 
88
- private rollup: GetContractReturnType<typeof RollupAbi, ViemPublicClient>;
89
- private inbox: GetContractReturnType<typeof InboxAbi, ViemPublicClient>;
135
+ private rollup: RollupContract;
136
+ private inbox: InboxContract;
90
137
 
91
138
  private store: ArchiverStoreHelper;
92
139
 
93
- public l1BlockNumber: bigint | undefined;
94
- public l1Timestamp: bigint | undefined;
140
+ private l1BlockNumber: bigint | undefined;
141
+ private l1Timestamp: bigint | undefined;
142
+ private initialSyncComplete: boolean = false;
143
+ private initialSyncPromise: PromiseWithResolvers<void>;
95
144
 
96
145
  public readonly tracer: Tracer;
97
146
 
@@ -109,10 +158,17 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
109
158
  private readonly publicClient: ViemPublicClient,
110
159
  private readonly l1Addresses: { rollupAddress: EthAddress; inboxAddress: EthAddress; registryAddress: EthAddress },
111
160
  readonly dataStore: ArchiverDataStore,
112
- private readonly config: { pollingIntervalMs: number; batchSize: number },
161
+ private config: {
162
+ pollingIntervalMs: number;
163
+ batchSize: number;
164
+ skipValidateBlockAttestations?: boolean;
165
+ maxAllowedEthClientDriftSeconds: number;
166
+ },
113
167
  private readonly blobSinkClient: BlobSinkClientInterface,
168
+ private readonly epochCache: EpochCache,
169
+ private readonly dateProvider: DateProvider,
114
170
  private readonly instrumentation: ArchiverInstrumentation,
115
- private readonly l1constants: L1RollupConstants,
171
+ private readonly l1constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
116
172
  private readonly log: Logger = createLogger('archiver'),
117
173
  ) {
118
174
  super();
@@ -120,17 +176,18 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
120
176
  this.tracer = instrumentation.tracer;
121
177
  this.store = new ArchiverStoreHelper(dataStore);
122
178
 
123
- this.rollup = getContract({
124
- address: l1Addresses.rollupAddress.toString(),
125
- abi: RollupAbi,
126
- client: publicClient,
127
- });
179
+ this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
180
+ this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
181
+ this.initialSyncPromise = promiseWithResolvers();
128
182
 
129
- this.inbox = getContract({
130
- address: l1Addresses.inboxAddress.toString(),
131
- abi: InboxAbi,
132
- client: publicClient,
133
- });
183
+ // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
184
+ // are done as fast as possible. This then gets updated once the initial sync completes.
185
+ this.runningPromise = new RunningPromise(
186
+ () => this.sync(),
187
+ this.log,
188
+ this.config.pollingIntervalMs / 10,
189
+ makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError),
190
+ );
134
191
  }
135
192
 
136
193
  /**
@@ -143,7 +200,7 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
143
200
  public static async createAndSync(
144
201
  config: ArchiverConfig,
145
202
  archiverStore: ArchiverDataStore,
146
- deps: { telemetry: TelemetryClient; blobSinkClient: BlobSinkClientInterface },
203
+ deps: ArchiverDeps,
147
204
  blockUntilSynced = true,
148
205
  ): Promise<Archiver> {
149
206
  const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
@@ -153,76 +210,116 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
153
210
  pollingInterval: config.viemPollingIntervalMS,
154
211
  });
155
212
 
156
- const rollup = getContract({
157
- address: config.l1Contracts.rollupAddress.toString(),
158
- abi: RollupAbi,
159
- client: publicClient,
160
- });
213
+ const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
161
214
 
162
- const [l1StartBlock, l1GenesisTime] = await Promise.all([
163
- rollup.read.L1_BLOCK_AT_GENESIS(),
164
- rollup.read.getGenesisTime(),
215
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
216
+ rollup.getL1StartBlock(),
217
+ rollup.getL1GenesisTime(),
218
+ rollup.getProofSubmissionEpochs(),
219
+ rollup.getGenesisArchiveTreeRoot(),
165
220
  ] as const);
166
221
 
222
+ const l1StartBlockHash = await publicClient
223
+ .getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
224
+ .then(block => Buffer32.fromString(block.hash));
225
+
167
226
  const { aztecEpochDuration: epochDuration, aztecSlotDuration: slotDuration, ethereumSlotDuration } = config;
168
227
 
228
+ const l1Constants = {
229
+ l1StartBlockHash,
230
+ l1StartBlock,
231
+ l1GenesisTime,
232
+ epochDuration,
233
+ slotDuration,
234
+ ethereumSlotDuration,
235
+ proofSubmissionEpochs: Number(proofSubmissionEpochs),
236
+ genesisArchiveRoot: Fr.fromHexString(genesisArchiveRoot),
237
+ };
238
+
239
+ const opts = merge(
240
+ { pollingIntervalMs: 10_000, batchSize: 100, maxAllowedEthClientDriftSeconds: 300 },
241
+ mapArchiverConfig(config),
242
+ );
243
+
244
+ const epochCache = deps.epochCache ?? (await EpochCache.create(config.l1Contracts.rollupAddress, config, deps));
245
+ const telemetry = deps.telemetry ?? getTelemetryClient();
246
+
169
247
  const archiver = new Archiver(
170
248
  publicClient,
171
249
  config.l1Contracts,
172
250
  archiverStore,
173
- {
174
- pollingIntervalMs: config.archiverPollingIntervalMS ?? 10_000,
175
- batchSize: config.archiverBatchSize ?? 100,
176
- },
251
+ opts,
177
252
  deps.blobSinkClient,
178
- await ArchiverInstrumentation.new(deps.telemetry, () => archiverStore.estimateSize()),
179
- { l1StartBlock, l1GenesisTime, epochDuration, slotDuration, ethereumSlotDuration },
253
+ epochCache,
254
+ deps.dateProvider ?? new DateProvider(),
255
+ await ArchiverInstrumentation.new(telemetry, () => archiverStore.estimateSize()),
256
+ l1Constants,
180
257
  );
181
258
  await archiver.start(blockUntilSynced);
182
259
  return archiver;
183
260
  }
184
261
 
262
+ /** Updates archiver config */
263
+ public updateConfig(newConfig: Partial<ArchiverConfig>) {
264
+ this.config = merge(this.config, mapArchiverConfig(newConfig));
265
+ }
266
+
185
267
  /**
186
268
  * Starts sync process.
187
269
  * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
188
270
  */
189
271
  public async start(blockUntilSynced: boolean): Promise<void> {
190
- if (this.runningPromise) {
272
+ if (this.runningPromise.isRunning()) {
191
273
  throw new Error('Archiver is already running');
192
274
  }
193
275
 
194
- if (blockUntilSynced) {
195
- await this.syncSafe(blockUntilSynced);
196
- }
276
+ await this.blobSinkClient.testSources();
277
+ await this.testEthereumNodeSynced();
197
278
 
198
- this.runningPromise = new RunningPromise(
199
- () => this.sync(false),
200
- this.log,
201
- this.config.pollingIntervalMs,
202
- makeLoggingErrorHandler(
203
- this.log,
204
- // Ignored errors will not log to the console
205
- // We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
206
- NoBlobBodiesFoundError,
207
- ),
279
+ // Log initial state for the archiver
280
+ const { l1StartBlock } = this.l1constants;
281
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
282
+ const currentL2Block = await this.getBlockNumber();
283
+ this.log.info(
284
+ `Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`,
285
+ { blocksSynchedTo, messagesSynchedTo, currentL2Block },
208
286
  );
209
287
 
288
+ // Start sync loop, and return the wait for initial sync if we are asked to block until synced
210
289
  this.runningPromise.start();
290
+ if (blockUntilSynced) {
291
+ return this.waitForInitialSync();
292
+ }
293
+ }
294
+
295
+ public syncImmediate() {
296
+ return this.runningPromise.trigger();
297
+ }
298
+
299
+ public waitForInitialSync() {
300
+ return this.initialSyncPromise.promise;
211
301
  }
212
302
 
213
- private async syncSafe(initialRun: boolean) {
214
- try {
215
- await this.sync(initialRun);
216
- } catch (error) {
217
- this.log.error('Error during sync', { error });
303
+ /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */
304
+ private async testEthereumNodeSynced() {
305
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
306
+ if (maxAllowedDelay === 0) {
307
+ return;
308
+ }
309
+ const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({ includeTransactions: false });
310
+ const currentTime = BigInt(this.dateProvider.nowInSeconds());
311
+ if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
312
+ throw new Error(
313
+ `Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`,
314
+ );
218
315
  }
219
316
  }
220
317
 
221
318
  /**
222
319
  * Fetches logs from L1 contracts and processes them.
223
320
  */
224
- @trackSpan('Archiver.sync', initialRun => ({ [Attributes.INITIAL_SYNC]: initialRun }))
225
- private async sync(initialRun: boolean) {
321
+ @trackSpan('Archiver.sync')
322
+ private async sync() {
226
323
  /**
227
324
  * We keep track of three "pointers" to L1 blocks:
228
325
  * 1. the last L1 block that published an L2 block
@@ -232,21 +329,23 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
232
329
  * We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
233
330
  * We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
234
331
  * The archiver will stay back, until there's data on L1 that will move the pointers forward.
235
- *
236
- * This code does not handle reorgs.
237
332
  */
238
- const { l1StartBlock } = this.l1constants;
239
- const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
240
- const currentL1BlockNumber = await this.publicClient.getBlockNumber();
241
-
242
- if (initialRun) {
243
- this.log.info(
244
- `Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${Math.min(
245
- Number(blocksSynchedTo),
246
- Number(messagesSynchedTo),
247
- )} to current L1 block ${currentL1BlockNumber}`,
248
- );
249
- }
333
+ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
334
+ const {
335
+ blocksSynchedTo = l1StartBlock,
336
+ messagesSynchedTo = { l1BlockNumber: l1StartBlock, l1BlockHash: l1StartBlockHash },
337
+ } = await this.store.getSynchPoint();
338
+
339
+ const currentL1Block = await this.publicClient.getBlock({ includeTransactions: false });
340
+ const currentL1BlockNumber = currentL1Block.number;
341
+ const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
342
+
343
+ this.log.trace(`Starting new archiver sync iteration`, {
344
+ blocksSynchedTo,
345
+ messagesSynchedTo,
346
+ currentL1BlockNumber,
347
+ currentL1BlockHash,
348
+ });
250
349
 
251
350
  // ********** Ensuring Consistency of data pulled from L1 **********
252
351
 
@@ -268,75 +367,141 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
268
367
  */
269
368
 
270
369
  // ********** Events that are processed per L1 block **********
271
- await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
272
-
273
- // Store latest l1 block number and timestamp seen. Used for epoch and slots calculations.
274
- if (!this.l1BlockNumber || this.l1BlockNumber < currentL1BlockNumber) {
275
- this.l1Timestamp = (await this.publicClient.getBlock({ blockNumber: currentL1BlockNumber })).timestamp;
276
- this.l1BlockNumber = currentL1BlockNumber;
370
+ await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber, currentL1BlockHash);
371
+
372
+ // Get L1 timestamp for the current block
373
+ const currentL1Timestamp =
374
+ !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber
375
+ ? (await this.publicClient.getBlock({ blockNumber: currentL1BlockNumber })).timestamp
376
+ : this.l1Timestamp;
377
+
378
+ // Warn if the latest L1 block timestamp is too old
379
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
380
+ const now = this.dateProvider.nowInSeconds();
381
+ if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
382
+ this.log.warn(
383
+ `Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`,
384
+ { currentL1BlockNumber, currentL1Timestamp, now, maxAllowedDelay },
385
+ );
277
386
  }
278
387
 
279
- // ********** Events that are processed per L2 block **********
388
+ // ********** Events that are processed per checkpoint **********
280
389
  if (currentL1BlockNumber > blocksSynchedTo) {
281
- // First we retrieve new L2 blocks
282
- const { provenBlockNumber } = await this.handleL2blocks(blocksSynchedTo, currentL1BlockNumber);
283
- // And then we prune the current epoch if it'd reorg on next submission.
284
- // Note that we don't do this before retrieving L2 blocks because we may need to retrieve
285
- // blocks from more than 2 epochs ago, so we want to make sure we have the latest view of
390
+ // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
391
+ // pending chain validation status, proven checkpoint number, and synched L1 block number.
392
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
393
+ // Then we prune the current epoch if it'd reorg on next submission.
394
+ // Note that we don't do this before retrieving checkpoints because we may need to retrieve
395
+ // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
286
396
  // the chain locally before we start unwinding stuff. This can be optimized by figuring out
287
- // up to which point we're pruning, and then requesting L2 blocks up to that point only.
288
- await this.handleEpochPrune(provenBlockNumber, currentL1BlockNumber);
397
+ // up to which point we're pruning, and then requesting checkpoints up to that point only.
398
+ const { rollupCanPrune } = await this.handleEpochPrune(
399
+ rollupStatus.provenCheckpointNumber,
400
+ currentL1BlockNumber,
401
+ currentL1Timestamp,
402
+ );
403
+
404
+ // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
405
+ // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
406
+ // we get a valid checkpoint to advance the syncpoint.
407
+ if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
408
+ await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
409
+ }
410
+
411
+ // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
412
+ // We only do this if rollup cant prune on the next submission. Otherwise we will end up
413
+ // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
414
+ // since the archiver will rightfully refuse to sync up to it.
415
+ if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
416
+ await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
417
+ }
289
418
 
290
419
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
291
420
  }
292
421
 
293
- if (initialRun) {
294
- this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete.`, {
422
+ // After syncing has completed, update the current l1 block number and timestamp,
423
+ // otherwise we risk announcing to the world that we've synced to a given point,
424
+ // but the corresponding blocks have not been processed (see #12631).
425
+ this.l1Timestamp = currentL1Timestamp;
426
+ this.l1BlockNumber = currentL1BlockNumber;
427
+
428
+ // We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
429
+ // so if the initial sync took too long, we still go for another iteration.
430
+ if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= (await this.publicClient.getBlockNumber())) {
431
+ this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
295
432
  l1BlockNumber: currentL1BlockNumber,
296
433
  syncPoint: await this.store.getSynchPoint(),
297
434
  ...(await this.getL2Tips()),
298
435
  });
436
+ this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
437
+ this.initialSyncComplete = true;
438
+ this.initialSyncPromise.resolve();
299
439
  }
300
440
  }
301
441
 
302
- /** Queries the rollup contract on whether a prune can be executed on the immediatenext L1 block. */
303
- private async canPrune(currentL1BlockNumber: bigint) {
304
- const time = (this.l1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
305
- return await this.rollup.read.canPruneAtTime([time], { blockNumber: currentL1BlockNumber });
442
+ /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */
443
+ private async canPrune(currentL1BlockNumber: bigint, currentL1Timestamp: bigint) {
444
+ const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
445
+ const result = await this.rollup.canPruneAtTime(time, { blockNumber: currentL1BlockNumber });
446
+ if (result) {
447
+ this.log.debug(`Rollup contract allows pruning at L1 block ${currentL1BlockNumber} time ${time}`, {
448
+ currentL1Timestamp,
449
+ pruneTime: time,
450
+ currentL1BlockNumber,
451
+ });
452
+ }
453
+ return result;
306
454
  }
307
455
 
308
- /** Checks if there'd be a reorg for the next block submission and start pruning now. */
309
- private async handleEpochPrune(provenBlockNumber: bigint, currentL1BlockNumber: bigint) {
310
- const localPendingBlockNumber = BigInt(await this.getBlockNumber());
311
- const canPrune = localPendingBlockNumber > provenBlockNumber && (await this.canPrune(currentL1BlockNumber));
456
+ /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */
457
+ private async handleEpochPrune(
458
+ provenCheckpointNumber: number,
459
+ currentL1BlockNumber: bigint,
460
+ currentL1Timestamp: bigint,
461
+ ) {
462
+ const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
463
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
464
+ const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
312
465
 
313
466
  if (canPrune) {
314
- const localPendingSlotNumber = await this.getL2SlotNumber();
315
- const localPendingEpochNumber = getEpochAtSlot(localPendingSlotNumber, this.l1constants);
467
+ const timer = new Timer();
468
+ const pruneFrom = provenCheckpointNumber + 1;
469
+
470
+ const header = await this.getCheckpointHeader(Number(pruneFrom));
471
+ if (header === undefined) {
472
+ throw new Error(`Missing checkpoint header ${pruneFrom}`);
473
+ }
474
+
475
+ const pruneFromSlotNumber = header.slotNumber;
476
+ const pruneFromEpochNumber: EpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
477
+
478
+ const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
479
+
480
+ const checkpoints = await this.getCheckpoints(Number(provenCheckpointNumber) + 1, Number(checkpointsToUnwind));
316
481
 
317
482
  // Emit an event for listening services to react to the chain prune
318
483
  this.emit(L2BlockSourceEvents.L2PruneDetected, {
319
484
  type: L2BlockSourceEvents.L2PruneDetected,
320
- blockNumber: localPendingBlockNumber,
321
- slotNumber: localPendingSlotNumber,
322
- epochNumber: localPendingEpochNumber,
485
+ epochNumber: pruneFromEpochNumber,
486
+ blocks: checkpoints.flatMap(c => L2Block.fromCheckpoint(c)),
323
487
  });
324
488
 
325
- const blocksToUnwind = localPendingBlockNumber - provenBlockNumber;
326
489
  this.log.debug(
327
- `L2 prune from ${provenBlockNumber + 1n} to ${localPendingBlockNumber} will occur on next block submission.`,
490
+ `L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`,
328
491
  );
329
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
492
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
330
493
  this.log.warn(
331
- `Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` +
332
- `to ${provenBlockNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
333
- `Updated L2 latest block is ${await this.getBlockNumber()}.`,
494
+ `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
495
+ `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
496
+ `Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
334
497
  );
335
- this.instrumentation.processPrune();
498
+ this.instrumentation.processPrune(timer.ms());
336
499
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
337
500
  // Seems like the next iteration should handle this.
338
501
  // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
339
502
  }
503
+
504
+ return { rollupCanPrune };
340
505
  }
341
506
 
342
507
  private nextRange(end: bigint, limit: bigint): [bigint, bigint] {
@@ -349,148 +514,344 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
349
514
  return [nextStart, nextEnd];
350
515
  }
351
516
 
352
- private async handleL1ToL2Messages(messagesSynchedTo: bigint, currentL1BlockNumber: bigint) {
353
- this.log.trace(`Handling L1 to L2 messages from ${messagesSynchedTo} to ${currentL1BlockNumber}.`);
354
- if (currentL1BlockNumber <= messagesSynchedTo) {
517
+ private async handleL1ToL2Messages(
518
+ messagesSyncPoint: L1BlockId,
519
+ currentL1BlockNumber: bigint,
520
+ _currentL1BlockHash: Buffer32,
521
+ ) {
522
+ this.log.trace(`Handling L1 to L2 messages from ${messagesSyncPoint.l1BlockNumber} to ${currentL1BlockNumber}.`);
523
+ if (currentL1BlockNumber <= messagesSyncPoint.l1BlockNumber) {
355
524
  return;
356
525
  }
357
526
 
358
- const localTotalMessageCount = await this.store.getTotalL1ToL2MessageCount();
359
- const destinationTotalMessageCount = await this.inbox.read.totalMessagesInserted();
527
+ // Load remote and local inbox states.
528
+ const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
529
+ const localLastMessage = await this.store.getLastL1ToL2Message();
530
+ const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
360
531
 
361
- if (localTotalMessageCount === destinationTotalMessageCount) {
362
- await this.store.setMessageSynchedL1BlockNumber(currentL1BlockNumber);
532
+ this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
533
+ localMessagesInserted,
534
+ localLastMessage,
535
+ remoteMessagesState,
536
+ });
537
+
538
+ // Compare message count and rolling hash. If they match, no need to retrieve anything.
539
+ if (
540
+ remoteMessagesState.totalMessagesInserted === localMessagesInserted &&
541
+ remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
542
+ ) {
363
543
  this.log.trace(
364
- `Retrieved no new L1 to L2 messages between L1 blocks ${messagesSynchedTo + 1n} and ${currentL1BlockNumber}.`,
544
+ `No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`,
365
545
  );
366
546
  return;
367
547
  }
368
548
 
369
- // Retrieve messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
370
- let searchStartBlock: bigint = messagesSynchedTo;
371
- let searchEndBlock: bigint = messagesSynchedTo;
549
+ // Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
550
+ // Note that we need to fetch it from logs and not from inbox state at the syncpoint l1 block number, since it
551
+ // could be older than 128 blocks and non-archive nodes cannot resolve it.
552
+ if (localLastMessage) {
553
+ const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
554
+ this.log.trace(`Retrieved remote message for local last`, { remoteLastMessage, localLastMessage });
555
+ if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
556
+ this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
557
+ remoteLastMessage,
558
+ messagesSyncPoint,
559
+ localLastMessage,
560
+ });
561
+
562
+ messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
563
+ this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
564
+ messagesSyncPoint,
565
+ });
566
+ }
567
+ }
568
+
569
+ // Retrieve and save messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
570
+ let searchStartBlock: bigint = 0n;
571
+ let searchEndBlock: bigint = messagesSyncPoint.l1BlockNumber;
572
+
573
+ let lastMessage: InboxMessage | undefined;
574
+ let messageCount = 0;
575
+
372
576
  do {
373
577
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
374
578
  this.log.trace(`Retrieving L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
375
- const retrievedL1ToL2Messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
579
+ const messages = await retrieveL1ToL2Messages(this.inbox.getContract(), searchStartBlock, searchEndBlock);
376
580
  this.log.verbose(
377
- `Retrieved ${retrievedL1ToL2Messages.retrievedData.length} new L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`,
581
+ `Retrieved ${messages.length} new L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`,
378
582
  );
379
- await this.store.addL1ToL2Messages(retrievedL1ToL2Messages);
380
- for (const msg of retrievedL1ToL2Messages.retrievedData) {
381
- this.log.debug(`Downloaded L1 to L2 message`, { leaf: msg.leaf.toString(), index: msg.index });
583
+ const timer = new Timer();
584
+ await this.store.addL1ToL2Messages(messages);
585
+ const perMsg = timer.ms() / messages.length;
586
+ this.instrumentation.processNewMessages(messages.length, perMsg);
587
+ for (const msg of messages) {
588
+ this.log.debug(`Downloaded L1 to L2 message`, { ...msg, leaf: msg.leaf.toString() });
589
+ lastMessage = msg;
590
+ messageCount++;
382
591
  }
383
592
  } while (searchEndBlock < currentL1BlockNumber);
593
+
594
+ // Log stats for messages retrieved (if any).
595
+ if (messageCount > 0) {
596
+ this.log.info(
597
+ `Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for L2 block ${lastMessage?.l2BlockNumber}`,
598
+ { lastMessage, messageCount },
599
+ );
600
+ }
601
+
602
+ // Warn if the resulting rolling hash does not match the remote state we had retrieved.
603
+ if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
604
+ this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
605
+ lastMessage,
606
+ remoteMessagesState,
607
+ });
608
+ }
384
609
  }
385
610
 
386
- private async handleL2blocks(
387
- blocksSynchedTo: bigint,
388
- currentL1BlockNumber: bigint,
389
- ): Promise<{ provenBlockNumber: bigint }> {
390
- const localPendingBlockNumber = BigInt(await this.getBlockNumber());
391
- const [provenBlockNumber, provenArchive, pendingBlockNumber, pendingArchive, archiveForLocalPendingBlockNumber] =
392
- await this.rollup.read.status([localPendingBlockNumber], { blockNumber: currentL1BlockNumber });
393
-
394
- const updateProvenBlock = async () => {
395
- const localBlockForDestinationProvenBlockNumber = await this.getBlock(Number(provenBlockNumber));
396
-
397
- // Sanity check. I've hit what seems to be a state where the proven block is set to a value greater than the latest
398
- // synched block when requesting L2Tips from the archiver. This is the only place where the proven block is set.
399
- const synched = await this.store.getSynchedL2BlockNumber();
400
- if (localBlockForDestinationProvenBlockNumber && synched < localBlockForDestinationProvenBlockNumber?.number) {
611
+ private async retrieveL1ToL2Message(leaf: Fr): Promise<InboxMessage | undefined> {
612
+ const currentL1BlockNumber = await this.publicClient.getBlockNumber();
613
+ let searchStartBlock: bigint = 0n;
614
+ let searchEndBlock: bigint = this.l1constants.l1StartBlock - 1n;
615
+
616
+ do {
617
+ [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
618
+
619
+ const message = await retrieveL1ToL2Message(this.inbox.getContract(), leaf, searchStartBlock, searchEndBlock);
620
+
621
+ if (message) {
622
+ return message;
623
+ }
624
+ } while (searchEndBlock < currentL1BlockNumber);
625
+
626
+ return undefined;
627
+ }
628
+
629
+ private async rollbackL1ToL2Messages(localLastMessage: InboxMessage, messagesSyncPoint: L1BlockId) {
630
+ // Slowly go back through our messages until we find the last common message.
631
+ // We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
632
+ // is a very rare case, so it's fine to query one log at a time.
633
+ let commonMsg: undefined | InboxMessage;
634
+ this.log.verbose(`Searching most recent common L1 to L2 message at or before index ${localLastMessage.index}`);
635
+ for await (const msg of this.store.iterateL1ToL2Messages({ reverse: true, end: localLastMessage.index })) {
636
+ const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
637
+ const logCtx = { remoteMsg, localMsg: msg };
638
+ if (remoteMsg && remoteMsg.rollingHash.equals(msg.rollingHash)) {
639
+ this.log.verbose(
640
+ `Found most recent common L1 to L2 message at index ${msg.index} on L1 block ${msg.l1BlockNumber}`,
641
+ logCtx,
642
+ );
643
+ commonMsg = remoteMsg;
644
+ break;
645
+ } else if (remoteMsg) {
646
+ this.log.debug(`Local L1 to L2 message with index ${msg.index} has different rolling hash`, logCtx);
647
+ } else {
648
+ this.log.debug(`Local L1 to L2 message with index ${msg.index} not found on L1`, logCtx);
649
+ }
650
+ }
651
+
652
+ // Delete everything after the common message we found.
653
+ const lastGoodIndex = commonMsg?.index;
654
+ this.log.warn(`Deleting all local L1 to L2 messages after index ${lastGoodIndex ?? 'undefined'}`);
655
+ await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
656
+
657
+ // Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
658
+ // the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
659
+ // after the last common message.
660
+ const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1constants.l1StartBlock;
661
+ const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
662
+ messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
663
+ await this.store.setMessageSynchedL1Block(messagesSyncPoint);
664
+ return messagesSyncPoint;
665
+ }
666
+
667
+ private async getL1BlockHash(l1BlockNumber: bigint): Promise<Buffer32> {
668
+ const block = await this.publicClient.getBlock({ blockNumber: l1BlockNumber, includeTransactions: false });
669
+ if (!block) {
670
+ throw new Error(`Missing L1 block ${l1BlockNumber}`);
671
+ }
672
+ return Buffer32.fromString(block.hash);
673
+ }
674
+
675
+ private async handleCheckpoints(blocksSynchedTo: bigint, currentL1BlockNumber: bigint): Promise<RollupStatus> {
676
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
677
+ const initialValidationResult: ValidateBlockResult | undefined = await this.store.getPendingChainValidationStatus();
678
+ const [
679
+ rollupProvenCheckpointNumber,
680
+ provenArchive,
681
+ rollupPendingCheckpointNumber,
682
+ pendingArchive,
683
+ archiveForLocalPendingCheckpointNumber,
684
+ ] = await this.rollup.status(BigInt(localPendingCheckpointNumber), { blockNumber: currentL1BlockNumber });
685
+ const provenCheckpointNumber = Number(rollupProvenCheckpointNumber);
686
+ const pendingCheckpointNumber = Number(rollupPendingCheckpointNumber);
687
+ const rollupStatus = {
688
+ provenCheckpointNumber,
689
+ provenArchive,
690
+ pendingCheckpointNumber,
691
+ pendingArchive,
692
+ validationResult: initialValidationResult,
693
+ };
694
+ this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
695
+ localPendingCheckpointNumber,
696
+ blocksSynchedTo,
697
+ currentL1BlockNumber,
698
+ archiveForLocalPendingCheckpointNumber,
699
+ ...rollupStatus,
700
+ });
701
+
702
+ const updateProvenCheckpoint = async () => {
703
+ // Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
704
+ // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
705
+ // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
706
+ if (provenCheckpointNumber === 0) {
707
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
708
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
709
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
710
+ this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
711
+ }
712
+ }
713
+
714
+ const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
715
+
716
+ // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
717
+ // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
718
+ const synched = await this.getSynchedCheckpointNumber();
719
+ if (
720
+ localCheckpointForDestinationProvenCheckpointNumber &&
721
+ synched < localCheckpointForDestinationProvenCheckpointNumber.number
722
+ ) {
401
723
  this.log.error(
402
- `Hit local block greater than last synched block: ${localBlockForDestinationProvenBlockNumber.number} > ${synched}`,
724
+ `Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`,
403
725
  );
404
726
  }
405
727
 
728
+ this.log.trace(
729
+ `Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${
730
+ localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'
731
+ }`,
732
+ );
733
+
734
+ const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
406
735
  if (
407
- localBlockForDestinationProvenBlockNumber &&
408
- provenArchive === localBlockForDestinationProvenBlockNumber.archive.root.toString()
736
+ localCheckpointForDestinationProvenCheckpointNumber &&
737
+ provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()
409
738
  ) {
410
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
411
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
412
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
413
- this.log.info(`Updated proven chain to block ${provenBlockNumber}`, {
414
- provenBlockNumber,
739
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
740
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
741
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
742
+ this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
743
+ provenCheckpointNumber,
744
+ });
745
+ const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
746
+ const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
747
+
748
+ this.emit(L2BlockSourceEvents.L2BlockProven, {
749
+ type: L2BlockSourceEvents.L2BlockProven,
750
+ blockNumber: BigInt(lastProvenBlockNumber),
751
+ slotNumber: provenSlotNumber,
752
+ epochNumber: provenEpochNumber,
415
753
  });
754
+ } else {
755
+ this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
416
756
  }
417
757
  }
418
- this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber));
758
+ this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
419
759
  };
420
760
 
421
- // This is an edge case that we only hit if there are no proposed blocks.
422
- // If we have 0 blocks locally and there are no blocks onchain there is nothing to do.
423
- const noBlocks = localPendingBlockNumber === 0n && pendingBlockNumber === 0n;
424
- if (noBlocks) {
761
+ // This is an edge case that we only hit if there are no proposed checkpoints.
762
+ // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
763
+ const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
764
+ if (noCheckpoints) {
425
765
  await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
426
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
427
- return { provenBlockNumber };
766
+ this.log.debug(
767
+ `No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`,
768
+ );
769
+ return rollupStatus;
428
770
  }
429
771
 
430
- await updateProvenBlock();
772
+ await updateProvenCheckpoint();
431
773
 
432
774
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
433
- // are any state that could be impacted by it. If we have no blocks, there is no impact.
434
- if (localPendingBlockNumber > 0) {
435
- const localPendingBlock = await this.getBlock(Number(localPendingBlockNumber));
436
- if (localPendingBlock === undefined) {
437
- throw new Error(`Missing block ${localPendingBlockNumber}`);
775
+ // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
776
+ if (localPendingCheckpointNumber > 0) {
777
+ const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
778
+ if (localPendingCheckpoint === undefined) {
779
+ throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
438
780
  }
439
781
 
440
- const noBlockSinceLast = localPendingBlock && pendingArchive === localPendingBlock.archive.root.toString();
441
- if (noBlockSinceLast) {
442
- await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
443
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
444
- return { provenBlockNumber };
782
+ const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
783
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
784
+ if (noCheckpointSinceLast) {
785
+ // We believe the following line causes a problem when we encounter L1 re-orgs.
786
+ // Basically, by setting the synched L1 block number here, we are saying that we have
787
+ // processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
788
+ // this block again (or any blocks before).
789
+ // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
790
+ // We must only set this block number based on actually retrieved logs.
791
+ // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
792
+ // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
793
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
794
+ return rollupStatus;
445
795
  }
446
796
 
447
- const localPendingBlockInChain = archiveForLocalPendingBlockNumber === localPendingBlock.archive.root.toString();
448
- if (!localPendingBlockInChain) {
449
- // If our local pending block tip is not in the chain on L1 a "prune" must have happened
797
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
798
+ if (!localPendingCheckpointInChain) {
799
+ // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
450
800
  // or the L1 have reorged.
451
801
  // In any case, we have to figure out how far into the past the action will take us.
452
- // For simplicity here, we will simply rewind until we end in a block that is also on the chain on L1.
453
- this.log.debug(`L2 prune has been detected.`);
802
+ // For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
803
+ this.log.debug(
804
+ `L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`,
805
+ { localPendingCheckpointNumber, localPendingArchiveRoot, archiveForLocalPendingCheckpointNumber },
806
+ );
454
807
 
455
- let tipAfterUnwind = localPendingBlockNumber;
808
+ let tipAfterUnwind = localPendingCheckpointNumber;
456
809
  while (true) {
457
- const candidateBlock = await this.getBlock(Number(tipAfterUnwind));
458
- if (candidateBlock === undefined) {
810
+ const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
811
+ if (candidateCheckpoint === undefined) {
459
812
  break;
460
813
  }
461
814
 
462
- const archiveAtContract = await this.rollup.read.archiveAt([BigInt(candidateBlock.number)]);
463
-
464
- if (archiveAtContract === candidateBlock.archive.root.toString()) {
815
+ const archiveAtContract = await this.rollup.archiveAt(BigInt(candidateCheckpoint.number));
816
+ this.log.trace(
817
+ `Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`,
818
+ {
819
+ archiveAtContract,
820
+ archiveLocal: candidateCheckpoint.archive.root.toString(),
821
+ },
822
+ );
823
+ if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
465
824
  break;
466
825
  }
467
826
  tipAfterUnwind--;
468
827
  }
469
828
 
470
- const blocksToUnwind = localPendingBlockNumber - tipAfterUnwind;
471
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
829
+ const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
830
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
472
831
 
473
832
  this.log.warn(
474
- `Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` +
475
- `due to mismatched block hashes at L1 block ${currentL1BlockNumber}. ` +
476
- `Updated L2 latest block is ${await this.getBlockNumber()}.`,
833
+ `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
834
+ `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` +
835
+ `Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
477
836
  );
478
837
  }
479
838
  }
480
839
 
481
- // Retrieve L2 blocks in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
840
+ // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
482
841
  // computed using the L2 block time vs the L1 block time.
483
842
  let searchStartBlock: bigint = blocksSynchedTo;
484
843
  let searchEndBlock: bigint = blocksSynchedTo;
844
+ let lastRetrievedCheckpoint: PublishedCheckpoint | undefined;
845
+ let lastL1BlockWithCheckpoint: bigint | undefined = undefined;
485
846
 
486
847
  do {
487
848
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
488
849
 
489
- this.log.trace(`Retrieving L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
850
+ this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
490
851
 
491
- // TODO(md): Retreive from blob sink then from consensus client, then from peers
492
- const retrievedBlocks = await retrieveBlocksFromRollup(
493
- this.rollup,
852
+ // TODO(md): Retrieve from blob sink then from consensus client, then from peers
853
+ const retrievedCheckpoints = await retrieveCheckpointsFromRollup(
854
+ this.rollup.getContract() as GetContractReturnType<typeof RollupAbi, ViemPublicClient>,
494
855
  this.publicClient,
495
856
  this.blobSinkClient,
496
857
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
@@ -498,47 +859,175 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
498
859
  this.log,
499
860
  );
500
861
 
501
- if (retrievedBlocks.length === 0) {
862
+ if (retrievedCheckpoints.length === 0) {
502
863
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
503
864
  // See further details in earlier comments.
504
- this.log.trace(`Retrieved no new L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
865
+ this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
505
866
  continue;
506
867
  }
507
868
 
508
- const lastProcessedL1BlockNumber = retrievedBlocks[retrievedBlocks.length - 1].l1.blockNumber;
509
869
  this.log.debug(
510
- `Retrieved ${retrievedBlocks.length} new L2 blocks between L1 blocks ${searchStartBlock} and ${searchEndBlock} with last processed L1 block ${lastProcessedL1BlockNumber}.`,
870
+ `Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
871
+ {
872
+ lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
873
+ searchStartBlock,
874
+ searchEndBlock,
875
+ },
511
876
  );
512
877
 
513
- for (const block of retrievedBlocks) {
514
- this.log.debug(`Ingesting new L2 block ${block.data.number} with ${block.data.body.txEffects.length} txs`, {
515
- blockHash: block.data.hash(),
516
- l1BlockNumber: block.l1.blockNumber,
517
- ...block.data.header.globalVariables.toInspect(),
518
- ...block.data.getStats(),
519
- });
878
+ const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map(b => retrievedToPublishedCheckpoint(b)));
879
+ const validCheckpoints: PublishedCheckpoint[] = [];
880
+
881
+ for (const published of publishedCheckpoints) {
882
+ const validationResult = this.config.skipValidateBlockAttestations
883
+ ? { valid: true as const }
884
+ : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
885
+
886
+ // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
887
+ // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
888
+ // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
889
+ // we need to update the validation result, since we need to be able to invalidate the new one.
890
+ // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
891
+ if (
892
+ rollupStatus.validationResult?.valid !== validationResult.valid ||
893
+ (!rollupStatus.validationResult.valid &&
894
+ !validationResult.valid &&
895
+ rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber)
896
+ ) {
897
+ rollupStatus.validationResult = validationResult;
898
+ }
899
+
900
+ if (!validationResult.valid) {
901
+ this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
902
+ checkpointHash: published.checkpoint.hash(),
903
+ l1BlockNumber: published.l1.blockNumber,
904
+ ...pick(validationResult, 'reason'),
905
+ });
906
+
907
+ // Emit event for invalid block detection
908
+ this.emit(L2BlockSourceEvents.InvalidAttestationsBlockDetected, {
909
+ type: L2BlockSourceEvents.InvalidAttestationsBlockDetected,
910
+ validationResult,
911
+ });
912
+
913
+ // We keep consuming checkpoints if we find an invalid one, since we do not listen for CheckpointInvalidated events
914
+ // We just pretend the invalid ones are not there and keep consuming the next checkpoints
915
+ // Note that this breaks if the committee ever attests to a descendant of an invalid checkpoint
916
+ continue;
917
+ }
918
+
919
+ validCheckpoints.push(published);
920
+ this.log.debug(
921
+ `Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`,
922
+ {
923
+ checkpointHash: published.checkpoint.hash(),
924
+ l1BlockNumber: published.l1.blockNumber,
925
+ ...published.checkpoint.header.toInspect(),
926
+ blocks: published.checkpoint.blocks.map(b => b.getStats()),
927
+ },
928
+ );
520
929
  }
521
930
 
522
- const [processDuration] = await elapsed(() => this.store.addBlocks(retrievedBlocks));
523
- this.instrumentation.processNewBlocks(
524
- processDuration / retrievedBlocks.length,
525
- retrievedBlocks.map(b => b.data),
526
- );
931
+ try {
932
+ const updatedValidationResult =
933
+ rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
934
+ const [processDuration] = await elapsed(() => this.addCheckpoints(validCheckpoints, updatedValidationResult));
935
+ this.instrumentation.processNewBlocks(
936
+ processDuration / validCheckpoints.length,
937
+ validCheckpoints.flatMap(c => c.checkpoint.blocks),
938
+ );
939
+ } catch (err) {
940
+ if (err instanceof InitialBlockNumberNotSequentialError) {
941
+ const { previousBlockNumber, newBlockNumber } = err;
942
+ const previousBlock = previousBlockNumber
943
+ ? await this.store.getPublishedBlock(previousBlockNumber)
944
+ : undefined;
945
+ const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
946
+ await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
947
+ this.log.warn(
948
+ `Attempting to insert block ${newBlockNumber} with previous block ${previousBlockNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`,
949
+ {
950
+ previousBlockNumber,
951
+ previousBlockHash: await previousBlock?.block.hash(),
952
+ newBlockNumber,
953
+ updatedL1SyncPoint,
954
+ },
955
+ );
956
+ }
957
+ throw err;
958
+ }
527
959
 
528
- for (const block of retrievedBlocks) {
529
- this.log.info(`Downloaded L2 block ${block.data.number}`, {
530
- blockHash: block.data.hash(),
531
- blockNumber: block.data.number,
532
- txCount: block.data.body.txEffects.length,
533
- globalVariables: block.data.header.globalVariables.toInspect(),
960
+ for (const checkpoint of validCheckpoints) {
961
+ this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
962
+ checkpointHash: checkpoint.checkpoint.hash(),
963
+ checkpointNumber: checkpoint.checkpoint.number,
964
+ blockCount: checkpoint.checkpoint.blocks.length,
965
+ txCount: checkpoint.checkpoint.blocks.reduce((acc, b) => acc + b.body.txEffects.length, 0),
966
+ header: checkpoint.checkpoint.header.toInspect(),
967
+ archiveRoot: checkpoint.checkpoint.archive.root.toString(),
968
+ archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex,
534
969
  });
535
970
  }
971
+ lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
972
+ lastL1BlockWithCheckpoint = publishedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
536
973
  } while (searchEndBlock < currentL1BlockNumber);
537
974
 
538
975
  // Important that we update AFTER inserting the blocks.
539
- await updateProvenBlock();
976
+ await updateProvenCheckpoint();
977
+
978
+ return { ...rollupStatus, lastRetrievedCheckpoint, lastL1BlockWithCheckpoint };
979
+ }
980
+
981
+ private async checkForNewCheckpointsBeforeL1SyncPoint(
982
+ status: RollupStatus,
983
+ blocksSynchedTo: bigint,
984
+ currentL1BlockNumber: bigint,
985
+ ) {
986
+ const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
987
+ // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
988
+ // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
989
+ const latestLocalCheckpointNumber =
990
+ lastRetrievedCheckpoint?.checkpoint.number ?? (await this.getSynchedCheckpointNumber());
991
+ if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
992
+ // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
993
+ // but still haven't reached the pending checkpoint according to the call to the rollup contract.
994
+ // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
995
+ // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
996
+ // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
997
+ const latestLocalCheckpoint =
998
+ lastRetrievedCheckpoint ??
999
+ (latestLocalCheckpointNumber > 0
1000
+ ? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c]) => c)
1001
+ : undefined);
1002
+ const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
1003
+ const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
1004
+ this.log.warn(
1005
+ `Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` +
1006
+ `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`,
1007
+ {
1008
+ latestLocalCheckpointNumber,
1009
+ latestLocalCheckpointArchive,
1010
+ blocksSynchedTo,
1011
+ currentL1BlockNumber,
1012
+ ...status,
1013
+ },
1014
+ );
1015
+ await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1016
+ } else {
1017
+ this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
1018
+ latestLocalCheckpointNumber,
1019
+ pendingCheckpointNumber,
1020
+ });
1021
+ }
1022
+ }
540
1023
 
541
- return { provenBlockNumber };
1024
+ /** Resumes the archiver after a stop. */
1025
+ public resume() {
1026
+ if (this.runningPromise.isRunning()) {
1027
+ this.log.warn(`Archiver already running`);
1028
+ }
1029
+ this.log.info(`Restarting archiver`);
1030
+ this.runningPromise.start();
542
1031
  }
543
1032
 
544
1033
  /**
@@ -547,16 +1036,24 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
547
1036
  */
548
1037
  public async stop(): Promise<void> {
549
1038
  this.log.debug('Stopping...');
550
- await this.runningPromise?.stop();
1039
+ await this.runningPromise.stop();
551
1040
 
552
1041
  this.log.info('Stopped.');
553
1042
  return Promise.resolve();
554
1043
  }
555
1044
 
1045
+ public backupTo(destPath: string): Promise<string> {
1046
+ return this.dataStore.backupTo(destPath);
1047
+ }
1048
+
556
1049
  public getL1Constants(): Promise<L1RollupConstants> {
557
1050
  return Promise.resolve(this.l1constants);
558
1051
  }
559
1052
 
1053
+ public getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }> {
1054
+ return Promise.resolve({ genesisArchiveRoot: this.l1constants.genesisArchiveRoot });
1055
+ }
1056
+
560
1057
  public getRollupAddress(): Promise<EthAddress> {
561
1058
  return Promise.resolve(this.l1Addresses.rollupAddress);
562
1059
  }
@@ -565,38 +1062,34 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
565
1062
  return Promise.resolve(this.l1Addresses.registryAddress);
566
1063
  }
567
1064
 
568
- public getL1BlockNumber(): bigint {
569
- const l1BlockNumber = this.l1BlockNumber;
570
- if (!l1BlockNumber) {
571
- throw new Error('L1 block number not yet available. Complete an initial sync first.');
572
- }
573
- return l1BlockNumber;
1065
+ public getL1BlockNumber(): bigint | undefined {
1066
+ return this.l1BlockNumber;
574
1067
  }
575
1068
 
576
- public getL1Timestamp(): bigint {
577
- const l1Timestamp = this.l1Timestamp;
578
- if (!l1Timestamp) {
579
- throw new Error('L1 timestamp not yet available. Complete an initial sync first.');
580
- }
581
- return l1Timestamp;
1069
+ public getL1Timestamp(): Promise<bigint | undefined> {
1070
+ return Promise.resolve(this.l1Timestamp);
582
1071
  }
583
1072
 
584
- public getL2SlotNumber(): Promise<bigint> {
585
- return Promise.resolve(getSlotAtTimestamp(this.getL1Timestamp(), this.l1constants));
1073
+ public getL2SlotNumber(): Promise<SlotNumber | undefined> {
1074
+ return Promise.resolve(
1075
+ this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants),
1076
+ );
586
1077
  }
587
1078
 
588
- public getL2EpochNumber(): Promise<bigint> {
589
- return Promise.resolve(getEpochNumberAtTimestamp(this.getL1Timestamp(), this.l1constants));
1079
+ public getL2EpochNumber(): Promise<EpochNumber | undefined> {
1080
+ return Promise.resolve(
1081
+ this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants),
1082
+ );
590
1083
  }
591
1084
 
592
- public async getBlocksForEpoch(epochNumber: bigint): Promise<L2Block[]> {
1085
+ public async getBlocksForEpoch(epochNumber: EpochNumber): Promise<L2Block[]> {
593
1086
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
594
1087
  const blocks: L2Block[] = [];
595
1088
 
596
1089
  // Walk the list of blocks backwards and filter by slots matching the requested epoch.
597
1090
  // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
598
1091
  let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
599
- const slot = (b: L2Block) => b.header.globalVariables.slotNumber.toBigInt();
1092
+ const slot = (b: L2Block) => b.header.globalVariables.slotNumber;
600
1093
  while (block && slot(block) >= start) {
601
1094
  if (slot(block) <= end) {
602
1095
  blocks.push(block);
@@ -607,10 +1100,28 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
607
1100
  return blocks.reverse();
608
1101
  }
609
1102
 
610
- public async isEpochComplete(epochNumber: bigint): Promise<boolean> {
1103
+ public async getBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
1104
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1105
+ const blocks: BlockHeader[] = [];
1106
+
1107
+ // Walk the list of blocks backwards and filter by slots matching the requested epoch.
1108
+ // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
1109
+ let number = await this.store.getSynchedL2BlockNumber();
1110
+ let header = await this.getBlockHeader(number);
1111
+ const slot = (b: BlockHeader) => b.globalVariables.slotNumber;
1112
+ while (header && slot(header) >= start) {
1113
+ if (slot(header) <= end) {
1114
+ blocks.push(header);
1115
+ }
1116
+ header = await this.getBlockHeader(--number);
1117
+ }
1118
+ return blocks.reverse();
1119
+ }
1120
+
1121
+ public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
611
1122
  // The epoch is complete if the current L2 block is the last one in the epoch (or later)
612
1123
  const header = await this.getBlockHeader('latest');
613
- const slot = header?.globalVariables.slotNumber.toBigInt();
1124
+ const slot = header ? header.globalVariables.slotNumber : undefined;
614
1125
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
615
1126
  if (slot && slot >= endSlot) {
616
1127
  return true;
@@ -635,6 +1146,82 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
635
1146
  return l1Timestamp + leeway >= endTimestamp;
636
1147
  }
637
1148
 
1149
+ /** Returns whether the archiver has completed an initial sync run successfully. */
1150
+ public isInitialSyncComplete(): boolean {
1151
+ return this.initialSyncComplete;
1152
+ }
1153
+
1154
+ public async getPublishedCheckpoints(from: number, limit: number, proven?: boolean): Promise<PublishedCheckpoint[]> {
1155
+ const blocks = await this.getPublishedBlocks(from, limit, proven);
1156
+ return blocks.map(b => b.toPublishedCheckpoint());
1157
+ }
1158
+
1159
+ public async getCheckpoints(from: number, limit: number, proven?: boolean): Promise<Checkpoint[]> {
1160
+ const published = await this.getPublishedCheckpoints(from, limit, proven);
1161
+ return published.map(p => p.checkpoint);
1162
+ }
1163
+
1164
+ public async getCheckpoint(number: number): Promise<Checkpoint | undefined> {
1165
+ if (number < 0) {
1166
+ number = await this.getSynchedCheckpointNumber();
1167
+ }
1168
+ if (number === 0) {
1169
+ return undefined;
1170
+ }
1171
+ const published = await this.getPublishedCheckpoints(number, 1);
1172
+ return published[0]?.checkpoint;
1173
+ }
1174
+
1175
+ public async getCheckpointHeader(number: number | 'latest'): Promise<CheckpointHeader | undefined> {
1176
+ if (number === 'latest') {
1177
+ number = await this.getSynchedCheckpointNumber();
1178
+ }
1179
+ if (number === 0) {
1180
+ return undefined;
1181
+ }
1182
+ const checkpoint = await this.getCheckpoint(number);
1183
+ return checkpoint?.header;
1184
+ }
1185
+
1186
+ public getCheckpointNumber(): Promise<number> {
1187
+ return this.getSynchedCheckpointNumber();
1188
+ }
1189
+
1190
+ public getSynchedCheckpointNumber(): Promise<number> {
1191
+ // TODO: Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
1192
+ return this.store.getSynchedL2BlockNumber();
1193
+ }
1194
+
1195
+ public getProvenCheckpointNumber(): Promise<number> {
1196
+ // TODO: Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
1197
+ return this.store.getProvenL2BlockNumber();
1198
+ }
1199
+
1200
+ public setProvenCheckpointNumber(checkpointNumber: number): Promise<void> {
1201
+ // TODO: Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
1202
+ return this.store.setProvenL2BlockNumber(checkpointNumber);
1203
+ }
1204
+
1205
+ public unwindCheckpoints(from: number, checkpointsToUnwind: number): Promise<boolean> {
1206
+ // TODO: This only works if we have one block per checkpoint.
1207
+ return this.store.unwindBlocks(from, checkpointsToUnwind);
1208
+ }
1209
+
1210
+ public getLastBlockNumberInCheckpoint(checkpointNumber: number): Promise<number> {
1211
+ // TODO: Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
1212
+ return Promise.resolve(checkpointNumber);
1213
+ }
1214
+
1215
+ public addCheckpoints(
1216
+ checkpoints: PublishedCheckpoint[],
1217
+ pendingChainValidationStatus?: ValidateBlockResult,
1218
+ ): Promise<boolean> {
1219
+ return this.store.addBlocks(
1220
+ checkpoints.map(p => PublishedL2Block.fromPublishedCheckpoint(p)),
1221
+ pendingChainValidationStatus,
1222
+ );
1223
+ }
1224
+
638
1225
  /**
639
1226
  * Gets up to `limit` amount of L2 blocks starting from `from`.
640
1227
  * @param from - Number of the first block to return (inclusive).
@@ -642,11 +1229,32 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
642
1229
  * @param proven - If true, only return blocks that have been proven.
643
1230
  * @returns The requested L2 blocks.
644
1231
  */
645
- public async getBlocks(from: number, limit: number, proven?: boolean): Promise<L2Block[]> {
1232
+ public getBlocks(from: number, limit: number, proven?: boolean): Promise<L2Block[]> {
1233
+ return this.getPublishedBlocks(from, limit, proven).then(blocks => blocks.map(b => b.block));
1234
+ }
1235
+
1236
+ /** Equivalent to getBlocks but includes publish data. */
1237
+ public async getPublishedBlocks(from: number, limit: number, proven?: boolean): Promise<PublishedL2Block[]> {
646
1238
  const limitWithProven = proven
647
1239
  ? Math.min(limit, Math.max((await this.store.getProvenL2BlockNumber()) - from + 1, 0))
648
1240
  : limit;
649
- return limitWithProven === 0 ? [] : (await this.store.getBlocks(from, limitWithProven)).map(b => b.data);
1241
+ return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
1242
+ }
1243
+
1244
+ public getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
1245
+ return this.store.getPublishedBlockByHash(blockHash);
1246
+ }
1247
+
1248
+ public getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
1249
+ return this.store.getPublishedBlockByArchive(archive);
1250
+ }
1251
+
1252
+ public getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
1253
+ return this.store.getBlockHeaderByHash(blockHash);
1254
+ }
1255
+
1256
+ public getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
1257
+ return this.store.getBlockHeaderByArchive(archive);
650
1258
  }
651
1259
 
652
1260
  /**
@@ -659,11 +1267,11 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
659
1267
  if (number < 0) {
660
1268
  number = await this.store.getSynchedL2BlockNumber();
661
1269
  }
662
- if (number == 0) {
1270
+ if (number === 0) {
663
1271
  return undefined;
664
1272
  }
665
- const blocks = await this.store.getBlocks(number, 1);
666
- return blocks.length === 0 ? undefined : blocks[0].data;
1273
+ const publishedBlock = await this.store.getPublishedBlock(number);
1274
+ return publishedBlock?.block;
667
1275
  }
668
1276
 
669
1277
  public async getBlockHeader(number: number | 'latest'): Promise<BlockHeader | undefined> {
@@ -685,29 +1293,6 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
685
1293
  return this.store.getSettledTxReceipt(txHash);
686
1294
  }
687
1295
 
688
- /**
689
- * Gets the public function data for a contract.
690
- * @param address - The contract address containing the function to fetch.
691
- * @param selector - The function selector of the function to fetch.
692
- * @returns The public function data (if found).
693
- */
694
- public async getPublicFunction(
695
- address: AztecAddress,
696
- selector: FunctionSelector,
697
- ): Promise<PublicFunction | undefined> {
698
- const instance = await this.getContract(address);
699
- if (!instance) {
700
- throw new Error(`Contract ${address.toString()} not found`);
701
- }
702
- const contractClass = await this.getContractClass(instance.currentContractClassId);
703
- if (!contractClass) {
704
- throw new Error(
705
- `Contract class ${instance.currentContractClassId.toString()} for ${address.toString()} not found`,
706
- );
707
- }
708
- return contractClass.publicFunctions.find(f => f.selector.equals(selector));
709
- }
710
-
711
1296
  /**
712
1297
  * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
713
1298
  * @param from - The block number from which to begin retrieving logs.
@@ -728,17 +1313,6 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
728
1313
  return this.store.getLogsByTags(tags);
729
1314
  }
730
1315
 
731
- /**
732
- * Returns the provided nullifier indexes scoped to the block
733
- * they were first included in, or undefined if they're not present in the tree
734
- * @param blockNumber Max block number to search for the nullifiers
735
- * @param nullifiers Nullifiers to get
736
- * @returns The block scoped indexes of the provided nullifiers, or undefined if the nullifier doesn't exist in the tree
737
- */
738
- findNullifiersIndexesWithBlock(blockNumber: number, nullifiers: Fr[]): Promise<(InBlock<bigint> | undefined)[]> {
739
- return this.store.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
740
- }
741
-
742
1316
  /**
743
1317
  * Gets public logs based on the provided filter.
744
1318
  * @param filter - The filter to apply to the logs.
@@ -782,8 +1356,20 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
782
1356
  return this.store.getBytecodeCommitment(id);
783
1357
  }
784
1358
 
785
- public getContract(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
786
- return this.store.getContractInstance(address);
1359
+ public async getContract(
1360
+ address: AztecAddress,
1361
+ maybeTimestamp?: UInt64,
1362
+ ): Promise<ContractInstanceWithAddress | undefined> {
1363
+ let timestamp;
1364
+ if (maybeTimestamp === undefined) {
1365
+ const latestBlockHeader = await this.getBlockHeader('latest');
1366
+ // If we get undefined block header, it means that the archiver has not yet synced any block so we default to 0.
1367
+ timestamp = latestBlockHeader ? latestBlockHeader.globalVariables.timestamp : 0n;
1368
+ } else {
1369
+ timestamp = maybeTimestamp;
1370
+ }
1371
+
1372
+ return this.store.getContractInstance(address, timestamp);
787
1373
  }
788
1374
 
789
1375
  /**
@@ -791,7 +1377,7 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
791
1377
  * @param blockNumber - L2 block number to get messages for.
792
1378
  * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
793
1379
  */
794
- getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
1380
+ getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
795
1381
  return this.store.getL1ToL2Messages(blockNumber);
796
1382
  }
797
1383
 
@@ -808,22 +1394,20 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
808
1394
  return this.store.getContractClassIds();
809
1395
  }
810
1396
 
811
- // TODO(#10007): Remove this method
812
- async addContractClass(contractClass: ContractClassPublic): Promise<void> {
813
- await this.store.addContractClasses(
814
- [contractClass],
815
- [await computePublicBytecodeCommitment(contractClass.packedBytecode)],
816
- 0,
817
- );
818
- return;
1397
+ registerContractFunctionSignatures(signatures: string[]): Promise<void> {
1398
+ return this.store.registerContractFunctionSignatures(signatures);
1399
+ }
1400
+
1401
+ getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
1402
+ return this.store.getDebugFunctionName(address, selector);
819
1403
  }
820
1404
 
821
- registerContractFunctionSignatures(address: AztecAddress, signatures: string[]): Promise<void> {
822
- return this.store.registerContractFunctionSignatures(address, signatures);
1405
+ async getPendingChainValidationStatus(): Promise<ValidateBlockResult> {
1406
+ return (await this.store.getPendingChainValidationStatus()) ?? { valid: true };
823
1407
  }
824
1408
 
825
- getContractFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
826
- return this.store.getContractFunctionName(address, selector);
1409
+ isPendingChainInvalid(): Promise<boolean> {
1410
+ return this.getPendingChainValidationStatus().then(status => !status.valid);
827
1411
  }
828
1412
 
829
1413
  async getL2Tips(): Promise<L2Tips> {
@@ -832,9 +1416,15 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
832
1416
  this.getProvenBlockNumber(),
833
1417
  ] as const);
834
1418
 
835
- const [latestBlockHeader, provenBlockHeader] = await Promise.all([
1419
+ // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
1420
+ // We just force it 2 epochs worth of proven data for now.
1421
+ // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
1422
+ const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
1423
+
1424
+ const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
836
1425
  latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
837
1426
  provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
1427
+ finalizedBlockNumber > 0 ? this.getBlockHeader(finalizedBlockNumber) : undefined,
838
1428
  ] as const);
839
1429
 
840
1430
  if (latestBlockNumber > 0 && !latestBlockHeader) {
@@ -847,9 +1437,16 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
847
1437
  );
848
1438
  }
849
1439
 
1440
+ if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
1441
+ throw new Error(
1442
+ `Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`,
1443
+ );
1444
+ }
1445
+
850
1446
  const latestBlockHeaderHash = await latestBlockHeader?.hash();
851
1447
  const provenBlockHeaderHash = await provenBlockHeader?.hash();
852
- const finalizedBlockHeaderHash = await provenBlockHeader?.hash();
1448
+ const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
1449
+
853
1450
  return {
854
1451
  latest: {
855
1452
  number: latestBlockNumber,
@@ -860,11 +1457,45 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
860
1457
  hash: provenBlockHeaderHash?.toString(),
861
1458
  } as L2BlockId,
862
1459
  finalized: {
863
- number: provenBlockNumber,
1460
+ number: finalizedBlockNumber,
864
1461
  hash: finalizedBlockHeaderHash?.toString(),
865
1462
  } as L2BlockId,
866
1463
  };
867
1464
  }
1465
+
1466
+ public async rollbackTo(targetL2BlockNumber: number): Promise<void> {
1467
+ const currentBlocks = await this.getL2Tips();
1468
+ const currentL2Block = currentBlocks.latest.number;
1469
+ const currentProvenBlock = currentBlocks.proven.number;
1470
+ // const currentFinalizedBlock = currentBlocks.finalized.number;
1471
+
1472
+ if (targetL2BlockNumber >= currentL2Block) {
1473
+ throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
1474
+ }
1475
+ const blocksToUnwind = currentL2Block - targetL2BlockNumber;
1476
+ const targetL2Block = await this.store.getPublishedBlock(targetL2BlockNumber);
1477
+ if (!targetL2Block) {
1478
+ throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1479
+ }
1480
+ const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1481
+ const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1482
+ this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
1483
+ await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
1484
+ this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
1485
+ await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
1486
+ this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1487
+ await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1488
+ await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
1489
+ if (targetL2BlockNumber < currentProvenBlock) {
1490
+ this.log.info(`Clearing proven L2 block number`);
1491
+ await this.store.setProvenL2BlockNumber(0);
1492
+ }
1493
+ // TODO(palla/reorg): Set the finalized block when we add support for it.
1494
+ // if (targetL2BlockNumber < currentFinalizedBlock) {
1495
+ // this.log.info(`Clearing finalized L2 block number`);
1496
+ // await this.store.setFinalizedL2BlockNumber(0);
1497
+ // }
1498
+ }
868
1499
  }
869
1500
 
870
1501
  enum Operation {
@@ -878,14 +1509,12 @@ enum Operation {
878
1509
  * I would have preferred to not have this type. But it is useful for handling the logic that any
879
1510
  * store would need to include otherwise while exposing fewer functions and logic directly to the archiver.
880
1511
  */
881
- class ArchiverStoreHelper
1512
+ export class ArchiverStoreHelper
882
1513
  implements
883
1514
  Omit<
884
1515
  ArchiverDataStore,
885
1516
  | 'addLogs'
886
1517
  | 'deleteLogs'
887
- | 'addNullifiers'
888
- | 'deleteNullifiers'
889
1518
  | 'addContractClasses'
890
1519
  | 'deleteContractClasses'
891
1520
  | 'addContractInstances'
@@ -893,31 +1522,26 @@ class ArchiverStoreHelper
893
1522
  | 'addContractInstanceUpdates'
894
1523
  | 'deleteContractInstanceUpdates'
895
1524
  | 'addFunctions'
1525
+ | 'backupTo'
1526
+ | 'close'
1527
+ | 'transactionAsync'
1528
+ | 'addBlocks'
896
1529
  >
897
1530
  {
898
1531
  #log = createLogger('archiver:block-helper');
899
1532
 
900
- constructor(private readonly store: ArchiverDataStore) {}
901
-
902
- // TODO(#10007): Remove this method
903
- addContractClasses(
904
- contractClasses: ContractClassPublic[],
905
- bytecodeCommitments: Fr[],
906
- blockNum: number,
907
- ): Promise<boolean> {
908
- return this.store.addContractClasses(contractClasses, bytecodeCommitments, blockNum);
909
- }
1533
+ constructor(protected readonly store: ArchiverDataStore) {}
910
1534
 
911
1535
  /**
912
- * Extracts and stores contract classes out of ContractClassRegistered events emitted by the class registerer contract.
1536
+ * Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
913
1537
  * @param allLogs - All logs emitted in a bunch of blocks.
914
1538
  */
915
- async #updateRegisteredContractClasses(allLogs: ContractClassLog[], blockNum: number, operation: Operation) {
916
- const contractClassRegisteredEvents = allLogs
917
- .filter(log => ContractClassRegisteredEvent.isContractClassRegisteredEvent(log))
918
- .map(log => ContractClassRegisteredEvent.fromLog(log));
1539
+ async #updatePublishedContractClasses(allLogs: ContractClassLog[], blockNum: number, operation: Operation) {
1540
+ const contractClassPublishedEvents = allLogs
1541
+ .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
1542
+ .map(log => ContractClassPublishedEvent.fromLog(log));
919
1543
 
920
- const contractClasses = await Promise.all(contractClassRegisteredEvents.map(e => e.toContractClassPublic()));
1544
+ const contractClasses = await Promise.all(contractClassPublishedEvents.map(e => e.toContractClassPublic()));
921
1545
  if (contractClasses.length > 0) {
922
1546
  contractClasses.forEach(c => this.#log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
923
1547
  if (operation == Operation.Store) {
@@ -934,13 +1558,13 @@ class ArchiverStoreHelper
934
1558
  }
935
1559
 
936
1560
  /**
937
- * Extracts and stores contract instances out of ContractInstanceDeployed events emitted by the canonical deployer contract.
1561
+ * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
938
1562
  * @param allLogs - All logs emitted in a bunch of blocks.
939
1563
  */
940
1564
  async #updateDeployedContractInstances(allLogs: PrivateLog[], blockNum: number, operation: Operation) {
941
1565
  const contractInstances = allLogs
942
- .filter(log => ContractInstanceDeployedEvent.isContractInstanceDeployedEvent(log))
943
- .map(log => ContractInstanceDeployedEvent.fromLog(log))
1566
+ .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
1567
+ .map(log => ContractInstancePublishedEvent.fromLog(log))
944
1568
  .map(e => e.toContractInstance());
945
1569
  if (contractInstances.length > 0) {
946
1570
  contractInstances.forEach(c =>
@@ -956,10 +1580,12 @@ class ArchiverStoreHelper
956
1580
  }
957
1581
 
958
1582
  /**
959
- * Extracts and stores contract instances out of ContractInstanceDeployed events emitted by the canonical deployer contract.
1583
+ * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
960
1584
  * @param allLogs - All logs emitted in a bunch of blocks.
1585
+ * @param timestamp - Timestamp at which the updates were scheduled.
1586
+ * @param operation - The operation to perform on the contract instance updates (Store or Delete).
961
1587
  */
962
- async #updateUpdatedContractInstances(allLogs: PublicLog[], blockNum: number, operation: Operation) {
1588
+ async #updateUpdatedContractInstances(allLogs: PublicLog[], timestamp: UInt64, operation: Operation) {
963
1589
  const contractUpdates = allLogs
964
1590
  .filter(log => ContractInstanceUpdatedEvent.isContractInstanceUpdatedEvent(log))
965
1591
  .map(log => ContractInstanceUpdatedEvent.fromLog(log))
@@ -970,16 +1596,16 @@ class ArchiverStoreHelper
970
1596
  this.#log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`),
971
1597
  );
972
1598
  if (operation == Operation.Store) {
973
- return await this.store.addContractInstanceUpdates(contractUpdates, blockNum);
1599
+ return await this.store.addContractInstanceUpdates(contractUpdates, timestamp);
974
1600
  } else if (operation == Operation.Delete) {
975
- return await this.store.deleteContractInstanceUpdates(contractUpdates, blockNum);
1601
+ return await this.store.deleteContractInstanceUpdates(contractUpdates, timestamp);
976
1602
  }
977
1603
  }
978
1604
  return true;
979
1605
  }
980
1606
 
981
1607
  /**
982
- * Stores the functions that was broadcasted individually
1608
+ * Stores the functions that were broadcasted individually
983
1609
  *
984
1610
  * @dev Beware that there is not a delete variant of this, since they are added to contract classes
985
1611
  * and will be deleted as part of the class if needed.
@@ -989,17 +1615,17 @@ class ArchiverStoreHelper
989
1615
  * @returns
990
1616
  */
991
1617
  async #storeBroadcastedIndividualFunctions(allLogs: ContractClassLog[], _blockNum: number) {
992
- // Filter out private and unconstrained function broadcast events
1618
+ // Filter out private and utility function broadcast events
993
1619
  const privateFnEvents = allLogs
994
1620
  .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
995
1621
  .map(log => PrivateFunctionBroadcastedEvent.fromLog(log));
996
- const unconstrainedFnEvents = allLogs
997
- .filter(log => UnconstrainedFunctionBroadcastedEvent.isUnconstrainedFunctionBroadcastedEvent(log))
998
- .map(log => UnconstrainedFunctionBroadcastedEvent.fromLog(log));
1622
+ const utilityFnEvents = allLogs
1623
+ .filter(log => UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log))
1624
+ .map(log => UtilityFunctionBroadcastedEvent.fromLog(log));
999
1625
 
1000
1626
  // Group all events by contract class id
1001
1627
  for (const [classIdString, classEvents] of Object.entries(
1002
- groupBy([...privateFnEvents, ...unconstrainedFnEvents], e => e.contractClassId.toString()),
1628
+ groupBy([...privateFnEvents, ...utilityFnEvents], e => e.contractClassId.toString()),
1003
1629
  )) {
1004
1630
  const contractClassId = Fr.fromHexString(classIdString);
1005
1631
  const contractClass = await this.getContractClass(contractClassId);
@@ -1008,27 +1634,27 @@ class ArchiverStoreHelper
1008
1634
  continue;
1009
1635
  }
1010
1636
 
1011
- // Split private and unconstrained functions, and filter out invalid ones
1637
+ // Split private and utility functions, and filter out invalid ones
1012
1638
  const allFns = classEvents.map(e => e.toFunctionWithMembershipProof());
1013
1639
  const privateFns = allFns.filter(
1014
- (fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'unconstrainedFunctionsArtifactTreeRoot' in fn,
1640
+ (fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'utilityFunctionsTreeRoot' in fn,
1015
1641
  );
1016
- const unconstrainedFns = allFns.filter(
1017
- (fn): fn is UnconstrainedFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
1642
+ const utilityFns = allFns.filter(
1643
+ (fn): fn is UtilityFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
1018
1644
  );
1019
1645
 
1020
1646
  const privateFunctionsWithValidity = await Promise.all(
1021
1647
  privateFns.map(async fn => ({ fn, valid: await isValidPrivateFunctionMembershipProof(fn, contractClass) })),
1022
1648
  );
1023
1649
  const validPrivateFns = privateFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
1024
- const unconstrainedFunctionsWithValidity = await Promise.all(
1025
- unconstrainedFns.map(async fn => ({
1650
+ const utilityFunctionsWithValidity = await Promise.all(
1651
+ utilityFns.map(async fn => ({
1026
1652
  fn,
1027
- valid: await isValidUnconstrainedFunctionMembershipProof(fn, contractClass),
1653
+ valid: await isValidUtilityFunctionMembershipProof(fn, contractClass),
1028
1654
  })),
1029
1655
  );
1030
- const validUnconstrainedFns = unconstrainedFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
1031
- const validFnCount = validPrivateFns.length + validUnconstrainedFns.length;
1656
+ const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
1657
+ const validFnCount = validPrivateFns.length + validUtilityFns.length;
1032
1658
  if (validFnCount !== allFns.length) {
1033
1659
  this.#log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
1034
1660
  }
@@ -1037,85 +1663,120 @@ class ArchiverStoreHelper
1037
1663
  if (validFnCount > 0) {
1038
1664
  this.#log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
1039
1665
  }
1040
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUnconstrainedFns);
1666
+ return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
1041
1667
  }
1042
1668
  return true;
1043
1669
  }
1044
1670
 
1045
- async addBlocks(blocks: L1Published<L2Block>[]): Promise<boolean> {
1046
- const opResults = await Promise.all([
1047
- this.store.addLogs(blocks.map(block => block.data)),
1048
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1049
- ...blocks.map(async block => {
1050
- const contractClassLogs = block.data.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1051
- // ContractInstanceDeployed event logs are broadcast in privateLogs.
1052
- const privateLogs = block.data.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
1053
- const publicLogs = block.data.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
1054
- return (
1055
- await Promise.all([
1056
- this.#updateRegisteredContractClasses(contractClassLogs, block.data.number, Operation.Store),
1057
- this.#updateDeployedContractInstances(privateLogs, block.data.number, Operation.Store),
1058
- this.#updateUpdatedContractInstances(publicLogs, block.data.number, Operation.Store),
1059
- this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.data.number),
1060
- ])
1061
- ).every(Boolean);
1062
- }),
1063
- this.store.addNullifiers(blocks.map(block => block.data)),
1064
- this.store.addBlocks(blocks),
1065
- ]);
1066
-
1067
- return opResults.every(Boolean);
1671
+ public addBlocks(blocks: PublishedL2Block[], pendingChainValidationStatus?: ValidateBlockResult): Promise<boolean> {
1672
+ // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1673
+ // or if the previous block is not in the store.
1674
+ return this.store.transactionAsync(async () => {
1675
+ await this.store.addBlocks(blocks);
1676
+
1677
+ const opResults = await Promise.all([
1678
+ // Update the pending chain validation status if provided
1679
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1680
+ // Add any logs emitted during the retrieved blocks
1681
+ this.store.addLogs(blocks.map(block => block.block)),
1682
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1683
+ ...blocks.map(async block => {
1684
+ const contractClassLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1685
+ // ContractInstancePublished event logs are broadcast in privateLogs.
1686
+ const privateLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
1687
+ const publicLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
1688
+ return (
1689
+ await Promise.all([
1690
+ this.#updatePublishedContractClasses(contractClassLogs, block.block.number, Operation.Store),
1691
+ this.#updateDeployedContractInstances(privateLogs, block.block.number, Operation.Store),
1692
+ this.#updateUpdatedContractInstances(
1693
+ publicLogs,
1694
+ block.block.header.globalVariables.timestamp,
1695
+ Operation.Store,
1696
+ ),
1697
+ this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.block.number),
1698
+ ])
1699
+ ).every(Boolean);
1700
+ }),
1701
+ ]);
1702
+
1703
+ return opResults.every(Boolean);
1704
+ });
1068
1705
  }
1069
1706
 
1070
- async unwindBlocks(from: number, blocksToUnwind: number): Promise<boolean> {
1707
+ public async unwindBlocks(from: number, blocksToUnwind: number): Promise<boolean> {
1071
1708
  const last = await this.getSynchedL2BlockNumber();
1072
1709
  if (from != last) {
1073
- throw new Error(`Can only remove from the tip`);
1710
+ throw new Error(`Cannot unwind blocks from block ${from} when the last block is ${last}`);
1711
+ }
1712
+ if (blocksToUnwind <= 0) {
1713
+ throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
1074
1714
  }
1075
1715
 
1076
1716
  // from - blocksToUnwind = the new head, so + 1 for what we need to remove
1077
- const blocks = await this.getBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1717
+ const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1078
1718
 
1079
1719
  const opResults = await Promise.all([
1720
+ // Prune rolls back to the last proven block, which is by definition valid
1721
+ this.store.setPendingChainValidationStatus({ valid: true }),
1080
1722
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1081
1723
  ...blocks.map(async block => {
1082
- const contractClassLogs = block.data.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1083
- // ContractInstanceDeployed event logs are broadcast in privateLogs.
1084
- const privateLogs = block.data.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
1085
- const publicLogs = block.data.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
1724
+ const contractClassLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
1725
+ // ContractInstancePublished event logs are broadcast in privateLogs.
1726
+ const privateLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
1727
+ const publicLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
1086
1728
 
1087
1729
  return (
1088
1730
  await Promise.all([
1089
- this.#updateRegisteredContractClasses(contractClassLogs, block.data.number, Operation.Delete),
1090
- this.#updateDeployedContractInstances(privateLogs, block.data.number, Operation.Delete),
1091
- this.#updateUpdatedContractInstances(publicLogs, block.data.number, Operation.Delete),
1731
+ this.#updatePublishedContractClasses(contractClassLogs, block.block.number, Operation.Delete),
1732
+ this.#updateDeployedContractInstances(privateLogs, block.block.number, Operation.Delete),
1733
+ this.#updateUpdatedContractInstances(
1734
+ publicLogs,
1735
+ block.block.header.globalVariables.timestamp,
1736
+ Operation.Delete,
1737
+ ),
1092
1738
  ])
1093
1739
  ).every(Boolean);
1094
1740
  }),
1095
1741
 
1096
- this.store.deleteLogs(blocks.map(b => b.data)),
1742
+ this.store.deleteLogs(blocks.map(b => b.block)),
1097
1743
  this.store.unwindBlocks(from, blocksToUnwind),
1098
1744
  ]);
1099
1745
 
1100
1746
  return opResults.every(Boolean);
1101
1747
  }
1102
1748
 
1103
- getBlocks(from: number, limit: number): Promise<L1Published<L2Block>[]> {
1104
- return this.store.getBlocks(from, limit);
1749
+ getPublishedBlocks(from: number, limit: number): Promise<PublishedL2Block[]> {
1750
+ return this.store.getPublishedBlocks(from, limit);
1751
+ }
1752
+ getPublishedBlock(number: number): Promise<PublishedL2Block | undefined> {
1753
+ return this.store.getPublishedBlock(number);
1754
+ }
1755
+ getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
1756
+ return this.store.getPublishedBlockByHash(blockHash);
1757
+ }
1758
+ getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
1759
+ return this.store.getPublishedBlockByArchive(archive);
1105
1760
  }
1106
1761
  getBlockHeaders(from: number, limit: number): Promise<BlockHeader[]> {
1107
1762
  return this.store.getBlockHeaders(from, limit);
1108
1763
  }
1109
- getTxEffect(txHash: TxHash): Promise<InBlock<TxEffect> | undefined> {
1764
+ getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
1765
+ return this.store.getBlockHeaderByHash(blockHash);
1766
+ }
1767
+ getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
1768
+ return this.store.getBlockHeaderByArchive(archive);
1769
+ }
1770
+ getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
1110
1771
  return this.store.getTxEffect(txHash);
1111
1772
  }
1112
1773
  getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
1113
1774
  return this.store.getSettledTxReceipt(txHash);
1114
1775
  }
1115
- addL1ToL2Messages(messages: DataRetrieval<InboxLeaf>): Promise<boolean> {
1776
+ addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
1116
1777
  return this.store.addL1ToL2Messages(messages);
1117
1778
  }
1118
- getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
1779
+ getL1ToL2Messages(blockNumber: number): Promise<Fr[]> {
1119
1780
  return this.store.getL1ToL2Messages(blockNumber);
1120
1781
  }
1121
1782
  getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
@@ -1124,11 +1785,8 @@ class ArchiverStoreHelper
1124
1785
  getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
1125
1786
  return this.store.getPrivateLogs(from, limit);
1126
1787
  }
1127
- getLogsByTags(tags: Fr[]): Promise<TxScopedL2Log[][]> {
1128
- return this.store.getLogsByTags(tags);
1129
- }
1130
- findNullifiersIndexesWithBlock(blockNumber: number, nullifiers: Fr[]): Promise<(InBlock<bigint> | undefined)[]> {
1131
- return this.store.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
1788
+ getLogsByTags(tags: Fr[], logsPerTag?: number): Promise<TxScopedL2Log[][]> {
1789
+ return this.store.getLogsByTags(tags, logsPerTag);
1132
1790
  }
1133
1791
  getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
1134
1792
  return this.store.getPublicLogs(filter);
@@ -1148,8 +1806,8 @@ class ArchiverStoreHelper
1148
1806
  setBlockSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
1149
1807
  return this.store.setBlockSynchedL1BlockNumber(l1BlockNumber);
1150
1808
  }
1151
- setMessageSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
1152
- return this.store.setMessageSynchedL1BlockNumber(l1BlockNumber);
1809
+ setMessageSynchedL1Block(l1Block: L1BlockId): Promise<void> {
1810
+ return this.store.setMessageSynchedL1Block(l1Block);
1153
1811
  }
1154
1812
  getSynchPoint(): Promise<ArchiverL1SynchPoint> {
1155
1813
  return this.store.getSynchPoint();
@@ -1160,22 +1818,41 @@ class ArchiverStoreHelper
1160
1818
  getBytecodeCommitment(contractClassId: Fr): Promise<Fr | undefined> {
1161
1819
  return this.store.getBytecodeCommitment(contractClassId);
1162
1820
  }
1163
- getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
1164
- return this.store.getContractInstance(address);
1821
+ getContractInstance(address: AztecAddress, timestamp: UInt64): Promise<ContractInstanceWithAddress | undefined> {
1822
+ return this.store.getContractInstance(address, timestamp);
1165
1823
  }
1166
1824
  getContractClassIds(): Promise<Fr[]> {
1167
1825
  return this.store.getContractClassIds();
1168
1826
  }
1169
- registerContractFunctionSignatures(address: AztecAddress, signatures: string[]): Promise<void> {
1170
- return this.store.registerContractFunctionSignatures(address, signatures);
1827
+ registerContractFunctionSignatures(signatures: string[]): Promise<void> {
1828
+ return this.store.registerContractFunctionSignatures(signatures);
1171
1829
  }
1172
- getContractFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
1173
- return this.store.getContractFunctionName(address, selector);
1830
+ getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
1831
+ return this.store.getDebugFunctionName(address, selector);
1174
1832
  }
1175
1833
  getTotalL1ToL2MessageCount(): Promise<bigint> {
1176
1834
  return this.store.getTotalL1ToL2MessageCount();
1177
1835
  }
1178
- estimateSize(): Promise<{ mappingSize: number; actualSize: number; numItems: number }> {
1836
+ estimateSize(): Promise<{ mappingSize: number; physicalFileSize: number; actualSize: number; numItems: number }> {
1179
1837
  return this.store.estimateSize();
1180
1838
  }
1839
+ rollbackL1ToL2MessagesToL2Block(targetBlockNumber: number): Promise<void> {
1840
+ return this.store.rollbackL1ToL2MessagesToL2Block(targetBlockNumber);
1841
+ }
1842
+ iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
1843
+ return this.store.iterateL1ToL2Messages(range);
1844
+ }
1845
+ removeL1ToL2Messages(startIndex: bigint): Promise<void> {
1846
+ return this.store.removeL1ToL2Messages(startIndex);
1847
+ }
1848
+ getLastL1ToL2Message(): Promise<InboxMessage | undefined> {
1849
+ return this.store.getLastL1ToL2Message();
1850
+ }
1851
+ getPendingChainValidationStatus(): Promise<ValidateBlockResult | undefined> {
1852
+ return this.store.getPendingChainValidationStatus();
1853
+ }
1854
+ setPendingChainValidationStatus(status: ValidateBlockResult | undefined): Promise<void> {
1855
+ this.#log.debug(`Setting pending chain validation status to valid ${status?.valid}`, status);
1856
+ return this.store.setPendingChainValidationStatus(status);
1857
+ }
1181
1858
  }