@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
@@ -6,33 +6,34 @@ import { createLogger } from '@aztec/foundation/log';
6
6
  import { BufferReader } from '@aztec/foundation/serialize';
7
7
  import { bufferToHex } from '@aztec/foundation/string';
8
8
  import { isDefined } from '@aztec/foundation/types';
9
- import { Body, CheckpointedL2Block, CommitteeAttestation, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
10
- import { L1PublishedData } from '@aztec/stdlib/checkpoint';
9
+ import { BlockHash, Body, CheckpointedL2Block, CommitteeAttestation, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
10
+ import { Checkpoint, L1PublishedData } from '@aztec/stdlib/checkpoint';
11
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
11
12
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
12
13
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
13
14
  import { BlockHeader, TxHash, TxReceipt, TxStatus, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
14
- import { BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotConsistentError, CheckpointNumberNotSequentialError, InitialBlockNumberNotSequentialError, InitialCheckpointNumberNotSequentialError } from '../errors.js';
15
+ import { BlockAlreadyCheckpointedError, BlockArchiveNotConsistentError, BlockCheckpointNumberNotSequentialError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotSequentialError, InitialCheckpointNumberNotSequentialError, NoProposedCheckpointToPromoteError, ProposedCheckpointArchiveRootMismatchError, ProposedCheckpointNotSequentialError, ProposedCheckpointPromotionNotSequentialError } from '../errors.js';
15
16
  export { TxReceipt } from '@aztec/stdlib/tx';
16
17
  /**
17
18
  * LMDB-based block storage for the archiver.
18
19
  */ export class BlockStore {
19
20
  db;
20
- l1Constants;
21
21
  /** Map block number to block data */ #blocks;
22
- /** Map checkpoint number to checkpoint data */ #checkpoints;
22
+ /** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */ #proposedCheckpoints;
23
+ /** Map checkpoint number to checkpoint data for mined checkpoints only */ #checkpoints;
23
24
  /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */ #slotToCheckpoint;
24
25
  /** Map block hash to list of tx hashes */ #blockTxs;
25
26
  /** Tx hash to serialized IndexedTxEffect */ #txEffects;
26
27
  /** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block;
27
28
  /** Stores last proven checkpoint */ #lastProvenCheckpoint;
29
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */ #lastFinalizedCheckpoint;
28
30
  /** Stores the pending chain validation status */ #pendingChainValidationStatus;
29
31
  /** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
30
32
  /** Index mapping block hash to block number */ #blockHashIndex;
31
33
  /** Index mapping block archive to block number */ #blockArchiveIndex;
32
34
  #log;
33
- constructor(db, l1Constants){
35
+ constructor(db){
34
36
  this.db = db;
35
- this.l1Constants = l1Constants;
36
37
  this.#log = createLogger('archiver:block_store');
37
38
  this.#blocks = db.openMap('archiver_blocks');
38
39
  this.#blockTxs = db.openMap('archiver_block_txs');
@@ -42,95 +43,92 @@ export { TxReceipt } from '@aztec/stdlib/tx';
42
43
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
43
44
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
44
45
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
46
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
45
47
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
46
48
  this.#checkpoints = db.openMap('archiver_checkpoints');
47
49
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
50
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
48
51
  }
49
52
  /**
50
- * Computes the finalized block number based on the proven block number.
51
- * A block is considered finalized when it's 2 epochs behind the proven block.
52
- * TODO(#13569): Compute proper finalized block number based on L1 finalized block.
53
- * TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
53
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
54
+ * in an L1 block that has itself been finalized on Ethereum.
54
55
  * @returns The finalized block number.
55
56
  */ async getFinalizedL2BlockNumber() {
56
- const provenBlockNumber = await this.getProvenBlockNumber();
57
- return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
57
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
58
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
59
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
60
+ }
61
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
62
+ if (!checkpointStorage) {
63
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
64
+ }
65
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
58
66
  }
59
67
  /**
60
- * Append new proposed blocks to the store's list. All blocks must be for the 'current' checkpoint.
61
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
68
+ * Append a new proposed block to the store.
69
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
62
70
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
63
- * @param blocks - The proposed L2 blocks to be added to the store.
71
+ * @param block - The proposed L2 block to be added to the store.
64
72
  * @returns True if the operation is successful.
65
- */ async addProposedBlocks(blocks, opts = {}) {
66
- if (blocks.length === 0) {
67
- return true;
68
- }
73
+ */ async addProposedBlock(block, opts = {}) {
69
74
  return await this.db.transactionAsync(async ()=>{
70
- // Check that the block immediately before the first block to be added is present in the store.
71
- const firstBlockNumber = blocks[0].number;
72
- const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
73
- const firstBlockIndex = blocks[0].indexWithinCheckpoint;
74
- const firstBlockLastArchive = blocks[0].header.lastArchive.root;
75
+ const blockNumber = block.number;
76
+ const blockCheckpointNumber = block.checkpointNumber;
77
+ const blockIndex = block.indexWithinCheckpoint;
78
+ const blockLastArchive = block.header.lastArchive.root;
75
79
  // Extract the latest block and checkpoint numbers
76
- const previousBlockNumber = await this.getLatestBlockNumber();
77
- const previousCheckpointNumber = await this.getLatestCheckpointNumber();
80
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
81
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
78
82
  // Verify we're not overwriting checkpointed blocks
79
83
  const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
80
- if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
81
- throw new CannotOverwriteCheckpointedBlockError(firstBlockNumber, lastCheckpointedBlockNumber);
84
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
85
+ // Check if the proposed block matches the already-checkpointed one
86
+ const existingBlock = await this.getBlock(BlockNumber(blockNumber));
87
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
88
+ throw new BlockAlreadyCheckpointedError(blockNumber);
89
+ }
90
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
82
91
  }
83
- // Check that the first block number is the expected one
84
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
85
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
92
+ // Check that the block number is the expected one
93
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
94
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
86
95
  }
87
- // The same check as above but for checkpoints
88
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
89
- throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
96
+ // Accept the block if either the confirmed checkpoint or a pending checkpoint matches
97
+ // the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
98
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
99
+ const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
100
+ if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
101
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
102
+ reverse: true,
103
+ limit: 1
104
+ }));
105
+ const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
106
+ throw new BlockCheckpointNumberNotSequentialError(blockNumber, blockCheckpointNumber, previous);
90
107
  }
91
108
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
92
109
  const previousBlockResult = await this.getBlock(previousBlockNumber);
93
- let expectedFirstblockIndex = 0;
110
+ let expectedBlockIndex = 0;
94
111
  let previousBlockIndex = undefined;
95
112
  if (previousBlockResult !== undefined) {
96
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
113
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
97
114
  // The previous block is for the same checkpoint, therefore our index should follow it
98
115
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
99
- expectedFirstblockIndex = previousBlockIndex + 1;
116
+ expectedBlockIndex = previousBlockIndex + 1;
100
117
  }
101
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
102
- throw new BlockArchiveNotConsistentError(firstBlockNumber, previousBlockResult.number, firstBlockLastArchive, previousBlockResult.archive.root);
118
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
119
+ throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.number, blockLastArchive, previousBlockResult.archive.root);
103
120
  }
104
121
  }
105
- // Now check that the first block has the expected index value
106
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
107
- throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
108
- }
109
- // Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
110
- let previousBlock = undefined;
111
- for (const block of blocks){
112
- if (!opts.force && previousBlock) {
113
- if (previousBlock.number + 1 !== block.number) {
114
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
115
- }
116
- if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
117
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
118
- }
119
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
120
- throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
121
- }
122
- }
123
- if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
124
- throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
125
- }
126
- previousBlock = block;
127
- await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
122
+ // Now check that the block has the expected index value
123
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
124
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
128
125
  }
126
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
129
127
  return true;
130
128
  });
131
129
  }
132
130
  /**
133
- * Append new cheskpoints to the store's list.
131
+ * Append new checkpoints to the store's list.
134
132
  * @param checkpoints - The L2 checkpoints to be added to the store.
135
133
  * @returns True if the operation is successful.
136
134
  */ async addCheckpoints(checkpoints, opts = {}) {
@@ -138,32 +136,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
138
136
  return true;
139
137
  }
140
138
  return await this.db.transactionAsync(async ()=>{
141
- // Check that the checkpoint immediately before the first block to be added is present in the store.
142
139
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
143
140
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
144
- if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
145
- throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
146
- }
147
- // Extract the previous checkpoint if there is one
148
- let previousCheckpointData = undefined;
149
- if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
150
- // There should be a previous checkpoint
151
- previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
152
- if (previousCheckpointData === undefined) {
153
- throw new CheckpointNotFoundError(previousCheckpointNumber);
141
+ // Handle already-stored checkpoints at the start of the batch.
142
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
143
+ // We accept them if archives match (same content) and update their L1 metadata.
144
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
145
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
146
+ if (checkpoints.length === 0) {
147
+ return true;
154
148
  }
155
- }
156
- let previousBlockNumber = undefined;
157
- let previousBlock = undefined;
158
- // If we have a previous checkpoint then we need to get the previous block number
159
- if (previousCheckpointData !== undefined) {
160
- previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
161
- previousBlock = await this.getBlock(previousBlockNumber);
162
- if (previousBlock === undefined) {
163
- // We should be able to get the required previous block
164
- throw new BlockNotFoundError(previousBlockNumber);
149
+ // Re-check sequentiality after skipping
150
+ const newFirstNumber = checkpoints[0].checkpoint.number;
151
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
152
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
165
153
  }
154
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
155
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
166
156
  }
157
+ // Get the last block of the previous checkpoint for archive chaining
158
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
167
159
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
168
160
  let previousCheckpoint = undefined;
169
161
  for (const checkpoint of checkpoints){
@@ -171,33 +163,13 @@ export { TxReceipt } from '@aztec/stdlib/tx';
171
163
  throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
172
164
  }
173
165
  previousCheckpoint = checkpoint;
174
- // Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
166
+ // Validate block sequencing, indexes, and archive chaining
167
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
168
+ // Store every block in the database (may already exist, but L1 data is authoritative)
175
169
  for(let i = 0; i < checkpoint.checkpoint.blocks.length; i++){
176
- const block = checkpoint.checkpoint.blocks[i];
177
- if (previousBlock) {
178
- // The blocks should have a sequential block number
179
- if (previousBlock.number !== block.number - 1) {
180
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
181
- }
182
- // If the blocks are for the same checkpoint then they should have sequential indexes
183
- if (previousBlock.checkpointNumber === block.checkpointNumber && previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
184
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
185
- }
186
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
187
- throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
188
- }
189
- } else {
190
- // No previous block, must be block 1 at checkpoint index 0
191
- if (block.indexWithinCheckpoint !== 0) {
192
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
193
- }
194
- if (block.number !== INITIAL_L2_BLOCK_NUM) {
195
- throw new BlockNumberNotSequentialError(block.number, undefined);
196
- }
197
- }
198
- previousBlock = block;
199
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
170
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
200
171
  }
172
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
201
173
  // Store the checkpoint in the database
202
174
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
203
175
  header: checkpoint.checkpoint.header.toBuffer(),
@@ -207,15 +179,102 @@ export { TxReceipt } from '@aztec/stdlib/tx';
207
179
  attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
208
180
  checkpointNumber: checkpoint.checkpoint.number,
209
181
  startBlock: checkpoint.checkpoint.blocks[0].number,
210
- blockCount: checkpoint.checkpoint.blocks.length
182
+ blockCount: checkpoint.checkpoint.blocks.length,
183
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
211
184
  });
212
185
  // Update slot-to-checkpoint index
213
186
  await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
187
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
188
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
214
189
  }
215
190
  await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
216
191
  return true;
217
192
  });
218
193
  }
194
+ /**
195
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
196
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
197
+ */ async skipOrUpdateAlreadyStoredCheckpoints(checkpoints, latestStored) {
198
+ let i = 0;
199
+ for(; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++){
200
+ const incoming = checkpoints[i];
201
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
202
+ if (!stored) {
203
+ break;
204
+ }
205
+ // Verify the checkpoint content matches (archive root)
206
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
207
+ throw new Error(`Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` + `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`);
208
+ }
209
+ // Update L1 metadata and attestations for the already-stored checkpoint
210
+ this.#log.warn(`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` + `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`);
211
+ await this.#checkpoints.set(incoming.checkpoint.number, {
212
+ header: incoming.checkpoint.header.toBuffer(),
213
+ archive: incoming.checkpoint.archive.toBuffer(),
214
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
215
+ l1: incoming.l1.toBuffer(),
216
+ attestations: incoming.attestations.map((a)=>a.toBuffer()),
217
+ checkpointNumber: incoming.checkpoint.number,
218
+ startBlock: incoming.checkpoint.blocks[0].number,
219
+ blockCount: incoming.checkpoint.blocks.length,
220
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString()
221
+ });
222
+ // Update the sync point to reflect the new L1 block
223
+ await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
224
+ }
225
+ return checkpoints.slice(i);
226
+ }
227
+ /**
228
+ * Gets the last block of the checkpoint before the given one.
229
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
230
+ */ async getPreviousCheckpointBlock(checkpointNumber) {
231
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
232
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
233
+ return undefined;
234
+ }
235
+ // Check across both proposed and mined checkpoints
236
+ const predecessor = await this.getProposedCheckpointByNumber(previousCheckpointNumber) ?? await this.getCheckpointData(previousCheckpointNumber);
237
+ if (!predecessor) {
238
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
239
+ }
240
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
241
+ const previousBlock = await this.getBlock(previousBlockNumber);
242
+ if (previousBlock === undefined) {
243
+ throw new BlockNotFoundError(previousBlockNumber);
244
+ }
245
+ return previousBlock;
246
+ }
247
+ /**
248
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
249
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
250
+ * proposed checkpoints (addProposedCheckpoint).
251
+ */ validateCheckpointBlocks(blocks, previousBlock) {
252
+ for (const block of blocks){
253
+ if (previousBlock) {
254
+ if (previousBlock.number !== block.number - 1) {
255
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
256
+ }
257
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
258
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
259
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
260
+ }
261
+ } else if (block.indexWithinCheckpoint !== 0) {
262
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
263
+ }
264
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
265
+ throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
266
+ }
267
+ } else {
268
+ if (block.indexWithinCheckpoint !== 0) {
269
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
270
+ }
271
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
272
+ throw new BlockNumberNotSequentialError(block.number, undefined);
273
+ }
274
+ }
275
+ previousBlock = block;
276
+ }
277
+ }
219
278
  async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
220
279
  const blockHash = await block.hash();
221
280
  await this.#blocks.set(block.number, {
@@ -293,6 +352,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
293
352
  await this.#checkpoints.delete(c);
294
353
  this.#log.debug(`Removed checkpoint ${c}`);
295
354
  }
355
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
356
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
296
357
  return {
297
358
  blocksRemoved
298
359
  };
@@ -337,6 +398,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
337
398
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
338
399
  startBlock: BlockNumber(checkpointStorage.startBlock),
339
400
  blockCount: checkpointStorage.blockCount,
401
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
340
402
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
341
403
  attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
342
404
  };
@@ -385,7 +447,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
385
447
  return await this.db.transactionAsync(async ()=>{
386
448
  const removedBlocks = [];
387
449
  // Get the latest block number to determine the range
388
- const latestBlockNumber = await this.getLatestBlockNumber();
450
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
389
451
  // Iterate from blockNumber + 1 to latestBlockNumber
390
452
  for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
391
453
  const block = await this.getBlock(BlockNumber(bn));
@@ -412,13 +474,6 @@ export { TxReceipt } from '@aztec/stdlib/tx';
412
474
  return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
413
475
  }
414
476
  }
415
- async getLatestBlockNumber() {
416
- const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({
417
- reverse: true,
418
- limit: 1
419
- }));
420
- return typeof latestBlocknumber === 'number' ? BlockNumber(latestBlocknumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
421
- }
422
477
  async getLatestCheckpointNumber() {
423
478
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
424
479
  reverse: true,
@@ -429,6 +484,142 @@ export { TxReceipt } from '@aztec/stdlib/tx';
429
484
  }
430
485
  return CheckpointNumber(latestCheckpointNumber);
431
486
  }
487
+ async hasProposedCheckpoint() {
488
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
489
+ limit: 1
490
+ }));
491
+ return key !== undefined;
492
+ }
493
+ /** Deletes all pending proposed checkpoints from storage. */ async deleteProposedCheckpoints() {
494
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
495
+ await this.#proposedCheckpoints.delete(key);
496
+ }
497
+ }
498
+ /**
499
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
500
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
501
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
502
+ * @param checkpointNumber - The checkpoint number to promote.
503
+ * @param l1 - L1 published data for the checkpoint.
504
+ * @param attestations - Committee attestations.
505
+ * @param expectedArchiveRoot - Archive root guard against races.
506
+ */ async promoteProposedToCheckpointed(checkpointNumber, l1, attestations, expectedArchiveRoot) {
507
+ return await this.db.transactionAsync(async ()=>{
508
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
509
+ if (!proposed) {
510
+ throw new NoProposedCheckpointToPromoteError();
511
+ }
512
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
513
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
514
+ }
515
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
516
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
517
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
518
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
519
+ }
520
+ // Write the checkpoint entry
521
+ await this.#checkpoints.set(proposed.checkpointNumber, {
522
+ header: proposed.header.toBuffer(),
523
+ archive: proposed.archive.toBuffer(),
524
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
525
+ l1: l1.toBuffer(),
526
+ attestations: attestations.map((attestation)=>attestation.toBuffer()),
527
+ checkpointNumber: proposed.checkpointNumber,
528
+ startBlock: proposed.startBlock,
529
+ blockCount: proposed.blockCount,
530
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
531
+ });
532
+ // Update the slot-to-checkpoint index
533
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
534
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
535
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
536
+ // Update the last synced L1 block
537
+ await this.#lastSynchedL1Block.set(l1.blockNumber);
538
+ });
539
+ }
540
+ /**
541
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
542
+ * No fallback to confirmed.
543
+ */ async getLastProposedCheckpoint() {
544
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
545
+ reverse: true,
546
+ limit: 1
547
+ }));
548
+ if (key === undefined) {
549
+ return undefined;
550
+ }
551
+ const stored = await this.#proposedCheckpoints.getAsync(key);
552
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
553
+ }
554
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */ async getProposedCheckpointByNumber(n) {
555
+ const stored = await this.#proposedCheckpoints.getAsync(n);
556
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
557
+ }
558
+ /** Returns all pending checkpoints in ascending checkpoint-number order. */ async getProposedCheckpoints() {
559
+ const results = [];
560
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()){
561
+ results.push(this.convertToProposedCheckpointData(stored));
562
+ }
563
+ return results;
564
+ }
565
+ /**
566
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
567
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
568
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
569
+ */ async evictProposedCheckpointsFrom(fromNumber) {
570
+ const keysToDelete = [];
571
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
572
+ if (key >= fromNumber) {
573
+ keysToDelete.push(key);
574
+ }
575
+ }
576
+ for (const key of keysToDelete){
577
+ await this.#proposedCheckpoints.delete(key);
578
+ }
579
+ }
580
+ /**
581
+ * Gets the checkpoint at the proposed tip:
582
+ * - latest pending checkpoint if any exist
583
+ * - fallsback to latest confirmed checkpoint otherwise
584
+ */ async getLastCheckpoint() {
585
+ const latest = await this.getLastProposedCheckpoint();
586
+ if (!latest) {
587
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
588
+ }
589
+ return latest;
590
+ }
591
+ convertToProposedCheckpointData(stored) {
592
+ return {
593
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
594
+ header: CheckpointHeader.fromBuffer(stored.header),
595
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
596
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
597
+ startBlock: BlockNumber(stored.startBlock),
598
+ blockCount: stored.blockCount,
599
+ totalManaUsed: BigInt(stored.totalManaUsed),
600
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier)
601
+ };
602
+ }
603
+ /**
604
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
605
+ * @returns CheckpointNumber
606
+ */ async getProposedCheckpointNumber() {
607
+ const proposed = await this.getLastCheckpoint();
608
+ if (!proposed) {
609
+ return await this.getLatestCheckpointNumber();
610
+ }
611
+ return CheckpointNumber(proposed.checkpointNumber);
612
+ }
613
+ /**
614
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
615
+ * @returns BlockNumber
616
+ */ async getProposedCheckpointL2BlockNumber() {
617
+ const proposed = await this.getLastCheckpoint();
618
+ if (!proposed) {
619
+ return await this.getCheckpointedL2BlockNumber();
620
+ }
621
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
622
+ }
432
623
  async getCheckpointedBlock(number) {
433
624
  const blockStorage = await this.#blocks.getAsync(number);
434
625
  if (!blockStorage) {
@@ -502,6 +693,37 @@ export { TxReceipt } from '@aztec/stdlib/tx';
502
693
  return this.getBlockDataFromBlockStorage(blockStorage);
503
694
  }
504
695
  /**
696
+ * Gets block metadata plus checkpoint-derived context (L1 publish info, attestations) without
697
+ * deserializing tx bodies. When the block's containing checkpoint has not yet been L1-confirmed,
698
+ * `checkpoint` and `l1` are `undefined` and `attestations` is empty.
699
+ */ async getBlockDataWithCheckpointContext(blockNumber) {
700
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
701
+ if (!blockStorage || !blockStorage.header) {
702
+ return undefined;
703
+ }
704
+ const data = this.getBlockDataFromBlockStorage(blockStorage);
705
+ const checkpointStorage = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
706
+ if (!checkpointStorage) {
707
+ return {
708
+ data,
709
+ checkpoint: undefined,
710
+ l1: undefined,
711
+ attestations: []
712
+ };
713
+ }
714
+ const checkpoint = this.checkpointDataFromCheckpointStorage(checkpointStorage);
715
+ return {
716
+ data,
717
+ checkpoint,
718
+ l1: checkpoint.l1,
719
+ attestations: checkpoint.attestations
720
+ };
721
+ }
722
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */ async getCheckpointNumberBySlot(slot) {
723
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
724
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
725
+ }
726
+ /**
505
727
  * Gets block metadata (without tx data) by archive root.
506
728
  * @param archive - The archive root of the block to return.
507
729
  * @returns The requested block data.
@@ -606,7 +828,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
606
828
  return {
607
829
  header: BlockHeader.fromBuffer(blockStorage.header),
608
830
  archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
609
- blockHash: Fr.fromBuffer(blockStorage.blockHash),
831
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
610
832
  checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
611
833
  indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
612
834
  };
@@ -653,17 +875,18 @@ export { TxReceipt } from '@aztec/stdlib/tx';
653
875
  * Gets a receipt of a settled tx.
654
876
  * @param txHash - The hash of a tx we try to get the receipt for.
655
877
  * @returns The requested tx receipt (or undefined if not found).
656
- */ async getSettledTxReceipt(txHash) {
878
+ */ async getSettledTxReceipt(txHash, l1Constants) {
657
879
  const txEffect = await this.getTxEffect(txHash);
658
880
  if (!txEffect) {
659
881
  return undefined;
660
882
  }
661
883
  const blockNumber = BlockNumber(txEffect.l2BlockNumber);
662
884
  // Use existing archiver methods to determine finalization level
663
- const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
885
+ const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
664
886
  this.getProvenBlockNumber(),
665
887
  this.getCheckpointedL2BlockNumber(),
666
- this.getFinalizedL2BlockNumber()
888
+ this.getFinalizedL2BlockNumber(),
889
+ this.getBlockData(blockNumber)
667
890
  ]);
668
891
  let status;
669
892
  if (blockNumber <= finalizedBlockNumber) {
@@ -675,7 +898,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
675
898
  } else {
676
899
  status = TxStatus.PROPOSED;
677
900
  }
678
- return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber);
901
+ const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
902
+ return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
679
903
  }
680
904
  /**
681
905
  * Looks up which block included the requested tx effect.
@@ -726,6 +950,47 @@ export { TxReceipt } from '@aztec/stdlib/tx';
726
950
  setSynchedL1BlockNumber(l1BlockNumber) {
727
951
  return this.#lastSynchedL1Block.set(l1BlockNumber);
728
952
  }
953
+ /**
954
+ * Adds a proposed checkpoint to the pending queue.
955
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
956
+ * confirmed and the highest pending checkpoint number.
957
+ * Computes archive and checkpointOutHash from the stored blocks.
958
+ */ async addProposedCheckpoint(proposed) {
959
+ return await this.db.transactionAsync(async ()=>{
960
+ const confirmed = await this.getLatestCheckpointNumber();
961
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
962
+ reverse: true,
963
+ limit: 1
964
+ }));
965
+ const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
966
+ if (proposed.checkpointNumber !== latestTip + 1) {
967
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
968
+ }
969
+ // Ensure the predecessor block (from pending or confirmed chain) exists
970
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
971
+ const blocks = [];
972
+ for(let i = 0; i < proposed.blockCount; i++){
973
+ const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
974
+ if (!block) {
975
+ throw new BlockNotFoundError(proposed.startBlock + i);
976
+ }
977
+ blocks.push(block);
978
+ }
979
+ this.validateCheckpointBlocks(blocks, previousBlock);
980
+ const archive = blocks[blocks.length - 1].archive;
981
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
982
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
983
+ header: proposed.header.toBuffer(),
984
+ archive: archive.toBuffer(),
985
+ checkpointOutHash: checkpointOutHash.toBuffer(),
986
+ checkpointNumber: proposed.checkpointNumber,
987
+ startBlock: proposed.startBlock,
988
+ blockCount: proposed.blockCount,
989
+ totalManaUsed: proposed.totalManaUsed.toString(),
990
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
991
+ });
992
+ });
993
+ }
729
994
  async getProvenCheckpointNumber() {
730
995
  const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
731
996
  this.getLatestCheckpointNumber(),
@@ -737,6 +1002,16 @@ export { TxReceipt } from '@aztec/stdlib/tx';
737
1002
  const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
738
1003
  return result;
739
1004
  }
1005
+ async getFinalizedCheckpointNumber() {
1006
+ const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1007
+ this.getLatestCheckpointNumber(),
1008
+ this.#lastFinalizedCheckpoint.getAsync()
1009
+ ]);
1010
+ return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1011
+ }
1012
+ setFinalizedCheckpointNumber(checkpointNumber) {
1013
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1014
+ }
740
1015
  #computeBlockRange(start, limit) {
741
1016
  if (limit < 1) {
742
1017
  throw new Error(`Invalid limit: ${limit}`);