@aztec/archiver 0.0.1-commit.3469e52 → 0.0.1-commit.35158ae7e

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 (107) hide show
  1. package/README.md +9 -0
  2. package/dest/archiver.d.ts +12 -9
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +98 -124
  5. package/dest/config.d.ts +3 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +2 -1
  8. package/dest/errors.d.ts +26 -9
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +34 -13
  11. package/dest/factory.d.ts +4 -2
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +29 -23
  14. package/dest/index.d.ts +2 -1
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -0
  17. package/dest/l1/bin/retrieve-calldata.js +35 -32
  18. package/dest/l1/calldata_retriever.d.ts +73 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +190 -259
  21. package/dest/l1/data_retrieval.d.ts +9 -9
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +24 -22
  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/validate_trace.d.ts +6 -3
  28. package/dest/l1/validate_trace.d.ts.map +1 -1
  29. package/dest/l1/validate_trace.js +13 -9
  30. package/dest/modules/data_source_base.d.ts +27 -23
  31. package/dest/modules/data_source_base.d.ts.map +1 -1
  32. package/dest/modules/data_source_base.js +49 -124
  33. package/dest/modules/data_store_updater.d.ts +42 -26
  34. package/dest/modules/data_store_updater.d.ts.map +1 -1
  35. package/dest/modules/data_store_updater.js +149 -129
  36. package/dest/modules/instrumentation.d.ts +17 -4
  37. package/dest/modules/instrumentation.d.ts.map +1 -1
  38. package/dest/modules/instrumentation.js +36 -12
  39. package/dest/modules/l1_synchronizer.d.ts +5 -8
  40. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  41. package/dest/modules/l1_synchronizer.js +59 -25
  42. package/dest/store/block_store.d.ts +49 -32
  43. package/dest/store/block_store.d.ts.map +1 -1
  44. package/dest/store/block_store.js +188 -95
  45. package/dest/store/contract_class_store.d.ts +2 -3
  46. package/dest/store/contract_class_store.d.ts.map +1 -1
  47. package/dest/store/contract_class_store.js +16 -72
  48. package/dest/store/contract_instance_store.d.ts +1 -1
  49. package/dest/store/contract_instance_store.d.ts.map +1 -1
  50. package/dest/store/contract_instance_store.js +6 -2
  51. package/dest/store/kv_archiver_store.d.ts +64 -36
  52. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  53. package/dest/store/kv_archiver_store.js +63 -29
  54. package/dest/store/l2_tips_cache.d.ts +19 -0
  55. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  56. package/dest/store/l2_tips_cache.js +89 -0
  57. package/dest/store/log_store.d.ts +9 -6
  58. package/dest/store/log_store.d.ts.map +1 -1
  59. package/dest/store/log_store.js +150 -53
  60. package/dest/store/message_store.d.ts +5 -1
  61. package/dest/store/message_store.d.ts.map +1 -1
  62. package/dest/store/message_store.js +14 -1
  63. package/dest/test/fake_l1_state.d.ts +16 -4
  64. package/dest/test/fake_l1_state.d.ts.map +1 -1
  65. package/dest/test/fake_l1_state.js +95 -23
  66. package/dest/test/index.js +3 -1
  67. package/dest/test/mock_archiver.d.ts +1 -1
  68. package/dest/test/mock_archiver.d.ts.map +1 -1
  69. package/dest/test/mock_archiver.js +3 -2
  70. package/dest/test/mock_l2_block_source.d.ts +39 -23
  71. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  72. package/dest/test/mock_l2_block_source.js +157 -112
  73. package/dest/test/mock_structs.d.ts +6 -2
  74. package/dest/test/mock_structs.d.ts.map +1 -1
  75. package/dest/test/mock_structs.js +24 -10
  76. package/dest/test/noop_l1_archiver.d.ts +26 -0
  77. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  78. package/dest/test/noop_l1_archiver.js +72 -0
  79. package/package.json +14 -13
  80. package/src/archiver.ts +126 -151
  81. package/src/config.ts +8 -1
  82. package/src/errors.ts +52 -24
  83. package/src/factory.ts +46 -22
  84. package/src/index.ts +1 -0
  85. package/src/l1/README.md +25 -68
  86. package/src/l1/bin/retrieve-calldata.ts +45 -33
  87. package/src/l1/calldata_retriever.ts +249 -379
  88. package/src/l1/data_retrieval.ts +27 -29
  89. package/src/l1/spire_proposer.ts +7 -15
  90. package/src/l1/validate_trace.ts +24 -6
  91. package/src/modules/data_source_base.ts +84 -169
  92. package/src/modules/data_store_updater.ts +166 -161
  93. package/src/modules/instrumentation.ts +46 -14
  94. package/src/modules/l1_synchronizer.ts +72 -36
  95. package/src/store/block_store.ts +239 -140
  96. package/src/store/contract_class_store.ts +16 -110
  97. package/src/store/contract_instance_store.ts +8 -5
  98. package/src/store/kv_archiver_store.ts +104 -53
  99. package/src/store/l2_tips_cache.ts +89 -0
  100. package/src/store/log_store.ts +231 -70
  101. package/src/store/message_store.ts +20 -1
  102. package/src/test/fake_l1_state.ts +127 -28
  103. package/src/test/index.ts +3 -0
  104. package/src/test/mock_archiver.ts +3 -2
  105. package/src/test/mock_l2_block_source.ts +211 -129
  106. package/src/test/mock_structs.ts +45 -15
  107. package/src/test/noop_l1_archiver.ts +115 -0
@@ -1,22 +1,21 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
2
  import { EpochCache } from '@aztec/epoch-cache';
3
3
  import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
4
- import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
5
4
  import type { L1BlockId } from '@aztec/ethereum/l1-types';
6
5
  import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
6
+ import { asyncPool } from '@aztec/foundation/async-pool';
7
7
  import { maxBigint } from '@aztec/foundation/bigint';
8
8
  import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
9
9
  import { Buffer32 } from '@aztec/foundation/buffer';
10
10
  import { pick } from '@aztec/foundation/collection';
11
11
  import { Fr } from '@aztec/foundation/curves/bn254';
12
- import { EthAddress } from '@aztec/foundation/eth-address';
13
12
  import { type Logger, createLogger } from '@aztec/foundation/log';
14
13
  import { count } from '@aztec/foundation/string';
15
14
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
16
15
  import { isDefined } from '@aztec/foundation/types';
17
16
  import { type ArchiverEmitter, L2BlockSourceEvents, type ValidateCheckpointResult } from '@aztec/stdlib/block';
18
17
  import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
19
- import { type L1RollupConstants, getEpochAtSlot, getSlotAtTimestamp } from '@aztec/stdlib/epoch-helpers';
18
+ import { type L1RollupConstants, getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
20
19
  import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
21
20
  import { type Traceable, type Tracer, execInSpan, trackSpan } from '@aztec/telemetry-client';
22
21
 
@@ -28,6 +27,7 @@ import {
28
27
  retrievedToPublishedCheckpoint,
29
28
  } from '../l1/data_retrieval.js';
30
29
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
30
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
31
31
  import type { InboxMessage } from '../structs/inbox_message.js';
32
32
  import { ArchiverDataStoreUpdater } from './data_store_updater.js';
33
33
  import type { ArchiverInstrumentation } from './instrumentation.js';
@@ -60,10 +60,6 @@ export class ArchiverL1Synchronizer implements Traceable {
60
60
  private readonly debugClient: ViemPublicDebugClient,
61
61
  private readonly rollup: RollupContract,
62
62
  private readonly inbox: InboxContract,
63
- private readonly l1Addresses: Pick<
64
- L1ContractAddresses,
65
- 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
66
- > & { slashingProposerAddress: EthAddress },
67
63
  private readonly store: KVArchiverDataStore,
68
64
  private config: {
69
65
  batchSize: number;
@@ -74,12 +70,18 @@ export class ArchiverL1Synchronizer implements Traceable {
74
70
  private readonly epochCache: EpochCache,
75
71
  private readonly dateProvider: DateProvider,
76
72
  private readonly instrumentation: ArchiverInstrumentation,
77
- private readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
73
+ private readonly l1Constants: L1RollupConstants & {
74
+ l1StartBlockHash: Buffer32;
75
+ genesisArchiveRoot: Fr;
76
+ },
78
77
  private readonly events: ArchiverEmitter,
79
78
  tracer: Tracer,
79
+ l2TipsCache?: L2TipsCache,
80
80
  private readonly log: Logger = createLogger('archiver:l1-sync'),
81
81
  ) {
82
- this.updater = new ArchiverDataStoreUpdater(this.store);
82
+ this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
83
+ rollupManaLimit: l1Constants.rollupManaLimit,
84
+ });
83
85
  this.tracer = tracer;
84
86
  }
85
87
 
@@ -215,6 +217,9 @@ export class ArchiverL1Synchronizer implements Traceable {
215
217
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
216
218
  }
217
219
 
220
+ // Update the finalized L2 checkpoint based on L1 finality.
221
+ await this.updateFinalizedCheckpoint();
222
+
218
223
  // After syncing has completed, update the current l1 block number and timestamp,
219
224
  // otherwise we risk announcing to the world that we've synced to a given point,
220
225
  // but the corresponding blocks have not been processed (see #12631).
@@ -230,6 +235,27 @@ export class ArchiverL1Synchronizer implements Traceable {
230
235
  });
231
236
  }
232
237
 
238
+ /** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
239
+ private async updateFinalizedCheckpoint(): Promise<void> {
240
+ try {
241
+ const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
242
+ const finalizedL1BlockNumber = finalizedL1Block.number;
243
+ const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
244
+ blockNumber: finalizedL1BlockNumber,
245
+ });
246
+ const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
247
+ if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
248
+ await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
249
+ this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
250
+ finalizedCheckpointNumber,
251
+ finalizedL1BlockNumber,
252
+ });
253
+ }
254
+ } catch (err) {
255
+ this.log.warn(`Failed to update finalized checkpoint: ${err}`);
256
+ }
257
+ }
258
+
233
259
  /** Prune all proposed local blocks that should have been checkpointed by now. */
234
260
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
235
261
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
@@ -249,8 +275,7 @@ export class ArchiverL1Synchronizer implements Traceable {
249
275
  const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
250
276
 
251
277
  // What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
252
- const nextL1BlockTimestamp = currentL1Timestamp + BigInt(this.l1Constants.ethereumSlotDuration);
253
- const slotAtNextL1Block = getSlotAtTimestamp(nextL1BlockTimestamp, this.l1Constants);
278
+ const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
254
279
 
255
280
  // Prune provisional blocks from slots that have ended without being checkpointed
256
281
  if (firstUncheckpointedBlockSlot !== undefined && firstUncheckpointedBlockSlot < slotAtNextL1Block) {
@@ -258,7 +283,7 @@ export class ArchiverL1Synchronizer implements Traceable {
258
283
  `Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`,
259
284
  { firstUncheckpointedBlockHeader: firstUncheckpointedBlockHeader.toInspect(), slotAtNextL1Block },
260
285
  );
261
- const prunedBlocks = await this.updater.removeBlocksAfter(lastCheckpointedBlockNumber);
286
+ const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
262
287
 
263
288
  if (prunedBlocks.length > 0) {
264
289
  this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
@@ -309,17 +334,20 @@ export class ArchiverL1Synchronizer implements Traceable {
309
334
 
310
335
  const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
311
336
 
312
- const checkpointPromises = Array.from({ length: checkpointsToUnwind })
313
- .fill(0)
314
- .map((_, i) => this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
315
- const checkpoints = await Promise.all(checkpointPromises);
316
-
317
- const blockPromises = await Promise.all(
318
- checkpoints
319
- .filter(isDefined)
320
- .map(cp => this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))),
337
+ // Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
338
+ // promises when the gap between local pending and proven checkpoint numbers is large.
339
+ const BATCH_SIZE = 10;
340
+ const indices = Array.from({ length: checkpointsToUnwind }, (_, i) => CheckpointNumber(i + pruneFrom));
341
+ const checkpoints = (await asyncPool(BATCH_SIZE, indices, idx => this.store.getCheckpointData(idx))).filter(
342
+ isDefined,
321
343
  );
322
- const newBlocks = blockPromises.filter(isDefined).flat();
344
+ const newBlocks = (
345
+ await asyncPool(BATCH_SIZE, checkpoints, cp =>
346
+ this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)),
347
+ )
348
+ )
349
+ .filter(isDefined)
350
+ .flat();
323
351
 
324
352
  // Emit an event for listening services to react to the chain prune
325
353
  this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
@@ -331,10 +359,10 @@ export class ArchiverL1Synchronizer implements Traceable {
331
359
  this.log.debug(
332
360
  `L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`,
333
361
  );
334
- await this.updater.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
362
+ await this.updater.removeCheckpointsAfter(provenCheckpointNumber);
335
363
  this.log.warn(
336
- `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
337
- `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
364
+ `Removed ${count(checkpointsToUnwind, 'checkpoint')} after checkpoint ${provenCheckpointNumber} ` +
365
+ `due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
338
366
  `Updated latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`,
339
367
  );
340
368
  this.instrumentation.processPrune(timer.ms());
@@ -367,6 +395,7 @@ export class ArchiverL1Synchronizer implements Traceable {
367
395
  const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
368
396
  const localLastMessage = await this.store.getLastL1ToL2Message();
369
397
  const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
398
+ await this.store.setInboxTreeInProgress(remoteMessagesState.treeInProgress);
370
399
 
371
400
  this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
372
401
  localMessagesInserted,
@@ -551,7 +580,7 @@ export class ArchiverL1Synchronizer implements Traceable {
551
580
  if (provenCheckpointNumber === 0) {
552
581
  const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
553
582
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
554
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
583
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
555
584
  this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
556
585
  }
557
586
  }
@@ -583,13 +612,13 @@ export class ArchiverL1Synchronizer implements Traceable {
583
612
  ) {
584
613
  const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
585
614
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
586
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
615
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
587
616
  this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
588
617
  const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
589
618
  const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1Constants);
590
619
  const lastBlockNumberInCheckpoint =
591
620
  localCheckpointForDestinationProvenCheckpointNumber.startBlock +
592
- localCheckpointForDestinationProvenCheckpointNumber.numBlocks -
621
+ localCheckpointForDestinationProvenCheckpointNumber.blockCount -
593
622
  1;
594
623
 
595
624
  this.events.emit(L2BlockSourceEvents.L2BlockProven, {
@@ -598,7 +627,7 @@ export class ArchiverL1Synchronizer implements Traceable {
598
627
  slotNumber: provenSlotNumber,
599
628
  epochNumber: provenEpochNumber,
600
629
  });
601
- this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
630
+ this.instrumentation.updateLastProvenCheckpoint(localCheckpointForDestinationProvenCheckpointNumber);
602
631
  } else {
603
632
  this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
604
633
  }
@@ -675,11 +704,11 @@ export class ArchiverL1Synchronizer implements Traceable {
675
704
  tipAfterUnwind--;
676
705
  }
677
706
 
678
- const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
679
- await this.updater.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
707
+ const checkpointsToRemove = localPendingCheckpointNumber - tipAfterUnwind;
708
+ await this.updater.removeCheckpointsAfter(CheckpointNumber(tipAfterUnwind));
680
709
 
681
710
  this.log.warn(
682
- `Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
711
+ `Removed ${count(checkpointsToRemove, 'checkpoint')} after checkpoint ${tipAfterUnwind} ` +
683
712
  `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` +
684
713
  `Updated L2 latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`,
685
714
  );
@@ -707,7 +736,6 @@ export class ArchiverL1Synchronizer implements Traceable {
707
736
  this.blobClient,
708
737
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
709
738
  searchEndBlock,
710
- this.l1Addresses,
711
739
  this.instrumentation,
712
740
  this.log,
713
741
  !initialSyncComplete, // isHistoricalSync
@@ -802,12 +830,20 @@ export class ArchiverL1Synchronizer implements Traceable {
802
830
  );
803
831
  }
804
832
 
833
+ for (const published of validCheckpoints) {
834
+ this.instrumentation.processCheckpointL1Timing({
835
+ slotNumber: published.checkpoint.header.slotNumber,
836
+ l1Timestamp: published.l1.timestamp,
837
+ l1Constants: this.l1Constants,
838
+ });
839
+ }
840
+
805
841
  try {
806
842
  const updatedValidationResult =
807
843
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
808
844
  const [processDuration, result] = await elapsed(() =>
809
- execInSpan(this.tracer, 'Archiver.setCheckpointData', () =>
810
- this.updater.setNewCheckpointData(validCheckpoints, updatedValidationResult),
845
+ execInSpan(this.tracer, 'Archiver.addCheckpoints', () =>
846
+ this.updater.addCheckpoints(validCheckpoints, updatedValidationResult),
811
847
  ),
812
848
  );
813
849
  this.instrumentation.processNewBlocks(
@@ -820,7 +856,7 @@ export class ArchiverL1Synchronizer implements Traceable {
820
856
  const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
821
857
  const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
822
858
 
823
- this.log.warn(
859
+ this.log.info(
824
860
  `Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
825
861
  { prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
826
862
  );