@aztec/archiver 0.0.1-commit.8f9871590 → 0.0.1-commit.934299a21

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 (68) hide show
  1. package/dest/archiver.d.ts +5 -2
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +9 -91
  4. package/dest/factory.d.ts +1 -1
  5. package/dest/factory.d.ts.map +1 -1
  6. package/dest/factory.js +5 -5
  7. package/dest/index.d.ts +2 -1
  8. package/dest/index.d.ts.map +1 -1
  9. package/dest/index.js +1 -0
  10. package/dest/l1/bin/retrieve-calldata.js +32 -28
  11. package/dest/l1/calldata_retriever.d.ts +73 -52
  12. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  13. package/dest/l1/calldata_retriever.js +190 -261
  14. package/dest/l1/data_retrieval.d.ts +2 -6
  15. package/dest/l1/data_retrieval.d.ts.map +1 -1
  16. package/dest/l1/data_retrieval.js +6 -11
  17. package/dest/l1/spire_proposer.d.ts +5 -5
  18. package/dest/l1/spire_proposer.d.ts.map +1 -1
  19. package/dest/l1/spire_proposer.js +9 -17
  20. package/dest/modules/data_source_base.d.ts +8 -3
  21. package/dest/modules/data_source_base.d.ts.map +1 -1
  22. package/dest/modules/data_source_base.js +28 -72
  23. package/dest/modules/data_store_updater.d.ts +9 -2
  24. package/dest/modules/data_store_updater.d.ts.map +1 -1
  25. package/dest/modules/data_store_updater.js +40 -19
  26. package/dest/modules/instrumentation.d.ts +15 -2
  27. package/dest/modules/instrumentation.d.ts.map +1 -1
  28. package/dest/modules/instrumentation.js +19 -2
  29. package/dest/modules/l1_synchronizer.d.ts +4 -8
  30. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  31. package/dest/modules/l1_synchronizer.js +14 -9
  32. package/dest/store/block_store.d.ts +18 -14
  33. package/dest/store/block_store.d.ts.map +1 -1
  34. package/dest/store/block_store.js +69 -17
  35. package/dest/store/kv_archiver_store.d.ts +18 -4
  36. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  37. package/dest/store/kv_archiver_store.js +18 -0
  38. package/dest/store/l2_tips_cache.d.ts +19 -0
  39. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  40. package/dest/store/l2_tips_cache.js +89 -0
  41. package/dest/test/fake_l1_state.d.ts +6 -1
  42. package/dest/test/fake_l1_state.d.ts.map +1 -1
  43. package/dest/test/fake_l1_state.js +56 -18
  44. package/dest/test/mock_archiver.d.ts +1 -1
  45. package/dest/test/mock_archiver.d.ts.map +1 -1
  46. package/dest/test/mock_archiver.js +3 -2
  47. package/dest/test/mock_l2_block_source.d.ts +18 -3
  48. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  49. package/dest/test/mock_l2_block_source.js +125 -82
  50. package/package.json +13 -13
  51. package/src/archiver.ts +10 -110
  52. package/src/factory.ts +7 -1
  53. package/src/index.ts +1 -0
  54. package/src/l1/README.md +25 -68
  55. package/src/l1/bin/retrieve-calldata.ts +40 -27
  56. package/src/l1/calldata_retriever.ts +249 -383
  57. package/src/l1/data_retrieval.ts +3 -16
  58. package/src/l1/spire_proposer.ts +7 -15
  59. package/src/modules/data_source_base.ts +53 -92
  60. package/src/modules/data_store_updater.ts +43 -18
  61. package/src/modules/instrumentation.ts +29 -2
  62. package/src/modules/l1_synchronizer.ts +15 -12
  63. package/src/store/block_store.ts +85 -36
  64. package/src/store/kv_archiver_store.ts +35 -3
  65. package/src/store/l2_tips_cache.ts +89 -0
  66. package/src/test/fake_l1_state.ts +75 -17
  67. package/src/test/mock_archiver.ts +3 -2
  68. package/src/test/mock_l2_block_source.ts +158 -78
@@ -8,6 +8,7 @@ import { createLogger } from '@aztec/foundation/log';
8
8
  import type { FunctionSelector } from '@aztec/stdlib/abi';
9
9
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
10
10
  import {
11
+ type BlockData,
11
12
  BlockHash,
12
13
  CheckpointedL2Block,
13
14
  L2Block,
@@ -15,9 +16,11 @@ import {
15
16
  type L2Tips,
16
17
  type ValidateCheckpointResult,
17
18
  } from '@aztec/stdlib/block';
18
- import { Checkpoint, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
19
+ import { Checkpoint, type CheckpointData, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
19
20
  import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
20
21
  import { EmptyL1RollupConstants, type L1RollupConstants, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
22
+ import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
23
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
21
24
  import { type BlockHeader, TxExecutionResult, TxHash, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
22
25
  import type { UInt64 } from '@aztec/stdlib/types';
23
26
 
@@ -26,6 +29,7 @@ import type { UInt64 } from '@aztec/stdlib/types';
26
29
  */
27
30
  export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
28
31
  protected l2Blocks: L2Block[] = [];
32
+ protected checkpointList: Checkpoint[] = [];
29
33
 
30
34
  private provenBlockNumber: number = 0;
31
35
  private finalizedBlockNumber: number = 0;
@@ -33,14 +37,30 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
33
37
 
34
38
  private log = createLogger('archiver:mock_l2_block_source');
35
39
 
40
+ /** Creates blocks grouped into single-block checkpoints. */
36
41
  public async createBlocks(numBlocks: number) {
37
- for (let i = 0; i < numBlocks; i++) {
38
- const blockNum = this.l2Blocks.length + 1;
39
- const block = await L2Block.random(BlockNumber(blockNum), { slotNumber: SlotNumber(blockNum) });
40
- this.l2Blocks.push(block);
42
+ await this.createCheckpoints(numBlocks, 1);
43
+ }
44
+
45
+ /** Creates checkpoints, each containing `blocksPerCheckpoint` blocks. */
46
+ public async createCheckpoints(numCheckpoints: number, blocksPerCheckpoint: number = 1) {
47
+ for (let c = 0; c < numCheckpoints; c++) {
48
+ const checkpointNum = CheckpointNumber(this.checkpointList.length + 1);
49
+ const startBlockNum = this.l2Blocks.length + 1;
50
+ const slotNumber = SlotNumber(Number(checkpointNum));
51
+ const checkpoint = await Checkpoint.random(checkpointNum, {
52
+ numBlocks: blocksPerCheckpoint,
53
+ startBlockNumber: startBlockNum,
54
+ slotNumber,
55
+ checkpointNumber: checkpointNum,
56
+ });
57
+ this.checkpointList.push(checkpoint);
58
+ this.l2Blocks.push(...checkpoint.blocks);
41
59
  }
42
60
 
43
- this.log.verbose(`Created ${numBlocks} blocks in the mock L2 block source`);
61
+ this.log.verbose(
62
+ `Created ${numCheckpoints} checkpoints with ${blocksPerCheckpoint} blocks each in the mock L2 block source`,
63
+ );
44
64
  }
45
65
 
46
66
  public addProposedBlocks(blocks: L2Block[]) {
@@ -50,6 +70,16 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
50
70
 
51
71
  public removeBlocks(numBlocks: number) {
52
72
  this.l2Blocks = this.l2Blocks.slice(0, -numBlocks);
73
+ const maxBlockNum = this.l2Blocks.length;
74
+ // Remove any checkpoint whose last block is beyond the remaining blocks.
75
+ this.checkpointList = this.checkpointList.filter(c => {
76
+ const lastBlockNum = c.blocks[0].number + c.blocks.length - 1;
77
+ return lastBlockNum <= maxBlockNum;
78
+ });
79
+ // Keep tip numbers consistent with remaining blocks.
80
+ this.checkpointedBlockNumber = Math.min(this.checkpointedBlockNumber, maxBlockNum);
81
+ this.provenBlockNumber = Math.min(this.provenBlockNumber, maxBlockNum);
82
+ this.finalizedBlockNumber = Math.min(this.finalizedBlockNumber, maxBlockNum);
53
83
  this.log.verbose(`Removed ${numBlocks} blocks from the mock L2 block source`);
54
84
  }
55
85
 
@@ -65,7 +95,33 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
65
95
  }
66
96
 
67
97
  public setCheckpointedBlockNumber(checkpointedBlockNumber: number) {
98
+ const prevCheckpointed = this.checkpointedBlockNumber;
68
99
  this.checkpointedBlockNumber = checkpointedBlockNumber;
100
+ // Auto-create single-block checkpoints for newly checkpointed blocks that don't have one yet.
101
+ // This handles blocks added via addProposedBlocks that are now being marked as checkpointed.
102
+ const newCheckpoints: Checkpoint[] = [];
103
+ for (let blockNum = prevCheckpointed + 1; blockNum <= checkpointedBlockNumber; blockNum++) {
104
+ const block = this.l2Blocks[blockNum - 1];
105
+ if (!block) {
106
+ continue;
107
+ }
108
+ if (this.checkpointList.some(c => c.blocks.some(b => b.number === block.number))) {
109
+ continue;
110
+ }
111
+ const checkpointNum = CheckpointNumber(this.checkpointList.length + newCheckpoints.length + 1);
112
+ const checkpoint = new Checkpoint(
113
+ block.archive,
114
+ CheckpointHeader.random({ slotNumber: block.header.globalVariables.slotNumber }),
115
+ [block],
116
+ checkpointNum,
117
+ );
118
+ newCheckpoints.push(checkpoint);
119
+ }
120
+ // Insert new checkpoints in order by number.
121
+ if (newCheckpoints.length > 0) {
122
+ this.checkpointList.push(...newCheckpoints);
123
+ this.checkpointList.sort((a, b) => a.number - b.number);
124
+ }
69
125
  }
70
126
 
71
127
  /**
@@ -112,13 +168,7 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
112
168
  if (!block) {
113
169
  return Promise.resolve(undefined);
114
170
  }
115
- const checkpointedBlock = new CheckpointedL2Block(
116
- CheckpointNumber.fromBlockNumber(number),
117
- block,
118
- new L1PublishedData(BigInt(number), BigInt(number), `0x${number.toString(16).padStart(64, '0')}`),
119
- [],
120
- );
121
- return Promise.resolve(checkpointedBlock);
171
+ return Promise.resolve(this.toCheckpointedBlock(block));
122
172
  }
123
173
 
124
174
  public async getCheckpointedBlocks(from: BlockNumber, limit: number): Promise<CheckpointedL2Block[]> {
@@ -167,44 +217,22 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
167
217
  }
168
218
 
169
219
  public getCheckpoints(from: CheckpointNumber, limit: number) {
170
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
171
- const blocks = this.l2Blocks.slice(from - 1, from - 1 + limit);
172
- return Promise.all(
173
- blocks.map(async block => {
174
- // Create a checkpoint from the block - manually construct since L2Block doesn't have toCheckpoint()
175
- const checkpoint = await Checkpoint.random(block.checkpointNumber, { numBlocks: 1 });
176
- checkpoint.blocks = [block];
177
- return new PublishedCheckpoint(
178
- checkpoint,
179
- new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
180
- [],
181
- );
182
- }),
220
+ const checkpoints = this.checkpointList.slice(from - 1, from - 1 + limit);
221
+ return Promise.resolve(
222
+ checkpoints.map(checkpoint => new PublishedCheckpoint(checkpoint, this.mockL1DataForCheckpoint(checkpoint), [])),
183
223
  );
184
224
  }
185
225
 
186
- public async getCheckpointByArchive(archive: Fr): Promise<Checkpoint | undefined> {
187
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
188
- const block = this.l2Blocks.find(b => b.archive.root.equals(archive));
189
- if (!block) {
190
- return undefined;
191
- }
192
- // Create a checkpoint from the block - manually construct since L2Block doesn't have toCheckpoint()
193
- const checkpoint = await Checkpoint.random(block.checkpointNumber, { numBlocks: 1 });
194
- checkpoint.blocks = [block];
195
- return checkpoint;
226
+ public getCheckpointByArchive(archive: Fr): Promise<Checkpoint | undefined> {
227
+ const checkpoint = this.checkpointList.find(c => c.archive.root.equals(archive));
228
+ return Promise.resolve(checkpoint);
196
229
  }
197
230
 
198
231
  public async getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
199
232
  for (const block of this.l2Blocks) {
200
233
  const hash = await block.hash();
201
234
  if (hash.equals(blockHash)) {
202
- return CheckpointedL2Block.fromFields({
203
- checkpointNumber: CheckpointNumber.fromBlockNumber(block.number),
204
- block,
205
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
206
- attestations: [],
207
- });
235
+ return this.toCheckpointedBlock(block);
208
236
  }
209
237
  }
210
238
  return undefined;
@@ -215,14 +243,7 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
215
243
  if (!block) {
216
244
  return Promise.resolve(undefined);
217
245
  }
218
- return Promise.resolve(
219
- CheckpointedL2Block.fromFields({
220
- checkpointNumber: CheckpointNumber.fromBlockNumber(block.number),
221
- block,
222
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
223
- attestations: [],
224
- }),
225
- );
246
+ return Promise.resolve(this.toCheckpointedBlock(block));
226
247
  }
227
248
 
228
249
  public async getL2BlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
@@ -255,47 +276,69 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
255
276
  return Promise.resolve(block?.header);
256
277
  }
257
278
 
279
+ public async getBlockData(number: BlockNumber): Promise<BlockData | undefined> {
280
+ const block = this.l2Blocks[number - 1];
281
+ if (!block) {
282
+ return undefined;
283
+ }
284
+ return {
285
+ header: block.header,
286
+ archive: block.archive,
287
+ blockHash: await block.hash(),
288
+ checkpointNumber: block.checkpointNumber,
289
+ indexWithinCheckpoint: block.indexWithinCheckpoint,
290
+ };
291
+ }
292
+
293
+ public async getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
294
+ const block = this.l2Blocks.find(b => b.archive.root.equals(archive));
295
+ if (!block) {
296
+ return undefined;
297
+ }
298
+ return {
299
+ header: block.header,
300
+ archive: block.archive,
301
+ blockHash: await block.hash(),
302
+ checkpointNumber: block.checkpointNumber,
303
+ indexWithinCheckpoint: block.indexWithinCheckpoint,
304
+ };
305
+ }
306
+
258
307
  getBlockHeader(number: number | 'latest'): Promise<BlockHeader | undefined> {
259
308
  return Promise.resolve(this.l2Blocks.at(typeof number === 'number' ? number - 1 : -1)?.header);
260
309
  }
261
310
 
262
311
  getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
263
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
264
- const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
265
- const [start, end] = getSlotRangeForEpoch(epochNumber, { epochDuration });
266
- const blocks = this.l2Blocks.filter(b => {
267
- const slot = b.header.globalVariables.slotNumber;
268
- return slot >= start && slot <= end;
269
- });
270
- // Create checkpoints from blocks - manually construct since L2Block doesn't have toCheckpoint()
271
- return Promise.all(
272
- blocks.map(async block => {
273
- const checkpoint = await Checkpoint.random(block.checkpointNumber, { numBlocks: 1 });
274
- checkpoint.blocks = [block];
275
- return checkpoint;
276
- }),
277
- );
312
+ return Promise.resolve(this.getCheckpointsInEpoch(epochNumber));
278
313
  }
279
314
 
280
- getCheckpointedBlocksForEpoch(epochNumber: EpochNumber): Promise<CheckpointedL2Block[]> {
281
- const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
282
- const [start, end] = getSlotRangeForEpoch(epochNumber, { epochDuration });
283
- const blocks = this.l2Blocks.filter(b => {
284
- const slot = b.header.globalVariables.slotNumber;
285
- return slot >= start && slot <= end;
286
- });
315
+ getCheckpointsDataForEpoch(epochNumber: EpochNumber): Promise<CheckpointData[]> {
316
+ const checkpoints = this.getCheckpointsInEpoch(epochNumber);
287
317
  return Promise.resolve(
288
- blocks.map(block =>
289
- CheckpointedL2Block.fromFields({
290
- checkpointNumber: CheckpointNumber.fromBlockNumber(block.number),
291
- block,
292
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
318
+ checkpoints.map(
319
+ (checkpoint): CheckpointData => ({
320
+ checkpointNumber: checkpoint.number,
321
+ header: checkpoint.header,
322
+ archive: checkpoint.archive,
323
+ checkpointOutHash: computeCheckpointOutHash(
324
+ checkpoint.blocks.map(b => b.body.txEffects.map(tx => tx.l2ToL1Msgs)),
325
+ ),
326
+ startBlock: checkpoint.blocks[0].number,
327
+ blockCount: checkpoint.blocks.length,
293
328
  attestations: [],
329
+ l1: this.mockL1DataForCheckpoint(checkpoint),
294
330
  }),
295
331
  ),
296
332
  );
297
333
  }
298
334
 
335
+ getCheckpointedBlocksForEpoch(epochNumber: EpochNumber): Promise<CheckpointedL2Block[]> {
336
+ const checkpoints = this.getCheckpointsInEpoch(epochNumber);
337
+ return Promise.resolve(
338
+ checkpoints.flatMap(checkpoint => checkpoint.blocks.map(block => this.toCheckpointedBlock(block))),
339
+ );
340
+ }
341
+
299
342
  getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
300
343
  const blocks = this.l2Blocks.filter(b => b.header.globalVariables.slotNumber === slotNumber);
301
344
  return Promise.resolve(blocks);
@@ -384,7 +427,10 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
384
427
 
385
428
  const makeTipId = (blockId: typeof latestBlockId) => ({
386
429
  block: blockId,
387
- checkpoint: { number: CheckpointNumber.fromBlockNumber(blockId.number), hash: blockId.hash },
430
+ checkpoint: {
431
+ number: this.findCheckpointNumberForBlock(blockId.number) ?? CheckpointNumber(0),
432
+ hash: blockId.hash,
433
+ },
388
434
  });
389
435
 
390
436
  return {
@@ -472,4 +518,38 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
472
518
  getPendingChainValidationStatus(): Promise<ValidateCheckpointResult> {
473
519
  return Promise.resolve({ valid: true });
474
520
  }
521
+
522
+ /** Returns checkpoints whose slot falls within the given epoch. */
523
+ private getCheckpointsInEpoch(epochNumber: EpochNumber): Checkpoint[] {
524
+ const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
525
+ const [start, end] = getSlotRangeForEpoch(epochNumber, { epochDuration });
526
+ return this.checkpointList.filter(c => c.header.slotNumber >= start && c.header.slotNumber <= end);
527
+ }
528
+
529
+ /** Creates a mock L1PublishedData for a checkpoint. */
530
+ private mockL1DataForCheckpoint(checkpoint: Checkpoint): L1PublishedData {
531
+ return new L1PublishedData(BigInt(checkpoint.number), BigInt(checkpoint.number), Buffer32.random().toString());
532
+ }
533
+
534
+ /** Creates a CheckpointedL2Block from a block using stored checkpoint info. */
535
+ private toCheckpointedBlock(block: L2Block): CheckpointedL2Block {
536
+ const checkpoint = this.checkpointList.find(c => c.blocks.some(b => b.number === block.number));
537
+ const checkpointNumber = checkpoint?.number ?? block.checkpointNumber;
538
+ return new CheckpointedL2Block(
539
+ checkpointNumber,
540
+ block,
541
+ new L1PublishedData(
542
+ BigInt(block.number),
543
+ BigInt(block.number),
544
+ `0x${block.number.toString(16).padStart(64, '0')}`,
545
+ ),
546
+ [],
547
+ );
548
+ }
549
+
550
+ /** Finds the checkpoint number for a block, or undefined if the block is not in any checkpoint. */
551
+ private findCheckpointNumberForBlock(blockNumber: BlockNumber): CheckpointNumber | undefined {
552
+ const checkpoint = this.checkpointList.find(c => c.blocks.some(b => b.number === blockNumber));
553
+ return checkpoint?.number;
554
+ }
475
555
  }