@aztec/archiver 0.0.1-commit.5914bae → 0.0.1-commit.59a0419c6

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 (95) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +7 -5
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +18 -5
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +15 -3
  8. package/dest/errors.d.ts +44 -2
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +58 -2
  11. package/dest/factory.d.ts +2 -2
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +3 -4
  14. package/dest/index.d.ts +3 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +2 -1
  17. package/dest/l1/calldata_retriever.d.ts +1 -1
  18. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  19. package/dest/l1/calldata_retriever.js +2 -1
  20. package/dest/l1/data_retrieval.d.ts +24 -12
  21. package/dest/l1/data_retrieval.d.ts.map +1 -1
  22. package/dest/l1/data_retrieval.js +36 -37
  23. package/dest/l1/validate_historical_logs.d.ts +23 -0
  24. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  25. package/dest/l1/validate_historical_logs.js +108 -0
  26. package/dest/modules/data_source_base.d.ts +6 -4
  27. package/dest/modules/data_source_base.d.ts.map +1 -1
  28. package/dest/modules/data_source_base.js +10 -4
  29. package/dest/modules/data_store_updater.d.ts +15 -10
  30. package/dest/modules/data_store_updater.d.ts.map +1 -1
  31. package/dest/modules/data_store_updater.js +27 -59
  32. package/dest/modules/instrumentation.d.ts +18 -2
  33. package/dest/modules/instrumentation.d.ts.map +1 -1
  34. package/dest/modules/instrumentation.js +32 -6
  35. package/dest/modules/l1_synchronizer.d.ts +6 -2
  36. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  37. package/dest/modules/l1_synchronizer.js +248 -149
  38. package/dest/modules/validation.d.ts +1 -1
  39. package/dest/modules/validation.d.ts.map +1 -1
  40. package/dest/modules/validation.js +2 -2
  41. package/dest/store/block_store.d.ts +46 -5
  42. package/dest/store/block_store.d.ts.map +1 -1
  43. package/dest/store/block_store.js +225 -63
  44. package/dest/store/contract_class_store.d.ts +2 -3
  45. package/dest/store/contract_class_store.d.ts.map +1 -1
  46. package/dest/store/contract_class_store.js +1 -65
  47. package/dest/store/kv_archiver_store.d.ts +35 -14
  48. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  49. package/dest/store/kv_archiver_store.js +40 -14
  50. package/dest/store/l2_tips_cache.d.ts +2 -1
  51. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  52. package/dest/store/l2_tips_cache.js +27 -7
  53. package/dest/store/log_store.d.ts +6 -3
  54. package/dest/store/log_store.d.ts.map +1 -1
  55. package/dest/store/log_store.js +47 -10
  56. package/dest/store/message_store.d.ts +5 -1
  57. package/dest/store/message_store.d.ts.map +1 -1
  58. package/dest/store/message_store.js +20 -8
  59. package/dest/test/fake_l1_state.d.ts +7 -3
  60. package/dest/test/fake_l1_state.d.ts.map +1 -1
  61. package/dest/test/fake_l1_state.js +50 -10
  62. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  63. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  64. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  65. package/dest/test/mock_l2_block_source.d.ts +7 -2
  66. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  67. package/dest/test/mock_l2_block_source.js +28 -3
  68. package/dest/test/noop_l1_archiver.d.ts +1 -1
  69. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  70. package/dest/test/noop_l1_archiver.js +4 -2
  71. package/package.json +13 -13
  72. package/src/archiver.ts +33 -8
  73. package/src/config.ts +22 -2
  74. package/src/errors.ts +94 -2
  75. package/src/factory.ts +2 -3
  76. package/src/index.ts +2 -1
  77. package/src/l1/calldata_retriever.ts +2 -1
  78. package/src/l1/data_retrieval.ts +52 -53
  79. package/src/l1/validate_historical_logs.ts +140 -0
  80. package/src/modules/data_source_base.ts +23 -4
  81. package/src/modules/data_store_updater.ts +43 -85
  82. package/src/modules/instrumentation.ts +47 -7
  83. package/src/modules/l1_synchronizer.ts +321 -185
  84. package/src/modules/validation.ts +2 -2
  85. package/src/store/block_store.ts +295 -73
  86. package/src/store/contract_class_store.ts +1 -103
  87. package/src/store/kv_archiver_store.ts +67 -24
  88. package/src/store/l2_tips_cache.ts +58 -13
  89. package/src/store/log_store.ts +62 -20
  90. package/src/store/message_store.ts +26 -9
  91. package/src/structs/inbox_message.ts +1 -1
  92. package/src/test/fake_l1_state.ts +72 -15
  93. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  94. package/src/test/mock_l2_block_source.ts +37 -2
  95. package/src/test/noop_l1_archiver.ts +3 -1
@@ -9,7 +9,7 @@ import {
9
9
  getAttestationInfoFromPayload,
10
10
  } from '@aztec/stdlib/block';
11
11
  import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
12
- import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
12
+ import { type L1RollupConstants, computeQuorum, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
13
13
  import { ConsensusPayload } from '@aztec/stdlib/p2p';
14
14
 
15
15
  export type { ValidateCheckpointResult };
@@ -66,7 +66,7 @@ export async function validateCheckpointAttestations(
66
66
  return { valid: true };
67
67
  }
68
68
 
69
- const requiredAttestationCount = Math.floor((committee.length * 2) / 3) + 1;
69
+ const requiredAttestationCount = computeQuorum(committee.length);
70
70
 
71
71
  const failedValidationResult = <TReason extends ValidateCheckpointNegativeResult['reason']>(reason: TReason) => ({
72
72
  valid: false as const,
@@ -19,7 +19,15 @@ import {
19
19
  deserializeValidateCheckpointResult,
20
20
  serializeValidateCheckpointResult,
21
21
  } from '@aztec/stdlib/block';
22
- import { type CheckpointData, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
22
+ import {
23
+ Checkpoint,
24
+ type CheckpointData,
25
+ type CommonCheckpointData,
26
+ L1PublishedData,
27
+ type ProposedCheckpointData,
28
+ type ProposedCheckpointInput,
29
+ PublishedCheckpoint,
30
+ } from '@aztec/stdlib/checkpoint';
23
31
  import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
24
32
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
25
33
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
@@ -44,6 +52,11 @@ import {
44
52
  CheckpointNotFoundError,
45
53
  CheckpointNumberNotSequentialError,
46
54
  InitialCheckpointNumberNotSequentialError,
55
+ NoProposedCheckpointToPromoteError,
56
+ ProposedCheckpointArchiveRootMismatchError,
57
+ ProposedCheckpointNotSequentialError,
58
+ ProposedCheckpointPromotionNotSequentialError,
59
+ ProposedCheckpointStaleError,
47
60
  } from '../errors.js';
48
61
 
49
62
  export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
@@ -58,17 +71,27 @@ type BlockStorage = {
58
71
  indexWithinCheckpoint: number;
59
72
  };
60
73
 
61
- type CheckpointStorage = {
74
+ /** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
75
+ type CommonCheckpointStorage = {
62
76
  header: Buffer;
63
77
  archive: Buffer;
64
78
  checkpointOutHash: Buffer;
65
79
  checkpointNumber: number;
66
80
  startBlock: number;
67
81
  blockCount: number;
82
+ };
83
+
84
+ type CheckpointStorage = CommonCheckpointStorage & {
68
85
  l1: Buffer;
69
86
  attestations: Buffer[];
70
87
  };
71
88
 
89
+ /** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
90
+ type ProposedCheckpointStorage = CommonCheckpointStorage & {
91
+ totalManaUsed: string;
92
+ feeAssetPriceModifier: string;
93
+ };
94
+
72
95
  export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
73
96
 
74
97
  /**
@@ -111,6 +134,9 @@ export class BlockStore {
111
134
  /** Index mapping block archive to block number */
112
135
  #blockArchiveIndex: AztecAsyncMap<string, number>;
113
136
 
137
+ /** Singleton: assumes max 1-deep pipeline. For deeper pipelining, replace with a map keyed by checkpoint number. */
138
+ #proposedCheckpoint: AztecAsyncSingleton<ProposedCheckpointStorage>;
139
+
114
140
  #log = createLogger('archiver:block_store');
115
141
 
116
142
  constructor(private db: AztecAsyncKVStore) {
@@ -126,6 +152,7 @@ export class BlockStore {
126
152
  this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
127
153
  this.#checkpoints = db.openMap('archiver_checkpoints');
128
154
  this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
155
+ this.#proposedCheckpoint = db.openSingleton('proposed_checkpoint_data');
129
156
  }
130
157
 
131
158
  /**
@@ -160,7 +187,8 @@ export class BlockStore {
160
187
  const blockLastArchive = block.header.lastArchive.root;
161
188
 
162
189
  // Extract the latest block and checkpoint numbers
163
- const previousBlockNumber = await this.getLatestBlockNumber();
190
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
191
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
164
192
  const previousCheckpointNumber = await this.getLatestCheckpointNumber();
165
193
 
166
194
  // Verify we're not overwriting checkpointed blocks
@@ -179,9 +207,19 @@ export class BlockStore {
179
207
  throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
180
208
  }
181
209
 
182
- // The same check as above but for checkpoints
183
- if (!opts.force && previousCheckpointNumber !== blockCheckpointNumber - 1) {
184
- throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, previousCheckpointNumber);
210
+ // The same check as above but for checkpoints. Accept the block if either the confirmed
211
+ // checkpoint or the pending (locally validated but not yet confirmed) checkpoint matches.
212
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
213
+ if (
214
+ !opts.force &&
215
+ previousCheckpointNumber !== expectedCheckpointNumber &&
216
+ proposedCheckpointNumber !== expectedCheckpointNumber
217
+ ) {
218
+ const [reported, source]: [CheckpointNumber, 'confirmed' | 'proposed'] =
219
+ proposedCheckpointNumber > previousCheckpointNumber
220
+ ? [proposedCheckpointNumber, 'proposed']
221
+ : [previousCheckpointNumber, 'confirmed'];
222
+ throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
185
223
  }
186
224
 
187
225
  // Extract the previous block if there is one and see if it is for the same checkpoint or not
@@ -217,7 +255,7 @@ export class BlockStore {
217
255
  }
218
256
 
219
257
  /**
220
- * Append new cheskpoints to the store's list.
258
+ * Append new checkpoints to the store's list.
221
259
  * @param checkpoints - The L2 checkpoints to be added to the store.
222
260
  * @returns True if the operation is successful.
223
261
  */
@@ -247,29 +285,8 @@ export class BlockStore {
247
285
  throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
248
286
  }
249
287
 
250
- // Extract the previous checkpoint if there is one
251
- const currentFirstCheckpointNumber = checkpoints[0].checkpoint.number;
252
- let previousCheckpointData: CheckpointData | undefined = undefined;
253
- if (currentFirstCheckpointNumber - 1 !== INITIAL_CHECKPOINT_NUMBER - 1) {
254
- // There should be a previous checkpoint
255
- previousCheckpointData = await this.getCheckpointData(CheckpointNumber(currentFirstCheckpointNumber - 1));
256
- if (previousCheckpointData === undefined) {
257
- throw new CheckpointNotFoundError(CheckpointNumber(currentFirstCheckpointNumber - 1));
258
- }
259
- }
260
-
261
- let previousBlockNumber: BlockNumber | undefined = undefined;
262
- let previousBlock: L2Block | undefined = undefined;
263
-
264
- // If we have a previous checkpoint then we need to get the previous block number
265
- if (previousCheckpointData !== undefined) {
266
- previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
267
- previousBlock = await this.getBlock(previousBlockNumber);
268
- if (previousBlock === undefined) {
269
- // We should be able to get the required previous block
270
- throw new BlockNotFoundError(previousBlockNumber);
271
- }
272
- }
288
+ // Get the last block of the previous checkpoint for archive chaining
289
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
273
290
 
274
291
  // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
275
292
  let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
@@ -286,42 +303,14 @@ export class BlockStore {
286
303
  }
287
304
  previousCheckpoint = checkpoint;
288
305
 
289
- // Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
290
- for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
291
- const block = checkpoint.checkpoint.blocks[i];
292
- if (previousBlock) {
293
- // The blocks should have a sequential block number
294
- if (previousBlock.number !== block.number - 1) {
295
- throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
296
- }
297
- // If the blocks are for the same checkpoint then they should have sequential indexes
298
- if (
299
- previousBlock.checkpointNumber === block.checkpointNumber &&
300
- previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
301
- ) {
302
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
303
- }
304
- if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
305
- throw new BlockArchiveNotConsistentError(
306
- block.number,
307
- previousBlock.number,
308
- block.header.lastArchive.root,
309
- previousBlock.archive.root,
310
- );
311
- }
312
- } else {
313
- // No previous block, must be block 1 at checkpoint index 0
314
- if (block.indexWithinCheckpoint !== 0) {
315
- throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
316
- }
317
- if (block.number !== INITIAL_L2_BLOCK_NUM) {
318
- throw new BlockNumberNotSequentialError(block.number, undefined);
319
- }
320
- }
306
+ // Validate block sequencing, indexes, and archive chaining
307
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
321
308
 
322
- previousBlock = block;
323
- await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
309
+ // Store every block in the database (may already exist, but L1 data is authoritative)
310
+ for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
311
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
324
312
  }
313
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
325
314
 
326
315
  // Store the checkpoint in the database
327
316
  await this.#checkpoints.set(checkpoint.checkpoint.number, {
@@ -339,6 +328,10 @@ export class BlockStore {
339
328
  await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
340
329
  }
341
330
 
331
+ // Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
332
+ const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
333
+ await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
334
+
342
335
  await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
343
336
  return true;
344
337
  });
@@ -388,6 +381,68 @@ export class BlockStore {
388
381
  return checkpoints.slice(i);
389
382
  }
390
383
 
384
+ /**
385
+ * Gets the last block of the checkpoint before the given one.
386
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
387
+ */
388
+ private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
389
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
390
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
391
+ return undefined;
392
+ }
393
+
394
+ const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
395
+ if (previousCheckpointData === undefined) {
396
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
397
+ }
398
+
399
+ const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
400
+ const previousBlock = await this.getBlock(previousBlockNumber);
401
+ if (previousBlock === undefined) {
402
+ throw new BlockNotFoundError(previousBlockNumber);
403
+ }
404
+
405
+ return previousBlock;
406
+ }
407
+
408
+ /**
409
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
410
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
411
+ * proposed checkpoints (setProposedCheckpoint).
412
+ */
413
+ private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
414
+ for (const block of blocks) {
415
+ if (previousBlock) {
416
+ if (previousBlock.number !== block.number - 1) {
417
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
418
+ }
419
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
420
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
421
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
422
+ }
423
+ } else if (block.indexWithinCheckpoint !== 0) {
424
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
425
+ }
426
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
427
+ throw new BlockArchiveNotConsistentError(
428
+ block.number,
429
+ previousBlock.number,
430
+ block.header.lastArchive.root,
431
+ previousBlock.archive.root,
432
+ );
433
+ }
434
+ } else {
435
+ if (block.indexWithinCheckpoint !== 0) {
436
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
437
+ }
438
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
439
+ throw new BlockNumberNotSequentialError(block.number, undefined);
440
+ }
441
+ }
442
+ previousBlock = block;
443
+ }
444
+ }
445
+
391
446
  private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
392
447
  const blockHash = await block.hash();
393
448
 
@@ -480,6 +535,12 @@ export class BlockStore {
480
535
  this.#log.debug(`Removed checkpoint ${c}`);
481
536
  }
482
537
 
538
+ // Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
539
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
540
+ if (proposedCheckpointNumber > checkpointNumber) {
541
+ await this.#proposedCheckpoint.delete();
542
+ }
543
+
483
544
  return { blocksRemoved };
484
545
  });
485
546
  }
@@ -585,7 +646,7 @@ export class BlockStore {
585
646
  const removedBlocks: L2Block[] = [];
586
647
 
587
648
  // Get the latest block number to determine the range
588
- const latestBlockNumber = await this.getLatestBlockNumber();
649
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
589
650
 
590
651
  // Iterate from blockNumber + 1 to latestBlockNumber
591
652
  for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
@@ -618,13 +679,6 @@ export class BlockStore {
618
679
  }
619
680
  }
620
681
 
621
- async getLatestBlockNumber(): Promise<BlockNumber> {
622
- const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
623
- return typeof latestBlocknumber === 'number'
624
- ? BlockNumber(latestBlocknumber)
625
- : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
626
- }
627
-
628
682
  async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
629
683
  const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
630
684
  if (latestCheckpointNumber === undefined) {
@@ -633,6 +687,133 @@ export class BlockStore {
633
687
  return CheckpointNumber(latestCheckpointNumber);
634
688
  }
635
689
 
690
+ async hasProposedCheckpoint(): Promise<boolean> {
691
+ const proposed = await this.#proposedCheckpoint.getAsync();
692
+ return proposed !== undefined;
693
+ }
694
+
695
+ /** Deletes the proposed checkpoint from storage. */
696
+ async deleteProposedCheckpoint(): Promise<void> {
697
+ await this.#proposedCheckpoint.delete();
698
+ }
699
+
700
+ /**
701
+ * Promotes the proposed checkpoint singleton to a confirmed checkpoint entry.
702
+ * This persists the checkpoint to the store, clears the proposed singleton, and updates the L1 sync point.
703
+ * Should only be called after the checkpoint has been validated.
704
+ * @param expectedArchiveRoot - The archive root to match against the proposed checkpoint, to guard against races.
705
+ */
706
+ async promoteProposedToCheckpointed(
707
+ l1: L1PublishedData,
708
+ attestations: CommitteeAttestation[],
709
+ expectedArchiveRoot: Fr,
710
+ ): Promise<void> {
711
+ return await this.db.transactionAsync(async () => {
712
+ const proposed = await this.getProposedCheckpointOnly();
713
+ if (!proposed) {
714
+ throw new NoProposedCheckpointToPromoteError();
715
+ }
716
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
717
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
718
+ }
719
+
720
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
721
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
722
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
723
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
724
+ }
725
+
726
+ // Write the checkpoint entry
727
+ await this.#checkpoints.set(proposed.checkpointNumber, {
728
+ header: proposed.header.toBuffer(),
729
+ archive: proposed.archive.toBuffer(),
730
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
731
+ l1: l1.toBuffer(),
732
+ attestations: attestations.map(attestation => attestation.toBuffer()),
733
+ checkpointNumber: proposed.checkpointNumber,
734
+ startBlock: proposed.startBlock,
735
+ blockCount: proposed.blockCount,
736
+ });
737
+
738
+ // Update the slot-to-checkpoint index
739
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
740
+
741
+ // Clear the proposed checkpoint singleton
742
+ await this.#proposedCheckpoint.delete();
743
+
744
+ // Update the last synced L1 block
745
+ await this.#lastSynchedL1Block.set(l1.blockNumber);
746
+ });
747
+ }
748
+
749
+ /** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */
750
+ async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber: CheckpointNumber): Promise<void> {
751
+ const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
752
+ if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
753
+ await this.#proposedCheckpoint.delete();
754
+ }
755
+ }
756
+
757
+ /** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */
758
+ async getProposedCheckpointOnly(): Promise<ProposedCheckpointData | undefined> {
759
+ const stored = await this.#proposedCheckpoint.getAsync();
760
+ if (!stored) {
761
+ return undefined;
762
+ }
763
+ return this.convertToProposedCheckpointData(stored);
764
+ }
765
+
766
+ /**
767
+ * Gets the checkpoint at the proposed tip
768
+ * - pending checkpoint if it exists
769
+ * - fallsback to latest confirmed checkpoint otherwise
770
+ * @returns CommonCheckpointData
771
+ */
772
+ async getProposedCheckpoint(): Promise<CommonCheckpointData | undefined> {
773
+ const stored = await this.#proposedCheckpoint.getAsync();
774
+ if (!stored) {
775
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
776
+ }
777
+ return this.convertToProposedCheckpointData(stored);
778
+ }
779
+
780
+ private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
781
+ return {
782
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
783
+ header: CheckpointHeader.fromBuffer(stored.header),
784
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
785
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
786
+ startBlock: BlockNumber(stored.startBlock),
787
+ blockCount: stored.blockCount,
788
+ totalManaUsed: BigInt(stored.totalManaUsed),
789
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
790
+ };
791
+ }
792
+
793
+ /**
794
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
795
+ * @returns CheckpointNumber
796
+ */
797
+ async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
798
+ const proposed = await this.getProposedCheckpoint();
799
+ if (!proposed) {
800
+ return await this.getLatestCheckpointNumber();
801
+ }
802
+ return CheckpointNumber(proposed.checkpointNumber);
803
+ }
804
+
805
+ /**
806
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
807
+ * @returns BlockNumber
808
+ */
809
+ async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
810
+ const proposed = await this.getProposedCheckpoint();
811
+ if (!proposed) {
812
+ return await this.getCheckpointedL2BlockNumber();
813
+ }
814
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
815
+ }
816
+
636
817
  async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
637
818
  const blockStorage = await this.#blocks.getAsync(number);
638
819
  if (!blockStorage) {
@@ -847,7 +1028,7 @@ export class BlockStore {
847
1028
  return {
848
1029
  header: BlockHeader.fromBuffer(blockStorage.header),
849
1030
  archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
850
- blockHash: Fr.fromBuffer(blockStorage.blockHash),
1031
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
851
1032
  checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
852
1033
  indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
853
1034
  };
@@ -1007,6 +1188,47 @@ export class BlockStore {
1007
1188
  return this.#lastSynchedL1Block.set(l1BlockNumber);
1008
1189
  }
1009
1190
 
1191
+ /** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
1192
+ * Computes archive and checkpointOutHash from the stored blocks. */
1193
+ async setProposedCheckpoint(proposed: ProposedCheckpointInput) {
1194
+ return await this.db.transactionAsync(async () => {
1195
+ const current = await this.getProposedCheckpointNumber();
1196
+ if (proposed.checkpointNumber <= current) {
1197
+ throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
1198
+ }
1199
+ const confirmed = await this.getLatestCheckpointNumber();
1200
+ if (proposed.checkpointNumber !== confirmed + 1) {
1201
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
1202
+ }
1203
+
1204
+ // Ensure the previous checkpoint + blocks exist
1205
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1206
+ const blocks: L2Block[] = [];
1207
+ for (let i = 0; i < proposed.blockCount; i++) {
1208
+ const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
1209
+ if (!block) {
1210
+ throw new BlockNotFoundError(proposed.startBlock + i);
1211
+ }
1212
+ blocks.push(block);
1213
+ }
1214
+ this.validateCheckpointBlocks(blocks, previousBlock);
1215
+
1216
+ const archive = blocks[blocks.length - 1].archive;
1217
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1218
+
1219
+ await this.#proposedCheckpoint.set({
1220
+ header: proposed.header.toBuffer(),
1221
+ archive: archive.toBuffer(),
1222
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1223
+ checkpointNumber: proposed.checkpointNumber,
1224
+ startBlock: proposed.startBlock,
1225
+ blockCount: proposed.blockCount,
1226
+ totalManaUsed: proposed.totalManaUsed.toString(),
1227
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1228
+ });
1229
+ });
1230
+ }
1231
+
1010
1232
  async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
1011
1233
  const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
1012
1234
  this.getLatestCheckpointNumber(),
@@ -2,14 +2,7 @@ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { toArray } from '@aztec/foundation/iterable';
3
3
  import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
4
4
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
5
- import { FunctionSelector } from '@aztec/stdlib/abi';
6
- import type {
7
- ContractClassPublic,
8
- ContractClassPublicWithBlockNumber,
9
- ExecutablePrivateFunctionWithMembershipProof,
10
- UtilityFunctionWithMembershipProof,
11
- } from '@aztec/stdlib/contract';
12
- import { Vector } from '@aztec/stdlib/types';
5
+ import type { ContractClassPublic, ContractClassPublicWithBlockNumber } from '@aztec/stdlib/contract';
13
6
 
14
7
  /**
15
8
  * LMDB-based contract class storage for the archiver.
@@ -64,37 +57,6 @@ export class ContractClassStore {
64
57
  async getContractClassIds(): Promise<Fr[]> {
65
58
  return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
66
59
  }
67
-
68
- async addFunctions(
69
- contractClassId: Fr,
70
- newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
71
- newUtilityFunctions: UtilityFunctionWithMembershipProof[],
72
- ): Promise<boolean> {
73
- await this.db.transactionAsync(async () => {
74
- const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
75
- if (!existingClassBuffer) {
76
- throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
77
- }
78
-
79
- const existingClass = deserializeContractClassPublic(existingClassBuffer);
80
- const { privateFunctions: existingPrivateFns, utilityFunctions: existingUtilityFns } = existingClass;
81
-
82
- const updatedClass: Omit<ContractClassPublicWithBlockNumber, 'id'> = {
83
- ...existingClass,
84
- privateFunctions: [
85
- ...existingPrivateFns,
86
- ...newPrivateFunctions.filter(newFn => !existingPrivateFns.some(f => f.selector.equals(newFn.selector))),
87
- ],
88
- utilityFunctions: [
89
- ...existingUtilityFns,
90
- ...newUtilityFunctions.filter(newFn => !existingUtilityFns.some(f => f.selector.equals(newFn.selector))),
91
- ],
92
- };
93
- await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
94
- });
95
-
96
- return true;
97
- }
98
60
  }
99
61
 
100
62
  function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
@@ -102,83 +64,19 @@ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWit
102
64
  contractClass.l2BlockNumber,
103
65
  numToUInt8(contractClass.version),
104
66
  contractClass.artifactHash,
105
- contractClass.privateFunctions.length,
106
- contractClass.privateFunctions.map(serializePrivateFunction),
107
- contractClass.utilityFunctions.length,
108
- contractClass.utilityFunctions.map(serializeUtilityFunction),
109
67
  contractClass.packedBytecode.length,
110
68
  contractClass.packedBytecode,
111
69
  contractClass.privateFunctionsRoot,
112
70
  );
113
71
  }
114
72
 
115
- function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
116
- return serializeToBuffer(
117
- fn.selector,
118
- fn.vkHash,
119
- fn.bytecode.length,
120
- fn.bytecode,
121
- fn.functionMetadataHash,
122
- fn.artifactMetadataHash,
123
- fn.utilityFunctionsTreeRoot,
124
- new Vector(fn.privateFunctionTreeSiblingPath),
125
- fn.privateFunctionTreeLeafIndex,
126
- new Vector(fn.artifactTreeSiblingPath),
127
- fn.artifactTreeLeafIndex,
128
- );
129
- }
130
-
131
- function serializeUtilityFunction(fn: UtilityFunctionWithMembershipProof): Buffer {
132
- return serializeToBuffer(
133
- fn.selector,
134
- fn.bytecode.length,
135
- fn.bytecode,
136
- fn.functionMetadataHash,
137
- fn.artifactMetadataHash,
138
- fn.privateFunctionsArtifactTreeRoot,
139
- new Vector(fn.artifactTreeSiblingPath),
140
- fn.artifactTreeLeafIndex,
141
- );
142
- }
143
-
144
73
  function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
145
74
  const reader = BufferReader.asReader(buffer);
146
75
  return {
147
76
  l2BlockNumber: reader.readNumber(),
148
77
  version: reader.readUInt8() as 1,
149
78
  artifactHash: reader.readObject(Fr),
150
- privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
151
- utilityFunctions: reader.readVector({ fromBuffer: deserializeUtilityFunction }),
152
79
  packedBytecode: reader.readBuffer(),
153
80
  privateFunctionsRoot: reader.readObject(Fr),
154
81
  };
155
82
  }
156
-
157
- function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
158
- const reader = BufferReader.asReader(buffer);
159
- return {
160
- selector: reader.readObject(FunctionSelector),
161
- vkHash: reader.readObject(Fr),
162
- bytecode: reader.readBuffer(),
163
- functionMetadataHash: reader.readObject(Fr),
164
- artifactMetadataHash: reader.readObject(Fr),
165
- utilityFunctionsTreeRoot: reader.readObject(Fr),
166
- privateFunctionTreeSiblingPath: reader.readVector(Fr),
167
- privateFunctionTreeLeafIndex: reader.readNumber(),
168
- artifactTreeSiblingPath: reader.readVector(Fr),
169
- artifactTreeLeafIndex: reader.readNumber(),
170
- };
171
- }
172
-
173
- function deserializeUtilityFunction(buffer: Buffer | BufferReader): UtilityFunctionWithMembershipProof {
174
- const reader = BufferReader.asReader(buffer);
175
- return {
176
- selector: reader.readObject(FunctionSelector),
177
- bytecode: reader.readBuffer(),
178
- functionMetadataHash: reader.readObject(Fr),
179
- artifactMetadataHash: reader.readObject(Fr),
180
- privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
181
- artifactTreeSiblingPath: reader.readVector(Fr),
182
- artifactTreeLeafIndex: reader.readNumber(),
183
- };
184
- }