@aztec/archiver 0.0.1-commit.5de5ca79e → 0.0.1-commit.6201a7b05

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 (99) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +16 -7
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +104 -48
  5. package/dest/config.d.ts +3 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +14 -3
  8. package/dest/errors.d.ts +41 -2
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +62 -1
  11. package/dest/factory.d.ts +2 -2
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +9 -7
  14. package/dest/index.d.ts +3 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +2 -1
  17. package/dest/l1/calldata_retriever.d.ts +2 -1
  18. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  19. package/dest/l1/calldata_retriever.js +11 -5
  20. package/dest/l1/data_retrieval.d.ts +19 -10
  21. package/dest/l1/data_retrieval.d.ts.map +1 -1
  22. package/dest/l1/data_retrieval.js +25 -32
  23. package/dest/l1/validate_historical_logs.d.ts +23 -0
  24. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  25. package/dest/l1/validate_historical_logs.js +108 -0
  26. package/dest/modules/data_source_base.d.ts +8 -2
  27. package/dest/modules/data_source_base.d.ts.map +1 -1
  28. package/dest/modules/data_source_base.js +19 -1
  29. package/dest/modules/data_store_updater.d.ts +15 -10
  30. package/dest/modules/data_store_updater.d.ts.map +1 -1
  31. package/dest/modules/data_store_updater.js +71 -68
  32. package/dest/modules/instrumentation.d.ts +7 -2
  33. package/dest/modules/instrumentation.d.ts.map +1 -1
  34. package/dest/modules/instrumentation.js +22 -6
  35. package/dest/modules/l1_synchronizer.d.ts +7 -2
  36. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  37. package/dest/modules/l1_synchronizer.js +261 -143
  38. package/dest/modules/validation.d.ts +4 -3
  39. package/dest/modules/validation.d.ts.map +1 -1
  40. package/dest/modules/validation.js +6 -6
  41. package/dest/store/block_store.d.ts +72 -5
  42. package/dest/store/block_store.d.ts.map +1 -1
  43. package/dest/store/block_store.js +341 -66
  44. package/dest/store/contract_class_store.d.ts +2 -3
  45. package/dest/store/contract_class_store.d.ts.map +1 -1
  46. package/dest/store/contract_class_store.js +7 -67
  47. package/dest/store/contract_instance_store.d.ts +1 -1
  48. package/dest/store/contract_instance_store.d.ts.map +1 -1
  49. package/dest/store/contract_instance_store.js +6 -2
  50. package/dest/store/kv_archiver_store.d.ts +51 -17
  51. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  52. package/dest/store/kv_archiver_store.js +60 -16
  53. package/dest/store/l2_tips_cache.d.ts +2 -1
  54. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  55. package/dest/store/l2_tips_cache.js +27 -7
  56. package/dest/store/log_store.d.ts +1 -1
  57. package/dest/store/log_store.d.ts.map +1 -1
  58. package/dest/store/log_store.js +2 -4
  59. package/dest/store/message_store.d.ts +3 -3
  60. package/dest/store/message_store.d.ts.map +1 -1
  61. package/dest/store/message_store.js +9 -10
  62. package/dest/test/fake_l1_state.d.ts +15 -3
  63. package/dest/test/fake_l1_state.d.ts.map +1 -1
  64. package/dest/test/fake_l1_state.js +80 -18
  65. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  66. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  67. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  68. package/dest/test/mock_l2_block_source.d.ts +16 -2
  69. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  70. package/dest/test/mock_l2_block_source.js +50 -3
  71. package/dest/test/noop_l1_archiver.d.ts +1 -1
  72. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  73. package/dest/test/noop_l1_archiver.js +4 -2
  74. package/package.json +13 -13
  75. package/src/archiver.ts +126 -46
  76. package/src/config.ts +15 -1
  77. package/src/errors.ts +97 -2
  78. package/src/factory.ts +13 -7
  79. package/src/index.ts +2 -1
  80. package/src/l1/calldata_retriever.ts +17 -5
  81. package/src/l1/data_retrieval.ts +36 -45
  82. package/src/l1/validate_historical_logs.ts +140 -0
  83. package/src/modules/data_source_base.ts +32 -1
  84. package/src/modules/data_store_updater.ts +98 -97
  85. package/src/modules/instrumentation.ts +27 -7
  86. package/src/modules/l1_synchronizer.ts +328 -169
  87. package/src/modules/validation.ts +10 -9
  88. package/src/store/block_store.ts +419 -76
  89. package/src/store/contract_class_store.ts +8 -106
  90. package/src/store/contract_instance_store.ts +8 -5
  91. package/src/store/kv_archiver_store.ts +100 -32
  92. package/src/store/l2_tips_cache.ts +58 -13
  93. package/src/store/log_store.ts +2 -5
  94. package/src/store/message_store.ts +10 -12
  95. package/src/structs/inbox_message.ts +1 -1
  96. package/src/test/fake_l1_state.ts +99 -27
  97. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  98. package/src/test/mock_l2_block_source.ts +58 -2
  99. package/src/test/noop_l1_archiver.ts +3 -1
@@ -10,6 +10,7 @@ import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } fro
10
10
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
11
11
  import {
12
12
  type BlockData,
13
+ type BlockDataWithCheckpointContext,
13
14
  BlockHash,
14
15
  Body,
15
16
  CheckpointedL2Block,
@@ -19,7 +20,15 @@ import {
19
20
  deserializeValidateCheckpointResult,
20
21
  serializeValidateCheckpointResult,
21
22
  } from '@aztec/stdlib/block';
22
- import { type CheckpointData, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
23
+ import {
24
+ Checkpoint,
25
+ type CheckpointData,
26
+ type CommonCheckpointData,
27
+ L1PublishedData,
28
+ type ProposedCheckpointData,
29
+ type ProposedCheckpointInput,
30
+ PublishedCheckpoint,
31
+ } from '@aztec/stdlib/checkpoint';
23
32
  import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
24
33
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
25
34
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
@@ -37,6 +46,7 @@ import {
37
46
  import {
38
47
  BlockAlreadyCheckpointedError,
39
48
  BlockArchiveNotConsistentError,
49
+ BlockCheckpointNumberNotSequentialError,
40
50
  BlockIndexNotSequentialError,
41
51
  BlockNotFoundError,
42
52
  BlockNumberNotSequentialError,
@@ -44,6 +54,10 @@ import {
44
54
  CheckpointNotFoundError,
45
55
  CheckpointNumberNotSequentialError,
46
56
  InitialCheckpointNumberNotSequentialError,
57
+ NoProposedCheckpointToPromoteError,
58
+ ProposedCheckpointArchiveRootMismatchError,
59
+ ProposedCheckpointNotSequentialError,
60
+ ProposedCheckpointPromotionNotSequentialError,
47
61
  } from '../errors.js';
48
62
 
49
63
  export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
@@ -58,15 +72,26 @@ type BlockStorage = {
58
72
  indexWithinCheckpoint: number;
59
73
  };
60
74
 
61
- type CheckpointStorage = {
75
+ /** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
76
+ type CommonCheckpointStorage = {
62
77
  header: Buffer;
63
78
  archive: Buffer;
64
79
  checkpointOutHash: Buffer;
65
80
  checkpointNumber: number;
66
81
  startBlock: number;
67
82
  blockCount: number;
83
+ };
84
+
85
+ type CheckpointStorage = CommonCheckpointStorage & {
68
86
  l1: Buffer;
69
87
  attestations: Buffer[];
88
+ feeAssetPriceModifier: string;
89
+ };
90
+
91
+ /** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
92
+ type ProposedCheckpointStorage = CommonCheckpointStorage & {
93
+ totalManaUsed: string;
94
+ feeAssetPriceModifier: string;
70
95
  };
71
96
 
72
97
  export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
@@ -78,7 +103,10 @@ export class BlockStore {
78
103
  /** Map block number to block data */
79
104
  #blocks: AztecAsyncMap<number, BlockStorage>;
80
105
 
81
- /** Map checkpoint number to checkpoint data */
106
+ /** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */
107
+ #proposedCheckpoints: AztecAsyncMap<number, ProposedCheckpointStorage>;
108
+
109
+ /** Map checkpoint number to checkpoint data for mined checkpoints only */
82
110
  #checkpoints: AztecAsyncMap<number, CheckpointStorage>;
83
111
 
84
112
  /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
@@ -126,6 +154,7 @@ export class BlockStore {
126
154
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
127
155
  this.#checkpoints = db.openMap('archiver_checkpoints');
128
156
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
157
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
129
158
  }
130
159
 
131
160
  /**
@@ -160,8 +189,8 @@ export class BlockStore {
160
189
  const blockLastArchive = block.header.lastArchive.root;
161
190
 
162
191
  // Extract the latest block and checkpoint numbers
163
- const previousBlockNumber = await this.getLatestBlockNumber();
164
- const previousCheckpointNumber = await this.getLatestCheckpointNumber();
192
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
193
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
165
194
 
166
195
  // Verify we're not overwriting checkpointed blocks
167
196
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
@@ -179,9 +208,14 @@ export class BlockStore {
179
208
  throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
180
209
  }
181
210
 
182
- // The same check as above but for checkpoints
183
- if (!opts.force && previousCheckpointNumber !== blockCheckpointNumber - 1) {
184
- throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, previousCheckpointNumber);
211
+ // Accept the block if either the confirmed checkpoint or a pending checkpoint matches
212
+ // the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
213
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
214
+ const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
215
+ if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
216
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
217
+ const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
218
+ throw new BlockCheckpointNumberNotSequentialError(blockNumber, blockCheckpointNumber, previous);
185
219
  }
186
220
 
187
221
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
@@ -217,7 +251,7 @@ export class BlockStore {
217
251
  }
218
252
 
219
253
  /**
220
- * Append new cheskpoints to the store's list.
254
+ * Append new checkpoints to the store's list.
221
255
  * @param checkpoints - The L2 checkpoints to be added to the store.
222
256
  * @returns True if the operation is successful.
223
257
  */
@@ -227,37 +261,29 @@ export class BlockStore {
227
261
  }
228
262
 
229
263
  return await this.db.transactionAsync(async () => {
230
- // Check that the checkpoint immediately before the first block to be added is present in the store.
231
264
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
232
265
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
233
266
 
234
- if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
235
- throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
236
- }
237
-
238
- // Extract the previous checkpoint if there is one
239
- let previousCheckpointData: CheckpointData | undefined = undefined;
240
- if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
241
- // There should be a previous checkpoint
242
- previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
243
- if (previousCheckpointData === undefined) {
244
- throw new CheckpointNotFoundError(previousCheckpointNumber);
267
+ // Handle already-stored checkpoints at the start of the batch.
268
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
269
+ // We accept them if archives match (same content) and update their L1 metadata.
270
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
271
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
272
+ if (checkpoints.length === 0) {
273
+ return true;
245
274
  }
246
- }
247
-
248
- let previousBlockNumber: BlockNumber | undefined = undefined;
249
- let previousBlock: L2Block | undefined = undefined;
250
-
251
- // If we have a previous checkpoint then we need to get the previous block number
252
- if (previousCheckpointData !== undefined) {
253
- previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
254
- previousBlock = await this.getBlock(previousBlockNumber);
255
- if (previousBlock === undefined) {
256
- // We should be able to get the required previous block
257
- throw new BlockNotFoundError(previousBlockNumber);
275
+ // Re-check sequentiality after skipping
276
+ const newFirstNumber = checkpoints[0].checkpoint.number;
277
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
278
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
258
279
  }
280
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
281
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
259
282
  }
260
283
 
284
+ // Get the last block of the previous checkpoint for archive chaining
285
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
286
+
261
287
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
262
288
  let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
263
289
  for (const checkpoint of checkpoints) {
@@ -273,42 +299,14 @@ export class BlockStore {
273
299
  }
274
300
  previousCheckpoint = checkpoint;
275
301
 
276
- // Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
277
- for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
278
- const block = checkpoint.checkpoint.blocks[i];
279
- if (previousBlock) {
280
- // The blocks should have a sequential block number
281
- if (previousBlock.number !== block.number - 1) {
282
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
283
- }
284
- // If the blocks are for the same checkpoint then they should have sequential indexes
285
- if (
286
- previousBlock.checkpointNumber === block.checkpointNumber &&
287
- previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
288
- ) {
289
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
290
- }
291
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
292
- throw new BlockArchiveNotConsistentError(
293
- block.number,
294
- previousBlock.number,
295
- block.header.lastArchive.root,
296
- previousBlock.archive.root,
297
- );
298
- }
299
- } else {
300
- // No previous block, must be block 1 at checkpoint index 0
301
- if (block.indexWithinCheckpoint !== 0) {
302
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
303
- }
304
- if (block.number !== INITIAL_L2_BLOCK_NUM) {
305
- throw new BlockNumberNotSequentialError(block.number, undefined);
306
- }
307
- }
302
+ // Validate block sequencing, indexes, and archive chaining
303
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
308
304
 
309
- previousBlock = block;
310
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
305
+ // Store every block in the database (may already exist, but L1 data is authoritative)
306
+ for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
307
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
311
308
  }
309
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
312
310
 
313
311
  // Store the checkpoint in the database
314
312
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
@@ -320,10 +318,14 @@ export class BlockStore {
320
318
  checkpointNumber: checkpoint.checkpoint.number,
321
319
  startBlock: checkpoint.checkpoint.blocks[0].number,
322
320
  blockCount: checkpoint.checkpoint.blocks.length,
321
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString(),
323
322
  });
324
323
 
325
324
  // Update slot-to-checkpoint index
326
325
  await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
326
+
327
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
328
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
327
329
  }
328
330
 
329
331
  await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
@@ -331,6 +333,116 @@ export class BlockStore {
331
333
  });
332
334
  }
333
335
 
336
+ /**
337
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
338
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
339
+ */
340
+ private async skipOrUpdateAlreadyStoredCheckpoints(
341
+ checkpoints: PublishedCheckpoint[],
342
+ latestStored: CheckpointNumber,
343
+ ): Promise<PublishedCheckpoint[]> {
344
+ let i = 0;
345
+ for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
346
+ const incoming = checkpoints[i];
347
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
348
+ if (!stored) {
349
+ // Should not happen if latestStored is correct, but be safe
350
+ break;
351
+ }
352
+ // Verify the checkpoint content matches (archive root)
353
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
354
+ throw new Error(
355
+ `Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
356
+ `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
357
+ );
358
+ }
359
+ // Update L1 metadata and attestations for the already-stored checkpoint
360
+ this.#log.warn(
361
+ `Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
362
+ `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
363
+ );
364
+ await this.#checkpoints.set(incoming.checkpoint.number, {
365
+ header: incoming.checkpoint.header.toBuffer(),
366
+ archive: incoming.checkpoint.archive.toBuffer(),
367
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
368
+ l1: incoming.l1.toBuffer(),
369
+ attestations: incoming.attestations.map(a => a.toBuffer()),
370
+ checkpointNumber: incoming.checkpoint.number,
371
+ startBlock: incoming.checkpoint.blocks[0].number,
372
+ blockCount: incoming.checkpoint.blocks.length,
373
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString(),
374
+ });
375
+ // Update the sync point to reflect the new L1 block
376
+ await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
377
+ }
378
+ return checkpoints.slice(i);
379
+ }
380
+
381
+ /**
382
+ * Gets the last block of the checkpoint before the given one.
383
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
384
+ */
385
+ private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
386
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
387
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
388
+ return undefined;
389
+ }
390
+
391
+ // Check across both proposed and mined checkpoints
392
+ const predecessor =
393
+ (await this.getProposedCheckpointByNumber(previousCheckpointNumber)) ??
394
+ (await this.getCheckpointData(previousCheckpointNumber));
395
+
396
+ if (!predecessor) {
397
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
398
+ }
399
+
400
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
401
+ const previousBlock = await this.getBlock(previousBlockNumber);
402
+ if (previousBlock === undefined) {
403
+ throw new BlockNotFoundError(previousBlockNumber);
404
+ }
405
+ return previousBlock;
406
+ }
407
+
408
+ /**
409
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
410
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
411
+ * proposed checkpoints (addProposedCheckpoint).
412
+ */
413
+ private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
414
+ for (const block of blocks) {
415
+ if (previousBlock) {
416
+ if (previousBlock.number !== block.number - 1) {
417
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
418
+ }
419
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
420
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
421
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
422
+ }
423
+ } else if (block.indexWithinCheckpoint !== 0) {
424
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
425
+ }
426
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
427
+ throw new BlockArchiveNotConsistentError(
428
+ block.number,
429
+ previousBlock.number,
430
+ block.header.lastArchive.root,
431
+ previousBlock.archive.root,
432
+ );
433
+ }
434
+ } else {
435
+ if (block.indexWithinCheckpoint !== 0) {
436
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
437
+ }
438
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
439
+ throw new BlockNumberNotSequentialError(block.number, undefined);
440
+ }
441
+ }
442
+ previousBlock = block;
443
+ }
444
+ }
445
+
334
446
  private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
335
447
  const blockHash = await block.hash();
336
448
 
@@ -423,6 +535,9 @@ export class BlockStore {
423
535
  this.#log.debug(`Removed checkpoint ${c}`);
424
536
  }
425
537
 
538
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
539
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
540
+
426
541
  return { blocksRemoved };
427
542
  });
428
543
  }
@@ -470,6 +585,7 @@ export class BlockStore {
470
585
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
471
586
  startBlock: BlockNumber(checkpointStorage.startBlock),
472
587
  blockCount: checkpointStorage.blockCount,
588
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
473
589
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
474
590
  attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
475
591
  };
@@ -528,7 +644,7 @@ export class BlockStore {
528
644
  const removedBlocks: L2Block[] = [];
529
645
 
530
646
  // Get the latest block number to determine the range
531
- const latestBlockNumber = await this.getLatestBlockNumber();
647
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
532
648
 
533
649
  // Iterate from blockNumber + 1 to latestBlockNumber
534
650
  for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
@@ -561,13 +677,6 @@ export class BlockStore {
561
677
  }
562
678
  }
563
679
 
564
- async getLatestBlockNumber(): Promise<BlockNumber> {
565
- const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
566
- return typeof latestBlocknumber === 'number'
567
- ? BlockNumber(latestBlocknumber)
568
- : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
569
- }
570
-
571
680
  async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
572
681
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
573
682
  if (latestCheckpointNumber === undefined) {
@@ -576,6 +685,167 @@ export class BlockStore {
576
685
  return CheckpointNumber(latestCheckpointNumber);
577
686
  }
578
687
 
688
+ async hasProposedCheckpoint(): Promise<boolean> {
689
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ limit: 1 }));
690
+ return key !== undefined;
691
+ }
692
+
693
+ /** Deletes all pending proposed checkpoints from storage. */
694
+ async deleteProposedCheckpoints(): Promise<void> {
695
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
696
+ await this.#proposedCheckpoints.delete(key);
697
+ }
698
+ }
699
+
700
+ /**
701
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
702
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
703
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
704
+ * @param checkpointNumber - The checkpoint number to promote.
705
+ * @param l1 - L1 published data for the checkpoint.
706
+ * @param attestations - Committee attestations.
707
+ * @param expectedArchiveRoot - Archive root guard against races.
708
+ */
709
+ async promoteProposedToCheckpointed(
710
+ checkpointNumber: CheckpointNumber,
711
+ l1: L1PublishedData,
712
+ attestations: CommitteeAttestation[],
713
+ expectedArchiveRoot: Fr,
714
+ ): Promise<void> {
715
+ return await this.db.transactionAsync(async () => {
716
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
717
+ if (!proposed) {
718
+ throw new NoProposedCheckpointToPromoteError();
719
+ }
720
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
721
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
722
+ }
723
+
724
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
725
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
726
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
727
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
728
+ }
729
+
730
+ // Write the checkpoint entry
731
+ await this.#checkpoints.set(proposed.checkpointNumber, {
732
+ header: proposed.header.toBuffer(),
733
+ archive: proposed.archive.toBuffer(),
734
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
735
+ l1: l1.toBuffer(),
736
+ attestations: attestations.map(attestation => attestation.toBuffer()),
737
+ checkpointNumber: proposed.checkpointNumber,
738
+ startBlock: proposed.startBlock,
739
+ blockCount: proposed.blockCount,
740
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
741
+ });
742
+
743
+ // Update the slot-to-checkpoint index
744
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
745
+
746
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
747
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
748
+
749
+ // Update the last synced L1 block
750
+ await this.#lastSynchedL1Block.set(l1.blockNumber);
751
+ });
752
+ }
753
+
754
+ /**
755
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
756
+ * No fallback to confirmed.
757
+ */
758
+ async getLastProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
759
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
760
+ if (key === undefined) {
761
+ return undefined;
762
+ }
763
+ const stored = await this.#proposedCheckpoints.getAsync(key);
764
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
765
+ }
766
+
767
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */
768
+ async getProposedCheckpointByNumber(n: CheckpointNumber): Promise<ProposedCheckpointData | undefined> {
769
+ const stored = await this.#proposedCheckpoints.getAsync(n);
770
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
771
+ }
772
+
773
+ /** Returns all pending checkpoints in ascending checkpoint-number order. */
774
+ async getProposedCheckpoints(): Promise<ProposedCheckpointData[]> {
775
+ const results: ProposedCheckpointData[] = [];
776
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()) {
777
+ results.push(this.convertToProposedCheckpointData(stored));
778
+ }
779
+ return results;
780
+ }
781
+
782
+ /**
783
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
784
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
785
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
786
+ */
787
+ async evictProposedCheckpointsFrom(fromNumber: CheckpointNumber): Promise<void> {
788
+ const keysToDelete: number[] = [];
789
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
790
+ if (key >= fromNumber) {
791
+ keysToDelete.push(key);
792
+ }
793
+ }
794
+ for (const key of keysToDelete) {
795
+ await this.#proposedCheckpoints.delete(key);
796
+ }
797
+ }
798
+
799
+ /**
800
+ * Gets the checkpoint at the proposed tip:
801
+ * - latest pending checkpoint if any exist
802
+ * - fallsback to latest confirmed checkpoint otherwise
803
+ */
804
+ async getLastCheckpoint(): Promise<CommonCheckpointData | undefined> {
805
+ const latest = await this.getLastProposedCheckpoint();
806
+ if (!latest) {
807
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
808
+ }
809
+ return latest;
810
+ }
811
+
812
+ private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
813
+ return {
814
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
815
+ header: CheckpointHeader.fromBuffer(stored.header),
816
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
817
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
818
+ startBlock: BlockNumber(stored.startBlock),
819
+ blockCount: stored.blockCount,
820
+ totalManaUsed: BigInt(stored.totalManaUsed),
821
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
822
+ };
823
+ }
824
+
825
+ /**
826
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
827
+ * @returns CheckpointNumber
828
+ */
829
+ async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
830
+ const proposed = await this.getLastCheckpoint();
831
+ if (!proposed) {
832
+ return await this.getLatestCheckpointNumber();
833
+ }
834
+ return CheckpointNumber(proposed.checkpointNumber);
835
+ }
836
+
837
+ /**
838
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
839
+ * @returns BlockNumber
840
+ */
841
+ async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
842
+ const proposed = await this.getLastCheckpoint();
843
+ if (!proposed) {
844
+ return await this.getCheckpointedL2BlockNumber();
845
+ }
846
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
847
+ }
848
+
579
849
  async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
580
850
  const blockStorage = await this.#blocks.getAsync(number);
581
851
  if (!blockStorage) {
@@ -669,6 +939,33 @@ export class BlockStore {
669
939
  return this.getBlockDataFromBlockStorage(blockStorage);
670
940
  }
671
941
 
942
+ /**
943
+ * Gets block metadata plus checkpoint-derived context (L1 publish info, attestations) without
944
+ * deserializing tx bodies. When the block's containing checkpoint has not yet been L1-confirmed,
945
+ * `checkpoint` and `l1` are `undefined` and `attestations` is empty.
946
+ */
947
+ async getBlockDataWithCheckpointContext(
948
+ blockNumber: BlockNumber,
949
+ ): Promise<BlockDataWithCheckpointContext | undefined> {
950
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
951
+ if (!blockStorage || !blockStorage.header) {
952
+ return undefined;
953
+ }
954
+ const data = this.getBlockDataFromBlockStorage(blockStorage);
955
+ const checkpointStorage = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
956
+ if (!checkpointStorage) {
957
+ return { data, checkpoint: undefined, l1: undefined, attestations: [] };
958
+ }
959
+ const checkpoint = this.checkpointDataFromCheckpointStorage(checkpointStorage);
960
+ return { data, checkpoint, l1: checkpoint.l1, attestations: checkpoint.attestations };
961
+ }
962
+
963
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */
964
+ async getCheckpointNumberBySlot(slot: SlotNumber): Promise<CheckpointNumber | undefined> {
965
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
966
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
967
+ }
968
+
672
969
  /**
673
970
  * Gets block metadata (without tx data) by archive root.
674
971
  * @param archive - The archive root of the block to return.
@@ -790,7 +1087,7 @@ export class BlockStore {
790
1087
  return {
791
1088
  header: BlockHeader.fromBuffer(blockStorage.header),
792
1089
  archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
793
- blockHash: Fr.fromBuffer(blockStorage.blockHash),
1090
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
794
1091
  checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
795
1092
  indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
796
1093
  };
@@ -950,6 +1247,52 @@ export class BlockStore {
950
1247
  return this.#lastSynchedL1Block.set(l1BlockNumber);
951
1248
  }
952
1249
 
1250
+ /**
1251
+ * Adds a proposed checkpoint to the pending queue.
1252
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1253
+ * confirmed and the highest pending checkpoint number.
1254
+ * Computes archive and checkpointOutHash from the stored blocks.
1255
+ */
1256
+ async addProposedCheckpoint(proposed: ProposedCheckpointInput) {
1257
+ return await this.db.transactionAsync(async () => {
1258
+ const confirmed = await this.getLatestCheckpointNumber();
1259
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
1260
+ const latestTip = CheckpointNumber(
1261
+ latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed,
1262
+ );
1263
+
1264
+ if (proposed.checkpointNumber !== latestTip + 1) {
1265
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
1266
+ }
1267
+
1268
+ // Ensure the predecessor block (from pending or confirmed chain) exists
1269
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1270
+ const blocks: L2Block[] = [];
1271
+ for (let i = 0; i < proposed.blockCount; i++) {
1272
+ const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1273
+ if (!block) {
1274
+ throw new BlockNotFoundError(proposed.startBlock + i);
1275
+ }
1276
+ blocks.push(block);
1277
+ }
1278
+ this.validateCheckpointBlocks(blocks, previousBlock);
1279
+
1280
+ const archive = blocks[blocks.length - 1].archive;
1281
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1282
+
1283
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
1284
+ header: proposed.header.toBuffer(),
1285
+ archive: archive.toBuffer(),
1286
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1287
+ checkpointNumber: proposed.checkpointNumber,
1288
+ startBlock: proposed.startBlock,
1289
+ blockCount: proposed.blockCount,
1290
+ totalManaUsed: proposed.totalManaUsed.toString(),
1291
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1292
+ });
1293
+ });
1294
+ }
1295
+
953
1296
  async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
954
1297
  const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
955
1298
  this.getLatestCheckpointNumber(),