@aztec/archiver 3.0.0-nightly.20251216 → 3.0.0-nightly.20251218

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 (41) hide show
  1. package/dest/archiver/archiver.d.ts +60 -36
  2. package/dest/archiver/archiver.d.ts.map +1 -1
  3. package/dest/archiver/archiver.js +366 -180
  4. package/dest/archiver/archiver_store.d.ts +79 -23
  5. package/dest/archiver/archiver_store.d.ts.map +1 -1
  6. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  8. package/dest/archiver/archiver_store_test_suite.js +1624 -251
  9. package/dest/archiver/errors.d.ts +25 -1
  10. package/dest/archiver/errors.d.ts.map +1 -1
  11. package/dest/archiver/errors.js +37 -0
  12. package/dest/archiver/index.d.ts +2 -2
  13. package/dest/archiver/index.d.ts.map +1 -1
  14. package/dest/archiver/kv_archiver_store/block_store.d.ts +49 -17
  15. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  16. package/dest/archiver/kv_archiver_store/block_store.js +320 -83
  17. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +29 -27
  18. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  19. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +50 -26
  20. package/dest/archiver/kv_archiver_store/log_store.d.ts +4 -4
  21. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  22. package/dest/archiver/l1/data_retrieval.d.ts +11 -8
  23. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -1
  24. package/dest/archiver/l1/data_retrieval.js +25 -17
  25. package/dest/archiver/structs/published.d.ts +1 -2
  26. package/dest/archiver/structs/published.d.ts.map +1 -1
  27. package/dest/test/mock_l2_block_source.d.ts +3 -2
  28. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  29. package/dest/test/mock_l2_block_source.js +8 -15
  30. package/package.json +13 -13
  31. package/src/archiver/archiver.ts +464 -222
  32. package/src/archiver/archiver_store.ts +88 -22
  33. package/src/archiver/archiver_store_test_suite.ts +1626 -232
  34. package/src/archiver/errors.ts +64 -0
  35. package/src/archiver/index.ts +1 -1
  36. package/src/archiver/kv_archiver_store/block_store.ts +435 -94
  37. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +62 -38
  38. package/src/archiver/kv_archiver_store/log_store.ts +4 -4
  39. package/src/archiver/l1/data_retrieval.ts +27 -13
  40. package/src/archiver/structs/published.ts +0 -1
  41. package/src/test/mock_l2_block_source.ts +9 -16
@@ -1,25 +1,28 @@
1
- import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
1
+ import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } 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
- import { Body, CommitteeAttestation, L2Block, L2BlockHash, PublishedL2Block } from '@aztec/stdlib/block';
9
- import { L2BlockHeader, deserializeValidateBlockResult, serializeValidateBlockResult } from '@aztec/stdlib/block';
8
+ import { isDefined } from '@aztec/foundation/types';
9
+ import { Body, CheckpointedL2Block, CommitteeAttestation, L2BlockHash, L2BlockNew, deserializeValidateBlockResult, serializeValidateBlockResult } from '@aztec/stdlib/block';
10
+ import { L1PublishedData } from '@aztec/stdlib/checkpoint';
11
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
10
12
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
11
- import { TxHash, TxReceipt, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
12
- import { BlockNumberNotSequentialError, InitialBlockNumberNotSequentialError } from '../errors.js';
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';
13
15
  export { TxReceipt } from '@aztec/stdlib/tx';
14
16
  /**
15
17
  * LMDB implementation of the ArchiverDataStore interface.
16
18
  */ export class BlockStore {
17
19
  db;
18
20
  /** Map block number to block data */ #blocks;
21
+ /** Map checkpoint number to checkpoint data */ #checkpoints;
19
22
  /** Map block hash to list of tx hashes */ #blockTxs;
20
23
  /** Tx hash to serialized IndexedTxEffect */ #txEffects;
21
24
  /** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block;
22
- /** Stores l2 block number of the last proven block */ #lastProvenL2Block;
25
+ /** Stores last proven checkpoint */ #lastProvenCheckpoint;
23
26
  /** Stores the pending chain validation status */ #pendingChainValidationStatus;
24
27
  /** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
25
28
  /** Index mapping block hash to block number */ #blockHashIndex;
@@ -35,11 +38,12 @@ export { TxReceipt } from '@aztec/stdlib/tx';
35
38
  this.#blockHashIndex = db.openMap('archiver_block_hash_index');
36
39
  this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
37
40
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
38
- this.#lastProvenL2Block = db.openSingleton('archiver_last_proven_l2_block');
41
+ this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
39
42
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
43
+ this.#checkpoints = db.openMap('archiver_checkpoints');
40
44
  }
41
45
  /**
42
- * Append new blocks to the store's list.
46
+ * Append new blocks to the store's list. All blocks must be for the 'current' checkpoint
43
47
  * @param blocks - The L2 blocks to be added to the store.
44
48
  * @returns True if the operation is successful.
45
49
  */ async addBlocks(blocks, opts = {}) {
@@ -48,84 +52,313 @@ export { TxReceipt } from '@aztec/stdlib/tx';
48
52
  }
49
53
  return await this.db.transactionAsync(async ()=>{
50
54
  // Check that the block immediately before the first block to be added is present in the store.
51
- const firstBlockNumber = blocks[0].block.number;
52
- const [previousBlockNumber] = await toArray(this.#blocks.keysAsync({
53
- reverse: true,
54
- limit: 1,
55
- end: firstBlockNumber - 1
56
- }));
57
- const hasPreviousBlock = firstBlockNumber === INITIAL_L2_BLOCK_NUM || previousBlockNumber !== undefined && previousBlockNumber === firstBlockNumber - 1;
58
- if (!opts.force && !hasPreviousBlock) {
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;
59
+ // 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) {
59
64
  throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
60
65
  }
61
- // Iterate over blocks array and insert them, checking that the block numbers are sequential.
66
+ // The same check as above but for checkpoints
67
+ if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
68
+ throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
69
+ }
70
+ // 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;
73
+ let previousBlockIndex = undefined;
74
+ if (previousBlockResult !== undefined) {
75
+ if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
76
+ // The previous block is for the same checkpoint, therefore our index should follow it
77
+ previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
78
+ expectedFirstblockIndex = previousBlockIndex + 1;
79
+ }
80
+ if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
81
+ throw new BlockArchiveNotConsistentError(firstBlockNumber, previousBlockResult.number, firstBlockLastArchive, previousBlockResult.archive.root);
82
+ }
83
+ }
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.
62
89
  let previousBlock = undefined;
63
90
  for (const block of blocks){
64
- if (!opts.force && previousBlock && previousBlock.block.number + 1 !== block.block.number) {
65
- throw new BlockNumberNotSequentialError(block.block.number, previousBlock.block.number);
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);
66
104
  }
67
105
  previousBlock = block;
68
- const blockHash = L2BlockHash.fromField(await block.block.hash());
69
- await this.#blocks.set(block.block.number, {
70
- header: block.block.header.toBuffer(),
71
- blockHash: blockHash.toBuffer(),
72
- archive: block.block.archive.toBuffer(),
73
- l1: block.l1,
74
- attestations: block.attestations.map((attestation)=>attestation.toBuffer())
75
- });
76
- for(let i = 0; i < block.block.body.txEffects.length; i++){
77
- const txEffect = {
78
- data: block.block.body.txEffects[i],
79
- l2BlockNumber: block.block.number,
80
- l2BlockHash: blockHash,
81
- txIndexInBlock: i
82
- };
83
- await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
106
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
107
+ }
108
+ return true;
109
+ });
110
+ }
111
+ /**
112
+ * Append new cheskpoints to the store's list.
113
+ * @param checkpoints - The L2 checkpoints to be added to the store.
114
+ * @returns True if the operation is successful.
115
+ */ async addCheckpoints(checkpoints, opts = {}) {
116
+ if (checkpoints.length === 0) {
117
+ return true;
118
+ }
119
+ 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
+ const firstCheckpointNumber = checkpoints[0].checkpoint.number;
122
+ 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);
84
133
  }
85
- await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.block.body.txEffects.map((tx)=>tx.txHash.toBuffer())));
86
- // Update indices for block hash and archive
87
- await this.#blockHashIndex.set(blockHash.toString(), block.block.number);
88
- await this.#blockArchiveIndex.set(block.block.archive.root.toString(), block.block.number);
89
134
  }
90
- await this.#lastSynchedL1Block.set(blocks[blocks.length - 1].l1.blockNumber);
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);
144
+ }
145
+ }
146
+ // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
147
+ let previousCheckpoint = undefined;
148
+ for (const checkpoint of checkpoints){
149
+ if (!opts.force && previousCheckpoint && previousCheckpoint.checkpoint.number + 1 !== checkpoint.checkpoint.number) {
150
+ throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
151
+ }
152
+ 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.
154
+ 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
+ }
180
+ // Store the checkpoint in the database
181
+ await this.#checkpoints.set(checkpoint.checkpoint.number, {
182
+ header: checkpoint.checkpoint.header.toBuffer(),
183
+ archive: checkpoint.checkpoint.archive.toBuffer(),
184
+ l1: checkpoint.l1.toBuffer(),
185
+ attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
186
+ checkpointNumber: checkpoint.checkpoint.number,
187
+ startBlock: checkpoint.checkpoint.blocks[0].number,
188
+ numBlocks: checkpoint.checkpoint.blocks.length
189
+ });
190
+ }
191
+ await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
91
192
  return true;
92
193
  });
93
194
  }
195
+ async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
196
+ const blockHash = L2BlockHash.fromField(await block.hash());
197
+ await this.#blocks.set(block.number, {
198
+ header: block.header.toBuffer(),
199
+ blockHash: blockHash.toBuffer(),
200
+ archive: block.archive.toBuffer(),
201
+ checkpointNumber,
202
+ indexWithinCheckpoint
203
+ });
204
+ for(let i = 0; i < block.body.txEffects.length; i++){
205
+ const txEffect = {
206
+ data: block.body.txEffects[i],
207
+ l2BlockNumber: block.number,
208
+ l2BlockHash: blockHash,
209
+ txIndexInBlock: i
210
+ };
211
+ await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
212
+ }
213
+ await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.body.txEffects.map((tx)=>tx.txHash.toBuffer())));
214
+ // Update indices for block hash and archive
215
+ await this.#blockHashIndex.set(blockHash.toString(), block.number);
216
+ await this.#blockArchiveIndex.set(block.archive.root.toString(), block.number);
217
+ }
94
218
  /**
95
- * Unwinds blocks from the database
219
+ * Unwinds checkpoints from the database
96
220
  * @param from - The tip of the chain, passed for verification purposes,
97
221
  * ensuring that we don't end up deleting something we did not intend
98
- * @param blocksToUnwind - The number of blocks we are to unwind
222
+ * @param checkpointsToUnwind - The number of checkpoints we are to unwind
99
223
  * @returns True if the operation is successful
100
- */ async unwindBlocks(from, blocksToUnwind) {
224
+ */ async unwindCheckpoints(from, checkpointsToUnwind) {
101
225
  return await this.db.transactionAsync(async ()=>{
102
- const last = await this.getSynchedL2BlockNumber();
226
+ const last = await this.getLatestCheckpointNumber();
103
227
  if (from !== last) {
104
- throw new Error(`Can only unwind blocks from the tip (requested ${from} but current tip is ${last})`);
228
+ throw new Error(`Can only unwind checkpoints from the tip (requested ${from} but current tip is ${last})`);
105
229
  }
106
- const proven = await this.getProvenL2BlockNumber();
107
- if (from - blocksToUnwind < proven) {
108
- await this.setProvenL2BlockNumber(BlockNumber(from - blocksToUnwind));
230
+ const proven = await this.getProvenCheckpointNumber();
231
+ if (from - checkpointsToUnwind < proven) {
232
+ await this.setProvenCheckpointNumber(CheckpointNumber(from - checkpointsToUnwind));
109
233
  }
110
- for(let i = 0; i < blocksToUnwind; i++){
111
- const blockNumber = from - i;
112
- const block = await this.getBlock(BlockNumber(blockNumber));
113
- if (block === undefined) {
114
- this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
234
+ for(let i = 0; i < checkpointsToUnwind; i++){
235
+ const checkpointNumber = from - i;
236
+ const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
237
+ if (checkpoint === undefined) {
238
+ this.#log.warn(`Cannot remove checkpoint ${checkpointNumber} from the store since we don't have it`);
115
239
  continue;
116
240
  }
117
- await this.#blocks.delete(block.block.number);
118
- await Promise.all(block.block.body.txEffects.map((tx)=>this.#txEffects.delete(tx.txHash.toString())));
119
- const blockHash = (await block.block.hash()).toString();
120
- await this.#blockTxs.delete(blockHash);
121
- // Clean up indices
122
- await this.#blockHashIndex.delete(blockHash);
123
- await this.#blockArchiveIndex.delete(block.block.archive.root.toString());
124
- this.#log.debug(`Unwound block ${blockNumber} ${blockHash}`);
241
+ await this.#checkpoints.delete(checkpointNumber);
242
+ const maxBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
243
+ for(let blockNumber = checkpoint.startBlock; blockNumber <= maxBlock; blockNumber++){
244
+ const block = await this.getBlock(BlockNumber(blockNumber));
245
+ if (block === undefined) {
246
+ this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
247
+ continue;
248
+ }
249
+ await this.#blocks.delete(block.number);
250
+ await Promise.all(block.body.txEffects.map((tx)=>this.#txEffects.delete(tx.txHash.toString())));
251
+ const blockHash = (await block.hash()).toString();
252
+ await this.#blockTxs.delete(blockHash);
253
+ // Clean up indices
254
+ await this.#blockHashIndex.delete(blockHash);
255
+ await this.#blockArchiveIndex.delete(block.archive.root.toString());
256
+ this.#log.debug(`Unwound block ${blockNumber} ${blockHash} for checkpoint ${checkpointNumber}`);
257
+ }
125
258
  }
126
259
  return true;
127
260
  });
128
261
  }
262
+ async getCheckpointData(checkpointNumber) {
263
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
264
+ if (!checkpointStorage) {
265
+ return undefined;
266
+ }
267
+ return this.checkpointDataFromCheckpointStorage(checkpointStorage);
268
+ }
269
+ async getRangeOfCheckpoints(from, limit) {
270
+ const checkpoints = [];
271
+ for(let checkpointNumber = from; checkpointNumber < from + limit; checkpointNumber++){
272
+ const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
273
+ if (!checkpoint) {
274
+ break;
275
+ }
276
+ checkpoints.push(this.checkpointDataFromCheckpointStorage(checkpoint));
277
+ }
278
+ return checkpoints;
279
+ }
280
+ checkpointDataFromCheckpointStorage(checkpointStorage) {
281
+ const data = {
282
+ header: CheckpointHeader.fromBuffer(checkpointStorage.header),
283
+ archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
284
+ checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
285
+ startBlock: checkpointStorage.startBlock,
286
+ numBlocks: checkpointStorage.numBlocks,
287
+ l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
288
+ attestations: checkpointStorage.attestations
289
+ };
290
+ return data;
291
+ }
292
+ async getBlocksForCheckpoint(checkpointNumber) {
293
+ const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
294
+ if (!checkpoint) {
295
+ return undefined;
296
+ }
297
+ const blocksForCheckpoint = await toArray(this.#blocks.entriesAsync({
298
+ start: checkpoint.startBlock,
299
+ end: checkpoint.startBlock + checkpoint.numBlocks
300
+ }));
301
+ const converted = await Promise.all(blocksForCheckpoint.map((x)=>this.getBlockFromBlockStorage(x[0], x[1])));
302
+ return converted.filter(isDefined);
303
+ }
304
+ async getProvenBlockNumber() {
305
+ const provenCheckpointNumber = await this.getProvenCheckpointNumber();
306
+ if (provenCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
307
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
308
+ }
309
+ const checkpointStorage = await this.#checkpoints.getAsync(provenCheckpointNumber);
310
+ if (!checkpointStorage) {
311
+ throw new CheckpointNotFoundError(provenCheckpointNumber);
312
+ } else {
313
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
314
+ }
315
+ }
316
+ async getLatestBlockNumber() {
317
+ const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({
318
+ reverse: true,
319
+ limit: 1
320
+ }));
321
+ return typeof latestBlocknumber === 'number' ? BlockNumber(latestBlocknumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
322
+ }
323
+ async getLatestCheckpointNumber() {
324
+ const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
325
+ reverse: true,
326
+ limit: 1
327
+ }));
328
+ if (latestCheckpointNumber === undefined) {
329
+ return CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
330
+ }
331
+ return CheckpointNumber(latestCheckpointNumber);
332
+ }
333
+ async getCheckpointedBlock(number) {
334
+ const blockStorage = await this.#blocks.getAsync(number);
335
+ if (!blockStorage) {
336
+ return undefined;
337
+ }
338
+ const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
339
+ if (!checkpoint) {
340
+ return undefined;
341
+ }
342
+ const block = await this.getBlockFromBlockStorage(number, blockStorage);
343
+ if (!block) {
344
+ return undefined;
345
+ }
346
+ return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
347
+ }
348
+ async getCheckpointedBlockByHash(blockHash) {
349
+ const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
350
+ if (blockNumber === undefined) {
351
+ return undefined;
352
+ }
353
+ return this.getCheckpointedBlock(BlockNumber(blockNumber));
354
+ }
355
+ async getCheckpointedBlockByArchive(archive) {
356
+ const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
357
+ if (blockNumber === undefined) {
358
+ return undefined;
359
+ }
360
+ return this.getCheckpointedBlock(BlockNumber(blockNumber));
361
+ }
129
362
  /**
130
363
  * Gets up to `limit` amount of L2 blocks starting from `from`.
131
364
  * @param start - Number of the first block to return (inclusive).
@@ -185,7 +418,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
185
418
  if (!blockStorage || !blockStorage.header) {
186
419
  return undefined;
187
420
  }
188
- return L2BlockHeader.fromBuffer(blockStorage.header).toBlockHeader();
421
+ return BlockHeader.fromBuffer(blockStorage.header);
189
422
  }
190
423
  /**
191
424
  * Gets a block header by its archive root.
@@ -200,7 +433,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
200
433
  if (!blockStorage || !blockStorage.header) {
201
434
  return undefined;
202
435
  }
203
- return L2BlockHeader.fromBuffer(blockStorage.header).toBlockHeader();
436
+ return BlockHeader.fromBuffer(blockStorage.header);
204
437
  }
205
438
  /**
206
439
  * Gets the headers for a sequence of L2 blocks.
@@ -209,7 +442,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
209
442
  * @returns The requested L2 block headers
210
443
  */ async *getBlockHeaders(start, limit) {
211
444
  for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
212
- const header = L2BlockHeader.fromBuffer(blockStorage.header).toBlockHeader();
445
+ const header = BlockHeader.fromBuffer(blockStorage.header);
213
446
  if (header.getBlockNumber() !== blockNumber) {
214
447
  throw new Error(`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`);
215
448
  }
@@ -230,7 +463,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
230
463
  }
231
464
  }
232
465
  async getBlockFromBlockStorage(blockNumber, blockStorage) {
233
- const header = L2BlockHeader.fromBuffer(blockStorage.header);
466
+ const header = BlockHeader.fromBuffer(blockStorage.header);
234
467
  const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
235
468
  const blockHash = blockStorage.blockHash;
236
469
  const blockHashString = bufferToHex(blockHash);
@@ -251,16 +484,11 @@ export { TxReceipt } from '@aztec/stdlib/tx';
251
484
  txEffects.push(deserializeIndexedTxEffect(txEffect).data);
252
485
  }
253
486
  const body = new Body(txEffects);
254
- const block = new L2Block(archive, header, body, Fr.fromBuffer(blockHash));
487
+ const block = new L2BlockNew(archive, header, body, CheckpointNumber(blockStorage.checkpointNumber), blockStorage.indexWithinCheckpoint, Fr.fromBuffer(blockHash));
255
488
  if (block.number !== blockNumber) {
256
489
  throw new Error(`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${block.number} with hash ${blockHashString})`);
257
490
  }
258
- const attestations = blockStorage.attestations.map(CommitteeAttestation.fromBuffer);
259
- return PublishedL2Block.fromFields({
260
- block,
261
- l1: blockStorage.l1,
262
- attestations
263
- });
491
+ return block;
264
492
  }
265
493
  /**
266
494
  * Gets a tx effect.
@@ -307,9 +535,17 @@ export { TxReceipt } from '@aztec/stdlib/tx';
307
535
  return this.#contractIndex.getAsync(contractAddress.toString());
308
536
  }
309
537
  /**
310
- * Gets the number of the latest L2 block processed.
311
- * @returns The number of the latest L2 block processed.
312
- */ async getSynchedL2BlockNumber() {
538
+ * Gets the number of the latest L2 block checkpointed.
539
+ * @returns The number of the latest L2 block checkpointed.
540
+ */ async getCheckpointedL2BlockNumber() {
541
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
542
+ const checkpoint = await this.getCheckpointData(latestCheckpointNumber);
543
+ if (!checkpoint) {
544
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
545
+ }
546
+ return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
547
+ }
548
+ async getLatestL2BlockNumber() {
313
549
  const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({
314
550
  reverse: true,
315
551
  limit: 1
@@ -325,15 +561,16 @@ export { TxReceipt } from '@aztec/stdlib/tx';
325
561
  setSynchedL1BlockNumber(l1BlockNumber) {
326
562
  return this.#lastSynchedL1Block.set(l1BlockNumber);
327
563
  }
328
- async getProvenL2BlockNumber() {
329
- const [latestBlockNumber, provenBlockNumber] = await Promise.all([
330
- this.getSynchedL2BlockNumber(),
331
- this.#lastProvenL2Block.getAsync()
564
+ async getProvenCheckpointNumber() {
565
+ const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
566
+ this.getLatestCheckpointNumber(),
567
+ this.#lastProvenCheckpoint.getAsync()
332
568
  ]);
333
- return (provenBlockNumber ?? 0) > latestBlockNumber ? latestBlockNumber : BlockNumber(provenBlockNumber ?? 0);
569
+ return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
334
570
  }
335
- setProvenL2BlockNumber(blockNumber) {
336
- return this.#lastProvenL2Block.set(blockNumber);
571
+ async setProvenCheckpointNumber(checkpointNumber) {
572
+ const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
573
+ return result;
337
574
  }
338
575
  #computeBlockRange(start, limit) {
339
576
  if (limit < 1) {