@aztec/archiver 0.0.1-commit.e6bd8901 → 0.0.1-commit.ec7ac5448

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 (116) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +14 -9
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +97 -115
  5. package/dest/config.d.ts +3 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +2 -1
  8. package/dest/errors.d.ts +34 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +45 -16
  11. package/dest/factory.d.ts +5 -4
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +32 -28
  14. package/dest/index.d.ts +2 -1
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -0
  17. package/dest/l1/bin/retrieve-calldata.js +36 -33
  18. package/dest/l1/calldata_retriever.d.ts +73 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +191 -259
  21. package/dest/l1/data_retrieval.d.ts +11 -11
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +36 -35
  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_trace.d.ts +6 -3
  28. package/dest/l1/validate_trace.d.ts.map +1 -1
  29. package/dest/l1/validate_trace.js +13 -9
  30. package/dest/modules/data_source_base.d.ts +17 -10
  31. package/dest/modules/data_source_base.d.ts.map +1 -1
  32. package/dest/modules/data_source_base.js +39 -77
  33. package/dest/modules/data_store_updater.d.ts +25 -12
  34. package/dest/modules/data_store_updater.d.ts.map +1 -1
  35. package/dest/modules/data_store_updater.js +125 -94
  36. package/dest/modules/instrumentation.d.ts +18 -3
  37. package/dest/modules/instrumentation.d.ts.map +1 -1
  38. package/dest/modules/instrumentation.js +53 -18
  39. package/dest/modules/l1_synchronizer.d.ts +7 -9
  40. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  41. package/dest/modules/l1_synchronizer.js +180 -139
  42. package/dest/modules/validation.d.ts +1 -1
  43. package/dest/modules/validation.d.ts.map +1 -1
  44. package/dest/modules/validation.js +2 -2
  45. package/dest/store/block_store.d.ts +69 -31
  46. package/dest/store/block_store.d.ts.map +1 -1
  47. package/dest/store/block_store.js +358 -137
  48. package/dest/store/contract_class_store.d.ts +2 -3
  49. package/dest/store/contract_class_store.d.ts.map +1 -1
  50. package/dest/store/contract_class_store.js +16 -72
  51. package/dest/store/contract_instance_store.d.ts +1 -1
  52. package/dest/store/contract_instance_store.d.ts.map +1 -1
  53. package/dest/store/contract_instance_store.js +6 -2
  54. package/dest/store/kv_archiver_store.d.ts +64 -27
  55. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  56. package/dest/store/kv_archiver_store.js +77 -30
  57. package/dest/store/l2_tips_cache.d.ts +20 -0
  58. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  59. package/dest/store/l2_tips_cache.js +109 -0
  60. package/dest/store/log_store.d.ts +6 -3
  61. package/dest/store/log_store.d.ts.map +1 -1
  62. package/dest/store/log_store.js +150 -53
  63. package/dest/store/message_store.d.ts +5 -1
  64. package/dest/store/message_store.d.ts.map +1 -1
  65. package/dest/store/message_store.js +21 -9
  66. package/dest/test/fake_l1_state.d.ts +21 -1
  67. package/dest/test/fake_l1_state.d.ts.map +1 -1
  68. package/dest/test/fake_l1_state.js +133 -26
  69. package/dest/test/index.js +3 -1
  70. package/dest/test/mock_archiver.d.ts +1 -1
  71. package/dest/test/mock_archiver.d.ts.map +1 -1
  72. package/dest/test/mock_archiver.js +3 -2
  73. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  74. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  75. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  76. package/dest/test/mock_l2_block_source.d.ts +30 -9
  77. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  78. package/dest/test/mock_l2_block_source.js +161 -90
  79. package/dest/test/mock_structs.d.ts +6 -2
  80. package/dest/test/mock_structs.d.ts.map +1 -1
  81. package/dest/test/mock_structs.js +20 -6
  82. package/dest/test/noop_l1_archiver.d.ts +26 -0
  83. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  84. package/dest/test/noop_l1_archiver.js +71 -0
  85. package/package.json +14 -13
  86. package/src/archiver.ts +128 -141
  87. package/src/config.ts +8 -1
  88. package/src/errors.ts +70 -26
  89. package/src/factory.ts +48 -26
  90. package/src/index.ts +1 -0
  91. package/src/l1/README.md +25 -68
  92. package/src/l1/bin/retrieve-calldata.ts +46 -39
  93. package/src/l1/calldata_retriever.ts +250 -379
  94. package/src/l1/data_retrieval.ts +32 -38
  95. package/src/l1/spire_proposer.ts +7 -15
  96. package/src/l1/validate_trace.ts +24 -6
  97. package/src/modules/data_source_base.ts +81 -101
  98. package/src/modules/data_store_updater.ts +138 -124
  99. package/src/modules/instrumentation.ts +63 -19
  100. package/src/modules/l1_synchronizer.ts +204 -174
  101. package/src/modules/validation.ts +2 -2
  102. package/src/store/block_store.ts +456 -177
  103. package/src/store/contract_class_store.ts +16 -110
  104. package/src/store/contract_instance_store.ts +8 -5
  105. package/src/store/kv_archiver_store.ts +120 -46
  106. package/src/store/l2_tips_cache.ts +128 -0
  107. package/src/store/log_store.ts +224 -63
  108. package/src/store/message_store.ts +27 -10
  109. package/src/structs/inbox_message.ts +1 -1
  110. package/src/test/fake_l1_state.ts +178 -30
  111. package/src/test/index.ts +3 -0
  112. package/src/test/mock_archiver.ts +3 -2
  113. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  114. package/src/test/mock_l2_block_source.ts +215 -88
  115. package/src/test/mock_structs.ts +42 -12
  116. package/src/test/noop_l1_archiver.ts +114 -0
@@ -9,17 +9,26 @@ 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
+ BlockHash,
12
14
  Body,
13
15
  CheckpointedL2Block,
14
16
  CommitteeAttestation,
15
17
  L2Block,
16
- L2BlockHash,
17
18
  type ValidateCheckpointResult,
18
19
  deserializeValidateCheckpointResult,
19
20
  serializeValidateCheckpointResult,
20
21
  } from '@aztec/stdlib/block';
21
- import { L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
22
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
22
+ import {
23
+ Checkpoint,
24
+ type CheckpointData,
25
+ type CommonCheckpointData,
26
+ L1PublishedData,
27
+ type ProposedCheckpointData,
28
+ type ProposedCheckpointInput,
29
+ PublishedCheckpoint,
30
+ } from '@aztec/stdlib/checkpoint';
31
+ import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
23
32
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
24
33
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
25
34
  import {
@@ -34,16 +43,17 @@ import {
34
43
  } from '@aztec/stdlib/tx';
35
44
 
36
45
  import {
46
+ BlockAlreadyCheckpointedError,
37
47
  BlockArchiveNotConsistentError,
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
+ ProposedCheckpointNotSequentialError,
56
+ ProposedCheckpointStaleError,
47
57
  } from '../errors.js';
48
58
 
49
59
  export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
@@ -58,24 +68,25 @@ type BlockStorage = {
58
68
  indexWithinCheckpoint: number;
59
69
  };
60
70
 
61
- type CheckpointStorage = {
71
+ /** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
72
+ type CommonCheckpointStorage = {
62
73
  header: Buffer;
63
74
  archive: Buffer;
75
+ checkpointOutHash: Buffer;
64
76
  checkpointNumber: number;
65
77
  startBlock: number;
66
- numBlocks: number;
78
+ blockCount: number;
79
+ };
80
+
81
+ type CheckpointStorage = CommonCheckpointStorage & {
67
82
  l1: Buffer;
68
83
  attestations: Buffer[];
69
84
  };
70
85
 
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[];
86
+ /** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
87
+ type ProposedCheckpointStorage = CommonCheckpointStorage & {
88
+ totalManaUsed: string;
89
+ feeAssetPriceModifier: string;
79
90
  };
80
91
 
81
92
  export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
@@ -90,6 +101,9 @@ export class BlockStore {
90
101
  /** Map checkpoint number to checkpoint data */
91
102
  #checkpoints: AztecAsyncMap<number, CheckpointStorage>;
92
103
 
104
+ /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
105
+ #slotToCheckpoint: AztecAsyncMap<number, number>;
106
+
93
107
  /** Map block hash to list of tx hashes */
94
108
  #blockTxs: AztecAsyncMap<string, Buffer>;
95
109
 
@@ -102,6 +116,9 @@ export class BlockStore {
102
116
  /** Stores last proven checkpoint */
103
117
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
104
118
 
119
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
120
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
121
+
105
122
  /** Stores the pending chain validation status */
106
123
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
107
124
 
@@ -114,12 +131,12 @@ export class BlockStore {
114
131
  /** Index mapping block archive to block number */
115
132
  #blockArchiveIndex: AztecAsyncMap<string, number>;
116
133
 
134
+ /** Singleton: assumes max 1-deep pipeline. For deeper pipelining, replace with a map keyed by checkpoint number. */
135
+ #proposedCheckpoint: AztecAsyncSingleton<ProposedCheckpointStorage>;
136
+
117
137
  #log = createLogger('archiver:block_store');
118
138
 
119
- constructor(
120
- private db: AztecAsyncKVStore,
121
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
122
- ) {
139
+ constructor(private db: AztecAsyncKVStore) {
123
140
  this.#blocks = db.openMap('archiver_blocks');
124
141
  this.#blockTxs = db.openMap('archiver_block_txs');
125
142
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -128,119 +145,114 @@ export class BlockStore {
128
145
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
129
146
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
130
147
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
148
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
131
149
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
132
150
  this.#checkpoints = db.openMap('archiver_checkpoints');
151
+ this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
152
+ this.#proposedCheckpoint = db.openSingleton('proposed_checkpoint_data');
133
153
  }
134
154
 
135
155
  /**
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
156
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
157
+ * in an L1 block that has itself been finalized on Ethereum.
140
158
  * @returns The finalized block number.
141
159
  */
142
160
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
143
- const provenBlockNumber = await this.getProvenBlockNumber();
144
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
161
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
162
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
163
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
164
+ }
165
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
166
+ if (!checkpointStorage) {
167
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
168
+ }
169
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
145
170
  }
146
171
 
147
172
  /**
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.
173
+ * Append a new proposed block to the store.
174
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
150
175
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
151
- * @param blocks - The proposed L2 blocks to be added to the store.
176
+ * @param block - The proposed L2 block to be added to the store.
152
177
  * @returns True if the operation is successful.
153
178
  */
154
- async addProposedBlocks(blocks: L2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
155
- if (blocks.length === 0) {
156
- return true;
157
- }
158
-
179
+ async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
159
180
  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;
181
+ const blockNumber = block.number;
182
+ const blockCheckpointNumber = block.checkpointNumber;
183
+ const blockIndex = block.indexWithinCheckpoint;
184
+ const blockLastArchive = block.header.lastArchive.root;
165
185
 
166
186
  // Extract the latest block and checkpoint numbers
167
- const previousBlockNumber = await this.getLatestBlockNumber();
187
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
188
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
168
189
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
169
190
 
170
191
  // Verify we're not overwriting checkpointed blocks
171
192
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
172
- if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
173
- throw new CannotOverwriteCheckpointedBlockError(firstBlockNumber, lastCheckpointedBlockNumber);
193
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
194
+ // Check if the proposed block matches the already-checkpointed one
195
+ const existingBlock = await this.getBlock(BlockNumber(blockNumber));
196
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
197
+ throw new BlockAlreadyCheckpointedError(blockNumber);
198
+ }
199
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
174
200
  }
175
201
 
176
- // Check that the first block number is the expected one
177
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
178
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
202
+ // Check that the block number is the expected one
203
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
204
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
179
205
  }
180
206
 
181
- // The same check as above but for checkpoints
182
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
183
- throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
207
+ // The same check as above but for checkpoints. Accept the block if either the confirmed
208
+ // checkpoint or the pending (locally validated but not yet confirmed) checkpoint matches.
209
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
210
+ if (
211
+ !opts.force &&
212
+ previousCheckpointNumber !== expectedCheckpointNumber &&
213
+ proposedCheckpointNumber !== expectedCheckpointNumber
214
+ ) {
215
+ const [reported, source]: [CheckpointNumber, 'confirmed' | 'proposed'] =
216
+ proposedCheckpointNumber > previousCheckpointNumber
217
+ ? [proposedCheckpointNumber, 'proposed']
218
+ : [previousCheckpointNumber, 'confirmed'];
219
+ throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
184
220
  }
185
221
 
186
222
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
187
223
  const previousBlockResult = await this.getBlock(previousBlockNumber);
188
224
 
189
- let expectedFirstblockIndex = 0;
225
+ let expectedBlockIndex = 0;
190
226
  let previousBlockIndex: number | undefined = undefined;
191
227
  if (previousBlockResult !== undefined) {
192
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
228
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
193
229
  // The previous block is for the same checkpoint, therefore our index should follow it
194
230
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
195
- expectedFirstblockIndex = previousBlockIndex + 1;
231
+ expectedBlockIndex = previousBlockIndex + 1;
196
232
  }
197
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
233
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
198
234
  throw new BlockArchiveNotConsistentError(
199
- firstBlockNumber,
235
+ blockNumber,
200
236
  previousBlockResult.number,
201
- firstBlockLastArchive,
237
+ blockLastArchive,
202
238
  previousBlockResult.archive.root,
203
239
  );
204
240
  }
205
241
  }
206
242
 
207
- // Now check that the first block has the expected index value
208
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
209
- throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
243
+ // Now check that the block has the expected index value
244
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
245
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
210
246
  }
211
247
 
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
- }
248
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
237
249
 
238
250
  return true;
239
251
  });
240
252
  }
241
253
 
242
254
  /**
243
- * Append new cheskpoints to the store's list.
255
+ * Append new checkpoints to the store's list.
244
256
  * @param checkpoints - The L2 checkpoints to be added to the store.
245
257
  * @returns True if the operation is successful.
246
258
  */
@@ -250,37 +262,29 @@ export class BlockStore {
250
262
  }
251
263
 
252
264
  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
265
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
255
266
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
256
267
 
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);
268
+ // Handle already-stored checkpoints at the start of the batch.
269
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
270
+ // We accept them if archives match (same content) and update their L1 metadata.
271
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
272
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
273
+ if (checkpoints.length === 0) {
274
+ return true;
268
275
  }
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);
276
+ // Re-check sequentiality after skipping
277
+ const newFirstNumber = checkpoints[0].checkpoint.number;
278
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
279
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
281
280
  }
281
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
282
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
282
283
  }
283
284
 
285
+ // Get the last block of the previous checkpoint for archive chaining
286
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
287
+
284
288
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
285
289
  let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
286
290
  for (const checkpoint of checkpoints) {
@@ -296,62 +300,148 @@ export class BlockStore {
296
300
  }
297
301
  previousCheckpoint = checkpoint;
298
302
 
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
- }
303
+ // Validate block sequencing, indexes, and archive chaining
304
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
331
305
 
332
- previousBlock = block;
333
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
306
+ // Store every block in the database (may already exist, but L1 data is authoritative)
307
+ for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
308
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
334
309
  }
310
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
335
311
 
336
312
  // Store the checkpoint in the database
337
313
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
338
314
  header: checkpoint.checkpoint.header.toBuffer(),
339
315
  archive: checkpoint.checkpoint.archive.toBuffer(),
316
+ checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
340
317
  l1: checkpoint.l1.toBuffer(),
341
318
  attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
342
319
  checkpointNumber: checkpoint.checkpoint.number,
343
320
  startBlock: checkpoint.checkpoint.blocks[0].number,
344
- numBlocks: checkpoint.checkpoint.blocks.length,
321
+ blockCount: checkpoint.checkpoint.blocks.length,
345
322
  });
323
+
324
+ // Update slot-to-checkpoint index
325
+ await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
346
326
  }
347
327
 
328
+ // Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
329
+ const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
330
+ await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
331
+
348
332
  await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
349
333
  return true;
350
334
  });
351
335
  }
352
336
 
337
+ /**
338
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
339
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
340
+ */
341
+ private async skipOrUpdateAlreadyStoredCheckpoints(
342
+ checkpoints: PublishedCheckpoint[],
343
+ latestStored: CheckpointNumber,
344
+ ): Promise<PublishedCheckpoint[]> {
345
+ let i = 0;
346
+ for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
347
+ const incoming = checkpoints[i];
348
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
349
+ if (!stored) {
350
+ // Should not happen if latestStored is correct, but be safe
351
+ break;
352
+ }
353
+ // Verify the checkpoint content matches (archive root)
354
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
355
+ throw new Error(
356
+ `Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
357
+ `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
358
+ );
359
+ }
360
+ // Update L1 metadata and attestations for the already-stored checkpoint
361
+ this.#log.warn(
362
+ `Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
363
+ `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
364
+ );
365
+ await this.#checkpoints.set(incoming.checkpoint.number, {
366
+ header: incoming.checkpoint.header.toBuffer(),
367
+ archive: incoming.checkpoint.archive.toBuffer(),
368
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
369
+ l1: incoming.l1.toBuffer(),
370
+ attestations: incoming.attestations.map(a => a.toBuffer()),
371
+ checkpointNumber: incoming.checkpoint.number,
372
+ startBlock: incoming.checkpoint.blocks[0].number,
373
+ blockCount: incoming.checkpoint.blocks.length,
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
+ const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
392
+ if (previousCheckpointData === undefined) {
393
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
394
+ }
395
+
396
+ const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
397
+ const previousBlock = await this.getBlock(previousBlockNumber);
398
+ if (previousBlock === undefined) {
399
+ throw new BlockNotFoundError(previousBlockNumber);
400
+ }
401
+
402
+ return previousBlock;
403
+ }
404
+
405
+ /**
406
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
407
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
408
+ * proposed checkpoints (setProposedCheckpoint).
409
+ */
410
+ private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
411
+ for (const block of blocks) {
412
+ if (previousBlock) {
413
+ if (previousBlock.number !== block.number - 1) {
414
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
415
+ }
416
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
417
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
418
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
419
+ }
420
+ } else if (block.indexWithinCheckpoint !== 0) {
421
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
422
+ }
423
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
424
+ throw new BlockArchiveNotConsistentError(
425
+ block.number,
426
+ previousBlock.number,
427
+ block.header.lastArchive.root,
428
+ previousBlock.archive.root,
429
+ );
430
+ }
431
+ } else {
432
+ if (block.indexWithinCheckpoint !== 0) {
433
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
434
+ }
435
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
436
+ throw new BlockNumberNotSequentialError(block.number, undefined);
437
+ }
438
+ }
439
+ previousBlock = block;
440
+ }
441
+ }
442
+
353
443
  private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
354
- const blockHash = L2BlockHash.fromField(await block.hash());
444
+ const blockHash = await block.hash();
355
445
 
356
446
  await this.#blocks.set(block.number, {
357
447
  header: block.header.toBuffer(),
@@ -425,7 +515,7 @@ export class BlockStore {
425
515
  if (!targetCheckpoint) {
426
516
  throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
427
517
  }
428
- lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.numBlocks - 1);
518
+ lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
429
519
  }
430
520
 
431
521
  // Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
@@ -433,10 +523,21 @@ export class BlockStore {
433
523
 
434
524
  // Remove all checkpoints after the target
435
525
  for (let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)) {
526
+ const checkpointStorage = await this.#checkpoints.getAsync(c);
527
+ if (checkpointStorage) {
528
+ const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
529
+ await this.#slotToCheckpoint.delete(slotNumber);
530
+ }
436
531
  await this.#checkpoints.delete(c);
437
532
  this.#log.debug(`Removed checkpoint ${c}`);
438
533
  }
439
534
 
535
+ // Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
536
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
537
+ if (proposedCheckpointNumber > checkpointNumber) {
538
+ await this.#proposedCheckpoint.delete();
539
+ }
540
+
440
541
  return { blocksRemoved };
441
542
  });
442
543
  }
@@ -461,17 +562,32 @@ 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,
471
588
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
472
- attestations: checkpointStorage.attestations,
589
+ attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
473
590
  };
474
- return data;
475
591
  }
476
592
 
477
593
  async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
@@ -483,7 +599,7 @@ export class BlockStore {
483
599
  const blocksForCheckpoint = await toArray(
484
600
  this.#blocks.entriesAsync({
485
601
  start: checkpoint.startBlock,
486
- end: checkpoint.startBlock + checkpoint.numBlocks,
602
+ end: checkpoint.startBlock + checkpoint.blockCount,
487
603
  }),
488
604
  );
489
605
 
@@ -527,7 +643,7 @@ export class BlockStore {
527
643
  const removedBlocks: L2Block[] = [];
528
644
 
529
645
  // Get the latest block number to determine the range
530
- const latestBlockNumber = await this.getLatestBlockNumber();
646
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
531
647
 
532
648
  // Iterate from blockNumber + 1 to latestBlockNumber
533
649
  for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
@@ -556,17 +672,10 @@ export class BlockStore {
556
672
  if (!checkpointStorage) {
557
673
  throw new CheckpointNotFoundError(provenCheckpointNumber);
558
674
  } else {
559
- return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
675
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
560
676
  }
561
677
  }
562
678
 
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
679
  async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
571
680
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
572
681
  if (latestCheckpointNumber === undefined) {
@@ -575,6 +684,84 @@ export class BlockStore {
575
684
  return CheckpointNumber(latestCheckpointNumber);
576
685
  }
577
686
 
687
+ async hasProposedCheckpoint(): Promise<boolean> {
688
+ const proposed = await this.#proposedCheckpoint.getAsync();
689
+ return proposed !== undefined;
690
+ }
691
+
692
+ /** Deletes the proposed checkpoint from storage. */
693
+ async deleteProposedCheckpoint(): Promise<void> {
694
+ await this.#proposedCheckpoint.delete();
695
+ }
696
+
697
+ /** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */
698
+ async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber: CheckpointNumber): Promise<void> {
699
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
700
+ if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
701
+ await this.#proposedCheckpoint.delete();
702
+ }
703
+ }
704
+
705
+ /** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */
706
+ async getProposedCheckpointOnly(): Promise<ProposedCheckpointData | undefined> {
707
+ const stored = await this.#proposedCheckpoint.getAsync();
708
+ if (!stored) {
709
+ return undefined;
710
+ }
711
+ return this.convertToProposedCheckpointData(stored);
712
+ }
713
+
714
+ /**
715
+ * Gets the checkpoint at the proposed tip
716
+ * - pending checkpoint if it exists
717
+ * - fallsback to latest confirmed checkpoint otherwise
718
+ * @returns CommonCheckpointData
719
+ */
720
+ async getProposedCheckpoint(): Promise<CommonCheckpointData | undefined> {
721
+ const stored = await this.#proposedCheckpoint.getAsync();
722
+ if (!stored) {
723
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
724
+ }
725
+ return this.convertToProposedCheckpointData(stored);
726
+ }
727
+
728
+ private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
729
+ return {
730
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
731
+ header: CheckpointHeader.fromBuffer(stored.header),
732
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
733
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
734
+ startBlock: BlockNumber(stored.startBlock),
735
+ blockCount: stored.blockCount,
736
+ totalManaUsed: BigInt(stored.totalManaUsed),
737
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
738
+ };
739
+ }
740
+
741
+ /**
742
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
743
+ * @returns CheckpointNumber
744
+ */
745
+ async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
746
+ const proposed = await this.getProposedCheckpoint();
747
+ if (!proposed) {
748
+ return await this.getLatestCheckpointNumber();
749
+ }
750
+ return CheckpointNumber(proposed.checkpointNumber);
751
+ }
752
+
753
+ /**
754
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
755
+ * @returns BlockNumber
756
+ */
757
+ async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
758
+ const proposed = await this.getProposedCheckpoint();
759
+ if (!proposed) {
760
+ return await this.getCheckpointedL2BlockNumber();
761
+ }
762
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
763
+ }
764
+
578
765
  async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
579
766
  const blockStorage = await this.#blocks.getAsync(number);
580
767
  if (!blockStorage) {
@@ -624,7 +811,7 @@ export class BlockStore {
624
811
  }
625
812
  }
626
813
 
627
- async getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
814
+ async getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
628
815
  const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
629
816
  if (blockNumber === undefined) {
630
817
  return undefined;
@@ -655,6 +842,32 @@ export class BlockStore {
655
842
  }
656
843
  }
657
844
 
845
+ /**
846
+ * Gets block metadata (without tx data) by block number.
847
+ * @param blockNumber - The number of the block to return.
848
+ * @returns The requested block data.
849
+ */
850
+ async getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
851
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
852
+ if (!blockStorage || !blockStorage.header) {
853
+ return undefined;
854
+ }
855
+ return this.getBlockDataFromBlockStorage(blockStorage);
856
+ }
857
+
858
+ /**
859
+ * Gets block metadata (without tx data) by archive root.
860
+ * @param archive - The archive root of the block to return.
861
+ * @returns The requested block data.
862
+ */
863
+ async getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
864
+ const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
865
+ if (blockNumber === undefined) {
866
+ return undefined;
867
+ }
868
+ return this.getBlockData(BlockNumber(blockNumber));
869
+ }
870
+
658
871
  /**
659
872
  * Gets an L2 block.
660
873
  * @param blockNumber - The number of the block to return.
@@ -673,7 +886,7 @@ export class BlockStore {
673
886
  * @param blockHash - The hash of the block to return.
674
887
  * @returns The requested L2 block.
675
888
  */
676
- async getBlockByHash(blockHash: L2BlockHash): Promise<L2Block | undefined> {
889
+ async getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
677
890
  const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
678
891
  if (blockNumber === undefined) {
679
892
  return undefined;
@@ -699,7 +912,7 @@ export class BlockStore {
699
912
  * @param blockHash - The hash of the block to return.
700
913
  * @returns The requested block header.
701
914
  */
702
- async getBlockHeaderByHash(blockHash: L2BlockHash): Promise<BlockHeader | undefined> {
915
+ async getBlockHeaderByHash(blockHash: BlockHash): Promise<BlockHeader | undefined> {
703
916
  const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
704
917
  if (blockNumber === undefined) {
705
918
  return undefined;
@@ -759,15 +972,24 @@ export class BlockStore {
759
972
  }
760
973
  }
761
974
 
975
+ private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
976
+ return {
977
+ header: BlockHeader.fromBuffer(blockStorage.header),
978
+ archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
979
+ blockHash: Fr.fromBuffer(blockStorage.blockHash),
980
+ checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
981
+ indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
982
+ };
983
+ }
984
+
762
985
  private async getBlockFromBlockStorage(
763
986
  blockNumber: number,
764
987
  blockStorage: BlockStorage,
765
988
  ): 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);
989
+ const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } =
990
+ this.getBlockDataFromBlockStorage(blockStorage);
991
+ header.setHash(blockHash);
992
+ const blockHashString = bufferToHex(blockStorage.blockHash);
771
993
  const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
772
994
  if (blockTxsBuffer === undefined) {
773
995
  this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
@@ -786,13 +1008,7 @@ export class BlockStore {
786
1008
  txEffects.push(deserializeIndexedTxEffect(txEffect).data);
787
1009
  }
788
1010
  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
- );
1011
+ const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
796
1012
 
797
1013
  if (block.number !== blockNumber) {
798
1014
  throw new Error(
@@ -822,7 +1038,10 @@ export class BlockStore {
822
1038
  * @param txHash - The hash of a tx we try to get the receipt for.
823
1039
  * @returns The requested tx receipt (or undefined if not found).
824
1040
  */
825
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
1041
+ async getSettledTxReceipt(
1042
+ txHash: TxHash,
1043
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
1044
+ ): Promise<TxReceipt | undefined> {
826
1045
  const txEffect = await this.getTxEffect(txHash);
827
1046
  if (!txEffect) {
828
1047
  return undefined;
@@ -831,10 +1050,11 @@ export class BlockStore {
831
1050
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
832
1051
 
833
1052
  // Use existing archiver methods to determine finalization level
834
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
1053
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
835
1054
  this.getProvenBlockNumber(),
836
1055
  this.getCheckpointedL2BlockNumber(),
837
1056
  this.getFinalizedL2BlockNumber(),
1057
+ this.getBlockData(blockNumber),
838
1058
  ]);
839
1059
 
840
1060
  let status: TxStatus;
@@ -848,6 +1068,9 @@ export class BlockStore {
848
1068
  status = TxStatus.PROPOSED;
849
1069
  }
850
1070
 
1071
+ const epochNumber =
1072
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
1073
+
851
1074
  return new TxReceipt(
852
1075
  txHash,
853
1076
  status,
@@ -856,6 +1079,7 @@ export class BlockStore {
856
1079
  txEffect.data.transactionFee.toBigInt(),
857
1080
  txEffect.l2BlockHash,
858
1081
  blockNumber,
1082
+ epochNumber,
859
1083
  );
860
1084
  }
861
1085
 
@@ -892,7 +1116,7 @@ export class BlockStore {
892
1116
  if (!checkpoint) {
893
1117
  return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
894
1118
  }
895
- return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
1119
+ return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
896
1120
  }
897
1121
 
898
1122
  async getLatestL2BlockNumber(): Promise<BlockNumber> {
@@ -912,6 +1136,47 @@ export class BlockStore {
912
1136
  return this.#lastSynchedL1Block.set(l1BlockNumber);
913
1137
  }
914
1138
 
1139
+ /** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
1140
+ * Computes archive and checkpointOutHash from the stored blocks. */
1141
+ async setProposedCheckpoint(proposed: ProposedCheckpointInput) {
1142
+ return await this.db.transactionAsync(async () => {
1143
+ const current = await this.getProposedCheckpointNumber();
1144
+ if (proposed.checkpointNumber <= current) {
1145
+ throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
1146
+ }
1147
+ const confirmed = await this.getLatestCheckpointNumber();
1148
+ if (proposed.checkpointNumber !== confirmed + 1) {
1149
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
1150
+ }
1151
+
1152
+ // Ensure the previous checkpoint + blocks exist
1153
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1154
+ const blocks: L2Block[] = [];
1155
+ for (let i = 0; i < proposed.blockCount; i++) {
1156
+ const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1157
+ if (!block) {
1158
+ throw new BlockNotFoundError(proposed.startBlock + i);
1159
+ }
1160
+ blocks.push(block);
1161
+ }
1162
+ this.validateCheckpointBlocks(blocks, previousBlock);
1163
+
1164
+ const archive = blocks[blocks.length - 1].archive;
1165
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1166
+
1167
+ await this.#proposedCheckpoint.set({
1168
+ header: proposed.header.toBuffer(),
1169
+ archive: archive.toBuffer(),
1170
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1171
+ checkpointNumber: proposed.checkpointNumber,
1172
+ startBlock: proposed.startBlock,
1173
+ blockCount: proposed.blockCount,
1174
+ totalManaUsed: proposed.totalManaUsed.toString(),
1175
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1176
+ });
1177
+ });
1178
+ }
1179
+
915
1180
  async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
916
1181
  const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
917
1182
  this.getLatestCheckpointNumber(),
@@ -927,6 +1192,20 @@ export class BlockStore {
927
1192
  return result;
928
1193
  }
929
1194
 
1195
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
1196
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1197
+ this.getLatestCheckpointNumber(),
1198
+ this.#lastFinalizedCheckpoint.getAsync(),
1199
+ ]);
1200
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
1201
+ ? latestCheckpointNumber
1202
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1203
+ }
1204
+
1205
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1206
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1207
+ }
1208
+
930
1209
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
931
1210
  if (limit < 1) {
932
1211
  throw new Error(`Invalid limit: ${limit}`);