@aztec/archiver 0.0.1-commit.86469d5 → 0.0.1-commit.8655d4a

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 (132) hide show
  1. package/README.md +19 -11
  2. package/dest/archiver.d.ts +39 -17
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +260 -160
  5. package/dest/config.d.ts +6 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +23 -15
  8. package/dest/errors.d.ts +55 -9
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +81 -14
  11. package/dest/factory.d.ts +13 -9
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +51 -37
  14. package/dest/index.d.ts +12 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +11 -2
  17. package/dest/l1/bin/retrieve-calldata.js +36 -33
  18. package/dest/l1/calldata_retriever.d.ts +74 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +201 -260
  21. package/dest/l1/data_retrieval.d.ts +26 -17
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +42 -47
  24. package/dest/l1/spire_proposer.d.ts +5 -5
  25. package/dest/l1/spire_proposer.d.ts.map +1 -1
  26. package/dest/l1/spire_proposer.js +9 -17
  27. package/dest/l1/trace_tx.d.ts +12 -66
  28. package/dest/l1/trace_tx.d.ts.map +1 -1
  29. package/dest/l1/validate_historical_logs.d.ts +23 -0
  30. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  31. package/dest/l1/validate_historical_logs.js +108 -0
  32. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  33. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  34. package/dest/modules/contract_data_source_adapter.js +40 -0
  35. package/dest/modules/data_source_base.d.ts +71 -42
  36. package/dest/modules/data_source_base.d.ts.map +1 -1
  37. package/dest/modules/data_source_base.js +270 -179
  38. package/dest/modules/data_store_updater.d.ts +49 -17
  39. package/dest/modules/data_store_updater.d.ts.map +1 -1
  40. package/dest/modules/data_store_updater.js +211 -121
  41. package/dest/modules/instrumentation.d.ts +21 -3
  42. package/dest/modules/instrumentation.d.ts.map +1 -1
  43. package/dest/modules/instrumentation.js +44 -9
  44. package/dest/modules/l1_synchronizer.d.ts +14 -12
  45. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  46. package/dest/modules/l1_synchronizer.js +443 -211
  47. package/dest/modules/validation.d.ts +4 -3
  48. package/dest/modules/validation.d.ts.map +1 -1
  49. package/dest/modules/validation.js +6 -6
  50. package/dest/store/block_store.d.ts +174 -66
  51. package/dest/store/block_store.d.ts.map +1 -1
  52. package/dest/store/block_store.js +743 -245
  53. package/dest/store/contract_class_store.d.ts +17 -4
  54. package/dest/store/contract_class_store.d.ts.map +1 -1
  55. package/dest/store/contract_class_store.js +24 -68
  56. package/dest/store/contract_instance_store.d.ts +28 -1
  57. package/dest/store/contract_instance_store.d.ts.map +1 -1
  58. package/dest/store/contract_instance_store.js +37 -2
  59. package/dest/store/data_stores.d.ts +68 -0
  60. package/dest/store/data_stores.d.ts.map +1 -0
  61. package/dest/store/data_stores.js +54 -0
  62. package/dest/store/function_names_cache.d.ts +17 -0
  63. package/dest/store/function_names_cache.d.ts.map +1 -0
  64. package/dest/store/function_names_cache.js +30 -0
  65. package/dest/store/l2_tips_cache.d.ts +25 -0
  66. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  67. package/dest/store/l2_tips_cache.js +26 -0
  68. package/dest/store/log_store.d.ts +42 -37
  69. package/dest/store/log_store.d.ts.map +1 -1
  70. package/dest/store/log_store.js +262 -388
  71. package/dest/store/log_store_codec.d.ts +70 -0
  72. package/dest/store/log_store_codec.d.ts.map +1 -0
  73. package/dest/store/log_store_codec.js +101 -0
  74. package/dest/store/message_store.d.ts +11 -1
  75. package/dest/store/message_store.d.ts.map +1 -1
  76. package/dest/store/message_store.js +51 -9
  77. package/dest/test/fake_l1_state.d.ts +25 -1
  78. package/dest/test/fake_l1_state.d.ts.map +1 -1
  79. package/dest/test/fake_l1_state.js +166 -32
  80. package/dest/test/mock_archiver.d.ts +1 -1
  81. package/dest/test/mock_archiver.d.ts.map +1 -1
  82. package/dest/test/mock_archiver.js +3 -2
  83. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  84. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  85. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  86. package/dest/test/mock_l2_block_source.d.ts +62 -41
  87. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  88. package/dest/test/mock_l2_block_source.js +321 -202
  89. package/dest/test/mock_structs.d.ts +4 -1
  90. package/dest/test/mock_structs.d.ts.map +1 -1
  91. package/dest/test/mock_structs.js +13 -1
  92. package/dest/test/noop_l1_archiver.d.ts +12 -6
  93. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  94. package/dest/test/noop_l1_archiver.js +26 -9
  95. package/package.json +14 -14
  96. package/src/archiver.ts +319 -181
  97. package/src/config.ts +32 -12
  98. package/src/errors.ts +122 -21
  99. package/src/factory.ts +75 -36
  100. package/src/index.ts +19 -2
  101. package/src/l1/README.md +25 -68
  102. package/src/l1/bin/retrieve-calldata.ts +46 -39
  103. package/src/l1/calldata_retriever.ts +260 -379
  104. package/src/l1/data_retrieval.ts +58 -69
  105. package/src/l1/spire_proposer.ts +7 -15
  106. package/src/l1/validate_historical_logs.ts +140 -0
  107. package/src/modules/contract_data_source_adapter.ts +55 -0
  108. package/src/modules/data_source_base.ts +347 -221
  109. package/src/modules/data_store_updater.ts +248 -153
  110. package/src/modules/instrumentation.ts +56 -9
  111. package/src/modules/l1_synchronizer.ts +585 -258
  112. package/src/modules/validation.ts +10 -9
  113. package/src/store/block_store.ts +924 -300
  114. package/src/store/contract_class_store.ts +31 -103
  115. package/src/store/contract_instance_store.ts +51 -5
  116. package/src/store/data_stores.ts +104 -0
  117. package/src/store/function_names_cache.ts +37 -0
  118. package/src/store/l2_tips_cache.ts +35 -0
  119. package/src/store/log_store.ts +303 -499
  120. package/src/store/log_store_codec.ts +132 -0
  121. package/src/store/message_store.ts +60 -10
  122. package/src/structs/inbox_message.ts +1 -1
  123. package/src/test/fake_l1_state.ts +213 -42
  124. package/src/test/mock_archiver.ts +3 -2
  125. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  126. package/src/test/mock_l2_block_source.ts +394 -210
  127. package/src/test/mock_structs.ts +20 -6
  128. package/src/test/noop_l1_archiver.ts +39 -9
  129. package/dest/store/kv_archiver_store.d.ts +0 -340
  130. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  131. package/dest/store/kv_archiver_store.js +0 -446
  132. package/src/store/kv_archiver_store.ts +0 -639
@@ -9,17 +9,28 @@ import { isDefined } from '@aztec/foundation/types';
9
9
  import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
10
10
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
11
11
  import {
12
+ type BlockData,
12
13
  BlockHash,
13
14
  Body,
14
- CheckpointedL2Block,
15
15
  CommitteeAttestation,
16
+ GENESIS_CHECKPOINT_HEADER_HASH,
16
17
  L2Block,
18
+ type L2TipId,
19
+ type L2Tips,
17
20
  type ValidateCheckpointResult,
18
21
  deserializeValidateCheckpointResult,
19
22
  serializeValidateCheckpointResult,
20
23
  } from '@aztec/stdlib/block';
21
- import { L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
22
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
24
+ import {
25
+ Checkpoint,
26
+ type CheckpointData,
27
+ type CommonCheckpointData,
28
+ L1PublishedData,
29
+ type ProposedCheckpointData,
30
+ type ProposedCheckpointInput,
31
+ PublishedCheckpoint,
32
+ } from '@aztec/stdlib/checkpoint';
33
+ import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
23
34
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
24
35
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
25
36
  import {
@@ -34,16 +45,20 @@ import {
34
45
  } from '@aztec/stdlib/tx';
35
46
 
36
47
  import {
48
+ BlockAlreadyCheckpointedError,
37
49
  BlockArchiveNotConsistentError,
50
+ BlockCheckpointNumberNotSequentialError,
38
51
  BlockIndexNotSequentialError,
39
52
  BlockNotFoundError,
40
53
  BlockNumberNotSequentialError,
41
54
  CannotOverwriteCheckpointedBlockError,
42
55
  CheckpointNotFoundError,
43
- CheckpointNumberNotConsistentError,
44
56
  CheckpointNumberNotSequentialError,
45
- InitialBlockNumberNotSequentialError,
46
57
  InitialCheckpointNumberNotSequentialError,
58
+ NoProposedCheckpointToPromoteError,
59
+ ProposedCheckpointArchiveRootMismatchError,
60
+ ProposedCheckpointNotSequentialError,
61
+ ProposedCheckpointPromotionNotSequentialError,
47
62
  } from '../errors.js';
48
63
 
49
64
  export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
@@ -58,28 +73,78 @@ type BlockStorage = {
58
73
  indexWithinCheckpoint: number;
59
74
  };
60
75
 
61
- type CheckpointStorage = {
76
+ /** Reason a checkpoint was rejected during sync. */
77
+ export type RejectedCheckpointReason = 'invalid-attestations' | 'descends-from-invalid-attestations';
78
+
79
+ /**
80
+ * A checkpoint observed on L1 that the archiver decided not to ingest, recorded so that
81
+ * any descendant that builds on top of it can also be skipped (rather than throwing
82
+ * `InitialCheckpointNumberNotSequentialError` and looping). An entry is dropped via
83
+ * {@link BlockStore.removeRejectedCheckpointByArchiveRoot} once a checkpoint with the same
84
+ * archive root is later ingested as valid (e.g. it gathered enough attestations), which
85
+ * re-enables its descendants.
86
+ */
87
+ export type RejectedCheckpoint = {
88
+ /** Checkpoint number this entry represents. */
89
+ checkpointNumber: CheckpointNumber;
90
+ /** Archive root produced by this rejected checkpoint (matched against descendants' `lastArchiveRoot`). */
91
+ archiveRoot: Fr;
92
+ /** `lastArchiveRoot` from this checkpoint's header (the ancestor it built on). */
93
+ parentArchiveRoot: Fr;
94
+ /** Slot number of the rejected checkpoint. */
95
+ slotNumber: SlotNumber;
96
+ /** L1 publication data for the rejected checkpoint (block number, hash, timestamp). */
97
+ l1: L1PublishedData;
98
+ /** Why the entry was recorded. */
99
+ reason: RejectedCheckpointReason;
100
+ };
101
+
102
+ type RejectedCheckpointStorage = {
103
+ checkpointNumber: number;
104
+ archiveRoot: Buffer;
105
+ parentArchiveRoot: Buffer;
106
+ slotNumber: number;
107
+ l1: Buffer;
108
+ reason: RejectedCheckpointReason;
109
+ };
110
+
111
+ /** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
112
+ type CommonCheckpointStorage = {
62
113
  header: Buffer;
63
114
  archive: Buffer;
115
+ checkpointOutHash: Buffer;
64
116
  checkpointNumber: number;
65
117
  startBlock: number;
66
- numBlocks: number;
118
+ blockCount: number;
119
+ };
120
+
121
+ type CheckpointStorage = CommonCheckpointStorage & {
67
122
  l1: Buffer;
68
123
  attestations: Buffer[];
124
+ feeAssetPriceModifier: string;
69
125
  };
70
126
 
71
- export type CheckpointData = {
72
- checkpointNumber: CheckpointNumber;
73
- header: CheckpointHeader;
74
- archive: AppendOnlyTreeSnapshot;
75
- startBlock: number;
76
- numBlocks: number;
77
- l1: L1PublishedData;
78
- attestations: Buffer[];
127
+ /** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
128
+ type ProposedCheckpointStorage = CommonCheckpointStorage & {
129
+ totalManaUsed: string;
130
+ feeAssetPriceModifier: string;
79
131
  };
80
132
 
81
133
  export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
82
134
 
135
+ /**
136
+ * Single-block lookup with the chain-tip `tag` variant of {@link BlockQuery} already resolved
137
+ * to a concrete block number. The `tag` branch is unrepresentable here so storage code does
138
+ * not need to handle it at runtime.
139
+ */
140
+ export type ResolvedBlockQuery = { number: BlockNumber } | { hash: BlockHash } | { archive: Fr };
141
+
142
+ /**
143
+ * Range lookup with the `epoch` variant of {@link BlocksQuery} already resolved to a
144
+ * `{ from, limit }` pair. Storage code never needs to map epoch numbers to block ranges.
145
+ */
146
+ export type ResolvedBlocksQuery = { from: BlockNumber; limit: number; onlyCheckpointed?: boolean };
147
+
83
148
  /**
84
149
  * LMDB-based block storage for the archiver.
85
150
  */
@@ -87,9 +152,15 @@ export class BlockStore {
87
152
  /** Map block number to block data */
88
153
  #blocks: AztecAsyncMap<number, BlockStorage>;
89
154
 
90
- /** Map checkpoint number to checkpoint data */
155
+ /** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */
156
+ #proposedCheckpoints: AztecAsyncMap<number, ProposedCheckpointStorage>;
157
+
158
+ /** Map checkpoint number to checkpoint data for mined checkpoints only */
91
159
  #checkpoints: AztecAsyncMap<number, CheckpointStorage>;
92
160
 
161
+ /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
162
+ #slotToCheckpoint: AztecAsyncMap<number, number>;
163
+
93
164
  /** Map block hash to list of tx hashes */
94
165
  #blockTxs: AztecAsyncMap<string, Buffer>;
95
166
 
@@ -102,6 +173,9 @@ export class BlockStore {
102
173
  /** Stores last proven checkpoint */
103
174
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
104
175
 
176
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
177
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
178
+
105
179
  /** Stores the pending chain validation status */
106
180
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
107
181
 
@@ -114,12 +188,15 @@ export class BlockStore {
114
188
  /** Index mapping block archive to block number */
115
189
  #blockArchiveIndex: AztecAsyncMap<string, number>;
116
190
 
191
+ /** Map rejected checkpoints (due to invalid attestations) by archive root */
192
+ #rejectedCheckpoints: AztecAsyncMap<string, RejectedCheckpointStorage>;
193
+
194
+ /** Index mapping a rejected checkpoint's number to its archive root, so the latest can be read in reverse order */
195
+ #rejectedCheckpointsByNumber: AztecAsyncMap<number, string>;
196
+
117
197
  #log = createLogger('archiver:block_store');
118
198
 
119
- constructor(
120
- private db: AztecAsyncKVStore,
121
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
122
- ) {
199
+ constructor(private db: AztecAsyncKVStore) {
123
200
  this.#blocks = db.openMap('archiver_blocks');
124
201
  this.#blockTxs = db.openMap('archiver_block_txs');
125
202
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -128,119 +205,110 @@ export class BlockStore {
128
205
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
129
206
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
130
207
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
208
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
131
209
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
132
210
  this.#checkpoints = db.openMap('archiver_checkpoints');
211
+ this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
212
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
213
+ this.#rejectedCheckpoints = db.openMap('archiver_rejected_checkpoints');
214
+ this.#rejectedCheckpointsByNumber = db.openMap('archiver_rejected_checkpoints_by_number');
133
215
  }
134
216
 
135
217
  /**
136
- * Computes the finalized block number based on the proven block number.
137
- * A block is considered finalized when it's 2 epochs behind the proven block.
138
- * TODO(#13569): Compute proper finalized block number based on L1 finalized block.
139
- * TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
218
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
219
+ * in an L1 block that has itself been finalized on Ethereum.
140
220
  * @returns The finalized block number.
141
221
  */
142
222
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
143
- const provenBlockNumber = await this.getProvenBlockNumber();
144
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
223
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
224
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
225
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
226
+ }
227
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
228
+ if (!checkpointStorage) {
229
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
230
+ }
231
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
145
232
  }
146
233
 
147
234
  /**
148
- * Append new proposed blocks to the store's list. All blocks must be for the 'current' checkpoint.
149
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
235
+ * Append a new proposed block to the store.
236
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
150
237
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
151
- * @param blocks - The proposed L2 blocks to be added to the store.
238
+ * @param block - The proposed L2 block to be added to the store.
152
239
  * @returns True if the operation is successful.
153
240
  */
154
- async addProposedBlocks(blocks: L2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
155
- if (blocks.length === 0) {
156
- return true;
157
- }
158
-
241
+ async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
159
242
  return await this.db.transactionAsync(async () => {
160
- // Check that the block immediately before the first block to be added is present in the store.
161
- const firstBlockNumber = blocks[0].number;
162
- const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
163
- const firstBlockIndex = blocks[0].indexWithinCheckpoint;
164
- const firstBlockLastArchive = blocks[0].header.lastArchive.root;
243
+ const blockNumber = block.number;
244
+ const blockCheckpointNumber = block.checkpointNumber;
245
+ const blockIndex = block.indexWithinCheckpoint;
246
+ const blockLastArchive = block.header.lastArchive.root;
165
247
 
166
248
  // Extract the latest block and checkpoint numbers
167
- const previousBlockNumber = await this.getLatestBlockNumber();
168
- const previousCheckpointNumber = await this.getLatestCheckpointNumber();
249
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
250
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
169
251
 
170
252
  // Verify we're not overwriting checkpointed blocks
171
253
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
172
- if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
173
- throw new CannotOverwriteCheckpointedBlockError(firstBlockNumber, lastCheckpointedBlockNumber);
254
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
255
+ // Check if the proposed block matches the already-checkpointed one
256
+ const existingBlock = await this.getBlockData({ number: BlockNumber(blockNumber) });
257
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
258
+ throw new BlockAlreadyCheckpointedError(blockNumber);
259
+ }
260
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
174
261
  }
175
262
 
176
- // Check that the first block number is the expected one
177
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
178
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
263
+ // Check that the block number is the expected one
264
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
265
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
179
266
  }
180
267
 
181
- // The same check as above but for checkpoints
182
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
183
- throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
268
+ // Accept the block if either the confirmed checkpoint or a pending checkpoint matches
269
+ // the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
270
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
271
+ const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
272
+ if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
273
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
274
+ const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
275
+ throw new BlockCheckpointNumberNotSequentialError(BlockNumber(blockNumber), blockCheckpointNumber, previous);
184
276
  }
185
277
 
186
278
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
187
- const previousBlockResult = await this.getBlock(previousBlockNumber);
279
+ const previousBlockResult = await this.getBlockData({ number: previousBlockNumber });
188
280
 
189
- let expectedFirstblockIndex = 0;
281
+ let expectedBlockIndex = 0;
190
282
  let previousBlockIndex: number | undefined = undefined;
191
283
  if (previousBlockResult !== undefined) {
192
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
284
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
193
285
  // The previous block is for the same checkpoint, therefore our index should follow it
194
286
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
195
- expectedFirstblockIndex = previousBlockIndex + 1;
287
+ expectedBlockIndex = previousBlockIndex + 1;
196
288
  }
197
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
289
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
198
290
  throw new BlockArchiveNotConsistentError(
199
- firstBlockNumber,
200
- previousBlockResult.number,
201
- firstBlockLastArchive,
291
+ blockNumber,
292
+ previousBlockResult.header.globalVariables.blockNumber,
293
+ blockLastArchive,
202
294
  previousBlockResult.archive.root,
203
295
  );
204
296
  }
205
297
  }
206
298
 
207
- // Now check that the first block has the expected index value
208
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
209
- throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
299
+ // Now check that the block has the expected index value
300
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
301
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
210
302
  }
211
303
 
212
- // Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
213
- let previousBlock: L2Block | undefined = undefined;
214
- for (const block of blocks) {
215
- if (!opts.force && previousBlock) {
216
- if (previousBlock.number + 1 !== block.number) {
217
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
218
- }
219
- if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
220
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
221
- }
222
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
223
- throw new BlockArchiveNotConsistentError(
224
- block.number,
225
- previousBlock.number,
226
- block.header.lastArchive.root,
227
- previousBlock.archive.root,
228
- );
229
- }
230
- }
231
- if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
232
- throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
233
- }
234
- previousBlock = block;
235
- await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
236
- }
304
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
237
305
 
238
306
  return true;
239
307
  });
240
308
  }
241
309
 
242
310
  /**
243
- * Append new cheskpoints to the store's list.
311
+ * Append new checkpoints to the store's list.
244
312
  * @param checkpoints - The L2 checkpoints to be added to the store.
245
313
  * @returns True if the operation is successful.
246
314
  */
@@ -250,37 +318,29 @@ export class BlockStore {
250
318
  }
251
319
 
252
320
  return await this.db.transactionAsync(async () => {
253
- // Check that the checkpoint immediately before the first block to be added is present in the store.
254
321
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
255
322
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
256
323
 
257
- if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
258
- throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
259
- }
260
-
261
- // Extract the previous checkpoint if there is one
262
- let previousCheckpointData: CheckpointData | undefined = undefined;
263
- if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
264
- // There should be a previous checkpoint
265
- previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
266
- if (previousCheckpointData === undefined) {
267
- throw new CheckpointNotFoundError(previousCheckpointNumber);
324
+ // Handle already-stored checkpoints at the start of the batch.
325
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
326
+ // We accept them if archives match (same content) and update their L1 metadata.
327
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
328
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
329
+ if (checkpoints.length === 0) {
330
+ return true;
268
331
  }
269
- }
270
-
271
- let previousBlockNumber: BlockNumber | undefined = undefined;
272
- let previousBlock: L2Block | undefined = undefined;
273
-
274
- // If we have a previous checkpoint then we need to get the previous block number
275
- if (previousCheckpointData !== undefined) {
276
- previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.numBlocks - 1);
277
- previousBlock = await this.getBlock(previousBlockNumber);
278
- if (previousBlock === undefined) {
279
- // We should be able to get the required previous block
280
- throw new BlockNotFoundError(previousBlockNumber);
332
+ // Re-check sequentiality after skipping
333
+ const newFirstNumber = checkpoints[0].checkpoint.number;
334
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
335
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
281
336
  }
337
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
338
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
282
339
  }
283
340
 
341
+ // Get the last block of the previous checkpoint for archive chaining
342
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
343
+
284
344
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
285
345
  let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
286
346
  for (const checkpoint of checkpoints) {
@@ -296,60 +356,154 @@ export class BlockStore {
296
356
  }
297
357
  previousCheckpoint = checkpoint;
298
358
 
299
- // Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
300
- for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
301
- const block = checkpoint.checkpoint.blocks[i];
302
- if (previousBlock) {
303
- // The blocks should have a sequential block number
304
- if (previousBlock.number !== block.number - 1) {
305
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
306
- }
307
- // If the blocks are for the same checkpoint then they should have sequential indexes
308
- if (
309
- previousBlock.checkpointNumber === block.checkpointNumber &&
310
- previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
311
- ) {
312
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
313
- }
314
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
315
- throw new BlockArchiveNotConsistentError(
316
- block.number,
317
- previousBlock.number,
318
- block.header.lastArchive.root,
319
- previousBlock.archive.root,
320
- );
321
- }
322
- } else {
323
- // No previous block, must be block 1 at checkpoint index 0
324
- if (block.indexWithinCheckpoint !== 0) {
325
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
326
- }
327
- if (block.number !== INITIAL_L2_BLOCK_NUM) {
328
- throw new BlockNumberNotSequentialError(block.number, undefined);
329
- }
330
- }
359
+ // Validate block sequencing, indexes, and archive chaining
360
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
331
361
 
332
- previousBlock = block;
333
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
362
+ // Store every block in the database (may already exist, but L1 data is authoritative)
363
+ for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
364
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
334
365
  }
366
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
335
367
 
336
368
  // Store the checkpoint in the database
337
369
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
338
370
  header: checkpoint.checkpoint.header.toBuffer(),
339
371
  archive: checkpoint.checkpoint.archive.toBuffer(),
372
+ checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
340
373
  l1: checkpoint.l1.toBuffer(),
341
374
  attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
342
375
  checkpointNumber: checkpoint.checkpoint.number,
343
376
  startBlock: checkpoint.checkpoint.blocks[0].number,
344
- numBlocks: checkpoint.checkpoint.blocks.length,
377
+ blockCount: checkpoint.checkpoint.blocks.length,
378
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString(),
345
379
  });
380
+
381
+ // Update slot-to-checkpoint index
382
+ await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
383
+
384
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
385
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
386
+
387
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
388
+ // (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
389
+ await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
346
390
  }
347
391
 
348
- await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
392
+ await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
349
393
  return true;
350
394
  });
351
395
  }
352
396
 
397
+ /**
398
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
399
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
400
+ */
401
+ private async skipOrUpdateAlreadyStoredCheckpoints(
402
+ checkpoints: PublishedCheckpoint[],
403
+ latestStored: CheckpointNumber,
404
+ ): Promise<PublishedCheckpoint[]> {
405
+ let i = 0;
406
+ for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
407
+ const incoming = checkpoints[i];
408
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
409
+ if (!stored) {
410
+ // Should not happen if latestStored is correct, but be safe
411
+ break;
412
+ }
413
+ // Verify the checkpoint content matches (archive root)
414
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
415
+ throw new Error(
416
+ `Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
417
+ `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
418
+ );
419
+ }
420
+ // Update L1 metadata and attestations for the already-stored checkpoint
421
+ this.#log.warn(
422
+ `Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
423
+ `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
424
+ );
425
+ await this.#checkpoints.set(incoming.checkpoint.number, {
426
+ header: incoming.checkpoint.header.toBuffer(),
427
+ archive: incoming.checkpoint.archive.toBuffer(),
428
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
429
+ l1: incoming.l1.toBuffer(),
430
+ attestations: incoming.attestations.map(a => a.toBuffer()),
431
+ checkpointNumber: incoming.checkpoint.number,
432
+ startBlock: incoming.checkpoint.blocks[0].number,
433
+ blockCount: incoming.checkpoint.blocks.length,
434
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString(),
435
+ });
436
+ // Update the sync point to reflect the new L1 block
437
+ await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
438
+ }
439
+ return checkpoints.slice(i);
440
+ }
441
+
442
+ /**
443
+ * Gets the last block of the checkpoint before the given one.
444
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
445
+ */
446
+ private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
447
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
448
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
449
+ return undefined;
450
+ }
451
+
452
+ // Check across both proposed and mined checkpoints
453
+ const predecessor =
454
+ (await this.getProposedCheckpointByNumber(previousCheckpointNumber)) ??
455
+ (await this.getCheckpointData(previousCheckpointNumber));
456
+
457
+ if (!predecessor) {
458
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
459
+ }
460
+
461
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
462
+ const previousBlock = await this.getBlock({ number: previousBlockNumber });
463
+ if (previousBlock === undefined) {
464
+ throw new BlockNotFoundError(previousBlockNumber);
465
+ }
466
+ return previousBlock;
467
+ }
468
+
469
+ /**
470
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
471
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
472
+ * proposed checkpoints (addProposedCheckpoint).
473
+ */
474
+ private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
475
+ for (const block of blocks) {
476
+ if (previousBlock) {
477
+ if (previousBlock.number !== block.number - 1) {
478
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
479
+ }
480
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
481
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
482
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
483
+ }
484
+ } else if (block.indexWithinCheckpoint !== 0) {
485
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
486
+ }
487
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
488
+ throw new BlockArchiveNotConsistentError(
489
+ block.number,
490
+ previousBlock.number,
491
+ block.header.lastArchive.root,
492
+ previousBlock.archive.root,
493
+ );
494
+ }
495
+ } else {
496
+ if (block.indexWithinCheckpoint !== 0) {
497
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
498
+ }
499
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
500
+ throw new BlockNumberNotSequentialError(block.number, undefined);
501
+ }
502
+ }
503
+ previousBlock = block;
504
+ }
505
+ }
506
+
353
507
  private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
354
508
  const blockHash = await block.hash();
355
509
 
@@ -425,7 +579,7 @@ export class BlockStore {
425
579
  if (!targetCheckpoint) {
426
580
  throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
427
581
  }
428
- lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.numBlocks - 1);
582
+ lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
429
583
  }
430
584
 
431
585
  // Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
@@ -433,10 +587,18 @@ export class BlockStore {
433
587
 
434
588
  // Remove all checkpoints after the target
435
589
  for (let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)) {
590
+ const checkpointStorage = await this.#checkpoints.getAsync(c);
591
+ if (checkpointStorage) {
592
+ const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
593
+ await this.#slotToCheckpoint.delete(slotNumber);
594
+ }
436
595
  await this.#checkpoints.delete(c);
437
596
  this.#log.debug(`Removed checkpoint ${c}`);
438
597
  }
439
598
 
599
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
600
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
601
+
440
602
  return { blocksRemoved };
441
603
  });
442
604
  }
@@ -461,17 +623,49 @@ export class BlockStore {
461
623
  return checkpoints;
462
624
  }
463
625
 
464
- private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage) {
465
- const data: CheckpointData = {
626
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
627
+ async getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
628
+ const result: CheckpointData[] = [];
629
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
630
+ start: startSlot,
631
+ end: endSlot + 1,
632
+ })) {
633
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
634
+ if (checkpointStorage) {
635
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
636
+ }
637
+ }
638
+ return result;
639
+ }
640
+
641
+ /**
642
+ * Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
643
+ * Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
644
+ * checkpoints fall in the range and will fetch full data for at most a few of them.
645
+ */
646
+ async getCheckpointNumbersForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointNumber[]> {
647
+ const result: CheckpointNumber[] = [];
648
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
649
+ start: startSlot,
650
+ end: endSlot + 1,
651
+ })) {
652
+ result.push(CheckpointNumber(checkpointNumber));
653
+ }
654
+ return result;
655
+ }
656
+
657
+ private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage): CheckpointData {
658
+ return {
466
659
  header: CheckpointHeader.fromBuffer(checkpointStorage.header),
467
660
  archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
661
+ checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
468
662
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
469
- startBlock: checkpointStorage.startBlock,
470
- numBlocks: checkpointStorage.numBlocks,
663
+ startBlock: BlockNumber(checkpointStorage.startBlock),
664
+ blockCount: checkpointStorage.blockCount,
665
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
471
666
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
472
- attestations: checkpointStorage.attestations,
667
+ attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
473
668
  };
474
- return data;
475
669
  }
476
670
 
477
671
  async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
@@ -483,7 +677,7 @@ export class BlockStore {
483
677
  const blocksForCheckpoint = await toArray(
484
678
  this.#blocks.entriesAsync({
485
679
  start: checkpoint.startBlock,
486
- end: checkpoint.startBlock + checkpoint.numBlocks,
680
+ end: checkpoint.startBlock + checkpoint.blockCount,
487
681
  }),
488
682
  );
489
683
 
@@ -527,11 +721,11 @@ export class BlockStore {
527
721
  const removedBlocks: L2Block[] = [];
528
722
 
529
723
  // Get the latest block number to determine the range
530
- const latestBlockNumber = await this.getLatestBlockNumber();
724
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
531
725
 
532
726
  // Iterate from blockNumber + 1 to latestBlockNumber
533
727
  for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
534
- const block = await this.getBlock(BlockNumber(bn));
728
+ const block = await this.getBlock({ number: BlockNumber(bn) });
535
729
 
536
730
  if (block === undefined) {
537
731
  this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
@@ -556,17 +750,10 @@ export class BlockStore {
556
750
  if (!checkpointStorage) {
557
751
  throw new CheckpointNotFoundError(provenCheckpointNumber);
558
752
  } else {
559
- return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
753
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
560
754
  }
561
755
  }
562
756
 
563
- async getLatestBlockNumber(): Promise<BlockNumber> {
564
- const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
565
- return typeof latestBlocknumber === 'number'
566
- ? BlockNumber(latestBlocknumber)
567
- : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
568
- }
569
-
570
757
  async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
571
758
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
572
759
  if (latestCheckpointNumber === undefined) {
@@ -575,149 +762,204 @@ export class BlockStore {
575
762
  return CheckpointNumber(latestCheckpointNumber);
576
763
  }
577
764
 
578
- async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
579
- const blockStorage = await this.#blocks.getAsync(number);
580
- if (!blockStorage) {
581
- return undefined;
582
- }
583
- const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
584
- if (!checkpoint) {
585
- return undefined;
586
- }
587
- const block = await this.getBlockFromBlockStorage(number, blockStorage);
588
- if (!block) {
589
- return undefined;
765
+ async hasProposedCheckpoint(): Promise<boolean> {
766
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ limit: 1 }));
767
+ return key !== undefined;
768
+ }
769
+
770
+ /** Deletes all pending proposed checkpoints from storage. */
771
+ async deleteProposedCheckpoints(): Promise<void> {
772
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
773
+ await this.#proposedCheckpoints.delete(key);
590
774
  }
591
- return new CheckpointedL2Block(
592
- CheckpointNumber(checkpoint.checkpointNumber),
593
- block,
594
- L1PublishedData.fromBuffer(checkpoint.l1),
595
- checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
596
- );
597
775
  }
598
776
 
599
777
  /**
600
- * Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
601
- * @param start - Number of the first block to return (inclusive).
602
- * @param limit - The number of blocks to return.
603
- * @returns The requested L2 blocks
778
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
779
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
780
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
781
+ * @param checkpointNumber - The checkpoint number to promote.
782
+ * @param l1 - L1 published data for the checkpoint.
783
+ * @param attestations - Committee attestations.
784
+ * @param expectedArchiveRoot - Archive root guard against races.
604
785
  */
605
- async *getCheckpointedBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<CheckpointedL2Block> {
606
- const checkpointCache = new Map<CheckpointNumber, CheckpointStorage>();
607
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
608
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
609
- if (block) {
610
- const checkpoint =
611
- checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ??
612
- (await this.#checkpoints.getAsync(blockStorage.checkpointNumber));
613
- if (checkpoint) {
614
- checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
615
- const checkpointedBlock = new CheckpointedL2Block(
616
- CheckpointNumber(checkpoint.checkpointNumber),
617
- block,
618
- L1PublishedData.fromBuffer(checkpoint.l1),
619
- checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
620
- );
621
- yield checkpointedBlock;
622
- }
786
+ async promoteProposedToCheckpointed(
787
+ checkpointNumber: CheckpointNumber,
788
+ l1: L1PublishedData,
789
+ attestations: CommitteeAttestation[],
790
+ expectedArchiveRoot: Fr,
791
+ ): Promise<void> {
792
+ return await this.db.transactionAsync(async () => {
793
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
794
+ if (!proposed) {
795
+ throw new NoProposedCheckpointToPromoteError();
623
796
  }
624
- }
797
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
798
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
799
+ }
800
+
801
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
802
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
803
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
804
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
805
+ }
806
+
807
+ // Write the checkpoint entry
808
+ await this.#checkpoints.set(proposed.checkpointNumber, {
809
+ header: proposed.header.toBuffer(),
810
+ archive: proposed.archive.toBuffer(),
811
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
812
+ l1: l1.toBuffer(),
813
+ attestations: attestations.map(attestation => attestation.toBuffer()),
814
+ checkpointNumber: proposed.checkpointNumber,
815
+ startBlock: proposed.startBlock,
816
+ blockCount: proposed.blockCount,
817
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
818
+ });
819
+
820
+ // Update the slot-to-checkpoint index
821
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
822
+
823
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
824
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
825
+
826
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
827
+ // (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
828
+ await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
829
+
830
+ // Update the last synced L1 block
831
+ await this.advanceSynchedL1BlockNumber(l1.blockNumber);
832
+ });
625
833
  }
626
834
 
627
- async getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
628
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
629
- if (blockNumber === undefined) {
835
+ /**
836
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
837
+ * No fallback to confirmed.
838
+ */
839
+ async getLastProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
840
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
841
+ if (key === undefined) {
630
842
  return undefined;
631
843
  }
632
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
844
+ const stored = await this.#proposedCheckpoints.getAsync(key);
845
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
633
846
  }
634
847
 
635
- async getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
636
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
637
- if (blockNumber === undefined) {
638
- return undefined;
639
- }
640
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
848
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */
849
+ async getProposedCheckpointByNumber(n: CheckpointNumber): Promise<ProposedCheckpointData | undefined> {
850
+ const stored = await this.#proposedCheckpoints.getAsync(n);
851
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
641
852
  }
642
853
 
643
854
  /**
644
- * Gets up to `limit` amount of L2 blocks starting from `from`.
645
- * @param start - Number of the first block to return (inclusive).
646
- * @param limit - The number of blocks to return.
647
- * @returns The requested L2 blocks
855
+ * Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
856
+ * Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
857
+ * in normal operation (bounded by the proposer pipelining window). Returns the first match.
648
858
  */
649
- async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2Block> {
650
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
651
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
652
- if (block) {
653
- yield block;
859
+ async getProposedCheckpointBySlot(slot: SlotNumber): Promise<ProposedCheckpointData | undefined> {
860
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()) {
861
+ const header = CheckpointHeader.fromBuffer(stored.header);
862
+ if (header.slotNumber === slot) {
863
+ return this.convertToProposedCheckpointData(stored);
654
864
  }
655
865
  }
866
+ return undefined;
656
867
  }
657
868
 
658
869
  /**
659
- * Gets an L2 block.
660
- * @param blockNumber - The number of the block to return.
661
- * @returns The requested L2 block.
870
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
871
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
872
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
662
873
  */
663
- async getBlock(blockNumber: BlockNumber): Promise<L2Block | undefined> {
664
- const blockStorage = await this.#blocks.getAsync(blockNumber);
665
- if (!blockStorage || !blockStorage.header) {
666
- return Promise.resolve(undefined);
874
+ async evictProposedCheckpointsFrom(fromNumber: CheckpointNumber): Promise<void> {
875
+ const keysToDelete: number[] = [];
876
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
877
+ if (key >= fromNumber) {
878
+ keysToDelete.push(key);
879
+ }
880
+ }
881
+ for (const key of keysToDelete) {
882
+ await this.#proposedCheckpoints.delete(key);
667
883
  }
668
- return this.getBlockFromBlockStorage(blockNumber, blockStorage);
669
884
  }
670
885
 
671
886
  /**
672
- * Gets an L2 block by its hash.
673
- * @param blockHash - The hash of the block to return.
674
- * @returns The requested L2 block.
887
+ * Gets the checkpoint at the proposed tip:
888
+ * - latest pending checkpoint if any exist
889
+ * - fallsback to latest confirmed checkpoint otherwise
675
890
  */
676
- async getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
677
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
678
- if (blockNumber === undefined) {
679
- return undefined;
891
+ async getLastCheckpoint(): Promise<CommonCheckpointData | undefined> {
892
+ const latest = await this.getLastProposedCheckpoint();
893
+ if (!latest) {
894
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
680
895
  }
681
- return this.getBlock(BlockNumber(blockNumber));
896
+ return latest;
897
+ }
898
+
899
+ private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
900
+ return {
901
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
902
+ header: CheckpointHeader.fromBuffer(stored.header),
903
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
904
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
905
+ startBlock: BlockNumber(stored.startBlock),
906
+ blockCount: stored.blockCount,
907
+ totalManaUsed: BigInt(stored.totalManaUsed),
908
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
909
+ };
682
910
  }
683
911
 
684
912
  /**
685
- * Gets an L2 block by its archive root.
686
- * @param archive - The archive root of the block to return.
687
- * @returns The requested L2 block.
913
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
914
+ * @returns CheckpointNumber
688
915
  */
689
- async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
690
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
691
- if (blockNumber === undefined) {
692
- return undefined;
916
+ async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
917
+ const proposed = await this.getLastCheckpoint();
918
+ if (!proposed) {
919
+ return await this.getLatestCheckpointNumber();
693
920
  }
694
- return this.getBlock(BlockNumber(blockNumber));
921
+ return CheckpointNumber(proposed.checkpointNumber);
695
922
  }
696
923
 
697
924
  /**
698
- * Gets a block header by its hash.
699
- * @param blockHash - The hash of the block to return.
700
- * @returns The requested block header.
925
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
926
+ * @returns BlockNumber
701
927
  */
702
- async getBlockHeaderByHash(blockHash: BlockHash): Promise<BlockHeader | undefined> {
703
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
928
+ async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
929
+ const proposed = await this.getLastCheckpoint();
930
+ if (!proposed) {
931
+ return await this.getCheckpointedL2BlockNumber();
932
+ }
933
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
934
+ }
935
+
936
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */
937
+ async getCheckpointNumberBySlot(slot: SlotNumber): Promise<CheckpointNumber | undefined> {
938
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
939
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
940
+ }
941
+
942
+ /** Gets a single L2 block matching the given resolved query. */
943
+ async getBlock(query: ResolvedBlockQuery): Promise<L2Block | undefined> {
944
+ const blockNumber = await this.getBlockNumber(query);
704
945
  if (blockNumber === undefined) {
705
946
  return undefined;
706
947
  }
707
948
  const blockStorage = await this.#blocks.getAsync(blockNumber);
708
- if (!blockStorage || !blockStorage.header) {
949
+ if (!blockStorage) {
709
950
  return undefined;
710
951
  }
711
- return BlockHeader.fromBuffer(blockStorage.header);
952
+ return this.getBlockFromBlockStorage(blockNumber, blockStorage);
712
953
  }
713
954
 
714
- /**
715
- * Gets a block header by its archive root.
716
- * @param archive - The archive root of the block to return.
717
- * @returns The requested block header.
718
- */
719
- async getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
720
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
955
+ /** Gets a collection of L2 blocks for a resolved range. */
956
+ getBlocks(query: ResolvedBlocksQuery): Promise<L2Block[]> {
957
+ return toArray(this.iterateBlocks(query));
958
+ }
959
+
960
+ /** Gets single block metadata matching the given resolved query. */
961
+ async getBlockData(query: ResolvedBlockQuery): Promise<BlockData | undefined> {
962
+ const blockNumber = await this.getBlockNumber(query);
721
963
  if (blockNumber === undefined) {
722
964
  return undefined;
723
965
  }
@@ -725,24 +967,36 @@ export class BlockStore {
725
967
  if (!blockStorage || !blockStorage.header) {
726
968
  return undefined;
727
969
  }
728
- return BlockHeader.fromBuffer(blockStorage.header);
970
+ return this.getBlockDataFromBlockStorage(blockStorage);
729
971
  }
730
972
 
731
- /**
732
- * Gets the headers for a sequence of L2 blocks.
733
- * @param start - Number of the first block to return (inclusive).
734
- * @param limit - The number of blocks to return.
735
- * @returns The requested L2 block headers
736
- */
737
- async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
738
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
739
- const header = BlockHeader.fromBuffer(blockStorage.header);
740
- if (header.getBlockNumber() !== blockNumber) {
741
- throw new Error(
742
- `Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`,
743
- );
973
+ /** Gets a collection of block metadata entries for a resolved range. */
974
+ getBlocksData(query: ResolvedBlocksQuery): Promise<BlockData[]> {
975
+ return toArray(this.iterateBlocksData(query));
976
+ }
977
+
978
+ /** Async iterator over L2 blocks for a resolved range. */
979
+ private async *iterateBlocks(query: ResolvedBlocksQuery): AsyncIterableIterator<L2Block> {
980
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
981
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
982
+ if (cap !== undefined && blockNumber > cap) {
983
+ break;
984
+ }
985
+ const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
986
+ if (block) {
987
+ yield block;
744
988
  }
745
- yield header;
989
+ }
990
+ }
991
+
992
+ /** Async iterator over block metadata for a resolved range. */
993
+ private async *iterateBlocksData(query: ResolvedBlocksQuery): AsyncIterableIterator<BlockData> {
994
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
995
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
996
+ if (cap !== undefined && blockNumber > cap) {
997
+ break;
998
+ }
999
+ yield this.getBlockDataFromBlockStorage(blockStorage);
746
1000
  }
747
1001
  }
748
1002
 
@@ -759,15 +1013,42 @@ export class BlockStore {
759
1013
  }
760
1014
  }
761
1015
 
1016
+ /** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */
1017
+ async getBlockNumber(query: ResolvedBlockQuery): Promise<BlockNumber | undefined> {
1018
+ let blockNumber: BlockNumber | undefined;
1019
+ if ('number' in query) {
1020
+ blockNumber = query.number;
1021
+ } else if ('hash' in query) {
1022
+ const n = await this.#blockHashIndex.getAsync(query.hash.toString());
1023
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
1024
+ } else {
1025
+ const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
1026
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
1027
+ }
1028
+ if (blockNumber === undefined) {
1029
+ return undefined;
1030
+ }
1031
+ return blockNumber;
1032
+ }
1033
+
1034
+ private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
1035
+ return {
1036
+ header: BlockHeader.fromBuffer(blockStorage.header),
1037
+ archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
1038
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
1039
+ checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
1040
+ indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
1041
+ };
1042
+ }
1043
+
762
1044
  private async getBlockFromBlockStorage(
763
1045
  blockNumber: number,
764
1046
  blockStorage: BlockStorage,
765
1047
  ): Promise<L2Block | undefined> {
766
- const header = BlockHeader.fromBuffer(blockStorage.header);
767
- const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
768
- const blockHash = blockStorage.blockHash;
769
- header.setHash(Fr.fromBuffer(blockHash));
770
- const blockHashString = bufferToHex(blockHash);
1048
+ const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } =
1049
+ this.getBlockDataFromBlockStorage(blockStorage);
1050
+ header.setHash(blockHash);
1051
+ const blockHashString = bufferToHex(blockStorage.blockHash);
771
1052
  const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
772
1053
  if (blockTxsBuffer === undefined) {
773
1054
  this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
@@ -786,13 +1067,7 @@ export class BlockStore {
786
1067
  txEffects.push(deserializeIndexedTxEffect(txEffect).data);
787
1068
  }
788
1069
  const body = new Body(txEffects);
789
- const block = new L2Block(
790
- archive,
791
- header,
792
- body,
793
- CheckpointNumber(blockStorage.checkpointNumber!),
794
- IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
795
- );
1070
+ const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
796
1071
 
797
1072
  if (block.number !== blockNumber) {
798
1073
  throw new Error(
@@ -822,7 +1097,10 @@ export class BlockStore {
822
1097
  * @param txHash - The hash of a tx we try to get the receipt for.
823
1098
  * @returns The requested tx receipt (or undefined if not found).
824
1099
  */
825
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
1100
+ async getSettledTxReceipt(
1101
+ txHash: TxHash,
1102
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
1103
+ ): Promise<TxReceipt | undefined> {
826
1104
  const txEffect = await this.getTxEffect(txHash);
827
1105
  if (!txEffect) {
828
1106
  return undefined;
@@ -831,10 +1109,11 @@ export class BlockStore {
831
1109
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
832
1110
 
833
1111
  // Use existing archiver methods to determine finalization level
834
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
1112
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
835
1113
  this.getProvenBlockNumber(),
836
1114
  this.getCheckpointedL2BlockNumber(),
837
1115
  this.getFinalizedL2BlockNumber(),
1116
+ this.getBlockData({ number: blockNumber }),
838
1117
  ]);
839
1118
 
840
1119
  let status: TxStatus;
@@ -848,6 +1127,9 @@ export class BlockStore {
848
1127
  status = TxStatus.PROPOSED;
849
1128
  }
850
1129
 
1130
+ const epochNumber =
1131
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
1132
+
851
1133
  return new TxReceipt(
852
1134
  txHash,
853
1135
  status,
@@ -856,6 +1138,7 @@ export class BlockStore {
856
1138
  txEffect.data.transactionFee.toBigInt(),
857
1139
  txEffect.l2BlockHash,
858
1140
  blockNumber,
1141
+ epochNumber,
859
1142
  );
860
1143
  }
861
1144
 
@@ -869,10 +1152,43 @@ export class BlockStore {
869
1152
  if (!txEffect) {
870
1153
  return undefined;
871
1154
  }
872
- const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
1155
+ // Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
1156
+ // large tail (the full TxEffect with logs etc.) is irrelevant here.
1157
+ const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
1158
+ const l2BlockNumber = view.readUInt32BE(32);
1159
+ const txIndexInBlock = view.readUInt32BE(36);
873
1160
  return [l2BlockNumber, txIndexInBlock];
874
1161
  }
875
1162
 
1163
+ /**
1164
+ * Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
1165
+ * txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
1166
+ * unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
1167
+ * attach effect data on demand without paying for a full {@link TxEffect} deserialization.
1168
+ *
1169
+ * The on-disk `IndexedTxEffect` layout starts with a fixed-length header
1170
+ * (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + revertCode(1) + txHash(32) + transactionFee(32)` =
1171
+ * 105 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr` vectors). We
1172
+ * skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
1173
+ * `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
1174
+ */
1175
+ public getNoteHashesAndNullifiers(txHashes: TxHash[]): Promise<[Fr[], Fr[]][]> {
1176
+ return Promise.all(
1177
+ txHashes.map(async (txHash): Promise<[Fr[], Fr[]]> => {
1178
+ const buffer = await this.#txEffects.getAsync(txHash.toString());
1179
+ if (!buffer) {
1180
+ return [[], []];
1181
+ }
1182
+ const reader = BufferReader.asReader(buffer);
1183
+ // Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + revertCode + txHash + transactionFee.
1184
+ reader.readBytes(32 + 4 + 4 + 1 + 32 + 32);
1185
+ const noteHashes = reader.readVectorUint8Prefix(Fr);
1186
+ const nullifiers = reader.readVectorUint8Prefix(Fr);
1187
+ return [noteHashes, nullifiers];
1188
+ }),
1189
+ );
1190
+ }
1191
+
876
1192
  /**
877
1193
  * Looks up which block deployed a particular contract.
878
1194
  * @param contractAddress - The address of the contract to look up.
@@ -892,7 +1208,7 @@ export class BlockStore {
892
1208
  if (!checkpoint) {
893
1209
  return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
894
1210
  }
895
- return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
1211
+ return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
896
1212
  }
897
1213
 
898
1214
  async getLatestL2BlockNumber(): Promise<BlockNumber> {
@@ -900,6 +1216,174 @@ export class BlockStore {
900
1216
  return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
901
1217
  }
902
1218
 
1219
+ /**
1220
+ * Resolves all five L2 chain tips (proposed, proposedCheckpoint, checkpointed, proven, finalized)
1221
+ * in a single read-only transaction so the snapshot is internally consistent. Each underlying
1222
+ * record is read at most once: latest block, latest confirmed checkpoint, and latest pending
1223
+ * checkpoint are each loaded directly (no separate "find the number, then look up data" hop),
1224
+ * the proven/finalized checkpoint singletons are read once and their storage entries are
1225
+ * reused if they coincide with the latest checkpoint, and per-tip block hashes are deduped
1226
+ * when two tips land on the same block (e.g. finalized == proven, or proposedCheckpoint falls
1227
+ * back to checkpointed when no pending checkpoint exists).
1228
+ *
1229
+ * The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
1230
+ * block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
1231
+ * `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
1232
+ *
1233
+ * @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
1234
+ * a tip is still at genesis).
1235
+ */
1236
+ async getL2TipsData(genesisBlockHash: BlockHash): Promise<L2Tips> {
1237
+ return await this.db.transactionAsync(async () => {
1238
+ // Define genesis tips
1239
+ const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
1240
+ const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
1241
+ const genesisBlockId = { number: genesisBlockNumber, hash: genesisBlockHash.toString() };
1242
+ const genesisCheckpointId = {
1243
+ number: genesisCheckpointNumber,
1244
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
1245
+ };
1246
+ const genesisTip: L2TipId = { block: genesisBlockId, checkpoint: genesisCheckpointId };
1247
+
1248
+ // Load latest block and checkpoint entries
1249
+ const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({ reverse: true, limit: 1 }));
1250
+ const [proposedCheckpointEntry] = await toArray(
1251
+ this.#proposedCheckpoints.entriesAsync({ reverse: true, limit: 1 }),
1252
+ );
1253
+ const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({ reverse: true, limit: 1 }));
1254
+ const latestCheckpointNumber = latestCheckpointEntry
1255
+ ? CheckpointNumber(latestCheckpointEntry[0])
1256
+ : genesisCheckpointNumber;
1257
+
1258
+ // Load proven and finalized checkpoint number pointers
1259
+ const [provenRaw, finalizedRaw] = await Promise.all([
1260
+ this.#lastProvenCheckpoint.getAsync(),
1261
+ this.#lastFinalizedCheckpoint.getAsync(),
1262
+ ]);
1263
+
1264
+ // Clamp to enforce finalized <= proven <= checkpointed.
1265
+ const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
1266
+ const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
1267
+
1268
+ // Avoid loading the same checkpoint more than once
1269
+ const checkpointStorageCache = new Map<CheckpointNumber, CheckpointStorage>();
1270
+ if (latestCheckpointEntry) {
1271
+ checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
1272
+ }
1273
+ const loadCheckpointStorage = async (n: CheckpointNumber): Promise<CheckpointStorage | undefined> => {
1274
+ if (n === 0) {
1275
+ return undefined;
1276
+ }
1277
+ if (!checkpointStorageCache.has(n)) {
1278
+ const checkpointStorage = await this.#checkpoints.getAsync(n);
1279
+ if (!checkpointStorage) {
1280
+ throw new CheckpointNotFoundError(n);
1281
+ }
1282
+ checkpointStorageCache.set(n, checkpointStorage);
1283
+ }
1284
+ return checkpointStorageCache.get(n)!;
1285
+ };
1286
+
1287
+ // Load proven and finalized checkpoint storage entries
1288
+ const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
1289
+ const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
1290
+
1291
+ // Avoid loading the same block hash multiple times when tips land on the same block
1292
+ const blockHashCache = new Map<number, string>();
1293
+ blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
1294
+ if (latestBlockEntry) {
1295
+ blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
1296
+ }
1297
+ const loadBlockHash = async (n: BlockNumber): Promise<string> => {
1298
+ if (!blockHashCache.has(n)) {
1299
+ const blockStorage = await this.#blocks.getAsync(n);
1300
+ if (!blockStorage) {
1301
+ throw new BlockNotFoundError(n);
1302
+ }
1303
+ const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
1304
+ blockHashCache.set(n, blockHash);
1305
+ }
1306
+ return blockHashCache.get(n)!;
1307
+ };
1308
+
1309
+ // Build proposed chain tip (this one has block only, no checkpoint)
1310
+ const proposedBlockId =
1311
+ latestBlockEntry === undefined
1312
+ ? genesisBlockId
1313
+ : {
1314
+ number: BlockNumber(latestBlockEntry[0]),
1315
+ hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString(),
1316
+ };
1317
+
1318
+ // Build other tips from checkpoint data, reading corresponding block data from the cache
1319
+ const buildTipFromCheckpoint = async (
1320
+ stored: ProposedCheckpointStorage | CheckpointStorage | undefined,
1321
+ ): Promise<L2TipId> => {
1322
+ if (!stored) {
1323
+ return genesisTip;
1324
+ }
1325
+ const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
1326
+ const blockHash = await loadBlockHash(blockNumber);
1327
+ const header = CheckpointHeader.fromBuffer(stored.header);
1328
+ return {
1329
+ block: { number: blockNumber, hash: blockHash },
1330
+ checkpoint: { number: CheckpointNumber(stored.checkpointNumber), hash: header.hash().toString() },
1331
+ };
1332
+ };
1333
+
1334
+ const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
1335
+ const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
1336
+ const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
1337
+
1338
+ // Proposed checkpoint falls back to the checkpoint tip if it's not set. And if local storage is
1339
+ // inconsistent and the proposed checkpoint is behind the checkpointed tip, we patch that and
1340
+ // report the checkpointed tip as the proposed checkpoint to maintain the invariant.
1341
+ const proposedCheckpointTip =
1342
+ proposedCheckpointEntry === undefined || proposedCheckpointEntry[0] <= latestCheckpointNumber
1343
+ ? checkpointedTip
1344
+ : await buildTipFromCheckpoint(proposedCheckpointEntry[1]);
1345
+
1346
+ // A checkpointed block past the latest stored block would mean a checkpoint
1347
+ // references blocks that aren't in blocks.
1348
+ if (proposedBlockId.number < checkpointedTip.block.number) {
1349
+ throw new Error(
1350
+ `Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`,
1351
+ );
1352
+ }
1353
+
1354
+ // Assert that checkpoint numbers are increasing
1355
+ if (
1356
+ finalizedTip.checkpoint.number > provenTip.checkpoint.number ||
1357
+ provenTip.checkpoint.number > checkpointedTip.checkpoint.number ||
1358
+ checkpointedTip.checkpoint.number > proposedCheckpointTip.checkpoint.number
1359
+ ) {
1360
+ throw new Error(
1361
+ `Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number} proposed=${proposedCheckpointTip.checkpoint.number}`,
1362
+ );
1363
+ }
1364
+
1365
+ // Assert block numbers are increasing
1366
+ if (
1367
+ finalizedTip.block.number > provenTip.block.number ||
1368
+ provenTip.block.number > checkpointedTip.block.number ||
1369
+ checkpointedTip.block.number > proposedCheckpointTip.block.number ||
1370
+ proposedCheckpointTip.block.number > proposedBlockId.number
1371
+ ) {
1372
+ throw new Error(
1373
+ `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}`,
1374
+ );
1375
+ }
1376
+
1377
+ return {
1378
+ proposed: proposedBlockId,
1379
+ proposedCheckpoint: proposedCheckpointTip,
1380
+ checkpointed: checkpointedTip,
1381
+ proven: provenTip,
1382
+ finalized: finalizedTip,
1383
+ };
1384
+ });
1385
+ }
1386
+
903
1387
  /**
904
1388
  * Gets the most recent L1 block processed.
905
1389
  * @returns The L1 block that published the latest L2 block
@@ -912,14 +1396,62 @@ export class BlockStore {
912
1396
  return this.#lastSynchedL1Block.set(l1BlockNumber);
913
1397
  }
914
1398
 
1399
+ /**
1400
+ * Adds a proposed checkpoint to the pending queue.
1401
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1402
+ * confirmed and the highest pending checkpoint number.
1403
+ * Computes archive and checkpointOutHash from the stored blocks.
1404
+ */
1405
+ async addProposedCheckpoint(proposed: ProposedCheckpointInput) {
1406
+ return await this.db.transactionAsync(async () => {
1407
+ const confirmed = await this.getLatestCheckpointNumber();
1408
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
1409
+ const latestTip = CheckpointNumber(
1410
+ latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed,
1411
+ );
1412
+
1413
+ if (proposed.checkpointNumber !== latestTip + 1) {
1414
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
1415
+ }
1416
+
1417
+ // Ensure the predecessor block (from pending or confirmed chain) exists
1418
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1419
+ const blocks: L2Block[] = [];
1420
+ for (let i = 0; i < proposed.blockCount; i++) {
1421
+ const block = await this.getBlock({ number: BlockNumber(proposed.startBlock + i) });
1422
+ if (!block) {
1423
+ throw new BlockNotFoundError(proposed.startBlock + i);
1424
+ }
1425
+ blocks.push(block);
1426
+ }
1427
+ this.validateCheckpointBlocks(blocks, previousBlock);
1428
+
1429
+ const archive = blocks[blocks.length - 1].archive;
1430
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1431
+
1432
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
1433
+ header: proposed.header.toBuffer(),
1434
+ archive: archive.toBuffer(),
1435
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1436
+ checkpointNumber: proposed.checkpointNumber,
1437
+ startBlock: proposed.startBlock,
1438
+ blockCount: proposed.blockCount,
1439
+ totalManaUsed: proposed.totalManaUsed.toString(),
1440
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1441
+ });
1442
+ });
1443
+ }
1444
+
915
1445
  async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
916
- const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
917
- this.getLatestCheckpointNumber(),
918
- this.#lastProvenCheckpoint.getAsync(),
919
- ]);
920
- return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
921
- ? latestCheckpointNumber
922
- : CheckpointNumber(provenCheckpointNumber ?? 0);
1446
+ return await this.db.transactionAsync(async () => {
1447
+ const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
1448
+ this.getLatestCheckpointNumber(),
1449
+ this.#lastProvenCheckpoint.getAsync(),
1450
+ ]);
1451
+ return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
1452
+ ? latestCheckpointNumber
1453
+ : CheckpointNumber(provenCheckpointNumber ?? 0);
1454
+ });
923
1455
  }
924
1456
 
925
1457
  async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
@@ -927,6 +1459,22 @@ export class BlockStore {
927
1459
  return result;
928
1460
  }
929
1461
 
1462
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
1463
+ return await this.db.transactionAsync(async () => {
1464
+ const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1465
+ this.getProvenCheckpointNumber(),
1466
+ this.#lastFinalizedCheckpoint.getAsync(),
1467
+ ]);
1468
+ return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber
1469
+ ? provenCheckpointNumber
1470
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1471
+ });
1472
+ }
1473
+
1474
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1475
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1476
+ }
1477
+
930
1478
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
931
1479
  if (limit < 1) {
932
1480
  throw new Error(`Invalid limit: ${limit}`);
@@ -963,4 +1511,80 @@ export class BlockStore {
963
1511
  await this.#pendingChainValidationStatus.delete();
964
1512
  }
965
1513
  }
1514
+
1515
+ /** Records a rejected-checkpoint entry, keyed by its own archive root. */
1516
+ async addRejectedCheckpoint(entry: RejectedCheckpoint): Promise<void> {
1517
+ const archiveRootHex = entry.archiveRoot.toString();
1518
+ await this.#rejectedCheckpoints.set(archiveRootHex, {
1519
+ checkpointNumber: entry.checkpointNumber,
1520
+ archiveRoot: entry.archiveRoot.toBuffer(),
1521
+ parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
1522
+ slotNumber: entry.slotNumber,
1523
+ l1: entry.l1.toBuffer(),
1524
+ reason: entry.reason,
1525
+ });
1526
+ await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
1527
+ await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
1528
+ }
1529
+
1530
+ /** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */
1531
+ async getRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<RejectedCheckpoint | undefined> {
1532
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
1533
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1534
+ }
1535
+
1536
+ /** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */
1537
+ async getRejectedCheckpointByNumber(checkpointNumber: CheckpointNumber): Promise<RejectedCheckpoint | undefined> {
1538
+ const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
1539
+ if (archiveRootHex === undefined) {
1540
+ return undefined;
1541
+ }
1542
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1543
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1544
+ }
1545
+
1546
+ /** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */
1547
+ async getLatestRejectedCheckpointNumber(): Promise<CheckpointNumber> {
1548
+ const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({ reverse: true, limit: 1 }));
1549
+ return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
1550
+ }
1551
+
1552
+ /** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */
1553
+ async removeRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<void> {
1554
+ const archiveRootHex = archiveRoot.toString();
1555
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1556
+ await this.#rejectedCheckpoints.delete(archiveRootHex);
1557
+ if (stored) {
1558
+ // Only clear the by-number index if it still points at this archive root, so a distinct
1559
+ // entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
1560
+ const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
1561
+ if (indexed === archiveRootHex) {
1562
+ await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
1563
+ }
1564
+ }
1565
+ }
1566
+
1567
+ /**
1568
+ * Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
1569
+ * greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
1570
+ * sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
1571
+ * advance followed by a valid-checkpoint commit landing at an earlier L1 block).
1572
+ */
1573
+ private async advanceSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
1574
+ const current = await this.#lastSynchedL1Block.getAsync();
1575
+ if (current === undefined || l1BlockNumber > current) {
1576
+ await this.#lastSynchedL1Block.set(l1BlockNumber);
1577
+ }
1578
+ }
1579
+
1580
+ private rejectedCheckpointFromStorage(stored: RejectedCheckpointStorage): RejectedCheckpoint {
1581
+ return {
1582
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
1583
+ archiveRoot: Fr.fromBuffer(stored.archiveRoot),
1584
+ parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
1585
+ slotNumber: SlotNumber(stored.slotNumber),
1586
+ l1: L1PublishedData.fromBuffer(stored.l1),
1587
+ reason: stored.reason,
1588
+ };
1589
+ }
966
1590
  }