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

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 (110) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +9 -6
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +78 -113
  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 +4 -5
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +31 -26
  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 +32 -28
  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 +10 -10
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +34 -33
  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/modules/data_source_base.d.ts +14 -7
  28. package/dest/modules/data_source_base.d.ts.map +1 -1
  29. package/dest/modules/data_source_base.js +39 -77
  30. package/dest/modules/data_store_updater.d.ts +25 -12
  31. package/dest/modules/data_store_updater.d.ts.map +1 -1
  32. package/dest/modules/data_store_updater.js +125 -94
  33. package/dest/modules/instrumentation.d.ts +15 -2
  34. package/dest/modules/instrumentation.d.ts.map +1 -1
  35. package/dest/modules/instrumentation.js +19 -2
  36. package/dest/modules/l1_synchronizer.d.ts +7 -9
  37. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  38. package/dest/modules/l1_synchronizer.js +176 -136
  39. package/dest/modules/validation.d.ts +1 -1
  40. package/dest/modules/validation.d.ts.map +1 -1
  41. package/dest/modules/validation.js +2 -2
  42. package/dest/store/block_store.d.ts +65 -28
  43. package/dest/store/block_store.d.ts.map +1 -1
  44. package/dest/store/block_store.js +311 -134
  45. package/dest/store/contract_class_store.d.ts +2 -3
  46. package/dest/store/contract_class_store.d.ts.map +1 -1
  47. package/dest/store/contract_class_store.js +7 -67
  48. package/dest/store/contract_instance_store.d.ts +1 -1
  49. package/dest/store/contract_instance_store.d.ts.map +1 -1
  50. package/dest/store/contract_instance_store.js +6 -2
  51. package/dest/store/kv_archiver_store.d.ts +61 -24
  52. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  53. package/dest/store/kv_archiver_store.js +75 -27
  54. package/dest/store/l2_tips_cache.d.ts +20 -0
  55. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  56. package/dest/store/l2_tips_cache.js +109 -0
  57. package/dest/store/log_store.d.ts +6 -3
  58. package/dest/store/log_store.d.ts.map +1 -1
  59. package/dest/store/log_store.js +93 -16
  60. package/dest/store/message_store.d.ts +5 -1
  61. package/dest/store/message_store.d.ts.map +1 -1
  62. package/dest/store/message_store.js +21 -9
  63. package/dest/test/fake_l1_state.d.ts +14 -1
  64. package/dest/test/fake_l1_state.d.ts.map +1 -1
  65. package/dest/test/fake_l1_state.js +120 -26
  66. package/dest/test/mock_archiver.d.ts +1 -1
  67. package/dest/test/mock_archiver.d.ts.map +1 -1
  68. package/dest/test/mock_archiver.js +3 -2
  69. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  70. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  71. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  72. package/dest/test/mock_l2_block_source.d.ts +26 -5
  73. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  74. package/dest/test/mock_l2_block_source.js +160 -89
  75. package/dest/test/mock_structs.d.ts +4 -1
  76. package/dest/test/mock_structs.d.ts.map +1 -1
  77. package/dest/test/mock_structs.js +13 -1
  78. package/dest/test/noop_l1_archiver.d.ts +4 -1
  79. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  80. package/dest/test/noop_l1_archiver.js +5 -1
  81. package/package.json +13 -13
  82. package/src/archiver.ts +97 -133
  83. package/src/config.ts +8 -1
  84. package/src/errors.ts +70 -26
  85. package/src/factory.ts +46 -24
  86. package/src/index.ts +1 -0
  87. package/src/l1/README.md +25 -68
  88. package/src/l1/bin/retrieve-calldata.ts +40 -27
  89. package/src/l1/calldata_retriever.ts +250 -379
  90. package/src/l1/data_retrieval.ts +30 -36
  91. package/src/l1/spire_proposer.ts +7 -15
  92. package/src/modules/data_source_base.ts +78 -98
  93. package/src/modules/data_store_updater.ts +138 -124
  94. package/src/modules/instrumentation.ts +29 -2
  95. package/src/modules/l1_synchronizer.ts +195 -168
  96. package/src/modules/validation.ts +2 -2
  97. package/src/store/block_store.ts +393 -170
  98. package/src/store/contract_class_store.ts +8 -106
  99. package/src/store/contract_instance_store.ts +8 -5
  100. package/src/store/kv_archiver_store.ts +115 -41
  101. package/src/store/l2_tips_cache.ts +128 -0
  102. package/src/store/log_store.ts +126 -27
  103. package/src/store/message_store.ts +27 -10
  104. package/src/structs/inbox_message.ts +1 -1
  105. package/src/test/fake_l1_state.ts +163 -30
  106. package/src/test/mock_archiver.ts +3 -2
  107. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  108. package/src/test/mock_l2_block_source.ts +209 -82
  109. package/src/test/mock_structs.ts +20 -6
  110. package/src/test/noop_l1_archiver.ts +7 -1
@@ -9,6 +9,7 @@ import { isDefined } from '@aztec/foundation/types';
9
9
  import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
10
10
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
11
11
  import {
12
+ type BlockData,
12
13
  BlockHash,
13
14
  Body,
14
15
  CheckpointedL2Block,
@@ -18,8 +19,16 @@ import {
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
  */
@@ -258,28 +270,8 @@ export class BlockStore {
258
270
  throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
259
271
  }
260
272
 
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
- }
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);
281
- }
282
- }
273
+ // Get the last block of the previous checkpoint for archive chaining
274
+ let previousBlock = await this.getPreviousCheckpointBlock(firstCheckpointNumber);
283
275
 
284
276
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
285
277
  let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
@@ -296,60 +288,102 @@ export class BlockStore {
296
288
  }
297
289
  previousCheckpoint = checkpoint;
298
290
 
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
- }
291
+ // Validate block sequencing, indexes, and archive chaining
292
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
331
293
 
332
- previousBlock = block;
333
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
294
+ // Store every block in the database (may already exist, but L1 data is authoritative)
295
+ for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
296
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
334
297
  }
298
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
335
299
 
336
300
  // Store the checkpoint in the database
337
301
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
338
302
  header: checkpoint.checkpoint.header.toBuffer(),
339
303
  archive: checkpoint.checkpoint.archive.toBuffer(),
304
+ checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
340
305
  l1: checkpoint.l1.toBuffer(),
341
306
  attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
342
307
  checkpointNumber: checkpoint.checkpoint.number,
343
308
  startBlock: checkpoint.checkpoint.blocks[0].number,
344
- numBlocks: checkpoint.checkpoint.blocks.length,
309
+ blockCount: checkpoint.checkpoint.blocks.length,
345
310
  });
311
+
312
+ // Update slot-to-checkpoint index
313
+ await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
346
314
  }
347
315
 
316
+ // Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
317
+ const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
318
+ await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
319
+
348
320
  await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
349
321
  return true;
350
322
  });
351
323
  }
352
324
 
325
+ /**
326
+ * Gets the last block of the checkpoint before the given one.
327
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
328
+ */
329
+ private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
330
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
331
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
332
+ return undefined;
333
+ }
334
+
335
+ const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
336
+ if (previousCheckpointData === undefined) {
337
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
338
+ }
339
+
340
+ const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
341
+ const previousBlock = await this.getBlock(previousBlockNumber);
342
+ if (previousBlock === undefined) {
343
+ throw new BlockNotFoundError(previousBlockNumber);
344
+ }
345
+
346
+ return previousBlock;
347
+ }
348
+
349
+ /**
350
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
351
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
352
+ * proposed checkpoints (setProposedCheckpoint).
353
+ */
354
+ private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
355
+ for (const block of blocks) {
356
+ if (previousBlock) {
357
+ if (previousBlock.number !== block.number - 1) {
358
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
359
+ }
360
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
361
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
362
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
363
+ }
364
+ } else if (block.indexWithinCheckpoint !== 0) {
365
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
366
+ }
367
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
368
+ throw new BlockArchiveNotConsistentError(
369
+ block.number,
370
+ previousBlock.number,
371
+ block.header.lastArchive.root,
372
+ previousBlock.archive.root,
373
+ );
374
+ }
375
+ } else {
376
+ if (block.indexWithinCheckpoint !== 0) {
377
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
378
+ }
379
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
380
+ throw new BlockNumberNotSequentialError(block.number, undefined);
381
+ }
382
+ }
383
+ previousBlock = block;
384
+ }
385
+ }
386
+
353
387
  private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
354
388
  const blockHash = await block.hash();
355
389
 
@@ -425,7 +459,7 @@ export class BlockStore {
425
459
  if (!targetCheckpoint) {
426
460
  throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
427
461
  }
428
- lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.numBlocks - 1);
462
+ lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
429
463
  }
430
464
 
431
465
  // Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
@@ -433,10 +467,21 @@ export class BlockStore {
433
467
 
434
468
  // Remove all checkpoints after the target
435
469
  for (let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)) {
470
+ const checkpointStorage = await this.#checkpoints.getAsync(c);
471
+ if (checkpointStorage) {
472
+ const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
473
+ await this.#slotToCheckpoint.delete(slotNumber);
474
+ }
436
475
  await this.#checkpoints.delete(c);
437
476
  this.#log.debug(`Removed checkpoint ${c}`);
438
477
  }
439
478
 
479
+ // Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
480
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
481
+ if (proposedCheckpointNumber > checkpointNumber) {
482
+ await this.#proposedCheckpoint.delete();
483
+ }
484
+
440
485
  return { blocksRemoved };
441
486
  });
442
487
  }
@@ -461,17 +506,32 @@ export class BlockStore {
461
506
  return checkpoints;
462
507
  }
463
508
 
464
- private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage) {
465
- const data: CheckpointData = {
509
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
510
+ async getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
511
+ const result: CheckpointData[] = [];
512
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
513
+ start: startSlot,
514
+ end: endSlot + 1,
515
+ })) {
516
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
517
+ if (checkpointStorage) {
518
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
519
+ }
520
+ }
521
+ return result;
522
+ }
523
+
524
+ private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage): CheckpointData {
525
+ return {
466
526
  header: CheckpointHeader.fromBuffer(checkpointStorage.header),
467
527
  archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
528
+ checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
468
529
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
469
- startBlock: checkpointStorage.startBlock,
470
- numBlocks: checkpointStorage.numBlocks,
530
+ startBlock: BlockNumber(checkpointStorage.startBlock),
531
+ blockCount: checkpointStorage.blockCount,
471
532
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
472
- attestations: checkpointStorage.attestations,
533
+ attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
473
534
  };
474
- return data;
475
535
  }
476
536
 
477
537
  async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
@@ -483,7 +543,7 @@ export class BlockStore {
483
543
  const blocksForCheckpoint = await toArray(
484
544
  this.#blocks.entriesAsync({
485
545
  start: checkpoint.startBlock,
486
- end: checkpoint.startBlock + checkpoint.numBlocks,
546
+ end: checkpoint.startBlock + checkpoint.blockCount,
487
547
  }),
488
548
  );
489
549
 
@@ -527,7 +587,7 @@ export class BlockStore {
527
587
  const removedBlocks: L2Block[] = [];
528
588
 
529
589
  // Get the latest block number to determine the range
530
- const latestBlockNumber = await this.getLatestBlockNumber();
590
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
531
591
 
532
592
  // Iterate from blockNumber + 1 to latestBlockNumber
533
593
  for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
@@ -556,17 +616,10 @@ export class BlockStore {
556
616
  if (!checkpointStorage) {
557
617
  throw new CheckpointNotFoundError(provenCheckpointNumber);
558
618
  } else {
559
- return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
619
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
560
620
  }
561
621
  }
562
622
 
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
623
  async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
571
624
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
572
625
  if (latestCheckpointNumber === undefined) {
@@ -575,6 +628,84 @@ export class BlockStore {
575
628
  return CheckpointNumber(latestCheckpointNumber);
576
629
  }
577
630
 
631
+ async hasProposedCheckpoint(): Promise<boolean> {
632
+ const proposed = await this.#proposedCheckpoint.getAsync();
633
+ return proposed !== undefined;
634
+ }
635
+
636
+ /** Deletes the proposed checkpoint from storage. */
637
+ async deleteProposedCheckpoint(): Promise<void> {
638
+ await this.#proposedCheckpoint.delete();
639
+ }
640
+
641
+ /** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */
642
+ async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber: CheckpointNumber): Promise<void> {
643
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
644
+ if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
645
+ await this.#proposedCheckpoint.delete();
646
+ }
647
+ }
648
+
649
+ /** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */
650
+ async getProposedCheckpointOnly(): Promise<ProposedCheckpointData | undefined> {
651
+ const stored = await this.#proposedCheckpoint.getAsync();
652
+ if (!stored) {
653
+ return undefined;
654
+ }
655
+ return this.convertToProposedCheckpointData(stored);
656
+ }
657
+
658
+ /**
659
+ * Gets the checkpoint at the proposed tip
660
+ * - pending checkpoint if it exists
661
+ * - fallsback to latest confirmed checkpoint otherwise
662
+ * @returns CommonCheckpointData
663
+ */
664
+ async getProposedCheckpoint(): Promise<CommonCheckpointData | undefined> {
665
+ const stored = await this.#proposedCheckpoint.getAsync();
666
+ if (!stored) {
667
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
668
+ }
669
+ return this.convertToProposedCheckpointData(stored);
670
+ }
671
+
672
+ private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
673
+ return {
674
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
675
+ header: CheckpointHeader.fromBuffer(stored.header),
676
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
677
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
678
+ startBlock: BlockNumber(stored.startBlock),
679
+ blockCount: stored.blockCount,
680
+ totalManaUsed: BigInt(stored.totalManaUsed),
681
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
682
+ };
683
+ }
684
+
685
+ /**
686
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
687
+ * @returns CheckpointNumber
688
+ */
689
+ async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
690
+ const proposed = await this.getProposedCheckpoint();
691
+ if (!proposed) {
692
+ return await this.getLatestCheckpointNumber();
693
+ }
694
+ return CheckpointNumber(proposed.checkpointNumber);
695
+ }
696
+
697
+ /**
698
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
699
+ * @returns BlockNumber
700
+ */
701
+ async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
702
+ const proposed = await this.getProposedCheckpoint();
703
+ if (!proposed) {
704
+ return await this.getCheckpointedL2BlockNumber();
705
+ }
706
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
707
+ }
708
+
578
709
  async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
579
710
  const blockStorage = await this.#blocks.getAsync(number);
580
711
  if (!blockStorage) {
@@ -655,6 +786,32 @@ export class BlockStore {
655
786
  }
656
787
  }
657
788
 
789
+ /**
790
+ * Gets block metadata (without tx data) by block number.
791
+ * @param blockNumber - The number of the block to return.
792
+ * @returns The requested block data.
793
+ */
794
+ async getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
795
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
796
+ if (!blockStorage || !blockStorage.header) {
797
+ return undefined;
798
+ }
799
+ return this.getBlockDataFromBlockStorage(blockStorage);
800
+ }
801
+
802
+ /**
803
+ * Gets block metadata (without tx data) by archive root.
804
+ * @param archive - The archive root of the block to return.
805
+ * @returns The requested block data.
806
+ */
807
+ async getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
808
+ const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
809
+ if (blockNumber === undefined) {
810
+ return undefined;
811
+ }
812
+ return this.getBlockData(BlockNumber(blockNumber));
813
+ }
814
+
658
815
  /**
659
816
  * Gets an L2 block.
660
817
  * @param blockNumber - The number of the block to return.
@@ -759,15 +916,24 @@ export class BlockStore {
759
916
  }
760
917
  }
761
918
 
919
+ private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
920
+ return {
921
+ header: BlockHeader.fromBuffer(blockStorage.header),
922
+ archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
923
+ blockHash: Fr.fromBuffer(blockStorage.blockHash),
924
+ checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
925
+ indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
926
+ };
927
+ }
928
+
762
929
  private async getBlockFromBlockStorage(
763
930
  blockNumber: number,
764
931
  blockStorage: BlockStorage,
765
932
  ): 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);
933
+ const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } =
934
+ this.getBlockDataFromBlockStorage(blockStorage);
935
+ header.setHash(blockHash);
936
+ const blockHashString = bufferToHex(blockStorage.blockHash);
771
937
  const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
772
938
  if (blockTxsBuffer === undefined) {
773
939
  this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
@@ -786,13 +952,7 @@ export class BlockStore {
786
952
  txEffects.push(deserializeIndexedTxEffect(txEffect).data);
787
953
  }
788
954
  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
- );
955
+ const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
796
956
 
797
957
  if (block.number !== blockNumber) {
798
958
  throw new Error(
@@ -822,7 +982,10 @@ export class BlockStore {
822
982
  * @param txHash - The hash of a tx we try to get the receipt for.
823
983
  * @returns The requested tx receipt (or undefined if not found).
824
984
  */
825
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
985
+ async getSettledTxReceipt(
986
+ txHash: TxHash,
987
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
988
+ ): Promise<TxReceipt | undefined> {
826
989
  const txEffect = await this.getTxEffect(txHash);
827
990
  if (!txEffect) {
828
991
  return undefined;
@@ -831,10 +994,11 @@ export class BlockStore {
831
994
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
832
995
 
833
996
  // Use existing archiver methods to determine finalization level
834
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
997
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
835
998
  this.getProvenBlockNumber(),
836
999
  this.getCheckpointedL2BlockNumber(),
837
1000
  this.getFinalizedL2BlockNumber(),
1001
+ this.getBlockData(blockNumber),
838
1002
  ]);
839
1003
 
840
1004
  let status: TxStatus;
@@ -848,6 +1012,9 @@ export class BlockStore {
848
1012
  status = TxStatus.PROPOSED;
849
1013
  }
850
1014
 
1015
+ const epochNumber =
1016
+ blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
1017
+
851
1018
  return new TxReceipt(
852
1019
  txHash,
853
1020
  status,
@@ -856,6 +1023,7 @@ export class BlockStore {
856
1023
  txEffect.data.transactionFee.toBigInt(),
857
1024
  txEffect.l2BlockHash,
858
1025
  blockNumber,
1026
+ epochNumber,
859
1027
  );
860
1028
  }
861
1029
 
@@ -892,7 +1060,7 @@ export class BlockStore {
892
1060
  if (!checkpoint) {
893
1061
  return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
894
1062
  }
895
- return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
1063
+ return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
896
1064
  }
897
1065
 
898
1066
  async getLatestL2BlockNumber(): Promise<BlockNumber> {
@@ -912,6 +1080,47 @@ export class BlockStore {
912
1080
  return this.#lastSynchedL1Block.set(l1BlockNumber);
913
1081
  }
914
1082
 
1083
+ /** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
1084
+ * Computes archive and checkpointOutHash from the stored blocks. */
1085
+ async setProposedCheckpoint(proposed: ProposedCheckpointInput) {
1086
+ return await this.db.transactionAsync(async () => {
1087
+ const current = await this.getProposedCheckpointNumber();
1088
+ if (proposed.checkpointNumber <= current) {
1089
+ throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
1090
+ }
1091
+ const confirmed = await this.getLatestCheckpointNumber();
1092
+ if (proposed.checkpointNumber !== confirmed + 1) {
1093
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
1094
+ }
1095
+
1096
+ // Ensure the previous checkpoint + blocks exist
1097
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1098
+ const blocks: L2Block[] = [];
1099
+ for (let i = 0; i < proposed.blockCount; i++) {
1100
+ const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1101
+ if (!block) {
1102
+ throw new BlockNotFoundError(proposed.startBlock + i);
1103
+ }
1104
+ blocks.push(block);
1105
+ }
1106
+ this.validateCheckpointBlocks(blocks, previousBlock);
1107
+
1108
+ const archive = blocks[blocks.length - 1].archive;
1109
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1110
+
1111
+ await this.#proposedCheckpoint.set({
1112
+ header: proposed.header.toBuffer(),
1113
+ archive: archive.toBuffer(),
1114
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1115
+ checkpointNumber: proposed.checkpointNumber,
1116
+ startBlock: proposed.startBlock,
1117
+ blockCount: proposed.blockCount,
1118
+ totalManaUsed: proposed.totalManaUsed.toString(),
1119
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1120
+ });
1121
+ });
1122
+ }
1123
+
915
1124
  async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
916
1125
  const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
917
1126
  this.getLatestCheckpointNumber(),
@@ -927,6 +1136,20 @@ export class BlockStore {
927
1136
  return result;
928
1137
  }
929
1138
 
1139
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
1140
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1141
+ this.getLatestCheckpointNumber(),
1142
+ this.#lastFinalizedCheckpoint.getAsync(),
1143
+ ]);
1144
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
1145
+ ? latestCheckpointNumber
1146
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1147
+ }
1148
+
1149
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1150
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1151
+ }
1152
+
930
1153
  #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
931
1154
  if (limit < 1) {
932
1155
  throw new Error(`Invalid limit: ${limit}`);