@aztec/archiver 0.0.1-commit.4d3c002 → 0.0.1-commit.4d9804df

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 (124) hide show
  1. package/README.md +7 -5
  2. package/dest/archiver.d.ts +70 -15
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +274 -71
  5. package/dest/config.d.ts +7 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +39 -16
  8. package/dest/errors.d.ts +35 -13
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +54 -16
  11. package/dest/factory.d.ts +11 -6
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +35 -21
  14. package/dest/index.d.ts +11 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +10 -2
  17. package/dest/l1/calldata_retriever.d.ts +9 -1
  18. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  19. package/dest/l1/calldata_retriever.js +20 -10
  20. package/dest/l1/data_retrieval.d.ts +24 -10
  21. package/dest/l1/data_retrieval.d.ts.map +1 -1
  22. package/dest/l1/data_retrieval.js +13 -19
  23. package/dest/l1/trace_tx.d.ts +12 -66
  24. package/dest/l1/trace_tx.d.ts.map +1 -1
  25. package/dest/l1/validate_historical_logs.d.ts +23 -0
  26. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  27. package/dest/l1/validate_historical_logs.js +108 -0
  28. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  29. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  30. package/dest/modules/contract_data_source_adapter.js +32 -0
  31. package/dest/modules/data_source_base.d.ts +71 -50
  32. package/dest/modules/data_source_base.d.ts.map +1 -1
  33. package/dest/modules/data_source_base.js +250 -150
  34. package/dest/modules/data_store_updater.d.ts +28 -9
  35. package/dest/modules/data_store_updater.d.ts.map +1 -1
  36. package/dest/modules/data_store_updater.js +121 -61
  37. package/dest/modules/instrumentation.d.ts +15 -2
  38. package/dest/modules/instrumentation.d.ts.map +1 -1
  39. package/dest/modules/instrumentation.js +48 -9
  40. package/dest/modules/l1_synchronizer.d.ts +11 -7
  41. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  42. package/dest/modules/l1_synchronizer.js +347 -118
  43. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  44. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  45. package/dest/modules/outbox_trees_resolver.js +162 -0
  46. package/dest/modules/validation.d.ts +30 -7
  47. package/dest/modules/validation.d.ts.map +1 -1
  48. package/dest/modules/validation.js +33 -15
  49. package/dest/store/block_store.d.ts +167 -94
  50. package/dest/store/block_store.d.ts.map +1 -1
  51. package/dest/store/block_store.js +524 -260
  52. package/dest/store/contract_class_store.d.ts +17 -3
  53. package/dest/store/contract_class_store.d.ts.map +1 -1
  54. package/dest/store/contract_class_store.js +36 -2
  55. package/dest/store/contract_instance_store.d.ts +28 -1
  56. package/dest/store/contract_instance_store.d.ts.map +1 -1
  57. package/dest/store/contract_instance_store.js +49 -1
  58. package/dest/store/data_stores.d.ts +68 -0
  59. package/dest/store/data_stores.d.ts.map +1 -0
  60. package/dest/store/data_stores.js +54 -0
  61. package/dest/store/function_names_cache.d.ts +17 -0
  62. package/dest/store/function_names_cache.d.ts.map +1 -0
  63. package/dest/store/function_names_cache.js +30 -0
  64. package/dest/store/l2_tips_cache.d.ts +13 -8
  65. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  66. package/dest/store/l2_tips_cache.js +13 -96
  67. package/dest/store/log_store.d.ts +42 -40
  68. package/dest/store/log_store.d.ts.map +1 -1
  69. package/dest/store/log_store.js +257 -480
  70. package/dest/store/log_store_codec.d.ts +78 -0
  71. package/dest/store/log_store_codec.d.ts.map +1 -0
  72. package/dest/store/log_store_codec.js +110 -0
  73. package/dest/store/message_store.d.ts +9 -3
  74. package/dest/store/message_store.d.ts.map +1 -1
  75. package/dest/store/message_store.js +31 -1
  76. package/dest/test/fake_l1_state.d.ts +7 -3
  77. package/dest/test/fake_l1_state.d.ts.map +1 -1
  78. package/dest/test/fake_l1_state.js +45 -18
  79. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  80. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  81. package/dest/test/mock_l1_to_l2_message_source.js +1 -2
  82. package/dest/test/mock_l2_block_source.d.ts +47 -54
  83. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  84. package/dest/test/mock_l2_block_source.js +229 -205
  85. package/dest/test/mock_structs.d.ts +1 -1
  86. package/dest/test/mock_structs.d.ts.map +1 -1
  87. package/dest/test/mock_structs.js +2 -2
  88. package/dest/test/noop_l1_archiver.d.ts +14 -6
  89. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  90. package/dest/test/noop_l1_archiver.js +26 -10
  91. package/package.json +14 -14
  92. package/src/archiver.ts +355 -77
  93. package/src/config.ts +43 -20
  94. package/src/errors.ts +80 -23
  95. package/src/factory.ts +46 -19
  96. package/src/index.ts +18 -2
  97. package/src/l1/calldata_retriever.ts +27 -10
  98. package/src/l1/data_retrieval.ts +38 -36
  99. package/src/l1/validate_historical_logs.ts +140 -0
  100. package/src/modules/contract_data_source_adapter.ts +46 -0
  101. package/src/modules/data_source_base.ts +319 -200
  102. package/src/modules/data_store_updater.ts +146 -64
  103. package/src/modules/instrumentation.ts +43 -10
  104. package/src/modules/l1_synchronizer.ts +499 -136
  105. package/src/modules/outbox_trees_resolver.ts +199 -0
  106. package/src/modules/validation.ts +78 -21
  107. package/src/store/block_store.ts +627 -321
  108. package/src/store/contract_class_store.ts +47 -3
  109. package/src/store/contract_instance_store.ts +61 -1
  110. package/src/store/data_stores.ts +104 -0
  111. package/src/store/function_names_cache.ts +37 -0
  112. package/src/store/l2_tips_cache.ts +16 -109
  113. package/src/store/log_store.ts +296 -652
  114. package/src/store/log_store_codec.ts +143 -0
  115. package/src/store/message_store.ts +35 -2
  116. package/src/test/fake_l1_state.ts +50 -27
  117. package/src/test/mock_l1_to_l2_message_source.ts +0 -1
  118. package/src/test/mock_l2_block_source.ts +276 -258
  119. package/src/test/mock_structs.ts +5 -2
  120. package/src/test/noop_l1_archiver.ts +54 -11
  121. package/dest/store/kv_archiver_store.d.ts +0 -377
  122. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  123. package/dest/store/kv_archiver_store.js +0 -494
  124. package/src/store/kv_archiver_store.ts +0 -713
@@ -6,8 +6,13 @@ import {
6
6
  ContractInstancePublishedEvent,
7
7
  ContractInstanceUpdatedEvent,
8
8
  } from '@aztec/protocol-contracts/instance-registry';
9
- import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
10
- import { type ProposedCheckpointInput, type PublishedCheckpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
9
+ import type { CommitteeAttestation, L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
10
+ import {
11
+ type L1PublishedData,
12
+ type ProposedCheckpointInput,
13
+ type PublishedCheckpoint,
14
+ validateCheckpoint,
15
+ } from '@aztec/stdlib/checkpoint';
11
16
  import {
12
17
  type ContractClassPublicWithCommitment,
13
18
  computeContractAddressFromInstance,
@@ -16,7 +21,7 @@ import {
16
21
  import type { ContractClassLog, PrivateLog, PublicLog } from '@aztec/stdlib/logs';
17
22
  import type { UInt64 } from '@aztec/stdlib/types';
18
23
 
19
- import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
24
+ import type { ArchiverDataStores } from '../store/data_stores.js';
20
25
  import type { L2TipsCache } from '../store/l2_tips_cache.js';
21
26
 
22
27
  /** Operation type for contract data updates. */
@@ -38,7 +43,7 @@ export class ArchiverDataStoreUpdater {
38
43
  private readonly log = createLogger('archiver:store_updater');
39
44
 
40
45
  constructor(
41
- private store: KVArchiverDataStore,
46
+ private stores: ArchiverDataStores,
42
47
  private l2TipsCache?: L2TipsCache,
43
48
  private opts: { rollupManaLimit?: number } = {},
44
49
  ) {}
@@ -56,21 +61,22 @@ export class ArchiverDataStoreUpdater {
56
61
  block: L2Block,
57
62
  pendingChainValidationStatus?: ValidateCheckpointResult,
58
63
  ): Promise<boolean> {
59
- const result = await this.store.transactionAsync(async () => {
60
- await this.store.addProposedBlock(block);
64
+ const result = await this.stores.db.transactionAsync(async () => {
65
+ await this.stores.blocks.addProposedBlock(block);
61
66
 
62
67
  const opResults = await Promise.all([
63
68
  // Update the pending chain validation status if provided
64
- pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
69
+ pendingChainValidationStatus &&
70
+ this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
65
71
  // Add any logs emitted during the retrieved block
66
- this.store.addLogs([block]),
72
+ this.stores.logs.addLogs([block]),
67
73
  // Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
68
74
  this.addContractDataToDb(block),
69
75
  ]);
70
76
 
71
- await this.l2TipsCache?.refresh();
72
77
  return opResults.every(Boolean);
73
78
  });
79
+ await this.l2TipsCache?.refresh();
74
80
  return result;
75
81
  }
76
82
 
@@ -79,64 +85,98 @@ export class ArchiverDataStoreUpdater {
79
85
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
80
86
  * Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
81
87
  * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
88
+ * If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
89
+ * as the added checkpoints so both updates are applied atomically.
82
90
  *
83
- * @param checkpoints - The published checkpoints to add.
91
+ * @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
84
92
  * @param pendingChainValidationStatus - Optional validation status to set.
93
+ * @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
85
94
  * @returns Result with information about any pruned blocks.
86
95
  */
87
96
  public async addCheckpoints(
88
97
  checkpoints: PublishedCheckpoint[],
89
98
  pendingChainValidationStatus?: ValidateCheckpointResult,
99
+ promoteProposed?: {
100
+ l1: L1PublishedData;
101
+ attestations: CommitteeAttestation[];
102
+ checkpoint: PublishedCheckpoint;
103
+ },
104
+ evictProposedFrom?: CheckpointNumber,
90
105
  ): Promise<ReconcileCheckpointsResult> {
91
106
  for (const checkpoint of checkpoints) {
92
107
  validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
93
108
  }
109
+ if (promoteProposed) {
110
+ validateCheckpoint(promoteProposed.checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
111
+ }
94
112
 
95
- const result = await this.store.transactionAsync(async () => {
113
+ const result = await this.stores.db.transactionAsync(async () => {
96
114
  // Before adding checkpoints, check for conflicts with local blocks if any
97
115
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
98
116
 
99
- await this.store.addCheckpoints(checkpoints);
117
+ const insertedCheckpoints = await this.stores.blocks.addCheckpoints(checkpoints);
100
118
 
101
- // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
102
- const newBlocks = checkpoints
119
+ // Skip blocks already inserted via addProposedBlock() and blocks of already-stored checkpoints
120
+ // re-included by an L1 reorg: their logs/contract data were extracted when first inserted.
121
+ const newBlocks = insertedCheckpoints
103
122
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
104
123
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
105
124
 
106
125
  await Promise.all([
107
126
  // Update the pending chain validation status if provided
108
- pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
127
+ pendingChainValidationStatus &&
128
+ this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
109
129
  // Add any logs emitted during the retrieved blocks
110
- this.store.addLogs(newBlocks),
130
+ this.stores.logs.addLogs(newBlocks),
111
131
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
112
132
  ...newBlocks.map(block => this.addContractDataToDb(block)),
133
+ // Promote the proposed checkpoint if requested (uses explicit checkpoint number)
134
+ promoteProposed
135
+ ? this.stores.blocks.promoteProposedToCheckpointed(
136
+ promoteProposed.checkpoint.checkpoint.number,
137
+ promoteProposed.l1,
138
+ promoteProposed.attestations,
139
+ promoteProposed.checkpoint.checkpoint.archive.root,
140
+ )
141
+ : undefined,
142
+ // Evict pending checkpoints that diverged from what L1 mined
143
+ evictProposedFrom !== undefined
144
+ ? this.stores.blocks.evictProposedCheckpointsFrom(evictProposedFrom)
145
+ : undefined,
113
146
  ]);
114
147
 
115
- await this.l2TipsCache?.refresh();
116
148
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
117
149
  });
150
+ await this.l2TipsCache?.refresh();
118
151
  return result;
119
152
  }
120
153
 
121
- public async setProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput) {
122
- const result = await this.store.transactionAsync(async () => {
123
- await this.store.setProposedCheckpoint(proposedCheckpoint);
124
- await this.l2TipsCache?.refresh();
154
+ public async addProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput) {
155
+ const result = await this.stores.db.transactionAsync(async () => {
156
+ await this.stores.blocks.addProposedCheckpoint(proposedCheckpoint);
125
157
  });
126
-
158
+ await this.l2TipsCache?.refresh();
127
159
  return result;
128
160
  }
129
161
 
130
162
  /**
131
163
  * Checks for local proposed blocks that do not match the ones to be checkpointed and prunes them.
132
- * This method handles multiple checkpoints but returns after pruning the first conflict found.
133
- * This is correct because pruning from the first conflict point removes all subsequent blocks,
134
- * and when checkpoints are added afterward, they include all the correct blocks.
164
+ * Conflict detection is keyed on `blockNumber`: when a local proposed block and an L1
165
+ * checkpointed block share a block number but live at different slots (e.g. a different proposer
166
+ * mined the same block number one slot earlier), we still treat them as a conflict and prune.
167
+ * The trailing per-checkpoint prune that handles "local has extra trailing blocks within the
168
+ * same slot as the published checkpoint" remains scoped by slot to preserve pipelining: local
169
+ * blocks that live at a later slot than the checkpoint being processed represent speculation
170
+ * atop the just-confirmed tip (and may be referenced by a pending proposed checkpoint), so we
171
+ * leave them in place. This method handles multiple checkpoints but returns after pruning the
172
+ * first conflict found. This is correct because pruning from the first conflict point removes
173
+ * all subsequent blocks, and when checkpoints are added afterward, they include all the correct
174
+ * blocks.
135
175
  */
136
176
  private async pruneMismatchingLocalBlocks(checkpoints: PublishedCheckpoint[]): Promise<ReconcileCheckpointsResult> {
137
177
  const [lastCheckpointedBlockNumber, lastBlockNumber] = await Promise.all([
138
- this.store.getCheckpointedL2BlockNumber(),
139
- this.store.getLatestBlockNumber(),
178
+ this.stores.blocks.getCheckpointedL2BlockNumber(),
179
+ this.stores.blocks.getLatestL2BlockNumber(),
140
180
  ]);
141
181
 
142
182
  // Exit early if there are no local uncheckpointed blocks
@@ -145,29 +185,29 @@ export class ArchiverDataStoreUpdater {
145
185
  }
146
186
 
147
187
  // Get all uncheckpointed local blocks
148
- const uncheckpointedLocalBlocks = await this.store.getBlocks(
149
- BlockNumber.add(lastCheckpointedBlockNumber, 1),
150
- lastBlockNumber - lastCheckpointedBlockNumber,
151
- );
188
+ const uncheckpointedLocalBlocks = await this.stores.blocks.getBlocksData({
189
+ from: BlockNumber.add(lastCheckpointedBlockNumber, 1),
190
+ limit: lastBlockNumber - lastCheckpointedBlockNumber,
191
+ });
152
192
 
153
193
  let lastAlreadyInsertedBlockNumber: BlockNumber | undefined;
154
194
 
155
195
  for (const publishedCheckpoint of checkpoints) {
156
196
  const checkpointBlocks = publishedCheckpoint.checkpoint.blocks;
157
197
  const slot = publishedCheckpoint.checkpoint.slot;
158
- const localBlocksInSlot = uncheckpointedLocalBlocks.filter(b => b.slot === slot);
159
198
 
160
199
  if (checkpointBlocks.length === 0) {
161
200
  this.log.warn(`Checkpoint ${publishedCheckpoint.checkpoint.number} for slot ${slot} has no blocks`);
162
201
  continue;
163
202
  }
164
203
 
165
- // Find the first checkpoint block that conflicts with an existing local block and prune local afterwards
204
+ // Find the first checkpoint block that conflicts with an existing local block and prune local afterwards.
205
+ // Conflict detection joins on block number only — same block number at a different slot is still a conflict.
166
206
  for (const checkpointBlock of checkpointBlocks) {
167
207
  const blockNumber = checkpointBlock.number;
168
- const existingBlock = localBlocksInSlot.find(b => b.number === blockNumber);
208
+ const existingBlock = uncheckpointedLocalBlocks.find(b => b.header.getBlockNumber() === blockNumber);
169
209
  const blockInfos = {
170
- existingBlock: existingBlock?.toBlockInfo(),
210
+ existingBlock: existingBlock?.header.toInspect(),
171
211
  checkpointBlock: checkpointBlock.toBlockInfo(),
172
212
  };
173
213
 
@@ -179,20 +219,24 @@ export class ArchiverDataStoreUpdater {
179
219
  } else {
180
220
  this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
181
221
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
222
+ await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
182
223
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
183
224
  }
184
225
  }
185
226
 
186
- // If local has more blocks than the checkpoint (e.g., local has [2,3,4] but checkpoint has [2,3]),
187
- // we need to prune the extra local blocks so they match what was checkpointed
227
+ // If the sequencer locally proposed extra blocks within this checkpoint's slot (e.g. local has
228
+ // [N, N+1] but L1 confirmed just [N]), prune the extras. Scoped to the checkpoint's slot so we
229
+ // do not throw away speculative blocks at later slots that belong to a pending proposed checkpoint.
188
230
  const lastCheckpointBlockNumber = checkpointBlocks.at(-1)!.number;
189
- const lastLocalBlockNumber = localBlocksInSlot.at(-1)?.number;
231
+ const localBlocksInSlot = uncheckpointedLocalBlocks.filter(b => b.header.getSlot() === slot);
232
+ const lastLocalBlockNumber = localBlocksInSlot.at(-1)?.header.getBlockNumber();
190
233
 
191
234
  if (lastLocalBlockNumber !== undefined && lastLocalBlockNumber > lastCheckpointBlockNumber) {
192
235
  this.log.warn(
193
236
  `Local chain for slot ${slot} ends at block ${lastLocalBlockNumber} but checkpoint ends at ${lastCheckpointBlockNumber}. Pruning blocks after block ${lastCheckpointBlockNumber}.`,
194
237
  );
195
238
  const prunedBlocks = await this.removeBlocksAfter(lastCheckpointBlockNumber);
239
+ await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
196
240
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
197
241
  }
198
242
  }
@@ -210,37 +254,74 @@ export class ArchiverDataStoreUpdater {
210
254
  * @throws Error if any block to be removed is checkpointed.
211
255
  */
212
256
  public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
213
- const result = await this.store.transactionAsync(async () => {
257
+ const result = await this.stores.db.transactionAsync(async () => {
214
258
  // Verify we're only removing uncheckpointed blocks
215
- const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
259
+ const lastCheckpointedBlockNumber = await this.stores.blocks.getCheckpointedL2BlockNumber();
216
260
  if (blockNumber < lastCheckpointedBlockNumber) {
217
261
  throw new Error(
218
262
  `Cannot remove blocks after ${blockNumber} because checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`,
219
263
  );
220
264
  }
221
265
 
222
- const result = await this.removeBlocksAfter(blockNumber);
266
+ const prunedBlocks = await this.removeBlocksAfter(blockNumber);
267
+ await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
223
268
 
224
- // Clear the proposed checkpoint if it exists, since its blocks have been pruned
225
- await this.store.deleteProposedCheckpoint();
269
+ return prunedBlocks;
270
+ });
271
+ await this.l2TipsCache?.refresh();
272
+ return result;
273
+ }
274
+
275
+ /**
276
+ * Removes all blocks without a proposed checkpoint strictly after the specified block number and cleans up associated contract data.
277
+ * This handles removal of provisionally added blocks along with their contract classes/instances.
278
+ * Verifies that each block being removed is not part of a stored checkpoint (proposed or not).
279
+ * This differs from `removeUncheckpointedBlocksAfter` in that it also checks proposed checkpoints.
280
+ *
281
+ * @param blockNumber - Remove all blocks with number greater than this.
282
+ * @returns The removed blocks.
283
+ * @throws Error if any block to be removed is checkpointed.
284
+ */
285
+ public async removeBlocksWithoutProposedCheckpointAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
286
+ const result = await this.stores.db.transactionAsync(async () => {
287
+ // Verify we're only removing uncheckpointed blocks
288
+ const lastCheckpointedBlockNumber = await this.stores.blocks.getProposedCheckpointL2BlockNumber();
289
+ if (blockNumber < lastCheckpointedBlockNumber) {
290
+ throw new Error(
291
+ `Cannot remove blocks after ${blockNumber} because proposed checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`,
292
+ );
293
+ }
226
294
 
227
- await this.l2TipsCache?.refresh();
228
- return result;
295
+ return await this.removeBlocksAfter(blockNumber);
229
296
  });
297
+ await this.l2TipsCache?.refresh();
230
298
  return result;
231
299
  }
232
300
 
301
+ /**
302
+ * Evicts pending proposed checkpoints that referenced any of the just-pruned blocks. Pruned
303
+ * blocks invalidate all proposed checkpoints from the lowest pruned block's checkpoint number
304
+ * onwards: those checkpoints either reference the pruned blocks directly or chain off them.
305
+ */
306
+ private async evictProposedCheckpointsForPrunedBlocks(prunedBlocks: L2Block[]): Promise<void> {
307
+ if (prunedBlocks.length === 0) {
308
+ return;
309
+ }
310
+ const fromCheckpointNumber = prunedBlocks[0].checkpointNumber;
311
+ await this.stores.blocks.evictProposedCheckpointsFrom(fromCheckpointNumber);
312
+ }
313
+
233
314
  /**
234
315
  * Removes all blocks strictly after the given block number along with any logs and contract data.
235
316
  * Does not remove their checkpoints.
236
317
  */
237
318
  private async removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
238
319
  // First get the blocks to be removed so we can clean up contract data
239
- const removedBlocks = await this.store.removeBlocksAfter(blockNumber);
320
+ const removedBlocks = await this.stores.blocks.removeBlocksAfter(blockNumber);
240
321
 
241
322
  // Clean up contract data and logs for the removed blocks
242
323
  await Promise.all([
243
- this.store.deleteLogs(removedBlocks),
324
+ this.stores.logs.deleteLogs(removedBlocks),
244
325
  ...removedBlocks.map(block => this.removeContractDataFromDb(block)),
245
326
  ]);
246
327
 
@@ -257,20 +338,21 @@ export class ArchiverDataStoreUpdater {
257
338
  * @returns True if the operation is successful.
258
339
  */
259
340
  public async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
260
- return await this.store.transactionAsync(async () => {
261
- const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
341
+ const result = await this.stores.db.transactionAsync(async () => {
342
+ const { blocksRemoved = [] } = await this.stores.blocks.removeCheckpointsAfter(checkpointNumber);
262
343
 
263
344
  const opResults = await Promise.all([
264
345
  // Prune rolls back to the last proven block, which is by definition valid
265
- this.store.setPendingChainValidationStatus({ valid: true }),
346
+ this.stores.blocks.setPendingChainValidationStatus({ valid: true }),
266
347
  // Remove contract data for all blocks being removed
267
348
  ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
268
- this.store.deleteLogs(blocksRemoved),
349
+ this.stores.logs.deleteLogs(blocksRemoved),
269
350
  ]);
270
351
 
271
- await this.l2TipsCache?.refresh();
272
352
  return opResults.every(Boolean);
273
353
  });
354
+ await this.l2TipsCache?.refresh();
355
+ return result;
274
356
  }
275
357
 
276
358
  /**
@@ -278,10 +360,10 @@ export class ArchiverDataStoreUpdater {
278
360
  * @param checkpointNumber - The checkpoint number to set as proven.
279
361
  */
280
362
  public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
281
- await this.store.transactionAsync(async () => {
282
- await this.store.setProvenCheckpointNumber(checkpointNumber);
283
- await this.l2TipsCache?.refresh();
363
+ await this.stores.db.transactionAsync(async () => {
364
+ await this.stores.blocks.setProvenCheckpointNumber(checkpointNumber);
284
365
  });
366
+ await this.l2TipsCache?.refresh();
285
367
  }
286
368
 
287
369
  /**
@@ -289,10 +371,10 @@ export class ArchiverDataStoreUpdater {
289
371
  * @param checkpointNumber - The checkpoint number to set as finalized.
290
372
  */
291
373
  public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
292
- await this.store.transactionAsync(async () => {
293
- await this.store.setFinalizedCheckpointNumber(checkpointNumber);
294
- await this.l2TipsCache?.refresh();
374
+ await this.stores.db.transactionAsync(async () => {
375
+ await this.stores.blocks.setFinalizedCheckpointNumber(checkpointNumber);
295
376
  });
377
+ await this.l2TipsCache?.refresh();
296
378
  }
297
379
 
298
380
  /** Extracts and stores contract data from a single block. */
@@ -336,7 +418,7 @@ export class ArchiverDataStoreUpdater {
336
418
  const contractClasses = contractClassPublishedEvents.map(e => e.toContractClassPublic());
337
419
  if (contractClasses.length > 0) {
338
420
  contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
339
- return await this.store.deleteContractClasses(contractClasses, blockNum);
421
+ return await this.stores.contractClasses.deleteContractClasses(contractClasses, blockNum);
340
422
  }
341
423
  return true;
342
424
  }
@@ -362,7 +444,7 @@ export class ArchiverDataStoreUpdater {
362
444
 
363
445
  if (contractClasses.length > 0) {
364
446
  contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
365
- return await this.store.addContractClasses(contractClasses, blockNum);
447
+ return await this.stores.contractClasses.addContractClasses(contractClasses, blockNum);
366
448
  }
367
449
  return true;
368
450
  }
@@ -401,9 +483,9 @@ export class ArchiverDataStoreUpdater {
401
483
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
402
484
  );
403
485
  if (operation == Operation.Store) {
404
- return await this.store.addContractInstances(contractInstances, blockNum);
486
+ return await this.stores.contractInstances.addContractInstances(contractInstances, blockNum);
405
487
  } else if (operation == Operation.Delete) {
406
- return await this.store.deleteContractInstances(contractInstances, blockNum);
488
+ return await this.stores.contractInstances.deleteContractInstances(contractInstances);
407
489
  }
408
490
  }
409
491
  return true;
@@ -427,9 +509,9 @@ export class ArchiverDataStoreUpdater {
427
509
  this.log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`),
428
510
  );
429
511
  if (operation == Operation.Store) {
430
- return await this.store.addContractInstanceUpdates(contractUpdates, timestamp);
512
+ return await this.stores.contractInstances.addContractInstanceUpdates(contractUpdates, timestamp);
431
513
  } else if (operation == Operation.Delete) {
432
- return await this.store.deleteContractInstanceUpdates(contractUpdates, timestamp);
514
+ return await this.stores.contractInstances.deleteContractInstanceUpdates(contractUpdates, timestamp);
433
515
  }
434
516
  }
435
517
  return true;
@@ -32,6 +32,7 @@ export class ArchiverInstrumentation {
32
32
  private pruneCount: UpDownCounter;
33
33
 
34
34
  private syncDurationPerBlock: Histogram;
35
+ private syncDurationPerCheckpoint: Histogram;
35
36
  private syncBlockCount: UpDownCounter;
36
37
  private manaPerBlock: Histogram;
37
38
  private txsPerBlock: Histogram;
@@ -42,6 +43,7 @@ export class ArchiverInstrumentation {
42
43
  private blockProposalTxTargetCount: UpDownCounter;
43
44
 
44
45
  private checkpointL1InclusionDelay: Histogram;
46
+ private checkpointPromotedCount: UpDownCounter;
45
47
 
46
48
  private log = createLogger('archiver:instrumentation');
47
49
 
@@ -68,6 +70,8 @@ export class ArchiverInstrumentation {
68
70
 
69
71
  this.syncDurationPerBlock = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_BLOCK);
70
72
 
73
+ this.syncDurationPerCheckpoint = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_CHECKPOINT);
74
+
71
75
  this.syncBlockCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_SYNC_BLOCK_COUNT);
72
76
 
73
77
  this.manaPerBlock = meter.createHistogram(Metrics.ARCHIVER_MANA_PER_BLOCK);
@@ -80,7 +84,9 @@ export class ArchiverInstrumentation {
80
84
 
81
85
  this.pruneDuration = meter.createHistogram(Metrics.ARCHIVER_PRUNE_DURATION);
82
86
 
83
- this.pruneCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_PRUNE_COUNT);
87
+ this.pruneCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_PRUNE_COUNT, {
88
+ [Attributes.PRUNE_TYPE]: ['unproven', 'uncheckpointed', 'l1_conflict', 'orphan', 'l1_mismatch'],
89
+ });
84
90
 
85
91
  this.blockProposalTxTargetCount = createUpDownCounterWithDefault(
86
92
  meter,
@@ -92,6 +98,8 @@ export class ArchiverInstrumentation {
92
98
 
93
99
  this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
94
100
 
101
+ this.checkpointPromotedCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_CHECKPOINT_PROMOTED_COUNT);
102
+
95
103
  this.dbMetrics = new LmdbMetrics(
96
104
  meter,
97
105
  {
@@ -113,17 +121,26 @@ export class ArchiverInstrumentation {
113
121
  return this.telemetry.isEnabled();
114
122
  }
115
123
 
116
- public processNewBlocks(syncTimePerBlock: number, blocks: L2Block[]) {
124
+ public processNewProposedBlock(syncTimePerBlock: number, block: L2Block) {
125
+ const attrs = { [Attributes.STATUS]: 'proposed' };
126
+ this.blockHeight.record(block.number, attrs);
117
127
  this.syncDurationPerBlock.record(Math.ceil(syncTimePerBlock));
118
- this.blockHeight.record(Math.max(...blocks.map(b => b.number)));
119
- this.checkpointHeight.record(Math.max(...blocks.map(b => b.checkpointNumber)));
120
- this.syncBlockCount.add(blocks.length);
121
128
 
122
- for (const block of blocks) {
123
- this.txCount.add(block.body.txEffects.length);
124
- this.txsPerBlock.record(block.body.txEffects.length);
125
- this.manaPerBlock.record(block.header.totalManaUsed.toNumber() / 1e6);
129
+ // Per block metrics
130
+ this.txCount.add(block.body.txEffects.length);
131
+ this.txsPerBlock.record(block.body.txEffects.length);
132
+ this.manaPerBlock.record(block.header.totalManaUsed.toNumber() / 1e6);
133
+ }
134
+
135
+ public processNewCheckpointedBlocks(syncTimePerCheckpoint: number, blocks: L2Block[]) {
136
+ if (blocks.length === 0) {
137
+ return;
126
138
  }
139
+
140
+ this.syncDurationPerCheckpoint.record(Math.ceil(syncTimePerCheckpoint));
141
+ this.blockHeight.record(Math.max(...blocks.map(b => b.number)), { [Attributes.STATUS]: 'checkpointed' });
142
+ this.checkpointHeight.record(Math.max(...blocks.map(b => b.checkpointNumber)));
143
+ this.syncBlockCount.add(blocks.length);
127
144
  }
128
145
 
129
146
  public processNewMessages(count: number, syncPerMessageMs: number) {
@@ -135,10 +152,21 @@ export class ArchiverInstrumentation {
135
152
  }
136
153
 
137
154
  public processPrune(duration: number) {
138
- this.pruneCount.add(1);
155
+ this.pruneCount.add(1, { [Attributes.PRUNE_TYPE]: 'unproven' });
139
156
  this.pruneDuration.record(Math.ceil(duration));
140
157
  }
141
158
 
159
+ /**
160
+ * Records a pending-chain reorg, where the archiver dropped proposed blocks (and world-state follows by pruning). The
161
+ * type distinguishes the cause: 'uncheckpointed' (slot ended without a checkpoint), 'l1_conflict' (proposed blocks
162
+ * conflicting with an L1 checkpoint), 'orphan' (no matching proposed checkpoint arrived before the deadline), or
163
+ * 'l1_mismatch' (the local checkpointed tip diverged from L1 — an L1 reorg or a pruned/missed-proof checkpoint — so
164
+ * already-checkpointed blocks were rewound).
165
+ */
166
+ public recordPrune(pruneType: 'uncheckpointed' | 'l1_conflict' | 'orphan' | 'l1_mismatch') {
167
+ this.pruneCount.add(1, { [Attributes.PRUNE_TYPE]: pruneType });
168
+ }
169
+
142
170
  public updateLastProvenCheckpoint(checkpoint: CheckpointData) {
143
171
  const lastBlockNumberInCheckpoint = checkpoint.startBlock + checkpoint.blockCount - 1;
144
172
  this.blockHeight.record(lastBlockNumberInCheckpoint, { [Attributes.STATUS]: 'proven' });
@@ -169,6 +197,11 @@ export class ArchiverInstrumentation {
169
197
  });
170
198
  }
171
199
 
200
+ /** Records a checkpoint promoted from proposed (blob fetch skipped). */
201
+ public processCheckpointPromoted() {
202
+ this.checkpointPromotedCount.add(1);
203
+ }
204
+
172
205
  /**
173
206
  * Records L1 inclusion timing for a checkpoint observed on L1 (seconds into the L2 slot).
174
207
  */