@aztec/archiver 0.0.1-commit.6d3c34e → 0.0.1-commit.7035c9bd6

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 (204) hide show
  1. package/README.md +156 -22
  2. package/dest/archiver.d.ts +138 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +732 -0
  5. package/dest/config.d.ts +30 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +11 -1
  8. package/dest/errors.d.ts +53 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +75 -0
  11. package/dest/factory.d.ts +8 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +90 -8
  14. package/dest/index.d.ts +11 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +9 -3
  17. package/dest/interfaces.d.ts +9 -0
  18. package/dest/interfaces.d.ts.map +1 -0
  19. package/dest/interfaces.js +3 -0
  20. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +35 -32
  23. package/dest/l1/calldata_retriever.d.ts +135 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +402 -0
  26. package/dest/l1/data_retrieval.d.ts +88 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/data_retrieval.js +54 -71
  29. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +5 -5
  32. package/dest/l1/spire_proposer.d.ts.map +1 -0
  33. package/dest/{archiver/l1 → l1}/spire_proposer.js +9 -17
  34. package/dest/{archiver/l1 → l1}/trace_tx.d.ts +1 -1
  35. package/dest/l1/trace_tx.d.ts.map +1 -0
  36. package/dest/l1/types.d.ts +12 -0
  37. package/dest/l1/types.d.ts.map +1 -0
  38. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +6 -3
  39. package/dest/l1/validate_trace.d.ts.map +1 -0
  40. package/dest/{archiver/l1 → l1}/validate_trace.js +13 -9
  41. package/dest/modules/data_source_base.d.ts +89 -0
  42. package/dest/modules/data_source_base.d.ts.map +1 -0
  43. package/dest/modules/data_source_base.js +216 -0
  44. package/dest/modules/data_store_updater.d.ts +88 -0
  45. package/dest/modules/data_store_updater.d.ts.map +1 -0
  46. package/dest/modules/data_store_updater.js +342 -0
  47. package/dest/modules/instrumentation.d.ts +50 -0
  48. package/dest/modules/instrumentation.d.ts.map +1 -0
  49. package/dest/{archiver → modules}/instrumentation.js +36 -12
  50. package/dest/modules/l1_synchronizer.d.ts +72 -0
  51. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  52. package/dest/modules/l1_synchronizer.js +1147 -0
  53. package/dest/{archiver → modules}/validation.d.ts +1 -1
  54. package/dest/modules/validation.d.ts.map +1 -0
  55. package/dest/{archiver → modules}/validation.js +6 -0
  56. package/dest/store/block_store.d.ts +195 -0
  57. package/dest/store/block_store.d.ts.map +1 -0
  58. package/dest/{archiver/kv_archiver_store → store}/block_store.js +248 -101
  59. package/dest/store/contract_class_store.d.ts +18 -0
  60. package/dest/store/contract_class_store.d.ts.map +1 -0
  61. package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +12 -8
  62. package/dest/store/contract_instance_store.d.ts +24 -0
  63. package/dest/store/contract_instance_store.d.ts.map +1 -0
  64. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +1 -1
  65. package/dest/store/kv_archiver_store.d.ts +367 -0
  66. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  67. package/dest/store/kv_archiver_store.js +481 -0
  68. package/dest/store/l2_tips_cache.d.ts +19 -0
  69. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  70. package/dest/store/l2_tips_cache.js +89 -0
  71. package/dest/store/log_store.d.ts +57 -0
  72. package/dest/store/log_store.d.ts.map +1 -0
  73. package/dest/store/log_store.js +533 -0
  74. package/dest/store/message_store.d.ts +44 -0
  75. package/dest/store/message_store.d.ts.map +1 -0
  76. package/dest/{archiver/kv_archiver_store → store}/message_store.js +14 -1
  77. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  78. package/dest/structs/data_retrieval.d.ts.map +1 -0
  79. package/dest/structs/inbox_message.d.ts +15 -0
  80. package/dest/structs/inbox_message.d.ts.map +1 -0
  81. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  82. package/dest/structs/published.d.ts.map +1 -0
  83. package/dest/test/fake_l1_state.d.ts +202 -0
  84. package/dest/test/fake_l1_state.d.ts.map +1 -0
  85. package/dest/test/fake_l1_state.js +455 -0
  86. package/dest/test/index.d.ts +2 -1
  87. package/dest/test/index.d.ts.map +1 -1
  88. package/dest/test/index.js +4 -1
  89. package/dest/test/mock_archiver.d.ts +2 -2
  90. package/dest/test/mock_archiver.d.ts.map +1 -1
  91. package/dest/test/mock_archiver.js +3 -3
  92. package/dest/test/mock_l2_block_source.d.ts +38 -19
  93. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  94. package/dest/test/mock_l2_block_source.js +183 -77
  95. package/dest/test/mock_structs.d.ts +81 -3
  96. package/dest/test/mock_structs.d.ts.map +1 -1
  97. package/dest/test/mock_structs.js +152 -7
  98. package/dest/test/noop_l1_archiver.d.ts +26 -0
  99. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  100. package/dest/test/noop_l1_archiver.js +72 -0
  101. package/package.json +16 -17
  102. package/src/archiver.ts +486 -0
  103. package/src/{archiver/config.ts → config.ts} +19 -1
  104. package/src/{archiver/errors.ts → errors.ts} +52 -24
  105. package/src/factory.ts +141 -10
  106. package/src/index.ts +11 -3
  107. package/src/interfaces.ts +9 -0
  108. package/src/l1/README.md +55 -0
  109. package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +45 -33
  110. package/src/l1/calldata_retriever.ts +511 -0
  111. package/src/{archiver/l1 → l1}/data_retrieval.ts +75 -94
  112. package/src/{archiver/l1 → l1}/spire_proposer.ts +7 -15
  113. package/src/{archiver/l1 → l1}/validate_trace.ts +24 -6
  114. package/src/modules/data_source_base.ts +333 -0
  115. package/src/modules/data_store_updater.ts +464 -0
  116. package/src/{archiver → modules}/instrumentation.ts +46 -14
  117. package/src/modules/l1_synchronizer.ts +967 -0
  118. package/src/{archiver → modules}/validation.ts +5 -0
  119. package/src/{archiver/kv_archiver_store → store}/block_store.ts +309 -141
  120. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +12 -8
  121. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +1 -1
  122. package/src/{archiver/kv_archiver_store → store}/kv_archiver_store.ts +294 -39
  123. package/src/store/l2_tips_cache.ts +89 -0
  124. package/src/store/log_store.ts +736 -0
  125. package/src/{archiver/kv_archiver_store → store}/message_store.ts +20 -1
  126. package/src/test/fake_l1_state.ts +698 -0
  127. package/src/test/index.ts +4 -0
  128. package/src/test/mock_archiver.ts +4 -3
  129. package/src/test/mock_l2_block_source.ts +233 -93
  130. package/src/test/mock_structs.ts +283 -8
  131. package/src/test/noop_l1_archiver.ts +115 -0
  132. package/dest/archiver/archiver.d.ts +0 -307
  133. package/dest/archiver/archiver.d.ts.map +0 -1
  134. package/dest/archiver/archiver.js +0 -2102
  135. package/dest/archiver/archiver_store.d.ts +0 -315
  136. package/dest/archiver/archiver_store.d.ts.map +0 -1
  137. package/dest/archiver/archiver_store.js +0 -4
  138. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  139. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  140. package/dest/archiver/archiver_store_test_suite.js +0 -2770
  141. package/dest/archiver/config.d.ts +0 -22
  142. package/dest/archiver/config.d.ts.map +0 -1
  143. package/dest/archiver/errors.d.ts +0 -36
  144. package/dest/archiver/errors.d.ts.map +0 -1
  145. package/dest/archiver/errors.js +0 -54
  146. package/dest/archiver/index.d.ts +0 -7
  147. package/dest/archiver/index.d.ts.map +0 -1
  148. package/dest/archiver/index.js +0 -4
  149. package/dest/archiver/instrumentation.d.ts +0 -37
  150. package/dest/archiver/instrumentation.d.ts.map +0 -1
  151. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -164
  152. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  153. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  154. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  155. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  156. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  157. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -159
  158. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  159. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -316
  160. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -45
  161. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  162. package/dest/archiver/kv_archiver_store/log_store.js +0 -401
  163. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -40
  164. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  165. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  166. package/dest/archiver/l1/calldata_retriever.d.ts +0 -112
  167. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  168. package/dest/archiver/l1/calldata_retriever.js +0 -471
  169. package/dest/archiver/l1/data_retrieval.d.ts +0 -90
  170. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  171. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  172. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  173. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  174. package/dest/archiver/l1/types.d.ts +0 -12
  175. package/dest/archiver/l1/types.d.ts.map +0 -1
  176. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  177. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  178. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  179. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  180. package/dest/archiver/structs/published.d.ts.map +0 -1
  181. package/dest/archiver/validation.d.ts.map +0 -1
  182. package/dest/rpc/index.d.ts +0 -9
  183. package/dest/rpc/index.d.ts.map +0 -1
  184. package/dest/rpc/index.js +0 -15
  185. package/src/archiver/archiver.ts +0 -2265
  186. package/src/archiver/archiver_store.ts +0 -380
  187. package/src/archiver/archiver_store_test_suite.ts +0 -2842
  188. package/src/archiver/index.ts +0 -6
  189. package/src/archiver/kv_archiver_store/log_store.ts +0 -516
  190. package/src/archiver/l1/README.md +0 -98
  191. package/src/archiver/l1/calldata_retriever.ts +0 -641
  192. package/src/rpc/index.ts +0 -16
  193. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  194. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  195. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  196. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  197. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  198. /package/dest/{archiver/structs → structs}/published.js +0 -0
  199. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  200. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  201. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  202. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  203. /package/src/{archiver/structs → structs}/inbox_message.ts +0 -0
  204. /package/src/{archiver/structs → structs}/published.ts +0 -0
@@ -0,0 +1,486 @@
1
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
2
+ import { EpochCache } from '@aztec/epoch-cache';
3
+ import { BlockTagTooOldError, RollupContract } from '@aztec/ethereum/contracts';
4
+ import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
5
+ import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
6
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
7
+ import { Buffer32 } from '@aztec/foundation/buffer';
8
+ import { merge } from '@aztec/foundation/collection';
9
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
+ import { EthAddress } from '@aztec/foundation/eth-address';
11
+ import { type Logger, createLogger } from '@aztec/foundation/log';
12
+ import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
13
+ import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
14
+ import { DateProvider } from '@aztec/foundation/timer';
15
+ import {
16
+ type ArchiverEmitter,
17
+ L2Block,
18
+ type L2BlockSink,
19
+ type L2Tips,
20
+ type ValidateCheckpointResult,
21
+ } from '@aztec/stdlib/block';
22
+ import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
23
+ import {
24
+ type L1RollupConstants,
25
+ getEpochAtSlot,
26
+ getSlotAtNextL1Block,
27
+ getSlotRangeForEpoch,
28
+ getTimestampRangeForEpoch,
29
+ } from '@aztec/stdlib/epoch-helpers';
30
+ import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
31
+
32
+ import { type ArchiverConfig, mapArchiverConfig } from './config.js';
33
+ import { BlockAlreadyCheckpointedError, NoBlobBodiesFoundError } from './errors.js';
34
+ import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
35
+ import { ArchiverDataSourceBase } from './modules/data_source_base.js';
36
+ import { ArchiverDataStoreUpdater } from './modules/data_store_updater.js';
37
+ import type { ArchiverInstrumentation } from './modules/instrumentation.js';
38
+ import type { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
39
+ import type { KVArchiverDataStore } from './store/kv_archiver_store.js';
40
+ import { L2TipsCache } from './store/l2_tips_cache.js';
41
+
42
+ /** Export ArchiverEmitter for use in factory and tests. */
43
+ export type { ArchiverEmitter };
44
+
45
+ /** Request to add a block to the archiver, queued for processing by the sync loop. */
46
+ type AddBlockRequest = {
47
+ block: L2Block;
48
+ resolve: () => void;
49
+ reject: (err: Error) => void;
50
+ };
51
+
52
+ export type ArchiverDeps = {
53
+ telemetry?: TelemetryClient;
54
+ blobClient: BlobClientInterface;
55
+ epochCache?: EpochCache;
56
+ dateProvider?: DateProvider;
57
+ };
58
+
59
+ /**
60
+ * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
61
+ * Responsible for handling robust L1 polling so that other components do not need to
62
+ * concern themselves with it.
63
+ */
64
+ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Traceable {
65
+ /** Event emitter for archiver events (L2BlockProven, L2PruneUnproven, L2PruneUncheckpointed, etc). */
66
+ public readonly events: ArchiverEmitter;
67
+
68
+ /** A loop in which we will be continually fetching new checkpoints. */
69
+ protected runningPromise: RunningPromise;
70
+
71
+ /** L1 synchronizer that handles fetching checkpoints and messages from L1. */
72
+ private readonly synchronizer: ArchiverL1Synchronizer;
73
+
74
+ private initialSyncComplete: boolean = false;
75
+ private initialSyncPromise: PromiseWithResolvers<void>;
76
+
77
+ /** Queue of blocks to be added to the store, processed by the sync loop. */
78
+ private blockQueue: AddBlockRequest[] = [];
79
+
80
+ /** Helper to handle updates to the store */
81
+ private readonly updater: ArchiverDataStoreUpdater;
82
+
83
+ /** In-memory cache for L2 chain tips. */
84
+ private readonly l2TipsCache: L2TipsCache;
85
+
86
+ public readonly tracer: Tracer;
87
+
88
+ /**
89
+ * Creates a new instance of the Archiver.
90
+ * @param publicClient - A client for interacting with the Ethereum node.
91
+ * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
92
+ * @param rollup - Rollup contract instance.
93
+ * @param inbox - Inbox contract instance.
94
+ * @param l1Addresses - L1 contract addresses (registry, governance proposer, slash factory, slashing proposer).
95
+ * @param dataStore - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
96
+ * @param config - Archiver configuration options.
97
+ * @param blobClient - Client for retrieving blob data.
98
+ * @param dateProvider - Provider for current date/time.
99
+ * @param instrumentation - Instrumentation for metrics and tracing.
100
+ * @param l1Constants - L1 rollup constants.
101
+ * @param log - A logger.
102
+ */
103
+ constructor(
104
+ private readonly publicClient: ViemPublicClient,
105
+ private readonly debugClient: ViemPublicDebugClient,
106
+ private readonly rollup: RollupContract,
107
+ private readonly l1Addresses: Pick<
108
+ L1ContractAddresses,
109
+ 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
110
+ > & { slashingProposerAddress: EthAddress },
111
+ readonly dataStore: KVArchiverDataStore,
112
+ private config: {
113
+ pollingIntervalMs: number;
114
+ batchSize: number;
115
+ skipValidateCheckpointAttestations?: boolean;
116
+ maxAllowedEthClientDriftSeconds: number;
117
+ ethereumAllowNoDebugHosts?: boolean;
118
+ },
119
+ private readonly blobClient: BlobClientInterface,
120
+ instrumentation: ArchiverInstrumentation,
121
+ protected override readonly l1Constants: L1RollupConstants & {
122
+ l1StartBlockHash: Buffer32;
123
+ genesisArchiveRoot: Fr;
124
+ },
125
+ synchronizer: ArchiverL1Synchronizer,
126
+ events: ArchiverEmitter,
127
+ l2TipsCache?: L2TipsCache,
128
+ private readonly log: Logger = createLogger('archiver'),
129
+ ) {
130
+ super(dataStore, l1Constants);
131
+
132
+ this.tracer = instrumentation.tracer;
133
+ this.initialSyncPromise = promiseWithResolvers();
134
+ this.synchronizer = synchronizer;
135
+ this.events = events;
136
+ this.l2TipsCache = l2TipsCache ?? new L2TipsCache(this.dataStore.blockStore);
137
+ this.updater = new ArchiverDataStoreUpdater(this.dataStore, this.l2TipsCache, {
138
+ rollupManaLimit: l1Constants.rollupManaLimit,
139
+ });
140
+
141
+ // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
142
+ // are done as fast as possible. This then gets updated once the initial sync completes.
143
+ this.runningPromise = new RunningPromise(
144
+ () => this.sync(),
145
+ this.log,
146
+ this.config.pollingIntervalMs / 10,
147
+ makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError),
148
+ );
149
+ }
150
+
151
+ /** Updates archiver config */
152
+ public updateConfig(newConfig: Partial<ArchiverConfig>) {
153
+ this.config = merge(this.config, mapArchiverConfig(newConfig));
154
+ this.synchronizer.setConfig(this.config);
155
+ }
156
+
157
+ /**
158
+ * Starts sync process.
159
+ * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
160
+ */
161
+ public async start(blockUntilSynced: boolean): Promise<void> {
162
+ if (this.runningPromise.isRunning()) {
163
+ throw new Error('Archiver is already running');
164
+ }
165
+
166
+ await this.blobClient.testSources();
167
+ await this.synchronizer.testEthereumNodeSynced();
168
+ await validateAndLogTraceAvailability(
169
+ this.debugClient,
170
+ this.config.ethereumAllowNoDebugHosts ?? false,
171
+ this.log.getBindings(),
172
+ );
173
+
174
+ // Log initial state for the archiver
175
+ const { l1StartBlock } = this.l1Constants;
176
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
177
+ const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
178
+ this.log.info(
179
+ `Starting archiver sync to rollup contract ${this.rollup.address} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`,
180
+ { blocksSynchedTo, messagesSynchedTo, currentL2Checkpoint },
181
+ );
182
+
183
+ // Start sync loop, and return the wait for initial sync if we are asked to block until synced
184
+ this.runningPromise.start();
185
+ if (blockUntilSynced) {
186
+ return this.waitForInitialSync();
187
+ }
188
+ }
189
+
190
+ public syncImmediate() {
191
+ return this.runningPromise.trigger();
192
+ }
193
+
194
+ /**
195
+ * Queues a block to be added to the archiver store and triggers processing.
196
+ * The block will be processed by the sync loop.
197
+ * Implements the L2BlockSink interface.
198
+ * @param block - The L2 block to add.
199
+ * @returns A promise that resolves when the block has been added to the store, or rejects on error.
200
+ */
201
+ public addBlock(block: L2Block): Promise<void> {
202
+ return new Promise<void>((resolve, reject) => {
203
+ this.blockQueue.push({ block, resolve, reject });
204
+ this.log.debug(`Queued block ${block.number} for processing`);
205
+ // Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
206
+ this.syncImmediate().catch(err => {
207
+ this.log.error(`Sync immediate call failed: ${err}`);
208
+ });
209
+ });
210
+ }
211
+
212
+ /**
213
+ * Processes all queued blocks, adding them to the store.
214
+ * Called at the beginning of each sync iteration.
215
+ * Blocks are processed in the order they were queued.
216
+ */
217
+ private async processQueuedBlocks(): Promise<void> {
218
+ if (this.blockQueue.length === 0) {
219
+ return;
220
+ }
221
+
222
+ // Take all blocks from the queue
223
+ const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
224
+ this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
225
+
226
+ // Calculate slot threshold for validation
227
+ const l1Timestamp = this.synchronizer.getL1Timestamp();
228
+ const slotAtNextL1Block =
229
+ l1Timestamp === undefined ? undefined : getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
230
+
231
+ // Process each block individually to properly resolve/reject each promise
232
+ for (const { block, resolve, reject } of queuedItems) {
233
+ const blockSlot = block.header.globalVariables.slotNumber;
234
+ if (slotAtNextL1Block !== undefined && blockSlot < slotAtNextL1Block) {
235
+ this.log.warn(
236
+ `Rejecting proposed block ${block.number} for past slot ${blockSlot} (current is ${slotAtNextL1Block})`,
237
+ { block: block.toBlockInfo(), l1Timestamp, slotAtNextL1Block },
238
+ );
239
+ reject(new Error(`Block ${block.number} is for past slot ${blockSlot} (current is ${slotAtNextL1Block})`));
240
+ continue;
241
+ }
242
+
243
+ try {
244
+ await this.updater.addProposedBlock(block);
245
+ this.log.debug(`Added block ${block.number} to store`);
246
+ resolve();
247
+ } catch (err: any) {
248
+ if (err instanceof BlockAlreadyCheckpointedError) {
249
+ this.log.debug(`Proposed block ${block.number} matches already checkpointed block, ignoring late proposal`);
250
+ resolve();
251
+ continue;
252
+ }
253
+ this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
254
+ reject(err);
255
+ }
256
+ }
257
+ }
258
+
259
+ public waitForInitialSync() {
260
+ return this.initialSyncPromise.promise;
261
+ }
262
+
263
+ /**
264
+ * Fetches logs from L1 contracts and processes them.
265
+ */
266
+ @trackSpan('Archiver.sync')
267
+ private async sync() {
268
+ // Process any queued blocks first, before doing L1 sync
269
+ await this.processQueuedBlocks();
270
+ // Now perform L1 sync
271
+ await this.syncFromL1();
272
+ }
273
+
274
+ private async syncFromL1() {
275
+ // Delegate to the L1 synchronizer
276
+ await this.synchronizer.syncFromL1(this.initialSyncComplete);
277
+
278
+ // Check if we've completed initial sync
279
+ const currentL1BlockNumber = this.synchronizer.getL1BlockNumber();
280
+ if (currentL1BlockNumber !== undefined && !this.initialSyncComplete) {
281
+ const l1BlockNumberAtEnd = await this.publicClient.getBlockNumber();
282
+ if (currentL1BlockNumber + 1n >= l1BlockNumberAtEnd) {
283
+ this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
284
+ l1BlockNumber: currentL1BlockNumber,
285
+ syncPoint: await this.store.getSynchPoint(),
286
+ ...(await this.getL2Tips()),
287
+ });
288
+ this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
289
+ this.initialSyncComplete = true;
290
+ this.initialSyncPromise.resolve();
291
+ }
292
+ }
293
+ }
294
+
295
+ /** Resumes the archiver after a stop. */
296
+ public resume() {
297
+ if (this.runningPromise.isRunning()) {
298
+ this.log.warn(`Archiver already running`);
299
+ }
300
+ this.log.info(`Restarting archiver`);
301
+ this.runningPromise.start();
302
+ }
303
+
304
+ /**
305
+ * Stops the archiver.
306
+ * @returns A promise signalling completion of the stop process.
307
+ */
308
+ public async stop(): Promise<void> {
309
+ this.log.debug('Stopping...');
310
+ await this.runningPromise.stop();
311
+
312
+ this.log.info('Stopped.');
313
+ return Promise.resolve();
314
+ }
315
+
316
+ public backupTo(destPath: string): Promise<string> {
317
+ return this.dataStore.backupTo(destPath);
318
+ }
319
+
320
+ public getL1Constants(): Promise<L1RollupConstants> {
321
+ return Promise.resolve(this.l1Constants);
322
+ }
323
+
324
+ public getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }> {
325
+ return Promise.resolve({ genesisArchiveRoot: this.l1Constants.genesisArchiveRoot });
326
+ }
327
+
328
+ public getRollupAddress(): Promise<EthAddress> {
329
+ return Promise.resolve(EthAddress.fromString(this.rollup.address));
330
+ }
331
+
332
+ public getRegistryAddress(): Promise<EthAddress> {
333
+ return Promise.resolve(this.l1Addresses.registryAddress);
334
+ }
335
+
336
+ public getL1BlockNumber(): bigint | undefined {
337
+ return this.synchronizer.getL1BlockNumber();
338
+ }
339
+
340
+ public getL1Timestamp(): Promise<bigint | undefined> {
341
+ return Promise.resolve(this.synchronizer.getL1Timestamp());
342
+ }
343
+
344
+ public getSyncedL2SlotNumber(): Promise<SlotNumber | undefined> {
345
+ const l1Timestamp = this.synchronizer.getL1Timestamp();
346
+ if (l1Timestamp === undefined) {
347
+ return Promise.resolve(undefined);
348
+ }
349
+ // The synced slot is the last L2 slot whose all L1 blocks have been processed.
350
+ // If the next L1 block (at l1Timestamp + ethereumSlotDuration) falls in slot N,
351
+ // then we've fully synced slot N-1.
352
+ const nextL1BlockSlot = getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
353
+ if (Number(nextL1BlockSlot) === 0) {
354
+ return Promise.resolve(undefined);
355
+ }
356
+ return Promise.resolve(SlotNumber(nextL1BlockSlot - 1));
357
+ }
358
+
359
+ public async getSyncedL2EpochNumber(): Promise<EpochNumber | undefined> {
360
+ const syncedSlot = await this.getSyncedL2SlotNumber();
361
+ if (syncedSlot === undefined) {
362
+ return undefined;
363
+ }
364
+ // An epoch is fully synced when all its slots are synced.
365
+ // We check if syncedSlot is the last slot of its epoch; if so, that epoch is fully synced.
366
+ // Otherwise, only the previous epoch is fully synced.
367
+ const epoch = getEpochAtSlot(syncedSlot, this.l1Constants);
368
+ const [, endSlot] = getSlotRangeForEpoch(epoch, this.l1Constants);
369
+ if (syncedSlot >= endSlot) {
370
+ return epoch;
371
+ }
372
+ return Number(epoch) > 0 ? EpochNumber(Number(epoch) - 1) : undefined;
373
+ }
374
+
375
+ public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
376
+ // The epoch is complete if the current checkpointed L2 block is the last one in the epoch (or later).
377
+ // We use the checkpointed block number (synced from L1) instead of 'latest' to avoid returning true
378
+ // prematurely when proposed blocks have been pushed to the archiver but not yet checkpointed on L1.
379
+ const checkpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
380
+ const header = checkpointedBlockNumber > 0 ? await this.getBlockHeader(checkpointedBlockNumber) : undefined;
381
+ const slot = header ? header.globalVariables.slotNumber : undefined;
382
+ const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
383
+ if (slot && slot >= endSlot) {
384
+ return true;
385
+ }
386
+
387
+ // If we haven't run an initial sync, just return false.
388
+ const l1Timestamp = this.synchronizer.getL1Timestamp();
389
+ if (l1Timestamp === undefined) {
390
+ return false;
391
+ }
392
+
393
+ // If not, the epoch may also be complete if the L2 slot has passed without a block
394
+ // We compute this based on the end timestamp for the given epoch and the timestamp of the last L1 block
395
+ const [_startTimestamp, endTimestamp] = getTimestampRangeForEpoch(epochNumber, this.l1Constants);
396
+
397
+ // For this computation, we throw in a few extra seconds just for good measure,
398
+ // since we know the next L1 block won't be mined within this range. Remember that
399
+ // l1timestamp is the timestamp of the last l1 block we've seen, so this relies on
400
+ // the fact that L1 won't mine two blocks within this time of each other.
401
+ // TODO(palla/reorg): Is the above a safe assumption?
402
+ const leeway = 1n;
403
+ return l1Timestamp + leeway >= endTimestamp;
404
+ }
405
+
406
+ /** Returns whether the archiver has completed an initial sync run successfully. */
407
+ public isInitialSyncComplete(): boolean {
408
+ return this.initialSyncComplete;
409
+ }
410
+
411
+ public removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
412
+ return this.updater.removeCheckpointsAfter(checkpointNumber);
413
+ }
414
+
415
+ /** Used by TXE to add checkpoints directly without syncing from L1. */
416
+ public async addCheckpoints(
417
+ checkpoints: PublishedCheckpoint[],
418
+ pendingChainValidationStatus?: ValidateCheckpointResult,
419
+ ): Promise<boolean> {
420
+ await this.updater.addCheckpoints(checkpoints, pendingChainValidationStatus);
421
+ return true;
422
+ }
423
+
424
+ public getL2Tips(): Promise<L2Tips> {
425
+ return this.l2TipsCache.getL2Tips();
426
+ }
427
+
428
+ public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
429
+ const currentBlocks = await this.getL2Tips();
430
+ const currentL2Block = currentBlocks.proposed.number;
431
+ const currentProvenBlock = currentBlocks.proven.block.number;
432
+
433
+ if (targetL2BlockNumber >= currentL2Block) {
434
+ throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
435
+ }
436
+ const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
437
+ if (!targetL2Block) {
438
+ throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
439
+ }
440
+ const targetCheckpointNumber = targetL2Block.checkpointNumber;
441
+
442
+ // Rollback operates at checkpoint granularity: the target block must be the last block of its checkpoint.
443
+ const checkpointData = await this.store.getCheckpointData(targetCheckpointNumber);
444
+ if (checkpointData) {
445
+ const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
446
+ if (targetL2BlockNumber !== lastBlockInCheckpoint) {
447
+ const previousCheckpointBoundary =
448
+ checkpointData.startBlock > 1 ? BlockNumber(checkpointData.startBlock - 1) : BlockNumber(0);
449
+ throw new Error(
450
+ `Target L2 block ${targetL2BlockNumber} is not at a checkpoint boundary. ` +
451
+ `Checkpoint ${targetCheckpointNumber} spans blocks ${checkpointData.startBlock} to ${lastBlockInCheckpoint}. ` +
452
+ `Use block ${lastBlockInCheckpoint} to roll back to this checkpoint, ` +
453
+ `or block ${previousCheckpointBoundary} to roll back to the previous one.`,
454
+ );
455
+ }
456
+ }
457
+
458
+ const targetL1BlockNumber = targetL2Block.l1.blockNumber;
459
+ const targetL1Block = await this.publicClient.getBlock({
460
+ blockNumber: targetL1BlockNumber,
461
+ includeTransactions: false,
462
+ });
463
+ if (!targetL1Block) {
464
+ throw new Error(`Missing L1 block ${targetL1BlockNumber}`);
465
+ }
466
+ const targetL1BlockHash = Buffer32.fromString(targetL1Block.hash);
467
+ this.log.info(
468
+ `Removing checkpoints after checkpoint ${targetCheckpointNumber} (target block ${targetL2BlockNumber})`,
469
+ );
470
+ await this.updater.removeCheckpointsAfter(targetCheckpointNumber);
471
+ this.log.info(`Rolling back L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
472
+ await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
473
+ this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
474
+ await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
475
+ await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
476
+ if (targetL2BlockNumber < currentProvenBlock) {
477
+ this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
478
+ await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
479
+ }
480
+ const currentFinalizedBlock = currentBlocks.finalized.block.number;
481
+ if (targetL2BlockNumber < currentFinalizedBlock) {
482
+ this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
483
+ await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
484
+ }
485
+ }
486
+ }
@@ -8,7 +8,12 @@ import {
8
8
  getConfigFromMappings,
9
9
  numberConfigHelper,
10
10
  } from '@aztec/foundation/config';
11
- import { type ChainConfig, chainConfigMappings } from '@aztec/stdlib/config';
11
+ import {
12
+ type ChainConfig,
13
+ type PipelineConfig,
14
+ chainConfigMappings,
15
+ pipelineConfigMappings,
16
+ } from '@aztec/stdlib/config';
12
17
  import type { ArchiverSpecificConfig } from '@aztec/stdlib/interfaces/server';
13
18
 
14
19
  /**
@@ -21,11 +26,13 @@ import type { ArchiverSpecificConfig } from '@aztec/stdlib/interfaces/server';
21
26
  export type ArchiverConfig = ArchiverSpecificConfig &
22
27
  L1ReaderConfig &
23
28
  L1ContractsConfig &
29
+ PipelineConfig & // required to pass through to epoch cache
24
30
  BlobClientConfig &
25
31
  ChainConfig;
26
32
 
27
33
  export const archiverConfigMappings: ConfigMappingsType<ArchiverConfig> = {
28
34
  ...blobClientConfigMapping,
35
+ ...pipelineConfigMappings,
29
36
  archiverPollingIntervalMS: {
30
37
  env: 'ARCHIVER_POLLING_INTERVAL_MS',
31
38
  description: 'The polling interval in ms for retrieving new L2 blocks and encrypted logs.',
@@ -82,3 +89,14 @@ export const archiverConfigMappings: ConfigMappingsType<ArchiverConfig> = {
82
89
  export function getArchiverConfigFromEnv(): ArchiverConfig {
83
90
  return getConfigFromMappings<ArchiverConfig>(archiverConfigMappings);
84
91
  }
92
+
93
+ /** Extracts the archiver-specific configuration from the full ArchiverConfig */
94
+ export function mapArchiverConfig(config: Partial<ArchiverConfig>) {
95
+ return {
96
+ pollingIntervalMs: config.archiverPollingIntervalMS,
97
+ batchSize: config.archiverBatchSize,
98
+ skipValidateCheckpointAttestations: config.skipValidateCheckpointAttestations,
99
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
100
+ ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts,
101
+ };
102
+ }
@@ -6,24 +6,9 @@ export class NoBlobBodiesFoundError extends Error {
6
6
  }
7
7
  }
8
8
 
9
- export class InitialBlockNumberNotSequentialError extends Error {
10
- constructor(
11
- public readonly newBlockNumber: number,
12
- public readonly previousBlockNumber: number | undefined,
13
- ) {
14
- super(
15
- `Cannot insert new block ${newBlockNumber} given previous block number in store is ${
16
- previousBlockNumber ?? 'undefined'
17
- }`,
18
- );
19
- }
20
- }
21
-
22
9
  export class BlockNumberNotSequentialError extends Error {
23
10
  constructor(newBlockNumber: number, previous: number | undefined) {
24
- super(
25
- `Cannot insert new block ${newBlockNumber} given previous block number in batch is ${previous ?? 'undefined'}`,
26
- );
11
+ super(`Cannot insert new block ${newBlockNumber} given previous block number is ${previous ?? 'undefined'}`);
27
12
  }
28
13
  }
29
14
 
@@ -48,14 +33,6 @@ export class CheckpointNumberNotSequentialError extends Error {
48
33
  }
49
34
  }
50
35
 
51
- export class CheckpointNumberNotConsistentError extends Error {
52
- constructor(newCheckpointNumber: number, previous: number | undefined) {
53
- super(
54
- `Cannot insert block for new checkpoint ${newCheckpointNumber} given previous block was checkpoint ${previous ?? 'undefined'}`,
55
- );
56
- }
57
- }
58
-
59
36
  export class BlockIndexNotSequentialError extends Error {
60
37
  constructor(newBlockIndex: number, previousBlockIndex: number | undefined) {
61
38
  super(
@@ -88,3 +65,54 @@ export class BlockNotFoundError extends Error {
88
65
  super(`Failed to find expected block number ${blockNumber}`);
89
66
  }
90
67
  }
68
+
69
+ /** Thrown when a proposed block matches a block that was already checkpointed. This is expected for late proposals. */
70
+ export class BlockAlreadyCheckpointedError extends Error {
71
+ constructor(public readonly blockNumber: number) {
72
+ super(`Block ${blockNumber} has already been checkpointed with the same content`);
73
+ this.name = 'BlockAlreadyCheckpointedError';
74
+ }
75
+ }
76
+
77
+ /** Thrown when logs are added for a tag whose last stored log has a higher block number than the new log. */
78
+ export class OutOfOrderLogInsertionError extends Error {
79
+ constructor(
80
+ public readonly logType: 'private' | 'public',
81
+ public readonly tag: string,
82
+ public readonly lastBlockNumber: number,
83
+ public readonly newBlockNumber: number,
84
+ ) {
85
+ super(
86
+ `Out-of-order ${logType} log insertion for tag ${tag}: ` +
87
+ `last existing log is from block ${lastBlockNumber} but new log is from block ${newBlockNumber}`,
88
+ );
89
+ this.name = 'OutOfOrderLogInsertionError';
90
+ }
91
+ }
92
+
93
+ /** Thrown when L1 to L2 messages are requested for a checkpoint whose message tree hasn't been sealed yet. */
94
+ export class L1ToL2MessagesNotReadyError extends Error {
95
+ constructor(
96
+ public readonly checkpointNumber: number,
97
+ public readonly inboxTreeInProgress: bigint,
98
+ ) {
99
+ super(
100
+ `Cannot get L1 to L2 messages for checkpoint ${checkpointNumber}: ` +
101
+ `inbox tree in progress is ${inboxTreeInProgress}, messages not yet sealed`,
102
+ );
103
+ this.name = 'L1ToL2MessagesNotReadyError';
104
+ }
105
+ }
106
+
107
+ /** Thrown when a proposed block conflicts with an already checkpointed block (different content). */
108
+ export class CannotOverwriteCheckpointedBlockError extends Error {
109
+ constructor(
110
+ public readonly blockNumber: number,
111
+ public readonly lastCheckpointedBlock: number,
112
+ ) {
113
+ super(
114
+ `Cannot add block ${blockNumber}: would overwrite checkpointed data (checkpointed up to block ${lastCheckpointedBlock})`,
115
+ );
116
+ this.name = 'CannotOverwriteCheckpointedBlockError';
117
+ }
118
+ }