@aztec/archiver 0.0.1-commit.d431d1c → 0.0.1-commit.d58ff9d0

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 (138) hide show
  1. package/README.md +28 -11
  2. package/dest/archiver.d.ts +80 -21
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +347 -167
  5. package/dest/config.d.ts +7 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +38 -14
  8. package/dest/errors.d.ts +60 -9
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +88 -13
  11. package/dest/factory.d.ts +13 -6
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +59 -39
  14. package/dest/index.d.ts +12 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +11 -2
  17. package/dest/l1/bin/retrieve-calldata.js +35 -32
  18. package/dest/l1/calldata_retriever.d.ts +81 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +202 -260
  21. package/dest/l1/data_retrieval.d.ts +32 -18
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +45 -50
  24. package/dest/l1/spire_proposer.d.ts +5 -5
  25. package/dest/l1/spire_proposer.d.ts.map +1 -1
  26. package/dest/l1/spire_proposer.js +9 -17
  27. package/dest/l1/trace_tx.d.ts +12 -66
  28. package/dest/l1/trace_tx.d.ts.map +1 -1
  29. package/dest/l1/validate_historical_logs.d.ts +23 -0
  30. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  31. package/dest/l1/validate_historical_logs.js +108 -0
  32. package/dest/l1/validate_trace.d.ts +6 -3
  33. package/dest/l1/validate_trace.d.ts.map +1 -1
  34. package/dest/l1/validate_trace.js +13 -9
  35. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  36. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  37. package/dest/modules/contract_data_source_adapter.js +32 -0
  38. package/dest/modules/data_source_base.d.ts +75 -48
  39. package/dest/modules/data_source_base.d.ts.map +1 -1
  40. package/dest/modules/data_source_base.js +251 -220
  41. package/dest/modules/data_store_updater.d.ts +65 -29
  42. package/dest/modules/data_store_updater.d.ts.map +1 -1
  43. package/dest/modules/data_store_updater.js +242 -153
  44. package/dest/modules/instrumentation.d.ts +30 -4
  45. package/dest/modules/instrumentation.d.ts.map +1 -1
  46. package/dest/modules/instrumentation.js +83 -20
  47. package/dest/modules/l1_synchronizer.d.ts +16 -14
  48. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  49. package/dest/modules/l1_synchronizer.js +493 -225
  50. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  51. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  52. package/dest/modules/outbox_trees_resolver.js +162 -0
  53. package/dest/modules/validation.d.ts +30 -7
  54. package/dest/modules/validation.d.ts.map +1 -1
  55. package/dest/modules/validation.js +35 -17
  56. package/dest/store/block_store.d.ts +200 -75
  57. package/dest/store/block_store.d.ts.map +1 -1
  58. package/dest/store/block_store.js +802 -276
  59. package/dest/store/contract_class_store.d.ts +17 -4
  60. package/dest/store/contract_class_store.d.ts.map +1 -1
  61. package/dest/store/contract_class_store.js +51 -73
  62. package/dest/store/contract_instance_store.d.ts +28 -1
  63. package/dest/store/contract_instance_store.d.ts.map +1 -1
  64. package/dest/store/contract_instance_store.js +54 -2
  65. package/dest/store/data_stores.d.ts +68 -0
  66. package/dest/store/data_stores.d.ts.map +1 -0
  67. package/dest/store/data_stores.js +54 -0
  68. package/dest/store/function_names_cache.d.ts +17 -0
  69. package/dest/store/function_names_cache.d.ts.map +1 -0
  70. package/dest/store/function_names_cache.js +30 -0
  71. package/dest/store/l2_tips_cache.d.ts +25 -0
  72. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  73. package/dest/store/l2_tips_cache.js +26 -0
  74. package/dest/store/log_store.d.ts +42 -37
  75. package/dest/store/log_store.d.ts.map +1 -1
  76. package/dest/store/log_store.js +262 -388
  77. package/dest/store/log_store_codec.d.ts +78 -0
  78. package/dest/store/log_store_codec.d.ts.map +1 -0
  79. package/dest/store/log_store_codec.js +110 -0
  80. package/dest/store/message_store.d.ts +11 -1
  81. package/dest/store/message_store.d.ts.map +1 -1
  82. package/dest/store/message_store.js +51 -9
  83. package/dest/test/fake_l1_state.d.ts +28 -4
  84. package/dest/test/fake_l1_state.d.ts.map +1 -1
  85. package/dest/test/fake_l1_state.js +166 -32
  86. package/dest/test/index.js +3 -1
  87. package/dest/test/mock_archiver.d.ts +1 -1
  88. package/dest/test/mock_archiver.d.ts.map +1 -1
  89. package/dest/test/mock_archiver.js +3 -2
  90. package/dest/test/mock_l2_block_source.d.ts +65 -51
  91. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  92. package/dest/test/mock_l2_block_source.js +309 -215
  93. package/dest/test/mock_structs.d.ts +6 -2
  94. package/dest/test/mock_structs.d.ts.map +1 -1
  95. package/dest/test/mock_structs.js +26 -12
  96. package/dest/test/noop_l1_archiver.d.ts +34 -0
  97. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  98. package/dest/test/noop_l1_archiver.js +88 -0
  99. package/package.json +15 -14
  100. package/src/archiver.ts +458 -198
  101. package/src/config.ts +43 -13
  102. package/src/errors.ts +134 -21
  103. package/src/factory.ts +87 -36
  104. package/src/index.ts +19 -2
  105. package/src/l1/README.md +25 -68
  106. package/src/l1/bin/retrieve-calldata.ts +45 -33
  107. package/src/l1/calldata_retriever.ts +267 -379
  108. package/src/l1/data_retrieval.ts +70 -74
  109. package/src/l1/spire_proposer.ts +7 -15
  110. package/src/l1/validate_historical_logs.ts +140 -0
  111. package/src/l1/validate_trace.ts +24 -6
  112. package/src/modules/contract_data_source_adapter.ts +46 -0
  113. package/src/modules/data_source_base.ts +336 -288
  114. package/src/modules/data_store_updater.ts +287 -187
  115. package/src/modules/instrumentation.ts +86 -21
  116. package/src/modules/l1_synchronizer.ts +667 -278
  117. package/src/modules/outbox_trees_resolver.ts +199 -0
  118. package/src/modules/validation.ts +80 -23
  119. package/src/store/block_store.ts +984 -352
  120. package/src/store/contract_class_store.ts +57 -107
  121. package/src/store/contract_instance_store.ts +68 -5
  122. package/src/store/data_stores.ts +104 -0
  123. package/src/store/function_names_cache.ts +37 -0
  124. package/src/store/l2_tips_cache.ts +35 -0
  125. package/src/store/log_store.ts +305 -500
  126. package/src/store/log_store_codec.ts +143 -0
  127. package/src/store/message_store.ts +60 -10
  128. package/src/structs/inbox_message.ts +1 -1
  129. package/src/test/fake_l1_state.ts +215 -44
  130. package/src/test/index.ts +3 -0
  131. package/src/test/mock_archiver.ts +3 -2
  132. package/src/test/mock_l2_block_source.ts +381 -246
  133. package/src/test/mock_structs.ts +50 -17
  134. package/src/test/noop_l1_archiver.ts +158 -0
  135. package/dest/store/kv_archiver_store.d.ts +0 -336
  136. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  137. package/dest/store/kv_archiver_store.js +0 -443
  138. package/src/store/kv_archiver_store.ts +0 -631
@@ -1,32 +1,36 @@
1
1
  import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
- import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
2
+ import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
3
3
  import { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import { toArray } from '@aztec/foundation/iterable';
5
5
  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, L2BlockHash, L2BlockNew, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
10
- import { L1PublishedData } from '@aztec/stdlib/checkpoint';
9
+ import { BlockHash, Body, CommitteeAttestation, GENESIS_CHECKPOINT_HEADER_HASH, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
10
+ import { Checkpoint, L1PublishedData } from '@aztec/stdlib/checkpoint';
11
11
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
12
12
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
13
- import { BlockHeader, TxHash, TxReceipt, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
14
- import { BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CheckpointNotFoundError, CheckpointNumberNotConsistentError, CheckpointNumberNotSequentialError, InitialBlockNumberNotSequentialError, InitialCheckpointNumberNotSequentialError } from '../errors.js';
15
- export { TxReceipt } from '@aztec/stdlib/tx';
13
+ import { BlockHeader, TxHash, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
14
+ import { BlockAlreadyCheckpointedError, BlockArchiveNotConsistentError, BlockCheckpointNumberNotSequentialError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotSequentialError, InitialCheckpointNumberNotSequentialError, NoProposedCheckpointToPromoteError, ProposedCheckpointArchiveRootMismatchError, ProposedCheckpointNotSequentialError, ProposedCheckpointPromotionNotSequentialError } from '../errors.js';
16
15
  /**
17
16
  * LMDB-based block storage for the archiver.
18
17
  */ export class BlockStore {
19
18
  db;
20
19
  /** Map block number to block data */ #blocks;
21
- /** Map checkpoint number to checkpoint data */ #checkpoints;
20
+ /** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */ #proposedCheckpoints;
21
+ /** Map checkpoint number to checkpoint data for mined checkpoints only */ #checkpoints;
22
+ /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */ #slotToCheckpoint;
22
23
  /** Map block hash to list of tx hashes */ #blockTxs;
23
24
  /** Tx hash to serialized IndexedTxEffect */ #txEffects;
24
25
  /** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block;
25
26
  /** Stores last proven checkpoint */ #lastProvenCheckpoint;
27
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */ #lastFinalizedCheckpoint;
26
28
  /** Stores the pending chain validation status */ #pendingChainValidationStatus;
27
29
  /** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
28
30
  /** Index mapping block hash to block number */ #blockHashIndex;
29
31
  /** Index mapping block archive to block number */ #blockArchiveIndex;
32
+ /** Map rejected checkpoints (due to invalid attestations) by archive root */ #rejectedCheckpoints;
33
+ /** Index mapping a rejected checkpoint's number to its archive root, so the latest can be read in reverse order */ #rejectedCheckpointsByNumber;
30
34
  #log;
31
35
  constructor(db){
32
36
  this.db = db;
@@ -39,110 +43,128 @@ export { TxReceipt } from '@aztec/stdlib/tx';
39
43
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
40
44
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
41
45
  this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
46
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
42
47
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
43
48
  this.#checkpoints = db.openMap('archiver_checkpoints');
49
+ this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
50
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
51
+ this.#rejectedCheckpoints = db.openMap('archiver_rejected_checkpoints');
52
+ this.#rejectedCheckpointsByNumber = db.openMap('archiver_rejected_checkpoints_by_number');
44
53
  }
45
54
  /**
46
- * Append new blocks to the store's list. All blocks must be for the 'current' checkpoint
47
- * @param blocks - The L2 blocks to be added to the store.
48
- * @returns True if the operation is successful.
49
- */ async addBlocks(blocks, opts = {}) {
50
- if (blocks.length === 0) {
51
- return true;
55
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
56
+ * in an L1 block that has itself been finalized on Ethereum.
57
+ * @returns The finalized block number.
58
+ */ async getFinalizedL2BlockNumber() {
59
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
60
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
61
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
52
62
  }
63
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
64
+ if (!checkpointStorage) {
65
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
66
+ }
67
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
68
+ }
69
+ /**
70
+ * Append a new proposed block to the store.
71
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
72
+ * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
73
+ * @param block - The proposed L2 block to be added to the store.
74
+ * @returns True if the operation is successful.
75
+ */ async addProposedBlock(block, opts = {}) {
53
76
  return await this.db.transactionAsync(async ()=>{
54
- // Check that the block immediately before the first block to be added is present in the store.
55
- const firstBlockNumber = blocks[0].number;
56
- const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
57
- const firstBlockIndex = blocks[0].indexWithinCheckpoint;
58
- const firstBlockLastArchive = blocks[0].header.lastArchive.root;
77
+ const blockNumber = block.number;
78
+ const blockCheckpointNumber = block.checkpointNumber;
79
+ const blockIndex = block.indexWithinCheckpoint;
80
+ const blockLastArchive = block.header.lastArchive.root;
59
81
  // Extract the latest block and checkpoint numbers
60
- const previousBlockNumber = await this.getLatestBlockNumber();
61
- const previousCheckpointNumber = await this.getLatestCheckpointNumber();
62
- // Check that the first block number is the expected one
63
- if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
64
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
82
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
83
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
84
+ // Verify we're not overwriting checkpointed blocks
85
+ const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
86
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
87
+ // Check if the proposed block matches the already-checkpointed one
88
+ const existingBlock = await this.getBlockData({
89
+ number: BlockNumber(blockNumber)
90
+ });
91
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
92
+ throw new BlockAlreadyCheckpointedError(blockNumber);
93
+ }
94
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
65
95
  }
66
- // The same check as above but for checkpoints
67
- if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
68
- throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
96
+ // Check that the block number is the expected one
97
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
98
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
99
+ }
100
+ // Accept the block if either the confirmed checkpoint or a pending checkpoint matches
101
+ // the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
102
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
103
+ const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
104
+ if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
105
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
106
+ reverse: true,
107
+ limit: 1
108
+ }));
109
+ const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
110
+ throw new BlockCheckpointNumberNotSequentialError(BlockNumber(blockNumber), blockCheckpointNumber, previous);
69
111
  }
70
112
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
71
- const previousBlockResult = await this.getBlock(previousBlockNumber);
72
- let expectedFirstblockIndex = 0;
113
+ const previousBlockResult = await this.getBlockData({
114
+ number: previousBlockNumber
115
+ });
116
+ let expectedBlockIndex = 0;
73
117
  let previousBlockIndex = undefined;
74
118
  if (previousBlockResult !== undefined) {
75
- if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
119
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
76
120
  // The previous block is for the same checkpoint, therefore our index should follow it
77
121
  previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
78
- expectedFirstblockIndex = previousBlockIndex + 1;
122
+ expectedBlockIndex = previousBlockIndex + 1;
79
123
  }
80
- if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
81
- throw new BlockArchiveNotConsistentError(firstBlockNumber, previousBlockResult.number, firstBlockLastArchive, previousBlockResult.archive.root);
124
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
125
+ throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.header.globalVariables.blockNumber, blockLastArchive, previousBlockResult.archive.root);
82
126
  }
83
127
  }
84
- // Now check that the first block has the expected index value
85
- if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
86
- throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
87
- }
88
- // Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
89
- let previousBlock = undefined;
90
- for (const block of blocks){
91
- if (!opts.force && previousBlock) {
92
- if (previousBlock.number + 1 !== block.number) {
93
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
94
- }
95
- if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
96
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
97
- }
98
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
99
- throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
100
- }
101
- }
102
- if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
103
- throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
104
- }
105
- previousBlock = block;
106
- await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
128
+ // Now check that the block has the expected index value
129
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
130
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
107
131
  }
132
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
108
133
  return true;
109
134
  });
110
135
  }
111
136
  /**
112
- * Append new cheskpoints to the store's list.
137
+ * Append new checkpoints to the store's list.
138
+ * Checkpoints at the start of the batch that are already stored (e.g. re-included by an L1 reorg)
139
+ * are accepted if their archive root matches: their L1 metadata is updated but they are not
140
+ * re-inserted, and they are excluded from the returned array.
113
141
  * @param checkpoints - The L2 checkpoints to be added to the store.
114
- * @returns True if the operation is successful.
142
+ * @returns The checkpoints that were actually inserted (excluding already-stored ones).
115
143
  */ async addCheckpoints(checkpoints, opts = {}) {
116
144
  if (checkpoints.length === 0) {
117
- return true;
145
+ return [];
118
146
  }
119
147
  return await this.db.transactionAsync(async ()=>{
120
- // Check that the checkpoint immediately before the first block to be added is present in the store.
121
148
  const firstCheckpointNumber = checkpoints[0].checkpoint.number;
122
149
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
123
- if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
124
- throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
125
- }
126
- // Extract the previous checkpoint if there is one
127
- let previousCheckpointData = undefined;
128
- if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
129
- // There should be a previous checkpoint
130
- previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
131
- if (previousCheckpointData === undefined) {
132
- throw new CheckpointNotFoundError(previousCheckpointNumber);
150
+ // Handle already-stored checkpoints at the start of the batch.
151
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
152
+ // We accept them if archives match (same content) and update their L1 metadata.
153
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
154
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
155
+ if (checkpoints.length === 0) {
156
+ return [];
133
157
  }
134
- }
135
- let previousBlockNumber = undefined;
136
- let previousBlock = undefined;
137
- // If we have a previous checkpoint then we need to get the previous block number
138
- if (previousCheckpointData !== undefined) {
139
- previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.numBlocks - 1);
140
- previousBlock = await this.getBlock(previousBlockNumber);
141
- if (previousBlock === undefined) {
142
- // We should be able to get the required previous block
143
- throw new BlockNotFoundError(previousBlockNumber);
158
+ // Re-check sequentiality after skipping
159
+ const newFirstNumber = checkpoints[0].checkpoint.number;
160
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
161
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
144
162
  }
163
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
164
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
145
165
  }
166
+ // Get the last block of the previous checkpoint for archive chaining
167
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
146
168
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
147
169
  let previousCheckpoint = undefined;
148
170
  for (const checkpoint of checkpoints){
@@ -150,50 +172,125 @@ export { TxReceipt } from '@aztec/stdlib/tx';
150
172
  throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
151
173
  }
152
174
  previousCheckpoint = checkpoint;
153
- // Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
175
+ // Validate block sequencing, indexes, and archive chaining
176
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
177
+ // Store every block in the database (may already exist, but L1 data is authoritative)
154
178
  for(let i = 0; i < checkpoint.checkpoint.blocks.length; i++){
155
- const block = checkpoint.checkpoint.blocks[i];
156
- if (previousBlock) {
157
- // The blocks should have a sequential block number
158
- if (previousBlock.number !== block.number - 1) {
159
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
160
- }
161
- // If the blocks are for the same checkpoint then they should have sequential indexes
162
- if (previousBlock.checkpointNumber === block.checkpointNumber && previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
163
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
164
- }
165
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
166
- throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
167
- }
168
- } else {
169
- // No previous block, must be block 1 at checkpoint index 0
170
- if (block.indexWithinCheckpoint !== 0) {
171
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
172
- }
173
- if (block.number !== INITIAL_L2_BLOCK_NUM) {
174
- throw new BlockNumberNotSequentialError(block.number, undefined);
175
- }
176
- }
177
- previousBlock = block;
178
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
179
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
179
180
  }
181
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
180
182
  // Store the checkpoint in the database
181
183
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
182
184
  header: checkpoint.checkpoint.header.toBuffer(),
183
185
  archive: checkpoint.checkpoint.archive.toBuffer(),
186
+ checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
184
187
  l1: checkpoint.l1.toBuffer(),
185
188
  attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
186
189
  checkpointNumber: checkpoint.checkpoint.number,
187
190
  startBlock: checkpoint.checkpoint.blocks[0].number,
188
- numBlocks: checkpoint.checkpoint.blocks.length
191
+ blockCount: checkpoint.checkpoint.blocks.length,
192
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
189
193
  });
194
+ // Update slot-to-checkpoint index
195
+ await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
196
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
197
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
198
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
199
+ // (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
200
+ await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
190
201
  }
191
- await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
192
- return true;
202
+ await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
203
+ return checkpoints;
193
204
  });
194
205
  }
206
+ /**
207
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
208
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
209
+ */ async skipOrUpdateAlreadyStoredCheckpoints(checkpoints, latestStored) {
210
+ let i = 0;
211
+ for(; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++){
212
+ const incoming = checkpoints[i];
213
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
214
+ if (!stored) {
215
+ break;
216
+ }
217
+ // Verify the checkpoint content matches (archive root)
218
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
219
+ 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}`);
220
+ }
221
+ // Update L1 metadata and attestations for the already-stored checkpoint
222
+ this.#log.warn(`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` + `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`);
223
+ await this.#checkpoints.set(incoming.checkpoint.number, {
224
+ header: incoming.checkpoint.header.toBuffer(),
225
+ archive: incoming.checkpoint.archive.toBuffer(),
226
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
227
+ l1: incoming.l1.toBuffer(),
228
+ attestations: incoming.attestations.map((a)=>a.toBuffer()),
229
+ checkpointNumber: incoming.checkpoint.number,
230
+ startBlock: incoming.checkpoint.blocks[0].number,
231
+ blockCount: incoming.checkpoint.blocks.length,
232
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString()
233
+ });
234
+ // Update the sync point to reflect the new L1 block
235
+ await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
236
+ }
237
+ return checkpoints.slice(i);
238
+ }
239
+ /**
240
+ * Gets the last block of the checkpoint before the given one.
241
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
242
+ */ async getPreviousCheckpointBlock(checkpointNumber) {
243
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
244
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
245
+ return undefined;
246
+ }
247
+ // Check across both proposed and mined checkpoints
248
+ const predecessor = await this.getProposedCheckpointByNumber(previousCheckpointNumber) ?? await this.getCheckpointData(previousCheckpointNumber);
249
+ if (!predecessor) {
250
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
251
+ }
252
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
253
+ const previousBlock = await this.getBlock({
254
+ number: previousBlockNumber
255
+ });
256
+ if (previousBlock === undefined) {
257
+ throw new BlockNotFoundError(previousBlockNumber);
258
+ }
259
+ return previousBlock;
260
+ }
261
+ /**
262
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
263
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
264
+ * proposed checkpoints (addProposedCheckpoint).
265
+ */ validateCheckpointBlocks(blocks, previousBlock) {
266
+ for (const block of blocks){
267
+ if (previousBlock) {
268
+ if (previousBlock.number !== block.number - 1) {
269
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
270
+ }
271
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
272
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
273
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
274
+ }
275
+ } else if (block.indexWithinCheckpoint !== 0) {
276
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
277
+ }
278
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
279
+ throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
280
+ }
281
+ } else {
282
+ if (block.indexWithinCheckpoint !== 0) {
283
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
284
+ }
285
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
286
+ throw new BlockNumberNotSequentialError(block.number, undefined);
287
+ }
288
+ }
289
+ previousBlock = block;
290
+ }
291
+ }
195
292
  async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
196
- const blockHash = L2BlockHash.fromField(await block.hash());
293
+ const blockHash = await block.hash();
197
294
  await this.#blocks.set(block.number, {
198
295
  header: block.header.toBuffer(),
199
296
  blockHash: blockHash.toBuffer(),
@@ -206,7 +303,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
206
303
  data: block.body.txEffects[i],
207
304
  l2BlockNumber: block.number,
208
305
  l2BlockHash: blockHash,
209
- txIndexInBlock: i
306
+ txIndexInBlock: i,
307
+ slotNumber: block.header.globalVariables.slotNumber
210
308
  };
211
309
  await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
212
310
  }
@@ -228,41 +326,52 @@ export { TxReceipt } from '@aztec/stdlib/tx';
228
326
  await this.#blockArchiveIndex.delete(block.archive.root.toString());
229
327
  }
230
328
  /**
231
- * Unwinds checkpoints from the database
232
- * @param from - The tip of the chain, passed for verification purposes,
233
- * ensuring that we don't end up deleting something we did not intend
234
- * @param checkpointsToUnwind - The number of checkpoints we are to unwind
235
- * @returns True if the operation is successful
236
- */ async unwindCheckpoints(from, checkpointsToUnwind) {
329
+ * Removes all checkpoints with checkpoint number > checkpointNumber.
330
+ * Also removes ALL blocks (both checkpointed and uncheckpointed) after the last block of the given checkpoint.
331
+ * @param checkpointNumber - Remove all checkpoints strictly after this one.
332
+ */ async removeCheckpointsAfter(checkpointNumber) {
237
333
  return await this.db.transactionAsync(async ()=>{
238
- const last = await this.getLatestCheckpointNumber();
239
- if (from !== last) {
240
- throw new Error(`Can only unwind checkpoints from the tip (requested ${from} but current tip is ${last})`);
334
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
335
+ if (checkpointNumber >= latestCheckpointNumber) {
336
+ this.#log.warn(`No checkpoints to remove after ${checkpointNumber} (latest is ${latestCheckpointNumber})`);
337
+ return {
338
+ blocksRemoved: undefined
339
+ };
241
340
  }
341
+ // If the proven checkpoint is beyond the target, update it
242
342
  const proven = await this.getProvenCheckpointNumber();
243
- if (from - checkpointsToUnwind < proven) {
244
- await this.setProvenCheckpointNumber(CheckpointNumber(from - checkpointsToUnwind));
245
- }
246
- for(let i = 0; i < checkpointsToUnwind; i++){
247
- const checkpointNumber = from - i;
248
- const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
249
- if (checkpoint === undefined) {
250
- this.#log.warn(`Cannot remove checkpoint ${checkpointNumber} from the store since we don't have it`);
251
- continue;
343
+ if (proven > checkpointNumber) {
344
+ this.#log.warn(`Updating proven checkpoint ${proven} to last valid checkpoint ${checkpointNumber}`);
345
+ await this.setProvenCheckpointNumber(checkpointNumber);
346
+ }
347
+ // Find the last block number to keep (last block of the given checkpoint, or 0 if no checkpoint)
348
+ let lastBlockToKeep;
349
+ if (checkpointNumber <= 0) {
350
+ lastBlockToKeep = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
351
+ } else {
352
+ const targetCheckpoint = await this.#checkpoints.getAsync(checkpointNumber);
353
+ if (!targetCheckpoint) {
354
+ throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
252
355
  }
253
- await this.#checkpoints.delete(checkpointNumber);
254
- const maxBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
255
- for(let blockNumber = checkpoint.startBlock; blockNumber <= maxBlock; blockNumber++){
256
- const block = await this.getBlock(BlockNumber(blockNumber));
257
- if (block === undefined) {
258
- this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
259
- continue;
260
- }
261
- await this.deleteBlock(block);
262
- this.#log.debug(`Unwound block ${blockNumber} ${(await block.hash()).toString()} for checkpoint ${checkpointNumber}`);
356
+ lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
357
+ }
358
+ // Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
359
+ const blocksRemoved = await this.removeBlocksAfter(lastBlockToKeep);
360
+ // Remove all checkpoints after the target
361
+ for(let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)){
362
+ const checkpointStorage = await this.#checkpoints.getAsync(c);
363
+ if (checkpointStorage) {
364
+ const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
365
+ await this.#slotToCheckpoint.delete(slotNumber);
263
366
  }
367
+ await this.#checkpoints.delete(c);
368
+ this.#log.debug(`Removed checkpoint ${c}`);
264
369
  }
265
- return true;
370
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
371
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
372
+ return {
373
+ blocksRemoved
374
+ };
266
375
  });
267
376
  }
268
377
  async getCheckpointData(checkpointNumber) {
@@ -283,17 +392,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
283
392
  }
284
393
  return checkpoints;
285
394
  }
395
+ /**
396
+ * Returns up to `limit` checkpoints anchored at `fromSlot`, ordered nearest-first, walking the slot index.
397
+ * With `reverse`, takes the checkpoints at or before `fromSlot` (descending by slot); otherwise the
398
+ * checkpoints at or after it (ascending). `limit: 1, reverse: true` yields the latest checkpoint at or
399
+ * before the slot in a single range scan.
400
+ */ async getCheckpointsBySlot(fromSlot, limit, reverse) {
401
+ // The KV range bounds are direction-dependent: forward is [start, end), reverse is (start, end], so a
402
+ // reverse scan uses `end: fromSlot` (inclusive) with no +1 to include the checkpoint at fromSlot itself.
403
+ const range = reverse ? {
404
+ end: fromSlot,
405
+ reverse: true,
406
+ limit
407
+ } : {
408
+ start: fromSlot,
409
+ limit
410
+ };
411
+ const result = [];
412
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync(range)){
413
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
414
+ if (checkpointStorage) {
415
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
416
+ }
417
+ }
418
+ return result;
419
+ }
420
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */ async getCheckpointDataForSlotRange(startSlot, endSlot) {
421
+ const result = [];
422
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
423
+ start: startSlot,
424
+ end: endSlot + 1
425
+ })){
426
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
427
+ if (checkpointStorage) {
428
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
429
+ }
430
+ }
431
+ return result;
432
+ }
433
+ /**
434
+ * Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
435
+ * Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
436
+ * checkpoints fall in the range and will fetch full data for at most a few of them.
437
+ */ async getCheckpointNumbersForSlotRange(startSlot, endSlot) {
438
+ const result = [];
439
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
440
+ start: startSlot,
441
+ end: endSlot + 1
442
+ })){
443
+ result.push(CheckpointNumber(checkpointNumber));
444
+ }
445
+ return result;
446
+ }
286
447
  checkpointDataFromCheckpointStorage(checkpointStorage) {
287
- const data = {
448
+ return {
288
449
  header: CheckpointHeader.fromBuffer(checkpointStorage.header),
289
450
  archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
451
+ checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
290
452
  checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
291
- startBlock: checkpointStorage.startBlock,
292
- numBlocks: checkpointStorage.numBlocks,
453
+ startBlock: BlockNumber(checkpointStorage.startBlock),
454
+ blockCount: checkpointStorage.blockCount,
455
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
293
456
  l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
294
- attestations: checkpointStorage.attestations
457
+ attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
295
458
  };
296
- return data;
297
459
  }
298
460
  async getBlocksForCheckpoint(checkpointNumber) {
299
461
  const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
@@ -302,7 +464,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
302
464
  }
303
465
  const blocksForCheckpoint = await toArray(this.#blocks.entriesAsync({
304
466
  start: checkpoint.startBlock,
305
- end: checkpoint.startBlock + checkpoint.numBlocks
467
+ end: checkpoint.startBlock + checkpoint.blockCount
306
468
  }));
307
469
  const converted = await Promise.all(blocksForCheckpoint.map((x)=>this.getBlockFromBlockStorage(x[0], x[1])));
308
470
  return converted.filter(isDefined);
@@ -332,16 +494,19 @@ export { TxReceipt } from '@aztec/stdlib/tx';
332
494
  }
333
495
  /**
334
496
  * Removes all blocks with block number > blockNumber.
497
+ * Does not remove any associated checkpoints.
335
498
  * @param blockNumber - The block number to remove after.
336
499
  * @returns The removed blocks (for event emission).
337
- */ async unwindBlocksAfter(blockNumber) {
500
+ */ async removeBlocksAfter(blockNumber) {
338
501
  return await this.db.transactionAsync(async ()=>{
339
502
  const removedBlocks = [];
340
503
  // Get the latest block number to determine the range
341
- const latestBlockNumber = await this.getLatestBlockNumber();
504
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
342
505
  // Iterate from blockNumber + 1 to latestBlockNumber
343
506
  for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
344
- const block = await this.getBlock(BlockNumber(bn));
507
+ const block = await this.getBlock({
508
+ number: BlockNumber(bn)
509
+ });
345
510
  if (block === undefined) {
346
511
  this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
347
512
  continue;
@@ -362,16 +527,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
362
527
  if (!checkpointStorage) {
363
528
  throw new CheckpointNotFoundError(provenCheckpointNumber);
364
529
  } else {
365
- return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
530
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
366
531
  }
367
532
  }
368
- async getLatestBlockNumber() {
369
- const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({
370
- reverse: true,
371
- limit: 1
372
- }));
373
- return typeof latestBlocknumber === 'number' ? BlockNumber(latestBlocknumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
374
- }
375
533
  async getLatestCheckpointNumber() {
376
534
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
377
535
  reverse: true,
@@ -382,121 +540,171 @@ export { TxReceipt } from '@aztec/stdlib/tx';
382
540
  }
383
541
  return CheckpointNumber(latestCheckpointNumber);
384
542
  }
385
- async getCheckpointedBlock(number) {
386
- const blockStorage = await this.#blocks.getAsync(number);
387
- if (!blockStorage) {
388
- return undefined;
389
- }
390
- const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
391
- if (!checkpoint) {
392
- return undefined;
393
- }
394
- const block = await this.getBlockFromBlockStorage(number, blockStorage);
395
- if (!block) {
396
- return undefined;
543
+ async hasProposedCheckpoint() {
544
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
545
+ limit: 1
546
+ }));
547
+ return key !== undefined;
548
+ }
549
+ /** Deletes all pending proposed checkpoints from storage. */ async deleteProposedCheckpoints() {
550
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
551
+ await this.#proposedCheckpoints.delete(key);
397
552
  }
398
- return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
399
553
  }
400
554
  /**
401
- * Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
402
- * @param start - Number of the first block to return (inclusive).
403
- * @param limit - The number of blocks to return.
404
- * @returns The requested L2 blocks
405
- */ async *getCheckpointedBlocks(start, limit) {
406
- const checkpointCache = new Map();
407
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
408
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
409
- if (block) {
410
- const checkpoint = checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ?? await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
411
- if (checkpoint) {
412
- checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
413
- const checkpointedBlock = new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
414
- yield checkpointedBlock;
415
- }
555
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
556
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
557
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
558
+ * @param checkpointNumber - The checkpoint number to promote.
559
+ * @param l1 - L1 published data for the checkpoint.
560
+ * @param attestations - Committee attestations.
561
+ * @param expectedArchiveRoot - Archive root guard against races.
562
+ */ async promoteProposedToCheckpointed(checkpointNumber, l1, attestations, expectedArchiveRoot) {
563
+ return await this.db.transactionAsync(async ()=>{
564
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
565
+ if (!proposed) {
566
+ throw new NoProposedCheckpointToPromoteError();
416
567
  }
417
- }
568
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
569
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
570
+ }
571
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
572
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
573
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
574
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
575
+ }
576
+ // Write the checkpoint entry
577
+ await this.#checkpoints.set(proposed.checkpointNumber, {
578
+ header: proposed.header.toBuffer(),
579
+ archive: proposed.archive.toBuffer(),
580
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
581
+ l1: l1.toBuffer(),
582
+ attestations: attestations.map((attestation)=>attestation.toBuffer()),
583
+ checkpointNumber: proposed.checkpointNumber,
584
+ startBlock: proposed.startBlock,
585
+ blockCount: proposed.blockCount,
586
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
587
+ });
588
+ // Update the slot-to-checkpoint index
589
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
590
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
591
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
592
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
593
+ // (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
594
+ await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
595
+ // Update the last synced L1 block
596
+ await this.advanceSynchedL1BlockNumber(l1.blockNumber);
597
+ });
418
598
  }
419
- async getCheckpointedBlockByHash(blockHash) {
420
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
421
- if (blockNumber === undefined) {
599
+ /**
600
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
601
+ * No fallback to confirmed.
602
+ */ async getLastProposedCheckpoint() {
603
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
604
+ reverse: true,
605
+ limit: 1
606
+ }));
607
+ if (key === undefined) {
422
608
  return undefined;
423
609
  }
424
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
610
+ const stored = await this.#proposedCheckpoints.getAsync(key);
611
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
425
612
  }
426
- async getCheckpointedBlockByArchive(archive) {
427
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
428
- if (blockNumber === undefined) {
429
- return undefined;
430
- }
431
- return this.getCheckpointedBlock(BlockNumber(blockNumber));
613
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */ async getProposedCheckpointByNumber(n) {
614
+ const stored = await this.#proposedCheckpoints.getAsync(n);
615
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
432
616
  }
433
617
  /**
434
- * Gets up to `limit` amount of L2 blocks starting from `from`.
435
- * @param start - Number of the first block to return (inclusive).
436
- * @param limit - The number of blocks to return.
437
- * @returns The requested L2 blocks
438
- */ async *getBlocks(start, limit) {
439
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
440
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
441
- if (block) {
442
- yield block;
618
+ * Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
619
+ * Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
620
+ * in normal operation (bounded by the proposer pipelining window). Returns the first match.
621
+ */ async getProposedCheckpointBySlot(slot) {
622
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()){
623
+ const header = CheckpointHeader.fromBuffer(stored.header);
624
+ if (header.slotNumber === slot) {
625
+ return this.convertToProposedCheckpointData(stored);
443
626
  }
444
627
  }
628
+ return undefined;
445
629
  }
446
630
  /**
447
- * Gets an L2 block.
448
- * @param blockNumber - The number of the block to return.
449
- * @returns The requested L2 block.
450
- */ async getBlock(blockNumber) {
451
- const blockStorage = await this.#blocks.getAsync(blockNumber);
452
- if (!blockStorage || !blockStorage.header) {
453
- return Promise.resolve(undefined);
631
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
632
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
633
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
634
+ */ async evictProposedCheckpointsFrom(fromNumber) {
635
+ const keysToDelete = [];
636
+ for await (const key of this.#proposedCheckpoints.keysAsync()){
637
+ if (key >= fromNumber) {
638
+ keysToDelete.push(key);
639
+ }
640
+ }
641
+ for (const key of keysToDelete){
642
+ await this.#proposedCheckpoints.delete(key);
454
643
  }
455
- return this.getBlockFromBlockStorage(blockNumber, blockStorage);
456
644
  }
457
645
  /**
458
- * Gets an L2 block by its hash.
459
- * @param blockHash - The hash of the block to return.
460
- * @returns The requested L2 block.
461
- */ async getBlockByHash(blockHash) {
462
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
463
- if (blockNumber === undefined) {
464
- return undefined;
646
+ * Gets the checkpoint at the proposed tip:
647
+ * - latest pending checkpoint if any exist
648
+ * - fallsback to latest confirmed checkpoint otherwise
649
+ */ async getLastCheckpoint() {
650
+ const latest = await this.getLastProposedCheckpoint();
651
+ if (!latest) {
652
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
465
653
  }
466
- return this.getBlock(BlockNumber(blockNumber));
654
+ return latest;
655
+ }
656
+ convertToProposedCheckpointData(stored) {
657
+ return {
658
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
659
+ header: CheckpointHeader.fromBuffer(stored.header),
660
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
661
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
662
+ startBlock: BlockNumber(stored.startBlock),
663
+ blockCount: stored.blockCount,
664
+ totalManaUsed: BigInt(stored.totalManaUsed),
665
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier)
666
+ };
467
667
  }
468
668
  /**
469
- * Gets an L2 block by its archive root.
470
- * @param archive - The archive root of the block to return.
471
- * @returns The requested L2 block.
472
- */ async getBlockByArchive(archive) {
473
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
474
- if (blockNumber === undefined) {
475
- return undefined;
669
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
670
+ * @returns CheckpointNumber
671
+ */ async getProposedCheckpointNumber() {
672
+ const proposed = await this.getLastCheckpoint();
673
+ if (!proposed) {
674
+ return await this.getLatestCheckpointNumber();
476
675
  }
477
- return this.getBlock(BlockNumber(blockNumber));
676
+ return CheckpointNumber(proposed.checkpointNumber);
478
677
  }
479
678
  /**
480
- * Gets a block header by its hash.
481
- * @param blockHash - The hash of the block to return.
482
- * @returns The requested block header.
483
- */ async getBlockHeaderByHash(blockHash) {
484
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
679
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
680
+ * @returns BlockNumber
681
+ */ async getProposedCheckpointL2BlockNumber() {
682
+ const proposed = await this.getLastCheckpoint();
683
+ if (!proposed) {
684
+ return await this.getCheckpointedL2BlockNumber();
685
+ }
686
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
687
+ }
688
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */ async getCheckpointNumberBySlot(slot) {
689
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
690
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
691
+ }
692
+ /** Gets a single L2 block matching the given resolved query. */ async getBlock(query) {
693
+ const blockNumber = await this.getBlockNumber(query);
485
694
  if (blockNumber === undefined) {
486
695
  return undefined;
487
696
  }
488
697
  const blockStorage = await this.#blocks.getAsync(blockNumber);
489
- if (!blockStorage || !blockStorage.header) {
698
+ if (!blockStorage) {
490
699
  return undefined;
491
700
  }
492
- return BlockHeader.fromBuffer(blockStorage.header);
701
+ return this.getBlockFromBlockStorage(blockNumber, blockStorage);
493
702
  }
494
- /**
495
- * Gets a block header by its archive root.
496
- * @param archive - The archive root of the block to return.
497
- * @returns The requested block header.
498
- */ async getBlockHeaderByArchive(archive) {
499
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
703
+ /** Gets a collection of L2 blocks for a resolved range. */ getBlocks(query) {
704
+ return toArray(this.iterateBlocks(query));
705
+ }
706
+ /** Gets single block metadata matching the given resolved query. */ async getBlockData(query) {
707
+ const blockNumber = await this.getBlockNumber(query);
500
708
  if (blockNumber === undefined) {
501
709
  return undefined;
502
710
  }
@@ -504,20 +712,30 @@ export { TxReceipt } from '@aztec/stdlib/tx';
504
712
  if (!blockStorage || !blockStorage.header) {
505
713
  return undefined;
506
714
  }
507
- return BlockHeader.fromBuffer(blockStorage.header);
715
+ return this.getBlockDataFromBlockStorage(blockStorage);
508
716
  }
509
- /**
510
- * Gets the headers for a sequence of L2 blocks.
511
- * @param start - Number of the first block to return (inclusive).
512
- * @param limit - The number of blocks to return.
513
- * @returns The requested L2 block headers
514
- */ async *getBlockHeaders(start, limit) {
515
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
516
- const header = BlockHeader.fromBuffer(blockStorage.header);
517
- if (header.getBlockNumber() !== blockNumber) {
518
- throw new Error(`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`);
717
+ /** Gets a collection of block metadata entries for a resolved range. */ getBlocksData(query) {
718
+ return toArray(this.iterateBlocksData(query));
719
+ }
720
+ /** Async iterator over L2 blocks for a resolved range. */ async *iterateBlocks(query) {
721
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
722
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
723
+ if (cap !== undefined && blockNumber > cap) {
724
+ break;
725
+ }
726
+ const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
727
+ if (block) {
728
+ yield block;
519
729
  }
520
- yield header;
730
+ }
731
+ }
732
+ /** Async iterator over block metadata for a resolved range. */ async *iterateBlocksData(query) {
733
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
734
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
735
+ if (cap !== undefined && blockNumber > cap) {
736
+ break;
737
+ }
738
+ yield this.getBlockDataFromBlockStorage(blockStorage);
521
739
  }
522
740
  }
523
741
  async *getBlockStorages(start, limit) {
@@ -533,12 +751,35 @@ export { TxReceipt } from '@aztec/stdlib/tx';
533
751
  ];
534
752
  }
535
753
  }
754
+ /** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */ async getBlockNumber(query) {
755
+ let blockNumber;
756
+ if ('number' in query) {
757
+ blockNumber = query.number;
758
+ } else if ('hash' in query) {
759
+ const n = await this.#blockHashIndex.getAsync(query.hash.toString());
760
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
761
+ } else {
762
+ const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
763
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
764
+ }
765
+ if (blockNumber === undefined) {
766
+ return undefined;
767
+ }
768
+ return blockNumber;
769
+ }
770
+ getBlockDataFromBlockStorage(blockStorage) {
771
+ return {
772
+ header: BlockHeader.fromBuffer(blockStorage.header),
773
+ archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
774
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
775
+ checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
776
+ indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
777
+ };
778
+ }
536
779
  async getBlockFromBlockStorage(blockNumber, blockStorage) {
537
- const header = BlockHeader.fromBuffer(blockStorage.header);
538
- const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
539
- const blockHash = blockStorage.blockHash;
540
- header.setHash(Fr.fromBuffer(blockHash));
541
- const blockHashString = bufferToHex(blockHash);
780
+ const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } = this.getBlockDataFromBlockStorage(blockStorage);
781
+ header.setHash(blockHash);
782
+ const blockHashString = bufferToHex(blockStorage.blockHash);
542
783
  const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
543
784
  if (blockTxsBuffer === undefined) {
544
785
  this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
@@ -556,7 +797,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
556
797
  txEffects.push(deserializeIndexedTxEffect(txEffect).data);
557
798
  }
558
799
  const body = new Body(txEffects);
559
- const block = new L2BlockNew(archive, header, body, CheckpointNumber(blockStorage.checkpointNumber), IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint));
800
+ const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
560
801
  if (block.number !== blockNumber) {
561
802
  throw new Error(`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${block.number} with hash ${blockHashString})`);
562
803
  }
@@ -574,17 +815,6 @@ export { TxReceipt } from '@aztec/stdlib/tx';
574
815
  return deserializeIndexedTxEffect(buffer);
575
816
  }
576
817
  /**
577
- * Gets a receipt of a settled tx.
578
- * @param txHash - The hash of a tx we try to get the receipt for.
579
- * @returns The requested tx receipt (or undefined if not found).
580
- */ async getSettledTxReceipt(txHash) {
581
- const txEffect = await this.getTxEffect(txHash);
582
- if (!txEffect) {
583
- return undefined;
584
- }
585
- return new TxReceipt(txHash, TxReceipt.statusFromRevertCode(txEffect.data.revertCode), '', txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, BlockNumber(txEffect.l2BlockNumber));
586
- }
587
- /**
588
818
  * Looks up which block included the requested tx effect.
589
819
  * @param txHash - The txHash of the tx.
590
820
  * @returns The block number and index of the tx.
@@ -593,13 +823,50 @@ export { TxReceipt } from '@aztec/stdlib/tx';
593
823
  if (!txEffect) {
594
824
  return undefined;
595
825
  }
596
- const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
826
+ // Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
827
+ // large tail (the full TxEffect with logs etc.) is irrelevant here.
828
+ const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
829
+ const l2BlockNumber = view.readUInt32BE(32);
830
+ const txIndexInBlock = view.readUInt32BE(36);
597
831
  return [
598
832
  l2BlockNumber,
599
833
  txIndexInBlock
600
834
  ];
601
835
  }
602
836
  /**
837
+ * Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
838
+ * txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
839
+ * unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
840
+ * attach effect data on demand without paying for a full {@link TxEffect} deserialization.
841
+ *
842
+ * The on-disk `IndexedTxEffect` layout starts with a fixed-length header
843
+ * (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + slotNumber(4) + revertCode(1) + txHash(32) +
844
+ * transactionFee(32)` = 109 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr`
845
+ * vectors). We
846
+ * skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
847
+ * `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
848
+ */ getNoteHashesAndNullifiers(txHashes) {
849
+ return Promise.all(txHashes.map(async (txHash)=>{
850
+ const buffer = await this.#txEffects.getAsync(txHash.toString());
851
+ if (!buffer) {
852
+ return [
853
+ [],
854
+ []
855
+ ];
856
+ }
857
+ const reader = BufferReader.asReader(buffer);
858
+ // Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + slotNumber + revertCode +
859
+ // txHash + transactionFee.
860
+ reader.readBytes(32 + 4 + 4 + 4 + 1 + 32 + 32);
861
+ const noteHashes = reader.readVectorUint8Prefix(Fr);
862
+ const nullifiers = reader.readVectorUint8Prefix(Fr);
863
+ return [
864
+ noteHashes,
865
+ nullifiers
866
+ ];
867
+ }));
868
+ }
869
+ /**
603
870
  * Looks up which block deployed a particular contract.
604
871
  * @param contractAddress - The address of the contract to look up.
605
872
  * @returns The block number and index of the contract.
@@ -615,7 +882,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
615
882
  if (!checkpoint) {
616
883
  return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
617
884
  }
618
- return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
885
+ return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
619
886
  }
620
887
  async getLatestL2BlockNumber() {
621
888
  const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({
@@ -625,6 +892,142 @@ export { TxReceipt } from '@aztec/stdlib/tx';
625
892
  return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
626
893
  }
627
894
  /**
895
+ * Resolves all four L2 chain tips (proposed, checkpointed, proven, finalized) in a single
896
+ * read-only transaction so the snapshot is internally consistent. Each underlying record is
897
+ * read at most once: latest block and latest confirmed checkpoint are loaded directly (no
898
+ * separate "find the number, then look up data" hop), the proven/finalized checkpoint
899
+ * singletons are read once and their storage entries are reused if they coincide with the
900
+ * latest checkpoint, and per-tip block hashes are deduped when two tips land on the same block
901
+ * (e.g. finalized == proven).
902
+ *
903
+ * The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
904
+ * block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
905
+ * `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
906
+ *
907
+ * @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
908
+ * a tip is still at genesis).
909
+ */ async getL2TipsData(genesisBlockHash) {
910
+ return await this.db.transactionAsync(async ()=>{
911
+ // Define genesis tips
912
+ const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
913
+ const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
914
+ const genesisBlockId = {
915
+ number: genesisBlockNumber,
916
+ hash: genesisBlockHash.toString()
917
+ };
918
+ const genesisCheckpointId = {
919
+ number: genesisCheckpointNumber,
920
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
921
+ };
922
+ const genesisTip = {
923
+ block: genesisBlockId,
924
+ checkpoint: genesisCheckpointId
925
+ };
926
+ // Load latest block and checkpoint entries
927
+ const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({
928
+ reverse: true,
929
+ limit: 1
930
+ }));
931
+ const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({
932
+ reverse: true,
933
+ limit: 1
934
+ }));
935
+ const latestCheckpointNumber = latestCheckpointEntry ? CheckpointNumber(latestCheckpointEntry[0]) : genesisCheckpointNumber;
936
+ // Load proven and finalized checkpoint number pointers
937
+ const [provenRaw, finalizedRaw] = await Promise.all([
938
+ this.#lastProvenCheckpoint.getAsync(),
939
+ this.#lastFinalizedCheckpoint.getAsync()
940
+ ]);
941
+ // Clamp to enforce finalized <= proven <= checkpointed.
942
+ const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
943
+ const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
944
+ // Avoid loading the same checkpoint more than once
945
+ const checkpointStorageCache = new Map();
946
+ if (latestCheckpointEntry) {
947
+ checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
948
+ }
949
+ const loadCheckpointStorage = async (n)=>{
950
+ if (n === 0) {
951
+ return undefined;
952
+ }
953
+ if (!checkpointStorageCache.has(n)) {
954
+ const checkpointStorage = await this.#checkpoints.getAsync(n);
955
+ if (!checkpointStorage) {
956
+ throw new CheckpointNotFoundError(n);
957
+ }
958
+ checkpointStorageCache.set(n, checkpointStorage);
959
+ }
960
+ return checkpointStorageCache.get(n);
961
+ };
962
+ // Load proven and finalized checkpoint storage entries
963
+ const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
964
+ const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
965
+ // Avoid loading the same block hash multiple times when tips land on the same block
966
+ const blockHashCache = new Map();
967
+ blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
968
+ if (latestBlockEntry) {
969
+ blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
970
+ }
971
+ const loadBlockHash = async (n)=>{
972
+ if (!blockHashCache.has(n)) {
973
+ const blockStorage = await this.#blocks.getAsync(n);
974
+ if (!blockStorage) {
975
+ throw new BlockNotFoundError(n);
976
+ }
977
+ const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
978
+ blockHashCache.set(n, blockHash);
979
+ }
980
+ return blockHashCache.get(n);
981
+ };
982
+ // Build proposed chain tip (this one has block only, no checkpoint)
983
+ const proposedBlockId = latestBlockEntry === undefined ? genesisBlockId : {
984
+ number: BlockNumber(latestBlockEntry[0]),
985
+ hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString()
986
+ };
987
+ // Build other tips from checkpoint data, reading corresponding block data from the cache
988
+ const buildTipFromCheckpoint = async (stored)=>{
989
+ if (!stored) {
990
+ return genesisTip;
991
+ }
992
+ const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
993
+ const blockHash = await loadBlockHash(blockNumber);
994
+ const header = CheckpointHeader.fromBuffer(stored.header);
995
+ return {
996
+ block: {
997
+ number: blockNumber,
998
+ hash: blockHash
999
+ },
1000
+ checkpoint: {
1001
+ number: CheckpointNumber(stored.checkpointNumber),
1002
+ hash: header.hash().toString()
1003
+ }
1004
+ };
1005
+ };
1006
+ const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
1007
+ const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
1008
+ const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
1009
+ // A checkpointed block past the latest stored block would mean a checkpoint
1010
+ // references blocks that aren't in blocks.
1011
+ if (proposedBlockId.number < checkpointedTip.block.number) {
1012
+ throw new Error(`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`);
1013
+ }
1014
+ // Assert that checkpoint numbers are increasing
1015
+ if (finalizedTip.checkpoint.number > provenTip.checkpoint.number || provenTip.checkpoint.number > checkpointedTip.checkpoint.number) {
1016
+ throw new Error(`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number}`);
1017
+ }
1018
+ // Assert block numbers are increasing
1019
+ if (finalizedTip.block.number > provenTip.block.number || provenTip.block.number > checkpointedTip.block.number || checkpointedTip.block.number > proposedBlockId.number) {
1020
+ throw new Error(`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposed=${proposedBlockId.number}`);
1021
+ }
1022
+ return {
1023
+ proposed: proposedBlockId,
1024
+ checkpointed: checkpointedTip,
1025
+ proven: provenTip,
1026
+ finalized: finalizedTip
1027
+ };
1028
+ });
1029
+ }
1030
+ /**
628
1031
  * Gets the most recent L1 block processed.
629
1032
  * @returns The L1 block that published the latest L2 block
630
1033
  */ getSynchedL1BlockNumber() {
@@ -633,17 +1036,74 @@ export { TxReceipt } from '@aztec/stdlib/tx';
633
1036
  setSynchedL1BlockNumber(l1BlockNumber) {
634
1037
  return this.#lastSynchedL1Block.set(l1BlockNumber);
635
1038
  }
1039
+ /**
1040
+ * Adds a proposed checkpoint to the pending queue.
1041
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1042
+ * confirmed and the highest pending checkpoint number.
1043
+ * Computes archive and checkpointOutHash from the stored blocks.
1044
+ */ async addProposedCheckpoint(proposed) {
1045
+ return await this.db.transactionAsync(async ()=>{
1046
+ const confirmed = await this.getLatestCheckpointNumber();
1047
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
1048
+ reverse: true,
1049
+ limit: 1
1050
+ }));
1051
+ const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
1052
+ if (proposed.checkpointNumber !== latestTip + 1) {
1053
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
1054
+ }
1055
+ // Ensure the predecessor block (from pending or confirmed chain) exists
1056
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1057
+ const blocks = [];
1058
+ for(let i = 0; i < proposed.blockCount; i++){
1059
+ const block = await this.getBlock({
1060
+ number: BlockNumber(proposed.startBlock + i)
1061
+ });
1062
+ if (!block) {
1063
+ throw new BlockNotFoundError(proposed.startBlock + i);
1064
+ }
1065
+ blocks.push(block);
1066
+ }
1067
+ this.validateCheckpointBlocks(blocks, previousBlock);
1068
+ const archive = blocks[blocks.length - 1].archive;
1069
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1070
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
1071
+ header: proposed.header.toBuffer(),
1072
+ archive: archive.toBuffer(),
1073
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1074
+ checkpointNumber: proposed.checkpointNumber,
1075
+ startBlock: proposed.startBlock,
1076
+ blockCount: proposed.blockCount,
1077
+ totalManaUsed: proposed.totalManaUsed.toString(),
1078
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
1079
+ });
1080
+ });
1081
+ }
636
1082
  async getProvenCheckpointNumber() {
637
- const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
638
- this.getLatestCheckpointNumber(),
639
- this.#lastProvenCheckpoint.getAsync()
640
- ]);
641
- return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
1083
+ return await this.db.transactionAsync(async ()=>{
1084
+ const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
1085
+ this.getLatestCheckpointNumber(),
1086
+ this.#lastProvenCheckpoint.getAsync()
1087
+ ]);
1088
+ return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
1089
+ });
642
1090
  }
643
1091
  async setProvenCheckpointNumber(checkpointNumber) {
644
1092
  const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
645
1093
  return result;
646
1094
  }
1095
+ async getFinalizedCheckpointNumber() {
1096
+ return await this.db.transactionAsync(async ()=>{
1097
+ const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1098
+ this.getProvenCheckpointNumber(),
1099
+ this.#lastFinalizedCheckpoint.getAsync()
1100
+ ]);
1101
+ return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber ? provenCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1102
+ });
1103
+ }
1104
+ setFinalizedCheckpointNumber(checkpointNumber) {
1105
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1106
+ }
647
1107
  #computeBlockRange(start, limit) {
648
1108
  if (limit < 1) {
649
1109
  throw new Error(`Invalid limit: ${limit}`);
@@ -677,4 +1137,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
677
1137
  await this.#pendingChainValidationStatus.delete();
678
1138
  }
679
1139
  }
1140
+ /** Records a rejected-checkpoint entry, keyed by its own archive root. */ async addRejectedCheckpoint(entry) {
1141
+ const archiveRootHex = entry.archiveRoot.toString();
1142
+ await this.#rejectedCheckpoints.set(archiveRootHex, {
1143
+ checkpointNumber: entry.checkpointNumber,
1144
+ archiveRoot: entry.archiveRoot.toBuffer(),
1145
+ parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
1146
+ slotNumber: entry.slotNumber,
1147
+ l1: entry.l1.toBuffer(),
1148
+ reason: entry.reason
1149
+ });
1150
+ await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
1151
+ await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
1152
+ }
1153
+ /** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */ async getRejectedCheckpointByArchiveRoot(archiveRoot) {
1154
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
1155
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1156
+ }
1157
+ /** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */ async getRejectedCheckpointByNumber(checkpointNumber) {
1158
+ const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
1159
+ if (archiveRootHex === undefined) {
1160
+ return undefined;
1161
+ }
1162
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1163
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1164
+ }
1165
+ /** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */ async getLatestRejectedCheckpointNumber() {
1166
+ const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({
1167
+ reverse: true,
1168
+ limit: 1
1169
+ }));
1170
+ return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
1171
+ }
1172
+ /** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */ async removeRejectedCheckpointByArchiveRoot(archiveRoot) {
1173
+ const archiveRootHex = archiveRoot.toString();
1174
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1175
+ await this.#rejectedCheckpoints.delete(archiveRootHex);
1176
+ if (stored) {
1177
+ // Only clear the by-number index if it still points at this archive root, so a distinct
1178
+ // entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
1179
+ const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
1180
+ if (indexed === archiveRootHex) {
1181
+ await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
1182
+ }
1183
+ }
1184
+ }
1185
+ /**
1186
+ * Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
1187
+ * greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
1188
+ * sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
1189
+ * advance followed by a valid-checkpoint commit landing at an earlier L1 block).
1190
+ */ async advanceSynchedL1BlockNumber(l1BlockNumber) {
1191
+ const current = await this.#lastSynchedL1Block.getAsync();
1192
+ if (current === undefined || l1BlockNumber > current) {
1193
+ await this.#lastSynchedL1Block.set(l1BlockNumber);
1194
+ }
1195
+ }
1196
+ rejectedCheckpointFromStorage(stored) {
1197
+ return {
1198
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
1199
+ archiveRoot: Fr.fromBuffer(stored.archiveRoot),
1200
+ parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
1201
+ slotNumber: SlotNumber(stored.slotNumber),
1202
+ l1: L1PublishedData.fromBuffer(stored.l1),
1203
+ reason: stored.reason
1204
+ };
1205
+ }
680
1206
  }