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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/README.md +7 -5
  2. package/dest/archiver.d.ts +70 -15
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +274 -71
  5. package/dest/config.d.ts +7 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +39 -16
  8. package/dest/errors.d.ts +35 -13
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +54 -16
  11. package/dest/factory.d.ts +11 -6
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +35 -21
  14. package/dest/index.d.ts +11 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +10 -2
  17. package/dest/l1/calldata_retriever.d.ts +9 -1
  18. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  19. package/dest/l1/calldata_retriever.js +20 -10
  20. package/dest/l1/data_retrieval.d.ts +24 -10
  21. package/dest/l1/data_retrieval.d.ts.map +1 -1
  22. package/dest/l1/data_retrieval.js +13 -19
  23. package/dest/l1/trace_tx.d.ts +12 -66
  24. package/dest/l1/trace_tx.d.ts.map +1 -1
  25. package/dest/l1/validate_historical_logs.d.ts +23 -0
  26. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  27. package/dest/l1/validate_historical_logs.js +108 -0
  28. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  29. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  30. package/dest/modules/contract_data_source_adapter.js +32 -0
  31. package/dest/modules/data_source_base.d.ts +71 -50
  32. package/dest/modules/data_source_base.d.ts.map +1 -1
  33. package/dest/modules/data_source_base.js +250 -150
  34. package/dest/modules/data_store_updater.d.ts +28 -9
  35. package/dest/modules/data_store_updater.d.ts.map +1 -1
  36. package/dest/modules/data_store_updater.js +121 -61
  37. package/dest/modules/instrumentation.d.ts +15 -2
  38. package/dest/modules/instrumentation.d.ts.map +1 -1
  39. package/dest/modules/instrumentation.js +48 -9
  40. package/dest/modules/l1_synchronizer.d.ts +11 -7
  41. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  42. package/dest/modules/l1_synchronizer.js +347 -118
  43. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  44. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  45. package/dest/modules/outbox_trees_resolver.js +162 -0
  46. package/dest/modules/validation.d.ts +30 -7
  47. package/dest/modules/validation.d.ts.map +1 -1
  48. package/dest/modules/validation.js +33 -15
  49. package/dest/store/block_store.d.ts +167 -94
  50. package/dest/store/block_store.d.ts.map +1 -1
  51. package/dest/store/block_store.js +524 -260
  52. package/dest/store/contract_class_store.d.ts +17 -3
  53. package/dest/store/contract_class_store.d.ts.map +1 -1
  54. package/dest/store/contract_class_store.js +36 -2
  55. package/dest/store/contract_instance_store.d.ts +28 -1
  56. package/dest/store/contract_instance_store.d.ts.map +1 -1
  57. package/dest/store/contract_instance_store.js +49 -1
  58. package/dest/store/data_stores.d.ts +68 -0
  59. package/dest/store/data_stores.d.ts.map +1 -0
  60. package/dest/store/data_stores.js +54 -0
  61. package/dest/store/function_names_cache.d.ts +17 -0
  62. package/dest/store/function_names_cache.d.ts.map +1 -0
  63. package/dest/store/function_names_cache.js +30 -0
  64. package/dest/store/l2_tips_cache.d.ts +13 -8
  65. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  66. package/dest/store/l2_tips_cache.js +13 -96
  67. package/dest/store/log_store.d.ts +42 -40
  68. package/dest/store/log_store.d.ts.map +1 -1
  69. package/dest/store/log_store.js +257 -480
  70. package/dest/store/log_store_codec.d.ts +78 -0
  71. package/dest/store/log_store_codec.d.ts.map +1 -0
  72. package/dest/store/log_store_codec.js +110 -0
  73. package/dest/store/message_store.d.ts +9 -3
  74. package/dest/store/message_store.d.ts.map +1 -1
  75. package/dest/store/message_store.js +31 -1
  76. package/dest/test/fake_l1_state.d.ts +7 -3
  77. package/dest/test/fake_l1_state.d.ts.map +1 -1
  78. package/dest/test/fake_l1_state.js +45 -18
  79. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  80. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  81. package/dest/test/mock_l1_to_l2_message_source.js +1 -2
  82. package/dest/test/mock_l2_block_source.d.ts +47 -54
  83. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  84. package/dest/test/mock_l2_block_source.js +229 -205
  85. package/dest/test/mock_structs.d.ts +1 -1
  86. package/dest/test/mock_structs.d.ts.map +1 -1
  87. package/dest/test/mock_structs.js +2 -2
  88. package/dest/test/noop_l1_archiver.d.ts +14 -6
  89. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  90. package/dest/test/noop_l1_archiver.js +26 -10
  91. package/package.json +14 -14
  92. package/src/archiver.ts +355 -77
  93. package/src/config.ts +43 -20
  94. package/src/errors.ts +80 -23
  95. package/src/factory.ts +46 -19
  96. package/src/index.ts +18 -2
  97. package/src/l1/calldata_retriever.ts +27 -10
  98. package/src/l1/data_retrieval.ts +38 -36
  99. package/src/l1/validate_historical_logs.ts +140 -0
  100. package/src/modules/contract_data_source_adapter.ts +46 -0
  101. package/src/modules/data_source_base.ts +319 -200
  102. package/src/modules/data_store_updater.ts +146 -64
  103. package/src/modules/instrumentation.ts +43 -10
  104. package/src/modules/l1_synchronizer.ts +499 -136
  105. package/src/modules/outbox_trees_resolver.ts +199 -0
  106. package/src/modules/validation.ts +78 -21
  107. package/src/store/block_store.ts +627 -321
  108. package/src/store/contract_class_store.ts +47 -3
  109. package/src/store/contract_instance_store.ts +61 -1
  110. package/src/store/data_stores.ts +104 -0
  111. package/src/store/function_names_cache.ts +37 -0
  112. package/src/store/l2_tips_cache.ts +16 -109
  113. package/src/store/log_store.ts +296 -652
  114. package/src/store/log_store_codec.ts +143 -0
  115. package/src/store/message_store.ts +35 -2
  116. package/src/test/fake_l1_state.ts +50 -27
  117. package/src/test/mock_l1_to_l2_message_source.ts +0 -1
  118. package/src/test/mock_l2_block_source.ts +276 -258
  119. package/src/test/mock_structs.ts +5 -2
  120. package/src/test/noop_l1_archiver.ts +54 -11
  121. package/dest/store/kv_archiver_store.d.ts +0 -377
  122. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  123. package/dest/store/kv_archiver_store.js +0 -494
  124. package/src/store/kv_archiver_store.ts +0 -713
@@ -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 { 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, ProposedCheckpointNotSequentialError, 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
@@ -131,11 +135,14 @@ export { TxReceipt } from '@aztec/stdlib/tx';
131
135
  }
132
136
  /**
133
137
  * Append new checkpoints to the store's list.
138
+ * Checkpoints at the start of the batch that are already stored (e.g. re-included by an L1 reorg)
139
+ * are accepted if their archive root matches: their L1 metadata is updated but they are not
140
+ * re-inserted, and they are excluded from the returned array.
134
141
  * @param checkpoints - The L2 checkpoints to be added to the store.
135
- * @returns True if the operation is successful.
142
+ * @returns The checkpoints that were actually inserted (excluding already-stored ones).
136
143
  */ async addCheckpoints(checkpoints, opts = {}) {
137
144
  if (checkpoints.length === 0) {
138
- return true;
145
+ return [];
139
146
  }
140
147
  return await this.db.transactionAsync(async ()=>{
141
148
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
@@ -146,7 +153,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
146
153
  if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
147
154
  checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
148
155
  if (checkpoints.length === 0) {
149
- return true;
156
+ return [];
150
157
  }
151
158
  // Re-check sequentiality after skipping
152
159
  const newFirstNumber = checkpoints[0].checkpoint.number;
@@ -181,16 +188,19 @@ export { TxReceipt } from '@aztec/stdlib/tx';
181
188
  attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
182
189
  checkpointNumber: checkpoint.checkpoint.number,
183
190
  startBlock: checkpoint.checkpoint.blocks[0].number,
184
- blockCount: checkpoint.checkpoint.blocks.length
191
+ blockCount: checkpoint.checkpoint.blocks.length,
192
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
185
193
  });
186
194
  // Update slot-to-checkpoint index
187
195
  await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
196
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
197
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
198
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
199
+ // (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
200
+ await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
188
201
  }
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);
193
- return true;
202
+ await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
203
+ return checkpoints;
194
204
  });
195
205
  }
196
206
  /**
@@ -218,10 +228,11 @@ export { TxReceipt } from '@aztec/stdlib/tx';
218
228
  attestations: incoming.attestations.map((a)=>a.toBuffer()),
219
229
  checkpointNumber: incoming.checkpoint.number,
220
230
  startBlock: incoming.checkpoint.blocks[0].number,
221
- blockCount: incoming.checkpoint.blocks.length
231
+ blockCount: incoming.checkpoint.blocks.length,
232
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString()
222
233
  });
223
234
  // Update the sync point to reflect the new L1 block
224
- await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
235
+ await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
225
236
  }
226
237
  return checkpoints.slice(i);
227
238
  }
@@ -233,12 +244,15 @@ export { TxReceipt } from '@aztec/stdlib/tx';
233
244
  if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
234
245
  return undefined;
235
246
  }
236
- const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
237
- if (previousCheckpointData === undefined) {
247
+ // Check across both proposed and mined checkpoints
248
+ const predecessor = await this.getProposedCheckpointByNumber(previousCheckpointNumber) ?? await this.getCheckpointData(previousCheckpointNumber);
249
+ if (!predecessor) {
238
250
  throw new CheckpointNotFoundError(previousCheckpointNumber);
239
251
  }
240
- const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
241
- const previousBlock = await this.getBlock(previousBlockNumber);
252
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
253
+ const previousBlock = await this.getBlock({
254
+ number: previousBlockNumber
255
+ });
242
256
  if (previousBlock === undefined) {
243
257
  throw new BlockNotFoundError(previousBlockNumber);
244
258
  }
@@ -247,7 +261,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
247
261
  /**
248
262
  * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
249
263
  * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
250
- * proposed checkpoints (setProposedCheckpoint).
264
+ * proposed checkpoints (addProposedCheckpoint).
251
265
  */ validateCheckpointBlocks(blocks, previousBlock) {
252
266
  for (const block of blocks){
253
267
  if (previousBlock) {
@@ -289,7 +303,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
289
303
  data: block.body.txEffects[i],
290
304
  l2BlockNumber: block.number,
291
305
  l2BlockHash: blockHash,
292
- txIndexInBlock: i
306
+ txIndexInBlock: i,
307
+ slotNumber: block.header.globalVariables.slotNumber
293
308
  };
294
309
  await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
295
310
  }
@@ -352,11 +367,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
352
367
  await this.#checkpoints.delete(c);
353
368
  this.#log.debug(`Removed checkpoint ${c}`);
354
369
  }
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
- }
370
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
371
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
360
372
  return {
361
373
  blocksRemoved
362
374
  };
@@ -380,6 +392,31 @@ export { TxReceipt } from '@aztec/stdlib/tx';
380
392
  }
381
393
  return checkpoints;
382
394
  }
395
+ /**
396
+ * Returns up to `limit` checkpoints anchored at `fromSlot`, ordered nearest-first, walking the slot index.
397
+ * With `reverse`, takes the checkpoints at or before `fromSlot` (descending by slot); otherwise the
398
+ * checkpoints at or after it (ascending). `limit: 1, reverse: true` yields the latest checkpoint at or
399
+ * before the slot in a single range scan.
400
+ */ async getCheckpointsBySlot(fromSlot, limit, reverse) {
401
+ // The KV range bounds are direction-dependent: forward is [start, end), reverse is (start, end], so a
402
+ // reverse scan uses `end: fromSlot` (inclusive) with no +1 to include the checkpoint at fromSlot itself.
403
+ const range = reverse ? {
404
+ end: fromSlot,
405
+ reverse: true,
406
+ limit
407
+ } : {
408
+ start: fromSlot,
409
+ limit
410
+ };
411
+ const result = [];
412
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync(range)){
413
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
414
+ if (checkpointStorage) {
415
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
416
+ }
417
+ }
418
+ return result;
419
+ }
383
420
  /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */ async getCheckpointDataForSlotRange(startSlot, endSlot) {
384
421
  const result = [];
385
422
  for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
@@ -393,6 +430,20 @@ export { TxReceipt } from '@aztec/stdlib/tx';
393
430
  }
394
431
  return result;
395
432
  }
433
+ /**
434
+ * Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
435
+ * Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
436
+ * checkpoints fall in the range and will fetch full data for at most a few of them.
437
+ */ async getCheckpointNumbersForSlotRange(startSlot, endSlot) {
438
+ const result = [];
439
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
440
+ start: startSlot,
441
+ end: endSlot + 1
442
+ })){
443
+ result.push(CheckpointNumber(checkpointNumber));
444
+ }
445
+ return result;
446
+ }
396
447
  checkpointDataFromCheckpointStorage(checkpointStorage) {
397
448
  return {
398
449
  header: CheckpointHeader.fromBuffer(checkpointStorage.header),
@@ -401,6 +452,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
401
452
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
402
453
  startBlock: BlockNumber(checkpointStorage.startBlock),
403
454
  blockCount: checkpointStorage.blockCount,
455
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
404
456
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
405
457
  attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
406
458
  };
@@ -452,7 +504,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
452
504
  const latestBlockNumber = await this.getLatestL2BlockNumber();
453
505
  // Iterate from blockNumber + 1 to latestBlockNumber
454
506
  for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
455
- const block = await this.getBlock(BlockNumber(bn));
507
+ const block = await this.getBlock({
508
+ number: BlockNumber(bn)
509
+ });
456
510
  if (block === undefined) {
457
511
  this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
458
512
  continue;
@@ -487,36 +541,117 @@ export { TxReceipt } from '@aztec/stdlib/tx';
487
541
  return CheckpointNumber(latestCheckpointNumber);
488
542
  }
489
543
  async hasProposedCheckpoint() {
490
- const proposed = await this.#proposedCheckpoint.getAsync();
491
- return proposed !== undefined;
492
- }
493
- /** Deletes the proposed checkpoint from storage. */ async deleteProposedCheckpoint() {
494
- await this.#proposedCheckpoint.delete();
544
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
545
+ limit: 1
546
+ }));
547
+ return key !== undefined;
495
548
  }
496
- /** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */ async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber) {
497
- const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
498
- if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
499
- await this.#proposedCheckpoint.delete();
549
+ /** Deletes all pending proposed checkpoints from storage. */ async deleteProposedCheckpoints() {
550
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
551
+ await this.#proposedCheckpoints.delete(key);
500
552
  }
501
553
  }
502
- /** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */ async getProposedCheckpointOnly() {
503
- const stored = await this.#proposedCheckpoint.getAsync();
504
- if (!stored) {
554
+ /**
555
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
556
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
557
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
558
+ * @param checkpointNumber - The checkpoint number to promote.
559
+ * @param l1 - L1 published data for the checkpoint.
560
+ * @param attestations - Committee attestations.
561
+ * @param expectedArchiveRoot - Archive root guard against races.
562
+ */ async promoteProposedToCheckpointed(checkpointNumber, l1, attestations, expectedArchiveRoot) {
563
+ return await this.db.transactionAsync(async ()=>{
564
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
565
+ if (!proposed) {
566
+ throw new NoProposedCheckpointToPromoteError();
567
+ }
568
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
569
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
570
+ }
571
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
572
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
573
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
574
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
575
+ }
576
+ // Write the checkpoint entry
577
+ await this.#checkpoints.set(proposed.checkpointNumber, {
578
+ header: proposed.header.toBuffer(),
579
+ archive: proposed.archive.toBuffer(),
580
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
581
+ l1: l1.toBuffer(),
582
+ attestations: attestations.map((attestation)=>attestation.toBuffer()),
583
+ checkpointNumber: proposed.checkpointNumber,
584
+ startBlock: proposed.startBlock,
585
+ blockCount: proposed.blockCount,
586
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
587
+ });
588
+ // Update the slot-to-checkpoint index
589
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
590
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
591
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
592
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
593
+ // (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
594
+ await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
595
+ // Update the last synced L1 block
596
+ await this.advanceSynchedL1BlockNumber(l1.blockNumber);
597
+ });
598
+ }
599
+ /**
600
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
601
+ * No fallback to confirmed.
602
+ */ async getLastProposedCheckpoint() {
603
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
604
+ reverse: true,
605
+ limit: 1
606
+ }));
607
+ if (key === undefined) {
505
608
  return undefined;
506
609
  }
507
- return this.convertToProposedCheckpointData(stored);
610
+ const stored = await this.#proposedCheckpoints.getAsync(key);
611
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
612
+ }
613
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */ async getProposedCheckpointByNumber(n) {
614
+ const stored = await this.#proposedCheckpoints.getAsync(n);
615
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
616
+ }
617
+ /**
618
+ * Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
619
+ * Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
620
+ * in normal operation (bounded by the proposer pipelining window). Returns the first match.
621
+ */ async getProposedCheckpointBySlot(slot) {
622
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()){
623
+ const header = CheckpointHeader.fromBuffer(stored.header);
624
+ if (header.slotNumber === slot) {
625
+ return this.convertToProposedCheckpointData(stored);
626
+ }
627
+ }
628
+ return undefined;
508
629
  }
509
630
  /**
510
- * Gets the checkpoint at the proposed tip
511
- * - pending checkpoint if it exists
631
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
632
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
633
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
634
+ */ async evictProposedCheckpointsFrom(fromNumber) {
635
+ const keysToDelete = [];
636
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
637
+ if (key >= fromNumber) {
638
+ keysToDelete.push(key);
639
+ }
640
+ }
641
+ for (const key of keysToDelete){
642
+ await this.#proposedCheckpoints.delete(key);
643
+ }
644
+ }
645
+ /**
646
+ * Gets the checkpoint at the proposed tip:
647
+ * - latest pending checkpoint if any exist
512
648
  * - fallsback to latest confirmed checkpoint otherwise
513
- * @returns CommonCheckpointData
514
- */ async getProposedCheckpoint() {
515
- const stored = await this.#proposedCheckpoint.getAsync();
516
- if (!stored) {
649
+ */ async getLastCheckpoint() {
650
+ const latest = await this.getLastProposedCheckpoint();
651
+ if (!latest) {
517
652
  return this.getCheckpointData(await this.getLatestCheckpointNumber());
518
653
  }
519
- return this.convertToProposedCheckpointData(stored);
654
+ return latest;
520
655
  }
521
656
  convertToProposedCheckpointData(stored) {
522
657
  return {
@@ -534,7 +669,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
534
669
  * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
535
670
  * @returns CheckpointNumber
536
671
  */ async getProposedCheckpointNumber() {
537
- const proposed = await this.getProposedCheckpoint();
672
+ const proposed = await this.getLastCheckpoint();
538
673
  if (!proposed) {
539
674
  return await this.getLatestCheckpointNumber();
540
675
  }
@@ -544,134 +679,32 @@ export { TxReceipt } from '@aztec/stdlib/tx';
544
679
  * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
545
680
  * @returns BlockNumber
546
681
  */ async getProposedCheckpointL2BlockNumber() {
547
- const proposed = await this.getProposedCheckpoint();
682
+ const proposed = await this.getLastCheckpoint();
548
683
  if (!proposed) {
549
684
  return await this.getCheckpointedL2BlockNumber();
550
685
  }
551
686
  return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
552
687
  }
553
- async getCheckpointedBlock(number) {
554
- const blockStorage = await this.#blocks.getAsync(number);
555
- if (!blockStorage) {
556
- return undefined;
557
- }
558
- const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
559
- if (!checkpoint) {
560
- return undefined;
561
- }
562
- const block = await this.getBlockFromBlockStorage(number, blockStorage);
563
- if (!block) {
564
- return undefined;
565
- }
566
- return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
688
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */ async getCheckpointNumberBySlot(slot) {
689
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
690
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
567
691
  }
568
- /**
569
- * Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
570
- * @param start - Number of the first block to return (inclusive).
571
- * @param limit - The number of blocks to return.
572
- * @returns The requested L2 blocks
573
- */ async *getCheckpointedBlocks(start, limit) {
574
- const checkpointCache = new Map();
575
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
576
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
577
- if (block) {
578
- const checkpoint = checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ?? await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
579
- if (checkpoint) {
580
- checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
581
- const checkpointedBlock = new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
582
- yield checkpointedBlock;
583
- }
584
- }
585
- }
586
- }
587
- async getCheckpointedBlockByHash(blockHash) {
588
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
692
+ /** Gets a single L2 block matching the given resolved query. */ async getBlock(query) {
693
+ const blockNumber = await this.getBlockNumber(query);
589
694
  if (blockNumber === undefined) {
590
695
  return undefined;
591
696
  }
592
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
593
- }
594
- async getCheckpointedBlockByArchive(archive) {
595
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
596
- if (blockNumber === undefined) {
597
- return undefined;
598
- }
599
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
600
- }
601
- /**
602
- * Gets up to `limit` amount of L2 blocks starting from `from`.
603
- * @param start - Number of the first block to return (inclusive).
604
- * @param limit - The number of blocks to return.
605
- * @returns The requested L2 blocks
606
- */ async *getBlocks(start, limit) {
607
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
608
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
609
- if (block) {
610
- yield block;
611
- }
612
- }
613
- }
614
- /**
615
- * Gets block metadata (without tx data) by block number.
616
- * @param blockNumber - The number of the block to return.
617
- * @returns The requested block data.
618
- */ async getBlockData(blockNumber) {
619
697
  const blockStorage = await this.#blocks.getAsync(blockNumber);
620
- if (!blockStorage || !blockStorage.header) {
621
- return undefined;
622
- }
623
- return this.getBlockDataFromBlockStorage(blockStorage);
624
- }
625
- /**
626
- * Gets block metadata (without tx data) by archive root.
627
- * @param archive - The archive root of the block to return.
628
- * @returns The requested block data.
629
- */ async getBlockDataByArchive(archive) {
630
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
631
- if (blockNumber === undefined) {
698
+ if (!blockStorage) {
632
699
  return undefined;
633
700
  }
634
- return this.getBlockData(BlockNumber(blockNumber));
635
- }
636
- /**
637
- * Gets an L2 block.
638
- * @param blockNumber - The number of the block to return.
639
- * @returns The requested L2 block.
640
- */ async getBlock(blockNumber) {
641
- const blockStorage = await this.#blocks.getAsync(blockNumber);
642
- if (!blockStorage || !blockStorage.header) {
643
- return Promise.resolve(undefined);
644
- }
645
701
  return this.getBlockFromBlockStorage(blockNumber, blockStorage);
646
702
  }
647
- /**
648
- * Gets an L2 block by its hash.
649
- * @param blockHash - The hash of the block to return.
650
- * @returns The requested L2 block.
651
- */ async getBlockByHash(blockHash) {
652
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
653
- if (blockNumber === undefined) {
654
- return undefined;
655
- }
656
- return this.getBlock(BlockNumber(blockNumber));
703
+ /** Gets a collection of L2 blocks for a resolved range. */ getBlocks(query) {
704
+ return toArray(this.iterateBlocks(query));
657
705
  }
658
- /**
659
- * Gets an L2 block by its archive root.
660
- * @param archive - The archive root of the block to return.
661
- * @returns The requested L2 block.
662
- */ async getBlockByArchive(archive) {
663
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
664
- if (blockNumber === undefined) {
665
- return undefined;
666
- }
667
- return this.getBlock(BlockNumber(blockNumber));
668
- }
669
- /**
670
- * Gets a block header by its hash.
671
- * @param blockHash - The hash of the block to return.
672
- * @returns The requested block header.
673
- */ async getBlockHeaderByHash(blockHash) {
674
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
706
+ /** Gets single block metadata matching the given resolved query. */ async getBlockData(query) {
707
+ const blockNumber = await this.getBlockNumber(query);
675
708
  if (blockNumber === undefined) {
676
709
  return undefined;
677
710
  }
@@ -679,35 +712,30 @@ export { TxReceipt } from '@aztec/stdlib/tx';
679
712
  if (!blockStorage || !blockStorage.header) {
680
713
  return undefined;
681
714
  }
682
- return BlockHeader.fromBuffer(blockStorage.header);
715
+ return this.getBlockDataFromBlockStorage(blockStorage);
683
716
  }
684
- /**
685
- * Gets a block header by its archive root.
686
- * @param archive - The archive root of the block to return.
687
- * @returns The requested block header.
688
- */ async getBlockHeaderByArchive(archive) {
689
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
690
- if (blockNumber === undefined) {
691
- return undefined;
692
- }
693
- const blockStorage = await this.#blocks.getAsync(blockNumber);
694
- if (!blockStorage || !blockStorage.header) {
695
- return undefined;
717
+ /** Gets a collection of block metadata entries for a resolved range. */ getBlocksData(query) {
718
+ return toArray(this.iterateBlocksData(query));
719
+ }
720
+ /** Async iterator over L2 blocks for a resolved range. */ async *iterateBlocks(query) {
721
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
722
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
723
+ if (cap !== undefined && blockNumber > cap) {
724
+ break;
725
+ }
726
+ const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
727
+ if (block) {
728
+ yield block;
729
+ }
696
730
  }
697
- return BlockHeader.fromBuffer(blockStorage.header);
698
731
  }
699
- /**
700
- * Gets the headers for a sequence of L2 blocks.
701
- * @param start - Number of the first block to return (inclusive).
702
- * @param limit - The number of blocks to return.
703
- * @returns The requested L2 block headers
704
- */ async *getBlockHeaders(start, limit) {
705
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
706
- const header = BlockHeader.fromBuffer(blockStorage.header);
707
- if (header.getBlockNumber() !== blockNumber) {
708
- throw new Error(`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`);
732
+ /** Async iterator over block metadata for a resolved range. */ async *iterateBlocksData(query) {
733
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
734
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
735
+ if (cap !== undefined && blockNumber > cap) {
736
+ break;
709
737
  }
710
- yield header;
738
+ yield this.getBlockDataFromBlockStorage(blockStorage);
711
739
  }
712
740
  }
713
741
  async *getBlockStorages(start, limit) {
@@ -723,11 +751,27 @@ export { TxReceipt } from '@aztec/stdlib/tx';
723
751
  ];
724
752
  }
725
753
  }
754
+ /** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */ async getBlockNumber(query) {
755
+ let blockNumber;
756
+ if ('number' in query) {
757
+ blockNumber = query.number;
758
+ } else if ('hash' in query) {
759
+ const n = await this.#blockHashIndex.getAsync(query.hash.toString());
760
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
761
+ } else {
762
+ const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
763
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
764
+ }
765
+ if (blockNumber === undefined) {
766
+ return undefined;
767
+ }
768
+ return blockNumber;
769
+ }
726
770
  getBlockDataFromBlockStorage(blockStorage) {
727
771
  return {
728
772
  header: BlockHeader.fromBuffer(blockStorage.header),
729
773
  archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
730
- blockHash: Fr.fromBuffer(blockStorage.blockHash),
774
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
731
775
  checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
732
776
  indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
733
777
  };
@@ -771,36 +815,6 @@ export { TxReceipt } from '@aztec/stdlib/tx';
771
815
  return deserializeIndexedTxEffect(buffer);
772
816
  }
773
817
  /**
774
- * Gets a receipt of a settled tx.
775
- * @param txHash - The hash of a tx we try to get the receipt for.
776
- * @returns The requested tx receipt (or undefined if not found).
777
- */ async getSettledTxReceipt(txHash, l1Constants) {
778
- const txEffect = await this.getTxEffect(txHash);
779
- if (!txEffect) {
780
- return undefined;
781
- }
782
- const blockNumber = BlockNumber(txEffect.l2BlockNumber);
783
- // Use existing archiver methods to determine finalization level
784
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
785
- this.getProvenBlockNumber(),
786
- this.getCheckpointedL2BlockNumber(),
787
- this.getFinalizedL2BlockNumber(),
788
- this.getBlockData(blockNumber)
789
- ]);
790
- let status;
791
- if (blockNumber <= finalizedBlockNumber) {
792
- status = TxStatus.FINALIZED;
793
- } else if (blockNumber <= provenBlockNumber) {
794
- status = TxStatus.PROVEN;
795
- } else if (blockNumber <= checkpointedBlockNumber) {
796
- status = TxStatus.CHECKPOINTED;
797
- } else {
798
- status = TxStatus.PROPOSED;
799
- }
800
- const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
801
- return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
802
- }
803
- /**
804
818
  * Looks up which block included the requested tx effect.
805
819
  * @param txHash - The txHash of the tx.
806
820
  * @returns The block number and index of the tx.
@@ -809,13 +823,50 @@ export { TxReceipt } from '@aztec/stdlib/tx';
809
823
  if (!txEffect) {
810
824
  return undefined;
811
825
  }
812
- const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
826
+ // Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
827
+ // large tail (the full TxEffect with logs etc.) is irrelevant here.
828
+ const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
829
+ const l2BlockNumber = view.readUInt32BE(32);
830
+ const txIndexInBlock = view.readUInt32BE(36);
813
831
  return [
814
832
  l2BlockNumber,
815
833
  txIndexInBlock
816
834
  ];
817
835
  }
818
836
  /**
837
+ * Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
838
+ * txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
839
+ * unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
840
+ * attach effect data on demand without paying for a full {@link TxEffect} deserialization.
841
+ *
842
+ * The on-disk `IndexedTxEffect` layout starts with a fixed-length header
843
+ * (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + slotNumber(4) + revertCode(1) + txHash(32) +
844
+ * transactionFee(32)` = 109 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr`
845
+ * vectors). We
846
+ * skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
847
+ * `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
848
+ */ getNoteHashesAndNullifiers(txHashes) {
849
+ return Promise.all(txHashes.map(async (txHash)=>{
850
+ const buffer = await this.#txEffects.getAsync(txHash.toString());
851
+ if (!buffer) {
852
+ return [
853
+ [],
854
+ []
855
+ ];
856
+ }
857
+ const reader = BufferReader.asReader(buffer);
858
+ // Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + slotNumber + revertCode +
859
+ // txHash + transactionFee.
860
+ reader.readBytes(32 + 4 + 4 + 4 + 1 + 32 + 32);
861
+ const noteHashes = reader.readVectorUint8Prefix(Fr);
862
+ const nullifiers = reader.readVectorUint8Prefix(Fr);
863
+ return [
864
+ noteHashes,
865
+ nullifiers
866
+ ];
867
+ }));
868
+ }
869
+ /**
819
870
  * Looks up which block deployed a particular contract.
820
871
  * @param contractAddress - The address of the contract to look up.
821
872
  * @returns The block number and index of the contract.
@@ -841,6 +892,142 @@ export { TxReceipt } from '@aztec/stdlib/tx';
841
892
  return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
842
893
  }
843
894
  /**
895
+ * Resolves all four L2 chain tips (proposed, checkpointed, proven, finalized) in a single
896
+ * read-only transaction so the snapshot is internally consistent. Each underlying record is
897
+ * read at most once: latest block and latest confirmed checkpoint are loaded directly (no
898
+ * separate "find the number, then look up data" hop), the proven/finalized checkpoint
899
+ * singletons are read once and their storage entries are reused if they coincide with the
900
+ * latest checkpoint, and per-tip block hashes are deduped when two tips land on the same block
901
+ * (e.g. finalized == proven).
902
+ *
903
+ * The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
904
+ * block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
905
+ * `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
906
+ *
907
+ * @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
908
+ * a tip is still at genesis).
909
+ */ async getL2TipsData(genesisBlockHash) {
910
+ return await this.db.transactionAsync(async ()=>{
911
+ // Define genesis tips
912
+ const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
913
+ const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
914
+ const genesisBlockId = {
915
+ number: genesisBlockNumber,
916
+ hash: genesisBlockHash.toString()
917
+ };
918
+ const genesisCheckpointId = {
919
+ number: genesisCheckpointNumber,
920
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
921
+ };
922
+ const genesisTip = {
923
+ block: genesisBlockId,
924
+ checkpoint: genesisCheckpointId
925
+ };
926
+ // Load latest block and checkpoint entries
927
+ const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({
928
+ reverse: true,
929
+ limit: 1
930
+ }));
931
+ const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({
932
+ reverse: true,
933
+ limit: 1
934
+ }));
935
+ const latestCheckpointNumber = latestCheckpointEntry ? CheckpointNumber(latestCheckpointEntry[0]) : genesisCheckpointNumber;
936
+ // Load proven and finalized checkpoint number pointers
937
+ const [provenRaw, finalizedRaw] = await Promise.all([
938
+ this.#lastProvenCheckpoint.getAsync(),
939
+ this.#lastFinalizedCheckpoint.getAsync()
940
+ ]);
941
+ // Clamp to enforce finalized <= proven <= checkpointed.
942
+ const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
943
+ const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
944
+ // Avoid loading the same checkpoint more than once
945
+ const checkpointStorageCache = new Map();
946
+ if (latestCheckpointEntry) {
947
+ checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
948
+ }
949
+ const loadCheckpointStorage = async (n)=>{
950
+ if (n === 0) {
951
+ return undefined;
952
+ }
953
+ if (!checkpointStorageCache.has(n)) {
954
+ const checkpointStorage = await this.#checkpoints.getAsync(n);
955
+ if (!checkpointStorage) {
956
+ throw new CheckpointNotFoundError(n);
957
+ }
958
+ checkpointStorageCache.set(n, checkpointStorage);
959
+ }
960
+ return checkpointStorageCache.get(n);
961
+ };
962
+ // Load proven and finalized checkpoint storage entries
963
+ const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
964
+ const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
965
+ // Avoid loading the same block hash multiple times when tips land on the same block
966
+ const blockHashCache = new Map();
967
+ blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
968
+ if (latestBlockEntry) {
969
+ blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
970
+ }
971
+ const loadBlockHash = async (n)=>{
972
+ if (!blockHashCache.has(n)) {
973
+ const blockStorage = await this.#blocks.getAsync(n);
974
+ if (!blockStorage) {
975
+ throw new BlockNotFoundError(n);
976
+ }
977
+ const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
978
+ blockHashCache.set(n, blockHash);
979
+ }
980
+ return blockHashCache.get(n);
981
+ };
982
+ // Build proposed chain tip (this one has block only, no checkpoint)
983
+ const proposedBlockId = latestBlockEntry === undefined ? genesisBlockId : {
984
+ number: BlockNumber(latestBlockEntry[0]),
985
+ hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString()
986
+ };
987
+ // Build other tips from checkpoint data, reading corresponding block data from the cache
988
+ const buildTipFromCheckpoint = async (stored)=>{
989
+ if (!stored) {
990
+ return genesisTip;
991
+ }
992
+ const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
993
+ const blockHash = await loadBlockHash(blockNumber);
994
+ const header = CheckpointHeader.fromBuffer(stored.header);
995
+ return {
996
+ block: {
997
+ number: blockNumber,
998
+ hash: blockHash
999
+ },
1000
+ checkpoint: {
1001
+ number: CheckpointNumber(stored.checkpointNumber),
1002
+ hash: header.hash().toString()
1003
+ }
1004
+ };
1005
+ };
1006
+ const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
1007
+ const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
1008
+ const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
1009
+ // A checkpointed block past the latest stored block would mean a checkpoint
1010
+ // references blocks that aren't in blocks.
1011
+ if (proposedBlockId.number < checkpointedTip.block.number) {
1012
+ throw new Error(`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`);
1013
+ }
1014
+ // Assert that checkpoint numbers are increasing
1015
+ if (finalizedTip.checkpoint.number > provenTip.checkpoint.number || provenTip.checkpoint.number > checkpointedTip.checkpoint.number) {
1016
+ throw new Error(`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number}`);
1017
+ }
1018
+ // Assert block numbers are increasing
1019
+ if (finalizedTip.block.number > provenTip.block.number || provenTip.block.number > checkpointedTip.block.number || checkpointedTip.block.number > proposedBlockId.number) {
1020
+ throw new Error(`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposed=${proposedBlockId.number}`);
1021
+ }
1022
+ return {
1023
+ proposed: proposedBlockId,
1024
+ checkpointed: checkpointedTip,
1025
+ proven: provenTip,
1026
+ finalized: finalizedTip
1027
+ };
1028
+ });
1029
+ }
1030
+ /**
844
1031
  * Gets the most recent L1 block processed.
845
1032
  * @returns The L1 block that published the latest L2 block
846
1033
  */ getSynchedL1BlockNumber() {
@@ -849,22 +1036,29 @@ export { TxReceipt } from '@aztec/stdlib/tx';
849
1036
  setSynchedL1BlockNumber(l1BlockNumber) {
850
1037
  return this.#lastSynchedL1Block.set(l1BlockNumber);
851
1038
  }
852
- /** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
853
- * Computes archive and checkpointOutHash from the stored blocks. */ async setProposedCheckpoint(proposed) {
1039
+ /**
1040
+ * Adds a proposed checkpoint to the pending queue.
1041
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1042
+ * confirmed and the highest pending checkpoint number.
1043
+ * Computes archive and checkpointOutHash from the stored blocks.
1044
+ */ async addProposedCheckpoint(proposed) {
854
1045
  return await this.db.transactionAsync(async ()=>{
855
- const current = await this.getProposedCheckpointNumber();
856
- if (proposed.checkpointNumber <= current) {
857
- throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
858
- }
859
1046
  const confirmed = await this.getLatestCheckpointNumber();
860
- if (proposed.checkpointNumber !== confirmed + 1) {
861
- throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
1047
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
1048
+ reverse: true,
1049
+ limit: 1
1050
+ }));
1051
+ const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
1052
+ if (proposed.checkpointNumber !== latestTip + 1) {
1053
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
862
1054
  }
863
- // Ensure the previous checkpoint + blocks exist
1055
+ // Ensure the predecessor block (from pending or confirmed chain) exists
864
1056
  const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
865
1057
  const blocks = [];
866
1058
  for(let i = 0; i < proposed.blockCount; i++){
867
- const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1059
+ const block = await this.getBlock({
1060
+ number: BlockNumber(proposed.startBlock + i)
1061
+ });
868
1062
  if (!block) {
869
1063
  throw new BlockNotFoundError(proposed.startBlock + i);
870
1064
  }
@@ -873,7 +1067,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
873
1067
  this.validateCheckpointBlocks(blocks, previousBlock);
874
1068
  const archive = blocks[blocks.length - 1].archive;
875
1069
  const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
876
- await this.#proposedCheckpoint.set({
1070
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
877
1071
  header: proposed.header.toBuffer(),
878
1072
  archive: archive.toBuffer(),
879
1073
  checkpointOutHash: checkpointOutHash.toBuffer(),
@@ -886,22 +1080,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
886
1080
  });
887
1081
  }
888
1082
  async getProvenCheckpointNumber() {
889
- const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
890
- this.getLatestCheckpointNumber(),
891
- this.#lastProvenCheckpoint.getAsync()
892
- ]);
893
- return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
1083
+ return await this.db.transactionAsync(async ()=>{
1084
+ const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
1085
+ this.getLatestCheckpointNumber(),
1086
+ this.#lastProvenCheckpoint.getAsync()
1087
+ ]);
1088
+ return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
1089
+ });
894
1090
  }
895
1091
  async setProvenCheckpointNumber(checkpointNumber) {
896
1092
  const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
897
1093
  return result;
898
1094
  }
899
1095
  async getFinalizedCheckpointNumber() {
900
- const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
901
- this.getLatestCheckpointNumber(),
902
- this.#lastFinalizedCheckpoint.getAsync()
903
- ]);
904
- return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1096
+ return await this.db.transactionAsync(async ()=>{
1097
+ const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1098
+ this.getProvenCheckpointNumber(),
1099
+ this.#lastFinalizedCheckpoint.getAsync()
1100
+ ]);
1101
+ return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber ? provenCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1102
+ });
905
1103
  }
906
1104
  setFinalizedCheckpointNumber(checkpointNumber) {
907
1105
  return this.#lastFinalizedCheckpoint.set(checkpointNumber);
@@ -939,4 +1137,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
939
1137
  await this.#pendingChainValidationStatus.delete();
940
1138
  }
941
1139
  }
1140
+ /** Records a rejected-checkpoint entry, keyed by its own archive root. */ async addRejectedCheckpoint(entry) {
1141
+ const archiveRootHex = entry.archiveRoot.toString();
1142
+ await this.#rejectedCheckpoints.set(archiveRootHex, {
1143
+ checkpointNumber: entry.checkpointNumber,
1144
+ archiveRoot: entry.archiveRoot.toBuffer(),
1145
+ parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
1146
+ slotNumber: entry.slotNumber,
1147
+ l1: entry.l1.toBuffer(),
1148
+ reason: entry.reason
1149
+ });
1150
+ await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
1151
+ await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
1152
+ }
1153
+ /** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */ async getRejectedCheckpointByArchiveRoot(archiveRoot) {
1154
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
1155
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1156
+ }
1157
+ /** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */ async getRejectedCheckpointByNumber(checkpointNumber) {
1158
+ const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
1159
+ if (archiveRootHex === undefined) {
1160
+ return undefined;
1161
+ }
1162
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1163
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1164
+ }
1165
+ /** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */ async getLatestRejectedCheckpointNumber() {
1166
+ const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({
1167
+ reverse: true,
1168
+ limit: 1
1169
+ }));
1170
+ return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
1171
+ }
1172
+ /** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */ async removeRejectedCheckpointByArchiveRoot(archiveRoot) {
1173
+ const archiveRootHex = archiveRoot.toString();
1174
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1175
+ await this.#rejectedCheckpoints.delete(archiveRootHex);
1176
+ if (stored) {
1177
+ // Only clear the by-number index if it still points at this archive root, so a distinct
1178
+ // entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
1179
+ const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
1180
+ if (indexed === archiveRootHex) {
1181
+ await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
1182
+ }
1183
+ }
1184
+ }
1185
+ /**
1186
+ * Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
1187
+ * greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
1188
+ * sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
1189
+ * advance followed by a valid-checkpoint commit landing at an earlier L1 block).
1190
+ */ async advanceSynchedL1BlockNumber(l1BlockNumber) {
1191
+ const current = await this.#lastSynchedL1Block.getAsync();
1192
+ if (current === undefined || l1BlockNumber > current) {
1193
+ await this.#lastSynchedL1Block.set(l1BlockNumber);
1194
+ }
1195
+ }
1196
+ rejectedCheckpointFromStorage(stored) {
1197
+ return {
1198
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
1199
+ archiveRoot: Fr.fromBuffer(stored.archiveRoot),
1200
+ parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
1201
+ slotNumber: SlotNumber(stored.slotNumber),
1202
+ l1: L1PublishedData.fromBuffer(stored.l1),
1203
+ reason: stored.reason
1204
+ };
1205
+ }
942
1206
  }