@aztec/archiver 4.0.0-nightly.20260114 → 4.0.0-nightly.20260115

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 (169) hide show
  1. package/dest/archiver.d.ts +134 -0
  2. package/dest/archiver.d.ts.map +1 -0
  3. package/dest/archiver.js +767 -0
  4. package/dest/{archiver/config.d.ts → config.d.ts} +9 -1
  5. package/dest/config.d.ts.map +1 -0
  6. package/dest/{archiver/config.js → config.js} +9 -0
  7. package/dest/{archiver/errors.d.ts → errors.d.ts} +1 -1
  8. package/dest/errors.d.ts.map +1 -0
  9. package/dest/factory.d.ts +5 -6
  10. package/dest/factory.d.ts.map +1 -1
  11. package/dest/factory.js +82 -5
  12. package/dest/index.d.ts +10 -4
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +8 -3
  15. package/dest/interfaces.d.ts +9 -0
  16. package/dest/interfaces.d.ts.map +1 -0
  17. package/dest/interfaces.js +3 -0
  18. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  19. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  20. package/dest/{archiver/l1 → l1}/calldata_retriever.d.ts +2 -2
  21. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/data_retrieval.d.ts +2 -2
  23. package/dest/l1/data_retrieval.d.ts.map +1 -0
  24. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  25. package/dest/l1/debug_tx.d.ts.map +1 -0
  26. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +1 -1
  27. package/dest/l1/spire_proposer.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/trace_tx.d.ts +1 -1
  29. package/dest/l1/trace_tx.d.ts.map +1 -0
  30. package/dest/l1/types.d.ts +12 -0
  31. package/dest/l1/types.d.ts.map +1 -0
  32. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +1 -1
  33. package/dest/l1/validate_trace.d.ts.map +1 -0
  34. package/dest/modules/data_source_base.d.ts +83 -0
  35. package/dest/modules/data_source_base.d.ts.map +1 -0
  36. package/dest/{archiver/archive_source_base.js → modules/data_source_base.js} +109 -10
  37. package/dest/modules/data_store_updater.d.ts +46 -0
  38. package/dest/modules/data_store_updater.d.ts.map +1 -0
  39. package/dest/modules/data_store_updater.js +216 -0
  40. package/dest/modules/instrumentation.d.ts +37 -0
  41. package/dest/modules/instrumentation.d.ts.map +1 -0
  42. package/dest/modules/l1_synchronizer.d.ts +67 -0
  43. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  44. package/dest/{archiver/archiver.js → modules/l1_synchronizer.js} +60 -543
  45. package/dest/{archiver → modules}/validation.d.ts +1 -1
  46. package/dest/modules/validation.d.ts.map +1 -0
  47. package/dest/{archiver/kv_archiver_store → store}/block_store.d.ts +1 -1
  48. package/dest/store/block_store.d.ts.map +1 -0
  49. package/dest/store/contract_class_store.d.ts +18 -0
  50. package/dest/store/contract_class_store.d.ts.map +1 -0
  51. package/dest/store/contract_instance_store.d.ts +24 -0
  52. package/dest/store/contract_instance_store.d.ts.map +1 -0
  53. package/dest/{archiver/kv_archiver_store → store}/kv_archiver_store.d.ts +2 -2
  54. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  55. package/dest/{archiver/kv_archiver_store → store}/log_store.d.ts +1 -1
  56. package/dest/store/log_store.d.ts.map +1 -0
  57. package/dest/{archiver/kv_archiver_store → store}/message_store.d.ts +1 -1
  58. package/dest/store/message_store.d.ts.map +1 -0
  59. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  60. package/dest/structs/data_retrieval.d.ts.map +1 -0
  61. package/dest/structs/inbox_message.d.ts +15 -0
  62. package/dest/structs/inbox_message.d.ts.map +1 -0
  63. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  64. package/dest/structs/published.d.ts.map +1 -0
  65. package/dest/{archiver/test → test}/fake_l1_state.d.ts +1 -1
  66. package/dest/test/fake_l1_state.d.ts.map +1 -0
  67. package/dest/test/index.d.ts +2 -1
  68. package/dest/test/index.d.ts.map +1 -1
  69. package/dest/test/index.js +1 -0
  70. package/dest/test/mock_structs.d.ts +76 -2
  71. package/dest/test/mock_structs.d.ts.map +1 -1
  72. package/dest/test/mock_structs.js +133 -2
  73. package/package.json +15 -17
  74. package/src/archiver.ts +522 -0
  75. package/src/{archiver/config.ts → config.ts} +11 -0
  76. package/src/factory.ts +118 -6
  77. package/src/index.ts +10 -3
  78. package/src/interfaces.ts +9 -0
  79. package/src/{archiver/l1 → l1}/calldata_retriever.ts +1 -1
  80. package/src/{archiver/l1 → l1}/data_retrieval.ts +1 -1
  81. package/src/{archiver/archive_source_base.ts → modules/data_source_base.ts} +130 -30
  82. package/src/modules/data_store_updater.ts +318 -0
  83. package/src/{archiver/archiver.ts → modules/l1_synchronizer.ts} +68 -717
  84. package/src/test/index.ts +1 -0
  85. package/src/test/mock_structs.ts +247 -2
  86. package/dest/archiver/archive_source_base.d.ts +0 -75
  87. package/dest/archiver/archive_source_base.d.ts.map +0 -1
  88. package/dest/archiver/archiver.d.ts +0 -168
  89. package/dest/archiver/archiver.d.ts.map +0 -1
  90. package/dest/archiver/archiver_store_updates.d.ts +0 -38
  91. package/dest/archiver/archiver_store_updates.d.ts.map +0 -1
  92. package/dest/archiver/archiver_store_updates.js +0 -212
  93. package/dest/archiver/config.d.ts.map +0 -1
  94. package/dest/archiver/errors.d.ts.map +0 -1
  95. package/dest/archiver/index.d.ts +0 -8
  96. package/dest/archiver/index.d.ts.map +0 -1
  97. package/dest/archiver/index.js +0 -6
  98. package/dest/archiver/instrumentation.d.ts +0 -37
  99. package/dest/archiver/instrumentation.d.ts.map +0 -1
  100. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  101. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  102. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  103. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  104. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  105. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  106. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  107. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  108. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  109. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  110. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  111. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  112. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  113. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  114. package/dest/archiver/l1/types.d.ts +0 -12
  115. package/dest/archiver/l1/types.d.ts.map +0 -1
  116. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  117. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  118. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  119. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  120. package/dest/archiver/structs/published.d.ts.map +0 -1
  121. package/dest/archiver/test/fake_l1_state.d.ts.map +0 -1
  122. package/dest/archiver/validation.d.ts.map +0 -1
  123. package/dest/rpc/index.d.ts +0 -9
  124. package/dest/rpc/index.d.ts.map +0 -1
  125. package/dest/rpc/index.js +0 -15
  126. package/src/archiver/archiver_store_updates.ts +0 -321
  127. package/src/archiver/index.ts +0 -7
  128. package/src/rpc/index.ts +0 -16
  129. /package/dest/{archiver/errors.js → errors.js} +0 -0
  130. /package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +0 -0
  131. /package/dest/{archiver/l1 → l1}/calldata_retriever.js +0 -0
  132. /package/dest/{archiver/l1 → l1}/data_retrieval.js +0 -0
  133. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  134. /package/dest/{archiver/l1 → l1}/spire_proposer.js +0 -0
  135. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  136. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  137. /package/dest/{archiver/l1 → l1}/validate_trace.js +0 -0
  138. /package/dest/{archiver → modules}/instrumentation.js +0 -0
  139. /package/dest/{archiver → modules}/validation.js +0 -0
  140. /package/dest/{archiver/kv_archiver_store → store}/block_store.js +0 -0
  141. /package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +0 -0
  142. /package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +0 -0
  143. /package/dest/{archiver/kv_archiver_store → store}/kv_archiver_store.js +0 -0
  144. /package/dest/{archiver/kv_archiver_store → store}/log_store.js +0 -0
  145. /package/dest/{archiver/kv_archiver_store → store}/message_store.js +0 -0
  146. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  147. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  148. /package/dest/{archiver/structs → structs}/published.js +0 -0
  149. /package/dest/{archiver/test → test}/fake_l1_state.js +0 -0
  150. /package/src/{archiver/errors.ts → errors.ts} +0 -0
  151. /package/src/{archiver/l1 → l1}/README.md +0 -0
  152. /package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +0 -0
  153. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  154. /package/src/{archiver/l1 → l1}/spire_proposer.ts +0 -0
  155. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  156. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  157. /package/src/{archiver/l1 → l1}/validate_trace.ts +0 -0
  158. /package/src/{archiver → modules}/instrumentation.ts +0 -0
  159. /package/src/{archiver → modules}/validation.ts +0 -0
  160. /package/src/{archiver/kv_archiver_store → store}/block_store.ts +0 -0
  161. /package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +0 -0
  162. /package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +0 -0
  163. /package/src/{archiver/kv_archiver_store → store}/kv_archiver_store.ts +0 -0
  164. /package/src/{archiver/kv_archiver_store → store}/log_store.ts +0 -0
  165. /package/src/{archiver/kv_archiver_store → store}/message_store.ts +0 -0
  166. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  167. /package/src/{archiver/structs → structs}/inbox_message.ts +0 -0
  168. /package/src/{archiver/structs → structs}/published.ts +0 -0
  169. /package/src/{archiver/test → test}/fake_l1_state.ts +0 -0
@@ -370,63 +370,41 @@ function applyDecs2203RFactory() {
370
370
  function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
371
371
  return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
372
372
  }
373
- var _dec, _dec1, _dec2, _dec3, _dec4, _initProto;
374
- import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
375
- import { EpochCache } from '@aztec/epoch-cache';
376
- import { createEthereumChain } from '@aztec/ethereum/chain';
377
- import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
373
+ var _dec, _dec1, _dec2, _dec3, _initProto;
378
374
  import { maxBigint } from '@aztec/foundation/bigint';
379
375
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
380
- import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
381
- import { merge, pick } from '@aztec/foundation/collection';
382
- import { Fr } from '@aztec/foundation/curves/bn254';
383
- import { EthAddress } from '@aztec/foundation/eth-address';
376
+ import { Buffer32 } from '@aztec/foundation/buffer';
377
+ import { pick } from '@aztec/foundation/collection';
384
378
  import { createLogger } from '@aztec/foundation/log';
385
- import { promiseWithResolvers } from '@aztec/foundation/promise';
386
- import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
387
379
  import { count } from '@aztec/foundation/string';
388
- import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
380
+ import { Timer, elapsed } from '@aztec/foundation/timer';
389
381
  import { isDefined } from '@aztec/foundation/types';
390
- import { GENESIS_CHECKPOINT_HEADER_HASH, L2BlockSourceEvents } from '@aztec/stdlib/block';
391
- import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
382
+ import { L2BlockSourceEvents } from '@aztec/stdlib/block';
383
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
392
384
  import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
393
- import { execInSpan, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
394
- import { EventEmitter } from 'events';
395
- import { createPublicClient, fallback, http } from 'viem';
396
- import { ArchiveSourceBase } from './archive_source_base.js';
397
- import { addBlocksWithContractData, addCheckpointsWithContractData, unwindCheckpointsWithContractData } from './archiver_store_updates.js';
398
- import { InitialCheckpointNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
399
- import { ArchiverInstrumentation } from './instrumentation.js';
400
- import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './l1/data_retrieval.js';
401
- import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
385
+ import { execInSpan, trackSpan } from '@aztec/telemetry-client';
386
+ import { InitialCheckpointNumberNotSequentialError } from '../errors.js';
387
+ import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from '../l1/data_retrieval.js';
388
+ import { ArchiverDataStoreUpdater } from './data_store_updater.js';
402
389
  import { validateCheckpointAttestations } from './validation.js';
403
- function mapArchiverConfig(config) {
404
- return {
405
- pollingIntervalMs: config.archiverPollingIntervalMS,
406
- batchSize: config.archiverBatchSize,
407
- skipValidateCheckpointAttestations: config.skipValidateCheckpointAttestations,
408
- maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
409
- ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts
410
- };
411
- }
412
- _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _dec2 = trackSpan('Archiver.handleEpochPrune'), _dec3 = trackSpan('Archiver.handleL1ToL2Messages'), _dec4 = trackSpan('Archiver.handleCheckpoints');
390
+ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpochPrune'), _dec2 = trackSpan('Archiver.handleL1ToL2Messages'), _dec3 = trackSpan('Archiver.handleCheckpoints');
413
391
  /**
414
- * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
415
- * Responsible for handling robust L1 polling so that other components do not need to
416
- * concern themselves with it.
417
- */ export class Archiver extends ArchiveSourceBase {
392
+ * Handles L1 synchronization for the archiver.
393
+ * Responsible for fetching checkpoints, L1→L2 messages, and handling L1 reorgs.
394
+ */ export class ArchiverL1Synchronizer {
418
395
  publicClient;
419
396
  debugClient;
420
397
  rollup;
421
398
  inbox;
422
399
  l1Addresses;
423
- dataStore;
400
+ store;
424
401
  config;
425
402
  blobClient;
426
403
  epochCache;
427
404
  dateProvider;
428
405
  instrumentation;
429
406
  l1constants;
407
+ events;
430
408
  log;
431
409
  static{
432
410
  ({ e: [_initProto] } = _apply_decs_2203_r(this, [
@@ -438,197 +416,48 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
438
416
  [
439
417
  _dec1,
440
418
  2,
441
- "sync"
442
- ],
443
- [
444
- _dec2,
445
- 2,
446
419
  "handleEpochPrune"
447
420
  ],
448
421
  [
449
- _dec3,
422
+ _dec2,
450
423
  2,
451
424
  "handleL1ToL2Messages"
452
425
  ],
453
426
  [
454
- _dec4,
427
+ _dec3,
455
428
  2,
456
429
  "handleCheckpoints"
457
430
  ]
458
431
  ], []));
459
432
  }
460
- /** Event emitter for archiver events (L2BlockProven, L2PruneDetected, etc). */ events;
461
- /** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
462
433
  l1BlockNumber;
463
434
  l1Timestamp;
464
- initialSyncComplete;
465
- initialSyncPromise;
466
- /** Queue of blocks to be added to the store, processed by the sync loop. */ blockQueue;
435
+ updater;
467
436
  tracer;
468
- /**
469
- * Creates a new instance of the Archiver.
470
- * @param publicClient - A client for interacting with the Ethereum node.
471
- * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
472
- * @param rollup - Rollup contract instance.
473
- * @param inbox - Inbox contract instance.
474
- * @param l1Addresses - L1 contract addresses (registry, governance proposer, slash factory, slashing proposer).
475
- * @param dataStore - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
476
- * @param config - Archiver configuration options.
477
- * @param blobClient - Client for retrieving blob data.
478
- * @param epochCache - Cache for epoch-related data.
479
- * @param dateProvider - Provider for current date/time.
480
- * @param instrumentation - Instrumentation for metrics and tracing.
481
- * @param l1constants - L1 rollup constants.
482
- * @param log - A logger.
483
- */ constructor(publicClient, debugClient, rollup, inbox, l1Addresses, dataStore, config, blobClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
484
- super(dataStore), this.publicClient = publicClient, this.debugClient = debugClient, this.rollup = rollup, this.inbox = inbox, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobClient = blobClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.events = (_initProto(this), new EventEmitter()), this.initialSyncComplete = false, this.blockQueue = [];
485
- this.tracer = instrumentation.tracer;
486
- this.initialSyncPromise = promiseWithResolvers();
487
- // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
488
- // are done as fast as possible. This then gets updated once the initial sync completes.
489
- this.runningPromise = new RunningPromise(()=>this.sync(), this.log, this.config.pollingIntervalMs / 10, makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError));
490
- }
491
- /**
492
- * Creates a new instance of the Archiver and blocks until it syncs from chain.
493
- * @param config - The archiver's desired configuration.
494
- * @param archiverStore - The backing store for the archiver.
495
- * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
496
- * @returns - An instance of the archiver.
497
- */ static async createAndSync(config, archiverStore, deps, blockUntilSynced = true) {
498
- const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
499
- const publicClient = createPublicClient({
500
- chain: chain.chainInfo,
501
- transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
502
- batch: false
503
- }))),
504
- pollingInterval: config.viemPollingIntervalMS
505
- });
506
- // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
507
- const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
508
- const debugClient = createPublicClient({
509
- chain: chain.chainInfo,
510
- transport: fallback(debugRpcUrls.map((url)=>http(url, {
511
- batch: false
512
- }))),
513
- pollingInterval: config.viemPollingIntervalMS
514
- });
515
- const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
516
- const inbox = new InboxContract(publicClient, config.l1Contracts.inboxAddress);
517
- const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] = await Promise.all([
518
- rollup.getL1StartBlock(),
519
- rollup.getL1GenesisTime(),
520
- rollup.getProofSubmissionEpochs(),
521
- rollup.getGenesisArchiveTreeRoot(),
522
- rollup.getSlashingProposerAddress()
523
- ]);
524
- const l1StartBlockHash = await publicClient.getBlock({
525
- blockNumber: l1StartBlock,
526
- includeTransactions: false
527
- }).then((block)=>Buffer32.fromString(block.hash));
528
- const { aztecEpochDuration: epochDuration, aztecSlotDuration: slotDuration, ethereumSlotDuration } = config;
529
- const l1Constants = {
530
- l1StartBlockHash,
531
- l1StartBlock,
532
- l1GenesisTime,
533
- epochDuration,
534
- slotDuration,
535
- ethereumSlotDuration,
536
- proofSubmissionEpochs: Number(proofSubmissionEpochs),
537
- genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString())
538
- };
539
- const opts = merge({
540
- pollingIntervalMs: 10_000,
541
- batchSize: 100,
542
- maxAllowedEthClientDriftSeconds: 300,
543
- ethereumAllowNoDebugHosts: false
544
- }, mapArchiverConfig(config));
545
- const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
546
- const telemetry = deps.telemetry ?? getTelemetryClient();
547
- const archiver = new Archiver(publicClient, debugClient, rollup, inbox, {
548
- ...config.l1Contracts,
549
- slashingProposerAddress
550
- }, archiverStore, opts, deps.blobClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
551
- await archiver.start(blockUntilSynced);
552
- return archiver;
553
- }
554
- /** Updates archiver config */ updateConfig(newConfig) {
555
- this.config = merge(this.config, mapArchiverConfig(newConfig));
556
- }
557
- /**
558
- * Starts sync process.
559
- * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
560
- */ async start(blockUntilSynced) {
561
- if (this.runningPromise.isRunning()) {
562
- throw new Error('Archiver is already running');
563
- }
564
- await this.blobClient.testSources();
565
- await this.testEthereumNodeSynced();
566
- await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
567
- // Log initial state for the archiver
568
- const { l1StartBlock } = this.l1constants;
569
- const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
570
- const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
571
- this.log.info(`Starting archiver sync to rollup contract ${this.rollup.address} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`, {
572
- blocksSynchedTo,
573
- messagesSynchedTo,
574
- currentL2Checkpoint
575
- });
576
- // Start sync loop, and return the wait for initial sync if we are asked to block until synced
577
- this.runningPromise.start();
578
- if (blockUntilSynced) {
579
- return this.waitForInitialSync();
580
- }
581
- }
582
- syncImmediate() {
583
- return this.runningPromise.trigger();
584
- }
585
- /**
586
- * Queues a block to be added to the archiver store and triggers processing.
587
- * The block will be processed by the sync loop.
588
- * Implements the L2BlockSink interface.
589
- * @param block - The L2 block to add.
590
- * @returns A promise that resolves when the block has been added to the store, or rejects on error.
591
- */ addBlock(block) {
592
- return new Promise((resolve, reject)=>{
593
- this.blockQueue.push({
594
- block,
595
- resolve,
596
- reject
597
- });
598
- this.log.debug(`Queued block ${block.number} for processing`);
599
- // Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
600
- this.syncImmediate().catch((err)=>{
601
- this.log.error(`Sync immediate call failed: ${err}`);
602
- });
603
- });
437
+ constructor(publicClient, debugClient, rollup, inbox, l1Addresses, store, config, blobClient, epochCache, dateProvider, instrumentation, l1constants, events, tracer, log = createLogger('archiver:l1-sync')){
438
+ this.publicClient = publicClient;
439
+ this.debugClient = debugClient;
440
+ this.rollup = rollup;
441
+ this.inbox = inbox;
442
+ this.l1Addresses = l1Addresses;
443
+ this.store = store;
444
+ this.config = config;
445
+ this.blobClient = blobClient;
446
+ this.epochCache = epochCache;
447
+ this.dateProvider = dateProvider;
448
+ this.instrumentation = instrumentation;
449
+ this.l1constants = l1constants;
450
+ this.events = events;
451
+ this.log = log;
452
+ _initProto(this);
453
+ this.updater = new ArchiverDataStoreUpdater(this.store);
454
+ this.tracer = tracer;
604
455
  }
605
- /**
606
- * Processes all queued blocks, adding them to the store.
607
- * Called at the beginning of each sync iteration.
608
- * Blocks are processed in the order they were queued.
609
- */ async processQueuedBlocks() {
610
- if (this.blockQueue.length === 0) {
611
- return;
612
- }
613
- // Take all blocks from the queue
614
- const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
615
- this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
616
- // Process each block individually to properly resolve/reject each promise
617
- for (const { block, resolve, reject } of queuedItems){
618
- try {
619
- await addBlocksWithContractData(this.store, [
620
- block
621
- ]);
622
- this.log.debug(`Added block ${block.number} to store`);
623
- resolve();
624
- } catch (err) {
625
- this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
626
- reject(err);
627
- }
628
- }
456
+ /** Returns the last L1 block number that was synced. */ getL1BlockNumber() {
457
+ return this.l1BlockNumber;
629
458
  }
630
- waitForInitialSync() {
631
- return this.initialSyncPromise.promise;
459
+ /** Returns the last L1 timestamp that was synced. */ getL1Timestamp() {
460
+ return this.l1Timestamp;
632
461
  }
633
462
  /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
634
463
  const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
@@ -643,7 +472,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
643
472
  throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
644
473
  }
645
474
  }
646
- async syncFromL1() {
475
+ async syncFromL1(initialSyncComplete) {
647
476
  /**
648
477
  * We keep track of three "pointers" to L1 blocks:
649
478
  * 1. the last L1 block that published an L2 block
@@ -706,7 +535,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
706
535
  if (currentL1BlockNumber > blocksSynchedTo) {
707
536
  // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
708
537
  // pending chain validation status, proven checkpoint number, and synched L1 block number.
709
- const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
538
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber, initialSyncComplete);
710
539
  // Then we prune the current epoch if it'd reorg on next submission.
711
540
  // Note that we don't do this before retrieving checkpoints because we may need to retrieve
712
541
  // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
@@ -739,26 +568,6 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
739
568
  l1TimestampAtStart: currentL1Timestamp,
740
569
  l1BlockNumberAtEnd
741
570
  });
742
- // We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
743
- // so if the initial sync took too long, we still go for another iteration.
744
- if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= l1BlockNumberAtEnd) {
745
- this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
746
- l1BlockNumber: currentL1BlockNumber,
747
- syncPoint: await this.store.getSynchPoint(),
748
- ...await this.getL2Tips()
749
- });
750
- this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
751
- this.initialSyncComplete = true;
752
- this.initialSyncPromise.resolve();
753
- }
754
- }
755
- /**
756
- * Fetches logs from L1 contracts and processes them.
757
- */ async sync() {
758
- // Process any queued blocks first, before doing L1 sync
759
- await this.processQueuedBlocks();
760
- // Now perform L1 sync
761
- await this.syncFromL1();
762
571
  }
763
572
  /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
764
573
  const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
@@ -776,7 +585,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
776
585
  }
777
586
  /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
778
587
  const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
779
- const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
588
+ const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
780
589
  const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
781
590
  if (canPrune) {
782
591
  const timer = new Timer();
@@ -801,8 +610,8 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
801
610
  blocks: newBlocks
802
611
  });
803
612
  this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
804
- await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
805
- this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
613
+ await this.updater.unwindCheckpointsWithContractData(localPendingCheckpointNumber, checkpointsToUnwind);
614
+ this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`);
806
615
  this.instrumentation.processPrune(timer.ms());
807
616
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
808
617
  // Seems like the next iteration should handle this.
@@ -844,7 +653,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
844
653
  remoteMessagesState
845
654
  });
846
655
  // Compare message count and rolling hash. If they match, no need to retrieve anything.
847
- if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)) {
656
+ if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer32.ZERO)) {
848
657
  this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
849
658
  return;
850
659
  }
@@ -971,8 +780,8 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
971
780
  }
972
781
  return Buffer32.fromString(block.hash);
973
782
  }
974
- async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
975
- const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
783
+ async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber, initialSyncComplete) {
784
+ const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
976
785
  const initialValidationResult = await this.store.getPendingChainValidationStatus();
977
786
  const { provenCheckpointNumber, provenArchive, pendingCheckpointNumber, pendingArchive, archiveOfMyCheckpoint: archiveForLocalPendingCheckpointNumber } = await execInSpan(this.tracer, 'Archiver.getRollupStatus', ()=>this.rollup.status(localPendingCheckpointNumber, {
978
787
  blockNumber: currentL1BlockNumber
@@ -996,9 +805,9 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
996
805
  // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
997
806
  // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
998
807
  if (provenCheckpointNumber === 0) {
999
- const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
808
+ const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
1000
809
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
1001
- await this.setProvenCheckpointNumber(provenCheckpointNumber);
810
+ await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
1002
811
  this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
1003
812
  provenCheckpointNumber
1004
813
  });
@@ -1007,15 +816,15 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
1007
816
  const localCheckpointForDestinationProvenCheckpointNumber = await this.store.getCheckpointData(provenCheckpointNumber);
1008
817
  // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
1009
818
  // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
1010
- const synched = await this.getSynchedCheckpointNumber();
819
+ const synched = await this.store.getSynchedCheckpointNumber();
1011
820
  if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
1012
821
  this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
1013
822
  }
1014
823
  this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
1015
824
  if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)) {
1016
- const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
825
+ const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
1017
826
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
1018
- await this.setProvenCheckpointNumber(provenCheckpointNumber);
827
+ await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
1019
828
  this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
1020
829
  provenCheckpointNumber
1021
830
  });
@@ -1092,8 +901,8 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
1092
901
  tipAfterUnwind--;
1093
902
  }
1094
903
  const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
1095
- await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
1096
- this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
904
+ await this.updater.unwindCheckpointsWithContractData(localPendingCheckpointNumber, checkpointsToUnwind);
905
+ this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`);
1097
906
  }
1098
907
  }
1099
908
  // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
@@ -1106,7 +915,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
1106
915
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
1107
916
  this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
1108
917
  // TODO(md): Retrieve from blob client then from consensus client, then from peers
1109
- const retrievedCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointsFromRollup', ()=>retrieveCheckpointsFromRollup(this.rollup, this.publicClient, this.debugClient, this.blobClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log, !this.initialSyncComplete));
918
+ const retrievedCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointsFromRollup', ()=>retrieveCheckpointsFromRollup(this.rollup, this.publicClient, this.debugClient, this.blobClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log, !initialSyncComplete));
1110
919
  if (retrievedCheckpoints.length === 0) {
1111
920
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
1112
921
  // See further details in earlier comments.
@@ -1148,7 +957,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
1148
957
  // Check the inHash of the checkpoint against the l1->l2 messages.
1149
958
  // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
1150
959
  // checkpoints we just retrieved.
1151
- const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
960
+ const l1ToL2Messages = await this.store.getL1ToL2Messages(published.checkpoint.number);
1152
961
  const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
1153
962
  const publishedInHash = published.checkpoint.header.inHash;
1154
963
  if (!computedInHash.equals(publishedInHash)) {
@@ -1171,7 +980,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
1171
980
  }
1172
981
  try {
1173
982
  const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
1174
- const [processDuration] = await elapsed(()=>execInSpan(this.tracer, 'Archiver.addCheckpoints', ()=>this.addCheckpoints(validCheckpoints, updatedValidationResult)));
983
+ const [processDuration] = await elapsed(()=>execInSpan(this.tracer, 'Archiver.addCheckpoints', ()=>this.updater.addCheckpointsWithContractData(validCheckpoints, updatedValidationResult)));
1175
984
  this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
1176
985
  } catch (err) {
1177
986
  if (err instanceof InitialCheckpointNumberNotSequentialError) {
@@ -1213,7 +1022,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
1213
1022
  const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
1214
1023
  // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
1215
1024
  // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
1216
- const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.getSynchedCheckpointNumber();
1025
+ const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.store.getSynchedCheckpointNumber();
1217
1026
  if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
1218
1027
  // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
1219
1028
  // but still haven't reached the pending checkpoint according to the call to the rollup contract.
@@ -1245,303 +1054,11 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _de
1245
1054
  });
1246
1055
  }
1247
1056
  }
1248
- /** Resumes the archiver after a stop. */ resume() {
1249
- if (this.runningPromise.isRunning()) {
1250
- this.log.warn(`Archiver already running`);
1251
- }
1252
- this.log.info(`Restarting archiver`);
1253
- this.runningPromise.start();
1254
- }
1255
- /**
1256
- * Stops the archiver.
1257
- * @returns A promise signalling completion of the stop process.
1258
- */ async stop() {
1259
- this.log.debug('Stopping...');
1260
- await this.runningPromise.stop();
1261
- this.log.info('Stopped.');
1262
- return Promise.resolve();
1263
- }
1264
- backupTo(destPath) {
1265
- return this.dataStore.backupTo(destPath);
1266
- }
1267
- getL1Constants() {
1268
- return Promise.resolve(this.l1constants);
1269
- }
1270
- getGenesisValues() {
1271
- return Promise.resolve({
1272
- genesisArchiveRoot: this.l1constants.genesisArchiveRoot
1273
- });
1274
- }
1275
- getRollupAddress() {
1276
- return Promise.resolve(EthAddress.fromString(this.rollup.address));
1277
- }
1278
- getRegistryAddress() {
1279
- return Promise.resolve(this.l1Addresses.registryAddress);
1280
- }
1281
- getL1BlockNumber() {
1282
- return this.l1BlockNumber;
1283
- }
1284
- getL1Timestamp() {
1285
- return Promise.resolve(this.l1Timestamp);
1286
- }
1287
- getL2SlotNumber() {
1288
- return Promise.resolve(this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants));
1289
- }
1290
- getL2EpochNumber() {
1291
- return Promise.resolve(this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants));
1292
- }
1293
- async getBlocksForEpoch(epochNumber) {
1294
- const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1295
- const blocks = [];
1296
- // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1297
- // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1298
- let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1299
- const slot = (b)=>b.header.slotNumber;
1300
- while(checkpoint && slot(checkpoint) >= start){
1301
- if (slot(checkpoint) <= end) {
1302
- // push the blocks on backwards
1303
- const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
1304
- for(let i = endBlock; i >= checkpoint.startBlock; i--){
1305
- const block = await this.getBlock(BlockNumber(i));
1306
- if (block) {
1307
- blocks.push(block);
1308
- }
1309
- }
1310
- }
1311
- checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
1312
- }
1313
- return blocks.reverse();
1314
- }
1315
- async getBlockHeadersForEpoch(epochNumber) {
1316
- const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1317
- const blocks = [];
1318
- // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1319
- // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1320
- let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1321
- const slot = (b)=>b.header.slotNumber;
1322
- while(checkpoint && slot(checkpoint) >= start){
1323
- if (slot(checkpoint) <= end) {
1324
- // push the blocks on backwards
1325
- const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
1326
- for(let i = endBlock; i >= checkpoint.startBlock; i--){
1327
- const block = await this.getBlockHeader(BlockNumber(i));
1328
- if (block) {
1329
- blocks.push(block);
1330
- }
1331
- }
1332
- }
1333
- checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
1334
- }
1335
- return blocks.reverse();
1336
- }
1337
- async isEpochComplete(epochNumber) {
1338
- // The epoch is complete if the current L2 block is the last one in the epoch (or later)
1339
- const header = await this.getBlockHeader('latest');
1340
- const slot = header ? header.globalVariables.slotNumber : undefined;
1341
- const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1342
- if (slot && slot >= endSlot) {
1343
- return true;
1344
- }
1345
- // If we haven't run an initial sync, just return false.
1346
- const l1Timestamp = this.l1Timestamp;
1347
- if (l1Timestamp === undefined) {
1348
- return false;
1349
- }
1350
- // If not, the epoch may also be complete if the L2 slot has passed without a block
1351
- // We compute this based on the end timestamp for the given epoch and the timestamp of the last L1 block
1352
- const [_startTimestamp, endTimestamp] = getTimestampRangeForEpoch(epochNumber, this.l1constants);
1353
- // For this computation, we throw in a few extra seconds just for good measure,
1354
- // since we know the next L1 block won't be mined within this range. Remember that
1355
- // l1timestamp is the timestamp of the last l1 block we've seen, so this relies on
1356
- // the fact that L1 won't mine two blocks within this time of each other.
1357
- // TODO(palla/reorg): Is the above a safe assumption?
1358
- const leeway = 1n;
1359
- return l1Timestamp + leeway >= endTimestamp;
1360
- }
1361
- /** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
1362
- return this.initialSyncComplete;
1363
- }
1364
1057
  async getCheckpointHeader(number) {
1365
- if (number === 'latest') {
1366
- number = await this.getSynchedCheckpointNumber();
1367
- }
1368
- if (number === 0) {
1369
- return undefined;
1370
- }
1371
1058
  const checkpoint = await this.store.getCheckpointData(number);
1372
1059
  if (!checkpoint) {
1373
1060
  return undefined;
1374
1061
  }
1375
1062
  return checkpoint.header;
1376
1063
  }
1377
- getCheckpointNumber() {
1378
- return this.getSynchedCheckpointNumber();
1379
- }
1380
- getSynchedCheckpointNumber() {
1381
- return this.store.getSynchedCheckpointNumber();
1382
- }
1383
- getProvenCheckpointNumber() {
1384
- return this.store.getProvenCheckpointNumber();
1385
- }
1386
- setProvenCheckpointNumber(checkpointNumber) {
1387
- return this.store.setProvenCheckpointNumber(checkpointNumber);
1388
- }
1389
- unwindCheckpoints(from, checkpointsToUnwind) {
1390
- return unwindCheckpointsWithContractData(this.store, from, checkpointsToUnwind);
1391
- }
1392
- async getLastBlockNumberInCheckpoint(checkpointNumber) {
1393
- const checkpointData = await this.store.getCheckpointData(checkpointNumber);
1394
- if (!checkpointData) {
1395
- return undefined;
1396
- }
1397
- return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
1398
- }
1399
- addCheckpoints(checkpoints, pendingChainValidationStatus) {
1400
- return addCheckpointsWithContractData(this.store, checkpoints, pendingChainValidationStatus);
1401
- }
1402
- getCheckpointedBlockNumber() {
1403
- return this.store.getCheckpointedL2BlockNumber();
1404
- }
1405
- async getL2Tips() {
1406
- const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber] = await Promise.all([
1407
- this.getBlockNumber(),
1408
- this.getProvenBlockNumber(),
1409
- this.getCheckpointedBlockNumber()
1410
- ]);
1411
- // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
1412
- // We just force it 2 epochs worth of proven data for now.
1413
- // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
1414
- const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
1415
- const beforeInitialblockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
1416
- // Get the latest block header and checkpointed blocks for proven, finalised and checkpointed blocks
1417
- const [latestBlockHeader, provenCheckpointedBlock, finalizedCheckpointedBlock, checkpointedBlock] = await Promise.all([
1418
- latestBlockNumber > beforeInitialblockNumber ? this.getBlockHeader(latestBlockNumber) : undefined,
1419
- provenBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(provenBlockNumber) : undefined,
1420
- finalizedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(finalizedBlockNumber) : undefined,
1421
- checkpointedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(checkpointedBlockNumber) : undefined
1422
- ]);
1423
- if (latestBlockNumber > beforeInitialblockNumber && !latestBlockHeader) {
1424
- throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
1425
- }
1426
- // Checkpointed blocks must exist for proven, finalized and checkpointed tips if they are beyond the initial block number.
1427
- if (checkpointedBlockNumber > beforeInitialblockNumber && !checkpointedBlock?.block.header) {
1428
- throw new Error(`Failed to retrieve checkpointed block header for block ${checkpointedBlockNumber} (latest block is ${latestBlockNumber})`);
1429
- }
1430
- if (provenBlockNumber > beforeInitialblockNumber && !provenCheckpointedBlock?.block.header) {
1431
- throw new Error(`Failed to retrieve proven checkpointed for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`);
1432
- }
1433
- if (finalizedBlockNumber > beforeInitialblockNumber && !finalizedCheckpointedBlock?.block.header) {
1434
- throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
1435
- }
1436
- const latestBlockHeaderHash = await latestBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1437
- const provenBlockHeaderHash = await provenCheckpointedBlock?.block.header?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1438
- const finalizedBlockHeaderHash = await finalizedCheckpointedBlock?.block.header?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1439
- const checkpointedBlockHeaderHash = await checkpointedBlock?.block.header?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1440
- // Now attempt to retrieve checkpoints for proven, finalised and checkpointed blocks
1441
- const [[provenBlockCheckpoint], [finalizedBlockCheckpoint], [checkpointedBlockCheckpoint]] = await Promise.all([
1442
- provenCheckpointedBlock !== undefined ? await this.getPublishedCheckpoints(provenCheckpointedBlock?.checkpointNumber, 1) : [
1443
- undefined
1444
- ],
1445
- finalizedCheckpointedBlock !== undefined ? await this.getPublishedCheckpoints(finalizedCheckpointedBlock?.checkpointNumber, 1) : [
1446
- undefined
1447
- ],
1448
- checkpointedBlock !== undefined ? await this.getPublishedCheckpoints(checkpointedBlock?.checkpointNumber, 1) : [
1449
- undefined
1450
- ]
1451
- ]);
1452
- const initialcheckpointId = {
1453
- number: CheckpointNumber.ZERO,
1454
- hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
1455
- };
1456
- const makeCheckpointId = (checkpoint)=>{
1457
- if (checkpoint === undefined) {
1458
- return initialcheckpointId;
1459
- }
1460
- return {
1461
- number: checkpoint.checkpoint.number,
1462
- hash: checkpoint.checkpoint.hash().toString()
1463
- };
1464
- };
1465
- const l2Tips = {
1466
- proposed: {
1467
- number: latestBlockNumber,
1468
- hash: latestBlockHeaderHash.toString()
1469
- },
1470
- proven: {
1471
- block: {
1472
- number: provenBlockNumber,
1473
- hash: provenBlockHeaderHash.toString()
1474
- },
1475
- checkpoint: makeCheckpointId(provenBlockCheckpoint)
1476
- },
1477
- finalized: {
1478
- block: {
1479
- number: finalizedBlockNumber,
1480
- hash: finalizedBlockHeaderHash.toString()
1481
- },
1482
- checkpoint: makeCheckpointId(finalizedBlockCheckpoint)
1483
- },
1484
- checkpointed: {
1485
- block: {
1486
- number: checkpointedBlockNumber,
1487
- hash: checkpointedBlockHeaderHash.toString()
1488
- },
1489
- checkpoint: makeCheckpointId(checkpointedBlockCheckpoint)
1490
- }
1491
- };
1492
- return l2Tips;
1493
- }
1494
- async rollbackTo(targetL2BlockNumber) {
1495
- // TODO(pw/mbps): This still assumes 1 block per checkpoint
1496
- const currentBlocks = await this.getL2Tips();
1497
- const currentL2Block = currentBlocks.proposed.number;
1498
- const currentProvenBlock = currentBlocks.proven.block.number;
1499
- if (targetL2BlockNumber >= currentL2Block) {
1500
- throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
1501
- }
1502
- const blocksToUnwind = currentL2Block - targetL2BlockNumber;
1503
- const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
1504
- if (!targetL2Block) {
1505
- throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1506
- }
1507
- const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1508
- const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
1509
- const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1510
- this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
1511
- await unwindCheckpointsWithContractData(this.store, CheckpointNumber(currentL2Block), blocksToUnwind);
1512
- this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
1513
- await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1514
- this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1515
- await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
1516
- await this.store.setMessageSynchedL1Block({
1517
- l1BlockNumber: targetL1BlockNumber,
1518
- l1BlockHash: targetL1BlockHash
1519
- });
1520
- if (targetL2BlockNumber < currentProvenBlock) {
1521
- this.log.info(`Clearing proven L2 block number`);
1522
- await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
1523
- }
1524
- // TODO(palla/reorg): Set the finalized block when we add support for it.
1525
- // if (targetL2BlockNumber < currentFinalizedBlock) {
1526
- // this.log.info(`Clearing finalized L2 block number`);
1527
- // await this.store.setFinalizedL2BlockNumber(0);
1528
- // }
1529
- }
1530
- async getCheckpointsForEpoch(epochNumber) {
1531
- const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1532
- const checkpoints = [];
1533
- // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1534
- // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1535
- let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1536
- const slot = (b)=>b.header.slotNumber;
1537
- while(checkpointData && slot(checkpointData) >= start){
1538
- if (slot(checkpointData) <= end) {
1539
- // push the checkpoints on backwards
1540
- const [checkpoint] = await this.getPublishedCheckpoints(checkpointData.checkpointNumber, 1);
1541
- checkpoints.push(checkpoint.checkpoint);
1542
- }
1543
- checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
1544
- }
1545
- return checkpoints.reverse();
1546
- }
1547
1064
  }