@aztec/archiver 3.0.0-devnet.2 → 3.0.0-devnet.2-patch.1

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 (127) hide show
  1. package/dest/archiver/archiver.d.ts +67 -60
  2. package/dest/archiver/archiver.d.ts.map +1 -1
  3. package/dest/archiver/archiver.js +391 -262
  4. package/dest/archiver/archiver_store.d.ts +21 -27
  5. package/dest/archiver/archiver_store.d.ts.map +1 -1
  6. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  8. package/dest/archiver/archiver_store_test_suite.js +125 -140
  9. package/dest/archiver/config.d.ts +3 -2
  10. package/dest/archiver/config.d.ts.map +1 -1
  11. package/dest/archiver/config.js +13 -1
  12. package/dest/archiver/errors.d.ts +1 -1
  13. package/dest/archiver/errors.d.ts.map +1 -1
  14. package/dest/archiver/index.d.ts +1 -1
  15. package/dest/archiver/instrumentation.d.ts +5 -3
  16. package/dest/archiver/instrumentation.d.ts.map +1 -1
  17. package/dest/archiver/instrumentation.js +11 -0
  18. package/dest/archiver/kv_archiver_store/block_store.d.ts +10 -9
  19. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  20. package/dest/archiver/kv_archiver_store/block_store.js +9 -8
  21. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +2 -2
  22. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  23. package/dest/archiver/kv_archiver_store/contract_class_store.js +1 -1
  24. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +2 -2
  25. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  26. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +23 -29
  27. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  28. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +7 -15
  29. package/dest/archiver/kv_archiver_store/log_store.d.ts +3 -10
  30. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  31. package/dest/archiver/kv_archiver_store/log_store.js +4 -26
  32. package/dest/archiver/kv_archiver_store/message_store.d.ts +6 -5
  33. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  34. package/dest/archiver/kv_archiver_store/message_store.js +15 -14
  35. package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
  36. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
  37. package/dest/archiver/l1/bin/retrieve-calldata.js +147 -0
  38. package/dest/archiver/l1/calldata_retriever.d.ts +98 -0
  39. package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
  40. package/dest/archiver/l1/calldata_retriever.js +403 -0
  41. package/dest/archiver/l1/data_retrieval.d.ts +87 -0
  42. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
  43. package/dest/archiver/l1/data_retrieval.js +318 -0
  44. package/dest/archiver/l1/debug_tx.d.ts +19 -0
  45. package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
  46. package/dest/archiver/l1/debug_tx.js +73 -0
  47. package/dest/archiver/l1/spire_proposer.d.ts +70 -0
  48. package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
  49. package/dest/archiver/l1/spire_proposer.js +157 -0
  50. package/dest/archiver/l1/trace_tx.d.ts +97 -0
  51. package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
  52. package/dest/archiver/l1/trace_tx.js +91 -0
  53. package/dest/archiver/l1/types.d.ts +12 -0
  54. package/dest/archiver/l1/types.d.ts.map +1 -0
  55. package/dest/archiver/l1/types.js +3 -0
  56. package/dest/archiver/l1/validate_trace.d.ts +29 -0
  57. package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
  58. package/dest/archiver/l1/validate_trace.js +150 -0
  59. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  60. package/dest/archiver/structs/inbox_message.d.ts +4 -4
  61. package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
  62. package/dest/archiver/structs/inbox_message.js +6 -5
  63. package/dest/archiver/structs/published.d.ts +3 -2
  64. package/dest/archiver/structs/published.d.ts.map +1 -1
  65. package/dest/archiver/validation.d.ts +10 -4
  66. package/dest/archiver/validation.d.ts.map +1 -1
  67. package/dest/archiver/validation.js +29 -21
  68. package/dest/factory.d.ts +3 -5
  69. package/dest/factory.d.ts.map +1 -1
  70. package/dest/factory.js +3 -2
  71. package/dest/index.d.ts +2 -2
  72. package/dest/index.d.ts.map +1 -1
  73. package/dest/index.js +1 -1
  74. package/dest/rpc/index.d.ts +2 -2
  75. package/dest/test/index.d.ts +1 -1
  76. package/dest/test/mock_archiver.d.ts +16 -8
  77. package/dest/test/mock_archiver.d.ts.map +1 -1
  78. package/dest/test/mock_archiver.js +19 -14
  79. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  80. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  81. package/dest/test/mock_l1_to_l2_message_source.js +10 -9
  82. package/dest/test/mock_l2_block_source.d.ts +15 -10
  83. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  84. package/dest/test/mock_l2_block_source.js +22 -9
  85. package/dest/test/mock_structs.d.ts +3 -2
  86. package/dest/test/mock_structs.d.ts.map +1 -1
  87. package/dest/test/mock_structs.js +9 -8
  88. package/package.json +18 -17
  89. package/src/archiver/archiver.ts +531 -345
  90. package/src/archiver/archiver_store.ts +24 -27
  91. package/src/archiver/archiver_store_test_suite.ts +151 -128
  92. package/src/archiver/config.ts +13 -7
  93. package/src/archiver/instrumentation.ts +16 -2
  94. package/src/archiver/kv_archiver_store/block_store.ts +18 -17
  95. package/src/archiver/kv_archiver_store/contract_class_store.ts +1 -1
  96. package/src/archiver/kv_archiver_store/contract_instance_store.ts +1 -1
  97. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +23 -32
  98. package/src/archiver/kv_archiver_store/log_store.ts +4 -30
  99. package/src/archiver/kv_archiver_store/message_store.ts +21 -18
  100. package/src/archiver/l1/README.md +98 -0
  101. package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
  102. package/src/archiver/l1/calldata_retriever.ts +531 -0
  103. package/src/archiver/{data_retrieval.ts → l1/data_retrieval.ts} +196 -250
  104. package/src/archiver/l1/debug_tx.ts +99 -0
  105. package/src/archiver/l1/spire_proposer.ts +160 -0
  106. package/src/archiver/l1/trace_tx.ts +128 -0
  107. package/src/archiver/l1/types.ts +13 -0
  108. package/src/archiver/l1/validate_trace.ts +211 -0
  109. package/src/archiver/structs/inbox_message.ts +8 -8
  110. package/src/archiver/structs/published.ts +2 -1
  111. package/src/archiver/validation.ts +52 -27
  112. package/src/factory.ts +4 -5
  113. package/src/index.ts +1 -1
  114. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  115. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  116. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  117. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  118. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  119. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  120. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  121. package/src/test/mock_archiver.ts +22 -16
  122. package/src/test/mock_l1_to_l2_message_source.ts +10 -9
  123. package/src/test/mock_l2_block_source.ts +32 -15
  124. package/src/test/mock_structs.ts +10 -9
  125. package/dest/archiver/data_retrieval.d.ts +0 -79
  126. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  127. package/dest/archiver/data_retrieval.js +0 -362
@@ -4,76 +4,89 @@ function _ts_decorate(decorators, target, key, desc) {
4
4
  else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  }
7
+ import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
7
8
  import { EpochCache } from '@aztec/epoch-cache';
8
- import { BlockTagTooOldError, InboxContract, RollupContract, createEthereumChain } from '@aztec/ethereum';
9
+ import { createEthereumChain } from '@aztec/ethereum/chain';
10
+ import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
9
11
  import { maxBigint } from '@aztec/foundation/bigint';
12
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
10
13
  import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
11
14
  import { merge, pick } from '@aztec/foundation/collection';
12
- import { Fr } from '@aztec/foundation/fields';
15
+ import { Fr } from '@aztec/foundation/curves/bn254';
13
16
  import { createLogger } from '@aztec/foundation/log';
17
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
14
18
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
15
- import { sleep } from '@aztec/foundation/sleep';
16
19
  import { count } from '@aztec/foundation/string';
17
- import { Timer, elapsed } from '@aztec/foundation/timer';
20
+ import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
18
21
  import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
19
22
  import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
20
- import { L2BlockSourceEvents } from '@aztec/stdlib/block';
23
+ import { L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
21
24
  import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
22
25
  import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
23
- import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
26
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
27
+ import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
24
28
  import { EventEmitter } from 'events';
25
29
  import groupBy from 'lodash.groupby';
26
30
  import { createPublicClient, fallback, http } from 'viem';
27
- import { retrieveBlocksFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedBlockToPublishedL2Block } from './data_retrieval.js';
28
31
  import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
29
32
  import { ArchiverInstrumentation } from './instrumentation.js';
30
- import { validateBlockAttestations } from './validation.js';
33
+ import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './l1/data_retrieval.js';
34
+ import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
35
+ import { validateCheckpointAttestations } from './validation.js';
31
36
  function mapArchiverConfig(config) {
32
37
  return {
33
38
  pollingIntervalMs: config.archiverPollingIntervalMS,
34
39
  batchSize: config.archiverBatchSize,
35
- skipValidateBlockAttestations: config.skipValidateBlockAttestations
40
+ skipValidateBlockAttestations: config.skipValidateBlockAttestations,
41
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
42
+ ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts
36
43
  };
37
44
  }
38
45
  /**
39
- * Pulls L2 blocks in a non-blocking manner and provides interface for their retrieval.
46
+ * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
40
47
  * Responsible for handling robust L1 polling so that other components do not need to
41
48
  * concern themselves with it.
42
49
  */ export class Archiver extends EventEmitter {
43
50
  publicClient;
51
+ debugClient;
44
52
  l1Addresses;
45
53
  dataStore;
46
54
  config;
47
55
  blobSinkClient;
48
56
  epochCache;
57
+ dateProvider;
49
58
  instrumentation;
50
59
  l1constants;
51
60
  log;
52
- /**
53
- * A promise in which we will be continually fetching new L2 blocks.
54
- */ runningPromise;
61
+ /** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
55
62
  rollup;
56
63
  inbox;
57
64
  store;
58
65
  l1BlockNumber;
59
66
  l1Timestamp;
60
67
  initialSyncComplete;
68
+ initialSyncPromise;
61
69
  tracer;
62
70
  /**
63
71
  * Creates a new instance of the Archiver.
64
72
  * @param publicClient - A client for interacting with the Ethereum node.
73
+ * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
65
74
  * @param rollupAddress - Ethereum address of the rollup contract.
66
75
  * @param inboxAddress - Ethereum address of the inbox contract.
67
76
  * @param registryAddress - Ethereum address of the registry contract.
68
77
  * @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
69
78
  * @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
70
79
  * @param log - A logger.
71
- */ constructor(publicClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, instrumentation, l1constants, log = createLogger('archiver')){
72
- super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.epochCache = epochCache, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false;
80
+ */ constructor(publicClient, debugClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
81
+ super(), this.publicClient = publicClient, this.debugClient = debugClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false;
73
82
  this.tracer = instrumentation.tracer;
74
83
  this.store = new ArchiverStoreHelper(dataStore);
75
84
  this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
76
85
  this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
86
+ this.initialSyncPromise = promiseWithResolvers();
87
+ // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
88
+ // are done as fast as possible. This then gets updated once the initial sync completes.
89
+ this.runningPromise = new RunningPromise(()=>this.sync(), this.log, this.config.pollingIntervalMs / 10, makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError));
77
90
  }
78
91
  /**
79
92
  * Creates a new instance of the Archiver and blocks until it syncs from chain.
@@ -88,12 +101,20 @@ function mapArchiverConfig(config) {
88
101
  transport: fallback(config.l1RpcUrls.map((url)=>http(url))),
89
102
  pollingInterval: config.viemPollingIntervalMS
90
103
  });
104
+ // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
105
+ const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
106
+ const debugClient = createPublicClient({
107
+ chain: chain.chainInfo,
108
+ transport: fallback(debugRpcUrls.map((url)=>http(url))),
109
+ pollingInterval: config.viemPollingIntervalMS
110
+ });
91
111
  const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
92
- const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
112
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] = await Promise.all([
93
113
  rollup.getL1StartBlock(),
94
114
  rollup.getL1GenesisTime(),
95
115
  rollup.getProofSubmissionEpochs(),
96
- rollup.getGenesisArchiveTreeRoot()
116
+ rollup.getGenesisArchiveTreeRoot(),
117
+ rollup.getSlashingProposerAddress()
97
118
  ]);
98
119
  const l1StartBlockHash = await publicClient.getBlock({
99
120
  blockNumber: l1StartBlock,
@@ -112,11 +133,16 @@ function mapArchiverConfig(config) {
112
133
  };
113
134
  const opts = merge({
114
135
  pollingIntervalMs: 10_000,
115
- batchSize: 100
136
+ batchSize: 100,
137
+ maxAllowedEthClientDriftSeconds: 300,
138
+ ethereumAllowNoDebugHosts: false
116
139
  }, mapArchiverConfig(config));
117
140
  const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
118
141
  const telemetry = deps.telemetry ?? getTelemetryClient();
119
- const archiver = new Archiver(publicClient, config.l1Contracts, archiverStore, opts, deps.blobSinkClient, epochCache, await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
142
+ const archiver = new Archiver(publicClient, debugClient, {
143
+ ...config.l1Contracts,
144
+ slashingProposerAddress
145
+ }, archiverStore, opts, deps.blobSinkClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
120
146
  await archiver.start(blockUntilSynced);
121
147
  return archiver;
122
148
  }
@@ -127,45 +153,49 @@ function mapArchiverConfig(config) {
127
153
  * Starts sync process.
128
154
  * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
129
155
  */ async start(blockUntilSynced) {
130
- if (this.runningPromise) {
156
+ if (this.runningPromise.isRunning()) {
131
157
  throw new Error('Archiver is already running');
132
158
  }
133
159
  await this.blobSinkClient.testSources();
160
+ await this.testEthereumNodeSynced();
161
+ await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
162
+ // Log initial state for the archiver
163
+ const { l1StartBlock } = this.l1constants;
164
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
165
+ const currentL2Block = await this.getBlockNumber();
166
+ this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`, {
167
+ blocksSynchedTo,
168
+ messagesSynchedTo,
169
+ currentL2Block
170
+ });
171
+ // Start sync loop, and return the wait for initial sync if we are asked to block until synced
172
+ this.runningPromise.start();
134
173
  if (blockUntilSynced) {
135
- while(!await this.syncSafe(true)){
136
- this.log.info(`Retrying initial archiver sync in ${this.config.pollingIntervalMs}ms`);
137
- await sleep(this.config.pollingIntervalMs);
138
- }
174
+ return this.waitForInitialSync();
139
175
  }
140
- this.runningPromise = new RunningPromise(()=>this.sync(false), this.log, this.config.pollingIntervalMs, makeLoggingErrorHandler(this.log, // Ignored errors will not log to the console
141
- // We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
142
- NoBlobBodiesFoundError));
143
- this.runningPromise.start();
144
176
  }
145
177
  syncImmediate() {
146
- if (!this.runningPromise) {
147
- throw new Error('Archiver is not running');
148
- }
149
178
  return this.runningPromise.trigger();
150
179
  }
151
- async syncSafe(initialRun) {
152
- try {
153
- await this.sync(initialRun);
154
- return true;
155
- } catch (error) {
156
- if (error instanceof NoBlobBodiesFoundError) {
157
- this.log.error(`Error syncing archiver: ${error.message}`);
158
- } else if (error instanceof BlockTagTooOldError) {
159
- this.log.warn(`Re-running archiver sync: ${error.message}`);
160
- } else {
161
- this.log.error('Error during archiver sync', error);
162
- }
163
- return false;
180
+ waitForInitialSync() {
181
+ return this.initialSyncPromise.promise;
182
+ }
183
+ /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
184
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
185
+ if (maxAllowedDelay === 0) {
186
+ return;
187
+ }
188
+ const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({
189
+ includeTransactions: false
190
+ });
191
+ const currentTime = BigInt(this.dateProvider.nowInSeconds());
192
+ if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
193
+ throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
164
194
  }
165
195
  }
166
196
  /**
167
197
  * Fetches logs from L1 contracts and processes them.
168
- */ async sync(initialRun) {
198
+ */ async sync() {
169
199
  /**
170
200
  * We keep track of three "pointers" to L1 blocks:
171
201
  * 1. the last L1 block that published an L2 block
@@ -175,8 +205,6 @@ function mapArchiverConfig(config) {
175
205
  * We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
176
206
  * We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
177
207
  * The archiver will stay back, until there's data on L1 that will move the pointers forward.
178
- *
179
- * This code does not handle reorgs.
180
208
  */ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
181
209
  const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = {
182
210
  l1BlockNumber: l1StartBlock,
@@ -187,12 +215,12 @@ function mapArchiverConfig(config) {
187
215
  });
188
216
  const currentL1BlockNumber = currentL1Block.number;
189
217
  const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
190
- if (initialRun) {
191
- this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo}` + ` to current L1 block ${currentL1BlockNumber} with hash ${currentL1BlockHash.toString()}`, {
192
- blocksSynchedTo,
193
- messagesSynchedTo
194
- });
195
- }
218
+ this.log.trace(`Starting new archiver sync iteration`, {
219
+ blocksSynchedTo,
220
+ messagesSynchedTo,
221
+ currentL1BlockNumber,
222
+ currentL1BlockHash
223
+ });
196
224
  // ********** Ensuring Consistency of data pulled from L1 **********
197
225
  /**
198
226
  * There are a number of calls in this sync operation to L1 for retrieving
@@ -215,23 +243,40 @@ function mapArchiverConfig(config) {
215
243
  const currentL1Timestamp = !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber ? (await this.publicClient.getBlock({
216
244
  blockNumber: currentL1BlockNumber
217
245
  })).timestamp : this.l1Timestamp;
218
- // ********** Events that are processed per L2 block **********
246
+ // Warn if the latest L1 block timestamp is too old
247
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
248
+ const now = this.dateProvider.nowInSeconds();
249
+ if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
250
+ this.log.warn(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
251
+ currentL1BlockNumber,
252
+ currentL1Timestamp,
253
+ now,
254
+ maxAllowedDelay
255
+ });
256
+ }
257
+ // ********** Events that are processed per checkpoint **********
219
258
  if (currentL1BlockNumber > blocksSynchedTo) {
220
- // First we retrieve new L2 blocks and store them in the DB. This will also update the
221
- // pending chain validation status, proven block number, and synched L1 block number.
222
- const rollupStatus = await this.handleL2blocks(blocksSynchedTo, currentL1BlockNumber);
259
+ // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
260
+ // pending chain validation status, proven checkpoint number, and synched L1 block number.
261
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
223
262
  // Then we prune the current epoch if it'd reorg on next submission.
224
- // Note that we don't do this before retrieving L2 blocks because we may need to retrieve
225
- // blocks from more than 2 epochs ago, so we want to make sure we have the latest view of
263
+ // Note that we don't do this before retrieving checkpoints because we may need to retrieve
264
+ // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
226
265
  // the chain locally before we start unwinding stuff. This can be optimized by figuring out
227
- // up to which point we're pruning, and then requesting L2 blocks up to that point only.
228
- const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenBlockNumber, currentL1BlockNumber, currentL1Timestamp);
229
- // And lastly we check if we are missing any L2 blocks behind us due to a possible L1 reorg.
266
+ // up to which point we're pruning, and then requesting checkpoints up to that point only.
267
+ const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
268
+ // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
269
+ // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
270
+ // we get a valid checkpoint to advance the syncpoint.
271
+ if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
272
+ await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
273
+ }
274
+ // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
230
275
  // We only do this if rollup cant prune on the next submission. Otherwise we will end up
231
- // re-syncing the blocks we have just unwound above. We also dont do this if the last block is invalid,
276
+ // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
232
277
  // since the archiver will rightfully refuse to sync up to it.
233
278
  if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
234
- await this.checkForNewBlocksBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
279
+ await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
235
280
  }
236
281
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
237
282
  }
@@ -240,13 +285,17 @@ function mapArchiverConfig(config) {
240
285
  // but the corresponding blocks have not been processed (see #12631).
241
286
  this.l1Timestamp = currentL1Timestamp;
242
287
  this.l1BlockNumber = currentL1BlockNumber;
243
- this.initialSyncComplete = true;
244
- if (initialRun) {
245
- this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete.`, {
288
+ // We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
289
+ // so if the initial sync took too long, we still go for another iteration.
290
+ if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= await this.publicClient.getBlockNumber()) {
291
+ this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
246
292
  l1BlockNumber: currentL1BlockNumber,
247
293
  syncPoint: await this.store.getSynchPoint(),
248
294
  ...await this.getL2Tips()
249
295
  });
296
+ this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
297
+ this.initialSyncComplete = true;
298
+ this.initialSyncPromise.resolve();
250
299
  }
251
300
  }
252
301
  /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
@@ -263,30 +312,30 @@ function mapArchiverConfig(config) {
263
312
  }
264
313
  return result;
265
314
  }
266
- /** Checks if there'd be a reorg for the next block submission and start pruning now. */ async handleEpochPrune(provenBlockNumber, currentL1BlockNumber, currentL1Timestamp) {
315
+ /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
267
316
  const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
268
- const localPendingBlockNumber = await this.getBlockNumber();
269
- const canPrune = localPendingBlockNumber > provenBlockNumber && rollupCanPrune;
317
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
318
+ const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
270
319
  if (canPrune) {
271
320
  const timer = new Timer();
272
- const pruneFrom = provenBlockNumber + 1;
273
- const header = await this.getBlockHeader(Number(pruneFrom));
321
+ const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
322
+ const header = await this.getCheckpointHeader(pruneFrom);
274
323
  if (header === undefined) {
275
- throw new Error(`Missing block header ${pruneFrom}`);
324
+ throw new Error(`Missing checkpoint header ${pruneFrom}`);
276
325
  }
277
- const pruneFromSlotNumber = header.globalVariables.slotNumber.toBigInt();
326
+ const pruneFromSlotNumber = header.slotNumber;
278
327
  const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
279
- const blocksToUnwind = localPendingBlockNumber - provenBlockNumber;
280
- const blocks = await this.getBlocks(Number(provenBlockNumber) + 1, Number(blocksToUnwind));
328
+ const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
329
+ const checkpoints = await this.getCheckpoints(pruneFrom, checkpointsToUnwind);
281
330
  // Emit an event for listening services to react to the chain prune
282
331
  this.emit(L2BlockSourceEvents.L2PruneDetected, {
283
332
  type: L2BlockSourceEvents.L2PruneDetected,
284
333
  epochNumber: pruneFromEpochNumber,
285
- blocks
334
+ blocks: checkpoints.flatMap((c)=>L2Block.fromCheckpoint(c))
286
335
  });
287
- this.log.debug(`L2 prune from ${provenBlockNumber + 1} to ${localPendingBlockNumber} will occur on next block submission.`);
288
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
289
- this.log.warn(`Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` + `to ${provenBlockNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest block is ${await this.getBlockNumber()}.`);
336
+ this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
337
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
338
+ 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()}.`);
290
339
  this.instrumentation.processPrune(timer.ms());
291
340
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
292
341
  // Seems like the next iteration should handle this.
@@ -329,7 +378,7 @@ function mapArchiverConfig(config) {
329
378
  });
330
379
  // Compare message count and rolling hash. If they match, no need to retrieve anything.
331
380
  if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)) {
332
- this.log.debug(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
381
+ this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
333
382
  return;
334
383
  }
335
384
  // Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
@@ -378,7 +427,7 @@ function mapArchiverConfig(config) {
378
427
  }while (searchEndBlock < currentL1BlockNumber)
379
428
  // Log stats for messages retrieved (if any).
380
429
  if (messageCount > 0) {
381
- this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for L2 block ${lastMessage?.l2BlockNumber}`, {
430
+ this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
382
431
  lastMessage,
383
432
  messageCount
384
433
  });
@@ -455,162 +504,173 @@ function mapArchiverConfig(config) {
455
504
  }
456
505
  return Buffer32.fromString(block.hash);
457
506
  }
458
- async handleL2blocks(blocksSynchedTo, currentL1BlockNumber) {
459
- const localPendingBlockNumber = await this.getBlockNumber();
507
+ async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
508
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
460
509
  const initialValidationResult = await this.store.getPendingChainValidationStatus();
461
- const [provenBlockNumber, provenArchive, pendingBlockNumber, pendingArchive, archiveForLocalPendingBlockNumber] = await this.rollup.status(BigInt(localPendingBlockNumber), {
510
+ const [rollupProvenCheckpointNumber, provenArchive, rollupPendingCheckpointNumber, pendingArchive, archiveForLocalPendingCheckpointNumber] = await this.rollup.status(localPendingCheckpointNumber, {
462
511
  blockNumber: currentL1BlockNumber
463
512
  });
513
+ const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
514
+ const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
464
515
  const rollupStatus = {
465
- provenBlockNumber: Number(provenBlockNumber),
516
+ provenCheckpointNumber,
466
517
  provenArchive,
467
- pendingBlockNumber: Number(pendingBlockNumber),
518
+ pendingCheckpointNumber,
468
519
  pendingArchive,
469
520
  validationResult: initialValidationResult
470
521
  };
471
522
  this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
472
- localPendingBlockNumber,
523
+ localPendingCheckpointNumber,
473
524
  blocksSynchedTo,
474
525
  currentL1BlockNumber,
475
- archiveForLocalPendingBlockNumber,
526
+ archiveForLocalPendingCheckpointNumber,
476
527
  ...rollupStatus
477
528
  });
478
- const updateProvenBlock = async ()=>{
479
- // Annoying edge case: if proven block is moved back to 0 due to a reorg at the beginning of the chain,
480
- // we need to set it to zero. This is an edge case because we dont have a block zero (initial block is one),
481
- // so localBlockForDestinationProvenBlockNumber would not be found below.
482
- if (provenBlockNumber === 0n) {
483
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
484
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
485
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
486
- this.log.info(`Rolled back proven chain to block ${provenBlockNumber}`, {
487
- provenBlockNumber
529
+ const updateProvenCheckpoint = async ()=>{
530
+ // Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
531
+ // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
532
+ // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
533
+ if (provenCheckpointNumber === 0) {
534
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
535
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
536
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
537
+ this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
538
+ provenCheckpointNumber
488
539
  });
489
540
  }
490
541
  }
491
- const localBlockForDestinationProvenBlockNumber = await this.getBlock(Number(provenBlockNumber));
492
- // Sanity check. I've hit what seems to be a state where the proven block is set to a value greater than the latest
493
- // synched block when requesting L2Tips from the archiver. This is the only place where the proven block is set.
494
- const synched = await this.store.getSynchedL2BlockNumber();
495
- if (localBlockForDestinationProvenBlockNumber && synched < localBlockForDestinationProvenBlockNumber?.number) {
496
- this.log.error(`Hit local block greater than last synched block: ${localBlockForDestinationProvenBlockNumber.number} > ${synched}`);
542
+ const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
543
+ // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
544
+ // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
545
+ const synched = await this.getSynchedCheckpointNumber();
546
+ if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.number) {
547
+ this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`);
497
548
  }
498
- this.log.trace(`Local block for remote proven block ${provenBlockNumber} is ${localBlockForDestinationProvenBlockNumber?.archive.root.toString() ?? 'undefined'}`);
499
- if (localBlockForDestinationProvenBlockNumber && provenArchive === localBlockForDestinationProvenBlockNumber.archive.root.toString()) {
500
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
501
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
502
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
503
- this.log.info(`Updated proven chain to block ${provenBlockNumber}`, {
504
- provenBlockNumber
549
+ this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
550
+ const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
551
+ if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
552
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
553
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
554
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
555
+ this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
556
+ provenCheckpointNumber
505
557
  });
506
- const provenSlotNumber = localBlockForDestinationProvenBlockNumber.header.globalVariables.slotNumber.toBigInt();
558
+ const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
507
559
  const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
508
560
  this.emit(L2BlockSourceEvents.L2BlockProven, {
509
561
  type: L2BlockSourceEvents.L2BlockProven,
510
- blockNumber: provenBlockNumber,
562
+ blockNumber: lastProvenBlockNumber,
511
563
  slotNumber: provenSlotNumber,
512
564
  epochNumber: provenEpochNumber
513
565
  });
514
566
  } else {
515
- this.log.trace(`Proven block ${provenBlockNumber} already stored.`);
567
+ this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
516
568
  }
517
569
  }
518
- this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber));
570
+ this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
519
571
  };
520
- // This is an edge case that we only hit if there are no proposed blocks.
521
- // If we have 0 blocks locally and there are no blocks onchain there is nothing to do.
522
- const noBlocks = localPendingBlockNumber === 0 && pendingBlockNumber === 0n;
523
- if (noBlocks) {
572
+ // This is an edge case that we only hit if there are no proposed checkpoints.
573
+ // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
574
+ const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
575
+ if (noCheckpoints) {
524
576
  await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
525
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no blocks on chain`);
577
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
526
578
  return rollupStatus;
527
579
  }
528
- await updateProvenBlock();
580
+ await updateProvenCheckpoint();
529
581
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
530
- // are any state that could be impacted by it. If we have no blocks, there is no impact.
531
- if (localPendingBlockNumber > 0) {
532
- const localPendingBlock = await this.getBlock(localPendingBlockNumber);
533
- if (localPendingBlock === undefined) {
534
- throw new Error(`Missing block ${localPendingBlockNumber}`);
582
+ // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
583
+ if (localPendingCheckpointNumber > 0) {
584
+ const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
585
+ if (localPendingCheckpoint === undefined) {
586
+ throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
535
587
  }
536
- const localPendingArchiveRoot = localPendingBlock.archive.root.toString();
537
- const noBlockSinceLast = localPendingBlock && pendingArchive === localPendingArchiveRoot;
538
- if (noBlockSinceLast) {
588
+ const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
589
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
590
+ if (noCheckpointSinceLast) {
539
591
  // We believe the following line causes a problem when we encounter L1 re-orgs.
540
592
  // Basically, by setting the synched L1 block number here, we are saying that we have
541
- // processed all blocks up to the current L1 block number and we will not attempt to retrieve logs from
593
+ // processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
542
594
  // this block again (or any blocks before).
543
- // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing blocks
595
+ // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
544
596
  // We must only set this block number based on actually retrieved logs.
545
597
  // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
546
598
  // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
547
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
599
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
548
600
  return rollupStatus;
549
601
  }
550
- const localPendingBlockInChain = archiveForLocalPendingBlockNumber === localPendingArchiveRoot;
551
- if (!localPendingBlockInChain) {
552
- // If our local pending block tip is not in the chain on L1 a "prune" must have happened
602
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
603
+ if (!localPendingCheckpointInChain) {
604
+ // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
553
605
  // or the L1 have reorged.
554
606
  // In any case, we have to figure out how far into the past the action will take us.
555
- // For simplicity here, we will simply rewind until we end in a block that is also on the chain on L1.
556
- this.log.debug(`L2 prune has been detected due to local pending block ${localPendingBlockNumber} not in chain`, {
557
- localPendingBlockNumber,
607
+ // For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
608
+ this.log.debug(`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`, {
609
+ localPendingCheckpointNumber,
558
610
  localPendingArchiveRoot,
559
- archiveForLocalPendingBlockNumber
611
+ archiveForLocalPendingCheckpointNumber
560
612
  });
561
- let tipAfterUnwind = localPendingBlockNumber;
613
+ let tipAfterUnwind = localPendingCheckpointNumber;
562
614
  while(true){
563
- const candidateBlock = await this.getBlock(Number(tipAfterUnwind));
564
- if (candidateBlock === undefined) {
615
+ const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
616
+ if (candidateCheckpoint === undefined) {
565
617
  break;
566
618
  }
567
- const archiveAtContract = await this.rollup.archiveAt(BigInt(candidateBlock.number));
568
- if (archiveAtContract === candidateBlock.archive.root.toString()) {
619
+ const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.number);
620
+ this.log.trace(`Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`, {
621
+ archiveAtContract,
622
+ archiveLocal: candidateCheckpoint.archive.root.toString()
623
+ });
624
+ if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
569
625
  break;
570
626
  }
571
627
  tipAfterUnwind--;
572
628
  }
573
- const blocksToUnwind = localPendingBlockNumber - tipAfterUnwind;
574
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
575
- this.log.warn(`Unwound ${count(blocksToUnwind, 'block')} from L2 block ${localPendingBlockNumber} ` + `due to mismatched block hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest block is ${await this.getBlockNumber()}.`);
629
+ const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
630
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
631
+ 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()}.`);
576
632
  }
577
633
  }
578
- // Retrieve L2 blocks in batches. Each batch is estimated to accommodate up to L2 'blockBatchSize' blocks,
634
+ // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
579
635
  // computed using the L2 block time vs the L1 block time.
580
636
  let searchStartBlock = blocksSynchedTo;
581
637
  let searchEndBlock = blocksSynchedTo;
582
- let lastRetrievedBlock;
638
+ let lastRetrievedCheckpoint;
639
+ let lastL1BlockWithCheckpoint = undefined;
583
640
  do {
584
641
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
585
- this.log.trace(`Retrieving L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
642
+ this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
586
643
  // TODO(md): Retrieve from blob sink then from consensus client, then from peers
587
- const retrievedBlocks = await retrieveBlocksFromRollup(this.rollup.getContract(), this.publicClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.log);
588
- if (retrievedBlocks.length === 0) {
644
+ const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.debugClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log);
645
+ if (retrievedCheckpoints.length === 0) {
589
646
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
590
647
  // See further details in earlier comments.
591
- this.log.trace(`Retrieved no new L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
648
+ this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
592
649
  continue;
593
650
  }
594
- const lastProcessedL1BlockNumber = retrievedBlocks[retrievedBlocks.length - 1].l1.blockNumber;
595
- this.log.debug(`Retrieved ${retrievedBlocks.length} new L2 blocks between L1 blocks ${searchStartBlock} and ${searchEndBlock} with last processed L1 block ${lastProcessedL1BlockNumber}.`);
596
- const publishedBlocks = await Promise.all(retrievedBlocks.map((b)=>retrievedBlockToPublishedL2Block(b)));
597
- const validBlocks = [];
598
- for (const block of publishedBlocks){
651
+ this.log.debug(`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
652
+ lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
653
+ searchStartBlock,
654
+ searchEndBlock
655
+ });
656
+ const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
657
+ const validCheckpoints = [];
658
+ for (const published of publishedCheckpoints){
599
659
  const validationResult = this.config.skipValidateBlockAttestations ? {
600
660
  valid: true
601
- } : await validateBlockAttestations(block, this.epochCache, this.l1constants, this.log);
602
- // Only update the validation result if it has changed, so we can keep track of the first invalid block
603
- // in case there is a sequence of more than one invalid block, as we need to invalidate the first one.
604
- // There is an exception though: if an invalid block is invalidated and replaced with another invalid block,
661
+ } : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
662
+ // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
663
+ // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
664
+ // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
605
665
  // we need to update the validation result, since we need to be able to invalidate the new one.
606
- // See test 'chain progresses if an invalid block is invalidated with an invalid one' for more info.
666
+ // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
607
667
  if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber) {
608
668
  rollupStatus.validationResult = validationResult;
609
669
  }
610
670
  if (!validationResult.valid) {
611
- this.log.warn(`Skipping block ${block.block.number} due to invalid attestations`, {
612
- blockHash: block.block.hash(),
613
- l1BlockNumber: block.l1.blockNumber,
671
+ this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
672
+ checkpointHash: published.checkpoint.hash(),
673
+ l1BlockNumber: published.l1.blockNumber,
614
674
  ...pick(validationResult, 'reason')
615
675
  });
616
676
  // Emit event for invalid block detection
@@ -620,22 +680,38 @@ function mapArchiverConfig(config) {
620
680
  });
621
681
  continue;
622
682
  }
623
- validBlocks.push(block);
624
- this.log.debug(`Ingesting new L2 block ${block.block.number} with ${block.block.body.txEffects.length} txs`, {
625
- blockHash: block.block.hash(),
626
- l1BlockNumber: block.l1.blockNumber,
627
- ...block.block.header.globalVariables.toInspect(),
628
- ...block.block.getStats()
683
+ // Check the inHash of the checkpoint against the l1->l2 messages.
684
+ // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
685
+ // checkpoints we just retrieved.
686
+ const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
687
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
688
+ const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
689
+ if (!computedInHash.equals(publishedInHash)) {
690
+ this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
691
+ checkpointHash: published.checkpoint.hash(),
692
+ l1BlockNumber: published.l1.blockNumber,
693
+ computedInHash,
694
+ publishedInHash
695
+ });
696
+ // Throwing an error since this is most likely caused by a bug.
697
+ throw new Error(`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`);
698
+ }
699
+ validCheckpoints.push(published);
700
+ this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
701
+ checkpointHash: published.checkpoint.hash(),
702
+ l1BlockNumber: published.l1.blockNumber,
703
+ ...published.checkpoint.header.toInspect(),
704
+ blocks: published.checkpoint.blocks.map((b)=>b.getStats())
629
705
  });
630
706
  }
631
707
  try {
632
708
  const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
633
- const [processDuration] = await elapsed(()=>this.store.addBlocks(validBlocks, updatedValidationResult));
634
- this.instrumentation.processNewBlocks(processDuration / validBlocks.length, validBlocks.map((b)=>b.block));
709
+ const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
710
+ this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
635
711
  } catch (err) {
636
712
  if (err instanceof InitialBlockNumberNotSequentialError) {
637
713
  const { previousBlockNumber, newBlockNumber } = err;
638
- const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(previousBlockNumber) : undefined;
714
+ const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(BlockNumber(previousBlockNumber)) : undefined;
639
715
  const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
640
716
  await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
641
717
  this.log.warn(`Attempting to insert block ${newBlockNumber} with previous block ${previousBlockNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`, {
@@ -647,58 +723,58 @@ function mapArchiverConfig(config) {
647
723
  }
648
724
  throw err;
649
725
  }
650
- for (const block of validBlocks){
651
- this.log.info(`Downloaded L2 block ${block.block.number}`, {
652
- blockHash: await block.block.hash(),
653
- blockNumber: block.block.number,
654
- txCount: block.block.body.txEffects.length,
655
- globalVariables: block.block.header.globalVariables.toInspect(),
656
- archiveRoot: block.block.archive.root.toString(),
657
- archiveNextLeafIndex: block.block.archive.nextAvailableLeafIndex
726
+ for (const checkpoint of validCheckpoints){
727
+ this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
728
+ checkpointHash: checkpoint.checkpoint.hash(),
729
+ checkpointNumber: checkpoint.checkpoint.number,
730
+ blockCount: checkpoint.checkpoint.blocks.length,
731
+ txCount: checkpoint.checkpoint.blocks.reduce((acc, b)=>acc + b.body.txEffects.length, 0),
732
+ header: checkpoint.checkpoint.header.toInspect(),
733
+ archiveRoot: checkpoint.checkpoint.archive.root.toString(),
734
+ archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex
658
735
  });
659
736
  }
660
- lastRetrievedBlock = validBlocks.at(-1) ?? lastRetrievedBlock;
737
+ lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
738
+ lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
661
739
  }while (searchEndBlock < currentL1BlockNumber)
662
740
  // Important that we update AFTER inserting the blocks.
663
- await updateProvenBlock();
741
+ await updateProvenCheckpoint();
664
742
  return {
665
743
  ...rollupStatus,
666
- lastRetrievedBlock
744
+ lastRetrievedCheckpoint,
745
+ lastL1BlockWithCheckpoint
667
746
  };
668
747
  }
669
- async checkForNewBlocksBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
670
- const { lastRetrievedBlock, pendingBlockNumber } = status;
671
- // Compare the last L2 block we have (either retrieved in this round or loaded from store) with what the
748
+ async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
749
+ const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
750
+ // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
672
751
  // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
673
- const latestLocalL2BlockNumber = lastRetrievedBlock?.block.number ?? await this.store.getSynchedL2BlockNumber();
674
- if (latestLocalL2BlockNumber < pendingBlockNumber) {
752
+ const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.getSynchedCheckpointNumber();
753
+ if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
675
754
  // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
676
- // but still havent reached the pending block according to the call to the rollup contract.
677
- // We suspect an L1 reorg that added blocks *behind* us. If that is the case, it must have happened between the
678
- // last L2 block we saw and the current one, so we reset the last synched L1 block number. In the edge case we
679
- // don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
680
- const latestLocalL2Block = lastRetrievedBlock ?? (latestLocalL2BlockNumber > 0 ? await this.store.getPublishedBlocks(latestLocalL2BlockNumber, 1).then(([b])=>b) : undefined);
681
- const targetL1BlockNumber = latestLocalL2Block?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
682
- const latestLocalL2BlockArchive = latestLocalL2Block?.block.archive.root.toString();
683
- this.log.warn(`Failed to reach L2 block ${pendingBlockNumber} at ${currentL1BlockNumber} (latest is ${latestLocalL2BlockNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
684
- latestLocalL2BlockNumber,
685
- latestLocalL2BlockArchive,
755
+ // but still haven't reached the pending checkpoint according to the call to the rollup contract.
756
+ // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
757
+ // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
758
+ // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
759
+ const latestLocalCheckpoint = lastRetrievedCheckpoint ?? (latestLocalCheckpointNumber > 0 ? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c) : undefined);
760
+ const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
761
+ const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
762
+ this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
763
+ latestLocalCheckpointNumber,
764
+ latestLocalCheckpointArchive,
686
765
  blocksSynchedTo,
687
766
  currentL1BlockNumber,
688
767
  ...status
689
768
  });
690
769
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
691
770
  } else {
692
- this.log.trace(`No new blocks behind L1 sync point to retrieve.`, {
693
- latestLocalL2BlockNumber,
694
- pendingBlockNumber
771
+ this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
772
+ latestLocalCheckpointNumber,
773
+ pendingCheckpointNumber
695
774
  });
696
775
  }
697
776
  }
698
777
  /** Resumes the archiver after a stop. */ resume() {
699
- if (!this.runningPromise) {
700
- throw new Error(`Archiver was never started`);
701
- }
702
778
  if (this.runningPromise.isRunning()) {
703
779
  this.log.warn(`Archiver already running`);
704
780
  }
@@ -710,7 +786,7 @@ function mapArchiverConfig(config) {
710
786
  * @returns A promise signalling completion of the stop process.
711
787
  */ async stop() {
712
788
  this.log.debug('Stopping...');
713
- await this.runningPromise?.stop();
789
+ await this.runningPromise.stop();
714
790
  this.log.info('Stopped.');
715
791
  return Promise.resolve();
716
792
  }
@@ -732,24 +808,16 @@ function mapArchiverConfig(config) {
732
808
  return Promise.resolve(this.l1Addresses.registryAddress);
733
809
  }
734
810
  getL1BlockNumber() {
735
- const l1BlockNumber = this.l1BlockNumber;
736
- if (!l1BlockNumber) {
737
- throw new Error('L1 block number not yet available. Complete an initial sync first.');
738
- }
739
- return l1BlockNumber;
811
+ return this.l1BlockNumber;
740
812
  }
741
813
  getL1Timestamp() {
742
- const l1Timestamp = this.l1Timestamp;
743
- if (!l1Timestamp) {
744
- throw new Error('L1 timestamp not yet available. Complete an initial sync first.');
745
- }
746
- return Promise.resolve(l1Timestamp);
814
+ return Promise.resolve(this.l1Timestamp);
747
815
  }
748
- async getL2SlotNumber() {
749
- return getSlotAtTimestamp(await this.getL1Timestamp(), this.l1constants);
816
+ getL2SlotNumber() {
817
+ return Promise.resolve(this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants));
750
818
  }
751
- async getL2EpochNumber() {
752
- return getEpochNumberAtTimestamp(await this.getL1Timestamp(), this.l1constants);
819
+ getL2EpochNumber() {
820
+ return Promise.resolve(this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants));
753
821
  }
754
822
  async getBlocksForEpoch(epochNumber) {
755
823
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
@@ -757,12 +825,12 @@ function mapArchiverConfig(config) {
757
825
  // Walk the list of blocks backwards and filter by slots matching the requested epoch.
758
826
  // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
759
827
  let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
760
- const slot = (b)=>b.header.globalVariables.slotNumber.toBigInt();
828
+ const slot = (b)=>b.header.globalVariables.slotNumber;
761
829
  while(block && slot(block) >= start){
762
830
  if (slot(block) <= end) {
763
831
  blocks.push(block);
764
832
  }
765
- block = await this.getBlock(block.number - 1);
833
+ block = await this.getBlock(BlockNumber(block.number - 1));
766
834
  }
767
835
  return blocks.reverse();
768
836
  }
@@ -773,19 +841,20 @@ function mapArchiverConfig(config) {
773
841
  // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
774
842
  let number = await this.store.getSynchedL2BlockNumber();
775
843
  let header = await this.getBlockHeader(number);
776
- const slot = (b)=>b.globalVariables.slotNumber.toBigInt();
844
+ const slot = (b)=>b.globalVariables.slotNumber;
777
845
  while(header && slot(header) >= start){
778
846
  if (slot(header) <= end) {
779
847
  blocks.push(header);
780
848
  }
781
- header = await this.getBlockHeader(--number);
849
+ number = BlockNumber(number - 1);
850
+ header = await this.getBlockHeader(number);
782
851
  }
783
852
  return blocks.reverse();
784
853
  }
785
854
  async isEpochComplete(epochNumber) {
786
855
  // The epoch is complete if the current L2 block is the last one in the epoch (or later)
787
856
  const header = await this.getBlockHeader('latest');
788
- const slot = header?.globalVariables.slotNumber.toBigInt();
857
+ const slot = header ? header.globalVariables.slotNumber : undefined;
789
858
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
790
859
  if (slot && slot >= endSlot) {
791
860
  return true;
@@ -809,6 +878,78 @@ function mapArchiverConfig(config) {
809
878
  /** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
810
879
  return this.initialSyncComplete;
811
880
  }
881
+ async getPublishedCheckpoints(from, limit, proven) {
882
+ // TODO: Implement this properly. This only works when we have one block per checkpoint.
883
+ const blocks = await this.getPublishedBlocks(BlockNumber(from), limit, proven);
884
+ return blocks.map((b)=>b.toPublishedCheckpoint());
885
+ }
886
+ async getCheckpointByArchive(archive) {
887
+ // TODO: Implement this properly. This only works when we have one block per checkpoint.
888
+ return (await this.getPublishedBlockByArchive(archive))?.block.toCheckpoint();
889
+ }
890
+ async getCheckpoints(from, limit, proven) {
891
+ const published = await this.getPublishedCheckpoints(from, limit, proven);
892
+ return published.map((p)=>p.checkpoint);
893
+ }
894
+ async getCheckpoint(number) {
895
+ if (number < 0) {
896
+ number = await this.getSynchedCheckpointNumber();
897
+ }
898
+ if (number === 0) {
899
+ return undefined;
900
+ }
901
+ const published = await this.getPublishedCheckpoints(number, 1);
902
+ return published[0]?.checkpoint;
903
+ }
904
+ async getCheckpointHeader(number) {
905
+ if (number === 'latest') {
906
+ number = await this.getSynchedCheckpointNumber();
907
+ }
908
+ if (number === 0) {
909
+ return undefined;
910
+ }
911
+ const checkpoint = await this.getCheckpoint(number);
912
+ return checkpoint?.header;
913
+ }
914
+ getCheckpointNumber() {
915
+ return this.getSynchedCheckpointNumber();
916
+ }
917
+ async getSynchedCheckpointNumber() {
918
+ // TODO: Create store and apis for checkpoints.
919
+ // Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
920
+ return CheckpointNumber(await this.store.getSynchedL2BlockNumber());
921
+ }
922
+ async getProvenCheckpointNumber() {
923
+ // TODO: Create store and apis for checkpoints.
924
+ // Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
925
+ return CheckpointNumber(await this.store.getProvenL2BlockNumber());
926
+ }
927
+ setProvenCheckpointNumber(checkpointNumber) {
928
+ // TODO: Create store and apis for checkpoints.
929
+ // Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
930
+ return this.store.setProvenL2BlockNumber(BlockNumber.fromCheckpointNumber(checkpointNumber));
931
+ }
932
+ unwindCheckpoints(from, checkpointsToUnwind) {
933
+ // TODO: Create store and apis for checkpoints.
934
+ // This only works when we have one block per checkpoint.
935
+ return this.store.unwindBlocks(BlockNumber.fromCheckpointNumber(from), checkpointsToUnwind);
936
+ }
937
+ getLastBlockNumberInCheckpoint(checkpointNumber) {
938
+ // TODO: Create store and apis for checkpoints.
939
+ // Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
940
+ return Promise.resolve(BlockNumber.fromCheckpointNumber(checkpointNumber));
941
+ }
942
+ addCheckpoints(checkpoints, pendingChainValidationStatus) {
943
+ // TODO: Create store and apis for checkpoints.
944
+ // This only works when we have one block per checkpoint.
945
+ return this.store.addBlocks(checkpoints.map((p)=>PublishedL2Block.fromPublishedCheckpoint(p)), pendingChainValidationStatus);
946
+ }
947
+ async getCheckpointsForEpoch(epochNumber) {
948
+ // TODO: Create store and apis for checkpoints.
949
+ // This only works when we have one block per checkpoint.
950
+ const blocks = await this.getBlocksForEpoch(epochNumber);
951
+ return blocks.map((b)=>b.toCheckpoint());
952
+ }
812
953
  /**
813
954
  * Gets up to `limit` amount of L2 blocks starting from `from`.
814
955
  * @param from - Number of the first block to return (inclusive).
@@ -866,14 +1007,6 @@ function mapArchiverConfig(config) {
866
1007
  return this.store.getSettledTxReceipt(txHash);
867
1008
  }
868
1009
  /**
869
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
870
- * @param from - The block number from which to begin retrieving logs.
871
- * @param limit - The maximum number of blocks to retrieve logs from.
872
- * @returns An array of private logs from the specified range of blocks.
873
- */ getPrivateLogs(from, limit) {
874
- return this.store.getPrivateLogs(from, limit);
875
- }
876
- /**
877
1010
  * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
878
1011
  * @param tags - The tags to filter the logs by.
879
1012
  * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
@@ -925,11 +1058,11 @@ function mapArchiverConfig(config) {
925
1058
  return this.store.getContractInstance(address, timestamp);
926
1059
  }
927
1060
  /**
928
- * Gets L1 to L2 message (to be) included in a given block.
929
- * @param blockNumber - L2 block number to get messages for.
1061
+ * Gets L1 to L2 message (to be) included in a given checkpoint.
1062
+ * @param checkpointNumber - Checkpoint number to get messages for.
930
1063
  * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
931
- */ getL1ToL2Messages(blockNumber) {
932
- return this.store.getL1ToL2Messages(blockNumber);
1064
+ */ getL1ToL2Messages(checkpointNumber) {
1065
+ return this.store.getL1ToL2Messages(checkpointNumber);
933
1066
  }
934
1067
  /**
935
1068
  * Gets the L1 to L2 message index in the L1 to L2 message tree.
@@ -963,7 +1096,7 @@ function mapArchiverConfig(config) {
963
1096
  // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
964
1097
  // We just force it 2 epochs worth of proven data for now.
965
1098
  // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
966
- const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
1099
+ const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
967
1100
  const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
968
1101
  latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
969
1102
  provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
@@ -978,21 +1111,21 @@ function mapArchiverConfig(config) {
978
1111
  if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
979
1112
  throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
980
1113
  }
981
- const latestBlockHeaderHash = await latestBlockHeader?.hash();
982
- const provenBlockHeaderHash = await provenBlockHeader?.hash();
983
- const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
1114
+ const latestBlockHeaderHash = await latestBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1115
+ const provenBlockHeaderHash = await provenBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1116
+ const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
984
1117
  return {
985
1118
  latest: {
986
1119
  number: latestBlockNumber,
987
- hash: latestBlockHeaderHash?.toString()
1120
+ hash: latestBlockHeaderHash.toString()
988
1121
  },
989
1122
  proven: {
990
1123
  number: provenBlockNumber,
991
- hash: provenBlockHeaderHash?.toString()
1124
+ hash: provenBlockHeaderHash.toString()
992
1125
  },
993
1126
  finalized: {
994
1127
  number: finalizedBlockNumber,
995
- hash: finalizedBlockHeaderHash?.toString()
1128
+ hash: finalizedBlockHeaderHash.toString()
996
1129
  }
997
1130
  };
998
1131
  }
@@ -1010,11 +1143,12 @@ function mapArchiverConfig(config) {
1010
1143
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1011
1144
  }
1012
1145
  const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1146
+ const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
1013
1147
  const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1014
1148
  this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
1015
- await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
1016
- this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
1017
- await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
1149
+ await this.store.unwindBlocks(BlockNumber(currentL2Block), blocksToUnwind);
1150
+ this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
1151
+ await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1018
1152
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1019
1153
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1020
1154
  await this.store.setMessageSynchedL1Block({
@@ -1023,7 +1157,7 @@ function mapArchiverConfig(config) {
1023
1157
  });
1024
1158
  if (targetL2BlockNumber < currentProvenBlock) {
1025
1159
  this.log.info(`Clearing proven L2 block number`);
1026
- await this.store.setProvenL2BlockNumber(0);
1160
+ await this.store.setProvenL2BlockNumber(BlockNumber.ZERO);
1027
1161
  }
1028
1162
  // TODO(palla/reorg): Set the finalized block when we add support for it.
1029
1163
  // if (targetL2BlockNumber < currentFinalizedBlock) {
@@ -1033,9 +1167,7 @@ function mapArchiverConfig(config) {
1033
1167
  }
1034
1168
  }
1035
1169
  _ts_decorate([
1036
- trackSpan('Archiver.sync', (initialRun)=>({
1037
- [Attributes.INITIAL_SYNC]: initialRun
1038
- }))
1170
+ trackSpan('Archiver.sync')
1039
1171
  ], Archiver.prototype, "sync", null);
1040
1172
  var Operation = /*#__PURE__*/ function(Operation) {
1041
1173
  Operation[Operation["Store"] = 0] = "Store";
@@ -1190,7 +1322,7 @@ var Operation = /*#__PURE__*/ function(Operation) {
1190
1322
  throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
1191
1323
  }
1192
1324
  // from - blocksToUnwind = the new head, so + 1 for what we need to remove
1193
- const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1325
+ const blocks = await this.getPublishedBlocks(BlockNumber(from - blocksToUnwind + 1), blocksToUnwind);
1194
1326
  const opResults = await Promise.all([
1195
1327
  // Prune rolls back to the last proven block, which is by definition valid
1196
1328
  this.store.setPendingChainValidationStatus({
@@ -1243,15 +1375,12 @@ var Operation = /*#__PURE__*/ function(Operation) {
1243
1375
  addL1ToL2Messages(messages) {
1244
1376
  return this.store.addL1ToL2Messages(messages);
1245
1377
  }
1246
- getL1ToL2Messages(blockNumber) {
1247
- return this.store.getL1ToL2Messages(blockNumber);
1378
+ getL1ToL2Messages(checkpointNumber) {
1379
+ return this.store.getL1ToL2Messages(checkpointNumber);
1248
1380
  }
1249
1381
  getL1ToL2MessageIndex(l1ToL2Message) {
1250
1382
  return this.store.getL1ToL2MessageIndex(l1ToL2Message);
1251
1383
  }
1252
- getPrivateLogs(from, limit) {
1253
- return this.store.getPrivateLogs(from, limit);
1254
- }
1255
1384
  getLogsByTags(tags, logsPerTag) {
1256
1385
  return this.store.getLogsByTags(tags, logsPerTag);
1257
1386
  }
@@ -1303,8 +1432,8 @@ var Operation = /*#__PURE__*/ function(Operation) {
1303
1432
  estimateSize() {
1304
1433
  return this.store.estimateSize();
1305
1434
  }
1306
- rollbackL1ToL2MessagesToL2Block(targetBlockNumber) {
1307
- return this.store.rollbackL1ToL2MessagesToL2Block(targetBlockNumber);
1435
+ rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber) {
1436
+ return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1308
1437
  }
1309
1438
  iterateL1ToL2Messages(range = {}) {
1310
1439
  return this.store.iterateL1ToL2Messages(range);