@aztec/archiver 0.0.1-commit.934299a21 → 0.0.1-commit.949a33fd8

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 (103) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +18 -10
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +146 -57
  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 +4 -5
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +26 -22
  14. package/dest/index.d.ts +3 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +2 -1
  17. package/dest/l1/calldata_retriever.d.ts +2 -1
  18. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  19. package/dest/l1/calldata_retriever.js +11 -5
  20. package/dest/l1/data_retrieval.d.ts +24 -12
  21. package/dest/l1/data_retrieval.d.ts.map +1 -1
  22. package/dest/l1/data_retrieval.js +36 -37
  23. package/dest/l1/validate_historical_logs.d.ts +23 -0
  24. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  25. package/dest/l1/validate_historical_logs.js +108 -0
  26. package/dest/modules/data_source_base.d.ts +12 -6
  27. package/dest/modules/data_source_base.d.ts.map +1 -1
  28. package/dest/modules/data_source_base.js +24 -6
  29. package/dest/modules/data_store_updater.d.ts +28 -15
  30. package/dest/modules/data_store_updater.d.ts.map +1 -1
  31. package/dest/modules/data_store_updater.js +102 -80
  32. package/dest/modules/instrumentation.d.ts +7 -2
  33. package/dest/modules/instrumentation.d.ts.map +1 -1
  34. package/dest/modules/instrumentation.js +22 -6
  35. package/dest/modules/l1_synchronizer.d.ts +8 -2
  36. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  37. package/dest/modules/l1_synchronizer.js +292 -144
  38. package/dest/modules/validation.d.ts +4 -3
  39. package/dest/modules/validation.d.ts.map +1 -1
  40. package/dest/modules/validation.js +6 -6
  41. package/dest/store/block_store.d.ts +83 -17
  42. package/dest/store/block_store.d.ts.map +1 -1
  43. package/dest/store/block_store.js +399 -124
  44. package/dest/store/contract_class_store.d.ts +2 -3
  45. package/dest/store/contract_class_store.d.ts.map +1 -1
  46. package/dest/store/contract_class_store.js +7 -67
  47. package/dest/store/contract_instance_store.d.ts +1 -1
  48. package/dest/store/contract_instance_store.d.ts.map +1 -1
  49. package/dest/store/contract_instance_store.js +6 -2
  50. package/dest/store/kv_archiver_store.d.ts +70 -23
  51. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  52. package/dest/store/kv_archiver_store.js +88 -27
  53. package/dest/store/l2_tips_cache.d.ts +2 -1
  54. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  55. package/dest/store/l2_tips_cache.js +27 -7
  56. package/dest/store/log_store.d.ts +6 -3
  57. package/dest/store/log_store.d.ts.map +1 -1
  58. package/dest/store/log_store.js +95 -20
  59. package/dest/store/message_store.d.ts +5 -1
  60. package/dest/store/message_store.d.ts.map +1 -1
  61. package/dest/store/message_store.js +21 -9
  62. package/dest/test/fake_l1_state.d.ts +20 -1
  63. package/dest/test/fake_l1_state.d.ts.map +1 -1
  64. package/dest/test/fake_l1_state.js +114 -18
  65. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  66. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  67. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  68. package/dest/test/mock_l2_block_source.d.ts +19 -4
  69. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  70. package/dest/test/mock_l2_block_source.js +57 -7
  71. package/dest/test/mock_structs.d.ts +4 -1
  72. package/dest/test/mock_structs.d.ts.map +1 -1
  73. package/dest/test/mock_structs.js +13 -1
  74. package/dest/test/noop_l1_archiver.d.ts +4 -1
  75. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  76. package/dest/test/noop_l1_archiver.js +9 -3
  77. package/package.json +13 -13
  78. package/src/archiver.ts +179 -56
  79. package/src/config.ts +23 -2
  80. package/src/errors.ts +133 -22
  81. package/src/factory.ts +24 -15
  82. package/src/index.ts +2 -1
  83. package/src/l1/calldata_retriever.ts +17 -5
  84. package/src/l1/data_retrieval.ts +52 -53
  85. package/src/l1/validate_historical_logs.ts +140 -0
  86. package/src/modules/data_source_base.ts +43 -7
  87. package/src/modules/data_store_updater.ts +126 -109
  88. package/src/modules/instrumentation.ts +27 -7
  89. package/src/modules/l1_synchronizer.ts +371 -178
  90. package/src/modules/validation.ts +10 -9
  91. package/src/store/block_store.ts +489 -143
  92. package/src/store/contract_class_store.ts +8 -106
  93. package/src/store/contract_instance_store.ts +8 -5
  94. package/src/store/kv_archiver_store.ts +133 -39
  95. package/src/store/l2_tips_cache.ts +58 -13
  96. package/src/store/log_store.ts +128 -32
  97. package/src/store/message_store.ts +27 -10
  98. package/src/structs/inbox_message.ts +1 -1
  99. package/src/test/fake_l1_state.ts +142 -29
  100. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  101. package/src/test/mock_l2_block_source.ts +73 -5
  102. package/src/test/mock_structs.ts +20 -6
  103. package/src/test/noop_l1_archiver.ts +10 -2
@@ -10,6 +10,7 @@ import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } fro
10
10
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
11
11
  import {
12
12
  type BlockData,
13
+ type BlockDataWithCheckpointContext,
13
14
  BlockHash,
14
15
  Body,
15
16
  CheckpointedL2Block,
@@ -19,8 +20,16 @@ import {
19
20
  deserializeValidateCheckpointResult,
20
21
  serializeValidateCheckpointResult,
21
22
  } from '@aztec/stdlib/block';
22
- import { type CheckpointData, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
23
- 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';
24
33
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
25
34
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
26
35
  import {
@@ -35,16 +44,20 @@ import {
35
44
  } from '@aztec/stdlib/tx';
36
45
 
37
46
  import {
47
+ BlockAlreadyCheckpointedError,
38
48
  BlockArchiveNotConsistentError,
49
+ BlockCheckpointNumberNotSequentialError,
39
50
  BlockIndexNotSequentialError,
40
51
  BlockNotFoundError,
41
52
  BlockNumberNotSequentialError,
42
53
  CannotOverwriteCheckpointedBlockError,
43
54
  CheckpointNotFoundError,
44
- CheckpointNumberNotConsistentError,
45
55
  CheckpointNumberNotSequentialError,
46
- InitialBlockNumberNotSequentialError,
47
56
  InitialCheckpointNumberNotSequentialError,
57
+ NoProposedCheckpointToPromoteError,
58
+ ProposedCheckpointArchiveRootMismatchError,
59
+ ProposedCheckpointNotSequentialError,
60
+ ProposedCheckpointPromotionNotSequentialError,
48
61
  } from '../errors.js';
49
62
 
50
63
  export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
@@ -59,15 +72,26 @@ type BlockStorage = {
59
72
  indexWithinCheckpoint: number;
60
73
  };
61
74
 
62
- type CheckpointStorage = {
75
+ /** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
76
+ type CommonCheckpointStorage = {
63
77
  header: Buffer;
64
78
  archive: Buffer;
65
79
  checkpointOutHash: Buffer;
66
80
  checkpointNumber: number;
67
81
  startBlock: number;
68
82
  blockCount: number;
83
+ };
84
+
85
+ type CheckpointStorage = CommonCheckpointStorage & {
69
86
  l1: Buffer;
70
87
  attestations: Buffer[];
88
+ feeAssetPriceModifier: string;
89
+ };
90
+
91
+ /** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
92
+ type ProposedCheckpointStorage = CommonCheckpointStorage & {
93
+ totalManaUsed: string;
94
+ feeAssetPriceModifier: string;
71
95
  };
72
96
 
73
97
  export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
@@ -79,7 +103,10 @@ export class BlockStore {
79
103
  /** Map block number to block data */
80
104
  #blocks: AztecAsyncMap<number, BlockStorage>;
81
105
 
82
- /** 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 */
83
110
  #checkpoints: AztecAsyncMap<number, CheckpointStorage>;
84
111
 
85
112
  /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
@@ -97,6 +124,9 @@ export class BlockStore {
97
124
  /** Stores last proven checkpoint */
98
125
  #lastProvenCheckpoint: AztecAsyncSingleton<number>;
99
126
 
127
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
128
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
129
+
100
130
  /** Stores the pending chain validation status */
101
131
  #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
102
132
 
@@ -111,10 +141,7 @@ export class BlockStore {
111
141
 
112
142
  #log = createLogger('archiver:block_store');
113
143
 
114
- constructor(
115
- private db: AztecAsyncKVStore,
116
- private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
117
- ) {
144
+ constructor(private db: AztecAsyncKVStore) {
118
145
  this.#blocks = db.openMap('archiver_blocks');
119
146
  this.#blockTxs = db.openMap('archiver_block_txs');
120
147
  this.#txEffects = db.openMap('archiver_tx_effects');
@@ -123,120 +150,108 @@ export class BlockStore {
123
150
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
124
151
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
125
152
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
153
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
126
154
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
127
155
  this.#checkpoints = db.openMap('archiver_checkpoints');
128
156
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
157
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
129
158
  }
130
159
 
131
160
  /**
132
- * Computes the finalized block number based on the proven block number.
133
- * A block is considered finalized when it's 2 epochs behind the proven block.
134
- * TODO(#13569): Compute proper finalized block number based on L1 finalized block.
135
- * 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.
136
163
  * @returns The finalized block number.
137
164
  */
138
165
  async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
139
- const provenBlockNumber = await this.getProvenBlockNumber();
140
- 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);
141
175
  }
142
176
 
143
177
  /**
144
- * Append new proposed blocks to the store's list. All blocks must be for the 'current' checkpoint.
145
- * 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.
146
180
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
147
- * @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.
148
182
  * @returns True if the operation is successful.
149
183
  */
150
- async addProposedBlocks(blocks: L2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
151
- if (blocks.length === 0) {
152
- return true;
153
- }
154
-
184
+ async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
155
185
  return await this.db.transactionAsync(async () => {
156
- // Check that the block immediately before the first block to be added is present in the store.
157
- const firstBlockNumber = blocks[0].number;
158
- const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
159
- const firstBlockIndex = blocks[0].indexWithinCheckpoint;
160
- 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;
161
190
 
162
191
  // Extract the latest block and checkpoint numbers
163
- const previousBlockNumber = await this.getLatestBlockNumber();
164
- const previousCheckpointNumber = await this.getLatestCheckpointNumber();
192
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
193
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
165
194
 
166
195
  // Verify we're not overwriting checkpointed blocks
167
196
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
168
- if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
169
- 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);
170
204
  }
171
205
 
172
- // Check that the first block number is the expected one
173
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
174
- 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);
175
209
  }
176
210
 
177
- // The same check as above but for checkpoints
178
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
179
- 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);
180
219
  }
181
220
 
182
221
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
183
222
  const previousBlockResult = await this.getBlock(previousBlockNumber);
184
223
 
185
- let expectedFirstblockIndex = 0;
224
+ let expectedBlockIndex = 0;
186
225
  let previousBlockIndex: number | undefined = undefined;
187
226
  if (previousBlockResult !== undefined) {
188
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
227
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
189
228
  // The previous block is for the same checkpoint, therefore our index should follow it
190
229
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
191
- expectedFirstblockIndex = previousBlockIndex + 1;
230
+ expectedBlockIndex = previousBlockIndex + 1;
192
231
  }
193
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
232
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
194
233
  throw new BlockArchiveNotConsistentError(
195
- firstBlockNumber,
234
+ blockNumber,
196
235
  previousBlockResult.number,
197
- firstBlockLastArchive,
236
+ blockLastArchive,
198
237
  previousBlockResult.archive.root,
199
238
  );
200
239
  }
201
240
  }
202
241
 
203
- // Now check that the first block has the expected index value
204
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
205
- 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);
206
245
  }
207
246
 
208
- // Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
209
- let previousBlock: L2Block | undefined = undefined;
210
- for (const block of blocks) {
211
- if (!opts.force && previousBlock) {
212
- if (previousBlock.number + 1 !== block.number) {
213
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
214
- }
215
- if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
216
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
217
- }
218
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
219
- throw new BlockArchiveNotConsistentError(
220
- block.number,
221
- previousBlock.number,
222
- block.header.lastArchive.root,
223
- previousBlock.archive.root,
224
- );
225
- }
226
- }
227
- if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
228
- throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
229
- }
230
- previousBlock = block;
231
- await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
232
- }
247
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
233
248
 
234
249
  return true;
235
250
  });
236
251
  }
237
252
 
238
253
  /**
239
- * Append new cheskpoints to the store's list.
254
+ * Append new checkpoints to the store's list.
240
255
  * @param checkpoints - The L2 checkpoints to be added to the store.
241
256
  * @returns True if the operation is successful.
242
257
  */
@@ -246,37 +261,29 @@ export class BlockStore {
246
261
  }
247
262
 
248
263
  return await this.db.transactionAsync(async () => {
249
- // Check that the checkpoint immediately before the first block to be added is present in the store.
250
264
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
251
265
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
252
266
 
253
- if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
254
- throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
255
- }
256
-
257
- // Extract the previous checkpoint if there is one
258
- let previousCheckpointData: CheckpointData | undefined = undefined;
259
- if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
260
- // There should be a previous checkpoint
261
- previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
262
- if (previousCheckpointData === undefined) {
263
- 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;
264
274
  }
265
- }
266
-
267
- let previousBlockNumber: BlockNumber | undefined = undefined;
268
- let previousBlock: L2Block | undefined = undefined;
269
-
270
- // If we have a previous checkpoint then we need to get the previous block number
271
- if (previousCheckpointData !== undefined) {
272
- previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
273
- previousBlock = await this.getBlock(previousBlockNumber);
274
- if (previousBlock === undefined) {
275
- // We should be able to get the required previous block
276
- 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);
277
279
  }
280
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
281
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
278
282
  }
279
283
 
284
+ // Get the last block of the previous checkpoint for archive chaining
285
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
286
+
280
287
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
281
288
  let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
282
289
  for (const checkpoint of checkpoints) {
@@ -292,42 +299,14 @@ export class BlockStore {
292
299
  }
293
300
  previousCheckpoint = checkpoint;
294
301
 
295
- // Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
296
- for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
297
- const block = checkpoint.checkpoint.blocks[i];
298
- if (previousBlock) {
299
- // The blocks should have a sequential block number
300
- if (previousBlock.number !== block.number - 1) {
301
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
302
- }
303
- // If the blocks are for the same checkpoint then they should have sequential indexes
304
- if (
305
- previousBlock.checkpointNumber === block.checkpointNumber &&
306
- previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
307
- ) {
308
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
309
- }
310
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
311
- throw new BlockArchiveNotConsistentError(
312
- block.number,
313
- previousBlock.number,
314
- block.header.lastArchive.root,
315
- previousBlock.archive.root,
316
- );
317
- }
318
- } else {
319
- // No previous block, must be block 1 at checkpoint index 0
320
- if (block.indexWithinCheckpoint !== 0) {
321
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
322
- }
323
- if (block.number !== INITIAL_L2_BLOCK_NUM) {
324
- throw new BlockNumberNotSequentialError(block.number, undefined);
325
- }
326
- }
302
+ // Validate block sequencing, indexes, and archive chaining
303
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
327
304
 
328
- previousBlock = block;
329
- 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);
330
308
  }
309
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
331
310
 
332
311
  // Store the checkpoint in the database
333
312
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
@@ -339,10 +318,14 @@ export class BlockStore {
339
318
  checkpointNumber: checkpoint.checkpoint.number,
340
319
  startBlock: checkpoint.checkpoint.blocks[0].number,
341
320
  blockCount: checkpoint.checkpoint.blocks.length,
321
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString(),
342
322
  });
343
323
 
344
324
  // Update slot-to-checkpoint index
345
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
 
@@ -442,6 +535,9 @@ export class BlockStore {
442
535
  this.#log.debug(`Removed checkpoint ${c}`);
443
536
  }
444
537
 
538
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
539
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
540
+
445
541
  return { blocksRemoved };
446
542
  });
447
543
  }
@@ -489,6 +585,7 @@ export class BlockStore {
489
585
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
490
586
  startBlock: BlockNumber(checkpointStorage.startBlock),
491
587
  blockCount: checkpointStorage.blockCount,
588
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
492
589
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
493
590
  attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
494
591
  };
@@ -547,7 +644,7 @@ export class BlockStore {
547
644
  const removedBlocks: L2Block[] = [];
548
645
 
549
646
  // Get the latest block number to determine the range
550
- const latestBlockNumber = await this.getLatestBlockNumber();
647
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
551
648
 
552
649
  // Iterate from blockNumber + 1 to latestBlockNumber
553
650
  for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
@@ -580,13 +677,6 @@ export class BlockStore {
580
677
  }
581
678
  }
582
679
 
583
- async getLatestBlockNumber(): Promise<BlockNumber> {
584
- const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
585
- return typeof latestBlocknumber === 'number'
586
- ? BlockNumber(latestBlocknumber)
587
- : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
588
- }
589
-
590
680
  async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
591
681
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
592
682
  if (latestCheckpointNumber === undefined) {
@@ -595,6 +685,167 @@ export class BlockStore {
595
685
  return CheckpointNumber(latestCheckpointNumber);
596
686
  }
597
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
+
598
849
  async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
599
850
  const blockStorage = await this.#blocks.getAsync(number);
600
851
  if (!blockStorage) {
@@ -688,6 +939,33 @@ export class BlockStore {
688
939
  return this.getBlockDataFromBlockStorage(blockStorage);
689
940
  }
690
941
 
942
+ /**
943
+ * Gets block metadata plus checkpoint-derived context (L1 publish info, attestations) without
944
+ * deserializing tx bodies. When the block's containing checkpoint has not yet been L1-confirmed,
945
+ * `checkpoint` and `l1` are `undefined` and `attestations` is empty.
946
+ */
947
+ async getBlockDataWithCheckpointContext(
948
+ blockNumber: BlockNumber,
949
+ ): Promise<BlockDataWithCheckpointContext | undefined> {
950
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
951
+ if (!blockStorage || !blockStorage.header) {
952
+ return undefined;
953
+ }
954
+ const data = this.getBlockDataFromBlockStorage(blockStorage);
955
+ const checkpointStorage = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
956
+ if (!checkpointStorage) {
957
+ return { data, checkpoint: undefined, l1: undefined, attestations: [] };
958
+ }
959
+ const checkpoint = this.checkpointDataFromCheckpointStorage(checkpointStorage);
960
+ return { data, checkpoint, l1: checkpoint.l1, attestations: checkpoint.attestations };
961
+ }
962
+
963
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */
964
+ async getCheckpointNumberBySlot(slot: SlotNumber): Promise<CheckpointNumber | undefined> {
965
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
966
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
967
+ }
968
+
691
969
  /**
692
970
  * Gets block metadata (without tx data) by archive root.
693
971
  * @param archive - The archive root of the block to return.
@@ -809,7 +1087,7 @@ export class BlockStore {
809
1087
  return {
810
1088
  header: BlockHeader.fromBuffer(blockStorage.header),
811
1089
  archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
812
- blockHash: Fr.fromBuffer(blockStorage.blockHash),
1090
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
813
1091
  checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
814
1092
  indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
815
1093
  };
@@ -871,7 +1149,10 @@ export class BlockStore {
871
1149
  * @param txHash - The hash of a tx we try to get the receipt for.
872
1150
  * @returns The requested tx receipt (or undefined if not found).
873
1151
  */
874
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
1152
+ async getSettledTxReceipt(
1153
+ txHash: TxHash,
1154
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
1155
+ ): Promise<TxReceipt | undefined> {
875
1156
  const txEffect = await this.getTxEffect(txHash);
876
1157
  if (!txEffect) {
877
1158
  return undefined;
@@ -880,10 +1161,11 @@ export class BlockStore {
880
1161
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
881
1162
 
882
1163
  // Use existing archiver methods to determine finalization level
883
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
1164
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
884
1165
  this.getProvenBlockNumber(),
885
1166
  this.getCheckpointedL2BlockNumber(),
886
1167
  this.getFinalizedL2BlockNumber(),
1168
+ this.getBlockData(blockNumber),
887
1169
  ]);
888
1170
 
889
1171
  let status: TxStatus;
@@ -897,6 +1179,9 @@ export class BlockStore {
897
1179
  status = TxStatus.PROPOSED;
898
1180
  }
899
1181
 
1182
+ const epochNumber =
1183
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
1184
+
900
1185
  return new TxReceipt(
901
1186
  txHash,
902
1187
  status,
@@ -905,6 +1190,7 @@ export class BlockStore {
905
1190
  txEffect.data.transactionFee.toBigInt(),
906
1191
  txEffect.l2BlockHash,
907
1192
  blockNumber,
1193
+ epochNumber,
908
1194
  );
909
1195
  }
910
1196
 
@@ -961,6 +1247,52 @@ export class BlockStore {
961
1247
  return this.#lastSynchedL1Block.set(l1BlockNumber);
962
1248
  }
963
1249
 
1250
+ /**
1251
+ * Adds a proposed checkpoint to the pending queue.
1252
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1253
+ * confirmed and the highest pending checkpoint number.
1254
+ * Computes archive and checkpointOutHash from the stored blocks.
1255
+ */
1256
+ async addProposedCheckpoint(proposed: ProposedCheckpointInput) {
1257
+ return await this.db.transactionAsync(async () => {
1258
+ const confirmed = await this.getLatestCheckpointNumber();
1259
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
1260
+ const latestTip = CheckpointNumber(
1261
+ latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed,
1262
+ );
1263
+
1264
+ if (proposed.checkpointNumber !== latestTip + 1) {
1265
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
1266
+ }
1267
+
1268
+ // Ensure the predecessor block (from pending or confirmed chain) exists
1269
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1270
+ const blocks: L2Block[] = [];
1271
+ for (let i = 0; i < proposed.blockCount; i++) {
1272
+ const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1273
+ if (!block) {
1274
+ throw new BlockNotFoundError(proposed.startBlock + i);
1275
+ }
1276
+ blocks.push(block);
1277
+ }
1278
+ this.validateCheckpointBlocks(blocks, previousBlock);
1279
+
1280
+ const archive = blocks[blocks.length - 1].archive;
1281
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1282
+
1283
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
1284
+ header: proposed.header.toBuffer(),
1285
+ archive: archive.toBuffer(),
1286
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1287
+ checkpointNumber: proposed.checkpointNumber,
1288
+ startBlock: proposed.startBlock,
1289
+ blockCount: proposed.blockCount,
1290
+ totalManaUsed: proposed.totalManaUsed.toString(),
1291
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1292
+ });
1293
+ });
1294
+ }
1295
+
964
1296
  async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
965
1297
  const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
966
1298
  this.getLatestCheckpointNumber(),
@@ -976,6 +1308,20 @@ export class BlockStore {
976
1308
  return result;
977
1309
  }
978
1310
 
1311
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
1312
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1313
+ this.getLatestCheckpointNumber(),
1314
+ this.#lastFinalizedCheckpoint.getAsync(),
1315
+ ]);
1316
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
1317
+ ? latestCheckpointNumber
1318
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1319
+ }
1320
+
1321
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1322
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1323
+ }
1324
+
979
1325
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
980
1326
  if (limit < 1) {
981
1327
  throw new Error(`Invalid limit: ${limit}`);