@aztec/archiver 0.0.1-commit.b2a5d0dd1 → 0.0.1-commit.b3d3157a

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 (106) hide show
  1. package/README.md +7 -5
  2. package/dest/archiver.d.ts +49 -14
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +216 -68
  5. package/dest/config.d.ts +4 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +11 -16
  8. package/dest/errors.d.ts +19 -13
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +32 -16
  11. package/dest/factory.d.ts +12 -6
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +31 -21
  14. package/dest/index.d.ts +9 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +8 -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 +13 -4
  20. package/dest/l1/trace_tx.d.ts +12 -66
  21. package/dest/l1/trace_tx.d.ts.map +1 -1
  22. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  23. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  24. package/dest/modules/contract_data_source_adapter.js +40 -0
  25. package/dest/modules/data_source_base.d.ts +71 -49
  26. package/dest/modules/data_source_base.d.ts.map +1 -1
  27. package/dest/modules/data_source_base.js +269 -143
  28. package/dest/modules/data_store_updater.d.ts +18 -6
  29. package/dest/modules/data_store_updater.d.ts.map +1 -1
  30. package/dest/modules/data_store_updater.js +108 -59
  31. package/dest/modules/instrumentation.js +3 -1
  32. package/dest/modules/l1_synchronizer.d.ts +6 -5
  33. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  34. package/dest/modules/l1_synchronizer.js +203 -100
  35. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  36. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  37. package/dest/modules/outbox_trees_resolver.js +162 -0
  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 +4 -4
  41. package/dest/store/block_store.d.ts +155 -98
  42. package/dest/store/block_store.d.ts.map +1 -1
  43. package/dest/store/block_store.js +473 -265
  44. package/dest/store/contract_class_store.d.ts +17 -3
  45. package/dest/store/contract_class_store.d.ts.map +1 -1
  46. package/dest/store/contract_class_store.js +17 -1
  47. package/dest/store/contract_instance_store.d.ts +28 -1
  48. package/dest/store/contract_instance_store.d.ts.map +1 -1
  49. package/dest/store/contract_instance_store.js +31 -0
  50. package/dest/store/data_stores.d.ts +68 -0
  51. package/dest/store/data_stores.d.ts.map +1 -0
  52. package/dest/store/data_stores.js +54 -0
  53. package/dest/store/function_names_cache.d.ts +17 -0
  54. package/dest/store/function_names_cache.d.ts.map +1 -0
  55. package/dest/store/function_names_cache.js +30 -0
  56. package/dest/store/l2_tips_cache.d.ts +13 -8
  57. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  58. package/dest/store/l2_tips_cache.js +13 -96
  59. package/dest/store/log_store.d.ts +42 -40
  60. package/dest/store/log_store.d.ts.map +1 -1
  61. package/dest/store/log_store.js +257 -478
  62. package/dest/store/log_store_codec.d.ts +70 -0
  63. package/dest/store/log_store_codec.d.ts.map +1 -0
  64. package/dest/store/log_store_codec.js +101 -0
  65. package/dest/store/message_store.d.ts +9 -3
  66. package/dest/store/message_store.d.ts.map +1 -1
  67. package/dest/store/message_store.js +31 -1
  68. package/dest/test/fake_l1_state.d.ts +2 -1
  69. package/dest/test/fake_l1_state.d.ts.map +1 -1
  70. package/dest/test/fake_l1_state.js +25 -8
  71. package/dest/test/mock_l2_block_source.d.ts +47 -51
  72. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  73. package/dest/test/mock_l2_block_source.js +223 -185
  74. package/dest/test/noop_l1_archiver.d.ts +10 -6
  75. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  76. package/dest/test/noop_l1_archiver.js +19 -8
  77. package/package.json +14 -14
  78. package/src/archiver.ts +265 -72
  79. package/src/config.ts +12 -19
  80. package/src/errors.ts +46 -23
  81. package/src/factory.ts +38 -20
  82. package/src/index.ts +16 -1
  83. package/src/l1/calldata_retriever.ts +14 -4
  84. package/src/modules/contract_data_source_adapter.ts +55 -0
  85. package/src/modules/data_source_base.ts +335 -187
  86. package/src/modules/data_store_updater.ts +118 -61
  87. package/src/modules/instrumentation.ts +1 -1
  88. package/src/modules/l1_synchronizer.ts +278 -120
  89. package/src/modules/outbox_trees_resolver.ts +199 -0
  90. package/src/modules/validation.ts +8 -7
  91. package/src/store/block_store.ts +566 -323
  92. package/src/store/contract_class_store.ts +28 -2
  93. package/src/store/contract_instance_store.ts +43 -0
  94. package/src/store/data_stores.ts +104 -0
  95. package/src/store/function_names_cache.ts +37 -0
  96. package/src/store/l2_tips_cache.ts +16 -115
  97. package/src/store/log_store.ts +296 -650
  98. package/src/store/log_store_codec.ts +132 -0
  99. package/src/store/message_store.ts +35 -2
  100. package/src/test/fake_l1_state.ts +24 -14
  101. package/src/test/mock_l2_block_source.ts +271 -239
  102. package/src/test/noop_l1_archiver.ts +39 -8
  103. package/dest/store/kv_archiver_store.d.ts +0 -383
  104. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  105. package/dest/store/kv_archiver_store.js +0 -501
  106. package/src/store/kv_archiver_store.ts +0 -728
@@ -1,25 +1,24 @@
1
1
  import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
- import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
2
+ import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
3
3
  import { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import { toArray } from '@aztec/foundation/iterable';
5
5
  import { createLogger } from '@aztec/foundation/log';
6
6
  import { BufferReader } from '@aztec/foundation/serialize';
7
7
  import { bufferToHex } from '@aztec/foundation/string';
8
8
  import { isDefined } from '@aztec/foundation/types';
9
- import { BlockHash, Body, CheckpointedL2Block, CommitteeAttestation, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
9
+ import { BlockHash, Body, CommitteeAttestation, GENESIS_CHECKPOINT_HEADER_HASH, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
10
10
  import { Checkpoint, L1PublishedData } from '@aztec/stdlib/checkpoint';
11
- import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
12
11
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
13
12
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
14
- import { BlockHeader, TxHash, TxReceipt, TxStatus, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
15
- import { BlockAlreadyCheckpointedError, BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotSequentialError, InitialCheckpointNumberNotSequentialError, NoProposedCheckpointToPromoteError, ProposedCheckpointArchiveRootMismatchError, ProposedCheckpointNotSequentialError, ProposedCheckpointPromotionNotSequentialError, ProposedCheckpointStaleError } from '../errors.js';
16
- export { TxReceipt } from '@aztec/stdlib/tx';
13
+ import { BlockHeader, TxHash, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
14
+ import { BlockAlreadyCheckpointedError, BlockArchiveNotConsistentError, BlockCheckpointNumberNotSequentialError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotSequentialError, InitialCheckpointNumberNotSequentialError, NoProposedCheckpointToPromoteError, ProposedCheckpointArchiveRootMismatchError, ProposedCheckpointNotSequentialError, ProposedCheckpointPromotionNotSequentialError } from '../errors.js';
17
15
  /**
18
16
  * LMDB-based block storage for the archiver.
19
17
  */ export class BlockStore {
20
18
  db;
21
19
  /** Map block number to block data */ #blocks;
22
- /** Map checkpoint number to checkpoint data */ #checkpoints;
20
+ /** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */ #proposedCheckpoints;
21
+ /** Map checkpoint number to checkpoint data for mined checkpoints only */ #checkpoints;
23
22
  /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */ #slotToCheckpoint;
24
23
  /** Map block hash to list of tx hashes */ #blockTxs;
25
24
  /** Tx hash to serialized IndexedTxEffect */ #txEffects;
@@ -30,7 +29,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
30
29
  /** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
31
30
  /** Index mapping block hash to block number */ #blockHashIndex;
32
31
  /** Index mapping block archive to block number */ #blockArchiveIndex;
33
- /** Singleton: assumes max 1-deep pipeline. For deeper pipelining, replace with a map keyed by checkpoint number. */ #proposedCheckpoint;
32
+ /** Map rejected checkpoints (due to invalid attestations) by archive root */ #rejectedCheckpoints;
33
+ /** Index mapping a rejected checkpoint's number to its archive root, so the latest can be read in reverse order */ #rejectedCheckpointsByNumber;
34
34
  #log;
35
35
  constructor(db){
36
36
  this.db = db;
@@ -47,7 +47,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
47
47
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
48
48
  this.#checkpoints = db.openMap('archiver_checkpoints');
49
49
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
50
- this.#proposedCheckpoint = db.openSingleton('proposed_checkpoint_data');
50
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
51
+ this.#rejectedCheckpoints = db.openMap('archiver_rejected_checkpoints');
52
+ this.#rejectedCheckpointsByNumber = db.openMap('archiver_rejected_checkpoints_by_number');
51
53
  }
52
54
  /**
53
55
  * Returns the finalized L2 block number. An L2 block is finalized when it was proven
@@ -78,13 +80,14 @@ export { TxReceipt } from '@aztec/stdlib/tx';
78
80
  const blockLastArchive = block.header.lastArchive.root;
79
81
  // Extract the latest block and checkpoint numbers
80
82
  const previousBlockNumber = await this.getLatestL2BlockNumber();
81
- const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
82
- const previousCheckpointNumber = await this.getLatestCheckpointNumber();
83
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
83
84
  // Verify we're not overwriting checkpointed blocks
84
85
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
85
86
  if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
86
87
  // Check if the proposed block matches the already-checkpointed one
87
- const existingBlock = await this.getBlock(BlockNumber(blockNumber));
88
+ const existingBlock = await this.getBlockData({
89
+ number: BlockNumber(blockNumber)
90
+ });
88
91
  if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
89
92
  throw new BlockAlreadyCheckpointedError(blockNumber);
90
93
  }
@@ -94,21 +97,22 @@ export { TxReceipt } from '@aztec/stdlib/tx';
94
97
  if (!opts.force && previousBlockNumber !== blockNumber - 1) {
95
98
  throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
96
99
  }
97
- // The same check as above but for checkpoints. Accept the block if either the confirmed
98
- // checkpoint or the pending (locally validated but not yet confirmed) checkpoint matches.
100
+ // Accept the block if either the confirmed checkpoint or a pending checkpoint matches
101
+ // the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
99
102
  const expectedCheckpointNumber = blockCheckpointNumber - 1;
100
- if (!opts.force && previousCheckpointNumber !== expectedCheckpointNumber && proposedCheckpointNumber !== expectedCheckpointNumber) {
101
- const [reported, source] = proposedCheckpointNumber > previousCheckpointNumber ? [
102
- proposedCheckpointNumber,
103
- 'proposed'
104
- ] : [
105
- previousCheckpointNumber,
106
- 'confirmed'
107
- ];
108
- throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
103
+ const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
104
+ if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
105
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
106
+ reverse: true,
107
+ limit: 1
108
+ }));
109
+ const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
110
+ throw new BlockCheckpointNumberNotSequentialError(BlockNumber(blockNumber), blockCheckpointNumber, previous);
109
111
  }
110
112
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
111
- const previousBlockResult = await this.getBlock(previousBlockNumber);
113
+ const previousBlockResult = await this.getBlockData({
114
+ number: previousBlockNumber
115
+ });
112
116
  let expectedBlockIndex = 0;
113
117
  let previousBlockIndex = undefined;
114
118
  if (previousBlockResult !== undefined) {
@@ -118,7 +122,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
118
122
  expectedBlockIndex = previousBlockIndex + 1;
119
123
  }
120
124
  if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
121
- throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.number, blockLastArchive, previousBlockResult.archive.root);
125
+ throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.header.globalVariables.blockNumber, blockLastArchive, previousBlockResult.archive.root);
122
126
  }
123
127
  }
124
128
  // Now check that the block has the expected index value
@@ -181,15 +185,18 @@ export { TxReceipt } from '@aztec/stdlib/tx';
181
185
  attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
182
186
  checkpointNumber: checkpoint.checkpoint.number,
183
187
  startBlock: checkpoint.checkpoint.blocks[0].number,
184
- blockCount: checkpoint.checkpoint.blocks.length
188
+ blockCount: checkpoint.checkpoint.blocks.length,
189
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
185
190
  });
186
191
  // Update slot-to-checkpoint index
187
192
  await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
193
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
194
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
195
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
196
+ // (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
197
+ await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
188
198
  }
189
- // Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
190
- const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
191
- await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
192
- await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
199
+ await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
193
200
  return true;
194
201
  });
195
202
  }
@@ -218,10 +225,11 @@ export { TxReceipt } from '@aztec/stdlib/tx';
218
225
  attestations: incoming.attestations.map((a)=>a.toBuffer()),
219
226
  checkpointNumber: incoming.checkpoint.number,
220
227
  startBlock: incoming.checkpoint.blocks[0].number,
221
- blockCount: incoming.checkpoint.blocks.length
228
+ blockCount: incoming.checkpoint.blocks.length,
229
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString()
222
230
  });
223
231
  // Update the sync point to reflect the new L1 block
224
- await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
232
+ await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
225
233
  }
226
234
  return checkpoints.slice(i);
227
235
  }
@@ -233,12 +241,15 @@ export { TxReceipt } from '@aztec/stdlib/tx';
233
241
  if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
234
242
  return undefined;
235
243
  }
236
- const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
237
- if (previousCheckpointData === undefined) {
244
+ // Check across both proposed and mined checkpoints
245
+ const predecessor = await this.getProposedCheckpointByNumber(previousCheckpointNumber) ?? await this.getCheckpointData(previousCheckpointNumber);
246
+ if (!predecessor) {
238
247
  throw new CheckpointNotFoundError(previousCheckpointNumber);
239
248
  }
240
- const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
241
- const previousBlock = await this.getBlock(previousBlockNumber);
249
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
250
+ const previousBlock = await this.getBlock({
251
+ number: previousBlockNumber
252
+ });
242
253
  if (previousBlock === undefined) {
243
254
  throw new BlockNotFoundError(previousBlockNumber);
244
255
  }
@@ -247,7 +258,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
247
258
  /**
248
259
  * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
249
260
  * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
250
- * proposed checkpoints (setProposedCheckpoint).
261
+ * proposed checkpoints (addProposedCheckpoint).
251
262
  */ validateCheckpointBlocks(blocks, previousBlock) {
252
263
  for (const block of blocks){
253
264
  if (previousBlock) {
@@ -289,7 +300,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
289
300
  data: block.body.txEffects[i],
290
301
  l2BlockNumber: block.number,
291
302
  l2BlockHash: blockHash,
292
- txIndexInBlock: i
303
+ txIndexInBlock: i,
304
+ slotNumber: block.header.globalVariables.slotNumber
293
305
  };
294
306
  await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
295
307
  }
@@ -352,11 +364,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
352
364
  await this.#checkpoints.delete(c);
353
365
  this.#log.debug(`Removed checkpoint ${c}`);
354
366
  }
355
- // Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
356
- const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
357
- if (proposedCheckpointNumber > checkpointNumber) {
358
- await this.#proposedCheckpoint.delete();
359
- }
367
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
368
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
360
369
  return {
361
370
  blocksRemoved
362
371
  };
@@ -393,6 +402,20 @@ export { TxReceipt } from '@aztec/stdlib/tx';
393
402
  }
394
403
  return result;
395
404
  }
405
+ /**
406
+ * Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
407
+ * Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
408
+ * checkpoints fall in the range and will fetch full data for at most a few of them.
409
+ */ async getCheckpointNumbersForSlotRange(startSlot, endSlot) {
410
+ const result = [];
411
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
412
+ start: startSlot,
413
+ end: endSlot + 1
414
+ })){
415
+ result.push(CheckpointNumber(checkpointNumber));
416
+ }
417
+ return result;
418
+ }
396
419
  checkpointDataFromCheckpointStorage(checkpointStorage) {
397
420
  return {
398
421
  header: CheckpointHeader.fromBuffer(checkpointStorage.header),
@@ -401,6 +424,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
401
424
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
402
425
  startBlock: BlockNumber(checkpointStorage.startBlock),
403
426
  blockCount: checkpointStorage.blockCount,
427
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
404
428
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
405
429
  attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
406
430
  };
@@ -452,7 +476,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
452
476
  const latestBlockNumber = await this.getLatestL2BlockNumber();
453
477
  // Iterate from blockNumber + 1 to latestBlockNumber
454
478
  for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
455
- const block = await this.getBlock(BlockNumber(bn));
479
+ const block = await this.getBlock({
480
+ number: BlockNumber(bn)
481
+ });
456
482
  if (block === undefined) {
457
483
  this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
458
484
  continue;
@@ -487,20 +513,27 @@ export { TxReceipt } from '@aztec/stdlib/tx';
487
513
  return CheckpointNumber(latestCheckpointNumber);
488
514
  }
489
515
  async hasProposedCheckpoint() {
490
- const proposed = await this.#proposedCheckpoint.getAsync();
491
- return proposed !== undefined;
516
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
517
+ limit: 1
518
+ }));
519
+ return key !== undefined;
492
520
  }
493
- /** Deletes the proposed checkpoint from storage. */ async deleteProposedCheckpoint() {
494
- await this.#proposedCheckpoint.delete();
521
+ /** Deletes all pending proposed checkpoints from storage. */ async deleteProposedCheckpoints() {
522
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
523
+ await this.#proposedCheckpoints.delete(key);
524
+ }
495
525
  }
496
526
  /**
497
- * Promotes the proposed checkpoint singleton to a confirmed checkpoint entry.
498
- * This persists the checkpoint to the store, clears the proposed singleton, and updates the L1 sync point.
499
- * Should only be called after the checkpoint has been validated.
500
- * @param expectedArchiveRoot - The archive root to match against the proposed checkpoint, to guard against races.
501
- */ async promoteProposedToCheckpointed(l1, attestations, expectedArchiveRoot) {
527
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
528
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
529
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
530
+ * @param checkpointNumber - The checkpoint number to promote.
531
+ * @param l1 - L1 published data for the checkpoint.
532
+ * @param attestations - Committee attestations.
533
+ * @param expectedArchiveRoot - Archive root guard against races.
534
+ */ async promoteProposedToCheckpointed(checkpointNumber, l1, attestations, expectedArchiveRoot) {
502
535
  return await this.db.transactionAsync(async ()=>{
503
- const proposed = await this.getProposedCheckpointOnly();
536
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
504
537
  if (!proposed) {
505
538
  throw new NoProposedCheckpointToPromoteError();
506
539
  }
@@ -521,40 +554,76 @@ export { TxReceipt } from '@aztec/stdlib/tx';
521
554
  attestations: attestations.map((attestation)=>attestation.toBuffer()),
522
555
  checkpointNumber: proposed.checkpointNumber,
523
556
  startBlock: proposed.startBlock,
524
- blockCount: proposed.blockCount
557
+ blockCount: proposed.blockCount,
558
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
525
559
  });
526
560
  // Update the slot-to-checkpoint index
527
561
  await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
528
- // Clear the proposed checkpoint singleton
529
- await this.#proposedCheckpoint.delete();
562
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
563
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
564
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
565
+ // (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
566
+ await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
530
567
  // Update the last synced L1 block
531
- await this.#lastSynchedL1Block.set(l1.blockNumber);
568
+ await this.advanceSynchedL1BlockNumber(l1.blockNumber);
532
569
  });
533
570
  }
534
- /** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */ async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber) {
535
- const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
536
- if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
537
- await this.#proposedCheckpoint.delete();
571
+ /**
572
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
573
+ * No fallback to confirmed.
574
+ */ async getLastProposedCheckpoint() {
575
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
576
+ reverse: true,
577
+ limit: 1
578
+ }));
579
+ if (key === undefined) {
580
+ return undefined;
538
581
  }
582
+ const stored = await this.#proposedCheckpoints.getAsync(key);
583
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
539
584
  }
540
- /** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */ async getProposedCheckpointOnly() {
541
- const stored = await this.#proposedCheckpoint.getAsync();
542
- if (!stored) {
543
- return undefined;
585
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */ async getProposedCheckpointByNumber(n) {
586
+ const stored = await this.#proposedCheckpoints.getAsync(n);
587
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
588
+ }
589
+ /**
590
+ * Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
591
+ * Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
592
+ * in normal operation (bounded by the proposer pipelining window). Returns the first match.
593
+ */ async getProposedCheckpointBySlot(slot) {
594
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()){
595
+ const header = CheckpointHeader.fromBuffer(stored.header);
596
+ if (header.slotNumber === slot) {
597
+ return this.convertToProposedCheckpointData(stored);
598
+ }
599
+ }
600
+ return undefined;
601
+ }
602
+ /**
603
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
604
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
605
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
606
+ */ async evictProposedCheckpointsFrom(fromNumber) {
607
+ const keysToDelete = [];
608
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
609
+ if (key >= fromNumber) {
610
+ keysToDelete.push(key);
611
+ }
612
+ }
613
+ for (const key of keysToDelete){
614
+ await this.#proposedCheckpoints.delete(key);
544
615
  }
545
- return this.convertToProposedCheckpointData(stored);
546
616
  }
547
617
  /**
548
- * Gets the checkpoint at the proposed tip
549
- * - pending checkpoint if it exists
618
+ * Gets the checkpoint at the proposed tip:
619
+ * - latest pending checkpoint if any exist
550
620
  * - fallsback to latest confirmed checkpoint otherwise
551
- * @returns CommonCheckpointData
552
- */ async getProposedCheckpoint() {
553
- const stored = await this.#proposedCheckpoint.getAsync();
554
- if (!stored) {
621
+ */ async getLastCheckpoint() {
622
+ const latest = await this.getLastProposedCheckpoint();
623
+ if (!latest) {
555
624
  return this.getCheckpointData(await this.getLatestCheckpointNumber());
556
625
  }
557
- return this.convertToProposedCheckpointData(stored);
626
+ return latest;
558
627
  }
559
628
  convertToProposedCheckpointData(stored) {
560
629
  return {
@@ -572,7 +641,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
572
641
  * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
573
642
  * @returns CheckpointNumber
574
643
  */ async getProposedCheckpointNumber() {
575
- const proposed = await this.getProposedCheckpoint();
644
+ const proposed = await this.getLastCheckpoint();
576
645
  if (!proposed) {
577
646
  return await this.getLatestCheckpointNumber();
578
647
  }
@@ -582,134 +651,32 @@ export { TxReceipt } from '@aztec/stdlib/tx';
582
651
  * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
583
652
  * @returns BlockNumber
584
653
  */ async getProposedCheckpointL2BlockNumber() {
585
- const proposed = await this.getProposedCheckpoint();
654
+ const proposed = await this.getLastCheckpoint();
586
655
  if (!proposed) {
587
656
  return await this.getCheckpointedL2BlockNumber();
588
657
  }
589
658
  return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
590
659
  }
591
- async getCheckpointedBlock(number) {
592
- const blockStorage = await this.#blocks.getAsync(number);
593
- if (!blockStorage) {
594
- return undefined;
595
- }
596
- const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
597
- if (!checkpoint) {
598
- return undefined;
599
- }
600
- const block = await this.getBlockFromBlockStorage(number, blockStorage);
601
- if (!block) {
602
- return undefined;
603
- }
604
- return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
660
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */ async getCheckpointNumberBySlot(slot) {
661
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
662
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
605
663
  }
606
- /**
607
- * Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
608
- * @param start - Number of the first block to return (inclusive).
609
- * @param limit - The number of blocks to return.
610
- * @returns The requested L2 blocks
611
- */ async *getCheckpointedBlocks(start, limit) {
612
- const checkpointCache = new Map();
613
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
614
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
615
- if (block) {
616
- const checkpoint = checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ?? await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
617
- if (checkpoint) {
618
- checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
619
- const checkpointedBlock = new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
620
- yield checkpointedBlock;
621
- }
622
- }
623
- }
624
- }
625
- async getCheckpointedBlockByHash(blockHash) {
626
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
664
+ /** Gets a single L2 block matching the given resolved query. */ async getBlock(query) {
665
+ const blockNumber = await this.getBlockNumber(query);
627
666
  if (blockNumber === undefined) {
628
667
  return undefined;
629
668
  }
630
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
631
- }
632
- async getCheckpointedBlockByArchive(archive) {
633
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
634
- if (blockNumber === undefined) {
635
- return undefined;
636
- }
637
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
638
- }
639
- /**
640
- * Gets up to `limit` amount of L2 blocks starting from `from`.
641
- * @param start - Number of the first block to return (inclusive).
642
- * @param limit - The number of blocks to return.
643
- * @returns The requested L2 blocks
644
- */ async *getBlocks(start, limit) {
645
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
646
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
647
- if (block) {
648
- yield block;
649
- }
650
- }
651
- }
652
- /**
653
- * Gets block metadata (without tx data) by block number.
654
- * @param blockNumber - The number of the block to return.
655
- * @returns The requested block data.
656
- */ async getBlockData(blockNumber) {
657
669
  const blockStorage = await this.#blocks.getAsync(blockNumber);
658
- if (!blockStorage || !blockStorage.header) {
659
- return undefined;
660
- }
661
- return this.getBlockDataFromBlockStorage(blockStorage);
662
- }
663
- /**
664
- * Gets block metadata (without tx data) by archive root.
665
- * @param archive - The archive root of the block to return.
666
- * @returns The requested block data.
667
- */ async getBlockDataByArchive(archive) {
668
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
669
- if (blockNumber === undefined) {
670
+ if (!blockStorage) {
670
671
  return undefined;
671
672
  }
672
- return this.getBlockData(BlockNumber(blockNumber));
673
- }
674
- /**
675
- * Gets an L2 block.
676
- * @param blockNumber - The number of the block to return.
677
- * @returns The requested L2 block.
678
- */ async getBlock(blockNumber) {
679
- const blockStorage = await this.#blocks.getAsync(blockNumber);
680
- if (!blockStorage || !blockStorage.header) {
681
- return Promise.resolve(undefined);
682
- }
683
673
  return this.getBlockFromBlockStorage(blockNumber, blockStorage);
684
674
  }
685
- /**
686
- * Gets an L2 block by its hash.
687
- * @param blockHash - The hash of the block to return.
688
- * @returns The requested L2 block.
689
- */ async getBlockByHash(blockHash) {
690
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
691
- if (blockNumber === undefined) {
692
- return undefined;
693
- }
694
- return this.getBlock(BlockNumber(blockNumber));
695
- }
696
- /**
697
- * Gets an L2 block by its archive root.
698
- * @param archive - The archive root of the block to return.
699
- * @returns The requested L2 block.
700
- */ async getBlockByArchive(archive) {
701
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
702
- if (blockNumber === undefined) {
703
- return undefined;
704
- }
705
- return this.getBlock(BlockNumber(blockNumber));
675
+ /** Gets a collection of L2 blocks for a resolved range. */ getBlocks(query) {
676
+ return toArray(this.iterateBlocks(query));
706
677
  }
707
- /**
708
- * Gets a block header by its hash.
709
- * @param blockHash - The hash of the block to return.
710
- * @returns The requested block header.
711
- */ async getBlockHeaderByHash(blockHash) {
712
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
678
+ /** Gets single block metadata matching the given resolved query. */ async getBlockData(query) {
679
+ const blockNumber = await this.getBlockNumber(query);
713
680
  if (blockNumber === undefined) {
714
681
  return undefined;
715
682
  }
@@ -717,35 +684,30 @@ export { TxReceipt } from '@aztec/stdlib/tx';
717
684
  if (!blockStorage || !blockStorage.header) {
718
685
  return undefined;
719
686
  }
720
- return BlockHeader.fromBuffer(blockStorage.header);
687
+ return this.getBlockDataFromBlockStorage(blockStorage);
721
688
  }
722
- /**
723
- * Gets a block header by its archive root.
724
- * @param archive - The archive root of the block to return.
725
- * @returns The requested block header.
726
- */ async getBlockHeaderByArchive(archive) {
727
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
728
- if (blockNumber === undefined) {
729
- return undefined;
730
- }
731
- const blockStorage = await this.#blocks.getAsync(blockNumber);
732
- if (!blockStorage || !blockStorage.header) {
733
- return undefined;
689
+ /** Gets a collection of block metadata entries for a resolved range. */ getBlocksData(query) {
690
+ return toArray(this.iterateBlocksData(query));
691
+ }
692
+ /** Async iterator over L2 blocks for a resolved range. */ async *iterateBlocks(query) {
693
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
694
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
695
+ if (cap !== undefined && blockNumber > cap) {
696
+ break;
697
+ }
698
+ const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
699
+ if (block) {
700
+ yield block;
701
+ }
734
702
  }
735
- return BlockHeader.fromBuffer(blockStorage.header);
736
703
  }
737
- /**
738
- * Gets the headers for a sequence of L2 blocks.
739
- * @param start - Number of the first block to return (inclusive).
740
- * @param limit - The number of blocks to return.
741
- * @returns The requested L2 block headers
742
- */ async *getBlockHeaders(start, limit) {
743
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
744
- const header = BlockHeader.fromBuffer(blockStorage.header);
745
- if (header.getBlockNumber() !== blockNumber) {
746
- throw new Error(`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`);
704
+ /** Async iterator over block metadata for a resolved range. */ async *iterateBlocksData(query) {
705
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
706
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
707
+ if (cap !== undefined && blockNumber > cap) {
708
+ break;
747
709
  }
748
- yield header;
710
+ yield this.getBlockDataFromBlockStorage(blockStorage);
749
711
  }
750
712
  }
751
713
  async *getBlockStorages(start, limit) {
@@ -761,6 +723,22 @@ export { TxReceipt } from '@aztec/stdlib/tx';
761
723
  ];
762
724
  }
763
725
  }
726
+ /** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */ async getBlockNumber(query) {
727
+ let blockNumber;
728
+ if ('number' in query) {
729
+ blockNumber = query.number;
730
+ } else if ('hash' in query) {
731
+ const n = await this.#blockHashIndex.getAsync(query.hash.toString());
732
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
733
+ } else {
734
+ const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
735
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
736
+ }
737
+ if (blockNumber === undefined) {
738
+ return undefined;
739
+ }
740
+ return blockNumber;
741
+ }
764
742
  getBlockDataFromBlockStorage(blockStorage) {
765
743
  return {
766
744
  header: BlockHeader.fromBuffer(blockStorage.header),
@@ -809,36 +787,6 @@ export { TxReceipt } from '@aztec/stdlib/tx';
809
787
  return deserializeIndexedTxEffect(buffer);
810
788
  }
811
789
  /**
812
- * Gets a receipt of a settled tx.
813
- * @param txHash - The hash of a tx we try to get the receipt for.
814
- * @returns The requested tx receipt (or undefined if not found).
815
- */ async getSettledTxReceipt(txHash, l1Constants) {
816
- const txEffect = await this.getTxEffect(txHash);
817
- if (!txEffect) {
818
- return undefined;
819
- }
820
- const blockNumber = BlockNumber(txEffect.l2BlockNumber);
821
- // Use existing archiver methods to determine finalization level
822
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
823
- this.getProvenBlockNumber(),
824
- this.getCheckpointedL2BlockNumber(),
825
- this.getFinalizedL2BlockNumber(),
826
- this.getBlockData(blockNumber)
827
- ]);
828
- let status;
829
- if (blockNumber <= finalizedBlockNumber) {
830
- status = TxStatus.FINALIZED;
831
- } else if (blockNumber <= provenBlockNumber) {
832
- status = TxStatus.PROVEN;
833
- } else if (blockNumber <= checkpointedBlockNumber) {
834
- status = TxStatus.CHECKPOINTED;
835
- } else {
836
- status = TxStatus.PROPOSED;
837
- }
838
- const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
839
- return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
840
- }
841
- /**
842
790
  * Looks up which block included the requested tx effect.
843
791
  * @param txHash - The txHash of the tx.
844
792
  * @returns The block number and index of the tx.
@@ -847,13 +795,50 @@ export { TxReceipt } from '@aztec/stdlib/tx';
847
795
  if (!txEffect) {
848
796
  return undefined;
849
797
  }
850
- const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
798
+ // Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
799
+ // large tail (the full TxEffect with logs etc.) is irrelevant here.
800
+ const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
801
+ const l2BlockNumber = view.readUInt32BE(32);
802
+ const txIndexInBlock = view.readUInt32BE(36);
851
803
  return [
852
804
  l2BlockNumber,
853
805
  txIndexInBlock
854
806
  ];
855
807
  }
856
808
  /**
809
+ * Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
810
+ * txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
811
+ * unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
812
+ * attach effect data on demand without paying for a full {@link TxEffect} deserialization.
813
+ *
814
+ * The on-disk `IndexedTxEffect` layout starts with a fixed-length header
815
+ * (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + slotNumber(4) + revertCode(1) + txHash(32) +
816
+ * transactionFee(32)` = 109 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr`
817
+ * vectors). We
818
+ * skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
819
+ * `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
820
+ */ getNoteHashesAndNullifiers(txHashes) {
821
+ return Promise.all(txHashes.map(async (txHash)=>{
822
+ const buffer = await this.#txEffects.getAsync(txHash.toString());
823
+ if (!buffer) {
824
+ return [
825
+ [],
826
+ []
827
+ ];
828
+ }
829
+ const reader = BufferReader.asReader(buffer);
830
+ // Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + slotNumber + revertCode +
831
+ // txHash + transactionFee.
832
+ reader.readBytes(32 + 4 + 4 + 4 + 1 + 32 + 32);
833
+ const noteHashes = reader.readVectorUint8Prefix(Fr);
834
+ const nullifiers = reader.readVectorUint8Prefix(Fr);
835
+ return [
836
+ noteHashes,
837
+ nullifiers
838
+ ];
839
+ }));
840
+ }
841
+ /**
857
842
  * Looks up which block deployed a particular contract.
858
843
  * @param contractAddress - The address of the contract to look up.
859
844
  * @returns The block number and index of the contract.
@@ -879,6 +864,152 @@ export { TxReceipt } from '@aztec/stdlib/tx';
879
864
  return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
880
865
  }
881
866
  /**
867
+ * Resolves all five L2 chain tips (proposed, proposedCheckpoint, checkpointed, proven, finalized)
868
+ * in a single read-only transaction so the snapshot is internally consistent. Each underlying
869
+ * record is read at most once: latest block, latest confirmed checkpoint, and latest pending
870
+ * checkpoint are each loaded directly (no separate "find the number, then look up data" hop),
871
+ * the proven/finalized checkpoint singletons are read once and their storage entries are
872
+ * reused if they coincide with the latest checkpoint, and per-tip block hashes are deduped
873
+ * when two tips land on the same block (e.g. finalized == proven, or proposedCheckpoint falls
874
+ * back to checkpointed when no pending checkpoint exists).
875
+ *
876
+ * The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
877
+ * block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
878
+ * `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
879
+ *
880
+ * @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
881
+ * a tip is still at genesis).
882
+ */ async getL2TipsData(genesisBlockHash) {
883
+ return await this.db.transactionAsync(async ()=>{
884
+ // Define genesis tips
885
+ const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
886
+ const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
887
+ const genesisBlockId = {
888
+ number: genesisBlockNumber,
889
+ hash: genesisBlockHash.toString()
890
+ };
891
+ const genesisCheckpointId = {
892
+ number: genesisCheckpointNumber,
893
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
894
+ };
895
+ const genesisTip = {
896
+ block: genesisBlockId,
897
+ checkpoint: genesisCheckpointId
898
+ };
899
+ // Load latest block and checkpoint entries
900
+ const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({
901
+ reverse: true,
902
+ limit: 1
903
+ }));
904
+ const [proposedCheckpointEntry] = await toArray(this.#proposedCheckpoints.entriesAsync({
905
+ reverse: true,
906
+ limit: 1
907
+ }));
908
+ const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({
909
+ reverse: true,
910
+ limit: 1
911
+ }));
912
+ const latestCheckpointNumber = latestCheckpointEntry ? CheckpointNumber(latestCheckpointEntry[0]) : genesisCheckpointNumber;
913
+ // Load proven and finalized checkpoint number pointers
914
+ const [provenRaw, finalizedRaw] = await Promise.all([
915
+ this.#lastProvenCheckpoint.getAsync(),
916
+ this.#lastFinalizedCheckpoint.getAsync()
917
+ ]);
918
+ // Clamp to enforce finalized <= proven <= checkpointed.
919
+ const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
920
+ const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
921
+ // Avoid loading the same checkpoint more than once
922
+ const checkpointStorageCache = new Map();
923
+ if (latestCheckpointEntry) {
924
+ checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
925
+ }
926
+ const loadCheckpointStorage = async (n)=>{
927
+ if (n === 0) {
928
+ return undefined;
929
+ }
930
+ if (!checkpointStorageCache.has(n)) {
931
+ const checkpointStorage = await this.#checkpoints.getAsync(n);
932
+ if (!checkpointStorage) {
933
+ throw new CheckpointNotFoundError(n);
934
+ }
935
+ checkpointStorageCache.set(n, checkpointStorage);
936
+ }
937
+ return checkpointStorageCache.get(n);
938
+ };
939
+ // Load proven and finalized checkpoint storage entries
940
+ const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
941
+ const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
942
+ // Avoid loading the same block hash multiple times when tips land on the same block
943
+ const blockHashCache = new Map();
944
+ blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
945
+ if (latestBlockEntry) {
946
+ blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
947
+ }
948
+ const loadBlockHash = async (n)=>{
949
+ if (!blockHashCache.has(n)) {
950
+ const blockStorage = await this.#blocks.getAsync(n);
951
+ if (!blockStorage) {
952
+ throw new BlockNotFoundError(n);
953
+ }
954
+ const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
955
+ blockHashCache.set(n, blockHash);
956
+ }
957
+ return blockHashCache.get(n);
958
+ };
959
+ // Build proposed chain tip (this one has block only, no checkpoint)
960
+ const proposedBlockId = latestBlockEntry === undefined ? genesisBlockId : {
961
+ number: BlockNumber(latestBlockEntry[0]),
962
+ hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString()
963
+ };
964
+ // Build other tips from checkpoint data, reading corresponding block data from the cache
965
+ const buildTipFromCheckpoint = async (stored)=>{
966
+ if (!stored) {
967
+ return genesisTip;
968
+ }
969
+ const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
970
+ const blockHash = await loadBlockHash(blockNumber);
971
+ const header = CheckpointHeader.fromBuffer(stored.header);
972
+ return {
973
+ block: {
974
+ number: blockNumber,
975
+ hash: blockHash
976
+ },
977
+ checkpoint: {
978
+ number: CheckpointNumber(stored.checkpointNumber),
979
+ hash: header.hash().toString()
980
+ }
981
+ };
982
+ };
983
+ const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
984
+ const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
985
+ const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
986
+ // Proposed checkpoint falls back to the checkpoint tip if it's not set. And if local storage is
987
+ // inconsistent and the proposed checkpoint is behind the checkpointed tip, we patch that and
988
+ // report the checkpointed tip as the proposed checkpoint to maintain the invariant.
989
+ const proposedCheckpointTip = proposedCheckpointEntry === undefined || proposedCheckpointEntry[0] <= latestCheckpointNumber ? checkpointedTip : await buildTipFromCheckpoint(proposedCheckpointEntry[1]);
990
+ // A checkpointed block past the latest stored block would mean a checkpoint
991
+ // references blocks that aren't in blocks.
992
+ if (proposedBlockId.number < checkpointedTip.block.number) {
993
+ throw new Error(`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`);
994
+ }
995
+ // Assert that checkpoint numbers are increasing
996
+ if (finalizedTip.checkpoint.number > provenTip.checkpoint.number || provenTip.checkpoint.number > checkpointedTip.checkpoint.number || checkpointedTip.checkpoint.number > proposedCheckpointTip.checkpoint.number) {
997
+ throw new Error(`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number} proposed=${proposedCheckpointTip.checkpoint.number}`);
998
+ }
999
+ // Assert block numbers are increasing
1000
+ if (finalizedTip.block.number > provenTip.block.number || provenTip.block.number > checkpointedTip.block.number || checkpointedTip.block.number > proposedCheckpointTip.block.number || proposedCheckpointTip.block.number > proposedBlockId.number) {
1001
+ throw new Error(`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposedCheckpoint=${proposedCheckpointTip.block.number} proposed=${proposedBlockId.number}`);
1002
+ }
1003
+ return {
1004
+ proposed: proposedBlockId,
1005
+ proposedCheckpoint: proposedCheckpointTip,
1006
+ checkpointed: checkpointedTip,
1007
+ proven: provenTip,
1008
+ finalized: finalizedTip
1009
+ };
1010
+ });
1011
+ }
1012
+ /**
882
1013
  * Gets the most recent L1 block processed.
883
1014
  * @returns The L1 block that published the latest L2 block
884
1015
  */ getSynchedL1BlockNumber() {
@@ -887,22 +1018,29 @@ export { TxReceipt } from '@aztec/stdlib/tx';
887
1018
  setSynchedL1BlockNumber(l1BlockNumber) {
888
1019
  return this.#lastSynchedL1Block.set(l1BlockNumber);
889
1020
  }
890
- /** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
891
- * Computes archive and checkpointOutHash from the stored blocks. */ async setProposedCheckpoint(proposed) {
1021
+ /**
1022
+ * Adds a proposed checkpoint to the pending queue.
1023
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1024
+ * confirmed and the highest pending checkpoint number.
1025
+ * Computes archive and checkpointOutHash from the stored blocks.
1026
+ */ async addProposedCheckpoint(proposed) {
892
1027
  return await this.db.transactionAsync(async ()=>{
893
- const current = await this.getProposedCheckpointNumber();
894
- if (proposed.checkpointNumber <= current) {
895
- throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
896
- }
897
1028
  const confirmed = await this.getLatestCheckpointNumber();
898
- if (proposed.checkpointNumber !== confirmed + 1) {
899
- throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
1029
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
1030
+ reverse: true,
1031
+ limit: 1
1032
+ }));
1033
+ const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
1034
+ if (proposed.checkpointNumber !== latestTip + 1) {
1035
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
900
1036
  }
901
- // Ensure the previous checkpoint + blocks exist
1037
+ // Ensure the predecessor block (from pending or confirmed chain) exists
902
1038
  const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
903
1039
  const blocks = [];
904
1040
  for(let i = 0; i < proposed.blockCount; i++){
905
- const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1041
+ const block = await this.getBlock({
1042
+ number: BlockNumber(proposed.startBlock + i)
1043
+ });
906
1044
  if (!block) {
907
1045
  throw new BlockNotFoundError(proposed.startBlock + i);
908
1046
  }
@@ -911,7 +1049,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
911
1049
  this.validateCheckpointBlocks(blocks, previousBlock);
912
1050
  const archive = blocks[blocks.length - 1].archive;
913
1051
  const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
914
- await this.#proposedCheckpoint.set({
1052
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
915
1053
  header: proposed.header.toBuffer(),
916
1054
  archive: archive.toBuffer(),
917
1055
  checkpointOutHash: checkpointOutHash.toBuffer(),
@@ -924,22 +1062,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
924
1062
  });
925
1063
  }
926
1064
  async getProvenCheckpointNumber() {
927
- const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
928
- this.getLatestCheckpointNumber(),
929
- this.#lastProvenCheckpoint.getAsync()
930
- ]);
931
- return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
1065
+ return await this.db.transactionAsync(async ()=>{
1066
+ const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
1067
+ this.getLatestCheckpointNumber(),
1068
+ this.#lastProvenCheckpoint.getAsync()
1069
+ ]);
1070
+ return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
1071
+ });
932
1072
  }
933
1073
  async setProvenCheckpointNumber(checkpointNumber) {
934
1074
  const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
935
1075
  return result;
936
1076
  }
937
1077
  async getFinalizedCheckpointNumber() {
938
- const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
939
- this.getLatestCheckpointNumber(),
940
- this.#lastFinalizedCheckpoint.getAsync()
941
- ]);
942
- return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1078
+ return await this.db.transactionAsync(async ()=>{
1079
+ const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1080
+ this.getProvenCheckpointNumber(),
1081
+ this.#lastFinalizedCheckpoint.getAsync()
1082
+ ]);
1083
+ return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber ? provenCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1084
+ });
943
1085
  }
944
1086
  setFinalizedCheckpointNumber(checkpointNumber) {
945
1087
  return this.#lastFinalizedCheckpoint.set(checkpointNumber);
@@ -977,4 +1119,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
977
1119
  await this.#pendingChainValidationStatus.delete();
978
1120
  }
979
1121
  }
1122
+ /** Records a rejected-checkpoint entry, keyed by its own archive root. */ async addRejectedCheckpoint(entry) {
1123
+ const archiveRootHex = entry.archiveRoot.toString();
1124
+ await this.#rejectedCheckpoints.set(archiveRootHex, {
1125
+ checkpointNumber: entry.checkpointNumber,
1126
+ archiveRoot: entry.archiveRoot.toBuffer(),
1127
+ parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
1128
+ slotNumber: entry.slotNumber,
1129
+ l1: entry.l1.toBuffer(),
1130
+ reason: entry.reason
1131
+ });
1132
+ await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
1133
+ await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
1134
+ }
1135
+ /** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */ async getRejectedCheckpointByArchiveRoot(archiveRoot) {
1136
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
1137
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1138
+ }
1139
+ /** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */ async getRejectedCheckpointByNumber(checkpointNumber) {
1140
+ const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
1141
+ if (archiveRootHex === undefined) {
1142
+ return undefined;
1143
+ }
1144
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1145
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1146
+ }
1147
+ /** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */ async getLatestRejectedCheckpointNumber() {
1148
+ const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({
1149
+ reverse: true,
1150
+ limit: 1
1151
+ }));
1152
+ return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
1153
+ }
1154
+ /** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */ async removeRejectedCheckpointByArchiveRoot(archiveRoot) {
1155
+ const archiveRootHex = archiveRoot.toString();
1156
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1157
+ await this.#rejectedCheckpoints.delete(archiveRootHex);
1158
+ if (stored) {
1159
+ // Only clear the by-number index if it still points at this archive root, so a distinct
1160
+ // entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
1161
+ const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
1162
+ if (indexed === archiveRootHex) {
1163
+ await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
1164
+ }
1165
+ }
1166
+ }
1167
+ /**
1168
+ * Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
1169
+ * greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
1170
+ * sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
1171
+ * advance followed by a valid-checkpoint commit landing at an earlier L1 block).
1172
+ */ async advanceSynchedL1BlockNumber(l1BlockNumber) {
1173
+ const current = await this.#lastSynchedL1Block.getAsync();
1174
+ if (current === undefined || l1BlockNumber > current) {
1175
+ await this.#lastSynchedL1Block.set(l1BlockNumber);
1176
+ }
1177
+ }
1178
+ rejectedCheckpointFromStorage(stored) {
1179
+ return {
1180
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
1181
+ archiveRoot: Fr.fromBuffer(stored.archiveRoot),
1182
+ parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
1183
+ slotNumber: SlotNumber(stored.slotNumber),
1184
+ l1: L1PublishedData.fromBuffer(stored.l1),
1185
+ reason: stored.reason
1186
+ };
1187
+ }
980
1188
  }