@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.03f7ef2

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