@aztec/archiver 0.0.1-commit.e558bd1c → 0.0.1-commit.e5a3663dd

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 (126) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +26 -15
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +161 -153
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +16 -4
  8. package/dest/errors.d.ts +61 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +88 -14
  11. package/dest/factory.d.ts +6 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +40 -32
  14. package/dest/index.d.ts +11 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +10 -2
  17. package/dest/l1/bin/retrieve-calldata.js +32 -28
  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 +197 -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/validate_historical_logs.d.ts +23 -0
  28. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  29. package/dest/l1/validate_historical_logs.js +108 -0
  30. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  31. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  32. package/dest/modules/contract_data_source_adapter.js +42 -0
  33. package/dest/modules/data_source_base.d.ts +27 -14
  34. package/dest/modules/data_source_base.d.ts.map +1 -1
  35. package/dest/modules/data_source_base.js +98 -125
  36. package/dest/modules/data_store_updater.d.ts +37 -17
  37. package/dest/modules/data_store_updater.d.ts.map +1 -1
  38. package/dest/modules/data_store_updater.js +155 -112
  39. package/dest/modules/instrumentation.d.ts +21 -3
  40. package/dest/modules/instrumentation.d.ts.map +1 -1
  41. package/dest/modules/instrumentation.js +41 -8
  42. package/dest/modules/l1_synchronizer.d.ts +13 -11
  43. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  44. package/dest/modules/l1_synchronizer.js +355 -182
  45. package/dest/modules/validation.d.ts +4 -3
  46. package/dest/modules/validation.d.ts.map +1 -1
  47. package/dest/modules/validation.js +6 -6
  48. package/dest/store/block_store.d.ts +107 -31
  49. package/dest/store/block_store.d.ts.map +1 -1
  50. package/dest/store/block_store.js +477 -141
  51. package/dest/store/contract_class_store.d.ts +17 -4
  52. package/dest/store/contract_class_store.d.ts.map +1 -1
  53. package/dest/store/contract_class_store.js +24 -68
  54. package/dest/store/contract_instance_store.d.ts +28 -1
  55. package/dest/store/contract_instance_store.d.ts.map +1 -1
  56. package/dest/store/contract_instance_store.js +37 -2
  57. package/dest/store/data_stores.d.ts +68 -0
  58. package/dest/store/data_stores.d.ts.map +1 -0
  59. package/dest/store/data_stores.js +50 -0
  60. package/dest/store/function_names_cache.d.ts +17 -0
  61. package/dest/store/function_names_cache.d.ts.map +1 -0
  62. package/dest/store/function_names_cache.js +30 -0
  63. package/dest/store/l2_tips_cache.d.ts +20 -0
  64. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  65. package/dest/store/l2_tips_cache.js +109 -0
  66. package/dest/store/log_store.d.ts +6 -3
  67. package/dest/store/log_store.d.ts.map +1 -1
  68. package/dest/store/log_store.js +95 -20
  69. package/dest/store/message_store.d.ts +11 -1
  70. package/dest/store/message_store.d.ts.map +1 -1
  71. package/dest/store/message_store.js +51 -9
  72. package/dest/test/fake_l1_state.d.ts +25 -1
  73. package/dest/test/fake_l1_state.d.ts.map +1 -1
  74. package/dest/test/fake_l1_state.js +166 -32
  75. package/dest/test/mock_archiver.d.ts +1 -1
  76. package/dest/test/mock_archiver.d.ts.map +1 -1
  77. package/dest/test/mock_archiver.js +3 -2
  78. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  79. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  80. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  81. package/dest/test/mock_l2_block_source.d.ts +35 -5
  82. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  83. package/dest/test/mock_l2_block_source.js +182 -89
  84. package/dest/test/mock_structs.d.ts +4 -1
  85. package/dest/test/mock_structs.d.ts.map +1 -1
  86. package/dest/test/mock_structs.js +13 -1
  87. package/dest/test/noop_l1_archiver.d.ts +7 -4
  88. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  89. package/dest/test/noop_l1_archiver.js +14 -8
  90. package/package.json +13 -13
  91. package/src/archiver.ts +199 -174
  92. package/src/config.ts +23 -2
  93. package/src/errors.ts +133 -22
  94. package/src/factory.ts +53 -30
  95. package/src/index.ts +18 -2
  96. package/src/l1/README.md +25 -68
  97. package/src/l1/bin/retrieve-calldata.ts +40 -27
  98. package/src/l1/calldata_retriever.ts +261 -379
  99. package/src/l1/data_retrieval.ts +58 -69
  100. package/src/l1/spire_proposer.ts +7 -15
  101. package/src/l1/validate_historical_logs.ts +140 -0
  102. package/src/modules/contract_data_source_adapter.ts +59 -0
  103. package/src/modules/data_source_base.ts +142 -144
  104. package/src/modules/data_store_updater.ts +187 -141
  105. package/src/modules/instrumentation.ts +56 -9
  106. package/src/modules/l1_synchronizer.ts +463 -218
  107. package/src/modules/validation.ts +10 -9
  108. package/src/store/block_store.ts +587 -177
  109. package/src/store/contract_class_store.ts +31 -103
  110. package/src/store/contract_instance_store.ts +51 -5
  111. package/src/store/data_stores.ts +108 -0
  112. package/src/store/function_names_cache.ts +37 -0
  113. package/src/store/l2_tips_cache.ts +134 -0
  114. package/src/store/log_store.ts +128 -32
  115. package/src/store/message_store.ts +60 -10
  116. package/src/structs/inbox_message.ts +1 -1
  117. package/src/test/fake_l1_state.ts +213 -42
  118. package/src/test/mock_archiver.ts +3 -2
  119. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  120. package/src/test/mock_l2_block_source.ts +230 -82
  121. package/src/test/mock_structs.ts +20 -6
  122. package/src/test/noop_l1_archiver.ts +16 -8
  123. package/dest/store/kv_archiver_store.d.ts +0 -340
  124. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  125. package/dest/store/kv_archiver_store.js +0 -446
  126. package/src/store/kv_archiver_store.ts +0 -639
@@ -9,6 +9,8 @@ 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,
13
+ type BlockDataWithCheckpointContext,
12
14
  BlockHash,
13
15
  Body,
14
16
  CheckpointedL2Block,
@@ -18,8 +20,16 @@ import {
18
20
  deserializeValidateCheckpointResult,
19
21
  serializeValidateCheckpointResult,
20
22
  } from '@aztec/stdlib/block';
21
- import { L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
22
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
23
+ import {
24
+ Checkpoint,
25
+ type CheckpointData,
26
+ type CommonCheckpointData,
27
+ L1PublishedData,
28
+ type ProposedCheckpointData,
29
+ type ProposedCheckpointInput,
30
+ PublishedCheckpoint,
31
+ } from '@aztec/stdlib/checkpoint';
32
+ import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
23
33
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
24
34
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
25
35
  import {
@@ -34,16 +44,20 @@ import {
34
44
  } from '@aztec/stdlib/tx';
35
45
 
36
46
  import {
47
+ BlockAlreadyCheckpointedError,
37
48
  BlockArchiveNotConsistentError,
49
+ BlockCheckpointNumberNotSequentialError,
38
50
  BlockIndexNotSequentialError,
39
51
  BlockNotFoundError,
40
52
  BlockNumberNotSequentialError,
41
53
  CannotOverwriteCheckpointedBlockError,
42
54
  CheckpointNotFoundError,
43
- CheckpointNumberNotConsistentError,
44
55
  CheckpointNumberNotSequentialError,
45
- InitialBlockNumberNotSequentialError,
46
56
  InitialCheckpointNumberNotSequentialError,
57
+ NoProposedCheckpointToPromoteError,
58
+ ProposedCheckpointArchiveRootMismatchError,
59
+ ProposedCheckpointNotSequentialError,
60
+ ProposedCheckpointPromotionNotSequentialError,
47
61
  } from '../errors.js';
48
62
 
49
63
  export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
@@ -58,24 +72,26 @@ type BlockStorage = {
58
72
  indexWithinCheckpoint: number;
59
73
  };
60
74
 
61
- type CheckpointStorage = {
75
+ /** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
76
+ type CommonCheckpointStorage = {
62
77
  header: Buffer;
63
78
  archive: Buffer;
79
+ checkpointOutHash: Buffer;
64
80
  checkpointNumber: number;
65
81
  startBlock: number;
66
- numBlocks: number;
82
+ blockCount: number;
83
+ };
84
+
85
+ type CheckpointStorage = CommonCheckpointStorage & {
67
86
  l1: Buffer;
68
87
  attestations: Buffer[];
88
+ feeAssetPriceModifier: string;
69
89
  };
70
90
 
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[];
91
+ /** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
92
+ type ProposedCheckpointStorage = CommonCheckpointStorage & {
93
+ totalManaUsed: string;
94
+ feeAssetPriceModifier: string;
79
95
  };
80
96
 
81
97
  export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
@@ -87,9 +103,15 @@ export class BlockStore {
87
103
  /** Map block number to block data */
88
104
  #blocks: AztecAsyncMap<number, BlockStorage>;
89
105
 
90
- /** Map checkpoint number to checkpoint data */
106
+ /** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */
107
+ #proposedCheckpoints: AztecAsyncMap<number, ProposedCheckpointStorage>;
108
+
109
+ /** Map checkpoint number to checkpoint data for mined checkpoints only */
91
110
  #checkpoints: AztecAsyncMap<number, CheckpointStorage>;
92
111
 
112
+ /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
113
+ #slotToCheckpoint: AztecAsyncMap<number, number>;
114
+
93
115
  /** Map block hash to list of tx hashes */
94
116
  #blockTxs: AztecAsyncMap<string, Buffer>;
95
117
 
@@ -102,6 +124,9 @@ export class BlockStore {
102
124
  /** Stores last proven checkpoint */
103
125
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
104
126
 
127
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
128
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
129
+
105
130
  /** Stores the pending chain validation status */
106
131
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
107
132
 
@@ -116,10 +141,7 @@ export class BlockStore {
116
141
 
117
142
  #log = createLogger('archiver:block_store');
118
143
 
119
- constructor(
120
- private db: AztecAsyncKVStore,
121
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
122
- ) {
144
+ constructor(private db: AztecAsyncKVStore) {
123
145
  this.#blocks = db.openMap('archiver_blocks');
124
146
  this.#blockTxs = db.openMap('archiver_block_txs');
125
147
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -128,119 +150,108 @@ export class BlockStore {
128
150
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
129
151
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
130
152
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
153
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
131
154
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
132
155
  this.#checkpoints = db.openMap('archiver_checkpoints');
156
+ this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
157
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
133
158
  }
134
159
 
135
160
  /**
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
161
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
162
+ * in an L1 block that has itself been finalized on Ethereum.
140
163
  * @returns The finalized block number.
141
164
  */
142
165
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
143
- const provenBlockNumber = await this.getProvenBlockNumber();
144
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
166
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
167
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
168
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
169
+ }
170
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
171
+ if (!checkpointStorage) {
172
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
173
+ }
174
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
145
175
  }
146
176
 
147
177
  /**
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.
178
+ * Append a new proposed block to the store.
179
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
150
180
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
151
- * @param blocks - The proposed L2 blocks to be added to the store.
181
+ * @param block - The proposed L2 block to be added to the store.
152
182
  * @returns True if the operation is successful.
153
183
  */
154
- async addProposedBlocks(blocks: L2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
155
- if (blocks.length === 0) {
156
- return true;
157
- }
158
-
184
+ async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
159
185
  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;
186
+ const blockNumber = block.number;
187
+ const blockCheckpointNumber = block.checkpointNumber;
188
+ const blockIndex = block.indexWithinCheckpoint;
189
+ const blockLastArchive = block.header.lastArchive.root;
165
190
 
166
191
  // Extract the latest block and checkpoint numbers
167
- const previousBlockNumber = await this.getLatestBlockNumber();
168
- const previousCheckpointNumber = await this.getLatestCheckpointNumber();
192
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
193
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
169
194
 
170
195
  // Verify we're not overwriting checkpointed blocks
171
196
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
172
- if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
173
- throw new CannotOverwriteCheckpointedBlockError(firstBlockNumber, lastCheckpointedBlockNumber);
197
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
198
+ // Check if the proposed block matches the already-checkpointed one
199
+ const existingBlock = await this.getBlock(BlockNumber(blockNumber));
200
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
201
+ throw new BlockAlreadyCheckpointedError(blockNumber);
202
+ }
203
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
174
204
  }
175
205
 
176
- // Check that the first block number is the expected one
177
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
178
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
206
+ // Check that the block number is the expected one
207
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
208
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
179
209
  }
180
210
 
181
- // The same check as above but for checkpoints
182
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
183
- throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
211
+ // Accept the block if either the confirmed checkpoint or a pending checkpoint matches
212
+ // the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
213
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
214
+ const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
215
+ if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
216
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
217
+ const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
218
+ throw new BlockCheckpointNumberNotSequentialError(blockNumber, blockCheckpointNumber, previous);
184
219
  }
185
220
 
186
221
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
187
222
  const previousBlockResult = await this.getBlock(previousBlockNumber);
188
223
 
189
- let expectedFirstblockIndex = 0;
224
+ let expectedBlockIndex = 0;
190
225
  let previousBlockIndex: number | undefined = undefined;
191
226
  if (previousBlockResult !== undefined) {
192
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
227
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
193
228
  // The previous block is for the same checkpoint, therefore our index should follow it
194
229
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
195
- expectedFirstblockIndex = previousBlockIndex + 1;
230
+ expectedBlockIndex = previousBlockIndex + 1;
196
231
  }
197
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
232
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
198
233
  throw new BlockArchiveNotConsistentError(
199
- firstBlockNumber,
234
+ blockNumber,
200
235
  previousBlockResult.number,
201
- firstBlockLastArchive,
236
+ blockLastArchive,
202
237
  previousBlockResult.archive.root,
203
238
  );
204
239
  }
205
240
  }
206
241
 
207
- // Now check that the first block has the expected index value
208
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
209
- throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
242
+ // Now check that the block has the expected index value
243
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
244
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
210
245
  }
211
246
 
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
- }
247
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
237
248
 
238
249
  return true;
239
250
  });
240
251
  }
241
252
 
242
253
  /**
243
- * Append new cheskpoints to the store's list.
254
+ * Append new checkpoints to the store's list.
244
255
  * @param checkpoints - The L2 checkpoints to be added to the store.
245
256
  * @returns True if the operation is successful.
246
257
  */
@@ -250,37 +261,29 @@ export class BlockStore {
250
261
  }
251
262
 
252
263
  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
264
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
255
265
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
256
266
 
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);
267
+ // Handle already-stored checkpoints at the start of the batch.
268
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
269
+ // We accept them if archives match (same content) and update their L1 metadata.
270
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
271
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
272
+ if (checkpoints.length === 0) {
273
+ return true;
268
274
  }
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);
275
+ // Re-check sequentiality after skipping
276
+ const newFirstNumber = checkpoints[0].checkpoint.number;
277
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
278
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
281
279
  }
280
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
281
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
282
282
  }
283
283
 
284
+ // Get the last block of the previous checkpoint for archive chaining
285
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
286
+
284
287
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
285
288
  let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
286
289
  for (const checkpoint of checkpoints) {
@@ -296,53 +299,33 @@ export class BlockStore {
296
299
  }
297
300
  previousCheckpoint = checkpoint;
298
301
 
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
- }
302
+ // Validate block sequencing, indexes, and archive chaining
303
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
331
304
 
332
- previousBlock = block;
333
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
305
+ // Store every block in the database (may already exist, but L1 data is authoritative)
306
+ for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
307
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
334
308
  }
309
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
335
310
 
336
311
  // Store the checkpoint in the database
337
312
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
338
313
  header: checkpoint.checkpoint.header.toBuffer(),
339
314
  archive: checkpoint.checkpoint.archive.toBuffer(),
315
+ checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
340
316
  l1: checkpoint.l1.toBuffer(),
341
317
  attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
342
318
  checkpointNumber: checkpoint.checkpoint.number,
343
319
  startBlock: checkpoint.checkpoint.blocks[0].number,
344
- numBlocks: checkpoint.checkpoint.blocks.length,
320
+ blockCount: checkpoint.checkpoint.blocks.length,
321
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString(),
345
322
  });
323
+
324
+ // Update slot-to-checkpoint index
325
+ await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
326
+
327
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
328
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
346
329
  }
347
330
 
348
331
  await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
@@ -350,6 +333,116 @@ export class BlockStore {
350
333
  });
351
334
  }
352
335
 
336
+ /**
337
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
338
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
339
+ */
340
+ private async skipOrUpdateAlreadyStoredCheckpoints(
341
+ checkpoints: PublishedCheckpoint[],
342
+ latestStored: CheckpointNumber,
343
+ ): Promise<PublishedCheckpoint[]> {
344
+ let i = 0;
345
+ for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
346
+ const incoming = checkpoints[i];
347
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
348
+ if (!stored) {
349
+ // Should not happen if latestStored is correct, but be safe
350
+ break;
351
+ }
352
+ // Verify the checkpoint content matches (archive root)
353
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
354
+ throw new Error(
355
+ `Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
356
+ `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
357
+ );
358
+ }
359
+ // Update L1 metadata and attestations for the already-stored checkpoint
360
+ this.#log.warn(
361
+ `Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
362
+ `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
363
+ );
364
+ await this.#checkpoints.set(incoming.checkpoint.number, {
365
+ header: incoming.checkpoint.header.toBuffer(),
366
+ archive: incoming.checkpoint.archive.toBuffer(),
367
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
368
+ l1: incoming.l1.toBuffer(),
369
+ attestations: incoming.attestations.map(a => a.toBuffer()),
370
+ checkpointNumber: incoming.checkpoint.number,
371
+ startBlock: incoming.checkpoint.blocks[0].number,
372
+ blockCount: incoming.checkpoint.blocks.length,
373
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString(),
374
+ });
375
+ // Update the sync point to reflect the new L1 block
376
+ await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
377
+ }
378
+ return checkpoints.slice(i);
379
+ }
380
+
381
+ /**
382
+ * Gets the last block of the checkpoint before the given one.
383
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
384
+ */
385
+ private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
386
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
387
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
388
+ return undefined;
389
+ }
390
+
391
+ // Check across both proposed and mined checkpoints
392
+ const predecessor =
393
+ (await this.getProposedCheckpointByNumber(previousCheckpointNumber)) ??
394
+ (await this.getCheckpointData(previousCheckpointNumber));
395
+
396
+ if (!predecessor) {
397
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
398
+ }
399
+
400
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
401
+ const previousBlock = await this.getBlock(previousBlockNumber);
402
+ if (previousBlock === undefined) {
403
+ throw new BlockNotFoundError(previousBlockNumber);
404
+ }
405
+ return previousBlock;
406
+ }
407
+
408
+ /**
409
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
410
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
411
+ * proposed checkpoints (addProposedCheckpoint).
412
+ */
413
+ private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
414
+ for (const block of blocks) {
415
+ if (previousBlock) {
416
+ if (previousBlock.number !== block.number - 1) {
417
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
418
+ }
419
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
420
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
421
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
422
+ }
423
+ } else if (block.indexWithinCheckpoint !== 0) {
424
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
425
+ }
426
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
427
+ throw new BlockArchiveNotConsistentError(
428
+ block.number,
429
+ previousBlock.number,
430
+ block.header.lastArchive.root,
431
+ previousBlock.archive.root,
432
+ );
433
+ }
434
+ } else {
435
+ if (block.indexWithinCheckpoint !== 0) {
436
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
437
+ }
438
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
439
+ throw new BlockNumberNotSequentialError(block.number, undefined);
440
+ }
441
+ }
442
+ previousBlock = block;
443
+ }
444
+ }
445
+
353
446
  private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
354
447
  const blockHash = await block.hash();
355
448
 
@@ -425,7 +518,7 @@ export class BlockStore {
425
518
  if (!targetCheckpoint) {
426
519
  throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
427
520
  }
428
- lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.numBlocks - 1);
521
+ lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
429
522
  }
430
523
 
431
524
  // Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
@@ -433,10 +526,18 @@ export class BlockStore {
433
526
 
434
527
  // Remove all checkpoints after the target
435
528
  for (let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)) {
529
+ const checkpointStorage = await this.#checkpoints.getAsync(c);
530
+ if (checkpointStorage) {
531
+ const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
532
+ await this.#slotToCheckpoint.delete(slotNumber);
533
+ }
436
534
  await this.#checkpoints.delete(c);
437
535
  this.#log.debug(`Removed checkpoint ${c}`);
438
536
  }
439
537
 
538
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
539
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
540
+
440
541
  return { blocksRemoved };
441
542
  });
442
543
  }
@@ -461,17 +562,33 @@ export class BlockStore {
461
562
  return checkpoints;
462
563
  }
463
564
 
464
- private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage) {
465
- const data: CheckpointData = {
565
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
566
+ async getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
567
+ const result: CheckpointData[] = [];
568
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
569
+ start: startSlot,
570
+ end: endSlot + 1,
571
+ })) {
572
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
573
+ if (checkpointStorage) {
574
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
575
+ }
576
+ }
577
+ return result;
578
+ }
579
+
580
+ private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage): CheckpointData {
581
+ return {
466
582
  header: CheckpointHeader.fromBuffer(checkpointStorage.header),
467
583
  archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
584
+ checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
468
585
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
469
- startBlock: checkpointStorage.startBlock,
470
- numBlocks: checkpointStorage.numBlocks,
586
+ startBlock: BlockNumber(checkpointStorage.startBlock),
587
+ blockCount: checkpointStorage.blockCount,
588
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
471
589
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
472
- attestations: checkpointStorage.attestations,
590
+ attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
473
591
  };
474
- return data;
475
592
  }
476
593
 
477
594
  async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
@@ -483,7 +600,7 @@ export class BlockStore {
483
600
  const blocksForCheckpoint = await toArray(
484
601
  this.#blocks.entriesAsync({
485
602
  start: checkpoint.startBlock,
486
- end: checkpoint.startBlock + checkpoint.numBlocks,
603
+ end: checkpoint.startBlock + checkpoint.blockCount,
487
604
  }),
488
605
  );
489
606
 
@@ -527,7 +644,7 @@ export class BlockStore {
527
644
  const removedBlocks: L2Block[] = [];
528
645
 
529
646
  // Get the latest block number to determine the range
530
- const latestBlockNumber = await this.getLatestBlockNumber();
647
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
531
648
 
532
649
  // Iterate from blockNumber + 1 to latestBlockNumber
533
650
  for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
@@ -556,17 +673,10 @@ export class BlockStore {
556
673
  if (!checkpointStorage) {
557
674
  throw new CheckpointNotFoundError(provenCheckpointNumber);
558
675
  } else {
559
- return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
676
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
560
677
  }
561
678
  }
562
679
 
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
680
  async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
571
681
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
572
682
  if (latestCheckpointNumber === undefined) {
@@ -575,6 +685,167 @@ export class BlockStore {
575
685
  return CheckpointNumber(latestCheckpointNumber);
576
686
  }
577
687
 
688
+ async hasProposedCheckpoint(): Promise<boolean> {
689
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ limit: 1 }));
690
+ return key !== undefined;
691
+ }
692
+
693
+ /** Deletes all pending proposed checkpoints from storage. */
694
+ async deleteProposedCheckpoints(): Promise<void> {
695
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
696
+ await this.#proposedCheckpoints.delete(key);
697
+ }
698
+ }
699
+
700
+ /**
701
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
702
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
703
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
704
+ * @param checkpointNumber - The checkpoint number to promote.
705
+ * @param l1 - L1 published data for the checkpoint.
706
+ * @param attestations - Committee attestations.
707
+ * @param expectedArchiveRoot - Archive root guard against races.
708
+ */
709
+ async promoteProposedToCheckpointed(
710
+ checkpointNumber: CheckpointNumber,
711
+ l1: L1PublishedData,
712
+ attestations: CommitteeAttestation[],
713
+ expectedArchiveRoot: Fr,
714
+ ): Promise<void> {
715
+ return await this.db.transactionAsync(async () => {
716
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
717
+ if (!proposed) {
718
+ throw new NoProposedCheckpointToPromoteError();
719
+ }
720
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
721
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
722
+ }
723
+
724
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
725
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
726
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
727
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
728
+ }
729
+
730
+ // Write the checkpoint entry
731
+ await this.#checkpoints.set(proposed.checkpointNumber, {
732
+ header: proposed.header.toBuffer(),
733
+ archive: proposed.archive.toBuffer(),
734
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
735
+ l1: l1.toBuffer(),
736
+ attestations: attestations.map(attestation => attestation.toBuffer()),
737
+ checkpointNumber: proposed.checkpointNumber,
738
+ startBlock: proposed.startBlock,
739
+ blockCount: proposed.blockCount,
740
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
741
+ });
742
+
743
+ // Update the slot-to-checkpoint index
744
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
745
+
746
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
747
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
748
+
749
+ // Update the last synced L1 block
750
+ await this.#lastSynchedL1Block.set(l1.blockNumber);
751
+ });
752
+ }
753
+
754
+ /**
755
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
756
+ * No fallback to confirmed.
757
+ */
758
+ async getLastProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
759
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
760
+ if (key === undefined) {
761
+ return undefined;
762
+ }
763
+ const stored = await this.#proposedCheckpoints.getAsync(key);
764
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
765
+ }
766
+
767
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */
768
+ async getProposedCheckpointByNumber(n: CheckpointNumber): Promise<ProposedCheckpointData | undefined> {
769
+ const stored = await this.#proposedCheckpoints.getAsync(n);
770
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
771
+ }
772
+
773
+ /** Returns all pending checkpoints in ascending checkpoint-number order. */
774
+ async getProposedCheckpoints(): Promise<ProposedCheckpointData[]> {
775
+ const results: ProposedCheckpointData[] = [];
776
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()) {
777
+ results.push(this.convertToProposedCheckpointData(stored));
778
+ }
779
+ return results;
780
+ }
781
+
782
+ /**
783
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
784
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
785
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
786
+ */
787
+ async evictProposedCheckpointsFrom(fromNumber: CheckpointNumber): Promise<void> {
788
+ const keysToDelete: number[] = [];
789
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
790
+ if (key >= fromNumber) {
791
+ keysToDelete.push(key);
792
+ }
793
+ }
794
+ for (const key of keysToDelete) {
795
+ await this.#proposedCheckpoints.delete(key);
796
+ }
797
+ }
798
+
799
+ /**
800
+ * Gets the checkpoint at the proposed tip:
801
+ * - latest pending checkpoint if any exist
802
+ * - fallsback to latest confirmed checkpoint otherwise
803
+ */
804
+ async getLastCheckpoint(): Promise<CommonCheckpointData | undefined> {
805
+ const latest = await this.getLastProposedCheckpoint();
806
+ if (!latest) {
807
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
808
+ }
809
+ return latest;
810
+ }
811
+
812
+ private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
813
+ return {
814
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
815
+ header: CheckpointHeader.fromBuffer(stored.header),
816
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
817
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
818
+ startBlock: BlockNumber(stored.startBlock),
819
+ blockCount: stored.blockCount,
820
+ totalManaUsed: BigInt(stored.totalManaUsed),
821
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
822
+ };
823
+ }
824
+
825
+ /**
826
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
827
+ * @returns CheckpointNumber
828
+ */
829
+ async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
830
+ const proposed = await this.getLastCheckpoint();
831
+ if (!proposed) {
832
+ return await this.getLatestCheckpointNumber();
833
+ }
834
+ return CheckpointNumber(proposed.checkpointNumber);
835
+ }
836
+
837
+ /**
838
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
839
+ * @returns BlockNumber
840
+ */
841
+ async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
842
+ const proposed = await this.getLastCheckpoint();
843
+ if (!proposed) {
844
+ return await this.getCheckpointedL2BlockNumber();
845
+ }
846
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
847
+ }
848
+
578
849
  async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
579
850
  const blockStorage = await this.#blocks.getAsync(number);
580
851
  if (!blockStorage) {
@@ -602,7 +873,12 @@ export class BlockStore {
602
873
  * @param limit - The number of blocks to return.
603
874
  * @returns The requested L2 blocks
604
875
  */
605
- async *getCheckpointedBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<CheckpointedL2Block> {
876
+ getCheckpointedBlocks(start: BlockNumber, limit: number): Promise<CheckpointedL2Block[]> {
877
+ return toArray(this.iterateCheckpointedBlocks(start, limit));
878
+ }
879
+
880
+ /** Async iterator variant of {@link getCheckpointedBlocks}. */
881
+ async *iterateCheckpointedBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<CheckpointedL2Block> {
606
882
  const checkpointCache = new Map<CheckpointNumber, CheckpointStorage>();
607
883
  for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
608
884
  const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
@@ -646,7 +922,12 @@ export class BlockStore {
646
922
  * @param limit - The number of blocks to return.
647
923
  * @returns The requested L2 blocks
648
924
  */
649
- async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2Block> {
925
+ getBlocks(start: BlockNumber, limit: number): Promise<L2Block[]> {
926
+ return toArray(this.iterateBlocks(start, limit));
927
+ }
928
+
929
+ /** Async iterator variant of {@link getBlocks}. */
930
+ async *iterateBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2Block> {
650
931
  for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
651
932
  const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
652
933
  if (block) {
@@ -655,6 +936,59 @@ export class BlockStore {
655
936
  }
656
937
  }
657
938
 
939
+ /**
940
+ * Gets block metadata (without tx data) by block number.
941
+ * @param blockNumber - The number of the block to return.
942
+ * @returns The requested block data.
943
+ */
944
+ async getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
945
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
946
+ if (!blockStorage || !blockStorage.header) {
947
+ return undefined;
948
+ }
949
+ return this.getBlockDataFromBlockStorage(blockStorage);
950
+ }
951
+
952
+ /**
953
+ * Gets block metadata plus checkpoint-derived context (L1 publish info, attestations) without
954
+ * deserializing tx bodies. When the block's containing checkpoint has not yet been L1-confirmed,
955
+ * `checkpoint` and `l1` are `undefined` and `attestations` is empty.
956
+ */
957
+ async getBlockDataWithCheckpointContext(
958
+ blockNumber: BlockNumber,
959
+ ): Promise<BlockDataWithCheckpointContext | undefined> {
960
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
961
+ if (!blockStorage || !blockStorage.header) {
962
+ return undefined;
963
+ }
964
+ const data = this.getBlockDataFromBlockStorage(blockStorage);
965
+ const checkpointStorage = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
966
+ if (!checkpointStorage) {
967
+ return { data, checkpoint: undefined, l1: undefined, attestations: [] };
968
+ }
969
+ const checkpoint = this.checkpointDataFromCheckpointStorage(checkpointStorage);
970
+ return { data, checkpoint, l1: checkpoint.l1, attestations: checkpoint.attestations };
971
+ }
972
+
973
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */
974
+ async getCheckpointNumberBySlot(slot: SlotNumber): Promise<CheckpointNumber | undefined> {
975
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
976
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
977
+ }
978
+
979
+ /**
980
+ * Gets block metadata (without tx data) by archive root.
981
+ * @param archive - The archive root of the block to return.
982
+ * @returns The requested block data.
983
+ */
984
+ async getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
985
+ const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
986
+ if (blockNumber === undefined) {
987
+ return undefined;
988
+ }
989
+ return this.getBlockData(BlockNumber(blockNumber));
990
+ }
991
+
658
992
  /**
659
993
  * Gets an L2 block.
660
994
  * @param blockNumber - The number of the block to return.
@@ -734,7 +1068,12 @@ export class BlockStore {
734
1068
  * @param limit - The number of blocks to return.
735
1069
  * @returns The requested L2 block headers
736
1070
  */
737
- async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
1071
+ getBlockHeaders(start: BlockNumber, limit: number): Promise<BlockHeader[]> {
1072
+ return toArray(this.iterateBlockHeaders(start, limit));
1073
+ }
1074
+
1075
+ /** Async iterator variant of {@link getBlockHeaders}. */
1076
+ async *iterateBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
738
1077
  for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
739
1078
  const header = BlockHeader.fromBuffer(blockStorage.header);
740
1079
  if (header.getBlockNumber() !== blockNumber) {
@@ -759,15 +1098,24 @@ export class BlockStore {
759
1098
  }
760
1099
  }
761
1100
 
1101
+ private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
1102
+ return {
1103
+ header: BlockHeader.fromBuffer(blockStorage.header),
1104
+ archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
1105
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
1106
+ checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
1107
+ indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
1108
+ };
1109
+ }
1110
+
762
1111
  private async getBlockFromBlockStorage(
763
1112
  blockNumber: number,
764
1113
  blockStorage: BlockStorage,
765
1114
  ): 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);
1115
+ const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } =
1116
+ this.getBlockDataFromBlockStorage(blockStorage);
1117
+ header.setHash(blockHash);
1118
+ const blockHashString = bufferToHex(blockStorage.blockHash);
771
1119
  const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
772
1120
  if (blockTxsBuffer === undefined) {
773
1121
  this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
@@ -786,13 +1134,7 @@ export class BlockStore {
786
1134
  txEffects.push(deserializeIndexedTxEffect(txEffect).data);
787
1135
  }
788
1136
  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
- );
1137
+ const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
796
1138
 
797
1139
  if (block.number !== blockNumber) {
798
1140
  throw new Error(
@@ -822,7 +1164,10 @@ export class BlockStore {
822
1164
  * @param txHash - The hash of a tx we try to get the receipt for.
823
1165
  * @returns The requested tx receipt (or undefined if not found).
824
1166
  */
825
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
1167
+ async getSettledTxReceipt(
1168
+ txHash: TxHash,
1169
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
1170
+ ): Promise<TxReceipt | undefined> {
826
1171
  const txEffect = await this.getTxEffect(txHash);
827
1172
  if (!txEffect) {
828
1173
  return undefined;
@@ -831,10 +1176,11 @@ export class BlockStore {
831
1176
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
832
1177
 
833
1178
  // Use existing archiver methods to determine finalization level
834
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
1179
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
835
1180
  this.getProvenBlockNumber(),
836
1181
  this.getCheckpointedL2BlockNumber(),
837
1182
  this.getFinalizedL2BlockNumber(),
1183
+ this.getBlockData(blockNumber),
838
1184
  ]);
839
1185
 
840
1186
  let status: TxStatus;
@@ -848,6 +1194,9 @@ export class BlockStore {
848
1194
  status = TxStatus.PROPOSED;
849
1195
  }
850
1196
 
1197
+ const epochNumber =
1198
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
1199
+
851
1200
  return new TxReceipt(
852
1201
  txHash,
853
1202
  status,
@@ -856,6 +1205,7 @@ export class BlockStore {
856
1205
  txEffect.data.transactionFee.toBigInt(),
857
1206
  txEffect.l2BlockHash,
858
1207
  blockNumber,
1208
+ epochNumber,
859
1209
  );
860
1210
  }
861
1211
 
@@ -892,7 +1242,7 @@ export class BlockStore {
892
1242
  if (!checkpoint) {
893
1243
  return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
894
1244
  }
895
- return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
1245
+ return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
896
1246
  }
897
1247
 
898
1248
  async getLatestL2BlockNumber(): Promise<BlockNumber> {
@@ -912,6 +1262,52 @@ export class BlockStore {
912
1262
  return this.#lastSynchedL1Block.set(l1BlockNumber);
913
1263
  }
914
1264
 
1265
+ /**
1266
+ * Adds a proposed checkpoint to the pending queue.
1267
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1268
+ * confirmed and the highest pending checkpoint number.
1269
+ * Computes archive and checkpointOutHash from the stored blocks.
1270
+ */
1271
+ async addProposedCheckpoint(proposed: ProposedCheckpointInput) {
1272
+ return await this.db.transactionAsync(async () => {
1273
+ const confirmed = await this.getLatestCheckpointNumber();
1274
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
1275
+ const latestTip = CheckpointNumber(
1276
+ latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed,
1277
+ );
1278
+
1279
+ if (proposed.checkpointNumber !== latestTip + 1) {
1280
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
1281
+ }
1282
+
1283
+ // Ensure the predecessor block (from pending or confirmed chain) exists
1284
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1285
+ const blocks: L2Block[] = [];
1286
+ for (let i = 0; i < proposed.blockCount; i++) {
1287
+ const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1288
+ if (!block) {
1289
+ throw new BlockNotFoundError(proposed.startBlock + i);
1290
+ }
1291
+ blocks.push(block);
1292
+ }
1293
+ this.validateCheckpointBlocks(blocks, previousBlock);
1294
+
1295
+ const archive = blocks[blocks.length - 1].archive;
1296
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1297
+
1298
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
1299
+ header: proposed.header.toBuffer(),
1300
+ archive: archive.toBuffer(),
1301
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1302
+ checkpointNumber: proposed.checkpointNumber,
1303
+ startBlock: proposed.startBlock,
1304
+ blockCount: proposed.blockCount,
1305
+ totalManaUsed: proposed.totalManaUsed.toString(),
1306
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1307
+ });
1308
+ });
1309
+ }
1310
+
915
1311
  async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
916
1312
  const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
917
1313
  this.getLatestCheckpointNumber(),
@@ -927,6 +1323,20 @@ export class BlockStore {
927
1323
  return result;
928
1324
  }
929
1325
 
1326
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
1327
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1328
+ this.getLatestCheckpointNumber(),
1329
+ this.#lastFinalizedCheckpoint.getAsync(),
1330
+ ]);
1331
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
1332
+ ? latestCheckpointNumber
1333
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1334
+ }
1335
+
1336
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1337
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1338
+ }
1339
+
930
1340
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
931
1341
  if (limit < 1) {
932
1342
  throw new Error(`Invalid limit: ${limit}`);