@aztec/archiver 0.0.1-commit.b655e406 → 0.0.1-commit.c7c42ec

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 (132) hide show
  1. package/dest/archiver/archiver.d.ts +110 -83
  2. package/dest/archiver/archiver.d.ts.map +1 -1
  3. package/dest/archiver/archiver.js +672 -349
  4. package/dest/archiver/archiver_store.d.ts +100 -47
  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 +1871 -389
  9. package/dest/archiver/config.d.ts +5 -4
  10. package/dest/archiver/config.d.ts.map +1 -1
  11. package/dest/archiver/config.js +15 -3
  12. package/dest/archiver/errors.d.ts +25 -1
  13. package/dest/archiver/errors.d.ts.map +1 -1
  14. package/dest/archiver/errors.js +37 -0
  15. package/dest/archiver/index.d.ts +2 -2
  16. package/dest/archiver/index.d.ts.map +1 -1
  17. package/dest/archiver/instrumentation.d.ts +5 -3
  18. package/dest/archiver/instrumentation.d.ts.map +1 -1
  19. package/dest/archiver/instrumentation.js +11 -0
  20. package/dest/archiver/kv_archiver_store/block_store.d.ts +51 -18
  21. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  22. package/dest/archiver/kv_archiver_store/block_store.js +324 -87
  23. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +2 -2
  24. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  25. package/dest/archiver/kv_archiver_store/contract_class_store.js +1 -1
  26. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +2 -2
  27. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  28. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +47 -57
  29. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  30. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +65 -48
  31. package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
  32. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  33. package/dest/archiver/kv_archiver_store/log_store.js +149 -84
  34. package/dest/archiver/kv_archiver_store/message_store.d.ts +6 -5
  35. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  36. package/dest/archiver/kv_archiver_store/message_store.js +15 -14
  37. package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
  38. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
  39. package/dest/archiver/l1/bin/retrieve-calldata.js +149 -0
  40. package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
  41. package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
  42. package/dest/archiver/l1/calldata_retriever.js +471 -0
  43. package/dest/archiver/l1/data_retrieval.d.ts +90 -0
  44. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
  45. package/dest/archiver/l1/data_retrieval.js +331 -0
  46. package/dest/archiver/l1/debug_tx.d.ts +19 -0
  47. package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
  48. package/dest/archiver/l1/debug_tx.js +73 -0
  49. package/dest/archiver/l1/spire_proposer.d.ts +70 -0
  50. package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
  51. package/dest/archiver/l1/spire_proposer.js +157 -0
  52. package/dest/archiver/l1/trace_tx.d.ts +97 -0
  53. package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
  54. package/dest/archiver/l1/trace_tx.js +91 -0
  55. package/dest/archiver/l1/types.d.ts +12 -0
  56. package/dest/archiver/l1/types.d.ts.map +1 -0
  57. package/dest/archiver/l1/types.js +3 -0
  58. package/dest/archiver/l1/validate_trace.d.ts +29 -0
  59. package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
  60. package/dest/archiver/l1/validate_trace.js +150 -0
  61. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  62. package/dest/archiver/structs/inbox_message.d.ts +4 -4
  63. package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
  64. package/dest/archiver/structs/inbox_message.js +6 -5
  65. package/dest/archiver/structs/published.d.ts +2 -2
  66. package/dest/archiver/structs/published.d.ts.map +1 -1
  67. package/dest/archiver/validation.d.ts +10 -4
  68. package/dest/archiver/validation.d.ts.map +1 -1
  69. package/dest/archiver/validation.js +29 -21
  70. package/dest/factory.d.ts +2 -2
  71. package/dest/factory.d.ts.map +1 -1
  72. package/dest/factory.js +4 -3
  73. package/dest/index.d.ts +2 -2
  74. package/dest/index.d.ts.map +1 -1
  75. package/dest/index.js +1 -1
  76. package/dest/rpc/index.d.ts +2 -2
  77. package/dest/test/index.d.ts +1 -1
  78. package/dest/test/mock_archiver.d.ts +16 -8
  79. package/dest/test/mock_archiver.d.ts.map +1 -1
  80. package/dest/test/mock_archiver.js +19 -14
  81. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  82. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  83. package/dest/test/mock_l1_to_l2_message_source.js +10 -9
  84. package/dest/test/mock_l2_block_source.d.ts +23 -11
  85. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  86. package/dest/test/mock_l2_block_source.js +38 -24
  87. package/dest/test/mock_structs.d.ts +3 -2
  88. package/dest/test/mock_structs.d.ts.map +1 -1
  89. package/dest/test/mock_structs.js +9 -8
  90. package/package.json +18 -17
  91. package/src/archiver/archiver.ts +884 -449
  92. package/src/archiver/archiver_store.ts +113 -46
  93. package/src/archiver/archiver_store_test_suite.ts +1936 -356
  94. package/src/archiver/config.ts +20 -10
  95. package/src/archiver/errors.ts +64 -0
  96. package/src/archiver/index.ts +1 -1
  97. package/src/archiver/instrumentation.ts +16 -2
  98. package/src/archiver/kv_archiver_store/block_store.ts +442 -101
  99. package/src/archiver/kv_archiver_store/contract_class_store.ts +1 -1
  100. package/src/archiver/kv_archiver_store/contract_instance_store.ts +1 -1
  101. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +87 -71
  102. package/src/archiver/kv_archiver_store/log_store.ts +209 -99
  103. package/src/archiver/kv_archiver_store/message_store.ts +21 -18
  104. package/src/archiver/l1/README.md +98 -0
  105. package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
  106. package/src/archiver/l1/calldata_retriever.ts +641 -0
  107. package/src/archiver/l1/data_retrieval.ts +512 -0
  108. package/src/archiver/l1/debug_tx.ts +99 -0
  109. package/src/archiver/l1/spire_proposer.ts +160 -0
  110. package/src/archiver/l1/trace_tx.ts +128 -0
  111. package/src/archiver/l1/types.ts +13 -0
  112. package/src/archiver/l1/validate_trace.ts +211 -0
  113. package/src/archiver/structs/inbox_message.ts +8 -8
  114. package/src/archiver/structs/published.ts +1 -1
  115. package/src/archiver/validation.ts +52 -27
  116. package/src/factory.ts +4 -3
  117. package/src/index.ts +1 -1
  118. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  119. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  120. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  121. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  122. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  123. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  124. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  125. package/src/test/mock_archiver.ts +22 -16
  126. package/src/test/mock_l1_to_l2_message_source.ts +10 -9
  127. package/src/test/mock_l2_block_source.ts +51 -30
  128. package/src/test/mock_structs.ts +10 -9
  129. package/dest/archiver/data_retrieval.d.ts +0 -79
  130. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  131. package/dest/archiver/data_retrieval.js +0 -362
  132. package/src/archiver/data_retrieval.ts +0 -545
@@ -4,53 +4,63 @@ 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';
14
17
  import { promiseWithResolvers } from '@aztec/foundation/promise';
15
18
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
16
- import { sleep } from '@aztec/foundation/sleep';
17
19
  import { count } from '@aztec/foundation/string';
18
- import { Timer, elapsed } from '@aztec/foundation/timer';
20
+ import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
21
+ import { isDefined } from '@aztec/foundation/types';
19
22
  import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
20
23
  import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
21
- import { L2BlockSourceEvents } from '@aztec/stdlib/block';
24
+ import { CommitteeAttestation, L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
25
+ import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
22
26
  import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
23
27
  import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
24
- import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
28
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
29
+ import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
25
30
  import { EventEmitter } from 'events';
26
31
  import groupBy from 'lodash.groupby';
27
32
  import { createPublicClient, fallback, http } from 'viem';
28
- import { retrieveBlocksFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedBlockToPublishedL2Block } from './data_retrieval.js';
29
- import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
33
+ import { InitialCheckpointNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
30
34
  import { ArchiverInstrumentation } from './instrumentation.js';
31
- import { validateBlockAttestations } from './validation.js';
35
+ import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './l1/data_retrieval.js';
36
+ import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
37
+ import { validateCheckpointAttestations } from './validation.js';
32
38
  function mapArchiverConfig(config) {
33
39
  return {
34
40
  pollingIntervalMs: config.archiverPollingIntervalMS,
35
41
  batchSize: config.archiverBatchSize,
36
- skipValidateBlockAttestations: config.skipValidateBlockAttestations
42
+ skipValidateBlockAttestations: config.skipValidateBlockAttestations,
43
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
44
+ ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts
37
45
  };
38
46
  }
39
47
  /**
40
- * Pulls L2 blocks in a non-blocking manner and provides interface for their retrieval.
48
+ * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
41
49
  * Responsible for handling robust L1 polling so that other components do not need to
42
50
  * concern themselves with it.
43
51
  */ export class Archiver extends EventEmitter {
44
52
  publicClient;
53
+ debugClient;
45
54
  l1Addresses;
46
55
  dataStore;
47
56
  config;
48
- blobSinkClient;
57
+ blobClient;
49
58
  epochCache;
59
+ dateProvider;
50
60
  instrumentation;
51
61
  l1constants;
52
62
  log;
53
- /** A loop in which we will be continually fetching new L2 blocks. */ runningPromise;
63
+ /** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
54
64
  rollup;
55
65
  inbox;
56
66
  store;
@@ -58,23 +68,28 @@ function mapArchiverConfig(config) {
58
68
  l1Timestamp;
59
69
  initialSyncComplete;
60
70
  initialSyncPromise;
71
+ /** Queue of blocks to be added to the store, processed by the sync loop. */ blockQueue;
61
72
  tracer;
62
73
  /**
63
74
  * Creates a new instance of the Archiver.
64
75
  * @param publicClient - A client for interacting with the Ethereum node.
76
+ * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
65
77
  * @param rollupAddress - Ethereum address of the rollup contract.
66
78
  * @param inboxAddress - Ethereum address of the inbox contract.
67
79
  * @param registryAddress - Ethereum address of the registry contract.
68
80
  * @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
69
81
  * @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
70
82
  * @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;
83
+ */ constructor(publicClient, debugClient, l1Addresses, dataStore, config, blobClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
84
+ super(), this.publicClient = publicClient, this.debugClient = debugClient, 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.initialSyncComplete = false, this.blockQueue = [];
73
85
  this.tracer = instrumentation.tracer;
74
86
  this.store = new ArchiverStoreHelper(dataStore);
75
87
  this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
76
88
  this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
77
89
  this.initialSyncPromise = promiseWithResolvers();
90
+ // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
91
+ // are done as fast as possible. This then gets updated once the initial sync completes.
92
+ this.runningPromise = new RunningPromise(()=>this.sync(), this.log, this.config.pollingIntervalMs / 10, makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError));
78
93
  }
79
94
  /**
80
95
  * Creates a new instance of the Archiver and blocks until it syncs from chain.
@@ -86,15 +101,27 @@ function mapArchiverConfig(config) {
86
101
  const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
87
102
  const publicClient = createPublicClient({
88
103
  chain: chain.chainInfo,
89
- transport: fallback(config.l1RpcUrls.map((url)=>http(url))),
104
+ transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
105
+ batch: false
106
+ }))),
107
+ pollingInterval: config.viemPollingIntervalMS
108
+ });
109
+ // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
110
+ const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
111
+ const debugClient = createPublicClient({
112
+ chain: chain.chainInfo,
113
+ transport: fallback(debugRpcUrls.map((url)=>http(url, {
114
+ batch: false
115
+ }))),
90
116
  pollingInterval: config.viemPollingIntervalMS
91
117
  });
92
118
  const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
93
- const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
119
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] = await Promise.all([
94
120
  rollup.getL1StartBlock(),
95
121
  rollup.getL1GenesisTime(),
96
122
  rollup.getProofSubmissionEpochs(),
97
- rollup.getGenesisArchiveTreeRoot()
123
+ rollup.getGenesisArchiveTreeRoot(),
124
+ rollup.getSlashingProposerAddress()
98
125
  ]);
99
126
  const l1StartBlockHash = await publicClient.getBlock({
100
127
  blockNumber: l1StartBlock,
@@ -113,11 +140,16 @@ function mapArchiverConfig(config) {
113
140
  };
114
141
  const opts = merge({
115
142
  pollingIntervalMs: 10_000,
116
- batchSize: 100
143
+ batchSize: 100,
144
+ maxAllowedEthClientDriftSeconds: 300,
145
+ ethereumAllowNoDebugHosts: false
117
146
  }, mapArchiverConfig(config));
118
147
  const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
119
148
  const telemetry = deps.telemetry ?? getTelemetryClient();
120
- const archiver = new Archiver(publicClient, config.l1Contracts, archiverStore, opts, deps.blobSinkClient, epochCache, await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
149
+ const archiver = new Archiver(publicClient, debugClient, {
150
+ ...config.l1Contracts,
151
+ slashingProposerAddress
152
+ }, archiverStore, opts, deps.blobClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
121
153
  await archiver.start(blockUntilSynced);
122
154
  return archiver;
123
155
  }
@@ -128,48 +160,92 @@ function mapArchiverConfig(config) {
128
160
  * Starts sync process.
129
161
  * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
130
162
  */ async start(blockUntilSynced) {
131
- if (this.runningPromise) {
163
+ if (this.runningPromise.isRunning()) {
132
164
  throw new Error('Archiver is already running');
133
165
  }
134
- await this.blobSinkClient.testSources();
166
+ await this.blobClient.testSources();
167
+ await this.testEthereumNodeSynced();
168
+ await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
169
+ // Log initial state for the archiver
170
+ const { l1StartBlock } = this.l1constants;
171
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
172
+ const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
173
+ this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`, {
174
+ blocksSynchedTo,
175
+ messagesSynchedTo,
176
+ currentL2Checkpoint
177
+ });
178
+ // Start sync loop, and return the wait for initial sync if we are asked to block until synced
179
+ this.runningPromise.start();
135
180
  if (blockUntilSynced) {
136
- while(!await this.syncSafe(true)){
137
- this.log.info(`Retrying initial archiver sync in ${this.config.pollingIntervalMs}ms`);
138
- await sleep(this.config.pollingIntervalMs);
139
- }
181
+ return this.waitForInitialSync();
140
182
  }
141
- this.runningPromise = new RunningPromise(()=>this.sync(false), this.log, this.config.pollingIntervalMs, makeLoggingErrorHandler(this.log, // Ignored errors will not log to the console
142
- // We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
143
- NoBlobBodiesFoundError));
144
- this.runningPromise.start();
145
183
  }
146
184
  syncImmediate() {
147
- if (!this.runningPromise) {
148
- throw new Error('Archiver is not running');
149
- }
150
185
  return this.runningPromise.trigger();
151
186
  }
187
+ /**
188
+ * Queues a block to be added to the archiver store and triggers processing.
189
+ * The block will be processed by the sync loop.
190
+ * Implements the L2BlockSink interface.
191
+ * @param block - The L2 block to add.
192
+ * @returns A promise that resolves when the block has been added to the store, or rejects on error.
193
+ */ addBlock(block) {
194
+ return new Promise((resolve, reject)=>{
195
+ this.blockQueue.push({
196
+ block,
197
+ resolve,
198
+ reject
199
+ });
200
+ this.log.debug(`Queued block ${block.number} for processing`);
201
+ // Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
202
+ this.syncImmediate().catch((err)=>{
203
+ this.log.error(`Sync immediate call failed: ${err}`);
204
+ });
205
+ });
206
+ }
207
+ /**
208
+ * Processes all queued blocks, adding them to the store.
209
+ * Called at the beginning of each sync iteration.
210
+ * Blocks are processed in the order they were queued.
211
+ */ async processQueuedBlocks() {
212
+ if (this.blockQueue.length === 0) {
213
+ return;
214
+ }
215
+ // Take all blocks from the queue
216
+ const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
217
+ this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
218
+ // Process each block individually to properly resolve/reject each promise
219
+ for (const { block, resolve, reject } of queuedItems){
220
+ try {
221
+ await this.store.addBlocks([
222
+ block
223
+ ]);
224
+ this.log.debug(`Added block ${block.number} to store`);
225
+ resolve();
226
+ } catch (err) {
227
+ this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
228
+ reject(err);
229
+ }
230
+ }
231
+ }
152
232
  waitForInitialSync() {
153
233
  return this.initialSyncPromise.promise;
154
234
  }
155
- async syncSafe(initialRun) {
156
- try {
157
- await this.sync(initialRun);
158
- return true;
159
- } catch (error) {
160
- if (error instanceof NoBlobBodiesFoundError) {
161
- this.log.error(`Error syncing archiver: ${error.message}`);
162
- } else if (error instanceof BlockTagTooOldError) {
163
- this.log.warn(`Re-running archiver sync: ${error.message}`);
164
- } else {
165
- this.log.error('Error during archiver sync', error);
166
- }
167
- return false;
235
+ /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
236
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
237
+ if (maxAllowedDelay === 0) {
238
+ return;
239
+ }
240
+ const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({
241
+ includeTransactions: false
242
+ });
243
+ const currentTime = BigInt(this.dateProvider.nowInSeconds());
244
+ if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
245
+ throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
168
246
  }
169
247
  }
170
- /**
171
- * Fetches logs from L1 contracts and processes them.
172
- */ async sync(initialRun) {
248
+ async syncFromL1() {
173
249
  /**
174
250
  * We keep track of three "pointers" to L1 blocks:
175
251
  * 1. the last L1 block that published an L2 block
@@ -179,8 +255,6 @@ function mapArchiverConfig(config) {
179
255
  * We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
180
256
  * We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
181
257
  * The archiver will stay back, until there's data on L1 that will move the pointers forward.
182
- *
183
- * This code does not handle reorgs.
184
258
  */ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
185
259
  const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = {
186
260
  l1BlockNumber: l1StartBlock,
@@ -191,12 +265,12 @@ function mapArchiverConfig(config) {
191
265
  });
192
266
  const currentL1BlockNumber = currentL1Block.number;
193
267
  const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
194
- if (initialRun) {
195
- 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()}`, {
196
- blocksSynchedTo,
197
- messagesSynchedTo
198
- });
199
- }
268
+ this.log.trace(`Starting new archiver sync iteration`, {
269
+ blocksSynchedTo,
270
+ messagesSynchedTo,
271
+ currentL1BlockNumber,
272
+ currentL1BlockHash
273
+ });
200
274
  // ********** Ensuring Consistency of data pulled from L1 **********
201
275
  /**
202
276
  * There are a number of calls in this sync operation to L1 for retrieving
@@ -219,23 +293,40 @@ function mapArchiverConfig(config) {
219
293
  const currentL1Timestamp = !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber ? (await this.publicClient.getBlock({
220
294
  blockNumber: currentL1BlockNumber
221
295
  })).timestamp : this.l1Timestamp;
222
- // ********** Events that are processed per L2 block **********
296
+ // Warn if the latest L1 block timestamp is too old
297
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
298
+ const now = this.dateProvider.nowInSeconds();
299
+ if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
300
+ this.log.warn(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
301
+ currentL1BlockNumber,
302
+ currentL1Timestamp,
303
+ now,
304
+ maxAllowedDelay
305
+ });
306
+ }
307
+ // ********** Events that are processed per checkpoint **********
223
308
  if (currentL1BlockNumber > blocksSynchedTo) {
224
- // First we retrieve new L2 blocks and store them in the DB. This will also update the
225
- // pending chain validation status, proven block number, and synched L1 block number.
226
- const rollupStatus = await this.handleL2blocks(blocksSynchedTo, currentL1BlockNumber);
309
+ // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
310
+ // pending chain validation status, proven checkpoint number, and synched L1 block number.
311
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
227
312
  // Then we prune the current epoch if it'd reorg on next submission.
228
- // Note that we don't do this before retrieving L2 blocks because we may need to retrieve
229
- // blocks from more than 2 epochs ago, so we want to make sure we have the latest view of
313
+ // Note that we don't do this before retrieving checkpoints because we may need to retrieve
314
+ // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
230
315
  // the chain locally before we start unwinding stuff. This can be optimized by figuring out
231
- // up to which point we're pruning, and then requesting L2 blocks up to that point only.
232
- const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenBlockNumber, currentL1BlockNumber, currentL1Timestamp);
233
- // And lastly we check if we are missing any L2 blocks behind us due to a possible L1 reorg.
316
+ // up to which point we're pruning, and then requesting checkpoints up to that point only.
317
+ const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
318
+ // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
319
+ // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
320
+ // we get a valid checkpoint to advance the syncpoint.
321
+ if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
322
+ await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
323
+ }
324
+ // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
234
325
  // We only do this if rollup cant prune on the next submission. Otherwise we will end up
235
- // re-syncing the blocks we have just unwound above. We also dont do this if the last block is invalid,
326
+ // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
236
327
  // since the archiver will rightfully refuse to sync up to it.
237
328
  if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
238
- await this.checkForNewBlocksBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
329
+ await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
239
330
  }
240
331
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
241
332
  }
@@ -244,16 +335,27 @@ function mapArchiverConfig(config) {
244
335
  // but the corresponding blocks have not been processed (see #12631).
245
336
  this.l1Timestamp = currentL1Timestamp;
246
337
  this.l1BlockNumber = currentL1BlockNumber;
247
- this.initialSyncComplete = true;
248
- this.initialSyncPromise.resolve();
249
- if (initialRun) {
250
- this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete.`, {
338
+ // We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
339
+ // so if the initial sync took too long, we still go for another iteration.
340
+ if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= await this.publicClient.getBlockNumber()) {
341
+ this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
251
342
  l1BlockNumber: currentL1BlockNumber,
252
343
  syncPoint: await this.store.getSynchPoint(),
253
344
  ...await this.getL2Tips()
254
345
  });
346
+ this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
347
+ this.initialSyncComplete = true;
348
+ this.initialSyncPromise.resolve();
255
349
  }
256
350
  }
351
+ /**
352
+ * Fetches logs from L1 contracts and processes them.
353
+ */ async sync() {
354
+ // Process any queued blocks first, before doing L1 sync
355
+ await this.processQueuedBlocks();
356
+ // Now perform L1 sync
357
+ await this.syncFromL1();
358
+ }
257
359
  /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
258
360
  const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
259
361
  const result = await this.rollup.canPruneAtTime(time, {
@@ -268,30 +370,37 @@ function mapArchiverConfig(config) {
268
370
  }
269
371
  return result;
270
372
  }
271
- /** Checks if there'd be a reorg for the next block submission and start pruning now. */ async handleEpochPrune(provenBlockNumber, currentL1BlockNumber, currentL1Timestamp) {
373
+ /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
272
374
  const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
273
- const localPendingBlockNumber = await this.getBlockNumber();
274
- const canPrune = localPendingBlockNumber > provenBlockNumber && rollupCanPrune;
375
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
376
+ const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
275
377
  if (canPrune) {
276
378
  const timer = new Timer();
277
- const pruneFrom = provenBlockNumber + 1;
278
- const header = await this.getBlockHeader(Number(pruneFrom));
379
+ const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
380
+ const header = await this.getCheckpointHeader(pruneFrom);
279
381
  if (header === undefined) {
280
- throw new Error(`Missing block header ${pruneFrom}`);
382
+ throw new Error(`Missing checkpoint header ${pruneFrom}`);
281
383
  }
282
- const pruneFromSlotNumber = header.globalVariables.slotNumber.toBigInt();
384
+ const pruneFromSlotNumber = header.slotNumber;
283
385
  const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
284
- const blocksToUnwind = localPendingBlockNumber - provenBlockNumber;
285
- const blocks = await this.getBlocks(Number(provenBlockNumber) + 1, Number(blocksToUnwind));
386
+ const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
387
+ const checkpointPromises = Array.from({
388
+ length: checkpointsToUnwind
389
+ }).fill(0).map((_, i)=>this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
390
+ const checkpoints = await Promise.all(checkpointPromises);
391
+ const blockPromises = await Promise.all(checkpoints.filter(isDefined).map((cp)=>this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))));
392
+ const newBlocks = blockPromises.filter(isDefined).flat();
393
+ // TODO(pw/mbps): Don't convert to legacy blocks here
394
+ const blocks = (await Promise.all(newBlocks.map((x)=>this.getBlock(x.number)))).filter(isDefined);
286
395
  // Emit an event for listening services to react to the chain prune
287
396
  this.emit(L2BlockSourceEvents.L2PruneDetected, {
288
397
  type: L2BlockSourceEvents.L2PruneDetected,
289
398
  epochNumber: pruneFromEpochNumber,
290
399
  blocks
291
400
  });
292
- this.log.debug(`L2 prune from ${provenBlockNumber + 1} to ${localPendingBlockNumber} will occur on next block submission.`);
293
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
294
- 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()}.`);
401
+ this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
402
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
403
+ 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()}.`);
295
404
  this.instrumentation.processPrune(timer.ms());
296
405
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
297
406
  // Seems like the next iteration should handle this.
@@ -334,7 +443,7 @@ function mapArchiverConfig(config) {
334
443
  });
335
444
  // Compare message count and rolling hash. If they match, no need to retrieve anything.
336
445
  if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)) {
337
- this.log.debug(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
446
+ this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
338
447
  return;
339
448
  }
340
449
  // Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
@@ -383,7 +492,7 @@ function mapArchiverConfig(config) {
383
492
  }while (searchEndBlock < currentL1BlockNumber)
384
493
  // Log stats for messages retrieved (if any).
385
494
  if (messageCount > 0) {
386
- this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for L2 block ${lastMessage?.l2BlockNumber}`, {
495
+ this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
387
496
  lastMessage,
388
497
  messageCount
389
498
  });
@@ -460,162 +569,173 @@ function mapArchiverConfig(config) {
460
569
  }
461
570
  return Buffer32.fromString(block.hash);
462
571
  }
463
- async handleL2blocks(blocksSynchedTo, currentL1BlockNumber) {
464
- const localPendingBlockNumber = await this.getBlockNumber();
572
+ async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
573
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
465
574
  const initialValidationResult = await this.store.getPendingChainValidationStatus();
466
- const [provenBlockNumber, provenArchive, pendingBlockNumber, pendingArchive, archiveForLocalPendingBlockNumber] = await this.rollup.status(BigInt(localPendingBlockNumber), {
575
+ const [rollupProvenCheckpointNumber, provenArchive, rollupPendingCheckpointNumber, pendingArchive, archiveForLocalPendingCheckpointNumber] = await this.rollup.status(localPendingCheckpointNumber, {
467
576
  blockNumber: currentL1BlockNumber
468
577
  });
578
+ const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
579
+ const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
469
580
  const rollupStatus = {
470
- provenBlockNumber: Number(provenBlockNumber),
581
+ provenCheckpointNumber,
471
582
  provenArchive,
472
- pendingBlockNumber: Number(pendingBlockNumber),
583
+ pendingCheckpointNumber,
473
584
  pendingArchive,
474
585
  validationResult: initialValidationResult
475
586
  };
476
587
  this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
477
- localPendingBlockNumber,
588
+ localPendingCheckpointNumber,
478
589
  blocksSynchedTo,
479
590
  currentL1BlockNumber,
480
- archiveForLocalPendingBlockNumber,
591
+ archiveForLocalPendingCheckpointNumber,
481
592
  ...rollupStatus
482
593
  });
483
- const updateProvenBlock = async ()=>{
484
- // Annoying edge case: if proven block is moved back to 0 due to a reorg at the beginning of the chain,
485
- // we need to set it to zero. This is an edge case because we dont have a block zero (initial block is one),
486
- // so localBlockForDestinationProvenBlockNumber would not be found below.
487
- if (provenBlockNumber === 0n) {
488
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
489
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
490
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
491
- this.log.info(`Rolled back proven chain to block ${provenBlockNumber}`, {
492
- provenBlockNumber
594
+ const updateProvenCheckpoint = async ()=>{
595
+ // Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
596
+ // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
597
+ // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
598
+ if (provenCheckpointNumber === 0) {
599
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
600
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
601
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
602
+ this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
603
+ provenCheckpointNumber
493
604
  });
494
605
  }
495
606
  }
496
- const localBlockForDestinationProvenBlockNumber = await this.getBlock(Number(provenBlockNumber));
497
- // Sanity check. I've hit what seems to be a state where the proven block is set to a value greater than the latest
498
- // synched block when requesting L2Tips from the archiver. This is the only place where the proven block is set.
499
- const synched = await this.store.getSynchedL2BlockNumber();
500
- if (localBlockForDestinationProvenBlockNumber && synched < localBlockForDestinationProvenBlockNumber?.number) {
501
- this.log.error(`Hit local block greater than last synched block: ${localBlockForDestinationProvenBlockNumber.number} > ${synched}`);
607
+ const localCheckpointForDestinationProvenCheckpointNumber = await this.store.getCheckpointData(provenCheckpointNumber);
608
+ // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
609
+ // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
610
+ const synched = await this.getSynchedCheckpointNumber();
611
+ if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
612
+ this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
502
613
  }
503
- this.log.trace(`Local block for remote proven block ${provenBlockNumber} is ${localBlockForDestinationProvenBlockNumber?.archive.root.toString() ?? 'undefined'}`);
504
- if (localBlockForDestinationProvenBlockNumber && provenArchive === localBlockForDestinationProvenBlockNumber.archive.root.toString()) {
505
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
506
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
507
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
508
- this.log.info(`Updated proven chain to block ${provenBlockNumber}`, {
509
- provenBlockNumber
614
+ this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
615
+ if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
616
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
617
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
618
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
619
+ this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
620
+ provenCheckpointNumber
510
621
  });
511
- const provenSlotNumber = localBlockForDestinationProvenBlockNumber.header.globalVariables.slotNumber.toBigInt();
622
+ const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
512
623
  const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
624
+ const lastBlockNumberInCheckpoint = localCheckpointForDestinationProvenCheckpointNumber.startBlock + localCheckpointForDestinationProvenCheckpointNumber.numBlocks - 1;
513
625
  this.emit(L2BlockSourceEvents.L2BlockProven, {
514
626
  type: L2BlockSourceEvents.L2BlockProven,
515
- blockNumber: provenBlockNumber,
627
+ blockNumber: BlockNumber(lastBlockNumberInCheckpoint),
516
628
  slotNumber: provenSlotNumber,
517
629
  epochNumber: provenEpochNumber
518
630
  });
631
+ this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
519
632
  } else {
520
- this.log.trace(`Proven block ${provenBlockNumber} already stored.`);
633
+ this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
521
634
  }
522
635
  }
523
- this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber));
524
636
  };
525
- // This is an edge case that we only hit if there are no proposed blocks.
526
- // If we have 0 blocks locally and there are no blocks onchain there is nothing to do.
527
- const noBlocks = localPendingBlockNumber === 0 && pendingBlockNumber === 0n;
528
- if (noBlocks) {
529
- await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
530
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no blocks on chain`);
637
+ // This is an edge case that we only hit if there are no proposed checkpoints.
638
+ // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
639
+ const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
640
+ if (noCheckpoints) {
641
+ await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
642
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
531
643
  return rollupStatus;
532
644
  }
533
- await updateProvenBlock();
645
+ await updateProvenCheckpoint();
534
646
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
535
- // are any state that could be impacted by it. If we have no blocks, there is no impact.
536
- if (localPendingBlockNumber > 0) {
537
- const localPendingBlock = await this.getBlock(localPendingBlockNumber);
538
- if (localPendingBlock === undefined) {
539
- throw new Error(`Missing block ${localPendingBlockNumber}`);
647
+ // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
648
+ if (localPendingCheckpointNumber > 0) {
649
+ const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
650
+ if (localPendingCheckpoint === undefined) {
651
+ throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
540
652
  }
541
- const localPendingArchiveRoot = localPendingBlock.archive.root.toString();
542
- const noBlockSinceLast = localPendingBlock && pendingArchive === localPendingArchiveRoot;
543
- if (noBlockSinceLast) {
653
+ const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
654
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
655
+ if (noCheckpointSinceLast) {
544
656
  // We believe the following line causes a problem when we encounter L1 re-orgs.
545
657
  // Basically, by setting the synched L1 block number here, we are saying that we have
546
- // processed all blocks up to the current L1 block number and we will not attempt to retrieve logs from
658
+ // processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
547
659
  // this block again (or any blocks before).
548
- // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing blocks
660
+ // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
549
661
  // We must only set this block number based on actually retrieved logs.
550
662
  // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
551
663
  // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
552
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
664
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
553
665
  return rollupStatus;
554
666
  }
555
- const localPendingBlockInChain = archiveForLocalPendingBlockNumber === localPendingArchiveRoot;
556
- if (!localPendingBlockInChain) {
557
- // If our local pending block tip is not in the chain on L1 a "prune" must have happened
667
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
668
+ if (!localPendingCheckpointInChain) {
669
+ // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
558
670
  // or the L1 have reorged.
559
671
  // In any case, we have to figure out how far into the past the action will take us.
560
- // For simplicity here, we will simply rewind until we end in a block that is also on the chain on L1.
561
- this.log.debug(`L2 prune has been detected due to local pending block ${localPendingBlockNumber} not in chain`, {
562
- localPendingBlockNumber,
672
+ // For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
673
+ this.log.debug(`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`, {
674
+ localPendingCheckpointNumber,
563
675
  localPendingArchiveRoot,
564
- archiveForLocalPendingBlockNumber
676
+ archiveForLocalPendingCheckpointNumber
565
677
  });
566
- let tipAfterUnwind = localPendingBlockNumber;
678
+ let tipAfterUnwind = localPendingCheckpointNumber;
567
679
  while(true){
568
- const candidateBlock = await this.getBlock(Number(tipAfterUnwind));
569
- if (candidateBlock === undefined) {
680
+ const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
681
+ if (candidateCheckpoint === undefined) {
570
682
  break;
571
683
  }
572
- const archiveAtContract = await this.rollup.archiveAt(BigInt(candidateBlock.number));
573
- if (archiveAtContract === candidateBlock.archive.root.toString()) {
684
+ const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.checkpointNumber);
685
+ this.log.trace(`Checking local checkpoint ${candidateCheckpoint.checkpointNumber} with archive ${candidateCheckpoint.archive.root}`, {
686
+ archiveAtContract,
687
+ archiveLocal: candidateCheckpoint.archive.root.toString()
688
+ });
689
+ if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
574
690
  break;
575
691
  }
576
692
  tipAfterUnwind--;
577
693
  }
578
- const blocksToUnwind = localPendingBlockNumber - tipAfterUnwind;
579
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
580
- 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()}.`);
694
+ const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
695
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
696
+ 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()}.`);
581
697
  }
582
698
  }
583
- // Retrieve L2 blocks in batches. Each batch is estimated to accommodate up to L2 'blockBatchSize' blocks,
699
+ // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
584
700
  // computed using the L2 block time vs the L1 block time.
585
701
  let searchStartBlock = blocksSynchedTo;
586
702
  let searchEndBlock = blocksSynchedTo;
587
- let lastRetrievedBlock;
703
+ let lastRetrievedCheckpoint;
704
+ let lastL1BlockWithCheckpoint = undefined;
588
705
  do {
589
706
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
590
- this.log.trace(`Retrieving L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
591
- // TODO(md): Retrieve from blob sink then from consensus client, then from peers
592
- const retrievedBlocks = await retrieveBlocksFromRollup(this.rollup.getContract(), this.publicClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.log);
593
- if (retrievedBlocks.length === 0) {
707
+ this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
708
+ // TODO(md): Retrieve from blob client then from consensus client, then from peers
709
+ const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.debugClient, this.blobClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log, !this.initialSyncComplete);
710
+ if (retrievedCheckpoints.length === 0) {
594
711
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
595
712
  // See further details in earlier comments.
596
- this.log.trace(`Retrieved no new L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
713
+ this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
597
714
  continue;
598
715
  }
599
- const lastProcessedL1BlockNumber = retrievedBlocks[retrievedBlocks.length - 1].l1.blockNumber;
600
- this.log.debug(`Retrieved ${retrievedBlocks.length} new L2 blocks between L1 blocks ${searchStartBlock} and ${searchEndBlock} with last processed L1 block ${lastProcessedL1BlockNumber}.`);
601
- const publishedBlocks = await Promise.all(retrievedBlocks.map((b)=>retrievedBlockToPublishedL2Block(b)));
602
- const validBlocks = [];
603
- for (const block of publishedBlocks){
716
+ this.log.debug(`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
717
+ lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
718
+ searchStartBlock,
719
+ searchEndBlock
720
+ });
721
+ const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
722
+ const validCheckpoints = [];
723
+ for (const published of publishedCheckpoints){
604
724
  const validationResult = this.config.skipValidateBlockAttestations ? {
605
725
  valid: true
606
- } : await validateBlockAttestations(block, this.epochCache, this.l1constants, this.log);
607
- // Only update the validation result if it has changed, so we can keep track of the first invalid block
608
- // in case there is a sequence of more than one invalid block, as we need to invalidate the first one.
609
- // There is an exception though: if an invalid block is invalidated and replaced with another invalid block,
726
+ } : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
727
+ // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
728
+ // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
729
+ // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
610
730
  // we need to update the validation result, since we need to be able to invalidate the new one.
611
- // See test 'chain progresses if an invalid block is invalidated with an invalid one' for more info.
731
+ // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
612
732
  if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber) {
613
733
  rollupStatus.validationResult = validationResult;
614
734
  }
615
735
  if (!validationResult.valid) {
616
- this.log.warn(`Skipping block ${block.block.number} due to invalid attestations`, {
617
- blockHash: block.block.hash(),
618
- l1BlockNumber: block.l1.blockNumber,
736
+ this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
737
+ checkpointHash: published.checkpoint.hash(),
738
+ l1BlockNumber: published.l1.blockNumber,
619
739
  ...pick(validationResult, 'reason')
620
740
  });
621
741
  // Emit event for invalid block detection
@@ -625,85 +745,107 @@ function mapArchiverConfig(config) {
625
745
  });
626
746
  continue;
627
747
  }
628
- validBlocks.push(block);
629
- this.log.debug(`Ingesting new L2 block ${block.block.number} with ${block.block.body.txEffects.length} txs`, {
630
- blockHash: block.block.hash(),
631
- l1BlockNumber: block.l1.blockNumber,
632
- ...block.block.header.globalVariables.toInspect(),
633
- ...block.block.getStats()
748
+ // Check the inHash of the checkpoint against the l1->l2 messages.
749
+ // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
750
+ // checkpoints we just retrieved.
751
+ const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
752
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
753
+ const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
754
+ if (!computedInHash.equals(publishedInHash)) {
755
+ this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
756
+ checkpointHash: published.checkpoint.hash(),
757
+ l1BlockNumber: published.l1.blockNumber,
758
+ computedInHash,
759
+ publishedInHash
760
+ });
761
+ // Throwing an error since this is most likely caused by a bug.
762
+ throw new Error(`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`);
763
+ }
764
+ validCheckpoints.push(published);
765
+ this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
766
+ checkpointHash: published.checkpoint.hash(),
767
+ l1BlockNumber: published.l1.blockNumber,
768
+ ...published.checkpoint.header.toInspect(),
769
+ blocks: published.checkpoint.blocks.map((b)=>b.getStats())
634
770
  });
635
771
  }
636
772
  try {
637
773
  const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
638
- const [processDuration] = await elapsed(()=>this.store.addBlocks(validBlocks, updatedValidationResult));
639
- this.instrumentation.processNewBlocks(processDuration / validBlocks.length, validBlocks.map((b)=>b.block));
774
+ const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
775
+ this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
640
776
  } catch (err) {
641
- if (err instanceof InitialBlockNumberNotSequentialError) {
642
- const { previousBlockNumber, newBlockNumber } = err;
643
- const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(previousBlockNumber) : undefined;
644
- const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
777
+ if (err instanceof InitialCheckpointNumberNotSequentialError) {
778
+ const { previousCheckpointNumber, newCheckpointNumber } = err;
779
+ const previousCheckpoint = previousCheckpointNumber ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber)) : undefined;
780
+ const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1constants.l1StartBlock;
645
781
  await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
646
- 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
- previousBlockNumber,
648
- previousBlockHash: await previousBlock?.block.hash(),
649
- newBlockNumber,
782
+ this.log.warn(`Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`, {
783
+ previousCheckpointNumber,
784
+ newCheckpointNumber,
650
785
  updatedL1SyncPoint
651
786
  });
652
787
  }
653
788
  throw err;
654
789
  }
655
- for (const block of validBlocks){
656
- this.log.info(`Downloaded L2 block ${block.block.number}`, {
657
- blockHash: await block.block.hash(),
658
- blockNumber: block.block.number,
659
- txCount: block.block.body.txEffects.length,
660
- globalVariables: block.block.header.globalVariables.toInspect(),
661
- archiveRoot: block.block.archive.root.toString(),
662
- archiveNextLeafIndex: block.block.archive.nextAvailableLeafIndex
790
+ for (const checkpoint of validCheckpoints){
791
+ this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
792
+ checkpointHash: checkpoint.checkpoint.hash(),
793
+ checkpointNumber: checkpoint.checkpoint.number,
794
+ blockCount: checkpoint.checkpoint.blocks.length,
795
+ txCount: checkpoint.checkpoint.blocks.reduce((acc, b)=>acc + b.body.txEffects.length, 0),
796
+ header: checkpoint.checkpoint.header.toInspect(),
797
+ archiveRoot: checkpoint.checkpoint.archive.root.toString(),
798
+ archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex
663
799
  });
664
800
  }
665
- lastRetrievedBlock = validBlocks.at(-1) ?? lastRetrievedBlock;
801
+ lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
802
+ lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
666
803
  }while (searchEndBlock < currentL1BlockNumber)
667
804
  // Important that we update AFTER inserting the blocks.
668
- await updateProvenBlock();
805
+ await updateProvenCheckpoint();
669
806
  return {
670
807
  ...rollupStatus,
671
- lastRetrievedBlock
808
+ lastRetrievedCheckpoint,
809
+ lastL1BlockWithCheckpoint
672
810
  };
673
811
  }
674
- async checkForNewBlocksBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
675
- const { lastRetrievedBlock, pendingBlockNumber } = status;
676
- // Compare the last L2 block we have (either retrieved in this round or loaded from store) with what the
812
+ async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
813
+ const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
814
+ // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
677
815
  // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
678
- const latestLocalL2BlockNumber = lastRetrievedBlock?.block.number ?? await this.store.getSynchedL2BlockNumber();
679
- if (latestLocalL2BlockNumber < pendingBlockNumber) {
816
+ const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.getSynchedCheckpointNumber();
817
+ if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
680
818
  // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
681
- // but still havent reached the pending block according to the call to the rollup contract.
682
- // We suspect an L1 reorg that added blocks *behind* us. If that is the case, it must have happened between the
683
- // last L2 block we saw and the current one, so we reset the last synched L1 block number. In the edge case we
684
- // don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
685
- const latestLocalL2Block = lastRetrievedBlock ?? (latestLocalL2BlockNumber > 0 ? await this.store.getPublishedBlocks(latestLocalL2BlockNumber, 1).then(([b])=>b) : undefined);
686
- const targetL1BlockNumber = latestLocalL2Block?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
687
- const latestLocalL2BlockArchive = latestLocalL2Block?.block.archive.root.toString();
688
- this.log.warn(`Failed to reach L2 block ${pendingBlockNumber} at ${currentL1BlockNumber} (latest is ${latestLocalL2BlockNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
689
- latestLocalL2BlockNumber,
690
- latestLocalL2BlockArchive,
819
+ // but still haven't reached the pending checkpoint according to the call to the rollup contract.
820
+ // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
821
+ // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
822
+ // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
823
+ let latestLocalCheckpointArchive = undefined;
824
+ let targetL1BlockNumber = maxBigint(currentL1BlockNumber - 64n, 0n);
825
+ if (lastRetrievedCheckpoint) {
826
+ latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
827
+ targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
828
+ } else if (latestLocalCheckpointNumber > 0) {
829
+ const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c);
830
+ latestLocalCheckpointArchive = checkpoint.archive.root.toString();
831
+ targetL1BlockNumber = checkpoint.l1.blockNumber;
832
+ }
833
+ this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
834
+ latestLocalCheckpointNumber,
835
+ latestLocalCheckpointArchive,
691
836
  blocksSynchedTo,
692
837
  currentL1BlockNumber,
693
838
  ...status
694
839
  });
695
840
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
696
841
  } else {
697
- this.log.trace(`No new blocks behind L1 sync point to retrieve.`, {
698
- latestLocalL2BlockNumber,
699
- pendingBlockNumber
842
+ this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
843
+ latestLocalCheckpointNumber,
844
+ pendingCheckpointNumber
700
845
  });
701
846
  }
702
847
  }
703
848
  /** Resumes the archiver after a stop. */ resume() {
704
- if (!this.runningPromise) {
705
- throw new Error(`Archiver was never started`);
706
- }
707
849
  if (this.runningPromise.isRunning()) {
708
850
  this.log.warn(`Archiver already running`);
709
851
  }
@@ -715,7 +857,7 @@ function mapArchiverConfig(config) {
715
857
  * @returns A promise signalling completion of the stop process.
716
858
  */ async stop() {
717
859
  this.log.debug('Stopping...');
718
- await this.runningPromise?.stop();
860
+ await this.runningPromise.stop();
719
861
  this.log.info('Stopped.');
720
862
  return Promise.resolve();
721
863
  }
@@ -751,38 +893,51 @@ function mapArchiverConfig(config) {
751
893
  async getBlocksForEpoch(epochNumber) {
752
894
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
753
895
  const blocks = [];
754
- // Walk the list of blocks backwards and filter by slots matching the requested epoch.
755
- // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
756
- let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
757
- const slot = (b)=>b.header.globalVariables.slotNumber.toBigInt();
758
- while(block && slot(block) >= start){
759
- if (slot(block) <= end) {
760
- blocks.push(block);
896
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
897
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
898
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
899
+ const slot = (b)=>b.header.slotNumber;
900
+ while(checkpoint && slot(checkpoint) >= start){
901
+ if (slot(checkpoint) <= end) {
902
+ // push the blocks on backwards
903
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
904
+ for(let i = endBlock; i >= checkpoint.startBlock; i--){
905
+ const block = await this.getBlock(BlockNumber(i));
906
+ if (block) {
907
+ blocks.push(block);
908
+ }
909
+ }
761
910
  }
762
- block = await this.getBlock(block.number - 1);
911
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
763
912
  }
764
913
  return blocks.reverse();
765
914
  }
766
915
  async getBlockHeadersForEpoch(epochNumber) {
767
916
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
768
917
  const blocks = [];
769
- // Walk the list of blocks backwards and filter by slots matching the requested epoch.
770
- // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
771
- let number = await this.store.getSynchedL2BlockNumber();
772
- let header = await this.getBlockHeader(number);
773
- const slot = (b)=>b.globalVariables.slotNumber.toBigInt();
774
- while(header && slot(header) >= start){
775
- if (slot(header) <= end) {
776
- blocks.push(header);
918
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
919
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
920
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
921
+ const slot = (b)=>b.header.slotNumber;
922
+ while(checkpoint && slot(checkpoint) >= start){
923
+ if (slot(checkpoint) <= end) {
924
+ // push the blocks on backwards
925
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
926
+ for(let i = endBlock; i >= checkpoint.startBlock; i--){
927
+ const block = await this.getBlockHeader(BlockNumber(i));
928
+ if (block) {
929
+ blocks.push(block);
930
+ }
931
+ }
777
932
  }
778
- header = await this.getBlockHeader(--number);
933
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
779
934
  }
780
935
  return blocks.reverse();
781
936
  }
782
937
  async isEpochComplete(epochNumber) {
783
938
  // The epoch is complete if the current L2 block is the last one in the epoch (or later)
784
939
  const header = await this.getBlockHeader('latest');
785
- const slot = header?.globalVariables.slotNumber.toBigInt();
940
+ const slot = header ? header.globalVariables.slotNumber : undefined;
786
941
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
787
942
  if (slot && slot >= endSlot) {
788
943
  return true;
@@ -806,24 +961,43 @@ function mapArchiverConfig(config) {
806
961
  /** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
807
962
  return this.initialSyncComplete;
808
963
  }
809
- /**
810
- * Gets up to `limit` amount of L2 blocks starting from `from`.
811
- * @param from - Number of the first block to return (inclusive).
812
- * @param limit - The number of blocks to return.
813
- * @param proven - If true, only return blocks that have been proven.
814
- * @returns The requested L2 blocks.
815
- */ getBlocks(from, limit, proven) {
816
- return this.getPublishedBlocks(from, limit, proven).then((blocks)=>blocks.map((b)=>b.block));
964
+ async getCheckpointHeader(number) {
965
+ if (number === 'latest') {
966
+ number = await this.getSynchedCheckpointNumber();
967
+ }
968
+ if (number === 0) {
969
+ return undefined;
970
+ }
971
+ const checkpoint = await this.store.getCheckpointData(number);
972
+ if (!checkpoint) {
973
+ return undefined;
974
+ }
975
+ return checkpoint.header;
976
+ }
977
+ getCheckpointNumber() {
978
+ return this.getSynchedCheckpointNumber();
979
+ }
980
+ getSynchedCheckpointNumber() {
981
+ return this.store.getSynchedCheckpointNumber();
817
982
  }
818
- /** Equivalent to getBlocks but includes publish data. */ async getPublishedBlocks(from, limit, proven) {
819
- const limitWithProven = proven ? Math.min(limit, Math.max(await this.store.getProvenL2BlockNumber() - from + 1, 0)) : limit;
820
- return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
983
+ getProvenCheckpointNumber() {
984
+ return this.store.getProvenCheckpointNumber();
821
985
  }
822
- getPublishedBlockByHash(blockHash) {
823
- return this.store.getPublishedBlockByHash(blockHash);
986
+ setProvenCheckpointNumber(checkpointNumber) {
987
+ return this.store.setProvenCheckpointNumber(checkpointNumber);
824
988
  }
825
- getPublishedBlockByArchive(archive) {
826
- return this.store.getPublishedBlockByArchive(archive);
989
+ unwindCheckpoints(from, checkpointsToUnwind) {
990
+ return this.store.unwindCheckpoints(from, checkpointsToUnwind);
991
+ }
992
+ async getLastBlockNumberInCheckpoint(checkpointNumber) {
993
+ const checkpointData = await this.store.getCheckpointData(checkpointNumber);
994
+ if (!checkpointData) {
995
+ return undefined;
996
+ }
997
+ return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
998
+ }
999
+ addCheckpoints(checkpoints, pendingChainValidationStatus) {
1000
+ return this.store.addCheckpoints(checkpoints, pendingChainValidationStatus);
827
1001
  }
828
1002
  getBlockHeaderByHash(blockHash) {
829
1003
  return this.store.getBlockHeaderByHash(blockHash);
@@ -835,7 +1009,7 @@ function mapArchiverConfig(config) {
835
1009
  * Gets an l2 block.
836
1010
  * @param number - The block number to return.
837
1011
  * @returns The requested L2 block.
838
- */ async getBlock(number) {
1012
+ */ async getL2BlockNew(number) {
839
1013
  // If the number provided is -ve, then return the latest block.
840
1014
  if (number < 0) {
841
1015
  number = await this.store.getSynchedL2BlockNumber();
@@ -843,8 +1017,8 @@ function mapArchiverConfig(config) {
843
1017
  if (number === 0) {
844
1018
  return undefined;
845
1019
  }
846
- const publishedBlock = await this.store.getPublishedBlock(number);
847
- return publishedBlock?.block;
1020
+ const publishedBlock = await this.store.store.getBlock(number);
1021
+ return publishedBlock;
848
1022
  }
849
1023
  async getBlockHeader(number) {
850
1024
  if (number === 'latest') {
@@ -856,27 +1030,29 @@ function mapArchiverConfig(config) {
856
1030
  const headers = await this.store.getBlockHeaders(number, 1);
857
1031
  return headers.length === 0 ? undefined : headers[0];
858
1032
  }
1033
+ getCheckpointedBlock(number) {
1034
+ return this.store.getCheckpointedBlock(number);
1035
+ }
1036
+ getCheckpointedBlockByHash(blockHash) {
1037
+ return this.store.getCheckpointedBlockByHash(blockHash);
1038
+ }
1039
+ getProvenBlockNumber() {
1040
+ return this.store.getProvenBlockNumber();
1041
+ }
1042
+ getCheckpointedBlockByArchive(archive) {
1043
+ return this.store.getCheckpointedBlockByArchive(archive);
1044
+ }
859
1045
  getTxEffect(txHash) {
860
1046
  return this.store.getTxEffect(txHash);
861
1047
  }
862
1048
  getSettledTxReceipt(txHash) {
863
1049
  return this.store.getSettledTxReceipt(txHash);
864
1050
  }
865
- /**
866
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
867
- * @param from - The block number from which to begin retrieving logs.
868
- * @param limit - The maximum number of blocks to retrieve logs from.
869
- * @returns An array of private logs from the specified range of blocks.
870
- */ getPrivateLogs(from, limit) {
871
- return this.store.getPrivateLogs(from, limit);
1051
+ getPrivateLogsByTags(tags) {
1052
+ return this.store.getPrivateLogsByTags(tags);
872
1053
  }
873
- /**
874
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
875
- * @param tags - The tags to filter the logs by.
876
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
877
- * that tag.
878
- */ getLogsByTags(tags) {
879
- return this.store.getLogsByTags(tags);
1054
+ getPublicLogsByTagsFromContract(contractAddress, tags) {
1055
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
880
1056
  }
881
1057
  /**
882
1058
  * Gets public logs based on the provided filter.
@@ -894,15 +1070,10 @@ function mapArchiverConfig(config) {
894
1070
  }
895
1071
  /**
896
1072
  * Gets the number of the latest L2 block processed by the block source implementation.
1073
+ * This includes both checkpointed and uncheckpointed blocks.
897
1074
  * @returns The number of the latest L2 block processed by the block source implementation.
898
1075
  */ getBlockNumber() {
899
- return this.store.getSynchedL2BlockNumber();
900
- }
901
- getProvenBlockNumber() {
902
- return this.store.getProvenL2BlockNumber();
903
- }
904
- /** Forcefully updates the last proven block number. Use for testing. */ setProvenBlockNumber(blockNumber) {
905
- return this.store.setProvenL2BlockNumber(blockNumber);
1076
+ return this.store.getLatestBlockNumber();
906
1077
  }
907
1078
  getContractClass(id) {
908
1079
  return this.store.getContractClass(id);
@@ -922,11 +1093,11 @@ function mapArchiverConfig(config) {
922
1093
  return this.store.getContractInstance(address, timestamp);
923
1094
  }
924
1095
  /**
925
- * Gets L1 to L2 message (to be) included in a given block.
926
- * @param blockNumber - L2 block number to get messages for.
1096
+ * Gets L1 to L2 message (to be) included in a given checkpoint.
1097
+ * @param checkpointNumber - Checkpoint number to get messages for.
927
1098
  * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
928
- */ getL1ToL2Messages(blockNumber) {
929
- return this.store.getL1ToL2Messages(blockNumber);
1099
+ */ getL1ToL2Messages(checkpointNumber) {
1100
+ return this.store.getL1ToL2Messages(checkpointNumber);
930
1101
  }
931
1102
  /**
932
1103
  * Gets the L1 to L2 message index in the L1 to L2 message tree.
@@ -960,7 +1131,7 @@ function mapArchiverConfig(config) {
960
1131
  // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
961
1132
  // We just force it 2 epochs worth of proven data for now.
962
1133
  // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
963
- const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
1134
+ const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
964
1135
  const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
965
1136
  latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
966
1137
  provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
@@ -975,43 +1146,44 @@ function mapArchiverConfig(config) {
975
1146
  if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
976
1147
  throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
977
1148
  }
978
- const latestBlockHeaderHash = await latestBlockHeader?.hash();
979
- const provenBlockHeaderHash = await provenBlockHeader?.hash();
980
- const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
1149
+ const latestBlockHeaderHash = await latestBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1150
+ const provenBlockHeaderHash = await provenBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1151
+ const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
981
1152
  return {
982
1153
  latest: {
983
1154
  number: latestBlockNumber,
984
- hash: latestBlockHeaderHash?.toString()
1155
+ hash: latestBlockHeaderHash.toString()
985
1156
  },
986
1157
  proven: {
987
1158
  number: provenBlockNumber,
988
- hash: provenBlockHeaderHash?.toString()
1159
+ hash: provenBlockHeaderHash.toString()
989
1160
  },
990
1161
  finalized: {
991
1162
  number: finalizedBlockNumber,
992
- hash: finalizedBlockHeaderHash?.toString()
1163
+ hash: finalizedBlockHeaderHash.toString()
993
1164
  }
994
1165
  };
995
1166
  }
996
1167
  async rollbackTo(targetL2BlockNumber) {
1168
+ // TODO(pw/mbps): This still assumes 1 block per checkpoint
997
1169
  const currentBlocks = await this.getL2Tips();
998
1170
  const currentL2Block = currentBlocks.latest.number;
999
1171
  const currentProvenBlock = currentBlocks.proven.number;
1000
- // const currentFinalizedBlock = currentBlocks.finalized.number;
1001
1172
  if (targetL2BlockNumber >= currentL2Block) {
1002
1173
  throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
1003
1174
  }
1004
1175
  const blocksToUnwind = currentL2Block - targetL2BlockNumber;
1005
- const targetL2Block = await this.store.getPublishedBlock(targetL2BlockNumber);
1176
+ const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
1006
1177
  if (!targetL2Block) {
1007
1178
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1008
1179
  }
1009
1180
  const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1181
+ const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
1010
1182
  const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1011
- this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
1012
- await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
1013
- this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
1014
- await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
1183
+ this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
1184
+ await this.store.unwindCheckpoints(CheckpointNumber(currentL2Block), blocksToUnwind);
1185
+ this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
1186
+ await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1015
1187
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1016
1188
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1017
1189
  await this.store.setMessageSynchedL1Block({
@@ -1020,7 +1192,7 @@ function mapArchiverConfig(config) {
1020
1192
  });
1021
1193
  if (targetL2BlockNumber < currentProvenBlock) {
1022
1194
  this.log.info(`Clearing proven L2 block number`);
1023
- await this.store.setProvenL2BlockNumber(0);
1195
+ await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
1024
1196
  }
1025
1197
  // TODO(palla/reorg): Set the finalized block when we add support for it.
1026
1198
  // if (targetL2BlockNumber < currentFinalizedBlock) {
@@ -1028,11 +1200,106 @@ function mapArchiverConfig(config) {
1028
1200
  // await this.store.setFinalizedL2BlockNumber(0);
1029
1201
  // }
1030
1202
  }
1203
+ async getPublishedCheckpoints(checkpointNumber, limit) {
1204
+ const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
1205
+ const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
1206
+ const fullCheckpoints = [];
1207
+ for(let i = 0; i < checkpoints.length; i++){
1208
+ const blocksForCheckpoint = blocks[i];
1209
+ const checkpoint = checkpoints[i];
1210
+ const fullCheckpoint = new Checkpoint(checkpoint.archive, checkpoint.header, blocksForCheckpoint, checkpoint.checkpointNumber);
1211
+ const publishedCheckpoint = new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
1212
+ fullCheckpoints.push(publishedCheckpoint);
1213
+ }
1214
+ return fullCheckpoints;
1215
+ }
1216
+ async getCheckpointsForEpoch(epochNumber) {
1217
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1218
+ const checkpoints = [];
1219
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1220
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1221
+ let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1222
+ const slot = (b)=>b.header.slotNumber;
1223
+ while(checkpointData && slot(checkpointData) >= start){
1224
+ if (slot(checkpointData) <= end) {
1225
+ // push the checkpoints on backwards
1226
+ const [checkpoint] = await this.getPublishedCheckpoints(checkpointData.checkpointNumber, 1);
1227
+ checkpoints.push(checkpoint.checkpoint);
1228
+ }
1229
+ checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
1230
+ }
1231
+ return checkpoints.reverse();
1232
+ }
1233
+ /* Legacy APIs */ async getPublishedBlockByHash(blockHash) {
1234
+ const checkpointedBlock = await this.store.getCheckpointedBlockByHash(blockHash);
1235
+ return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
1236
+ }
1237
+ async getPublishedBlockByArchive(archive) {
1238
+ const checkpointedBlock = await this.store.getCheckpointedBlockByArchive(archive);
1239
+ return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
1240
+ }
1241
+ /**
1242
+ * Gets up to `limit` amount of L2 blocks starting from `from`.
1243
+ * @param from - Number of the first block to return (inclusive).
1244
+ * @param limit - The number of blocks to return.
1245
+ * @param proven - If true, only return blocks that have been proven.
1246
+ * @returns The requested L2 blocks.
1247
+ */ async getBlocks(from, limit, proven) {
1248
+ const publishedBlocks = await this.getPublishedBlocks(from, limit, proven);
1249
+ return publishedBlocks.map((x)=>x.block);
1250
+ }
1251
+ async getPublishedBlocks(from, limit, proven) {
1252
+ const checkpoints = await this.store.getRangeOfCheckpoints(CheckpointNumber(from), limit);
1253
+ const provenCheckpointNumber = await this.getProvenCheckpointNumber();
1254
+ const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
1255
+ const olbBlocks = [];
1256
+ for(let i = 0; i < checkpoints.length; i++){
1257
+ const blockForCheckpoint = blocks[i][0];
1258
+ const checkpoint = checkpoints[i];
1259
+ if (checkpoint.checkpointNumber > provenCheckpointNumber && proven === true) {
1260
+ continue;
1261
+ }
1262
+ const oldCheckpoint = new Checkpoint(blockForCheckpoint.archive, checkpoint.header, [
1263
+ blockForCheckpoint
1264
+ ], checkpoint.checkpointNumber);
1265
+ const oldBlock = L2Block.fromCheckpoint(oldCheckpoint);
1266
+ const publishedBlock = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
1267
+ olbBlocks.push(publishedBlock);
1268
+ }
1269
+ return olbBlocks;
1270
+ }
1271
+ async buildOldBlockFromCheckpointedBlock(checkpointedBlock) {
1272
+ if (!checkpointedBlock) {
1273
+ return undefined;
1274
+ }
1275
+ const checkpoint = await this.store.getCheckpointData(checkpointedBlock.checkpointNumber);
1276
+ if (!checkpoint) {
1277
+ return checkpoint;
1278
+ }
1279
+ const fullCheckpoint = new Checkpoint(checkpointedBlock?.block.archive, checkpoint?.header, [
1280
+ checkpointedBlock.block
1281
+ ], checkpoint.checkpointNumber);
1282
+ const oldBlock = L2Block.fromCheckpoint(fullCheckpoint);
1283
+ const published = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
1284
+ return published;
1285
+ }
1286
+ async getBlock(number) {
1287
+ // If the number provided is -ve, then return the latest block.
1288
+ if (number < 0) {
1289
+ number = await this.store.getSynchedL2BlockNumber();
1290
+ }
1291
+ if (number === 0) {
1292
+ return undefined;
1293
+ }
1294
+ const publishedBlocks = await this.getPublishedBlocks(number, 1);
1295
+ if (publishedBlocks.length === 0) {
1296
+ return undefined;
1297
+ }
1298
+ return publishedBlocks[0].block;
1299
+ }
1031
1300
  }
1032
1301
  _ts_decorate([
1033
- trackSpan('Archiver.sync', (initialRun)=>({
1034
- [Attributes.INITIAL_SYNC]: initialRun
1035
- }))
1302
+ trackSpan('Archiver.sync')
1036
1303
  ], Archiver.prototype, "sync", null);
1037
1304
  var Operation = /*#__PURE__*/ function(Operation) {
1038
1305
  Operation[Operation["Store"] = 0] = "Store";
@@ -1151,6 +1418,18 @@ var Operation = /*#__PURE__*/ function(Operation) {
1151
1418
  }
1152
1419
  return true;
1153
1420
  }
1421
+ async addBlockDataToDB(block) {
1422
+ const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1423
+ // ContractInstancePublished event logs are broadcast in privateLogs.
1424
+ const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1425
+ const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1426
+ return (await Promise.all([
1427
+ this.#updatePublishedContractClasses(contractClassLogs, block.number, 0),
1428
+ this.#updateDeployedContractInstances(privateLogs, block.number, 0),
1429
+ this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 0),
1430
+ this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
1431
+ ])).every(Boolean);
1432
+ }
1154
1433
  addBlocks(blocks, pendingChainValidationStatus) {
1155
1434
  // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1156
1435
  // or if the previous block is not in the store.
@@ -1160,34 +1439,51 @@ var Operation = /*#__PURE__*/ function(Operation) {
1160
1439
  // Update the pending chain validation status if provided
1161
1440
  pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1162
1441
  // Add any logs emitted during the retrieved blocks
1163
- this.store.addLogs(blocks.map((block)=>block.block)),
1442
+ this.store.addLogs(blocks),
1443
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1444
+ ...blocks.map((block)=>{
1445
+ return this.addBlockDataToDB(block);
1446
+ })
1447
+ ]);
1448
+ return opResults.every(Boolean);
1449
+ });
1450
+ }
1451
+ addCheckpoints(checkpoints, pendingChainValidationStatus) {
1452
+ // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1453
+ // or if the previous block is not in the store.
1454
+ return this.store.transactionAsync(async ()=>{
1455
+ await this.store.addCheckpoints(checkpoints);
1456
+ const allBlocks = checkpoints.flatMap((ch)=>ch.checkpoint.blocks);
1457
+ const opResults = await Promise.all([
1458
+ // Update the pending chain validation status if provided
1459
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1460
+ // Add any logs emitted during the retrieved blocks
1461
+ this.store.addLogs(allBlocks),
1164
1462
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1165
- ...blocks.map(async (block)=>{
1166
- const contractClassLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1167
- // ContractInstancePublished event logs are broadcast in privateLogs.
1168
- const privateLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1169
- const publicLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1170
- return (await Promise.all([
1171
- this.#updatePublishedContractClasses(contractClassLogs, block.block.number, 0),
1172
- this.#updateDeployedContractInstances(privateLogs, block.block.number, 0),
1173
- this.#updateUpdatedContractInstances(publicLogs, block.block.header.globalVariables.timestamp, 0),
1174
- this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.block.number)
1175
- ])).every(Boolean);
1463
+ ...allBlocks.map((block)=>{
1464
+ return this.addBlockDataToDB(block);
1176
1465
  })
1177
1466
  ]);
1178
1467
  return opResults.every(Boolean);
1179
1468
  });
1180
1469
  }
1181
- async unwindBlocks(from, blocksToUnwind) {
1182
- const last = await this.getSynchedL2BlockNumber();
1470
+ async unwindCheckpoints(from, checkpointsToUnwind) {
1471
+ if (checkpointsToUnwind <= 0) {
1472
+ throw new Error(`Cannot unwind ${checkpointsToUnwind} blocks`);
1473
+ }
1474
+ const last = await this.getSynchedCheckpointNumber();
1183
1475
  if (from != last) {
1184
- throw new Error(`Cannot unwind blocks from block ${from} when the last block is ${last}`);
1476
+ throw new Error(`Cannot unwind checkpoints from checkpoint ${from} when the last checkpoint is ${last}`);
1185
1477
  }
1186
- if (blocksToUnwind <= 0) {
1187
- throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
1478
+ const blocks = [];
1479
+ const lastCheckpointNumber = from + checkpointsToUnwind - 1;
1480
+ for(let checkpointNumber = from; checkpointNumber <= lastCheckpointNumber; checkpointNumber++){
1481
+ const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpointNumber);
1482
+ if (!blocksForCheckpoint) {
1483
+ continue;
1484
+ }
1485
+ blocks.push(...blocksForCheckpoint);
1188
1486
  }
1189
- // from - blocksToUnwind = the new head, so + 1 for what we need to remove
1190
- const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1191
1487
  const opResults = await Promise.all([
1192
1488
  // Prune rolls back to the last proven block, which is by definition valid
1193
1489
  this.store.setPendingChainValidationStatus({
@@ -1195,32 +1491,44 @@ var Operation = /*#__PURE__*/ function(Operation) {
1195
1491
  }),
1196
1492
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1197
1493
  ...blocks.map(async (block)=>{
1198
- const contractClassLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1494
+ const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1199
1495
  // ContractInstancePublished event logs are broadcast in privateLogs.
1200
- const privateLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1201
- const publicLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1496
+ const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1497
+ const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1202
1498
  return (await Promise.all([
1203
- this.#updatePublishedContractClasses(contractClassLogs, block.block.number, 1),
1204
- this.#updateDeployedContractInstances(privateLogs, block.block.number, 1),
1205
- this.#updateUpdatedContractInstances(publicLogs, block.block.header.globalVariables.timestamp, 1)
1499
+ this.#updatePublishedContractClasses(contractClassLogs, block.number, 1),
1500
+ this.#updateDeployedContractInstances(privateLogs, block.number, 1),
1501
+ this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 1)
1206
1502
  ])).every(Boolean);
1207
1503
  }),
1208
- this.store.deleteLogs(blocks.map((b)=>b.block)),
1209
- this.store.unwindBlocks(from, blocksToUnwind)
1504
+ this.store.deleteLogs(blocks),
1505
+ this.store.unwindCheckpoints(from, checkpointsToUnwind)
1210
1506
  ]);
1211
1507
  return opResults.every(Boolean);
1212
1508
  }
1213
- getPublishedBlocks(from, limit) {
1214
- return this.store.getPublishedBlocks(from, limit);
1509
+ getCheckpointData(checkpointNumber) {
1510
+ return this.store.getCheckpointData(checkpointNumber);
1511
+ }
1512
+ getRangeOfCheckpoints(from, limit) {
1513
+ return this.store.getRangeOfCheckpoints(from, limit);
1215
1514
  }
1216
- getPublishedBlock(number) {
1217
- return this.store.getPublishedBlock(number);
1515
+ getCheckpointedL2BlockNumber() {
1516
+ return this.store.getCheckpointedL2BlockNumber();
1218
1517
  }
1219
- getPublishedBlockByHash(blockHash) {
1220
- return this.store.getPublishedBlockByHash(blockHash);
1518
+ getSynchedCheckpointNumber() {
1519
+ return this.store.getSynchedCheckpointNumber();
1221
1520
  }
1222
- getPublishedBlockByArchive(archive) {
1223
- return this.store.getPublishedBlockByArchive(archive);
1521
+ setCheckpointSynchedL1BlockNumber(l1BlockNumber) {
1522
+ return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
1523
+ }
1524
+ getCheckpointedBlock(number) {
1525
+ return this.store.getCheckpointedBlock(number);
1526
+ }
1527
+ getCheckpointedBlockByHash(blockHash) {
1528
+ return this.store.getCheckpointedBlockByHash(blockHash);
1529
+ }
1530
+ getCheckpointedBlockByArchive(archive) {
1531
+ return this.store.getCheckpointedBlockByArchive(archive);
1224
1532
  }
1225
1533
  getBlockHeaders(from, limit) {
1226
1534
  return this.store.getBlockHeaders(from, limit);
@@ -1231,6 +1539,18 @@ var Operation = /*#__PURE__*/ function(Operation) {
1231
1539
  getBlockHeaderByArchive(archive) {
1232
1540
  return this.store.getBlockHeaderByArchive(archive);
1233
1541
  }
1542
+ getBlockByHash(blockHash) {
1543
+ return this.store.getBlockByHash(blockHash);
1544
+ }
1545
+ getBlockByArchive(archive) {
1546
+ return this.store.getBlockByArchive(archive);
1547
+ }
1548
+ getLatestBlockNumber() {
1549
+ return this.store.getLatestBlockNumber();
1550
+ }
1551
+ getBlocksForCheckpoint(checkpointNumber) {
1552
+ return this.store.getBlocksForCheckpoint(checkpointNumber);
1553
+ }
1234
1554
  getTxEffect(txHash) {
1235
1555
  return this.store.getTxEffect(txHash);
1236
1556
  }
@@ -1240,17 +1560,17 @@ var Operation = /*#__PURE__*/ function(Operation) {
1240
1560
  addL1ToL2Messages(messages) {
1241
1561
  return this.store.addL1ToL2Messages(messages);
1242
1562
  }
1243
- getL1ToL2Messages(blockNumber) {
1244
- return this.store.getL1ToL2Messages(blockNumber);
1563
+ getL1ToL2Messages(checkpointNumber) {
1564
+ return this.store.getL1ToL2Messages(checkpointNumber);
1245
1565
  }
1246
1566
  getL1ToL2MessageIndex(l1ToL2Message) {
1247
1567
  return this.store.getL1ToL2MessageIndex(l1ToL2Message);
1248
1568
  }
1249
- getPrivateLogs(from, limit) {
1250
- return this.store.getPrivateLogs(from, limit);
1569
+ getPrivateLogsByTags(tags) {
1570
+ return this.store.getPrivateLogsByTags(tags);
1251
1571
  }
1252
- getLogsByTags(tags, logsPerTag) {
1253
- return this.store.getLogsByTags(tags, logsPerTag);
1572
+ getPublicLogsByTagsFromContract(contractAddress, tags) {
1573
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
1254
1574
  }
1255
1575
  getPublicLogs(filter) {
1256
1576
  return this.store.getPublicLogs(filter);
@@ -1259,16 +1579,19 @@ var Operation = /*#__PURE__*/ function(Operation) {
1259
1579
  return this.store.getContractClassLogs(filter);
1260
1580
  }
1261
1581
  getSynchedL2BlockNumber() {
1262
- return this.store.getSynchedL2BlockNumber();
1582
+ return this.store.getCheckpointedL2BlockNumber();
1263
1583
  }
1264
- getProvenL2BlockNumber() {
1265
- return this.store.getProvenL2BlockNumber();
1584
+ getProvenCheckpointNumber() {
1585
+ return this.store.getProvenCheckpointNumber();
1586
+ }
1587
+ getProvenBlockNumber() {
1588
+ return this.store.getProvenBlockNumber();
1266
1589
  }
1267
- setProvenL2BlockNumber(l2BlockNumber) {
1268
- return this.store.setProvenL2BlockNumber(l2BlockNumber);
1590
+ setProvenCheckpointNumber(checkpointNumber) {
1591
+ return this.store.setProvenCheckpointNumber(checkpointNumber);
1269
1592
  }
1270
1593
  setBlockSynchedL1BlockNumber(l1BlockNumber) {
1271
- return this.store.setBlockSynchedL1BlockNumber(l1BlockNumber);
1594
+ return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
1272
1595
  }
1273
1596
  setMessageSynchedL1Block(l1Block) {
1274
1597
  return this.store.setMessageSynchedL1Block(l1Block);
@@ -1300,8 +1623,8 @@ var Operation = /*#__PURE__*/ function(Operation) {
1300
1623
  estimateSize() {
1301
1624
  return this.store.estimateSize();
1302
1625
  }
1303
- rollbackL1ToL2MessagesToL2Block(targetBlockNumber) {
1304
- return this.store.rollbackL1ToL2MessagesToL2Block(targetBlockNumber);
1626
+ rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber) {
1627
+ return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1305
1628
  }
1306
1629
  iterateL1ToL2Messages(range = {}) {
1307
1630
  return this.store.iterateL1ToL2Messages(range);