@aztec/archiver 0.0.1-commit.e3c1de76 → 0.0.1-commit.e57c76e

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